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" |
| 22 | #include "clang/Sema/ExternalSemaSource.h" |
| 23 | #include "clang/Sema/Lookup.h" |
| 24 | #include "clang/Sema/Overload.h" |
| 25 | #include "clang/Sema/Scope.h" |
| 26 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/DenseSet.h" |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallBitVector.h" |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallPtrSet.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | e6688e6 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/StringSwitch.h" |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 34 | #include <list> |
| 35 | #include <map> |
| 36 | #include <vector> |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace clang; |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 39 | using namespace sema; |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 40 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 41 | namespace { |
| 42 | /// \brief A container of code-completion results. |
| 43 | class ResultBuilder { |
| 44 | public: |
| 45 | /// \brief The type of a name-lookup filter, which can be provided to the |
| 46 | /// name-lookup routines to specify which declarations should be included in |
| 47 | /// the result set (when it returns true) and which declarations should be |
| 48 | /// filtered out (returns false). |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 49 | typedef bool (ResultBuilder::*LookupFilter)(const NamedDecl *) const; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 50 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 51 | typedef CodeCompletionResult Result; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 52 | |
| 53 | private: |
| 54 | /// \brief The actual results we have found. |
| 55 | std::vector<Result> Results; |
| 56 | |
| 57 | /// \brief A record of all of the declarations we have found and placed |
| 58 | /// into the result set, used to ensure that no declaration ever gets into |
| 59 | /// the result set twice. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 60 | llvm::SmallPtrSet<const Decl*, 16> AllDeclsFound; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 61 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 62 | typedef std::pair<const NamedDecl *, unsigned> DeclIndexPair; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 63 | |
| 64 | /// \brief An entry in the shadow map, which is optimized to store |
| 65 | /// a single (declaration, index) mapping (the common case) but |
| 66 | /// can also store a list of (declaration, index) mappings. |
| 67 | class ShadowMapEntry { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 68 | typedef SmallVector<DeclIndexPair, 4> DeclIndexPairVector; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 69 | |
| 70 | /// \brief Contains either the solitary NamedDecl * or a vector |
| 71 | /// of (declaration, index) pairs. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 72 | llvm::PointerUnion<const NamedDecl *, DeclIndexPairVector*> DeclOrVector; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 73 | |
| 74 | /// \brief When the entry contains a single declaration, this is |
| 75 | /// the index associated with that entry. |
| 76 | unsigned SingleDeclIndex; |
| 77 | |
| 78 | public: |
| 79 | ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { } |
| 80 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 81 | void Add(const NamedDecl *ND, unsigned Index) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 82 | if (DeclOrVector.isNull()) { |
| 83 | // 0 - > 1 elements: just set the single element information. |
| 84 | DeclOrVector = ND; |
| 85 | SingleDeclIndex = Index; |
| 86 | return; |
| 87 | } |
| 88 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 89 | if (const NamedDecl *PrevND = |
| 90 | DeclOrVector.dyn_cast<const NamedDecl *>()) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 91 | // 1 -> 2 elements: create the vector of results and push in the |
| 92 | // existing declaration. |
| 93 | DeclIndexPairVector *Vec = new DeclIndexPairVector; |
| 94 | Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex)); |
| 95 | DeclOrVector = Vec; |
| 96 | } |
| 97 | |
| 98 | // Add the new element to the end of the vector. |
| 99 | DeclOrVector.get<DeclIndexPairVector*>()->push_back( |
| 100 | DeclIndexPair(ND, Index)); |
| 101 | } |
| 102 | |
| 103 | void Destroy() { |
| 104 | if (DeclIndexPairVector *Vec |
| 105 | = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) { |
| 106 | delete Vec; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 107 | DeclOrVector = ((NamedDecl *)nullptr); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | // Iteration. |
| 112 | class iterator; |
| 113 | iterator begin() const; |
| 114 | iterator end() const; |
| 115 | }; |
| 116 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 117 | /// \brief A mapping from declaration names to the declarations that have |
| 118 | /// this name within a particular scope and their index within the list of |
| 119 | /// results. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 120 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 121 | |
| 122 | /// \brief The semantic analysis object for which results are being |
| 123 | /// produced. |
| 124 | Sema &SemaRef; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 125 | |
| 126 | /// \brief The allocator used to allocate new code-completion strings. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 127 | CodeCompletionAllocator &Allocator; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 128 | |
| 129 | CodeCompletionTUInfo &CCTUInfo; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 130 | |
| 131 | /// \brief If non-NULL, a filter function used to remove any code-completion |
| 132 | /// results that are not desirable. |
| 133 | LookupFilter Filter; |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 134 | |
| 135 | /// \brief Whether we should allow declarations as |
| 136 | /// nested-name-specifiers that would otherwise be filtered out. |
| 137 | bool AllowNestedNameSpecifiers; |
| 138 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 139 | /// \brief If set, the type that we would prefer our resulting value |
| 140 | /// declarations to have. |
| 141 | /// |
| 142 | /// Closely matching the preferred type gives a boost to a result's |
| 143 | /// priority. |
| 144 | CanQualType PreferredType; |
| 145 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 146 | /// \brief A list of shadow maps, which is used to model name hiding at |
| 147 | /// different levels of, e.g., the inheritance hierarchy. |
| 148 | std::list<ShadowMap> ShadowMaps; |
| 149 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 150 | /// \brief If we're potentially referring to a C++ member function, the set |
| 151 | /// of qualifiers applied to the object type. |
| 152 | Qualifiers ObjectTypeQualifiers; |
| 153 | |
| 154 | /// \brief Whether the \p ObjectTypeQualifiers field is active. |
| 155 | bool HasObjectTypeQualifiers; |
| 156 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 157 | /// \brief The selector that we prefer. |
| 158 | Selector PreferredSelector; |
| 159 | |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 160 | /// \brief The completion context in which we are gathering results. |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 161 | CodeCompletionContext CompletionContext; |
| 162 | |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 163 | /// \brief If we are in an instance method definition, the \@implementation |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 164 | /// object. |
| 165 | ObjCImplementationDecl *ObjCImplementation; |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 166 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 167 | void AdjustResultPriorityForDecl(Result &R); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 168 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 169 | void MaybeAddConstructorResults(Result R); |
| 170 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 171 | public: |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 172 | explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator, |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 173 | CodeCompletionTUInfo &CCTUInfo, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 174 | const CodeCompletionContext &CompletionContext, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 175 | LookupFilter Filter = nullptr) |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 176 | : SemaRef(SemaRef), Allocator(Allocator), CCTUInfo(CCTUInfo), |
| 177 | Filter(Filter), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 178 | AllowNestedNameSpecifiers(false), HasObjectTypeQualifiers(false), |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 179 | CompletionContext(CompletionContext), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 180 | ObjCImplementation(nullptr) |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 181 | { |
| 182 | // If this is an Objective-C instance method definition, dig out the |
| 183 | // corresponding implementation. |
| 184 | switch (CompletionContext.getKind()) { |
| 185 | case CodeCompletionContext::CCC_Expression: |
| 186 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
| 187 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
| 188 | case CodeCompletionContext::CCC_Statement: |
| 189 | case CodeCompletionContext::CCC_Recovery: |
| 190 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) |
| 191 | if (Method->isInstanceMethod()) |
| 192 | if (ObjCInterfaceDecl *Interface = Method->getClassInterface()) |
| 193 | ObjCImplementation = Interface->getImplementation(); |
| 194 | break; |
| 195 | |
| 196 | default: |
| 197 | break; |
| 198 | } |
| 199 | } |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 200 | |
| 201 | /// \brief Determine the priority for a reference to the given declaration. |
| 202 | unsigned getBasePriority(const NamedDecl *D); |
| 203 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 204 | /// \brief Whether we should include code patterns in the completion |
| 205 | /// results. |
| 206 | bool includeCodePatterns() const { |
| 207 | return SemaRef.CodeCompleter && |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 208 | SemaRef.CodeCompleter->includeCodePatterns(); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 211 | /// \brief Set the filter used for code-completion results. |
| 212 | void setFilter(LookupFilter Filter) { |
| 213 | this->Filter = Filter; |
| 214 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 215 | |
| 216 | Result *data() { return Results.empty()? nullptr : &Results.front(); } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 217 | unsigned size() const { return Results.size(); } |
| 218 | bool empty() const { return Results.empty(); } |
| 219 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 220 | /// \brief Specify the preferred type. |
| 221 | void setPreferredType(QualType T) { |
| 222 | PreferredType = SemaRef.Context.getCanonicalType(T); |
| 223 | } |
| 224 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 225 | /// \brief Set the cv-qualifiers on the object type, for us in filtering |
| 226 | /// calls to member functions. |
| 227 | /// |
| 228 | /// When there are qualifiers in this set, they will be used to filter |
| 229 | /// out member functions that aren't available (because there will be a |
| 230 | /// cv-qualifier mismatch) or prefer functions with an exact qualifier |
| 231 | /// match. |
| 232 | void setObjectTypeQualifiers(Qualifiers Quals) { |
| 233 | ObjectTypeQualifiers = Quals; |
| 234 | HasObjectTypeQualifiers = true; |
| 235 | } |
| 236 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 237 | /// \brief Set the preferred selector. |
| 238 | /// |
| 239 | /// When an Objective-C method declaration result is added, and that |
| 240 | /// method's selector matches this preferred selector, we give that method |
| 241 | /// a slight priority boost. |
| 242 | void setPreferredSelector(Selector Sel) { |
| 243 | PreferredSelector = Sel; |
| 244 | } |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 245 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 246 | /// \brief Retrieve the code-completion context for which results are |
| 247 | /// being collected. |
| 248 | const CodeCompletionContext &getCompletionContext() const { |
| 249 | return CompletionContext; |
| 250 | } |
| 251 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 252 | /// \brief Specify whether nested-name-specifiers are allowed. |
| 253 | void allowNestedNameSpecifiers(bool Allow = true) { |
| 254 | AllowNestedNameSpecifiers = Allow; |
| 255 | } |
| 256 | |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 257 | /// \brief Return the semantic analysis object for which we are collecting |
| 258 | /// code completion results. |
| 259 | Sema &getSema() const { return SemaRef; } |
| 260 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 261 | /// \brief Retrieve the allocator used to allocate code completion strings. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 262 | CodeCompletionAllocator &getAllocator() const { return Allocator; } |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 263 | |
| 264 | CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 265 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 266 | /// \brief Determine whether the given declaration is at all interesting |
| 267 | /// as a code-completion result. |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 268 | /// |
| 269 | /// \param ND the declaration that we are inspecting. |
| 270 | /// |
| 271 | /// \param AsNestedNameSpecifier will be set true if this declaration is |
| 272 | /// only interesting when it is a nested-name-specifier. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 273 | bool isInterestingDecl(const NamedDecl *ND, |
| 274 | bool &AsNestedNameSpecifier) const; |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 275 | |
| 276 | /// \brief Check whether the result is hidden by the Hiding declaration. |
| 277 | /// |
| 278 | /// \returns true if the result is hidden and cannot be found, false if |
| 279 | /// the hidden result could still be found. When false, \p R may be |
| 280 | /// modified to describe how the result can be found (e.g., via extra |
| 281 | /// qualification). |
| 282 | bool CheckHiddenResult(Result &R, DeclContext *CurContext, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 283 | const NamedDecl *Hiding); |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 284 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 285 | /// \brief Add a new result to this result set (if it isn't already in one |
| 286 | /// of the shadow maps), or replace an existing result (for, e.g., a |
| 287 | /// redeclaration). |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 288 | /// |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 289 | /// \param R the result to add (if it is unique). |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 290 | /// |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 291 | /// \param CurContext the context in which this result will be named. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 292 | void MaybeAddResult(Result R, DeclContext *CurContext = nullptr); |
| 293 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 294 | /// \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] | 295 | /// the hiding declaration (if any). |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 296 | /// |
| 297 | /// \param R the result to add (if it is unique). |
| 298 | /// |
| 299 | /// \param CurContext the context in which this result will be named. |
| 300 | /// |
| 301 | /// \param Hiding the declaration that hides the result. |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 302 | /// |
| 303 | /// \param InBaseClass whether the result was found in a base |
| 304 | /// class of the searched context. |
| 305 | void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding, |
| 306 | bool InBaseClass); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 307 | |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 308 | /// \brief Add a new non-declaration result to this result set. |
| 309 | void AddResult(Result R); |
| 310 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 311 | /// \brief Enter into a new scope. |
| 312 | void EnterNewScope(); |
| 313 | |
| 314 | /// \brief Exit from the current scope. |
| 315 | void ExitScope(); |
| 316 | |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 317 | /// \brief Ignore this declaration, if it is seen again. |
Dmitri Gribenko | 6cfb153 | 2013-02-14 13:53:30 +0000 | [diff] [blame] | 318 | void Ignore(const Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); } |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 319 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 320 | /// \name Name lookup predicates |
| 321 | /// |
| 322 | /// These predicates can be passed to the name lookup functions to filter the |
| 323 | /// results of name lookup. All of the predicates have the same type, so that |
| 324 | /// |
| 325 | //@{ |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 326 | bool IsOrdinaryName(const NamedDecl *ND) const; |
| 327 | bool IsOrdinaryNonTypeName(const NamedDecl *ND) const; |
| 328 | bool IsIntegralConstantValue(const NamedDecl *ND) const; |
| 329 | bool IsOrdinaryNonValueName(const NamedDecl *ND) const; |
| 330 | bool IsNestedNameSpecifier(const NamedDecl *ND) const; |
| 331 | bool IsEnum(const NamedDecl *ND) const; |
| 332 | bool IsClassOrStruct(const NamedDecl *ND) const; |
| 333 | bool IsUnion(const NamedDecl *ND) const; |
| 334 | bool IsNamespace(const NamedDecl *ND) const; |
| 335 | bool IsNamespaceOrAlias(const NamedDecl *ND) const; |
| 336 | bool IsType(const NamedDecl *ND) const; |
| 337 | bool IsMember(const NamedDecl *ND) const; |
| 338 | bool IsObjCIvar(const NamedDecl *ND) const; |
| 339 | bool IsObjCMessageReceiver(const NamedDecl *ND) const; |
| 340 | bool IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const; |
| 341 | bool IsObjCCollection(const NamedDecl *ND) const; |
| 342 | bool IsImpossibleToSatisfy(const NamedDecl *ND) const; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 343 | //@} |
| 344 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 345 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 346 | |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 347 | class ResultBuilder::ShadowMapEntry::iterator { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 348 | llvm::PointerUnion<const NamedDecl *, const DeclIndexPair *> DeclOrIterator; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 349 | unsigned SingleDeclIndex; |
| 350 | |
| 351 | public: |
| 352 | typedef DeclIndexPair value_type; |
| 353 | typedef value_type reference; |
| 354 | typedef std::ptrdiff_t difference_type; |
| 355 | typedef std::input_iterator_tag iterator_category; |
| 356 | |
| 357 | class pointer { |
| 358 | DeclIndexPair Value; |
| 359 | |
| 360 | public: |
| 361 | pointer(const DeclIndexPair &Value) : Value(Value) { } |
| 362 | |
| 363 | const DeclIndexPair *operator->() const { |
| 364 | return &Value; |
| 365 | } |
| 366 | }; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 367 | |
| 368 | iterator() : DeclOrIterator((NamedDecl *)nullptr), SingleDeclIndex(0) {} |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 369 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 370 | iterator(const NamedDecl *SingleDecl, unsigned Index) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 371 | : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { } |
| 372 | |
| 373 | iterator(const DeclIndexPair *Iterator) |
| 374 | : DeclOrIterator(Iterator), SingleDeclIndex(0) { } |
| 375 | |
| 376 | iterator &operator++() { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 377 | if (DeclOrIterator.is<const NamedDecl *>()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 378 | DeclOrIterator = (NamedDecl *)nullptr; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 379 | SingleDeclIndex = 0; |
| 380 | return *this; |
| 381 | } |
| 382 | |
| 383 | const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>(); |
| 384 | ++I; |
| 385 | DeclOrIterator = I; |
| 386 | return *this; |
| 387 | } |
| 388 | |
Chris Lattner | 9795b39 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 389 | /*iterator operator++(int) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 390 | iterator tmp(*this); |
| 391 | ++(*this); |
| 392 | return tmp; |
Chris Lattner | 9795b39 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 393 | }*/ |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 394 | |
| 395 | reference operator*() const { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 396 | if (const NamedDecl *ND = DeclOrIterator.dyn_cast<const NamedDecl *>()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 397 | return reference(ND, SingleDeclIndex); |
| 398 | |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 399 | return *DeclOrIterator.get<const DeclIndexPair*>(); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | pointer operator->() const { |
| 403 | return pointer(**this); |
| 404 | } |
| 405 | |
| 406 | friend bool operator==(const iterator &X, const iterator &Y) { |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 407 | return X.DeclOrIterator.getOpaqueValue() |
| 408 | == Y.DeclOrIterator.getOpaqueValue() && |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 409 | X.SingleDeclIndex == Y.SingleDeclIndex; |
| 410 | } |
| 411 | |
| 412 | friend bool operator!=(const iterator &X, const iterator &Y) { |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 413 | return !(X == Y); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 414 | } |
| 415 | }; |
| 416 | |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 417 | ResultBuilder::ShadowMapEntry::iterator |
| 418 | ResultBuilder::ShadowMapEntry::begin() const { |
| 419 | if (DeclOrVector.isNull()) |
| 420 | return iterator(); |
| 421 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 422 | if (const NamedDecl *ND = DeclOrVector.dyn_cast<const NamedDecl *>()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 423 | return iterator(ND, SingleDeclIndex); |
| 424 | |
| 425 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin()); |
| 426 | } |
| 427 | |
| 428 | ResultBuilder::ShadowMapEntry::iterator |
| 429 | ResultBuilder::ShadowMapEntry::end() const { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 430 | if (DeclOrVector.is<const NamedDecl *>() || DeclOrVector.isNull()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 431 | return iterator(); |
| 432 | |
| 433 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end()); |
| 434 | } |
| 435 | |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 436 | /// \brief Compute the qualification required to get from the current context |
| 437 | /// (\p CurContext) to the target context (\p TargetContext). |
| 438 | /// |
| 439 | /// \param Context the AST context in which the qualification will be used. |
| 440 | /// |
| 441 | /// \param CurContext the context where an entity is being named, which is |
| 442 | /// typically based on the current scope. |
| 443 | /// |
| 444 | /// \param TargetContext the context in which the named entity actually |
| 445 | /// resides. |
| 446 | /// |
| 447 | /// \returns a nested name specifier that refers into the target context, or |
| 448 | /// NULL if no qualification is needed. |
| 449 | static NestedNameSpecifier * |
| 450 | getRequiredQualification(ASTContext &Context, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 451 | const DeclContext *CurContext, |
| 452 | const DeclContext *TargetContext) { |
| 453 | SmallVector<const DeclContext *, 4> TargetParents; |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 454 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 455 | for (const DeclContext *CommonAncestor = TargetContext; |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 456 | CommonAncestor && !CommonAncestor->Encloses(CurContext); |
| 457 | CommonAncestor = CommonAncestor->getLookupParent()) { |
| 458 | if (CommonAncestor->isTransparentContext() || |
| 459 | CommonAncestor->isFunctionOrMethod()) |
| 460 | continue; |
| 461 | |
| 462 | TargetParents.push_back(CommonAncestor); |
| 463 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 464 | |
| 465 | NestedNameSpecifier *Result = nullptr; |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 466 | while (!TargetParents.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 467 | const DeclContext *Parent = TargetParents.pop_back_val(); |
| 468 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 469 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) { |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 470 | if (!Namespace->getIdentifier()) |
| 471 | continue; |
| 472 | |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 473 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 474 | } |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 475 | else if (const TagDecl *TD = dyn_cast<TagDecl>(Parent)) |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 476 | Result = NestedNameSpecifier::Create(Context, Result, |
| 477 | false, |
| 478 | Context.getTypeDeclType(TD).getTypePtr()); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 479 | } |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 480 | return Result; |
| 481 | } |
| 482 | |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 483 | /// Determine whether \p Id is a name reserved for the implementation (C99 |
| 484 | /// 7.1.3, C++ [lib.global.names]). |
| 485 | static bool isReservedName(const IdentifierInfo *Id) { |
| 486 | if (Id->getLength() < 2) |
| 487 | return false; |
| 488 | const char *Name = Id->getNameStart(); |
| 489 | return Name[0] == '_' && |
| 490 | (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z')); |
| 491 | } |
| 492 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 493 | bool ResultBuilder::isInterestingDecl(const NamedDecl *ND, |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 494 | bool &AsNestedNameSpecifier) const { |
| 495 | AsNestedNameSpecifier = false; |
| 496 | |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 497 | auto *Named = ND; |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 498 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 499 | |
| 500 | // Skip unnamed entities. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 501 | if (!ND->getDeclName()) |
| 502 | return false; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 503 | |
| 504 | // Friend declarations and declarations introduced due to friends are never |
| 505 | // added as results. |
Richard Smith | 6eece29 | 2015-01-15 02:27:20 +0000 | [diff] [blame] | 506 | if (ND->getFriendObjectKind() == Decl::FOK_Undeclared) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 507 | return false; |
| 508 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 509 | // Class template (partial) specializations are never added as results. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 510 | if (isa<ClassTemplateSpecializationDecl>(ND) || |
| 511 | isa<ClassTemplatePartialSpecializationDecl>(ND)) |
| 512 | return false; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 513 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 514 | // Using declarations themselves are never added as results. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 515 | if (isa<UsingDecl>(ND)) |
| 516 | return false; |
| 517 | |
| 518 | // Some declarations have reserved names that we don't want to ever show. |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 519 | // Filter out names reserved for the implementation if they come from a |
| 520 | // system header. |
| 521 | // TODO: Add a predicate for this. |
| 522 | if (const IdentifierInfo *Id = ND->getIdentifier()) |
| 523 | if (isReservedName(Id) && |
| 524 | (ND->getLocation().isInvalid() || |
| 525 | SemaRef.SourceMgr.isInSystemHeader( |
| 526 | SemaRef.SourceMgr.getSpellingLoc(ND->getLocation())))) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 527 | return false; |
Douglas Gregor | 2927c0c | 2010-11-09 03:59:40 +0000 | [diff] [blame] | 528 | |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 529 | if (Filter == &ResultBuilder::IsNestedNameSpecifier || |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 530 | (isa<NamespaceDecl>(ND) && |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 531 | Filter != &ResultBuilder::IsNamespace && |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 532 | Filter != &ResultBuilder::IsNamespaceOrAlias && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 533 | Filter != nullptr)) |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 534 | AsNestedNameSpecifier = true; |
| 535 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 536 | // Filter out any unwanted results. |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 537 | if (Filter && !(this->*Filter)(Named)) { |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 538 | // Check whether it is interesting as a nested-name-specifier. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 539 | if (AllowNestedNameSpecifiers && SemaRef.getLangOpts().CPlusPlus && |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 540 | IsNestedNameSpecifier(ND) && |
| 541 | (Filter != &ResultBuilder::IsMember || |
| 542 | (isa<CXXRecordDecl>(ND) && |
| 543 | cast<CXXRecordDecl>(ND)->isInjectedClassName()))) { |
| 544 | AsNestedNameSpecifier = true; |
| 545 | return true; |
| 546 | } |
| 547 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 548 | return false; |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 549 | } |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 550 | // ... then it must be interesting! |
| 551 | return true; |
| 552 | } |
| 553 | |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 554 | bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 555 | const NamedDecl *Hiding) { |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 556 | // In C, there is no way to refer to a hidden name. |
| 557 | // FIXME: This isn't true; we can find a tag name hidden by an ordinary |
| 558 | // name if we introduce the tag type. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 559 | if (!SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 560 | return true; |
| 561 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 562 | const DeclContext *HiddenCtx = |
| 563 | R.Declaration->getDeclContext()->getRedeclContext(); |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 564 | |
| 565 | // There is no way to qualify a name declared in a function or method. |
| 566 | if (HiddenCtx->isFunctionOrMethod()) |
| 567 | return true; |
| 568 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 569 | if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext()) |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 570 | return true; |
| 571 | |
| 572 | // We can refer to the result with the appropriate qualification. Do it. |
| 573 | R.Hidden = true; |
| 574 | R.QualifierIsInformative = false; |
| 575 | |
| 576 | if (!R.Qualifier) |
| 577 | R.Qualifier = getRequiredQualification(SemaRef.Context, |
| 578 | CurContext, |
| 579 | R.Declaration->getDeclContext()); |
| 580 | return false; |
| 581 | } |
| 582 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 583 | /// \brief A simplified classification of types used to determine whether two |
| 584 | /// types are "similar enough" when adjusting priorities. |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 585 | SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 586 | switch (T->getTypeClass()) { |
| 587 | case Type::Builtin: |
| 588 | switch (cast<BuiltinType>(T)->getKind()) { |
| 589 | case BuiltinType::Void: |
| 590 | return STC_Void; |
| 591 | |
| 592 | case BuiltinType::NullPtr: |
| 593 | return STC_Pointer; |
| 594 | |
| 595 | case BuiltinType::Overload: |
| 596 | case BuiltinType::Dependent: |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 597 | return STC_Other; |
| 598 | |
| 599 | case BuiltinType::ObjCId: |
| 600 | case BuiltinType::ObjCClass: |
| 601 | case BuiltinType::ObjCSel: |
| 602 | return STC_ObjectiveC; |
| 603 | |
| 604 | default: |
| 605 | return STC_Arithmetic; |
| 606 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 607 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 608 | case Type::Complex: |
| 609 | return STC_Arithmetic; |
| 610 | |
| 611 | case Type::Pointer: |
| 612 | return STC_Pointer; |
| 613 | |
| 614 | case Type::BlockPointer: |
| 615 | return STC_Block; |
| 616 | |
| 617 | case Type::LValueReference: |
| 618 | case Type::RValueReference: |
| 619 | return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType()); |
| 620 | |
| 621 | case Type::ConstantArray: |
| 622 | case Type::IncompleteArray: |
| 623 | case Type::VariableArray: |
| 624 | case Type::DependentSizedArray: |
| 625 | return STC_Array; |
| 626 | |
| 627 | case Type::DependentSizedExtVector: |
| 628 | case Type::Vector: |
| 629 | case Type::ExtVector: |
| 630 | return STC_Arithmetic; |
| 631 | |
| 632 | case Type::FunctionProto: |
| 633 | case Type::FunctionNoProto: |
| 634 | return STC_Function; |
| 635 | |
| 636 | case Type::Record: |
| 637 | return STC_Record; |
| 638 | |
| 639 | case Type::Enum: |
| 640 | return STC_Arithmetic; |
| 641 | |
| 642 | case Type::ObjCObject: |
| 643 | case Type::ObjCInterface: |
| 644 | case Type::ObjCObjectPointer: |
| 645 | return STC_ObjectiveC; |
| 646 | |
| 647 | default: |
| 648 | return STC_Other; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /// \brief Get the type that a given expression will have if this declaration |
| 653 | /// is used as an expression in its "typical" code-completion form. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 654 | QualType clang::getDeclUsageType(ASTContext &C, const NamedDecl *ND) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 655 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 656 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 657 | if (const TypeDecl *Type = dyn_cast<TypeDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 658 | return C.getTypeDeclType(Type); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 659 | if (const ObjCInterfaceDecl *Iface = dyn_cast<ObjCInterfaceDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 660 | return C.getObjCInterfaceType(Iface); |
| 661 | |
| 662 | QualType T; |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 663 | if (const FunctionDecl *Function = ND->getAsFunction()) |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 664 | T = Function->getCallResultType(); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 665 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 666 | T = Method->getSendResultType(); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 667 | else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 668 | T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext())); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 669 | else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 670 | T = Property->getType(); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 671 | else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 672 | T = Value->getType(); |
| 673 | else |
| 674 | return QualType(); |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 675 | |
| 676 | // Dig through references, function pointers, and block pointers to |
| 677 | // get down to the likely type of an expression when the entity is |
| 678 | // used. |
| 679 | do { |
| 680 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) { |
| 681 | T = Ref->getPointeeType(); |
| 682 | continue; |
| 683 | } |
| 684 | |
| 685 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 686 | if (Pointer->getPointeeType()->isFunctionType()) { |
| 687 | T = Pointer->getPointeeType(); |
| 688 | continue; |
| 689 | } |
| 690 | |
| 691 | break; |
| 692 | } |
| 693 | |
| 694 | if (const BlockPointerType *Block = T->getAs<BlockPointerType>()) { |
| 695 | T = Block->getPointeeType(); |
| 696 | continue; |
| 697 | } |
| 698 | |
| 699 | if (const FunctionType *Function = T->getAs<FunctionType>()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 700 | T = Function->getReturnType(); |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 701 | continue; |
| 702 | } |
| 703 | |
| 704 | break; |
| 705 | } while (true); |
| 706 | |
| 707 | return T; |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 710 | unsigned ResultBuilder::getBasePriority(const NamedDecl *ND) { |
| 711 | if (!ND) |
| 712 | return CCP_Unlikely; |
| 713 | |
| 714 | // Context-based decisions. |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 715 | const DeclContext *LexicalDC = ND->getLexicalDeclContext(); |
| 716 | if (LexicalDC->isFunctionOrMethod()) { |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 717 | // _cmd is relatively rare |
| 718 | if (const ImplicitParamDecl *ImplicitParam = |
| 719 | dyn_cast<ImplicitParamDecl>(ND)) |
| 720 | if (ImplicitParam->getIdentifier() && |
| 721 | ImplicitParam->getIdentifier()->isStr("_cmd")) |
| 722 | return CCP_ObjC_cmd; |
| 723 | |
| 724 | return CCP_LocalDeclaration; |
| 725 | } |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 726 | |
| 727 | const DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 728 | if (DC->isRecord() || isa<ObjCContainerDecl>(DC)) |
| 729 | return CCP_MemberDeclaration; |
| 730 | |
| 731 | // Content-based decisions. |
| 732 | if (isa<EnumConstantDecl>(ND)) |
| 733 | return CCP_Constant; |
| 734 | |
Douglas Gregor | 52e0de4 | 2013-01-31 05:03:46 +0000 | [diff] [blame] | 735 | // Use CCP_Type for type declarations unless we're in a statement, Objective-C |
| 736 | // message receiver, or parenthesized expression context. There, it's as |
| 737 | // likely that the user will want to write a type as other declarations. |
| 738 | if ((isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) && |
| 739 | !(CompletionContext.getKind() == CodeCompletionContext::CCC_Statement || |
| 740 | CompletionContext.getKind() |
| 741 | == CodeCompletionContext::CCC_ObjCMessageReceiver || |
| 742 | CompletionContext.getKind() |
| 743 | == CodeCompletionContext::CCC_ParenthesizedExpression)) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 744 | return CCP_Type; |
| 745 | |
| 746 | return CCP_Declaration; |
| 747 | } |
| 748 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 749 | void ResultBuilder::AdjustResultPriorityForDecl(Result &R) { |
| 750 | // If this is an Objective-C method declaration whose selector matches our |
| 751 | // preferred selector, give it a priority boost. |
| 752 | if (!PreferredSelector.isNull()) |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 753 | if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration)) |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 754 | if (PreferredSelector == Method->getSelector()) |
| 755 | R.Priority += CCD_SelectorMatch; |
Douglas Gregor | 5fb901d | 2010-09-20 23:11:55 +0000 | [diff] [blame] | 756 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 757 | // If we have a preferred type, adjust the priority for results with exactly- |
| 758 | // matching or nearly-matching types. |
| 759 | if (!PreferredType.isNull()) { |
| 760 | QualType T = getDeclUsageType(SemaRef.Context, R.Declaration); |
| 761 | if (!T.isNull()) { |
| 762 | CanQualType TC = SemaRef.Context.getCanonicalType(T); |
| 763 | // Check for exactly-matching types (modulo qualifiers). |
| 764 | if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) |
| 765 | R.Priority /= CCF_ExactTypeMatch; |
| 766 | // Check for nearly-matching types, based on classification of each. |
| 767 | else if ((getSimplifiedTypeClass(PreferredType) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 768 | == getSimplifiedTypeClass(TC)) && |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 769 | !(PreferredType->isEnumeralType() && TC->isEnumeralType())) |
| 770 | R.Priority /= CCF_SimilarTypeMatch; |
| 771 | } |
| 772 | } |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 775 | void ResultBuilder::MaybeAddConstructorResults(Result R) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 776 | if (!SemaRef.getLangOpts().CPlusPlus || !R.Declaration || |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 777 | !CompletionContext.wantConstructorResults()) |
| 778 | return; |
| 779 | |
| 780 | ASTContext &Context = SemaRef.Context; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 781 | const NamedDecl *D = R.Declaration; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 782 | const CXXRecordDecl *Record = nullptr; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 783 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 784 | Record = ClassTemplate->getTemplatedDecl(); |
| 785 | else if ((Record = dyn_cast<CXXRecordDecl>(D))) { |
| 786 | // Skip specializations and partial specializations. |
| 787 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 788 | return; |
| 789 | } else { |
| 790 | // There are no constructors here. |
| 791 | return; |
| 792 | } |
| 793 | |
| 794 | Record = Record->getDefinition(); |
| 795 | if (!Record) |
| 796 | return; |
| 797 | |
| 798 | |
| 799 | QualType RecordTy = Context.getTypeDeclType(Record); |
| 800 | DeclarationName ConstructorName |
| 801 | = Context.DeclarationNames.getCXXConstructorName( |
| 802 | Context.getCanonicalType(RecordTy)); |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 803 | DeclContext::lookup_result Ctors = Record->lookup(ConstructorName); |
| 804 | for (DeclContext::lookup_iterator I = Ctors.begin(), |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 805 | E = Ctors.end(); |
| 806 | I != E; ++I) { |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 807 | R.Declaration = *I; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 808 | R.CursorKind = getCursorKindForDecl(R.Declaration); |
| 809 | Results.push_back(R); |
| 810 | } |
| 811 | } |
| 812 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 813 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { |
| 814 | assert(!ShadowMaps.empty() && "Must enter into a results scope"); |
| 815 | |
| 816 | if (R.Kind != Result::RK_Declaration) { |
| 817 | // For non-declaration results, just add the result. |
| 818 | Results.push_back(R); |
| 819 | return; |
| 820 | } |
| 821 | |
| 822 | // Look through using declarations. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 823 | if (const UsingShadowDecl *Using = |
| 824 | dyn_cast<UsingShadowDecl>(R.Declaration)) { |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 825 | MaybeAddResult(Result(Using->getTargetDecl(), |
| 826 | getBasePriority(Using->getTargetDecl()), |
| 827 | R.Qualifier), |
| 828 | CurContext); |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 829 | return; |
| 830 | } |
| 831 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 832 | const Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 833 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 834 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 835 | bool AsNestedNameSpecifier = false; |
| 836 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 837 | return; |
| 838 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 839 | // C++ constructors are never found by name lookup. |
| 840 | if (isa<CXXConstructorDecl>(R.Declaration)) |
| 841 | return; |
| 842 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 843 | ShadowMap &SMap = ShadowMaps.back(); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 844 | ShadowMapEntry::iterator I, IEnd; |
| 845 | ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName()); |
| 846 | if (NamePos != SMap.end()) { |
| 847 | I = NamePos->second.begin(); |
| 848 | IEnd = NamePos->second.end(); |
| 849 | } |
| 850 | |
| 851 | for (; I != IEnd; ++I) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 852 | const NamedDecl *ND = I->first; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 853 | unsigned Index = I->second; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 854 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 855 | // This is a redeclaration. Always pick the newer declaration. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 856 | Results[Index].Declaration = R.Declaration; |
| 857 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 858 | // We're done. |
| 859 | return; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | // This is a new declaration in this scope. However, check whether this |
| 864 | // declaration name is hidden by a similarly-named declaration in an outer |
| 865 | // scope. |
| 866 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 867 | --SMEnd; |
| 868 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 869 | ShadowMapEntry::iterator I, IEnd; |
| 870 | ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName()); |
| 871 | if (NamePos != SM->end()) { |
| 872 | I = NamePos->second.begin(); |
| 873 | IEnd = NamePos->second.end(); |
| 874 | } |
| 875 | for (; I != IEnd; ++I) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 876 | // A tag declaration does not hide a non-tag declaration. |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 877 | if (I->first->hasTagIdentifierNamespace() && |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 878 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 879 | Decl::IDNS_LocalExtern | Decl::IDNS_ObjCProtocol))) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 880 | continue; |
| 881 | |
| 882 | // Protocols are in distinct namespaces from everything else. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 883 | if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 884 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 885 | I->first->getIdentifierNamespace() != IDNS) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 886 | continue; |
| 887 | |
| 888 | // 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] | 889 | if (CheckHiddenResult(R, CurContext, I->first)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 890 | return; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 891 | |
| 892 | break; |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | // 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] | 897 | if (!AllDeclsFound.insert(CanonDecl).second) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 898 | return; |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 899 | |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 900 | // If the filter is for nested-name-specifiers, then this result starts a |
| 901 | // nested-name-specifier. |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 902 | if (AsNestedNameSpecifier) { |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 903 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 904 | R.Priority = CCP_NestedNameSpecifier; |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 905 | } else |
| 906 | AdjustResultPriorityForDecl(R); |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 907 | |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 908 | // If this result is supposed to have an informative qualifier, add one. |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 909 | if (R.QualifierIsInformative && !R.Qualifier && |
| 910 | !R.StartsNestedNameSpecifier) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 911 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 912 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 913 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 914 | Namespace); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 915 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 916 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 917 | false, SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 918 | else |
| 919 | R.QualifierIsInformative = false; |
| 920 | } |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 921 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 922 | // Insert this result into the set of results and into the current shadow |
| 923 | // map. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 924 | SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 925 | Results.push_back(R); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 926 | |
| 927 | if (!AsNestedNameSpecifier) |
| 928 | MaybeAddConstructorResults(R); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 931 | void ResultBuilder::AddResult(Result R, DeclContext *CurContext, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 932 | NamedDecl *Hiding, bool InBaseClass = false) { |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 933 | if (R.Kind != Result::RK_Declaration) { |
| 934 | // For non-declaration results, just add the result. |
| 935 | Results.push_back(R); |
| 936 | return; |
| 937 | } |
| 938 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 939 | // Look through using declarations. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 940 | if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 941 | AddResult(Result(Using->getTargetDecl(), |
| 942 | getBasePriority(Using->getTargetDecl()), |
| 943 | R.Qualifier), |
| 944 | CurContext, Hiding); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 945 | return; |
| 946 | } |
| 947 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 948 | bool AsNestedNameSpecifier = false; |
| 949 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 950 | return; |
| 951 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 952 | // C++ constructors are never found by name lookup. |
| 953 | if (isa<CXXConstructorDecl>(R.Declaration)) |
| 954 | return; |
| 955 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 956 | if (Hiding && CheckHiddenResult(R, CurContext, Hiding)) |
| 957 | return; |
Nick Lewycky | c392148 | 2012-04-03 21:44:08 +0000 | [diff] [blame] | 958 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 959 | // 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] | 960 | if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()).second) |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 961 | return; |
| 962 | |
| 963 | // If the filter is for nested-name-specifiers, then this result starts a |
| 964 | // nested-name-specifier. |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 965 | if (AsNestedNameSpecifier) { |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 966 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 967 | R.Priority = CCP_NestedNameSpecifier; |
| 968 | } |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 969 | else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass && |
| 970 | isa<CXXRecordDecl>(R.Declaration->getDeclContext() |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 971 | ->getRedeclContext())) |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 972 | R.QualifierIsInformative = true; |
| 973 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 974 | // If this result is supposed to have an informative qualifier, add one. |
| 975 | if (R.QualifierIsInformative && !R.Qualifier && |
| 976 | !R.StartsNestedNameSpecifier) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 977 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 978 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 979 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 980 | Namespace); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 981 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 982 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, false, |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 983 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 984 | else |
| 985 | R.QualifierIsInformative = false; |
| 986 | } |
| 987 | |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 988 | // Adjust the priority if this result comes from a base class. |
| 989 | if (InBaseClass) |
| 990 | R.Priority += CCD_InBaseClass; |
| 991 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 992 | AdjustResultPriorityForDecl(R); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 993 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 994 | if (HasObjectTypeQualifiers) |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 995 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(R.Declaration)) |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 996 | if (Method->isInstance()) { |
| 997 | Qualifiers MethodQuals |
| 998 | = Qualifiers::fromCVRMask(Method->getTypeQualifiers()); |
| 999 | if (ObjectTypeQualifiers == MethodQuals) |
| 1000 | R.Priority += CCD_ObjectQualifierMatch; |
| 1001 | else if (ObjectTypeQualifiers - MethodQuals) { |
| 1002 | // The method cannot be invoked, because doing so would drop |
| 1003 | // qualifiers. |
| 1004 | return; |
| 1005 | } |
| 1006 | } |
| 1007 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1008 | // Insert this result into the set of results. |
| 1009 | Results.push_back(R); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1010 | |
| 1011 | if (!AsNestedNameSpecifier) |
| 1012 | MaybeAddConstructorResults(R); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1015 | void ResultBuilder::AddResult(Result R) { |
| 1016 | assert(R.Kind != Result::RK_Declaration && |
| 1017 | "Declaration results need more context"); |
| 1018 | Results.push_back(R); |
| 1019 | } |
| 1020 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1021 | /// \brief Enter into a new scope. |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1022 | void ResultBuilder::EnterNewScope() { ShadowMaps.emplace_back(); } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1023 | |
| 1024 | /// \brief Exit from the current scope. |
| 1025 | void ResultBuilder::ExitScope() { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1026 | for (ShadowMap::iterator E = ShadowMaps.back().begin(), |
| 1027 | EEnd = ShadowMaps.back().end(); |
| 1028 | E != EEnd; |
| 1029 | ++E) |
| 1030 | E->second.Destroy(); |
| 1031 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1032 | ShadowMaps.pop_back(); |
| 1033 | } |
| 1034 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1035 | /// \brief Determines whether this given declaration will be found by |
| 1036 | /// ordinary name lookup. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1037 | bool ResultBuilder::IsOrdinaryName(const NamedDecl *ND) const { |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1038 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 1039 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1040 | // If name lookup finds a local extern declaration, then we are in a |
| 1041 | // context where it behaves like an ordinary name. |
| 1042 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1043 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 9858ed5 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1044 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1045 | else if (SemaRef.getLangOpts().ObjC1) { |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1046 | if (isa<ObjCIvarDecl>(ND)) |
| 1047 | return true; |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1050 | return ND->getIdentifierNamespace() & IDNS; |
| 1051 | } |
| 1052 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1053 | /// \brief Determines whether this given declaration will be found by |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1054 | /// ordinary name lookup but is not a type name. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1055 | bool ResultBuilder::IsOrdinaryNonTypeName(const NamedDecl *ND) const { |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1056 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 1057 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) |
| 1058 | return false; |
| 1059 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1060 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1061 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 9858ed5 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1062 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1063 | else if (SemaRef.getLangOpts().ObjC1) { |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1064 | if (isa<ObjCIvarDecl>(ND)) |
| 1065 | return true; |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1068 | return ND->getIdentifierNamespace() & IDNS; |
| 1069 | } |
| 1070 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1071 | bool ResultBuilder::IsIntegralConstantValue(const NamedDecl *ND) const { |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1072 | if (!IsOrdinaryNonTypeName(ND)) |
| 1073 | return 0; |
| 1074 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1075 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl())) |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1076 | if (VD->getType()->isIntegralOrEnumerationType()) |
| 1077 | return true; |
| 1078 | |
| 1079 | return false; |
| 1080 | } |
| 1081 | |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1082 | /// \brief Determines whether this given declaration will be found by |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1083 | /// ordinary name lookup. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1084 | bool ResultBuilder::IsOrdinaryNonValueName(const NamedDecl *ND) const { |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1085 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 1086 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1087 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1088 | if (SemaRef.getLangOpts().CPlusPlus) |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1089 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1090 | |
| 1091 | return (ND->getIdentifierNamespace() & IDNS) && |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1092 | !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) && |
| 1093 | !isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1096 | /// \brief Determines whether the given declaration is suitable as the |
| 1097 | /// 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] | 1098 | bool ResultBuilder::IsNestedNameSpecifier(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1099 | // Allow us to find class templates, too. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1100 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1101 | ND = ClassTemplate->getTemplatedDecl(); |
| 1102 | |
| 1103 | return SemaRef.isAcceptableNestedNameSpecifier(ND); |
| 1104 | } |
| 1105 | |
| 1106 | /// \brief Determines whether the given declaration is an enumeration. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1107 | bool ResultBuilder::IsEnum(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1108 | return isa<EnumDecl>(ND); |
| 1109 | } |
| 1110 | |
| 1111 | /// \brief Determines whether the given declaration is a class or struct. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1112 | bool ResultBuilder::IsClassOrStruct(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1113 | // Allow us to find class templates, too. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1114 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1115 | ND = ClassTemplate->getTemplatedDecl(); |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1116 | |
| 1117 | // For purposes of this check, interfaces match too. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1118 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1119 | return RD->getTagKind() == TTK_Class || |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1120 | RD->getTagKind() == TTK_Struct || |
| 1121 | RD->getTagKind() == TTK_Interface; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1122 | |
| 1123 | return false; |
| 1124 | } |
| 1125 | |
| 1126 | /// \brief Determines whether the given declaration is a union. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1127 | bool ResultBuilder::IsUnion(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1128 | // Allow us to find class templates, too. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1129 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1130 | ND = ClassTemplate->getTemplatedDecl(); |
| 1131 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1132 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1133 | return RD->getTagKind() == TTK_Union; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1134 | |
| 1135 | return false; |
| 1136 | } |
| 1137 | |
| 1138 | /// \brief Determines whether the given declaration is a namespace. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1139 | bool ResultBuilder::IsNamespace(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1140 | return isa<NamespaceDecl>(ND); |
| 1141 | } |
| 1142 | |
| 1143 | /// \brief Determines whether the given declaration is a namespace or |
| 1144 | /// namespace alias. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1145 | bool ResultBuilder::IsNamespaceOrAlias(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1146 | return isa<NamespaceDecl>(ND->getUnderlyingDecl()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1149 | /// \brief Determines whether the given declaration is a type. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1150 | bool ResultBuilder::IsType(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1151 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1152 | return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1155 | /// \brief Determines which members of a class should be visible via |
| 1156 | /// "." or "->". Only value declarations, nested name specifiers, and |
| 1157 | /// using declarations thereof should show up. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1158 | bool ResultBuilder::IsMember(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1159 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 7078839 | 2009-12-11 18:14:22 +0000 | [diff] [blame] | 1160 | return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) || |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1161 | isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1164 | static bool isObjCReceiverType(ASTContext &C, QualType T) { |
| 1165 | T = C.getCanonicalType(T); |
| 1166 | switch (T->getTypeClass()) { |
| 1167 | case Type::ObjCObject: |
| 1168 | case Type::ObjCInterface: |
| 1169 | case Type::ObjCObjectPointer: |
| 1170 | return true; |
| 1171 | |
| 1172 | case Type::Builtin: |
| 1173 | switch (cast<BuiltinType>(T)->getKind()) { |
| 1174 | case BuiltinType::ObjCId: |
| 1175 | case BuiltinType::ObjCClass: |
| 1176 | case BuiltinType::ObjCSel: |
| 1177 | return true; |
| 1178 | |
| 1179 | default: |
| 1180 | break; |
| 1181 | } |
| 1182 | return false; |
| 1183 | |
| 1184 | default: |
| 1185 | break; |
| 1186 | } |
| 1187 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1188 | if (!C.getLangOpts().CPlusPlus) |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1189 | return false; |
| 1190 | |
| 1191 | // FIXME: We could perform more analysis here to determine whether a |
| 1192 | // particular class type has any conversions to Objective-C types. For now, |
| 1193 | // just accept all class types. |
| 1194 | return T->isDependentType() || T->isRecordType(); |
| 1195 | } |
| 1196 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1197 | bool ResultBuilder::IsObjCMessageReceiver(const NamedDecl *ND) const { |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1198 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1199 | if (T.isNull()) |
| 1200 | return false; |
| 1201 | |
| 1202 | T = SemaRef.Context.getBaseElementType(T); |
| 1203 | return isObjCReceiverType(SemaRef.Context, T); |
| 1204 | } |
| 1205 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1206 | bool ResultBuilder::IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const { |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1207 | if (IsObjCMessageReceiver(ND)) |
| 1208 | return true; |
| 1209 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1210 | const VarDecl *Var = dyn_cast<VarDecl>(ND); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1211 | if (!Var) |
| 1212 | return false; |
| 1213 | |
| 1214 | return Var->hasLocalStorage() && !Var->hasAttr<BlocksAttr>(); |
| 1215 | } |
| 1216 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1217 | bool ResultBuilder::IsObjCCollection(const NamedDecl *ND) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1218 | if ((SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryName(ND)) || |
| 1219 | (!SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryNonTypeName(ND))) |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1220 | return false; |
| 1221 | |
| 1222 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1223 | if (T.isNull()) |
| 1224 | return false; |
| 1225 | |
| 1226 | T = SemaRef.Context.getBaseElementType(T); |
| 1227 | return T->isObjCObjectType() || T->isObjCObjectPointerType() || |
| 1228 | T->isObjCIdType() || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1229 | (SemaRef.getLangOpts().CPlusPlus && T->isRecordType()); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1230 | } |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1231 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1232 | bool ResultBuilder::IsImpossibleToSatisfy(const NamedDecl *ND) const { |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1233 | return false; |
| 1234 | } |
| 1235 | |
James Dennett | f124387 | 2012-06-17 05:33:25 +0000 | [diff] [blame] | 1236 | /// \brief Determines whether the given declaration is an Objective-C |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1237 | /// instance variable. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1238 | bool ResultBuilder::IsObjCIvar(const NamedDecl *ND) const { |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1239 | return isa<ObjCIvarDecl>(ND); |
| 1240 | } |
| 1241 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1242 | namespace { |
| 1243 | /// \brief Visible declaration consumer that adds a code-completion result |
| 1244 | /// for each visible declaration. |
| 1245 | class CodeCompletionDeclConsumer : public VisibleDeclConsumer { |
| 1246 | ResultBuilder &Results; |
| 1247 | DeclContext *CurContext; |
| 1248 | |
| 1249 | public: |
| 1250 | CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext) |
| 1251 | : Results(Results), CurContext(CurContext) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1252 | |
| 1253 | void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx, |
| 1254 | bool InBaseClass) override { |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1255 | bool Accessible = true; |
Douglas Gregor | 03ba188 | 2011-11-03 16:51:37 +0000 | [diff] [blame] | 1256 | if (Ctx) |
| 1257 | Accessible = Results.getSema().IsSimplyAccessible(ND, Ctx); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1258 | |
| 1259 | ResultBuilder::Result Result(ND, Results.getBasePriority(ND), nullptr, |
| 1260 | false, Accessible); |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1261 | Results.AddResult(Result, CurContext, Hiding, InBaseClass); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1262 | } |
| 1263 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1264 | } |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1265 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1266 | /// \brief Add type specifiers for the current language as keyword results. |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1267 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1268 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1269 | typedef CodeCompletionResult Result; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1270 | Results.AddResult(Result("short", CCP_Type)); |
| 1271 | Results.AddResult(Result("long", CCP_Type)); |
| 1272 | Results.AddResult(Result("signed", CCP_Type)); |
| 1273 | Results.AddResult(Result("unsigned", CCP_Type)); |
| 1274 | Results.AddResult(Result("void", CCP_Type)); |
| 1275 | Results.AddResult(Result("char", CCP_Type)); |
| 1276 | Results.AddResult(Result("int", CCP_Type)); |
| 1277 | Results.AddResult(Result("float", CCP_Type)); |
| 1278 | Results.AddResult(Result("double", CCP_Type)); |
| 1279 | Results.AddResult(Result("enum", CCP_Type)); |
| 1280 | Results.AddResult(Result("struct", CCP_Type)); |
| 1281 | Results.AddResult(Result("union", CCP_Type)); |
| 1282 | Results.AddResult(Result("const", CCP_Type)); |
| 1283 | Results.AddResult(Result("volatile", CCP_Type)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1284 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1285 | if (LangOpts.C99) { |
| 1286 | // C99-specific |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1287 | Results.AddResult(Result("_Complex", CCP_Type)); |
| 1288 | Results.AddResult(Result("_Imaginary", CCP_Type)); |
| 1289 | Results.AddResult(Result("_Bool", CCP_Type)); |
| 1290 | Results.AddResult(Result("restrict", CCP_Type)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1293 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 1294 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1295 | if (LangOpts.CPlusPlus) { |
| 1296 | // C++-specific |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 1297 | Results.AddResult(Result("bool", CCP_Type + |
| 1298 | (LangOpts.ObjC1? CCD_bool_in_ObjC : 0))); |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1299 | Results.AddResult(Result("class", CCP_Type)); |
| 1300 | Results.AddResult(Result("wchar_t", CCP_Type)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1301 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1302 | // typename qualified-id |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1303 | Builder.AddTypedTextChunk("typename"); |
| 1304 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1305 | Builder.AddPlaceholderChunk("qualifier"); |
| 1306 | Builder.AddTextChunk("::"); |
| 1307 | Builder.AddPlaceholderChunk("name"); |
| 1308 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1309 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1310 | if (LangOpts.CPlusPlus11) { |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1311 | Results.AddResult(Result("auto", CCP_Type)); |
| 1312 | Results.AddResult(Result("char16_t", CCP_Type)); |
| 1313 | Results.AddResult(Result("char32_t", CCP_Type)); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1314 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1315 | Builder.AddTypedTextChunk("decltype"); |
| 1316 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1317 | Builder.AddPlaceholderChunk("expression"); |
| 1318 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1319 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | // GNU extensions |
| 1324 | if (LangOpts.GNUMode) { |
| 1325 | // FIXME: Enable when we actually support decimal floating point. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1326 | // Results.AddResult(Result("_Decimal32")); |
| 1327 | // Results.AddResult(Result("_Decimal64")); |
| 1328 | // Results.AddResult(Result("_Decimal128")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1329 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1330 | Builder.AddTypedTextChunk("typeof"); |
| 1331 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1332 | Builder.AddPlaceholderChunk("expression"); |
| 1333 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1334 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1335 | Builder.AddTypedTextChunk("typeof"); |
| 1336 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1337 | Builder.AddPlaceholderChunk("type"); |
| 1338 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1339 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1340 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 1341 | |
| 1342 | // Nullability |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 1343 | Results.AddResult(Result("_Nonnull", CCP_Type)); |
| 1344 | Results.AddResult(Result("_Null_unspecified", CCP_Type)); |
| 1345 | Results.AddResult(Result("_Nullable", CCP_Type)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1348 | static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1349 | const LangOptions &LangOpts, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1350 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1351 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1352 | // Note: we don't suggest either "auto" or "register", because both |
| 1353 | // are pointless as storage specifiers. Elsewhere, we suggest "auto" |
| 1354 | // in C++0x as a type specifier. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1355 | Results.AddResult(Result("extern")); |
| 1356 | Results.AddResult(Result("static")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1359 | static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1360 | const LangOptions &LangOpts, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1361 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1362 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1363 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1364 | case Sema::PCC_Class: |
| 1365 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1366 | if (LangOpts.CPlusPlus) { |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1367 | Results.AddResult(Result("explicit")); |
| 1368 | Results.AddResult(Result("friend")); |
| 1369 | Results.AddResult(Result("mutable")); |
| 1370 | Results.AddResult(Result("virtual")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1371 | } |
| 1372 | // Fall through |
| 1373 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1374 | case Sema::PCC_ObjCInterface: |
| 1375 | case Sema::PCC_ObjCImplementation: |
| 1376 | case Sema::PCC_Namespace: |
| 1377 | case Sema::PCC_Template: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1378 | if (LangOpts.CPlusPlus || LangOpts.C99) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1379 | Results.AddResult(Result("inline")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1380 | break; |
| 1381 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1382 | case Sema::PCC_ObjCInstanceVariableList: |
| 1383 | case Sema::PCC_Expression: |
| 1384 | case Sema::PCC_Statement: |
| 1385 | case Sema::PCC_ForInit: |
| 1386 | case Sema::PCC_Condition: |
| 1387 | case Sema::PCC_RecoveryInFunction: |
| 1388 | case Sema::PCC_Type: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1389 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1390 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1391 | break; |
| 1392 | } |
| 1393 | } |
| 1394 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1395 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); |
| 1396 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); |
| 1397 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1398 | ResultBuilder &Results, |
| 1399 | bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1400 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1401 | ResultBuilder &Results, |
| 1402 | bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1403 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1404 | ResultBuilder &Results, |
| 1405 | bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1406 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1407 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1408 | static void AddTypedefResult(ResultBuilder &Results) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1409 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 1410 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1411 | Builder.AddTypedTextChunk("typedef"); |
| 1412 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1413 | Builder.AddPlaceholderChunk("type"); |
| 1414 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1415 | Builder.AddPlaceholderChunk("name"); |
| 1416 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1419 | static bool WantTypesInContext(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1420 | const LangOptions &LangOpts) { |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1421 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1422 | case Sema::PCC_Namespace: |
| 1423 | case Sema::PCC_Class: |
| 1424 | case Sema::PCC_ObjCInstanceVariableList: |
| 1425 | case Sema::PCC_Template: |
| 1426 | case Sema::PCC_MemberTemplate: |
| 1427 | case Sema::PCC_Statement: |
| 1428 | case Sema::PCC_RecoveryInFunction: |
| 1429 | case Sema::PCC_Type: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1430 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1431 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1432 | return true; |
| 1433 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1434 | case Sema::PCC_Expression: |
| 1435 | case Sema::PCC_Condition: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1436 | return LangOpts.CPlusPlus; |
| 1437 | |
| 1438 | case Sema::PCC_ObjCInterface: |
| 1439 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1440 | return false; |
| 1441 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1442 | case Sema::PCC_ForInit: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1443 | return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99; |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1444 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1445 | |
| 1446 | llvm_unreachable("Invalid ParserCompletionContext!"); |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 1449 | static PrintingPolicy getCompletionPrintingPolicy(const ASTContext &Context, |
| 1450 | const Preprocessor &PP) { |
| 1451 | PrintingPolicy Policy = Sema::getPrintingPolicy(Context, PP); |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1452 | Policy.AnonymousTagLocations = false; |
| 1453 | Policy.SuppressStrongLifetime = true; |
Douglas Gregor | 2e10cf9 | 2011-11-03 00:16:13 +0000 | [diff] [blame] | 1454 | Policy.SuppressUnwrittenScope = true; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1455 | return Policy; |
| 1456 | } |
| 1457 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 1458 | /// \brief Retrieve a printing policy suitable for code completion. |
| 1459 | static PrintingPolicy getCompletionPrintingPolicy(Sema &S) { |
| 1460 | return getCompletionPrintingPolicy(S.Context, S.PP); |
| 1461 | } |
| 1462 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1463 | /// \brief Retrieve the string representation of the given type as a string |
| 1464 | /// that has the appropriate lifetime for code completion. |
| 1465 | /// |
| 1466 | /// This routine provides a fast path where we provide constant strings for |
| 1467 | /// common type names. |
| 1468 | static const char *GetCompletionTypeString(QualType T, |
| 1469 | ASTContext &Context, |
| 1470 | const PrintingPolicy &Policy, |
| 1471 | CodeCompletionAllocator &Allocator) { |
| 1472 | if (!T.getLocalQualifiers()) { |
| 1473 | // Built-in type names are constant strings. |
| 1474 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) |
Argyrios Kyrtzidis | bbff3da | 2012-05-05 04:20:28 +0000 | [diff] [blame] | 1475 | return BT->getNameAsCString(Policy); |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1476 | |
| 1477 | // Anonymous tag types are constant strings. |
| 1478 | if (const TagType *TagT = dyn_cast<TagType>(T)) |
| 1479 | if (TagDecl *Tag = TagT->getDecl()) |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 1480 | if (!Tag->hasNameForLinkage()) { |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1481 | switch (Tag->getTagKind()) { |
| 1482 | case TTK_Struct: return "struct <anonymous>"; |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1483 | case TTK_Interface: return "__interface <anonymous>"; |
| 1484 | case TTK_Class: return "class <anonymous>"; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1485 | case TTK_Union: return "union <anonymous>"; |
| 1486 | case TTK_Enum: return "enum <anonymous>"; |
| 1487 | } |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | // Slow path: format the type as a string. |
| 1492 | std::string Result; |
| 1493 | T.getAsStringInternal(Result, Policy); |
| 1494 | return Allocator.CopyString(Result); |
| 1495 | } |
| 1496 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1497 | /// \brief Add a completion for "this", if we're in a member function. |
| 1498 | static void addThisCompletion(Sema &S, ResultBuilder &Results) { |
| 1499 | QualType ThisTy = S.getCurrentThisType(); |
| 1500 | if (ThisTy.isNull()) |
| 1501 | return; |
| 1502 | |
| 1503 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1504 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1505 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
| 1506 | Builder.AddResultTypeChunk(GetCompletionTypeString(ThisTy, |
| 1507 | S.Context, |
| 1508 | Policy, |
| 1509 | Allocator)); |
| 1510 | Builder.AddTypedTextChunk("this"); |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1511 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1514 | /// \brief Add language constructs that show up for "ordinary" names. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1515 | static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1516 | Scope *S, |
| 1517 | Sema &SemaRef, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1518 | ResultBuilder &Results) { |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1519 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1520 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1521 | PrintingPolicy Policy = getCompletionPrintingPolicy(SemaRef); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1522 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1523 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1524 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1525 | case Sema::PCC_Namespace: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1526 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1527 | if (Results.includeCodePatterns()) { |
| 1528 | // namespace <identifier> { declarations } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1529 | Builder.AddTypedTextChunk("namespace"); |
| 1530 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1531 | Builder.AddPlaceholderChunk("identifier"); |
| 1532 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1533 | Builder.AddPlaceholderChunk("declarations"); |
| 1534 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1535 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1536 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1539 | // namespace identifier = identifier ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1540 | Builder.AddTypedTextChunk("namespace"); |
| 1541 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1542 | Builder.AddPlaceholderChunk("name"); |
| 1543 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
| 1544 | Builder.AddPlaceholderChunk("namespace"); |
| 1545 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1546 | |
| 1547 | // Using directives |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1548 | Builder.AddTypedTextChunk("using"); |
| 1549 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1550 | Builder.AddTextChunk("namespace"); |
| 1551 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1552 | Builder.AddPlaceholderChunk("identifier"); |
| 1553 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1554 | |
| 1555 | // asm(string-literal) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1556 | Builder.AddTypedTextChunk("asm"); |
| 1557 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1558 | Builder.AddPlaceholderChunk("string-literal"); |
| 1559 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1560 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1561 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1562 | if (Results.includeCodePatterns()) { |
| 1563 | // Explicit template instantiation |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1564 | Builder.AddTypedTextChunk("template"); |
| 1565 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1566 | Builder.AddPlaceholderChunk("declaration"); |
| 1567 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1568 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1569 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1570 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1571 | if (SemaRef.getLangOpts().ObjC1) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1572 | AddObjCTopLevelResults(Results, true); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1573 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1574 | AddTypedefResult(Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1575 | // Fall through |
| 1576 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1577 | case Sema::PCC_Class: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1578 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1579 | // Using declaration |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1580 | Builder.AddTypedTextChunk("using"); |
| 1581 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1582 | Builder.AddPlaceholderChunk("qualifier"); |
| 1583 | Builder.AddTextChunk("::"); |
| 1584 | Builder.AddPlaceholderChunk("name"); |
| 1585 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1586 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1587 | // using typename qualifier::name (only in a dependent context) |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1588 | if (SemaRef.CurContext->isDependentContext()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1589 | Builder.AddTypedTextChunk("using"); |
| 1590 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1591 | Builder.AddTextChunk("typename"); |
| 1592 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1593 | Builder.AddPlaceholderChunk("qualifier"); |
| 1594 | Builder.AddTextChunk("::"); |
| 1595 | Builder.AddPlaceholderChunk("name"); |
| 1596 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1599 | if (CCC == Sema::PCC_Class) { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1600 | AddTypedefResult(Results); |
| 1601 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1602 | // public: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1603 | Builder.AddTypedTextChunk("public"); |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1604 | if (Results.includeCodePatterns()) |
| 1605 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1606 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1607 | |
| 1608 | // protected: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1609 | Builder.AddTypedTextChunk("protected"); |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1610 | if (Results.includeCodePatterns()) |
| 1611 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1612 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1613 | |
| 1614 | // private: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1615 | Builder.AddTypedTextChunk("private"); |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1616 | if (Results.includeCodePatterns()) |
| 1617 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1618 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1619 | } |
| 1620 | } |
| 1621 | // Fall through |
| 1622 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1623 | case Sema::PCC_Template: |
| 1624 | case Sema::PCC_MemberTemplate: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1625 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns()) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1626 | // template < parameters > |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1627 | Builder.AddTypedTextChunk("template"); |
| 1628 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1629 | Builder.AddPlaceholderChunk("parameters"); |
| 1630 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1631 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1634 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1635 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1636 | break; |
| 1637 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1638 | case Sema::PCC_ObjCInterface: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1639 | AddObjCInterfaceResults(SemaRef.getLangOpts(), Results, true); |
| 1640 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1641 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1642 | break; |
| 1643 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1644 | case Sema::PCC_ObjCImplementation: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1645 | AddObjCImplementationResults(SemaRef.getLangOpts(), Results, true); |
| 1646 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1647 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1648 | break; |
| 1649 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1650 | case Sema::PCC_ObjCInstanceVariableList: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1651 | AddObjCVisibilityResults(SemaRef.getLangOpts(), Results, true); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1652 | break; |
| 1653 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1654 | case Sema::PCC_RecoveryInFunction: |
| 1655 | case Sema::PCC_Statement: { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1656 | AddTypedefResult(Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1657 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1658 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns() && |
| 1659 | SemaRef.getLangOpts().CXXExceptions) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1660 | Builder.AddTypedTextChunk("try"); |
| 1661 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1662 | Builder.AddPlaceholderChunk("statements"); |
| 1663 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1664 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1665 | Builder.AddTextChunk("catch"); |
| 1666 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1667 | Builder.AddPlaceholderChunk("declaration"); |
| 1668 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1669 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1670 | Builder.AddPlaceholderChunk("statements"); |
| 1671 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1672 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1673 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1674 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1675 | if (SemaRef.getLangOpts().ObjC1) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1676 | AddObjCStatementResults(Results, true); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1677 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1678 | if (Results.includeCodePatterns()) { |
| 1679 | // if (condition) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1680 | Builder.AddTypedTextChunk("if"); |
| 1681 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1682 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1683 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1684 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1685 | Builder.AddPlaceholderChunk("expression"); |
| 1686 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1687 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1688 | Builder.AddPlaceholderChunk("statements"); |
| 1689 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1690 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1691 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1692 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1693 | // switch (condition) { } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1694 | Builder.AddTypedTextChunk("switch"); |
| 1695 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1696 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1697 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1698 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1699 | Builder.AddPlaceholderChunk("expression"); |
| 1700 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1701 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1702 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1703 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1704 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1707 | // Switch-specific statements. |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 1708 | if (!SemaRef.getCurFunction()->SwitchStack.empty()) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1709 | // case expression: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1710 | Builder.AddTypedTextChunk("case"); |
| 1711 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1712 | Builder.AddPlaceholderChunk("expression"); |
| 1713 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1714 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1715 | |
| 1716 | // default: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1717 | Builder.AddTypedTextChunk("default"); |
| 1718 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1719 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1722 | if (Results.includeCodePatterns()) { |
| 1723 | /// while (condition) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1724 | Builder.AddTypedTextChunk("while"); |
| 1725 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1726 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1727 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1728 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1729 | Builder.AddPlaceholderChunk("expression"); |
| 1730 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1731 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1732 | Builder.AddPlaceholderChunk("statements"); |
| 1733 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1734 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1735 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1736 | |
| 1737 | // do { statements } while ( expression ); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1738 | Builder.AddTypedTextChunk("do"); |
| 1739 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1740 | Builder.AddPlaceholderChunk("statements"); |
| 1741 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1742 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1743 | Builder.AddTextChunk("while"); |
| 1744 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1745 | Builder.AddPlaceholderChunk("expression"); |
| 1746 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1747 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1748 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1749 | // for ( for-init-statement ; condition ; expression ) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1750 | Builder.AddTypedTextChunk("for"); |
| 1751 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1752 | if (SemaRef.getLangOpts().CPlusPlus || SemaRef.getLangOpts().C99) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1753 | Builder.AddPlaceholderChunk("init-statement"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1754 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1755 | Builder.AddPlaceholderChunk("init-expression"); |
| 1756 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1757 | Builder.AddPlaceholderChunk("condition"); |
| 1758 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1759 | Builder.AddPlaceholderChunk("inc-expression"); |
| 1760 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1761 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1762 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1763 | Builder.AddPlaceholderChunk("statements"); |
| 1764 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1765 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1766 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1767 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1768 | |
| 1769 | if (S->getContinueParent()) { |
| 1770 | // continue ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1771 | Builder.AddTypedTextChunk("continue"); |
| 1772 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | if (S->getBreakParent()) { |
| 1776 | // break ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1777 | Builder.AddTypedTextChunk("break"); |
| 1778 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | // "return expression ;" or "return ;", depending on whether we |
| 1782 | // know the function is void or not. |
| 1783 | bool isVoid = false; |
| 1784 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1785 | isVoid = Function->getReturnType()->isVoidType(); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1786 | else if (ObjCMethodDecl *Method |
| 1787 | = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1788 | isVoid = Method->getReturnType()->isVoidType(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1789 | else if (SemaRef.getCurBlock() && |
| 1790 | !SemaRef.getCurBlock()->ReturnType.isNull()) |
| 1791 | isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType(); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1792 | Builder.AddTypedTextChunk("return"); |
Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1793 | if (!isVoid) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1794 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1795 | Builder.AddPlaceholderChunk("expression"); |
Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1796 | } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1797 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1798 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1799 | // goto identifier ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1800 | Builder.AddTypedTextChunk("goto"); |
| 1801 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1802 | Builder.AddPlaceholderChunk("label"); |
| 1803 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1804 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1805 | // Using directives |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1806 | Builder.AddTypedTextChunk("using"); |
| 1807 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1808 | Builder.AddTextChunk("namespace"); |
| 1809 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1810 | Builder.AddPlaceholderChunk("identifier"); |
| 1811 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | // Fall through (for statement expressions). |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1815 | case Sema::PCC_ForInit: |
| 1816 | case Sema::PCC_Condition: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1817 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1818 | // Fall through: conditions and statements can have expressions. |
| 1819 | |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1820 | case Sema::PCC_ParenthesizedExpression: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1821 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1822 | CCC == Sema::PCC_ParenthesizedExpression) { |
| 1823 | // (__bridge <type>)<expression> |
| 1824 | Builder.AddTypedTextChunk("__bridge"); |
| 1825 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1826 | Builder.AddPlaceholderChunk("type"); |
| 1827 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1828 | Builder.AddPlaceholderChunk("expression"); |
| 1829 | Results.AddResult(Result(Builder.TakeString())); |
| 1830 | |
| 1831 | // (__bridge_transfer <Objective-C type>)<expression> |
| 1832 | Builder.AddTypedTextChunk("__bridge_transfer"); |
| 1833 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1834 | Builder.AddPlaceholderChunk("Objective-C type"); |
| 1835 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1836 | Builder.AddPlaceholderChunk("expression"); |
| 1837 | Results.AddResult(Result(Builder.TakeString())); |
| 1838 | |
| 1839 | // (__bridge_retained <CF type>)<expression> |
| 1840 | Builder.AddTypedTextChunk("__bridge_retained"); |
| 1841 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1842 | Builder.AddPlaceholderChunk("CF type"); |
| 1843 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1844 | Builder.AddPlaceholderChunk("expression"); |
| 1845 | Results.AddResult(Result(Builder.TakeString())); |
| 1846 | } |
| 1847 | // Fall through |
| 1848 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1849 | case Sema::PCC_Expression: { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1850 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1851 | // 'this', if we're in a non-static member function. |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1852 | addThisCompletion(SemaRef, Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1853 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1854 | // true |
| 1855 | Builder.AddResultTypeChunk("bool"); |
| 1856 | Builder.AddTypedTextChunk("true"); |
| 1857 | Results.AddResult(Result(Builder.TakeString())); |
| 1858 | |
| 1859 | // false |
| 1860 | Builder.AddResultTypeChunk("bool"); |
| 1861 | Builder.AddTypedTextChunk("false"); |
| 1862 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1863 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1864 | if (SemaRef.getLangOpts().RTTI) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1865 | // dynamic_cast < type-id > ( expression ) |
| 1866 | Builder.AddTypedTextChunk("dynamic_cast"); |
| 1867 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1868 | Builder.AddPlaceholderChunk("type"); |
| 1869 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1870 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1871 | Builder.AddPlaceholderChunk("expression"); |
| 1872 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1873 | Results.AddResult(Result(Builder.TakeString())); |
| 1874 | } |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1875 | |
| 1876 | // static_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1877 | Builder.AddTypedTextChunk("static_cast"); |
| 1878 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1879 | Builder.AddPlaceholderChunk("type"); |
| 1880 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1881 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1882 | Builder.AddPlaceholderChunk("expression"); |
| 1883 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1884 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1885 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1886 | // reinterpret_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1887 | Builder.AddTypedTextChunk("reinterpret_cast"); |
| 1888 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1889 | Builder.AddPlaceholderChunk("type"); |
| 1890 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1891 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1892 | Builder.AddPlaceholderChunk("expression"); |
| 1893 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1894 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1895 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1896 | // const_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1897 | Builder.AddTypedTextChunk("const_cast"); |
| 1898 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1899 | Builder.AddPlaceholderChunk("type"); |
| 1900 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1901 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1902 | Builder.AddPlaceholderChunk("expression"); |
| 1903 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1904 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1905 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1906 | if (SemaRef.getLangOpts().RTTI) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1907 | // typeid ( expression-or-type ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1908 | Builder.AddResultTypeChunk("std::type_info"); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1909 | Builder.AddTypedTextChunk("typeid"); |
| 1910 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1911 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 1912 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1913 | Results.AddResult(Result(Builder.TakeString())); |
| 1914 | } |
| 1915 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1916 | // new T ( ... ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1917 | Builder.AddTypedTextChunk("new"); |
| 1918 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1919 | Builder.AddPlaceholderChunk("type"); |
| 1920 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1921 | Builder.AddPlaceholderChunk("expressions"); |
| 1922 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1923 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1924 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1925 | // new T [ ] ( ... ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1926 | Builder.AddTypedTextChunk("new"); |
| 1927 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1928 | Builder.AddPlaceholderChunk("type"); |
| 1929 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1930 | Builder.AddPlaceholderChunk("size"); |
| 1931 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 1932 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1933 | Builder.AddPlaceholderChunk("expressions"); |
| 1934 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1935 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1936 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1937 | // delete expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1938 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1939 | Builder.AddTypedTextChunk("delete"); |
| 1940 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1941 | Builder.AddPlaceholderChunk("expression"); |
| 1942 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1943 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1944 | // delete [] expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1945 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1946 | Builder.AddTypedTextChunk("delete"); |
| 1947 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1948 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1949 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 1950 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1951 | Builder.AddPlaceholderChunk("expression"); |
| 1952 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1953 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1954 | if (SemaRef.getLangOpts().CXXExceptions) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1955 | // throw expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1956 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1957 | Builder.AddTypedTextChunk("throw"); |
| 1958 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1959 | Builder.AddPlaceholderChunk("expression"); |
| 1960 | Results.AddResult(Result(Builder.TakeString())); |
| 1961 | } |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1962 | |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1963 | // FIXME: Rethrow? |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1964 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1965 | if (SemaRef.getLangOpts().CPlusPlus11) { |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1966 | // nullptr |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1967 | Builder.AddResultTypeChunk("std::nullptr_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1968 | Builder.AddTypedTextChunk("nullptr"); |
| 1969 | Results.AddResult(Result(Builder.TakeString())); |
| 1970 | |
| 1971 | // alignof |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1972 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1973 | Builder.AddTypedTextChunk("alignof"); |
| 1974 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1975 | Builder.AddPlaceholderChunk("type"); |
| 1976 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1977 | Results.AddResult(Result(Builder.TakeString())); |
| 1978 | |
| 1979 | // noexcept |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1980 | Builder.AddResultTypeChunk("bool"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1981 | Builder.AddTypedTextChunk("noexcept"); |
| 1982 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1983 | Builder.AddPlaceholderChunk("expression"); |
| 1984 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1985 | Results.AddResult(Result(Builder.TakeString())); |
| 1986 | |
| 1987 | // sizeof... expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1988 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1989 | Builder.AddTypedTextChunk("sizeof..."); |
| 1990 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1991 | Builder.AddPlaceholderChunk("parameter-pack"); |
| 1992 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1993 | Results.AddResult(Result(Builder.TakeString())); |
| 1994 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1997 | if (SemaRef.getLangOpts().ObjC1) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1998 | // 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] | 1999 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
| 2000 | // The interface can be NULL. |
| 2001 | if (ObjCInterfaceDecl *ID = Method->getClassInterface()) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2002 | if (ID->getSuperClass()) { |
| 2003 | std::string SuperType; |
| 2004 | SuperType = ID->getSuperClass()->getNameAsString(); |
| 2005 | if (Method->isInstanceMethod()) |
| 2006 | SuperType += " *"; |
| 2007 | |
| 2008 | Builder.AddResultTypeChunk(Allocator.CopyString(SuperType)); |
| 2009 | Builder.AddTypedTextChunk("super"); |
| 2010 | Results.AddResult(Result(Builder.TakeString())); |
| 2011 | } |
Ted Kremenek | 305a0a7 | 2010-05-31 21:43:10 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2014 | AddObjCExpressionResults(Results, true); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2017 | if (SemaRef.getLangOpts().C11) { |
| 2018 | // _Alignof |
| 2019 | Builder.AddResultTypeChunk("size_t"); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2020 | if (SemaRef.PP.isMacroDefined("alignof")) |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2021 | Builder.AddTypedTextChunk("alignof"); |
| 2022 | else |
| 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 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2030 | // sizeof expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2031 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2032 | Builder.AddTypedTextChunk("sizeof"); |
| 2033 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2034 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 2035 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2036 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2037 | break; |
| 2038 | } |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 2039 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2040 | case Sema::PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2041 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 2042 | break; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2045 | if (WantTypesInContext(CCC, SemaRef.getLangOpts())) |
| 2046 | AddTypeSpecifierResults(SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2047 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2048 | if (SemaRef.getLangOpts().CPlusPlus && CCC != Sema::PCC_Type) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2049 | Results.AddResult(Result("operator")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2052 | /// \brief If the given declaration has an associated type, add it as a result |
| 2053 | /// type chunk. |
| 2054 | static void AddResultTypeChunk(ASTContext &Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2055 | const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2056 | const NamedDecl *ND, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2057 | QualType BaseType, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2058 | CodeCompletionBuilder &Result) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2059 | if (!ND) |
| 2060 | return; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2061 | |
| 2062 | // Skip constructors and conversion functions, which have their return types |
| 2063 | // built into their names. |
| 2064 | if (isa<CXXConstructorDecl>(ND) || isa<CXXConversionDecl>(ND)) |
| 2065 | return; |
| 2066 | |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2067 | // Determine the type of the declaration (if it has a type). |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 2068 | QualType T; |
| 2069 | if (const FunctionDecl *Function = ND->getAsFunction()) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2070 | T = Function->getReturnType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2071 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
| 2072 | if (!BaseType.isNull()) |
| 2073 | T = Method->getSendResultType(BaseType); |
| 2074 | else |
| 2075 | T = Method->getReturnType(); |
| 2076 | } else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2077 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); |
| 2078 | else if (isa<UnresolvedUsingValueDecl>(ND)) { |
| 2079 | /* Do nothing: ignore unresolved using declarations*/ |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2080 | } else if (const ObjCIvarDecl *Ivar = dyn_cast<ObjCIvarDecl>(ND)) { |
| 2081 | if (!BaseType.isNull()) |
| 2082 | T = Ivar->getUsageType(BaseType); |
| 2083 | else |
| 2084 | T = Ivar->getType(); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2085 | } else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND)) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2086 | T = Value->getType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2087 | } else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) { |
| 2088 | if (!BaseType.isNull()) |
| 2089 | T = Property->getUsageType(BaseType); |
| 2090 | else |
| 2091 | T = Property->getType(); |
| 2092 | } |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2093 | |
| 2094 | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) |
| 2095 | return; |
| 2096 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2097 | Result.AddResultTypeChunk(GetCompletionTypeString(T, Context, Policy, |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2098 | Result.getAllocator())); |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2101 | static void MaybeAddSentinel(Preprocessor &PP, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2102 | const NamedDecl *FunctionOrMethod, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2103 | CodeCompletionBuilder &Result) { |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2104 | if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) |
| 2105 | if (Sentinel->getSentinel() == 0) { |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2106 | if (PP.getLangOpts().ObjC1 && PP.isMacroDefined("nil")) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2107 | Result.AddTextChunk(", nil"); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2108 | else if (PP.isMacroDefined("NULL")) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2109 | Result.AddTextChunk(", NULL"); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2110 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2111 | Result.AddTextChunk(", (void*)0"); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2112 | } |
| 2113 | } |
| 2114 | |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2115 | static std::string formatObjCParamQualifiers(unsigned ObjCQuals, |
| 2116 | QualType &Type) { |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2117 | std::string Result; |
| 2118 | if (ObjCQuals & Decl::OBJC_TQ_In) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2119 | Result += "in "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2120 | else if (ObjCQuals & Decl::OBJC_TQ_Inout) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2121 | Result += "inout "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2122 | else if (ObjCQuals & Decl::OBJC_TQ_Out) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2123 | Result += "out "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2124 | if (ObjCQuals & Decl::OBJC_TQ_Bycopy) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2125 | Result += "bycopy "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2126 | else if (ObjCQuals & Decl::OBJC_TQ_Byref) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2127 | Result += "byref "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2128 | if (ObjCQuals & Decl::OBJC_TQ_Oneway) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2129 | Result += "oneway "; |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2130 | if (ObjCQuals & Decl::OBJC_TQ_CSNullability) { |
| 2131 | if (auto nullability = AttributedType::stripOuterNullability(Type)) { |
| 2132 | switch (*nullability) { |
| 2133 | case NullabilityKind::NonNull: |
| 2134 | Result += "nonnull "; |
| 2135 | break; |
| 2136 | |
| 2137 | case NullabilityKind::Nullable: |
| 2138 | Result += "nullable "; |
| 2139 | break; |
| 2140 | |
| 2141 | case NullabilityKind::Unspecified: |
| 2142 | Result += "null_unspecified "; |
| 2143 | break; |
| 2144 | } |
| 2145 | } |
| 2146 | } |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2147 | return Result; |
| 2148 | } |
| 2149 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2150 | static std::string FormatFunctionParameter(const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2151 | const ParmVarDecl *Param, |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2152 | bool SuppressName = false, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2153 | bool SuppressBlock = false, |
| 2154 | Optional<ArrayRef<QualType>> ObjCSubsts = None) { |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2155 | bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext()); |
| 2156 | if (Param->getType()->isDependentType() || |
| 2157 | !Param->getType()->isBlockPointerType()) { |
| 2158 | // The argument for a dependent or non-block parameter is a placeholder |
| 2159 | // containing that parameter's type. |
| 2160 | std::string Result; |
| 2161 | |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2162 | if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName) |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2163 | Result = Param->getIdentifier()->getName(); |
| 2164 | |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2165 | QualType Type = Param->getType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2166 | if (ObjCSubsts) |
| 2167 | Type = Type.substObjCTypeArgs(Param->getASTContext(), *ObjCSubsts, |
| 2168 | ObjCSubstitutionContext::Parameter); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2169 | if (ObjCMethodParam) { |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2170 | Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier(), |
| 2171 | Type); |
| 2172 | Result += Type.getAsString(Policy) + ")"; |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2173 | if (Param->getIdentifier() && !SuppressName) |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2174 | Result += Param->getIdentifier()->getName(); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2175 | } else { |
| 2176 | Type.getAsStringInternal(Result, Policy); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2177 | } |
| 2178 | return Result; |
| 2179 | } |
| 2180 | |
| 2181 | // The argument for a block pointer parameter is a block literal with |
| 2182 | // the appropriate type. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2183 | FunctionTypeLoc Block; |
| 2184 | FunctionProtoTypeLoc BlockProto; |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2185 | TypeLoc TL; |
| 2186 | if (TypeSourceInfo *TSInfo = Param->getTypeSourceInfo()) { |
| 2187 | TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 2188 | while (true) { |
| 2189 | // Look through typedefs. |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2190 | if (!SuppressBlock) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2191 | if (TypedefTypeLoc TypedefTL = TL.getAs<TypedefTypeLoc>()) { |
| 2192 | if (TypeSourceInfo *InnerTSInfo = |
| 2193 | TypedefTL.getTypedefNameDecl()->getTypeSourceInfo()) { |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2194 | TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 2195 | continue; |
| 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | // Look through qualified types |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2200 | if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>()) { |
| 2201 | TL = QualifiedTL.getUnqualifiedLoc(); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2202 | continue; |
| 2203 | } |
Douglas Gregor | 4c850f3 | 2015-07-07 06:20:22 +0000 | [diff] [blame] | 2204 | |
| 2205 | if (AttributedTypeLoc AttrTL = TL.getAs<AttributedTypeLoc>()) { |
| 2206 | TL = AttrTL.getModifiedLoc(); |
| 2207 | continue; |
| 2208 | } |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2211 | // Try to get the function prototype behind the block pointer type, |
| 2212 | // then we're done. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2213 | if (BlockPointerTypeLoc BlockPtr = TL.getAs<BlockPointerTypeLoc>()) { |
| 2214 | TL = BlockPtr.getPointeeLoc().IgnoreParens(); |
| 2215 | Block = TL.getAs<FunctionTypeLoc>(); |
| 2216 | BlockProto = TL.getAs<FunctionProtoTypeLoc>(); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2217 | } |
| 2218 | break; |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | if (!Block) { |
| 2223 | // We were unable to find a FunctionProtoTypeLoc with parameter names |
| 2224 | // for the block; just use the parameter type as a placeholder. |
| 2225 | std::string Result; |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2226 | if (!ObjCMethodParam && Param->getIdentifier()) |
| 2227 | Result = Param->getIdentifier()->getName(); |
| 2228 | |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2229 | QualType Type = Param->getType().getUnqualifiedType(); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2230 | |
| 2231 | if (ObjCMethodParam) { |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2232 | Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier(), |
| 2233 | Type); |
| 2234 | Result += Type.getAsString(Policy) + Result + ")"; |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2235 | if (Param->getIdentifier()) |
| 2236 | Result += Param->getIdentifier()->getName(); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2237 | } else { |
| 2238 | Type.getAsStringInternal(Result, Policy); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | return Result; |
| 2242 | } |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2243 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2244 | // We have the function prototype behind the block pointer type, as it was |
| 2245 | // written in the source. |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2246 | std::string Result; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2247 | QualType ResultType = Block.getTypePtr()->getReturnType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2248 | if (ObjCSubsts) |
| 2249 | ResultType = ResultType.substObjCTypeArgs(Param->getASTContext(), |
| 2250 | *ObjCSubsts, |
| 2251 | ObjCSubstitutionContext::Result); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2252 | if (!ResultType->isVoidType() || SuppressBlock) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2253 | ResultType.getAsStringInternal(Result, Policy); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2254 | |
| 2255 | // Format the parameter list. |
| 2256 | std::string Params; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2257 | if (!BlockProto || Block.getNumParams() == 0) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2258 | if (BlockProto && BlockProto.getTypePtr()->isVariadic()) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2259 | Params = "(...)"; |
Douglas Gregor | af25cfa | 2010-10-02 23:49:58 +0000 | [diff] [blame] | 2260 | else |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2261 | Params = "(void)"; |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2262 | } else { |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2263 | Params += "("; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2264 | for (unsigned I = 0, N = Block.getNumParams(); I != N; ++I) { |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2265 | if (I) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2266 | Params += ", "; |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2267 | Params += FormatFunctionParameter(Policy, Block.getParam(I), |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2268 | /*SuppressName=*/false, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2269 | /*SuppressBlock=*/true, |
| 2270 | ObjCSubsts); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2271 | |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2272 | if (I == N - 1 && BlockProto.getTypePtr()->isVariadic()) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2273 | Params += ", ..."; |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2274 | } |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2275 | Params += ")"; |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2276 | } |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2277 | |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2278 | if (SuppressBlock) { |
| 2279 | // Format as a parameter. |
| 2280 | Result = Result + " (^"; |
| 2281 | if (Param->getIdentifier()) |
| 2282 | Result += Param->getIdentifier()->getName(); |
| 2283 | Result += ")"; |
| 2284 | Result += Params; |
| 2285 | } else { |
| 2286 | // Format as a block literal argument. |
| 2287 | Result = '^' + Result; |
| 2288 | Result += Params; |
| 2289 | |
| 2290 | if (Param->getIdentifier()) |
| 2291 | Result += Param->getIdentifier()->getName(); |
| 2292 | } |
| 2293 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2294 | return Result; |
| 2295 | } |
| 2296 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2297 | /// \brief Add function parameter chunks to the given code completion string. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2298 | static void AddFunctionParameterChunks(Preprocessor &PP, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2299 | const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2300 | const FunctionDecl *Function, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2301 | CodeCompletionBuilder &Result, |
| 2302 | unsigned Start = 0, |
| 2303 | bool InOptional = false) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2304 | bool FirstParameter = true; |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2305 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2306 | for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2307 | const ParmVarDecl *Param = Function->getParamDecl(P); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2308 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2309 | if (Param->hasDefaultArg() && !InOptional) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2310 | // When we see an optional default argument, put that argument and |
| 2311 | // the remaining default arguments into a new, optional string. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2312 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2313 | Result.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2314 | if (!FirstParameter) |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2315 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2316 | AddFunctionParameterChunks(PP, Policy, Function, Opt, P, true); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2317 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2318 | break; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2321 | if (FirstParameter) |
| 2322 | FirstParameter = false; |
| 2323 | else |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2324 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2325 | |
| 2326 | InOptional = false; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2327 | |
| 2328 | // Format the placeholder string. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2329 | std::string PlaceholderStr = FormatFunctionParameter(Policy, Param); |
| 2330 | |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2331 | if (Function->isVariadic() && P == N - 1) |
| 2332 | PlaceholderStr += ", ..."; |
| 2333 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2334 | // Add the placeholder string. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2335 | Result.AddPlaceholderChunk( |
| 2336 | Result.getAllocator().CopyString(PlaceholderStr)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2337 | } |
Douglas Gregor | ba44903 | 2009-09-22 21:42:17 +0000 | [diff] [blame] | 2338 | |
| 2339 | if (const FunctionProtoType *Proto |
| 2340 | = Function->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2341 | if (Proto->isVariadic()) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2342 | if (Proto->getNumParams() == 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2343 | Result.AddPlaceholderChunk("..."); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2344 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2345 | MaybeAddSentinel(PP, Function, Result); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2346 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | /// \brief Add template parameter chunks to the given code completion string. |
| 2350 | static void AddTemplateParameterChunks(ASTContext &Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2351 | const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2352 | const TemplateDecl *Template, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2353 | CodeCompletionBuilder &Result, |
| 2354 | unsigned MaxParameters = 0, |
| 2355 | unsigned Start = 0, |
| 2356 | bool InDefaultArg = false) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2357 | bool FirstParameter = true; |
Richard Smith | 6eece29 | 2015-01-15 02:27:20 +0000 | [diff] [blame] | 2358 | |
| 2359 | // Prefer to take the template parameter names from the first declaration of |
| 2360 | // the template. |
| 2361 | Template = cast<TemplateDecl>(Template->getCanonicalDecl()); |
| 2362 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2363 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2364 | TemplateParameterList::iterator PEnd = Params->end(); |
| 2365 | if (MaxParameters) |
| 2366 | PEnd = Params->begin() + MaxParameters; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2367 | for (TemplateParameterList::iterator P = Params->begin() + Start; |
| 2368 | P != PEnd; ++P) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2369 | bool HasDefaultArg = false; |
| 2370 | std::string PlaceholderStr; |
| 2371 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 2372 | if (TTP->wasDeclaredWithTypename()) |
| 2373 | PlaceholderStr = "typename"; |
| 2374 | else |
| 2375 | PlaceholderStr = "class"; |
| 2376 | |
| 2377 | if (TTP->getIdentifier()) { |
| 2378 | PlaceholderStr += ' '; |
| 2379 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2380 | } |
| 2381 | |
| 2382 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2383 | } else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2384 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2385 | if (NTTP->getIdentifier()) |
| 2386 | PlaceholderStr = NTTP->getIdentifier()->getName(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2387 | NTTP->getType().getAsStringInternal(PlaceholderStr, Policy); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2388 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2389 | } else { |
| 2390 | assert(isa<TemplateTemplateParmDecl>(*P)); |
| 2391 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 2392 | |
| 2393 | // Since putting the template argument list into the placeholder would |
| 2394 | // be very, very long, we just use an abbreviation. |
| 2395 | PlaceholderStr = "template<...> class"; |
| 2396 | if (TTP->getIdentifier()) { |
| 2397 | PlaceholderStr += ' '; |
| 2398 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2399 | } |
| 2400 | |
| 2401 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2402 | } |
| 2403 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2404 | if (HasDefaultArg && !InDefaultArg) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2405 | // When we see an optional default argument, put that argument and |
| 2406 | // the remaining default arguments into a new, optional string. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2407 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2408 | Result.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2409 | if (!FirstParameter) |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2410 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2411 | AddTemplateParameterChunks(Context, Policy, Template, Opt, MaxParameters, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2412 | P - Params->begin(), true); |
| 2413 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2414 | break; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2417 | InDefaultArg = false; |
| 2418 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2419 | if (FirstParameter) |
| 2420 | FirstParameter = false; |
| 2421 | else |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2422 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2423 | |
| 2424 | // Add the placeholder string. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2425 | Result.AddPlaceholderChunk( |
| 2426 | Result.getAllocator().CopyString(PlaceholderStr)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2427 | } |
| 2428 | } |
| 2429 | |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2430 | /// \brief Add a qualifier to the given code-completion string, if the |
| 2431 | /// provided nested-name-specifier is non-NULL. |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2432 | static void |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2433 | AddQualifierToCompletionString(CodeCompletionBuilder &Result, |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2434 | NestedNameSpecifier *Qualifier, |
| 2435 | bool QualifierIsInformative, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2436 | ASTContext &Context, |
| 2437 | const PrintingPolicy &Policy) { |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2438 | if (!Qualifier) |
| 2439 | return; |
| 2440 | |
| 2441 | std::string PrintedNNS; |
| 2442 | { |
| 2443 | llvm::raw_string_ostream OS(PrintedNNS); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2444 | Qualifier->print(OS, Policy); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2445 | } |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2446 | if (QualifierIsInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2447 | Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2448 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2449 | Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2452 | static void |
| 2453 | AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2454 | const FunctionDecl *Function) { |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2455 | const FunctionProtoType *Proto |
| 2456 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2457 | if (!Proto || !Proto->getTypeQuals()) |
| 2458 | return; |
| 2459 | |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2460 | // FIXME: Add ref-qualifier! |
| 2461 | |
| 2462 | // Handle single qualifiers without copying |
| 2463 | if (Proto->getTypeQuals() == Qualifiers::Const) { |
| 2464 | Result.AddInformativeChunk(" const"); |
| 2465 | return; |
| 2466 | } |
| 2467 | |
| 2468 | if (Proto->getTypeQuals() == Qualifiers::Volatile) { |
| 2469 | Result.AddInformativeChunk(" volatile"); |
| 2470 | return; |
| 2471 | } |
| 2472 | |
| 2473 | if (Proto->getTypeQuals() == Qualifiers::Restrict) { |
| 2474 | Result.AddInformativeChunk(" restrict"); |
| 2475 | return; |
| 2476 | } |
| 2477 | |
| 2478 | // Handle multiple qualifiers. |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2479 | std::string QualsStr; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2480 | if (Proto->isConst()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2481 | QualsStr += " const"; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2482 | if (Proto->isVolatile()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2483 | QualsStr += " volatile"; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2484 | if (Proto->isRestrict()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2485 | QualsStr += " restrict"; |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2486 | Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr)); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2489 | /// \brief Add the name of the given declaration |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2490 | static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2491 | const NamedDecl *ND, |
| 2492 | CodeCompletionBuilder &Result) { |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2493 | DeclarationName Name = ND->getDeclName(); |
| 2494 | if (!Name) |
| 2495 | return; |
| 2496 | |
| 2497 | switch (Name.getNameKind()) { |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2498 | case DeclarationName::CXXOperatorName: { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2499 | const char *OperatorName = nullptr; |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2500 | switch (Name.getCXXOverloadedOperator()) { |
| 2501 | case OO_None: |
| 2502 | case OO_Conditional: |
| 2503 | case NUM_OVERLOADED_OPERATORS: |
| 2504 | OperatorName = "operator"; |
| 2505 | break; |
| 2506 | |
| 2507 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 2508 | case OO_##Name: OperatorName = "operator" Spelling; break; |
| 2509 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) |
| 2510 | #include "clang/Basic/OperatorKinds.def" |
| 2511 | |
| 2512 | case OO_New: OperatorName = "operator new"; break; |
| 2513 | case OO_Delete: OperatorName = "operator delete"; break; |
| 2514 | case OO_Array_New: OperatorName = "operator new[]"; break; |
| 2515 | case OO_Array_Delete: OperatorName = "operator delete[]"; break; |
| 2516 | case OO_Call: OperatorName = "operator()"; break; |
| 2517 | case OO_Subscript: OperatorName = "operator[]"; break; |
| 2518 | } |
| 2519 | Result.AddTypedTextChunk(OperatorName); |
| 2520 | break; |
| 2521 | } |
| 2522 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2523 | case DeclarationName::Identifier: |
| 2524 | case DeclarationName::CXXConversionFunctionName: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2525 | case DeclarationName::CXXDestructorName: |
| 2526 | case DeclarationName::CXXLiteralOperatorName: |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2527 | Result.AddTypedTextChunk( |
| 2528 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2529 | break; |
| 2530 | |
| 2531 | case DeclarationName::CXXUsingDirective: |
| 2532 | case DeclarationName::ObjCZeroArgSelector: |
| 2533 | case DeclarationName::ObjCOneArgSelector: |
| 2534 | case DeclarationName::ObjCMultiArgSelector: |
| 2535 | break; |
| 2536 | |
| 2537 | case DeclarationName::CXXConstructorName: { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2538 | CXXRecordDecl *Record = nullptr; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2539 | QualType Ty = Name.getCXXNameType(); |
| 2540 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) |
| 2541 | Record = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 2542 | else if (const InjectedClassNameType *InjectedTy |
| 2543 | = Ty->getAs<InjectedClassNameType>()) |
| 2544 | Record = InjectedTy->getDecl(); |
| 2545 | else { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2546 | Result.AddTypedTextChunk( |
| 2547 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2548 | break; |
| 2549 | } |
| 2550 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2551 | Result.AddTypedTextChunk( |
| 2552 | Result.getAllocator().CopyString(Record->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2553 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2554 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2555 | AddTemplateParameterChunks(Context, Policy, Template, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2556 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2557 | } |
| 2558 | break; |
| 2559 | } |
| 2560 | } |
| 2561 | } |
| 2562 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2563 | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString(Sema &S, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2564 | const CodeCompletionContext &CCContext, |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2565 | CodeCompletionAllocator &Allocator, |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2566 | CodeCompletionTUInfo &CCTUInfo, |
| 2567 | bool IncludeBriefComments) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2568 | return CreateCodeCompletionString(S.Context, S.PP, CCContext, Allocator, |
| 2569 | CCTUInfo, IncludeBriefComments); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2572 | /// \brief If possible, create a new code completion string for the given |
| 2573 | /// result. |
| 2574 | /// |
| 2575 | /// \returns Either a new, heap-allocated code completion string describing |
| 2576 | /// how to use this result, or NULL to indicate that the string or name of the |
| 2577 | /// result is all that is needed. |
| 2578 | CodeCompletionString * |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2579 | CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx, |
| 2580 | Preprocessor &PP, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2581 | const CodeCompletionContext &CCContext, |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2582 | CodeCompletionAllocator &Allocator, |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2583 | CodeCompletionTUInfo &CCTUInfo, |
| 2584 | bool IncludeBriefComments) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2585 | CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2586 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2587 | PrintingPolicy Policy = getCompletionPrintingPolicy(Ctx, PP); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2588 | if (Kind == RK_Pattern) { |
| 2589 | Pattern->Priority = Priority; |
| 2590 | Pattern->Availability = Availability; |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2591 | |
| 2592 | if (Declaration) { |
| 2593 | Result.addParentContext(Declaration->getDeclContext()); |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2594 | Pattern->ParentName = Result.getParentName(); |
Fariborz Jahanian | 1fcf492 | 2013-03-22 17:55:27 +0000 | [diff] [blame] | 2595 | // Provide code completion comment for self.GetterName where |
| 2596 | // GetterName is the getter method for a property with name |
| 2597 | // different from the property name (declared via a property |
| 2598 | // getter attribute. |
| 2599 | const NamedDecl *ND = Declaration; |
| 2600 | if (const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(ND)) |
| 2601 | if (M->isPropertyAccessor()) |
| 2602 | if (const ObjCPropertyDecl *PDecl = M->findPropertyDecl()) |
| 2603 | if (PDecl->getGetterName() == M->getSelector() && |
Fariborz Jahanian | be8bc67 | 2013-03-23 01:10:45 +0000 | [diff] [blame] | 2604 | PDecl->getIdentifier() != M->getIdentifier()) { |
| 2605 | if (const RawComment *RC = |
| 2606 | Ctx.getRawCommentForAnyRedecl(M)) { |
Fariborz Jahanian | 1fcf492 | 2013-03-22 17:55:27 +0000 | [diff] [blame] | 2607 | Result.addBriefComment(RC->getBriefText(Ctx)); |
| 2608 | Pattern->BriefComment = Result.getBriefComment(); |
| 2609 | } |
Fariborz Jahanian | be8bc67 | 2013-03-23 01:10:45 +0000 | [diff] [blame] | 2610 | else if (const RawComment *RC = |
| 2611 | Ctx.getRawCommentForAnyRedecl(PDecl)) { |
| 2612 | Result.addBriefComment(RC->getBriefText(Ctx)); |
| 2613 | Pattern->BriefComment = Result.getBriefComment(); |
| 2614 | } |
| 2615 | } |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2618 | return Pattern; |
| 2619 | } |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2620 | |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2621 | if (Kind == RK_Keyword) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2622 | Result.AddTypedTextChunk(Keyword); |
| 2623 | return Result.TakeString(); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2624 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2625 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2626 | if (Kind == RK_Macro) { |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2627 | const MacroInfo *MI = PP.getMacroInfo(Macro); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2628 | Result.AddTypedTextChunk( |
| 2629 | Result.getAllocator().CopyString(Macro->getName())); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2630 | |
Ben Langmuir | c28ce3a | 2014-09-30 20:00:18 +0000 | [diff] [blame] | 2631 | if (!MI || !MI->isFunctionLike()) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2632 | return Result.TakeString(); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2633 | |
| 2634 | // Format a function-like macro with placeholders for the arguments. |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2635 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | 0c50531 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2636 | MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end(); |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2637 | |
| 2638 | // C99 variadic macros add __VA_ARGS__ at the end. Skip it. |
| 2639 | if (MI->isC99Varargs()) { |
| 2640 | --AEnd; |
| 2641 | |
| 2642 | if (A == AEnd) { |
| 2643 | Result.AddPlaceholderChunk("..."); |
| 2644 | } |
Douglas Gregor | 0c50531 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2645 | } |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2646 | |
Douglas Gregor | 0c50531 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2647 | for (MacroInfo::arg_iterator A = MI->arg_begin(); A != AEnd; ++A) { |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2648 | if (A != MI->arg_begin()) |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2649 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2650 | |
| 2651 | if (MI->isVariadic() && (A+1) == AEnd) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2652 | SmallString<32> Arg = (*A)->getName(); |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2653 | if (MI->isC99Varargs()) |
| 2654 | Arg += ", ..."; |
| 2655 | else |
| 2656 | Arg += "..."; |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2657 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2658 | break; |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2659 | } |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2660 | |
| 2661 | // Non-variadic macros are simple. |
| 2662 | Result.AddPlaceholderChunk( |
| 2663 | Result.getAllocator().CopyString((*A)->getName())); |
Douglas Gregor | 0c50531 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2664 | } |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2665 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2666 | return Result.TakeString(); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2669 | assert(Kind == RK_Declaration && "Missed a result kind?"); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2670 | const NamedDecl *ND = Declaration; |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2671 | Result.addParentContext(ND->getDeclContext()); |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2672 | |
| 2673 | if (IncludeBriefComments) { |
| 2674 | // Add documentation comment, if it exists. |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 2675 | if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(ND)) { |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2676 | Result.addBriefComment(RC->getBriefText(Ctx)); |
Fariborz Jahanian | 15a0b55 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 2677 | } |
| 2678 | else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
| 2679 | if (OMD->isPropertyAccessor()) |
| 2680 | if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) |
| 2681 | if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(PDecl)) |
| 2682 | Result.addBriefComment(RC->getBriefText(Ctx)); |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2685 | if (StartsNestedNameSpecifier) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2686 | Result.AddTypedTextChunk( |
| 2687 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2688 | Result.AddTextChunk("::"); |
| 2689 | return Result.TakeString(); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2690 | } |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 2691 | |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2692 | for (const auto *I : ND->specific_attrs<AnnotateAttr>()) |
| 2693 | Result.AddAnnotation(Result.getAllocator().CopyString(I->getAnnotation())); |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 2694 | |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2695 | AddResultTypeChunk(Ctx, Policy, ND, CCContext.getBaseType(), Result); |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2696 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2697 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2698 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2699 | Ctx, Policy); |
| 2700 | AddTypedNameChunk(Ctx, Policy, ND, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2701 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2702 | AddFunctionParameterChunks(PP, Policy, Function, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2703 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2704 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2705 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2708 | if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2709 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2710 | Ctx, Policy); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2711 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2712 | AddTypedNameChunk(Ctx, Policy, Function, Result); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2713 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2714 | // Figure out which template parameters are deduced (or have default |
| 2715 | // arguments). |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 2716 | llvm::SmallBitVector Deduced; |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2717 | Sema::MarkDeducedTemplateParameters(Ctx, FunTmpl, Deduced); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2718 | unsigned LastDeducibleArgument; |
| 2719 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
| 2720 | --LastDeducibleArgument) { |
| 2721 | if (!Deduced[LastDeducibleArgument - 1]) { |
| 2722 | // C++0x: Figure out if the template argument has a default. If so, |
| 2723 | // the user doesn't need to type this argument. |
| 2724 | // FIXME: We need to abstract template parameters better! |
| 2725 | bool HasDefaultArg = false; |
| 2726 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2727 | LastDeducibleArgument - 1); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2728 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 2729 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2730 | else if (NonTypeTemplateParmDecl *NTTP |
| 2731 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 2732 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2733 | else { |
| 2734 | assert(isa<TemplateTemplateParmDecl>(Param)); |
| 2735 | HasDefaultArg |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2736 | = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | if (!HasDefaultArg) |
| 2740 | break; |
| 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | if (LastDeducibleArgument) { |
| 2745 | // Some of the function template arguments cannot be deduced from a |
| 2746 | // function call, so we introduce an explicit template argument list |
| 2747 | // containing all of the arguments up to the first deducible argument. |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2748 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2749 | AddTemplateParameterChunks(Ctx, Policy, FunTmpl, Result, |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2750 | LastDeducibleArgument); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2751 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
| 2754 | // Add the function parameters |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2755 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2756 | AddFunctionParameterChunks(PP, Policy, Function, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2757 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2758 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2759 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2762 | if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2763 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2764 | Ctx, Policy); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2765 | Result.AddTypedTextChunk( |
| 2766 | Result.getAllocator().CopyString(Template->getNameAsString())); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2767 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2768 | AddTemplateParameterChunks(Ctx, Policy, Template, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2769 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2770 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2773 | if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2774 | Selector Sel = Method->getSelector(); |
| 2775 | if (Sel.isUnarySelector()) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2776 | Result.AddTypedTextChunk(Result.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2777 | Sel.getNameForSlot(0))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2778 | return Result.TakeString(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2781 | std::string SelName = Sel.getNameForSlot(0).str(); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2782 | SelName += ':'; |
| 2783 | if (StartParameter == 0) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2784 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName)); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2785 | else { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2786 | Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName)); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2787 | |
| 2788 | // If there is only one parameter, and we're past it, add an empty |
| 2789 | // typed-text chunk since there is nothing to type. |
| 2790 | if (Method->param_size() == 1) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2791 | Result.AddTypedTextChunk(""); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2792 | } |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2793 | unsigned Idx = 0; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2794 | for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(), |
| 2795 | PEnd = Method->param_end(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2796 | P != PEnd; (void)++P, ++Idx) { |
| 2797 | if (Idx > 0) { |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2798 | std::string Keyword; |
| 2799 | if (Idx > StartParameter) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2800 | Result.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2801 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 2802 | Keyword += II->getName(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2803 | Keyword += ":"; |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2804 | if (Idx < StartParameter || AllParametersAreInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2805 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword)); |
Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2806 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2807 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword)); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2808 | } |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2809 | |
| 2810 | // If we're before the starting parameter, skip the placeholder. |
| 2811 | if (Idx < StartParameter) |
| 2812 | continue; |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2813 | |
| 2814 | std::string Arg; |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2815 | QualType ParamType = (*P)->getType(); |
| 2816 | Optional<ArrayRef<QualType>> ObjCSubsts; |
| 2817 | if (!CCContext.getBaseType().isNull()) |
| 2818 | ObjCSubsts = CCContext.getBaseType()->getObjCSubstitutions(Method); |
| 2819 | |
| 2820 | if (ParamType->isBlockPointerType() && !DeclaringEntity) |
| 2821 | Arg = FormatFunctionParameter(Policy, *P, true, |
| 2822 | /*SuppressBlock=*/false, |
| 2823 | ObjCSubsts); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2824 | else { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2825 | if (ObjCSubsts) |
| 2826 | ParamType = ParamType.substObjCTypeArgs(Ctx, *ObjCSubsts, |
| 2827 | ObjCSubstitutionContext::Parameter); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2828 | Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier(), |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2829 | ParamType); |
| 2830 | Arg += ParamType.getAsString(Policy) + ")"; |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2831 | if (IdentifierInfo *II = (*P)->getIdentifier()) |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2832 | if (DeclaringEntity || AllParametersAreInformative) |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 2833 | Arg += II->getName(); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2836 | if (Method->isVariadic() && (P + 1) == PEnd) |
| 2837 | Arg += ", ..."; |
| 2838 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2839 | if (DeclaringEntity) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2840 | Result.AddTextChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2841 | else if (AllParametersAreInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2842 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2843 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2844 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2847 | if (Method->isVariadic()) { |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2848 | if (Method->param_size() == 0) { |
| 2849 | if (DeclaringEntity) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2850 | Result.AddTextChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2851 | else if (AllParametersAreInformative) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2852 | Result.AddInformativeChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2853 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2854 | Result.AddPlaceholderChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2855 | } |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2856 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2857 | MaybeAddSentinel(PP, Method, Result); |
Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2860 | return Result.TakeString(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2863 | if (Qualifier) |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2864 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2865 | Ctx, Policy); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2866 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2867 | Result.AddTypedTextChunk( |
| 2868 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2869 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2872 | /// \brief Add function overload parameter chunks to the given code completion |
| 2873 | /// string. |
| 2874 | static void AddOverloadParameterChunks(ASTContext &Context, |
| 2875 | const PrintingPolicy &Policy, |
| 2876 | const FunctionDecl *Function, |
| 2877 | const FunctionProtoType *Prototype, |
| 2878 | CodeCompletionBuilder &Result, |
| 2879 | unsigned CurrentArg, |
| 2880 | unsigned Start = 0, |
| 2881 | bool InOptional = false) { |
| 2882 | bool FirstParameter = true; |
| 2883 | unsigned NumParams = Function ? Function->getNumParams() |
| 2884 | : Prototype->getNumParams(); |
| 2885 | |
| 2886 | for (unsigned P = Start; P != NumParams; ++P) { |
| 2887 | if (Function && Function->getParamDecl(P)->hasDefaultArg() && !InOptional) { |
| 2888 | // When we see an optional default argument, put that argument and |
| 2889 | // the remaining default arguments into a new, optional string. |
| 2890 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2891 | Result.getCodeCompletionTUInfo()); |
| 2892 | if (!FirstParameter) |
| 2893 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| 2894 | // Optional sections are nested. |
| 2895 | AddOverloadParameterChunks(Context, Policy, Function, Prototype, Opt, |
| 2896 | CurrentArg, P, /*InOptional=*/true); |
| 2897 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2898 | return; |
| 2899 | } |
| 2900 | |
| 2901 | if (FirstParameter) |
| 2902 | FirstParameter = false; |
| 2903 | else |
| 2904 | Result.AddChunk(CodeCompletionString::CK_Comma); |
| 2905 | |
| 2906 | InOptional = false; |
| 2907 | |
| 2908 | // Format the placeholder string. |
| 2909 | std::string Placeholder; |
| 2910 | if (Function) |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2911 | Placeholder = FormatFunctionParameter(Policy, Function->getParamDecl(P)); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2912 | else |
| 2913 | Placeholder = Prototype->getParamType(P).getAsString(Policy); |
| 2914 | |
| 2915 | if (P == CurrentArg) |
| 2916 | Result.AddCurrentParameterChunk( |
| 2917 | Result.getAllocator().CopyString(Placeholder)); |
| 2918 | else |
| 2919 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Placeholder)); |
| 2920 | } |
| 2921 | |
| 2922 | if (Prototype && Prototype->isVariadic()) { |
| 2923 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2924 | Result.getCodeCompletionTUInfo()); |
| 2925 | if (!FirstParameter) |
| 2926 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| 2927 | |
| 2928 | if (CurrentArg < NumParams) |
| 2929 | Opt.AddPlaceholderChunk("..."); |
| 2930 | else |
| 2931 | Opt.AddCurrentParameterChunk("..."); |
| 2932 | |
| 2933 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2934 | } |
| 2935 | } |
| 2936 | |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2937 | CodeCompletionString * |
| 2938 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2939 | unsigned CurrentArg, Sema &S, |
| 2940 | CodeCompletionAllocator &Allocator, |
| 2941 | CodeCompletionTUInfo &CCTUInfo, |
| 2942 | bool IncludeBriefComments) const { |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2943 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2944 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2945 | // FIXME: Set priority, availability appropriately. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2946 | CodeCompletionBuilder Result(Allocator,CCTUInfo, 1, CXAvailability_Available); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2947 | FunctionDecl *FDecl = getFunction(); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2948 | const FunctionProtoType *Proto |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2949 | = dyn_cast<FunctionProtoType>(getFunctionType()); |
| 2950 | if (!FDecl && !Proto) { |
| 2951 | // Function without a prototype. Just give the return type and a |
| 2952 | // highlighted ellipsis. |
| 2953 | const FunctionType *FT = getFunctionType(); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2954 | Result.AddResultTypeChunk(Result.getAllocator().CopyString( |
| 2955 | FT->getReturnType().getAsString(Policy))); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2956 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2957 | Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "..."); |
| 2958 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2959 | return Result.TakeString(); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2960 | } |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2961 | |
| 2962 | if (FDecl) { |
| 2963 | if (IncludeBriefComments && CurrentArg < FDecl->getNumParams()) |
| 2964 | if (auto RC = S.getASTContext().getRawCommentForAnyRedecl( |
| 2965 | FDecl->getParamDecl(CurrentArg))) |
| 2966 | Result.addBriefComment(RC->getBriefText(S.getASTContext())); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2967 | AddResultTypeChunk(S.Context, Policy, FDecl, QualType(), Result); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2968 | Result.AddTextChunk( |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2969 | Result.getAllocator().CopyString(FDecl->getNameAsString())); |
| 2970 | } else { |
| 2971 | Result.AddResultTypeChunk( |
| 2972 | Result.getAllocator().CopyString( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2973 | Proto->getReturnType().getAsString(Policy))); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2974 | } |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2975 | |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2976 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2977 | AddOverloadParameterChunks(S.getASTContext(), Policy, FDecl, Proto, Result, |
| 2978 | CurrentArg); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2979 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2980 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2981 | return Result.TakeString(); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2984 | unsigned clang::getMacroUsagePriority(StringRef MacroName, |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2985 | const LangOptions &LangOpts, |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2986 | bool PreferredTypeIsPointer) { |
| 2987 | unsigned Priority = CCP_Macro; |
| 2988 | |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2989 | // Treat the "nil", "Nil" and "NULL" macros as null pointer constants. |
| 2990 | if (MacroName.equals("nil") || MacroName.equals("NULL") || |
| 2991 | MacroName.equals("Nil")) { |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2992 | Priority = CCP_Constant; |
| 2993 | if (PreferredTypeIsPointer) |
| 2994 | Priority = Priority / CCF_SimilarTypeMatch; |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2995 | } |
| 2996 | // Treat "YES", "NO", "true", and "false" as constants. |
| 2997 | else if (MacroName.equals("YES") || MacroName.equals("NO") || |
| 2998 | MacroName.equals("true") || MacroName.equals("false")) |
| 2999 | Priority = CCP_Constant; |
| 3000 | // Treat "bool" as a type. |
| 3001 | else if (MacroName.equals("bool")) |
| 3002 | Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0); |
| 3003 | |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3004 | |
| 3005 | return Priority; |
| 3006 | } |
| 3007 | |
Dmitri Gribenko | bdc80de | 2013-01-11 20:32:41 +0000 | [diff] [blame] | 3008 | CXCursorKind clang::getCursorKindForDecl(const Decl *D) { |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3009 | if (!D) |
| 3010 | return CXCursor_UnexposedDecl; |
| 3011 | |
| 3012 | switch (D->getKind()) { |
| 3013 | case Decl::Enum: return CXCursor_EnumDecl; |
| 3014 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 3015 | case Decl::Field: return CXCursor_FieldDecl; |
| 3016 | case Decl::Function: |
| 3017 | return CXCursor_FunctionDecl; |
| 3018 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 3019 | case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryImplDecl; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3020 | case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl; |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 3021 | |
Argyrios Kyrtzidis | 3698cef | 2012-01-24 21:39:26 +0000 | [diff] [blame] | 3022 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3023 | case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl; |
| 3024 | case Decl::ObjCMethod: |
| 3025 | return cast<ObjCMethodDecl>(D)->isInstanceMethod() |
| 3026 | ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl; |
| 3027 | case Decl::CXXMethod: return CXCursor_CXXMethod; |
| 3028 | case Decl::CXXConstructor: return CXCursor_Constructor; |
| 3029 | case Decl::CXXDestructor: return CXCursor_Destructor; |
| 3030 | case Decl::CXXConversion: return CXCursor_ConversionFunction; |
| 3031 | case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl; |
Argyrios Kyrtzidis | 3698cef | 2012-01-24 21:39:26 +0000 | [diff] [blame] | 3032 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3033 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 3034 | case Decl::Typedef: return CXCursor_TypedefDecl; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3035 | case Decl::TypeAlias: return CXCursor_TypeAliasDecl; |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 3036 | case Decl::TypeAliasTemplate: return CXCursor_TypeAliasTemplateDecl; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3037 | case Decl::Var: return CXCursor_VarDecl; |
| 3038 | case Decl::Namespace: return CXCursor_Namespace; |
| 3039 | case Decl::NamespaceAlias: return CXCursor_NamespaceAlias; |
| 3040 | case Decl::TemplateTypeParm: return CXCursor_TemplateTypeParameter; |
| 3041 | case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter; |
| 3042 | case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter; |
| 3043 | case Decl::FunctionTemplate: return CXCursor_FunctionTemplate; |
| 3044 | case Decl::ClassTemplate: return CXCursor_ClassTemplate; |
Argyrios Kyrtzidis | 12afd70 | 2011-09-30 17:58:23 +0000 | [diff] [blame] | 3045 | case Decl::AccessSpec: return CXCursor_CXXAccessSpecifier; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3046 | case Decl::ClassTemplatePartialSpecialization: |
| 3047 | return CXCursor_ClassTemplatePartialSpecialization; |
| 3048 | case Decl::UsingDirective: return CXCursor_UsingDirective; |
Douglas Gregor | 3e653b3 | 2012-04-30 23:41:16 +0000 | [diff] [blame] | 3049 | case Decl::TranslationUnit: return CXCursor_TranslationUnit; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3050 | |
| 3051 | case Decl::Using: |
| 3052 | case Decl::UnresolvedUsingValue: |
| 3053 | case Decl::UnresolvedUsingTypename: |
| 3054 | return CXCursor_UsingDeclaration; |
| 3055 | |
Douglas Gregor | 4cd6596 | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3056 | case Decl::ObjCPropertyImpl: |
| 3057 | switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) { |
| 3058 | case ObjCPropertyImplDecl::Dynamic: |
| 3059 | return CXCursor_ObjCDynamicDecl; |
| 3060 | |
| 3061 | case ObjCPropertyImplDecl::Synthesize: |
| 3062 | return CXCursor_ObjCSynthesizeDecl; |
| 3063 | } |
Argyrios Kyrtzidis | 50e5b1d | 2012-10-05 00:22:24 +0000 | [diff] [blame] | 3064 | |
| 3065 | case Decl::Import: |
| 3066 | return CXCursor_ModuleImportDecl; |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3067 | |
| 3068 | case Decl::ObjCTypeParam: return CXCursor_TemplateTypeParameter; |
| 3069 | |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3070 | default: |
Dmitri Gribenko | bdc80de | 2013-01-11 20:32:41 +0000 | [diff] [blame] | 3071 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3072 | switch (TD->getTagKind()) { |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3073 | case TTK_Interface: // fall through |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3074 | case TTK_Struct: return CXCursor_StructDecl; |
| 3075 | case TTK_Class: return CXCursor_ClassDecl; |
| 3076 | case TTK_Union: return CXCursor_UnionDecl; |
| 3077 | case TTK_Enum: return CXCursor_EnumDecl; |
| 3078 | } |
| 3079 | } |
| 3080 | } |
| 3081 | |
| 3082 | return CXCursor_UnexposedDecl; |
| 3083 | } |
| 3084 | |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3085 | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3086 | bool IncludeUndefined, |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3087 | bool TargetTypeIsPointer = false) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3088 | typedef CodeCompletionResult Result; |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3089 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3090 | Results.EnterNewScope(); |
Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 3091 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3092 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 3093 | MEnd = PP.macro_end(); |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3094 | M != MEnd; ++M) { |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 3095 | auto MD = PP.getMacroDefinition(M->first); |
| 3096 | if (IncludeUndefined || MD) { |
| 3097 | if (MacroInfo *MI = MD.getMacroInfo()) |
Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 3098 | if (MI->isUsedForHeaderGuard()) |
| 3099 | continue; |
| 3100 | |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3101 | Results.AddResult(Result(M->first, |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3102 | getMacroUsagePriority(M->first->getName(), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3103 | PP.getLangOpts(), |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3104 | TargetTypeIsPointer))); |
Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 3105 | } |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3106 | } |
Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 3107 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3108 | Results.ExitScope(); |
Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 3109 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3112 | static void AddPrettyFunctionResults(const LangOptions &LangOpts, |
| 3113 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3114 | typedef CodeCompletionResult Result; |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3115 | |
| 3116 | Results.EnterNewScope(); |
Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 3117 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3118 | Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant)); |
| 3119 | Results.AddResult(Result("__FUNCTION__", CCP_Constant)); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3120 | if (LangOpts.C99 || LangOpts.CPlusPlus11) |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3121 | Results.AddResult(Result("__func__", CCP_Constant)); |
| 3122 | Results.ExitScope(); |
| 3123 | } |
| 3124 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3125 | static void HandleCodeCompleteResults(Sema *S, |
| 3126 | CodeCompleteConsumer *CodeCompleter, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3127 | CodeCompletionContext Context, |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3128 | CodeCompletionResult *Results, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3129 | unsigned NumResults) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3130 | if (CodeCompleter) |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3131 | CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3134 | static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S, |
| 3135 | Sema::ParserCompletionContext PCC) { |
| 3136 | switch (PCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3137 | case Sema::PCC_Namespace: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3138 | return CodeCompletionContext::CCC_TopLevel; |
| 3139 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3140 | case Sema::PCC_Class: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3141 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 3142 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3143 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3144 | return CodeCompletionContext::CCC_ObjCInterface; |
| 3145 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3146 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3147 | return CodeCompletionContext::CCC_ObjCImplementation; |
| 3148 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3149 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3150 | return CodeCompletionContext::CCC_ObjCIvarList; |
| 3151 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3152 | case Sema::PCC_Template: |
| 3153 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3154 | if (S.CurContext->isFileContext()) |
| 3155 | return CodeCompletionContext::CCC_TopLevel; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3156 | if (S.CurContext->isRecord()) |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3157 | return CodeCompletionContext::CCC_ClassStructUnion; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3158 | return CodeCompletionContext::CCC_Other; |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3159 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3160 | case Sema::PCC_RecoveryInFunction: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3161 | return CodeCompletionContext::CCC_Recovery; |
Douglas Gregor | c769d6e | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 3162 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3163 | case Sema::PCC_ForInit: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3164 | if (S.getLangOpts().CPlusPlus || S.getLangOpts().C99 || |
| 3165 | S.getLangOpts().ObjC1) |
Douglas Gregor | c769d6e | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 3166 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
| 3167 | else |
| 3168 | return CodeCompletionContext::CCC_Expression; |
| 3169 | |
| 3170 | case Sema::PCC_Expression: |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3171 | case Sema::PCC_Condition: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3172 | return CodeCompletionContext::CCC_Expression; |
| 3173 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3174 | case Sema::PCC_Statement: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3175 | return CodeCompletionContext::CCC_Statement; |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3176 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3177 | case Sema::PCC_Type: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3178 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3179 | |
| 3180 | case Sema::PCC_ParenthesizedExpression: |
| 3181 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3182 | |
| 3183 | case Sema::PCC_LocalDeclarationSpecifiers: |
| 3184 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3185 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3186 | |
| 3187 | llvm_unreachable("Invalid ParserCompletionContext!"); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3190 | /// \brief If we're in a C++ virtual member function, add completion results |
| 3191 | /// that invoke the functions we override, since it's common to invoke the |
| 3192 | /// overridden function as well as adding new functionality. |
| 3193 | /// |
| 3194 | /// \param S The semantic analysis object for which we are generating results. |
| 3195 | /// |
| 3196 | /// \param InContext This context in which the nested-name-specifier preceding |
| 3197 | /// the code-completion point |
| 3198 | static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, |
| 3199 | ResultBuilder &Results) { |
| 3200 | // Look through blocks. |
| 3201 | DeclContext *CurContext = S.CurContext; |
| 3202 | while (isa<BlockDecl>(CurContext)) |
| 3203 | CurContext = CurContext->getParent(); |
| 3204 | |
| 3205 | |
| 3206 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext); |
| 3207 | if (!Method || !Method->isVirtual()) |
| 3208 | return; |
| 3209 | |
| 3210 | // We need to have names for all of the parameters, if we're going to |
| 3211 | // generate a forwarding call. |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3212 | for (auto P : Method->params()) |
| 3213 | if (!P->getDeclName()) |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3214 | return; |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3215 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3216 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3217 | for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(), |
| 3218 | MEnd = Method->end_overridden_methods(); |
| 3219 | M != MEnd; ++M) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3220 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3221 | Results.getCodeCompletionTUInfo()); |
Dmitri Gribenko | 6cfb153 | 2013-02-14 13:53:30 +0000 | [diff] [blame] | 3222 | const CXXMethodDecl *Overridden = *M; |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3223 | if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl()) |
| 3224 | continue; |
| 3225 | |
| 3226 | // If we need a nested-name-specifier, add one now. |
| 3227 | if (!InContext) { |
| 3228 | NestedNameSpecifier *NNS |
| 3229 | = getRequiredQualification(S.Context, CurContext, |
| 3230 | Overridden->getDeclContext()); |
| 3231 | if (NNS) { |
| 3232 | std::string Str; |
| 3233 | llvm::raw_string_ostream OS(Str); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3234 | NNS->print(OS, Policy); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3235 | Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str())); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3236 | } |
| 3237 | } else if (!InContext->Equals(Overridden->getDeclContext())) |
| 3238 | continue; |
| 3239 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3240 | Builder.AddTypedTextChunk(Results.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3241 | Overridden->getNameAsString())); |
| 3242 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3243 | bool FirstParam = true; |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3244 | for (auto P : Method->params()) { |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3245 | if (FirstParam) |
| 3246 | FirstParam = false; |
| 3247 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3248 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3249 | |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3250 | Builder.AddPlaceholderChunk( |
| 3251 | Results.getAllocator().CopyString(P->getIdentifier()->getName())); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3252 | } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3253 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3254 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3255 | CCP_SuperCompletion, |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3256 | CXCursor_CXXMethod, |
| 3257 | CXAvailability_Available, |
| 3258 | Overridden)); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3259 | Results.Ignore(Overridden); |
| 3260 | } |
| 3261 | } |
| 3262 | |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3263 | void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc, |
| 3264 | ModuleIdPath Path) { |
| 3265 | typedef CodeCompletionResult Result; |
| 3266 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3267 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3268 | CodeCompletionContext::CCC_Other); |
| 3269 | Results.EnterNewScope(); |
| 3270 | |
| 3271 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3272 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3273 | typedef CodeCompletionResult Result; |
| 3274 | if (Path.empty()) { |
| 3275 | // Enumerate all top-level modules. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3276 | SmallVector<Module *, 8> Modules; |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3277 | PP.getHeaderSearchInfo().collectAllModules(Modules); |
| 3278 | for (unsigned I = 0, N = Modules.size(); I != N; ++I) { |
| 3279 | Builder.AddTypedTextChunk( |
| 3280 | Builder.getAllocator().CopyString(Modules[I]->Name)); |
| 3281 | Results.AddResult(Result(Builder.TakeString(), |
| 3282 | CCP_Declaration, |
Argyrios Kyrtzidis | 345d05f | 2013-05-29 18:50:15 +0000 | [diff] [blame] | 3283 | CXCursor_ModuleImportDecl, |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3284 | Modules[I]->isAvailable() |
| 3285 | ? CXAvailability_Available |
| 3286 | : CXAvailability_NotAvailable)); |
| 3287 | } |
Daniel Jasper | 07e6c40 | 2013-08-05 20:26:17 +0000 | [diff] [blame] | 3288 | } else if (getLangOpts().Modules) { |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3289 | // Load the named module. |
| 3290 | Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path, |
| 3291 | Module::AllVisible, |
| 3292 | /*IsInclusionDirective=*/false); |
| 3293 | // Enumerate submodules. |
| 3294 | if (Mod) { |
| 3295 | for (Module::submodule_iterator Sub = Mod->submodule_begin(), |
| 3296 | SubEnd = Mod->submodule_end(); |
| 3297 | Sub != SubEnd; ++Sub) { |
| 3298 | |
| 3299 | Builder.AddTypedTextChunk( |
| 3300 | Builder.getAllocator().CopyString((*Sub)->Name)); |
| 3301 | Results.AddResult(Result(Builder.TakeString(), |
| 3302 | CCP_Declaration, |
Argyrios Kyrtzidis | 345d05f | 2013-05-29 18:50:15 +0000 | [diff] [blame] | 3303 | CXCursor_ModuleImportDecl, |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3304 | (*Sub)->isAvailable() |
| 3305 | ? CXAvailability_Available |
| 3306 | : CXAvailability_NotAvailable)); |
| 3307 | } |
| 3308 | } |
| 3309 | } |
| 3310 | Results.ExitScope(); |
| 3311 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 3312 | Results.data(),Results.size()); |
| 3313 | } |
| 3314 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3315 | void Sema::CodeCompleteOrdinaryName(Scope *S, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3316 | ParserCompletionContext CompletionContext) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3317 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3318 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3319 | mapCodeCompletionContext(*this, CompletionContext)); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3320 | Results.EnterNewScope(); |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 3321 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3322 | // Determine how to filter results, e.g., so that the names of |
| 3323 | // values (functions, enumerators, function templates, etc.) are |
| 3324 | // only allowed where we can have an expression. |
| 3325 | switch (CompletionContext) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3326 | case PCC_Namespace: |
| 3327 | case PCC_Class: |
| 3328 | case PCC_ObjCInterface: |
| 3329 | case PCC_ObjCImplementation: |
| 3330 | case PCC_ObjCInstanceVariableList: |
| 3331 | case PCC_Template: |
| 3332 | case PCC_MemberTemplate: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3333 | case PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3334 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3335 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 3336 | break; |
| 3337 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3338 | case PCC_Statement: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3339 | case PCC_ParenthesizedExpression: |
Douglas Gregor | 4d755e8 | 2010-08-24 23:58:17 +0000 | [diff] [blame] | 3340 | case PCC_Expression: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3341 | case PCC_ForInit: |
| 3342 | case PCC_Condition: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3343 | if (WantTypesInContext(CompletionContext, getLangOpts())) |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 3344 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 3345 | else |
| 3346 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3347 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3348 | if (getLangOpts().CPlusPlus) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3349 | MaybeAddOverrideCalls(*this, /*InContext=*/nullptr, Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3350 | break; |
Douglas Gregor | 6da3db4 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 3351 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3352 | case PCC_RecoveryInFunction: |
Douglas Gregor | 6da3db4 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 3353 | // Unfiltered |
| 3354 | break; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3357 | // If we are in a C++ non-static member function, check the qualifiers on |
| 3358 | // the member function to filter/prioritize the results list. |
| 3359 | if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext)) |
| 3360 | if (CurMethod->isInstance()) |
| 3361 | Results.setObjectTypeQualifiers( |
| 3362 | Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers())); |
| 3363 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 3364 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3365 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3366 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 3367 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3368 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); |
Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 3369 | Results.ExitScope(); |
| 3370 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3371 | switch (CompletionContext) { |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3372 | case PCC_ParenthesizedExpression: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3373 | case PCC_Expression: |
| 3374 | case PCC_Statement: |
| 3375 | case PCC_RecoveryInFunction: |
| 3376 | if (S->getFnParent()) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 3377 | AddPrettyFunctionResults(getLangOpts(), Results); |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3378 | break; |
| 3379 | |
| 3380 | case PCC_Namespace: |
| 3381 | case PCC_Class: |
| 3382 | case PCC_ObjCInterface: |
| 3383 | case PCC_ObjCImplementation: |
| 3384 | case PCC_ObjCInstanceVariableList: |
| 3385 | case PCC_Template: |
| 3386 | case PCC_MemberTemplate: |
| 3387 | case PCC_ForInit: |
| 3388 | case PCC_Condition: |
| 3389 | case PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3390 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3391 | break; |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3392 | } |
| 3393 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3394 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3395 | AddMacroResults(PP, Results, false); |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3396 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 3397 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3398 | Results.data(),Results.size()); |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3401 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| 3402 | ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 3403 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 3404 | bool AtArgumentExpression, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3405 | bool IsSuper, |
| 3406 | ResultBuilder &Results); |
| 3407 | |
| 3408 | void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, |
| 3409 | bool AllowNonIdentifiers, |
| 3410 | bool AllowNestedNameSpecifiers) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3411 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3412 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3413 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3414 | AllowNestedNameSpecifiers |
| 3415 | ? CodeCompletionContext::CCC_PotentiallyQualifiedName |
| 3416 | : CodeCompletionContext::CCC_Name); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3417 | Results.EnterNewScope(); |
| 3418 | |
| 3419 | // Type qualifiers can come after names. |
| 3420 | Results.AddResult(Result("const")); |
| 3421 | Results.AddResult(Result("volatile")); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3422 | if (getLangOpts().C99) |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3423 | Results.AddResult(Result("restrict")); |
| 3424 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3425 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3426 | if (AllowNonIdentifiers) { |
| 3427 | Results.AddResult(Result("operator")); |
| 3428 | } |
| 3429 | |
| 3430 | // Add nested-name-specifiers. |
| 3431 | if (AllowNestedNameSpecifiers) { |
| 3432 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3433 | Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3434 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3435 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
| 3436 | CodeCompleter->includeGlobals()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3437 | Results.setFilter(nullptr); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3438 | } |
| 3439 | } |
| 3440 | Results.ExitScope(); |
| 3441 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3442 | // If we're in a context where we might have an expression (rather than a |
| 3443 | // declaration), and what we've seen so far is an Objective-C type that could |
| 3444 | // be a receiver of a class message, this may be a class message send with |
| 3445 | // the initial opening bracket '[' missing. Add appropriate completions. |
| 3446 | if (AllowNonIdentifiers && !AllowNestedNameSpecifiers && |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3447 | DS.getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3448 | DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3449 | DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified && |
| 3450 | DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3451 | !DS.isTypeAltiVecVector() && |
| 3452 | S && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3453 | (S->getFlags() & Scope::DeclScope) != 0 && |
| 3454 | (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope | |
| 3455 | Scope::FunctionPrototypeScope | |
| 3456 | Scope::AtCatchScope)) == 0) { |
| 3457 | ParsedType T = DS.getRepAsType(); |
| 3458 | if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType()) |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 3459 | AddClassMessageCompletions(*this, S, T, None, false, false, Results); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3460 | } |
| 3461 | |
Douglas Gregor | 56ccce0 | 2010-08-24 04:59:56 +0000 | [diff] [blame] | 3462 | // Note that we intentionally suppress macro results here, since we do not |
| 3463 | // encourage using macros to produce the names of entities. |
| 3464 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3465 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3466 | Results.getCompletionContext(), |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3467 | Results.data(), Results.size()); |
| 3468 | } |
| 3469 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3470 | struct Sema::CodeCompleteExpressionData { |
| 3471 | CodeCompleteExpressionData(QualType PreferredType = QualType()) |
| 3472 | : PreferredType(PreferredType), IntegralConstantExpression(false), |
| 3473 | ObjCCollection(false) { } |
| 3474 | |
| 3475 | QualType PreferredType; |
| 3476 | bool IntegralConstantExpression; |
| 3477 | bool ObjCCollection; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3478 | SmallVector<Decl *, 4> IgnoreDecls; |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3479 | }; |
| 3480 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3481 | /// \brief Perform code-completion in an expression context when we know what |
| 3482 | /// type we're looking for. |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3483 | void Sema::CodeCompleteExpression(Scope *S, |
| 3484 | const CodeCompleteExpressionData &Data) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3485 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3486 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3487 | CodeCompletionContext::CCC_Expression); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3488 | if (Data.ObjCCollection) |
| 3489 | Results.setFilter(&ResultBuilder::IsObjCCollection); |
| 3490 | else if (Data.IntegralConstantExpression) |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3491 | Results.setFilter(&ResultBuilder::IsIntegralConstantValue); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3492 | else if (WantTypesInContext(PCC_Expression, getLangOpts())) |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3493 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 3494 | else |
| 3495 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3496 | |
| 3497 | if (!Data.PreferredType.isNull()) |
| 3498 | Results.setPreferredType(Data.PreferredType.getNonReferenceType()); |
| 3499 | |
| 3500 | // Ignore any declarations that we were told that we don't care about. |
| 3501 | for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I) |
| 3502 | Results.Ignore(Data.IgnoreDecls[I]); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3503 | |
| 3504 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3505 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3506 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3507 | |
| 3508 | Results.EnterNewScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3509 | AddOrdinaryNameResults(PCC_Expression, S, *this, Results); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3510 | Results.ExitScope(); |
| 3511 | |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3512 | bool PreferredTypeIsPointer = false; |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3513 | if (!Data.PreferredType.isNull()) |
| 3514 | PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType() |
| 3515 | || Data.PreferredType->isMemberPointerType() |
| 3516 | || Data.PreferredType->isBlockPointerType(); |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3517 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3518 | if (S->getFnParent() && |
| 3519 | !Data.ObjCCollection && |
| 3520 | !Data.IntegralConstantExpression) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 3521 | AddPrettyFunctionResults(getLangOpts(), Results); |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3522 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3523 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3524 | AddMacroResults(PP, Results, false, PreferredTypeIsPointer); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3525 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3526 | CodeCompletionContext(CodeCompletionContext::CCC_Expression, |
| 3527 | Data.PreferredType), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3528 | Results.data(),Results.size()); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
Douglas Gregor | eda7e54 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 3531 | void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) { |
| 3532 | if (E.isInvalid()) |
| 3533 | CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3534 | else if (getLangOpts().ObjC1) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3535 | CodeCompleteObjCInstanceMessage(S, E.get(), None, false); |
Douglas Gregor | ed0b69d | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 3536 | } |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3537 | |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3538 | /// \brief The set of properties that have already been added, referenced by |
| 3539 | /// property name. |
| 3540 | typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet; |
| 3541 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3542 | /// \brief Retrieve the container definition, if any? |
| 3543 | static ObjCContainerDecl *getContainerDef(ObjCContainerDecl *Container) { |
| 3544 | if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 3545 | if (Interface->hasDefinition()) |
| 3546 | return Interface->getDefinition(); |
| 3547 | |
| 3548 | return Interface; |
| 3549 | } |
| 3550 | |
| 3551 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 3552 | if (Protocol->hasDefinition()) |
| 3553 | return Protocol->getDefinition(); |
| 3554 | |
| 3555 | return Protocol; |
| 3556 | } |
| 3557 | return Container; |
| 3558 | } |
| 3559 | |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3560 | static void AddObjCProperties(const CodeCompletionContext &CCContext, |
| 3561 | ObjCContainerDecl *Container, |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3562 | bool AllowCategories, |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3563 | bool AllowNullaryMethods, |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3564 | DeclContext *CurContext, |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3565 | AddedPropertiesSet &AddedProperties, |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3566 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3567 | typedef CodeCompletionResult Result; |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3568 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3569 | // Retrieve the definition. |
| 3570 | Container = getContainerDef(Container); |
| 3571 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3572 | // Add properties in this container. |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame^] | 3573 | for (const auto *P : Container->instance_properties()) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 3574 | if (AddedProperties.insert(P->getIdentifier()).second) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3575 | Results.MaybeAddResult(Result(P, Results.getBasePriority(P), nullptr), |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 3576 | CurContext); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3577 | |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3578 | // Add nullary methods |
| 3579 | if (AllowNullaryMethods) { |
| 3580 | ASTContext &Context = Container->getASTContext(); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3581 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 3582 | for (auto *M : Container->methods()) { |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3583 | if (M->getSelector().isUnarySelector()) |
| 3584 | if (IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 3585 | if (AddedProperties.insert(Name).second) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3586 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3587 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3588 | AddResultTypeChunk(Context, Policy, M, CCContext.getBaseType(), |
| 3589 | Builder); |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3590 | Builder.AddTypedTextChunk( |
| 3591 | Results.getAllocator().CopyString(Name->getName())); |
| 3592 | |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 3593 | Results.MaybeAddResult(Result(Builder.TakeString(), M, |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3594 | CCP_MemberDeclaration + CCD_MethodAsProperty), |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3595 | CurContext); |
| 3596 | } |
| 3597 | } |
| 3598 | } |
| 3599 | |
| 3600 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3601 | // Add properties in referenced protocols. |
| 3602 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 3603 | for (auto *P : Protocol->protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3604 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, |
| 3605 | CurContext, AddedProperties, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3606 | } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){ |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3607 | if (AllowCategories) { |
| 3608 | // Look through categories. |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 3609 | for (auto *Cat : IFace->known_categories()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3610 | AddObjCProperties(CCContext, Cat, AllowCategories, AllowNullaryMethods, |
| 3611 | CurContext, AddedProperties, Results); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3612 | } |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 3613 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3614 | // Look through protocols. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 3615 | for (auto *I : IFace->all_referenced_protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3616 | AddObjCProperties(CCContext, I, AllowCategories, AllowNullaryMethods, |
| 3617 | CurContext, AddedProperties, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3618 | |
| 3619 | // Look in the superclass. |
| 3620 | if (IFace->getSuperClass()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3621 | AddObjCProperties(CCContext, IFace->getSuperClass(), AllowCategories, |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3622 | AllowNullaryMethods, CurContext, |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3623 | AddedProperties, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3624 | } else if (const ObjCCategoryDecl *Category |
| 3625 | = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 3626 | // Look through protocols. |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 3627 | for (auto *P : Category->protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3628 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, |
| 3629 | CurContext, AddedProperties, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3630 | } |
| 3631 | } |
| 3632 | |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 3633 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base, |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3634 | SourceLocation OpLoc, |
| 3635 | bool IsArrow) { |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 3636 | if (!Base || !CodeCompleter) |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3637 | return; |
| 3638 | |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 3639 | ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow); |
| 3640 | if (ConvertedBase.isInvalid()) |
| 3641 | return; |
| 3642 | Base = ConvertedBase.get(); |
| 3643 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3644 | typedef CodeCompletionResult Result; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3645 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3646 | QualType BaseType = Base->getType(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3647 | |
| 3648 | if (IsArrow) { |
| 3649 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 3650 | BaseType = Ptr->getPointeeType(); |
| 3651 | else if (BaseType->isObjCObjectPointerType()) |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3652 | /*Do nothing*/ ; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3653 | else |
| 3654 | return; |
| 3655 | } |
| 3656 | |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3657 | enum CodeCompletionContext::Kind contextKind; |
| 3658 | |
| 3659 | if (IsArrow) { |
| 3660 | contextKind = CodeCompletionContext::CCC_ArrowMemberAccess; |
| 3661 | } |
| 3662 | else { |
| 3663 | if (BaseType->isObjCObjectPointerType() || |
| 3664 | BaseType->isObjCObjectOrInterfaceType()) { |
| 3665 | contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess; |
| 3666 | } |
| 3667 | else { |
| 3668 | contextKind = CodeCompletionContext::CCC_DotMemberAccess; |
| 3669 | } |
| 3670 | } |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3671 | |
| 3672 | CodeCompletionContext CCContext(contextKind, BaseType); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3673 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3674 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3675 | CCContext, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3676 | &ResultBuilder::IsMember); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3677 | Results.EnterNewScope(); |
| 3678 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3679 | // Indicate that we are performing a member access, and the cv-qualifiers |
| 3680 | // for the base object type. |
| 3681 | Results.setObjectTypeQualifiers(BaseType.getQualifiers()); |
| 3682 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3683 | // Access to a C/C++ class, struct, or union. |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 3684 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3685 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3686 | LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer, |
| 3687 | CodeCompleter->includeGlobals()); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3688 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3689 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3690 | if (!Results.empty()) { |
| 3691 | // The "template" keyword can follow "->" or "." in the grammar. |
| 3692 | // However, we only want to suggest the template keyword if something |
| 3693 | // is dependent. |
| 3694 | bool IsDependent = BaseType->isDependentType(); |
| 3695 | if (!IsDependent) { |
| 3696 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 3697 | if (DeclContext *Ctx = DepScope->getEntity()) { |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3698 | IsDependent = Ctx->isDependentContext(); |
| 3699 | break; |
| 3700 | } |
| 3701 | } |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3702 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3703 | if (IsDependent) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3704 | Results.AddResult(Result("template")); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3705 | } |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3706 | } |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3707 | } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) { |
| 3708 | // Objective-C property reference. |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3709 | AddedPropertiesSet AddedProperties; |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3710 | |
| 3711 | // Add property results based on our interface. |
| 3712 | const ObjCObjectPointerType *ObjCPtr |
| 3713 | = BaseType->getAsObjCInterfacePointerType(); |
| 3714 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!"); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3715 | AddObjCProperties(CCContext, ObjCPtr->getInterfaceDecl(), true, |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3716 | /*AllowNullaryMethods=*/true, CurContext, |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3717 | AddedProperties, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3718 | |
| 3719 | // Add properties from the protocols in a qualified interface. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 3720 | for (auto *I : ObjCPtr->quals()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3721 | AddObjCProperties(CCContext, I, true, /*AllowNullaryMethods=*/true, |
| 3722 | CurContext, AddedProperties, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3723 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3724 | (!IsArrow && BaseType->isObjCObjectType())) { |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3725 | // Objective-C instance variable access. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3726 | ObjCInterfaceDecl *Class = nullptr; |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3727 | if (const ObjCObjectPointerType *ObjCPtr |
| 3728 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 3729 | Class = ObjCPtr->getInterfaceDecl(); |
| 3730 | else |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3731 | Class = BaseType->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3732 | |
| 3733 | // Add all ivars from this class and its superclasses. |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 3734 | if (Class) { |
| 3735 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3736 | Results.setFilter(&ResultBuilder::IsObjCIvar); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3737 | LookupVisibleDecls(Class, LookupMemberName, Consumer, |
| 3738 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3739 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3740 | } |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3741 | |
| 3742 | // FIXME: How do we cope with isa? |
| 3743 | |
| 3744 | Results.ExitScope(); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3745 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3746 | // Hand off the results found for code completion. |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3747 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3748 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3749 | Results.data(),Results.size()); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3752 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { |
| 3753 | if (!CodeCompleter) |
| 3754 | return; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3755 | |
| 3756 | ResultBuilder::LookupFilter Filter = nullptr; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3757 | enum CodeCompletionContext::Kind ContextKind |
| 3758 | = CodeCompletionContext::CCC_Other; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3759 | switch ((DeclSpec::TST)TagSpec) { |
| 3760 | case DeclSpec::TST_enum: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3761 | Filter = &ResultBuilder::IsEnum; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3762 | ContextKind = CodeCompletionContext::CCC_EnumTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3763 | break; |
| 3764 | |
| 3765 | case DeclSpec::TST_union: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3766 | Filter = &ResultBuilder::IsUnion; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3767 | ContextKind = CodeCompletionContext::CCC_UnionTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3768 | break; |
| 3769 | |
| 3770 | case DeclSpec::TST_struct: |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3771 | case DeclSpec::TST_class: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3772 | case DeclSpec::TST_interface: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3773 | Filter = &ResultBuilder::IsClassOrStruct; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3774 | ContextKind = CodeCompletionContext::CCC_ClassOrStructTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3775 | break; |
| 3776 | |
| 3777 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3778 | llvm_unreachable("Unknown type specifier kind in CodeCompleteTag"); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3779 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3780 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3781 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3782 | CodeCompleter->getCodeCompletionTUInfo(), ContextKind); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3783 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 3784 | |
| 3785 | // First pass: look for tags. |
| 3786 | Results.setFilter(Filter); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3787 | LookupVisibleDecls(S, LookupTagName, Consumer, |
| 3788 | CodeCompleter->includeGlobals()); |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 3789 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3790 | if (CodeCompleter->includeGlobals()) { |
| 3791 | // Second pass: look for nested name specifiers. |
| 3792 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
| 3793 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer); |
| 3794 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3795 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3796 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3797 | Results.data(),Results.size()); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3798 | } |
| 3799 | |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3800 | void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3801 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3802 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3803 | CodeCompletionContext::CCC_TypeQualifiers); |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3804 | Results.EnterNewScope(); |
| 3805 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const)) |
| 3806 | Results.AddResult("const"); |
| 3807 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile)) |
| 3808 | Results.AddResult("volatile"); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3809 | if (getLangOpts().C99 && |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3810 | !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict)) |
| 3811 | Results.AddResult("restrict"); |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3812 | if (getLangOpts().C11 && |
| 3813 | !(DS.getTypeQualifiers() & DeclSpec::TQ_atomic)) |
| 3814 | Results.AddResult("_Atomic"); |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3815 | Results.ExitScope(); |
| 3816 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3817 | Results.getCompletionContext(), |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3818 | Results.data(), Results.size()); |
| 3819 | } |
| 3820 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3821 | void Sema::CodeCompleteCase(Scope *S) { |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3822 | if (getCurFunction()->SwitchStack.empty() || !CodeCompleter) |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3823 | return; |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 3824 | |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3825 | SwitchStmt *Switch = getCurFunction()->SwitchStack.back(); |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 3826 | QualType type = Switch->getCond()->IgnoreImplicit()->getType(); |
| 3827 | if (!type->isEnumeralType()) { |
| 3828 | CodeCompleteExpressionData Data(type); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3829 | Data.IntegralConstantExpression = true; |
| 3830 | CodeCompleteExpression(S, Data); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3831 | return; |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3832 | } |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3833 | |
| 3834 | // Code-complete the cases of a switch statement over an enumeration type |
| 3835 | // by providing the list of |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 3836 | EnumDecl *Enum = type->castAs<EnumType>()->getDecl(); |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3837 | if (EnumDecl *Def = Enum->getDefinition()) |
| 3838 | Enum = Def; |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3839 | |
| 3840 | // Determine which enumerators we have already seen in the switch statement. |
| 3841 | // FIXME: Ideally, we would also be able to look *past* the code-completion |
| 3842 | // token, in case we are code-completing in the middle of the switch and not |
| 3843 | // at the end. However, we aren't able to do so at the moment. |
| 3844 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3845 | NestedNameSpecifier *Qualifier = nullptr; |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3846 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; |
| 3847 | SC = SC->getNextSwitchCase()) { |
| 3848 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); |
| 3849 | if (!Case) |
| 3850 | continue; |
| 3851 | |
| 3852 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); |
| 3853 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal)) |
| 3854 | if (EnumConstantDecl *Enumerator |
| 3855 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 3856 | // We look into the AST of the case statement to determine which |
| 3857 | // enumerator was named. Alternatively, we could compute the value of |
| 3858 | // the integral constant expression, then compare it against the |
| 3859 | // values of each enumerator. However, value-based approach would not |
| 3860 | // work as well with C++ templates where enumerators declared within a |
| 3861 | // template are type- and value-dependent. |
| 3862 | EnumeratorsSeen.insert(Enumerator); |
| 3863 | |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3864 | // If this is a qualified-id, keep track of the nested-name-specifier |
| 3865 | // 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] | 3866 | // |
| 3867 | // switch (TagD.getKind()) { |
| 3868 | // case TagDecl::TK_enum: |
| 3869 | // break; |
| 3870 | // case XXX |
| 3871 | // |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3872 | // At the XXX, our completions are TagDecl::TK_union, |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3873 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, |
| 3874 | // TK_struct, and TK_class. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3875 | Qualifier = DRE->getQualifier(); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3876 | } |
| 3877 | } |
| 3878 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3879 | if (getLangOpts().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3880 | // If there are no prior enumerators in C++, check whether we have to |
| 3881 | // qualify the names of the enumerators that we suggest, because they |
| 3882 | // may not be visible in this scope. |
Douglas Gregor | d3cebdb | 2012-02-01 05:02:47 +0000 | [diff] [blame] | 3883 | Qualifier = getRequiredQualification(Context, CurContext, Enum); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3886 | // Add any enumerators that have not yet been mentioned. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3887 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3888 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3889 | CodeCompletionContext::CCC_Expression); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3890 | Results.EnterNewScope(); |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 3891 | for (auto *E : Enum->enumerators()) { |
| 3892 | if (EnumeratorsSeen.count(E)) |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3893 | continue; |
| 3894 | |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 3895 | CodeCompletionResult R(E, CCP_EnumInCase, Qualifier); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3896 | Results.AddResult(R, CurContext, nullptr, false); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3897 | } |
| 3898 | Results.ExitScope(); |
Douglas Gregor | 28556092 | 2010-04-06 20:02:15 +0000 | [diff] [blame] | 3899 | |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3900 | //We need to make sure we're setting the right context, |
| 3901 | //so only say we include macros if the code completer says we do |
| 3902 | enum CodeCompletionContext::Kind kind = CodeCompletionContext::CCC_Other; |
| 3903 | if (CodeCompleter->includeMacros()) { |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3904 | AddMacroResults(PP, Results, false); |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3905 | kind = CodeCompletionContext::CCC_OtherWithMacros; |
| 3906 | } |
| 3907 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3908 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3909 | kind, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3910 | Results.data(),Results.size()); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3911 | } |
| 3912 | |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 3913 | static bool anyNullArguments(ArrayRef<Expr *> Args) { |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3914 | if (Args.size() && !Args.data()) |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3915 | return true; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3916 | |
| 3917 | for (unsigned I = 0; I != Args.size(); ++I) |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3918 | if (!Args[I]) |
| 3919 | return true; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3920 | |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3921 | return false; |
| 3922 | } |
| 3923 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3924 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; |
| 3925 | |
Francisco Lopes da Silva | 8cafefa | 2015-01-29 05:54:59 +0000 | [diff] [blame] | 3926 | static void mergeCandidatesWithResults(Sema &SemaRef, |
| 3927 | SmallVectorImpl<ResultCandidate> &Results, |
| 3928 | OverloadCandidateSet &CandidateSet, |
| 3929 | SourceLocation Loc) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3930 | if (!CandidateSet.empty()) { |
| 3931 | // Sort the overload candidate set by placing the best overloads first. |
| 3932 | std::stable_sort( |
| 3933 | CandidateSet.begin(), CandidateSet.end(), |
| 3934 | [&](const OverloadCandidate &X, const OverloadCandidate &Y) { |
| 3935 | return isBetterOverloadCandidate(SemaRef, X, Y, Loc); |
| 3936 | }); |
| 3937 | |
| 3938 | // Add the remaining viable overload candidates as code-completion results. |
| 3939 | for (auto &Candidate : CandidateSet) |
| 3940 | if (Candidate.Viable) |
| 3941 | Results.push_back(ResultCandidate(Candidate.Function)); |
| 3942 | } |
| 3943 | } |
| 3944 | |
| 3945 | /// \brief Get the type of the Nth parameter from a given set of overload |
| 3946 | /// candidates. |
Francisco Lopes da Silva | 8cafefa | 2015-01-29 05:54:59 +0000 | [diff] [blame] | 3947 | static QualType getParamType(Sema &SemaRef, |
| 3948 | ArrayRef<ResultCandidate> Candidates, |
| 3949 | unsigned N) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3950 | |
| 3951 | // Given the overloads 'Candidates' for a function call matching all arguments |
| 3952 | // up to N, return the type of the Nth parameter if it is the same for all |
| 3953 | // overload candidates. |
| 3954 | QualType ParamType; |
| 3955 | for (auto &Candidate : Candidates) { |
| 3956 | if (auto FType = Candidate.getFunctionType()) |
| 3957 | if (auto Proto = dyn_cast<FunctionProtoType>(FType)) |
| 3958 | if (N < Proto->getNumParams()) { |
| 3959 | if (ParamType.isNull()) |
| 3960 | ParamType = Proto->getParamType(N); |
| 3961 | else if (!SemaRef.Context.hasSameUnqualifiedType( |
| 3962 | ParamType.getNonReferenceType(), |
| 3963 | Proto->getParamType(N).getNonReferenceType())) |
| 3964 | // Otherwise return a default-constructed QualType. |
| 3965 | return QualType(); |
| 3966 | } |
| 3967 | } |
| 3968 | |
| 3969 | return ParamType; |
| 3970 | } |
| 3971 | |
Francisco Lopes da Silva | 8cafefa | 2015-01-29 05:54:59 +0000 | [diff] [blame] | 3972 | static void CodeCompleteOverloadResults(Sema &SemaRef, Scope *S, |
| 3973 | MutableArrayRef<ResultCandidate> Candidates, |
| 3974 | unsigned CurrentArg, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3975 | bool CompleteExpressionWithCurrentArg = true) { |
| 3976 | QualType ParamType; |
| 3977 | if (CompleteExpressionWithCurrentArg) |
| 3978 | ParamType = getParamType(SemaRef, Candidates, CurrentArg); |
| 3979 | |
| 3980 | if (ParamType.isNull()) |
| 3981 | SemaRef.CodeCompleteOrdinaryName(S, Sema::PCC_Expression); |
| 3982 | else |
| 3983 | SemaRef.CodeCompleteExpression(S, ParamType); |
| 3984 | |
| 3985 | if (!Candidates.empty()) |
| 3986 | SemaRef.CodeCompleter->ProcessOverloadCandidates(SemaRef, CurrentArg, |
| 3987 | Candidates.data(), |
| 3988 | Candidates.size()); |
| 3989 | } |
| 3990 | |
| 3991 | void Sema::CodeCompleteCall(Scope *S, Expr *Fn, ArrayRef<Expr *> Args) { |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3992 | if (!CodeCompleter) |
| 3993 | return; |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3994 | |
| 3995 | // When we're code-completing for a call, we fall back to ordinary |
| 3996 | // name code-completion whenever we can't produce specific |
| 3997 | // results. We may want to revisit this strategy in the future, |
| 3998 | // e.g., by merging the two kinds of results. |
| 3999 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4000 | // FIXME: Provide support for variadic template functions. |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4001 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4002 | // Ignore type-dependent call expressions entirely. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4003 | if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args) || |
| 4004 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4005 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4006 | return; |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4007 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4008 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4009 | // Build an overload candidate set based on the functions we find. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4010 | SourceLocation Loc = Fn->getExprLoc(); |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 4011 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4012 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4013 | SmallVector<ResultCandidate, 8> Results; |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 4014 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4015 | Expr *NakedFn = Fn->IgnoreParenCasts(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4016 | if (auto ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4017 | AddOverloadedCallCandidates(ULE, Args, CandidateSet, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4018 | /*PartialOverloading=*/true); |
| 4019 | else if (auto UME = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 4020 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
| 4021 | if (UME->hasExplicitTemplateArgs()) { |
| 4022 | UME->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 4023 | TemplateArgs = &TemplateArgsBuffer; |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 4024 | } |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4025 | SmallVector<Expr *, 12> ArgExprs(1, UME->getBase()); |
| 4026 | ArgExprs.append(Args.begin(), Args.end()); |
| 4027 | UnresolvedSet<8> Decls; |
| 4028 | Decls.append(UME->decls_begin(), UME->decls_end()); |
| 4029 | AddFunctionCandidates(Decls, ArgExprs, CandidateSet, TemplateArgs, |
| 4030 | /*SuppressUsedConversions=*/false, |
| 4031 | /*PartialOverloading=*/true); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4032 | } else { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4033 | FunctionDecl *FD = nullptr; |
| 4034 | if (auto MCE = dyn_cast<MemberExpr>(NakedFn)) |
| 4035 | FD = dyn_cast<FunctionDecl>(MCE->getMemberDecl()); |
| 4036 | else if (auto DRE = dyn_cast<DeclRefExpr>(NakedFn)) |
| 4037 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4038 | 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] | 4039 | if (!getLangOpts().CPlusPlus || |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4040 | !FD->getType()->getAs<FunctionProtoType>()) |
| 4041 | Results.push_back(ResultCandidate(FD)); |
| 4042 | else |
| 4043 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, FD->getAccess()), |
| 4044 | Args, CandidateSet, |
| 4045 | /*SuppressUsedConversions=*/false, |
| 4046 | /*PartialOverloading=*/true); |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4047 | |
| 4048 | } else if (auto DC = NakedFn->getType()->getAsCXXRecordDecl()) { |
| 4049 | // If expression's type is CXXRecordDecl, it may overload the function |
| 4050 | // 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] | 4051 | // A complete type is needed to lookup for member function call operators. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4052 | if (isCompleteType(Loc, NakedFn->getType())) { |
Francisco Lopes da Silva | a349a8a | 2015-01-25 08:47:59 +0000 | [diff] [blame] | 4053 | DeclarationName OpName = Context.DeclarationNames |
| 4054 | .getCXXOperatorName(OO_Call); |
| 4055 | LookupResult R(*this, OpName, Loc, LookupOrdinaryName); |
| 4056 | LookupQualifiedName(R, DC); |
| 4057 | R.suppressDiagnostics(); |
| 4058 | SmallVector<Expr *, 12> ArgExprs(1, NakedFn); |
| 4059 | ArgExprs.append(Args.begin(), Args.end()); |
| 4060 | AddFunctionCandidates(R.asUnresolvedSet(), ArgExprs, CandidateSet, |
| 4061 | /*ExplicitArgs=*/nullptr, |
| 4062 | /*SuppressUsedConversions=*/false, |
| 4063 | /*PartialOverloading=*/true); |
| 4064 | } |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4065 | } else { |
| 4066 | // Lastly we check whether expression's type is function pointer or |
| 4067 | // function. |
| 4068 | QualType T = NakedFn->getType(); |
| 4069 | if (!T->getPointeeType().isNull()) |
| 4070 | T = T->getPointeeType(); |
| 4071 | |
| 4072 | if (auto FP = T->getAs<FunctionProtoType>()) { |
| 4073 | if (!TooManyArguments(FP->getNumParams(), Args.size(), |
Francisco Lopes da Silva | 62a9a4f | 2015-01-23 13:17:51 +0000 | [diff] [blame] | 4074 | /*PartialOverloading=*/true) || |
| 4075 | FP->isVariadic()) |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4076 | Results.push_back(ResultCandidate(FP)); |
| 4077 | } else if (auto FT = T->getAs<FunctionType>()) |
Francisco Lopes da Silva | 62a9a4f | 2015-01-23 13:17:51 +0000 | [diff] [blame] | 4078 | // 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] | 4079 | Results.push_back(ResultCandidate(FT)); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4080 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4081 | } |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4082 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4083 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); |
| 4084 | CodeCompleteOverloadResults(*this, S, Results, Args.size(), |
| 4085 | !CandidateSet.empty()); |
| 4086 | } |
| 4087 | |
| 4088 | void Sema::CodeCompleteConstructor(Scope *S, QualType Type, SourceLocation Loc, |
| 4089 | ArrayRef<Expr *> Args) { |
| 4090 | if (!CodeCompleter) |
| 4091 | return; |
| 4092 | |
| 4093 | // A complete type is needed to lookup for constructors. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4094 | if (!isCompleteType(Loc, Type)) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4095 | return; |
| 4096 | |
Argyrios Kyrtzidis | ee1d76f | 2015-03-13 07:39:30 +0000 | [diff] [blame] | 4097 | CXXRecordDecl *RD = Type->getAsCXXRecordDecl(); |
| 4098 | if (!RD) { |
| 4099 | CodeCompleteExpression(S, Type); |
| 4100 | return; |
| 4101 | } |
| 4102 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4103 | // FIXME: Provide support for member initializers. |
| 4104 | // FIXME: Provide support for variadic template constructors. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4105 | |
| 4106 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
| 4107 | |
Argyrios Kyrtzidis | ee1d76f | 2015-03-13 07:39:30 +0000 | [diff] [blame] | 4108 | for (auto C : LookupConstructors(RD)) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4109 | if (auto FD = dyn_cast<FunctionDecl>(C)) { |
| 4110 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, C->getAccess()), |
| 4111 | Args, CandidateSet, |
| 4112 | /*SuppressUsedConversions=*/false, |
| 4113 | /*PartialOverloading=*/true); |
| 4114 | } else if (auto FTD = dyn_cast<FunctionTemplateDecl>(C)) { |
| 4115 | AddTemplateOverloadCandidate(FTD, |
| 4116 | DeclAccessPair::make(FTD, C->getAccess()), |
| 4117 | /*ExplicitTemplateArgs=*/nullptr, |
| 4118 | Args, CandidateSet, |
| 4119 | /*SuppressUsedConversions=*/false, |
| 4120 | /*PartialOverloading=*/true); |
| 4121 | } |
| 4122 | } |
| 4123 | |
| 4124 | SmallVector<ResultCandidate, 8> Results; |
| 4125 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); |
| 4126 | CodeCompleteOverloadResults(*this, S, Results, Args.size()); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4129 | void Sema::CodeCompleteInitializer(Scope *S, Decl *D) { |
| 4130 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4131 | if (!VD) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4132 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4133 | return; |
| 4134 | } |
| 4135 | |
| 4136 | CodeCompleteExpression(S, VD->getType()); |
| 4137 | } |
| 4138 | |
| 4139 | void Sema::CodeCompleteReturn(Scope *S) { |
| 4140 | QualType ResultType; |
| 4141 | if (isa<BlockDecl>(CurContext)) { |
| 4142 | if (BlockScopeInfo *BSI = getCurBlock()) |
| 4143 | ResultType = BSI->ReturnType; |
| 4144 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4145 | ResultType = Function->getReturnType(); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4146 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4147 | ResultType = Method->getReturnType(); |
| 4148 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4149 | if (ResultType.isNull()) |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4150 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4151 | else |
| 4152 | CodeCompleteExpression(S, ResultType); |
| 4153 | } |
| 4154 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4155 | void Sema::CodeCompleteAfterIf(Scope *S) { |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4156 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4157 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4158 | mapCodeCompletionContext(*this, PCC_Statement)); |
| 4159 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 4160 | Results.EnterNewScope(); |
| 4161 | |
| 4162 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4163 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4164 | CodeCompleter->includeGlobals()); |
| 4165 | |
| 4166 | AddOrdinaryNameResults(PCC_Statement, S, *this, Results); |
| 4167 | |
| 4168 | // "else" block |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4169 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4170 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4171 | Builder.AddTypedTextChunk("else"); |
Douglas Gregor | 3a5d6c2 | 2012-02-16 17:49:04 +0000 | [diff] [blame] | 4172 | if (Results.includeCodePatterns()) { |
| 4173 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4174 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4175 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4176 | Builder.AddPlaceholderChunk("statements"); |
| 4177 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4178 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4179 | } |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4180 | Results.AddResult(Builder.TakeString()); |
| 4181 | |
| 4182 | // "else if" block |
| 4183 | Builder.AddTypedTextChunk("else"); |
| 4184 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4185 | Builder.AddTextChunk("if"); |
| 4186 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4187 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4188 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4189 | Builder.AddPlaceholderChunk("condition"); |
| 4190 | else |
| 4191 | Builder.AddPlaceholderChunk("expression"); |
| 4192 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 3a5d6c2 | 2012-02-16 17:49:04 +0000 | [diff] [blame] | 4193 | if (Results.includeCodePatterns()) { |
| 4194 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4195 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4196 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4197 | Builder.AddPlaceholderChunk("statements"); |
| 4198 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4199 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4200 | } |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4201 | Results.AddResult(Builder.TakeString()); |
| 4202 | |
| 4203 | Results.ExitScope(); |
| 4204 | |
| 4205 | if (S->getFnParent()) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 4206 | AddPrettyFunctionResults(getLangOpts(), Results); |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4207 | |
| 4208 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 4209 | AddMacroResults(PP, Results, false); |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4210 | |
| 4211 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4212 | Results.data(),Results.size()); |
| 4213 | } |
| 4214 | |
Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 4215 | void Sema::CodeCompleteAssignmentRHS(Scope *S, Expr *LHS) { |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4216 | if (LHS) |
| 4217 | CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType()); |
| 4218 | else |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4219 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4222 | void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4223 | bool EnteringContext) { |
| 4224 | if (!SS.getScopeRep() || !CodeCompleter) |
| 4225 | return; |
| 4226 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4227 | DeclContext *Ctx = computeDeclContext(SS, EnteringContext); |
| 4228 | if (!Ctx) |
| 4229 | return; |
Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 4230 | |
| 4231 | // Try to instantiate any non-dependent declaration contexts before |
| 4232 | // we look in them. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4233 | if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx)) |
Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 4234 | return; |
| 4235 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4236 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4237 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4238 | CodeCompletionContext::CCC_Name); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4239 | Results.EnterNewScope(); |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4240 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4241 | // The "template" keyword can follow "::" in the grammar, but only |
| 4242 | // put it into the grammar if the nested-name-specifier is dependent. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4243 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4244 | if (!Results.empty() && NNS->isDependent()) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4245 | Results.AddResult("template"); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4246 | |
| 4247 | // Add calls to overridden virtual functions, if there are any. |
| 4248 | // |
| 4249 | // FIXME: This isn't wonderful, because we don't know whether we're actually |
| 4250 | // in a context that permits expressions. This is a general issue with |
| 4251 | // qualified-id completions. |
| 4252 | if (!EnteringContext) |
| 4253 | MaybeAddOverrideCalls(*this, Ctx, Results); |
| 4254 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4255 | |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4256 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4257 | LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer); |
| 4258 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4259 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | c1679ec | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 4260 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4261 | Results.data(),Results.size()); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4262 | } |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4263 | |
| 4264 | void Sema::CodeCompleteUsing(Scope *S) { |
| 4265 | if (!CodeCompleter) |
| 4266 | return; |
| 4267 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4268 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4269 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4270 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
| 4271 | &ResultBuilder::IsNestedNameSpecifier); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4272 | Results.EnterNewScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4273 | |
| 4274 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 4275 | if (!S->isClassScope()) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4276 | Results.AddResult(CodeCompletionResult("namespace")); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4277 | |
| 4278 | // After "using", we can see anything that would start a |
| 4279 | // nested-name-specifier. |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4280 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4281 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4282 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4283 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4284 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4285 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4286 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4287 | Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | void Sema::CodeCompleteUsingDirective(Scope *S) { |
| 4291 | if (!CodeCompleter) |
| 4292 | return; |
| 4293 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4294 | // After "using namespace", we expect to see a namespace name or namespace |
| 4295 | // alias. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4296 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4297 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4298 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4299 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4300 | Results.EnterNewScope(); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4301 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4302 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4303 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4304 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4305 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4306 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4307 | Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { |
| 4311 | if (!CodeCompleter) |
| 4312 | return; |
| 4313 | |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 4314 | DeclContext *Ctx = S->getEntity(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4315 | if (!S->getParent()) |
| 4316 | Ctx = Context.getTranslationUnitDecl(); |
| 4317 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4318 | bool SuppressedGlobalResults |
| 4319 | = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx); |
| 4320 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4321 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4322 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4323 | SuppressedGlobalResults |
| 4324 | ? CodeCompletionContext::CCC_Namespace |
| 4325 | : CodeCompletionContext::CCC_Other, |
| 4326 | &ResultBuilder::IsNamespace); |
| 4327 | |
| 4328 | if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4329 | // We only want to see those namespaces that have already been defined |
| 4330 | // within this scope, because its likely that the user is creating an |
| 4331 | // extended namespace declaration. Keep track of the most recent |
| 4332 | // definition of each namespace. |
| 4333 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
| 4334 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
| 4335 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); |
| 4336 | NS != NSEnd; ++NS) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4337 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4338 | |
| 4339 | // Add the most recent definition (or extended definition) of each |
| 4340 | // namespace to the list of results. |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4341 | Results.EnterNewScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4342 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 4343 | NS = OrigToLatest.begin(), |
| 4344 | NSEnd = OrigToLatest.end(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4345 | NS != NSEnd; ++NS) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 4346 | Results.AddResult(CodeCompletionResult( |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4347 | NS->second, Results.getBasePriority(NS->second), |
| 4348 | nullptr), |
| 4349 | CurContext, nullptr, false); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4350 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4351 | } |
| 4352 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4353 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4354 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4355 | Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { |
| 4359 | if (!CodeCompleter) |
| 4360 | return; |
| 4361 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4362 | // After "namespace", we expect to see a namespace or alias. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4363 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4364 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4365 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4366 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4367 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4368 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4369 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4370 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4371 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4372 | Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 4375 | void Sema::CodeCompleteOperatorName(Scope *S) { |
| 4376 | if (!CodeCompleter) |
| 4377 | return; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4378 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4379 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4380 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4381 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4382 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4383 | &ResultBuilder::IsType); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4384 | Results.EnterNewScope(); |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 4385 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4386 | // Add the names of overloadable operators. |
| 4387 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 4388 | if (std::strcmp(Spelling, "?")) \ |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4389 | Results.AddResult(Result(Spelling)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4390 | #include "clang/Basic/OperatorKinds.def" |
| 4391 | |
| 4392 | // Add any type names visible from the current scope |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 4393 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4394 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4395 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4396 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4397 | |
| 4398 | // Add any type specifiers |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4399 | AddTypeSpecifierResults(getLangOpts(), Results); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4400 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4401 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4402 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4403 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4404 | Results.data(),Results.size()); |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 4405 | } |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4406 | |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4407 | void Sema::CodeCompleteConstructorInitializer( |
| 4408 | Decl *ConstructorD, |
| 4409 | ArrayRef <CXXCtorInitializer *> Initializers) { |
Benjamin Kramer | a4f8df0 | 2015-07-09 15:31:10 +0000 | [diff] [blame] | 4410 | if (!ConstructorD) |
| 4411 | return; |
| 4412 | |
| 4413 | AdjustDeclIfTemplate(ConstructorD); |
| 4414 | |
| 4415 | CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4416 | if (!Constructor) |
| 4417 | return; |
| 4418 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4419 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4420 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4421 | CodeCompletionContext::CCC_PotentiallyQualifiedName); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4422 | Results.EnterNewScope(); |
| 4423 | |
| 4424 | // Fill in any already-initialized fields or base classes. |
| 4425 | llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields; |
| 4426 | llvm::SmallPtrSet<CanQualType, 4> InitializedBases; |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4427 | for (unsigned I = 0, E = Initializers.size(); I != E; ++I) { |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4428 | if (Initializers[I]->isBaseInitializer()) |
| 4429 | InitializedBases.insert( |
| 4430 | Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0))); |
| 4431 | else |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4432 | InitializedFields.insert(cast<FieldDecl>( |
| 4433 | Initializers[I]->getAnyMember())); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | // Add completions for base classes. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4437 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4438 | Results.getCodeCompletionTUInfo()); |
Benjamin Kramer | a4f8df0 | 2015-07-09 15:31:10 +0000 | [diff] [blame] | 4439 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4440 | bool SawLastInitializer = Initializers.empty(); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4441 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4442 | for (const auto &Base : ClassDecl->bases()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4443 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) |
| 4444 | .second) { |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4445 | SawLastInitializer |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4446 | = !Initializers.empty() && |
| 4447 | Initializers.back()->isBaseInitializer() && |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4448 | Context.hasSameUnqualifiedType(Base.getType(), |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4449 | QualType(Initializers.back()->getBaseClass(), 0)); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4450 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4451 | } |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4452 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4453 | Builder.AddTypedTextChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4454 | Results.getAllocator().CopyString( |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4455 | Base.getType().getAsString(Policy))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4456 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4457 | Builder.AddPlaceholderChunk("args"); |
| 4458 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4459 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4460 | SawLastInitializer? CCP_NextInitializer |
| 4461 | : CCP_MemberDeclaration)); |
| 4462 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4463 | } |
| 4464 | |
| 4465 | // Add completions for virtual base classes. |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4466 | for (const auto &Base : ClassDecl->vbases()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4467 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) |
| 4468 | .second) { |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4469 | SawLastInitializer |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4470 | = !Initializers.empty() && |
| 4471 | Initializers.back()->isBaseInitializer() && |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4472 | Context.hasSameUnqualifiedType(Base.getType(), |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4473 | QualType(Initializers.back()->getBaseClass(), 0)); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4474 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4475 | } |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4476 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4477 | Builder.AddTypedTextChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4478 | Builder.getAllocator().CopyString( |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4479 | Base.getType().getAsString(Policy))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4480 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4481 | Builder.AddPlaceholderChunk("args"); |
| 4482 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4483 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4484 | SawLastInitializer? CCP_NextInitializer |
| 4485 | : CCP_MemberDeclaration)); |
| 4486 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | // Add completions for members. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4490 | for (auto *Field : ClassDecl->fields()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4491 | if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl())) |
| 4492 | .second) { |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4493 | SawLastInitializer |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4494 | = !Initializers.empty() && |
| 4495 | Initializers.back()->isAnyMemberInitializer() && |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4496 | Initializers.back()->getAnyMember() == Field; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4497 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4498 | } |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4499 | |
| 4500 | if (!Field->getDeclName()) |
| 4501 | continue; |
| 4502 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4503 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4504 | Field->getIdentifier()->getName())); |
| 4505 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4506 | Builder.AddPlaceholderChunk("args"); |
| 4507 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4508 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4509 | SawLastInitializer? CCP_NextInitializer |
Douglas Gregor | f3af311 | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 4510 | : CCP_MemberDeclaration, |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 4511 | CXCursor_MemberRef, |
| 4512 | CXAvailability_Available, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4513 | Field)); |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4514 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4515 | } |
| 4516 | Results.ExitScope(); |
| 4517 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4518 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4519 | Results.data(), Results.size()); |
| 4520 | } |
| 4521 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4522 | /// \brief Determine whether this scope denotes a namespace. |
| 4523 | static bool isNamespaceScope(Scope *S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 4524 | DeclContext *DC = S->getEntity(); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4525 | if (!DC) |
| 4526 | return false; |
| 4527 | |
| 4528 | return DC->isFileContext(); |
| 4529 | } |
| 4530 | |
| 4531 | void Sema::CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro, |
| 4532 | bool AfterAmpersand) { |
| 4533 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4534 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4535 | CodeCompletionContext::CCC_Other); |
| 4536 | Results.EnterNewScope(); |
| 4537 | |
| 4538 | // Note what has already been captured. |
| 4539 | llvm::SmallPtrSet<IdentifierInfo *, 4> Known; |
| 4540 | bool IncludedThis = false; |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 4541 | for (const auto &C : Intro.Captures) { |
| 4542 | if (C.Kind == LCK_This) { |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4543 | IncludedThis = true; |
| 4544 | continue; |
| 4545 | } |
| 4546 | |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 4547 | Known.insert(C.Id); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
| 4550 | // Look for other capturable variables. |
| 4551 | for (; S && !isNamespaceScope(S); S = S->getParent()) { |
Aaron Ballman | 35c5495 | 2014-03-17 16:55:25 +0000 | [diff] [blame] | 4552 | for (const auto *D : S->decls()) { |
| 4553 | const auto *Var = dyn_cast<VarDecl>(D); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4554 | if (!Var || |
| 4555 | !Var->hasLocalStorage() || |
| 4556 | Var->hasAttr<BlocksAttr>()) |
| 4557 | continue; |
| 4558 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4559 | if (Known.insert(Var->getIdentifier()).second) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 4560 | Results.AddResult(CodeCompletionResult(Var, CCP_LocalDeclaration), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4561 | CurContext, nullptr, false); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4562 | } |
| 4563 | } |
| 4564 | |
| 4565 | // Add 'this', if it would be valid. |
| 4566 | if (!IncludedThis && !AfterAmpersand && Intro.Default != LCD_ByCopy) |
| 4567 | addThisCompletion(*this, Results); |
| 4568 | |
| 4569 | Results.ExitScope(); |
| 4570 | |
| 4571 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4572 | Results.data(), Results.size()); |
| 4573 | } |
| 4574 | |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4575 | /// Macro that optionally prepends an "@" to the string literal passed in via |
| 4576 | /// Keyword, depending on whether NeedAt is true or false. |
| 4577 | #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) ((NeedAt)? "@" Keyword : Keyword) |
| 4578 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4579 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4580 | ResultBuilder &Results, |
| 4581 | bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4582 | typedef CodeCompletionResult Result; |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4583 | // Since we have an implementation, we can end it. |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4584 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4585 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4586 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4587 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4588 | if (LangOpts.ObjC2) { |
| 4589 | // @dynamic |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4590 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"dynamic")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4591 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4592 | Builder.AddPlaceholderChunk("property"); |
| 4593 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4594 | |
| 4595 | // @synthesize |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4596 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synthesize")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4597 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4598 | Builder.AddPlaceholderChunk("property"); |
| 4599 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4600 | } |
| 4601 | } |
| 4602 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4603 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4604 | ResultBuilder &Results, |
| 4605 | bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4606 | typedef CodeCompletionResult Result; |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4607 | |
| 4608 | // Since we have an interface or protocol, we can end it. |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4609 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4610 | |
| 4611 | if (LangOpts.ObjC2) { |
| 4612 | // @property |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4613 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"property"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4614 | |
| 4615 | // @required |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4616 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"required"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4617 | |
| 4618 | // @optional |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4619 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"optional"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4620 | } |
| 4621 | } |
| 4622 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4623 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4624 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4625 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4626 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4627 | |
| 4628 | // @class name ; |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4629 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"class")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4630 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4631 | Builder.AddPlaceholderChunk("name"); |
| 4632 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4633 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4634 | if (Results.includeCodePatterns()) { |
| 4635 | // @interface name |
| 4636 | // FIXME: Could introduce the whole pattern, including superclasses and |
| 4637 | // such. |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4638 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"interface")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4639 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4640 | Builder.AddPlaceholderChunk("class"); |
| 4641 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4642 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4643 | // @protocol name |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4644 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4645 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4646 | Builder.AddPlaceholderChunk("protocol"); |
| 4647 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4648 | |
| 4649 | // @implementation name |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4650 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"implementation")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4651 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4652 | Builder.AddPlaceholderChunk("class"); |
| 4653 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4654 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4655 | |
| 4656 | // @compatibility_alias name |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4657 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"compatibility_alias")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4658 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4659 | Builder.AddPlaceholderChunk("alias"); |
| 4660 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4661 | Builder.AddPlaceholderChunk("class"); |
| 4662 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 61e3681 | 2013-03-07 23:26:24 +0000 | [diff] [blame] | 4663 | |
| 4664 | if (Results.getSema().getLangOpts().Modules) { |
| 4665 | // @import name |
| 4666 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "import")); |
| 4667 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4668 | Builder.AddPlaceholderChunk("module"); |
| 4669 | Results.AddResult(Result(Builder.TakeString())); |
| 4670 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4671 | } |
| 4672 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4673 | void Sema::CodeCompleteObjCAtDirective(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4674 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4675 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4676 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4677 | Results.EnterNewScope(); |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4678 | if (isa<ObjCImplDecl>(CurContext)) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4679 | AddObjCImplementationResults(getLangOpts(), Results, false); |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4680 | else if (CurContext->isObjCContainer()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4681 | AddObjCInterfaceResults(getLangOpts(), Results, false); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4682 | else |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4683 | AddObjCTopLevelResults(Results, false); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4684 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4685 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4686 | CodeCompletionContext::CCC_Other, |
| 4687 | Results.data(),Results.size()); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4688 | } |
| 4689 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4690 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4691 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4692 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4693 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4694 | |
| 4695 | // @encode ( type-name ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 4696 | const char *EncodeType = "char[]"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4697 | if (Results.getSema().getLangOpts().CPlusPlus || |
| 4698 | Results.getSema().getLangOpts().ConstStrings) |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4699 | EncodeType = "const char[]"; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 4700 | Builder.AddResultTypeChunk(EncodeType); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4701 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"encode")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4702 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4703 | Builder.AddPlaceholderChunk("type-name"); |
| 4704 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4705 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4706 | |
| 4707 | // @protocol ( protocol-name ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 4708 | Builder.AddResultTypeChunk("Protocol *"); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4709 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4710 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4711 | Builder.AddPlaceholderChunk("protocol-name"); |
| 4712 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4713 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4714 | |
| 4715 | // @selector ( selector ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 4716 | Builder.AddResultTypeChunk("SEL"); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4717 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"selector")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4718 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4719 | Builder.AddPlaceholderChunk("selector"); |
| 4720 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4721 | Results.AddResult(Result(Builder.TakeString())); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4722 | |
| 4723 | // @"string" |
| 4724 | Builder.AddResultTypeChunk("NSString *"); |
| 4725 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"\"")); |
| 4726 | Builder.AddPlaceholderChunk("string"); |
| 4727 | Builder.AddTextChunk("\""); |
| 4728 | Results.AddResult(Result(Builder.TakeString())); |
| 4729 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 4730 | // @[objects, ...] |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4731 | Builder.AddResultTypeChunk("NSArray *"); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4732 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"[")); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4733 | Builder.AddPlaceholderChunk("objects, ..."); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4734 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 4735 | Results.AddResult(Result(Builder.TakeString())); |
| 4736 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 4737 | // @{key : object, ...} |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4738 | Builder.AddResultTypeChunk("NSDictionary *"); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4739 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"{")); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4740 | Builder.AddPlaceholderChunk("key"); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4741 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 4742 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4743 | Builder.AddPlaceholderChunk("object, ..."); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4744 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4745 | Results.AddResult(Result(Builder.TakeString())); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4746 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 4747 | // @(expression) |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4748 | Builder.AddResultTypeChunk("id"); |
| 4749 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "(")); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4750 | Builder.AddPlaceholderChunk("expression"); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4751 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4752 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4753 | } |
| 4754 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4755 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4756 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4757 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4758 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4759 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4760 | if (Results.includeCodePatterns()) { |
| 4761 | // @try { statements } @catch ( declaration ) { statements } @finally |
| 4762 | // { statements } |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4763 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"try")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4764 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4765 | Builder.AddPlaceholderChunk("statements"); |
| 4766 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4767 | Builder.AddTextChunk("@catch"); |
| 4768 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4769 | Builder.AddPlaceholderChunk("parameter"); |
| 4770 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4771 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4772 | Builder.AddPlaceholderChunk("statements"); |
| 4773 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4774 | Builder.AddTextChunk("@finally"); |
| 4775 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4776 | Builder.AddPlaceholderChunk("statements"); |
| 4777 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4778 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4779 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4780 | |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4781 | // @throw |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4782 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"throw")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4783 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4784 | Builder.AddPlaceholderChunk("expression"); |
| 4785 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4786 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4787 | if (Results.includeCodePatterns()) { |
| 4788 | // @synchronized ( expression ) { statements } |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4789 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synchronized")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4790 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4791 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4792 | Builder.AddPlaceholderChunk("expression"); |
| 4793 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4794 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4795 | Builder.AddPlaceholderChunk("statements"); |
| 4796 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4797 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4798 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4799 | } |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4800 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4801 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4802 | ResultBuilder &Results, |
| 4803 | bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4804 | typedef CodeCompletionResult Result; |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4805 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"private"))); |
| 4806 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"protected"))); |
| 4807 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"public"))); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4808 | if (LangOpts.ObjC2) |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4809 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"package"))); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | void Sema::CodeCompleteObjCAtVisibility(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4813 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4814 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4815 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4816 | Results.EnterNewScope(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4817 | AddObjCVisibilityResults(getLangOpts(), Results, false); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4818 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4819 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4820 | CodeCompletionContext::CCC_Other, |
| 4821 | Results.data(),Results.size()); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | void Sema::CodeCompleteObjCAtStatement(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4825 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4826 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4827 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4828 | Results.EnterNewScope(); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4829 | AddObjCStatementResults(Results, false); |
| 4830 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4831 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4832 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4833 | CodeCompletionContext::CCC_Other, |
| 4834 | Results.data(),Results.size()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4835 | } |
| 4836 | |
| 4837 | void Sema::CodeCompleteObjCAtExpression(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4838 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4839 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4840 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4841 | Results.EnterNewScope(); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4842 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4843 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4844 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4845 | CodeCompletionContext::CCC_Other, |
| 4846 | Results.data(),Results.size()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4847 | } |
| 4848 | |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4849 | /// \brief Determine whether the addition of the given flag to an Objective-C |
| 4850 | /// property's attributes will cause a conflict. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4851 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4852 | // Check if we've already added this flag. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4853 | if (Attributes & NewFlag) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4854 | return true; |
| 4855 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4856 | Attributes |= NewFlag; |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4857 | |
| 4858 | // Check for collisions with "readonly". |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4859 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 4860 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4861 | return true; |
| 4862 | |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 4863 | // Check for more than one of { assign, copy, retain, strong, weak }. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4864 | unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4865 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4866 | ObjCDeclSpec::DQ_PR_copy | |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 4867 | ObjCDeclSpec::DQ_PR_retain | |
| 4868 | ObjCDeclSpec::DQ_PR_strong | |
| 4869 | ObjCDeclSpec::DQ_PR_weak); |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4870 | if (AssignCopyRetMask && |
| 4871 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4872 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained && |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4873 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4874 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain && |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 4875 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong && |
| 4876 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_weak) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4877 | return true; |
| 4878 | |
| 4879 | return false; |
| 4880 | } |
| 4881 | |
Douglas Gregor | 36029f4 | 2009-11-18 23:08:07 +0000 | [diff] [blame] | 4882 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4883 | if (!CodeCompleter) |
| 4884 | return; |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4885 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4886 | unsigned Attributes = ODS.getPropertyAttributes(); |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4887 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4888 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4889 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4890 | CodeCompletionContext::CCC_Other); |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4891 | Results.EnterNewScope(); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4892 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4893 | Results.AddResult(CodeCompletionResult("readonly")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4894 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4895 | Results.AddResult(CodeCompletionResult("assign")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4896 | if (!ObjCPropertyFlagConflicts(Attributes, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4897 | ObjCDeclSpec::DQ_PR_unsafe_unretained)) |
| 4898 | Results.AddResult(CodeCompletionResult("unsafe_unretained")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4899 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4900 | Results.AddResult(CodeCompletionResult("readwrite")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4901 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4902 | Results.AddResult(CodeCompletionResult("retain")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4903 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4904 | Results.AddResult(CodeCompletionResult("strong")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4905 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4906 | Results.AddResult(CodeCompletionResult("copy")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4907 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4908 | Results.AddResult(CodeCompletionResult("nonatomic")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4909 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic)) |
Fariborz Jahanian | 1c2d29e | 2011-06-11 17:14:27 +0000 | [diff] [blame] | 4910 | Results.AddResult(CodeCompletionResult("atomic")); |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 4911 | |
| 4912 | // 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] | 4913 | if (getLangOpts().ObjCWeak || getLangOpts().getGC() != LangOptions::NonGC) |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4914 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_weak)) |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 4915 | Results.AddResult(CodeCompletionResult("weak")); |
| 4916 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4917 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4918 | CodeCompletionBuilder Setter(Results.getAllocator(), |
| 4919 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4920 | Setter.AddTypedTextChunk("setter"); |
Argyrios Kyrtzidis | 7bbb881 | 2014-02-20 07:55:15 +0000 | [diff] [blame] | 4921 | Setter.AddTextChunk("="); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4922 | Setter.AddPlaceholderChunk("method"); |
| 4923 | Results.AddResult(CodeCompletionResult(Setter.TakeString())); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 4924 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4925 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4926 | CodeCompletionBuilder Getter(Results.getAllocator(), |
| 4927 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4928 | Getter.AddTypedTextChunk("getter"); |
Argyrios Kyrtzidis | 7bbb881 | 2014-02-20 07:55:15 +0000 | [diff] [blame] | 4929 | Getter.AddTextChunk("="); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4930 | Getter.AddPlaceholderChunk("method"); |
| 4931 | Results.AddResult(CodeCompletionResult(Getter.TakeString())); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 4932 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 4933 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nullability)) { |
| 4934 | Results.AddResult(CodeCompletionResult("nonnull")); |
| 4935 | Results.AddResult(CodeCompletionResult("nullable")); |
| 4936 | Results.AddResult(CodeCompletionResult("null_unspecified")); |
| 4937 | Results.AddResult(CodeCompletionResult("null_resettable")); |
| 4938 | } |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4939 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4940 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4941 | CodeCompletionContext::CCC_Other, |
| 4942 | Results.data(),Results.size()); |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4943 | } |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4944 | |
James Dennett | f124387 | 2012-06-17 05:33:25 +0000 | [diff] [blame] | 4945 | /// \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] | 4946 | /// via code completion. |
| 4947 | enum ObjCMethodKind { |
Dmitri Gribenko | 4280e5c | 2012-06-08 23:13:42 +0000 | [diff] [blame] | 4948 | MK_Any, ///< Any kind of method, provided it means other specified criteria. |
| 4949 | MK_ZeroArgSelector, ///< Zero-argument (unary) selector. |
| 4950 | MK_OneArgSelector ///< One-argument selector. |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4951 | }; |
| 4952 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4953 | static bool isAcceptableObjCSelector(Selector Sel, |
| 4954 | ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 4955 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4956 | bool AllowSameLength = true) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 4957 | unsigned NumSelIdents = SelIdents.size(); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4958 | if (NumSelIdents > Sel.getNumArgs()) |
| 4959 | return false; |
| 4960 | |
| 4961 | switch (WantKind) { |
| 4962 | case MK_Any: break; |
| 4963 | case MK_ZeroArgSelector: return Sel.isUnarySelector(); |
| 4964 | case MK_OneArgSelector: return Sel.getNumArgs() == 1; |
| 4965 | } |
| 4966 | |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4967 | if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs()) |
| 4968 | return false; |
| 4969 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4970 | for (unsigned I = 0; I != NumSelIdents; ++I) |
| 4971 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) |
| 4972 | return false; |
| 4973 | |
| 4974 | return true; |
| 4975 | } |
| 4976 | |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4977 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, |
| 4978 | ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 4979 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4980 | bool AllowSameLength = true) { |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4981 | return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 4982 | AllowSameLength); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4983 | } |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4984 | |
| 4985 | namespace { |
| 4986 | /// \brief A set of selectors, which is used to avoid introducing multiple |
| 4987 | /// completions with the same selector into the result set. |
| 4988 | typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet; |
| 4989 | } |
| 4990 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4991 | /// \brief Add all of the Objective-C methods in the given Objective-C |
| 4992 | /// container to the set of results. |
| 4993 | /// |
| 4994 | /// The container will be a class, protocol, category, or implementation of |
| 4995 | /// any of the above. This mether will recurse to include methods from |
| 4996 | /// the superclasses of classes along with their categories, protocols, and |
| 4997 | /// implementations. |
| 4998 | /// |
| 4999 | /// \param Container the container in which we'll look to find methods. |
| 5000 | /// |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5001 | /// \param WantInstanceMethods Whether to add instance methods (only); if |
| 5002 | /// false, this routine will add factory methods (only). |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5003 | /// |
| 5004 | /// \param CurContext the context in which we're performing the lookup that |
| 5005 | /// finds methods. |
| 5006 | /// |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5007 | /// \param AllowSameLength Whether we allow a method to be added to the list |
| 5008 | /// when it has the same number of parameters as we have selector identifiers. |
| 5009 | /// |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5010 | /// \param Results the structure into which we'll add results. |
| 5011 | static void AddObjCMethods(ObjCContainerDecl *Container, |
| 5012 | bool WantInstanceMethods, |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5013 | ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5014 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5015 | DeclContext *CurContext, |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5016 | VisitedSelectorSet &Selectors, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5017 | bool AllowSameLength, |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5018 | ResultBuilder &Results, |
| 5019 | bool InOriginalClass = true) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5020 | typedef CodeCompletionResult Result; |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 5021 | Container = getContainerDef(Container); |
Douglas Gregor | 41778c3 | 2013-01-30 06:58:39 +0000 | [diff] [blame] | 5022 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); |
| 5023 | bool isRootClass = IFace && !IFace->getSuperClass(); |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 5024 | for (auto *M : Container->methods()) { |
Douglas Gregor | 41778c3 | 2013-01-30 06:58:39 +0000 | [diff] [blame] | 5025 | // The instance methods on the root class can be messaged via the |
| 5026 | // metaclass. |
| 5027 | if (M->isInstanceMethod() == WantInstanceMethods || |
| 5028 | (isRootClass && !WantInstanceMethods)) { |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5029 | // Check whether the selector identifiers we've been given are a |
| 5030 | // subset of the identifiers for this particular method. |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 5031 | if (!isAcceptableObjCMethod(M, WantKind, SelIdents, AllowSameLength)) |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5032 | continue; |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5033 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 5034 | if (!Selectors.insert(M->getSelector()).second) |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5035 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5036 | |
| 5037 | Result R = Result(M, Results.getBasePriority(M), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5038 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5039 | R.AllParametersAreInformative = (WantKind != MK_Any); |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5040 | if (!InOriginalClass) |
| 5041 | R.Priority += CCD_InBaseClass; |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5042 | Results.MaybeAddResult(R, CurContext); |
| 5043 | } |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5044 | } |
| 5045 | |
Douglas Gregor | f37c949 | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 5046 | // Visit the protocols of protocols. |
| 5047 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5048 | if (Protocol->hasDefinition()) { |
| 5049 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5050 | = Protocol->getReferencedProtocols(); |
| 5051 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 5052 | E = Protocols.end(); |
| 5053 | I != E; ++I) |
| 5054 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5055 | CurContext, Selectors, AllowSameLength, Results, false); |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5056 | } |
Douglas Gregor | f37c949 | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 5057 | } |
| 5058 | |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 5059 | if (!IFace || !IFace->hasDefinition()) |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5060 | return; |
| 5061 | |
| 5062 | // Add methods in protocols. |
Aaron Ballman | a49c506 | 2014-03-13 20:29:09 +0000 | [diff] [blame] | 5063 | for (auto *I : IFace->protocols()) |
| 5064 | AddObjCMethods(I, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5065 | CurContext, Selectors, AllowSameLength, Results, false); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5066 | |
| 5067 | // Add methods in categories. |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 5068 | for (auto *CatDecl : IFace->known_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5069 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5070 | CurContext, Selectors, AllowSameLength, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5071 | Results, InOriginalClass); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5072 | |
| 5073 | // Add a categories protocol methods. |
| 5074 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5075 | = CatDecl->getReferencedProtocols(); |
| 5076 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 5077 | E = Protocols.end(); |
| 5078 | I != E; ++I) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5079 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5080 | CurContext, Selectors, AllowSameLength, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5081 | Results, false); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5082 | |
| 5083 | // Add methods in category implementations. |
| 5084 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5085 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5086 | CurContext, Selectors, AllowSameLength, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5087 | Results, InOriginalClass); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5088 | } |
| 5089 | |
| 5090 | // Add methods in superclass. |
| 5091 | if (IFace->getSuperClass()) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5092 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5093 | SelIdents, CurContext, Selectors, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5094 | AllowSameLength, Results, false); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5095 | |
| 5096 | // Add methods in our implementation, if any. |
| 5097 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5098 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5099 | CurContext, Selectors, AllowSameLength, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5100 | Results, InOriginalClass); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5101 | } |
| 5102 | |
| 5103 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5104 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S) { |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5105 | // Try to find the interface where getters might live. |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5106 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5107 | if (!Class) { |
| 5108 | if (ObjCCategoryDecl *Category |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5109 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5110 | Class = Category->getClassInterface(); |
| 5111 | |
| 5112 | if (!Class) |
| 5113 | return; |
| 5114 | } |
| 5115 | |
| 5116 | // Find all of the potential getters. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5117 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5118 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5119 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5120 | Results.EnterNewScope(); |
| 5121 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5122 | VisitedSelectorSet Selectors; |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5123 | AddObjCMethods(Class, true, MK_ZeroArgSelector, None, CurContext, Selectors, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5124 | /*AllowSameLength=*/true, Results); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5125 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5126 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5127 | CodeCompletionContext::CCC_Other, |
| 5128 | Results.data(),Results.size()); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5129 | } |
| 5130 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5131 | void Sema::CodeCompleteObjCPropertySetter(Scope *S) { |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5132 | // Try to find the interface where setters might live. |
| 5133 | ObjCInterfaceDecl *Class |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5134 | = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5135 | if (!Class) { |
| 5136 | if (ObjCCategoryDecl *Category |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5137 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5138 | Class = Category->getClassInterface(); |
| 5139 | |
| 5140 | if (!Class) |
| 5141 | return; |
| 5142 | } |
| 5143 | |
| 5144 | // Find all of the potential getters. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5145 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5146 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5147 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5148 | Results.EnterNewScope(); |
| 5149 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5150 | VisitedSelectorSet Selectors; |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5151 | AddObjCMethods(Class, true, MK_OneArgSelector, None, CurContext, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5152 | Selectors, /*AllowSameLength=*/true, Results); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5153 | |
| 5154 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5155 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5156 | CodeCompletionContext::CCC_Other, |
| 5157 | Results.data(),Results.size()); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5158 | } |
| 5159 | |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5160 | void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS, |
| 5161 | bool IsParameter) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5162 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5163 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5164 | CodeCompletionContext::CCC_Type); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5165 | Results.EnterNewScope(); |
| 5166 | |
| 5167 | // Add context-sensitive, Objective-C parameter-passing keywords. |
| 5168 | bool AddedInOut = false; |
| 5169 | if ((DS.getObjCDeclQualifier() & |
| 5170 | (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 5171 | Results.AddResult("in"); |
| 5172 | Results.AddResult("inout"); |
| 5173 | AddedInOut = true; |
| 5174 | } |
| 5175 | if ((DS.getObjCDeclQualifier() & |
| 5176 | (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 5177 | Results.AddResult("out"); |
| 5178 | if (!AddedInOut) |
| 5179 | Results.AddResult("inout"); |
| 5180 | } |
| 5181 | if ((DS.getObjCDeclQualifier() & |
| 5182 | (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref | |
| 5183 | ObjCDeclSpec::DQ_Oneway)) == 0) { |
| 5184 | Results.AddResult("bycopy"); |
| 5185 | Results.AddResult("byref"); |
| 5186 | Results.AddResult("oneway"); |
| 5187 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 5188 | if ((DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability) == 0) { |
| 5189 | Results.AddResult("nonnull"); |
| 5190 | Results.AddResult("nullable"); |
| 5191 | Results.AddResult("null_unspecified"); |
| 5192 | } |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5193 | |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5194 | // If we're completing the return type of an Objective-C method and the |
| 5195 | // identifier IBAction refers to a macro, provide a completion item for |
| 5196 | // an action, e.g., |
| 5197 | // IBAction)<#selector#>:(id)sender |
| 5198 | if (DS.getObjCDeclQualifier() == 0 && !IsParameter && |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 5199 | PP.isMacroDefined("IBAction")) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5200 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5201 | Results.getCodeCompletionTUInfo(), |
| 5202 | CCP_CodePattern, CXAvailability_Available); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5203 | Builder.AddTypedTextChunk("IBAction"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5204 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5205 | Builder.AddPlaceholderChunk("selector"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5206 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 5207 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5208 | Builder.AddTextChunk("id"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5209 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5210 | Builder.AddTextChunk("sender"); |
| 5211 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 5212 | } |
Douglas Gregor | ed1f597 | 2013-01-30 07:11:43 +0000 | [diff] [blame] | 5213 | |
| 5214 | // If we're completing the return type, provide 'instancetype'. |
| 5215 | if (!IsParameter) { |
| 5216 | Results.AddResult(CodeCompletionResult("instancetype")); |
| 5217 | } |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5218 | |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5219 | // Add various builtin type names and specifiers. |
| 5220 | AddOrdinaryNameResults(PCC_Type, S, *this, Results); |
| 5221 | Results.ExitScope(); |
| 5222 | |
| 5223 | // Add the various type names |
| 5224 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 5225 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 5226 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 5227 | CodeCompleter->includeGlobals()); |
| 5228 | |
| 5229 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 5230 | AddMacroResults(PP, Results, false); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5231 | |
| 5232 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5233 | CodeCompletionContext::CCC_Type, |
| 5234 | Results.data(), Results.size()); |
| 5235 | } |
| 5236 | |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5237 | /// \brief When we have an expression with type "id", we may assume |
| 5238 | /// that it has some more-specific class type based on knowledge of |
| 5239 | /// common uses of Objective-C. This routine returns that class type, |
| 5240 | /// or NULL if no better result could be determined. |
| 5241 | static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) { |
Douglas Gregor | ed0b69d | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 5242 | ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5243 | if (!Msg) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5244 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5245 | |
| 5246 | Selector Sel = Msg->getSelector(); |
| 5247 | if (Sel.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5248 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5249 | |
| 5250 | IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0); |
| 5251 | if (!Id) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5252 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5253 | |
| 5254 | ObjCMethodDecl *Method = Msg->getMethodDecl(); |
| 5255 | if (!Method) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5256 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5257 | |
| 5258 | // Determine the class that we're sending the message to. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5259 | ObjCInterfaceDecl *IFace = nullptr; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 5260 | switch (Msg->getReceiverKind()) { |
| 5261 | case ObjCMessageExpr::Class: |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5262 | if (const ObjCObjectType *ObjType |
| 5263 | = Msg->getClassReceiver()->getAs<ObjCObjectType>()) |
| 5264 | IFace = ObjType->getInterface(); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 5265 | break; |
| 5266 | |
| 5267 | case ObjCMessageExpr::Instance: { |
| 5268 | QualType T = Msg->getInstanceReceiver()->getType(); |
| 5269 | if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) |
| 5270 | IFace = Ptr->getInterfaceDecl(); |
| 5271 | break; |
| 5272 | } |
| 5273 | |
| 5274 | case ObjCMessageExpr::SuperInstance: |
| 5275 | case ObjCMessageExpr::SuperClass: |
| 5276 | break; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5277 | } |
| 5278 | |
| 5279 | if (!IFace) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5280 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5281 | |
| 5282 | ObjCInterfaceDecl *Super = IFace->getSuperClass(); |
| 5283 | if (Method->isInstanceMethod()) |
| 5284 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 5285 | .Case("retain", IFace) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5286 | .Case("strong", IFace) |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5287 | .Case("autorelease", IFace) |
| 5288 | .Case("copy", IFace) |
| 5289 | .Case("copyWithZone", IFace) |
| 5290 | .Case("mutableCopy", IFace) |
| 5291 | .Case("mutableCopyWithZone", IFace) |
| 5292 | .Case("awakeFromCoder", IFace) |
| 5293 | .Case("replacementObjectFromCoder", IFace) |
| 5294 | .Case("class", IFace) |
| 5295 | .Case("classForCoder", IFace) |
| 5296 | .Case("superclass", Super) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5297 | .Default(nullptr); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5298 | |
| 5299 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 5300 | .Case("new", IFace) |
| 5301 | .Case("alloc", IFace) |
| 5302 | .Case("allocWithZone", IFace) |
| 5303 | .Case("class", IFace) |
| 5304 | .Case("superclass", Super) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5305 | .Default(nullptr); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5306 | } |
| 5307 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5308 | // Add a special completion for a message send to "super", which fills in the |
| 5309 | // most likely case of forwarding all of our arguments to the superclass |
| 5310 | // function. |
| 5311 | /// |
| 5312 | /// \param S The semantic analysis object. |
| 5313 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 5314 | /// \param NeedSuperKeyword Whether we need to prefix this completion with |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5315 | /// the "super" keyword. Otherwise, we just need to provide the arguments. |
| 5316 | /// |
| 5317 | /// \param SelIdents The identifiers in the selector that have already been |
| 5318 | /// provided as arguments for a send to "super". |
| 5319 | /// |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5320 | /// \param Results The set of results to augment. |
| 5321 | /// |
| 5322 | /// \returns the Objective-C method declaration that would be invoked by |
| 5323 | /// this "super" completion. If NULL, no completion was added. |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5324 | static ObjCMethodDecl *AddSuperSendCompletion( |
| 5325 | Sema &S, bool NeedSuperKeyword, |
| 5326 | ArrayRef<IdentifierInfo *> SelIdents, |
| 5327 | ResultBuilder &Results) { |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5328 | ObjCMethodDecl *CurMethod = S.getCurMethodDecl(); |
| 5329 | if (!CurMethod) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5330 | return nullptr; |
| 5331 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5332 | ObjCInterfaceDecl *Class = CurMethod->getClassInterface(); |
| 5333 | if (!Class) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5334 | return nullptr; |
| 5335 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5336 | // Try to find a superclass method with the same selector. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5337 | ObjCMethodDecl *SuperMethod = nullptr; |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5338 | while ((Class = Class->getSuperClass()) && !SuperMethod) { |
| 5339 | // Check in the class |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5340 | SuperMethod = Class->getMethod(CurMethod->getSelector(), |
| 5341 | CurMethod->isInstanceMethod()); |
| 5342 | |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5343 | // Check in categories or class extensions. |
| 5344 | if (!SuperMethod) { |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 5345 | for (const auto *Cat : Class->known_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5346 | if ((SuperMethod = Cat->getMethod(CurMethod->getSelector(), |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5347 | CurMethod->isInstanceMethod()))) |
| 5348 | break; |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5349 | } |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5350 | } |
| 5351 | } |
| 5352 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5353 | if (!SuperMethod) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5354 | return nullptr; |
| 5355 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5356 | // Check whether the superclass method has the same signature. |
| 5357 | if (CurMethod->param_size() != SuperMethod->param_size() || |
| 5358 | CurMethod->isVariadic() != SuperMethod->isVariadic()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5359 | return nullptr; |
| 5360 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5361 | for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(), |
| 5362 | CurPEnd = CurMethod->param_end(), |
| 5363 | SuperP = SuperMethod->param_begin(); |
| 5364 | CurP != CurPEnd; ++CurP, ++SuperP) { |
| 5365 | // Make sure the parameter types are compatible. |
| 5366 | if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(), |
| 5367 | (*SuperP)->getType())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5368 | return nullptr; |
| 5369 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5370 | // Make sure we have a parameter name to forward! |
| 5371 | if (!(*CurP)->getIdentifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5372 | return nullptr; |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5373 | } |
| 5374 | |
| 5375 | // We have a superclass method. Now, form the send-to-super completion. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5376 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5377 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5378 | |
| 5379 | // Give this completion a return type. |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 5380 | AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod, |
| 5381 | Results.getCompletionContext().getBaseType(), |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 5382 | Builder); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5383 | |
| 5384 | // If we need the "super" keyword, add it (plus some spacing). |
| 5385 | if (NeedSuperKeyword) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5386 | Builder.AddTypedTextChunk("super"); |
| 5387 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | Selector Sel = CurMethod->getSelector(); |
| 5391 | if (Sel.isUnarySelector()) { |
| 5392 | if (NeedSuperKeyword) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5393 | Builder.AddTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5394 | Sel.getNameForSlot(0))); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5395 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5396 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5397 | Sel.getNameForSlot(0))); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5398 | } else { |
| 5399 | ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(); |
| 5400 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5401 | if (I > SelIdents.size()) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5402 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5403 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5404 | if (I < SelIdents.size()) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5405 | Builder.AddInformativeChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5406 | Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5407 | Sel.getNameForSlot(I) + ":")); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5408 | else if (NeedSuperKeyword || I > SelIdents.size()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5409 | Builder.AddTextChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5410 | Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5411 | Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5412 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5413 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5414 | } else { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5415 | Builder.AddTypedTextChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5416 | Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5417 | Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5418 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5419 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5420 | } |
| 5421 | } |
| 5422 | } |
| 5423 | |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 5424 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), SuperMethod, |
| 5425 | CCP_SuperCompletion)); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5426 | return SuperMethod; |
| 5427 | } |
| 5428 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5429 | void Sema::CodeCompleteObjCMessageReceiver(Scope *S) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5430 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5431 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5432 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5433 | CodeCompletionContext::CCC_ObjCMessageReceiver, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5434 | getLangOpts().CPlusPlus11 |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5435 | ? &ResultBuilder::IsObjCMessageReceiverOrLambdaCapture |
| 5436 | : &ResultBuilder::IsObjCMessageReceiver); |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5437 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5438 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 5439 | Results.EnterNewScope(); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5440 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 5441 | CodeCompleter->includeGlobals()); |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5442 | |
| 5443 | // If we are in an Objective-C method inside a class that has a superclass, |
| 5444 | // add "super" as an option. |
| 5445 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 5446 | if (ObjCInterfaceDecl *Iface = Method->getClassInterface()) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5447 | if (Iface->getSuperClass()) { |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5448 | Results.AddResult(Result("super")); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5449 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5450 | AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, None, Results); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5451 | } |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5452 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5453 | if (getLangOpts().CPlusPlus11) |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5454 | addThisCompletion(*this, Results); |
| 5455 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5456 | Results.ExitScope(); |
| 5457 | |
| 5458 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 5459 | AddMacroResults(PP, Results, false); |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 5460 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5461 | Results.data(), Results.size()); |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5462 | |
| 5463 | } |
| 5464 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5465 | void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5466 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5467 | bool AtArgumentExpression) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5468 | ObjCInterfaceDecl *CDecl = nullptr; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5469 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 5470 | // Figure out which interface we're in. |
| 5471 | CDecl = CurMethod->getClassInterface(); |
| 5472 | if (!CDecl) |
| 5473 | return; |
| 5474 | |
| 5475 | // Find the superclass of this class. |
| 5476 | CDecl = CDecl->getSuperClass(); |
| 5477 | if (!CDecl) |
| 5478 | return; |
| 5479 | |
| 5480 | if (CurMethod->isInstanceMethod()) { |
| 5481 | // We are inside an instance method, which means that the message |
| 5482 | // send [super ...] is actually calling an instance method on the |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5483 | // current object. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5484 | return CodeCompleteObjCInstanceMessage(S, nullptr, SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5485 | AtArgumentExpression, |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5486 | CDecl); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5487 | } |
| 5488 | |
| 5489 | // Fall through to send to the superclass in CDecl. |
| 5490 | } else { |
| 5491 | // "super" may be the name of a type or variable. Figure out which |
| 5492 | // it is. |
Argyrios Kyrtzidis | 3e56dd4 | 2013-03-14 22:56:43 +0000 | [diff] [blame] | 5493 | IdentifierInfo *Super = getSuperIdentifier(); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5494 | NamedDecl *ND = LookupSingleName(S, Super, SuperLoc, |
| 5495 | LookupOrdinaryName); |
| 5496 | if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) { |
| 5497 | // "super" names an interface. Use it. |
| 5498 | } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5499 | if (const ObjCObjectType *Iface |
| 5500 | = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>()) |
| 5501 | CDecl = Iface->getInterface(); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5502 | } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) { |
| 5503 | // "super" names an unresolved type; we can't be more specific. |
| 5504 | } else { |
| 5505 | // Assume that "super" names some kind of value and parse that way. |
| 5506 | CXXScopeSpec SS; |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 5507 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5508 | UnqualifiedId id; |
| 5509 | id.setIdentifier(Super, SuperLoc); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 5510 | ExprResult SuperExpr = ActOnIdExpression(S, SS, TemplateKWLoc, id, |
| 5511 | false, false); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5512 | return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(), |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5513 | SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5514 | AtArgumentExpression); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5515 | } |
| 5516 | |
| 5517 | // Fall through |
| 5518 | } |
| 5519 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5520 | ParsedType Receiver; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5521 | if (CDecl) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5522 | Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl)); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5523 | return CodeCompleteObjCClassMessage(S, Receiver, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5524 | AtArgumentExpression, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5525 | /*IsSuper=*/true); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5526 | } |
| 5527 | |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5528 | /// \brief Given a set of code-completion results for the argument of a message |
| 5529 | /// send, determine the preferred type (if any) for that argument expression. |
| 5530 | static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results, |
| 5531 | unsigned NumSelIdents) { |
| 5532 | typedef CodeCompletionResult Result; |
| 5533 | ASTContext &Context = Results.getSema().Context; |
| 5534 | |
| 5535 | QualType PreferredType; |
| 5536 | unsigned BestPriority = CCP_Unlikely * 2; |
| 5537 | Result *ResultsData = Results.data(); |
| 5538 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 5539 | Result &R = ResultsData[I]; |
| 5540 | if (R.Kind == Result::RK_Declaration && |
| 5541 | isa<ObjCMethodDecl>(R.Declaration)) { |
| 5542 | if (R.Priority <= BestPriority) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 5543 | const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5544 | if (NumSelIdents <= Method->param_size()) { |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 5545 | QualType MyPreferredType = Method->parameters()[NumSelIdents - 1] |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5546 | ->getType(); |
| 5547 | if (R.Priority < BestPriority || PreferredType.isNull()) { |
| 5548 | BestPriority = R.Priority; |
| 5549 | PreferredType = MyPreferredType; |
| 5550 | } else if (!Context.hasSameUnqualifiedType(PreferredType, |
| 5551 | MyPreferredType)) { |
| 5552 | PreferredType = QualType(); |
| 5553 | } |
| 5554 | } |
| 5555 | } |
| 5556 | } |
| 5557 | } |
| 5558 | |
| 5559 | return PreferredType; |
| 5560 | } |
| 5561 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5562 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| 5563 | ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5564 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5565 | bool AtArgumentExpression, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5566 | bool IsSuper, |
| 5567 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5568 | typedef CodeCompletionResult Result; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5569 | ObjCInterfaceDecl *CDecl = nullptr; |
| 5570 | |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 5571 | // If the given name refers to an interface type, retrieve the |
| 5572 | // corresponding declaration. |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5573 | if (Receiver) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5574 | QualType T = SemaRef.GetTypeFromParser(Receiver, nullptr); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5575 | if (!T.isNull()) |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5576 | if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>()) |
| 5577 | CDecl = Interface->getInterface(); |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 5578 | } |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5579 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5580 | // Add all of the factory methods in this Objective-C class, its protocols, |
| 5581 | // superclasses, categories, implementation, etc. |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5582 | Results.EnterNewScope(); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5583 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5584 | // If this is a send-to-super, try to add the special "super" send |
| 5585 | // completion. |
| 5586 | if (IsSuper) { |
| 5587 | if (ObjCMethodDecl *SuperMethod |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5588 | = AddSuperSendCompletion(SemaRef, false, SelIdents, Results)) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5589 | Results.Ignore(SuperMethod); |
| 5590 | } |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5591 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 5592 | // If we're inside an Objective-C method definition, prefer its selector to |
| 5593 | // others. |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5594 | if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl()) |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 5595 | Results.setPreferredSelector(CurMethod->getSelector()); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5596 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5597 | VisitedSelectorSet Selectors; |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5598 | if (CDecl) |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5599 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5600 | SemaRef.CurContext, Selectors, AtArgumentExpression, |
| 5601 | Results); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5602 | else { |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5603 | // We're messaging "id" as a type; provide all class/factory methods. |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5604 | |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5605 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 5606 | // pool from the AST file. |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 5607 | if (SemaRef.getExternalSource()) { |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5608 | for (uint32_t I = 0, |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 5609 | N = SemaRef.getExternalSource()->GetNumExternalSelectors(); |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 5610 | I != N; ++I) { |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 5611 | Selector Sel = SemaRef.getExternalSource()->GetExternalSelector(I); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5612 | if (Sel.isNull() || SemaRef.MethodPool.count(Sel)) |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5613 | continue; |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5614 | |
| 5615 | SemaRef.ReadMethodPool(Sel); |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5616 | } |
| 5617 | } |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5618 | |
| 5619 | for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(), |
| 5620 | MEnd = SemaRef.MethodPool.end(); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5621 | M != MEnd; ++M) { |
| 5622 | for (ObjCMethodList *MethList = &M->second.second; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5623 | MethList && MethList->getMethod(); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 5624 | MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5625 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5626 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5627 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5628 | Result R(MethList->getMethod(), |
| 5629 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5630 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5631 | R.AllParametersAreInformative = false; |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5632 | Results.MaybeAddResult(R, SemaRef.CurContext); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5633 | } |
| 5634 | } |
| 5635 | } |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5636 | |
| 5637 | Results.ExitScope(); |
| 5638 | } |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5639 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5640 | void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5641 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5642 | bool AtArgumentExpression, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5643 | bool IsSuper) { |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5644 | |
| 5645 | QualType T = this->GetTypeFromParser(Receiver); |
| 5646 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5647 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5648 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5649 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5650 | T, SelIdents)); |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5651 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5652 | AddClassMessageCompletions(*this, S, Receiver, SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5653 | AtArgumentExpression, IsSuper, Results); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5654 | |
| 5655 | // If we're actually at the argument expression (rather than prior to the |
| 5656 | // selector), we're actually performing code completion for an expression. |
| 5657 | // Determine whether we have a single, best method. If so, we can |
| 5658 | // code-complete the expression using the corresponding parameter type as |
| 5659 | // our preferred type, improving completion results. |
| 5660 | if (AtArgumentExpression) { |
| 5661 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5662 | SelIdents.size()); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5663 | if (PreferredType.isNull()) |
| 5664 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 5665 | else |
| 5666 | CodeCompleteExpression(S, PreferredType); |
| 5667 | return; |
| 5668 | } |
| 5669 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5670 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5671 | Results.getCompletionContext(), |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5672 | Results.data(), Results.size()); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5673 | } |
| 5674 | |
Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 5675 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5676 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5677 | bool AtArgumentExpression, |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5678 | ObjCInterfaceDecl *Super) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5679 | typedef CodeCompletionResult Result; |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5680 | |
| 5681 | Expr *RecExpr = static_cast<Expr *>(Receiver); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5682 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5683 | // If necessary, apply function/array conversion to the receiver. |
| 5684 | // C99 6.7.5.3p[7,8]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5685 | if (RecExpr) { |
| 5686 | ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr); |
| 5687 | if (Conv.isInvalid()) // conversion failed. bail. |
| 5688 | return; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5689 | RecExpr = Conv.get(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5690 | } |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5691 | QualType ReceiverType = RecExpr? RecExpr->getType() |
| 5692 | : Super? Context.getObjCObjectPointerType( |
| 5693 | Context.getObjCInterfaceType(Super)) |
| 5694 | : Context.getObjCIdType(); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5695 | |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 5696 | // If we're messaging an expression with type "id" or "Class", check |
| 5697 | // whether we know something special about the receiver that allows |
| 5698 | // us to assume a more-specific receiver type. |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 5699 | if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) { |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 5700 | if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) { |
| 5701 | if (ReceiverType->isObjCClassType()) |
| 5702 | return CodeCompleteObjCClassMessage(S, |
| 5703 | ParsedType::make(Context.getObjCInterfaceType(IFace)), |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5704 | SelIdents, |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 5705 | AtArgumentExpression, Super); |
| 5706 | |
| 5707 | ReceiverType = Context.getObjCObjectPointerType( |
| 5708 | Context.getObjCInterfaceType(IFace)); |
| 5709 | } |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 5710 | } else if (RecExpr && getLangOpts().CPlusPlus) { |
| 5711 | ExprResult Conv = PerformContextuallyConvertToObjCPointer(RecExpr); |
| 5712 | if (Conv.isUsable()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5713 | RecExpr = Conv.get(); |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 5714 | ReceiverType = RecExpr->getType(); |
| 5715 | } |
| 5716 | } |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 5717 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5718 | // Build the set of methods we can see. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5719 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5720 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5721 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5722 | ReceiverType, SelIdents)); |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5723 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5724 | Results.EnterNewScope(); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5725 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5726 | // If this is a send-to-super, try to add the special "super" send |
| 5727 | // completion. |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5728 | if (Super) { |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5729 | if (ObjCMethodDecl *SuperMethod |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5730 | = AddSuperSendCompletion(*this, false, SelIdents, Results)) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5731 | Results.Ignore(SuperMethod); |
| 5732 | } |
| 5733 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 5734 | // If we're inside an Objective-C method definition, prefer its selector to |
| 5735 | // others. |
| 5736 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 5737 | Results.setPreferredSelector(CurMethod->getSelector()); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5738 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5739 | // Keep track of the selectors we've already added. |
| 5740 | VisitedSelectorSet Selectors; |
| 5741 | |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5742 | // Handle messages to Class. This really isn't a message to an instance |
| 5743 | // method, so we treat it the same way we would treat a message send to a |
| 5744 | // class method. |
| 5745 | if (ReceiverType->isObjCClassType() || |
| 5746 | ReceiverType->isObjCQualifiedClassType()) { |
| 5747 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 5748 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5749 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5750 | CurContext, Selectors, AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5751 | } |
| 5752 | } |
| 5753 | // Handle messages to a qualified ID ("id<foo>"). |
| 5754 | else if (const ObjCObjectPointerType *QualID |
| 5755 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 5756 | // Search protocols for instance methods. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 5757 | for (auto *I : QualID->quals()) |
| 5758 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5759 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5760 | } |
| 5761 | // Handle messages to a pointer to interface type. |
| 5762 | else if (const ObjCObjectPointerType *IFacePtr |
| 5763 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 5764 | // Search the class, its superclasses, etc., for instance methods. |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5765 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5766 | CurContext, Selectors, AtArgumentExpression, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5767 | Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5768 | |
| 5769 | // Search protocols for instance methods. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 5770 | for (auto *I : IFacePtr->quals()) |
| 5771 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5772 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5773 | } |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5774 | // Handle messages to "id". |
| 5775 | else if (ReceiverType->isObjCIdType()) { |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5776 | // We're messaging "id", so provide all instance methods we know |
| 5777 | // about as code-completion results. |
| 5778 | |
| 5779 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 5780 | // pool from the AST file. |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5781 | if (ExternalSource) { |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 5782 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 5783 | I != N; ++I) { |
| 5784 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5785 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5786 | continue; |
| 5787 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5788 | ReadMethodPool(Sel); |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5789 | } |
| 5790 | } |
| 5791 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5792 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 5793 | MEnd = MethodPool.end(); |
| 5794 | M != MEnd; ++M) { |
| 5795 | for (ObjCMethodList *MethList = &M->second.first; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5796 | MethList && MethList->getMethod(); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 5797 | MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5798 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5799 | continue; |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5800 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5801 | if (!Selectors.insert(MethList->getMethod()->getSelector()).second) |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5802 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5803 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5804 | Result R(MethList->getMethod(), |
| 5805 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5806 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5807 | R.AllParametersAreInformative = false; |
| 5808 | Results.MaybeAddResult(R, CurContext); |
| 5809 | } |
| 5810 | } |
| 5811 | } |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5812 | Results.ExitScope(); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5813 | |
| 5814 | |
| 5815 | // If we're actually at the argument expression (rather than prior to the |
| 5816 | // selector), we're actually performing code completion for an expression. |
| 5817 | // Determine whether we have a single, best method. If so, we can |
| 5818 | // code-complete the expression using the corresponding parameter type as |
| 5819 | // our preferred type, improving completion results. |
| 5820 | if (AtArgumentExpression) { |
| 5821 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5822 | SelIdents.size()); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5823 | if (PreferredType.isNull()) |
| 5824 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 5825 | else |
| 5826 | CodeCompleteExpression(S, PreferredType); |
| 5827 | return; |
| 5828 | } |
| 5829 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5830 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5831 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5832 | Results.data(),Results.size()); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5833 | } |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5834 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 5835 | void Sema::CodeCompleteObjCForCollection(Scope *S, |
| 5836 | DeclGroupPtrTy IterationVar) { |
| 5837 | CodeCompleteExpressionData Data; |
| 5838 | Data.ObjCCollection = true; |
| 5839 | |
| 5840 | if (IterationVar.getAsOpaquePtr()) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 5841 | DeclGroupRef DG = IterationVar.get(); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 5842 | for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) { |
| 5843 | if (*I) |
| 5844 | Data.IgnoreDecls.push_back(*I); |
| 5845 | } |
| 5846 | } |
| 5847 | |
| 5848 | CodeCompleteExpression(S, Data); |
| 5849 | } |
| 5850 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5851 | void Sema::CodeCompleteObjCSelector(Scope *S, |
| 5852 | ArrayRef<IdentifierInfo *> SelIdents) { |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5853 | // If we have an external source, load the entire class method |
| 5854 | // pool from the AST file. |
| 5855 | if (ExternalSource) { |
| 5856 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 5857 | I != N; ++I) { |
| 5858 | Selector Sel = ExternalSource->GetExternalSelector(I); |
| 5859 | if (Sel.isNull() || MethodPool.count(Sel)) |
| 5860 | continue; |
| 5861 | |
| 5862 | ReadMethodPool(Sel); |
| 5863 | } |
| 5864 | } |
| 5865 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5866 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5867 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5868 | CodeCompletionContext::CCC_SelectorName); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5869 | Results.EnterNewScope(); |
| 5870 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 5871 | MEnd = MethodPool.end(); |
| 5872 | M != MEnd; ++M) { |
| 5873 | |
| 5874 | Selector Sel = M->first; |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5875 | if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents)) |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5876 | continue; |
| 5877 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5878 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5879 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5880 | if (Sel.isUnarySelector()) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5881 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5882 | Sel.getNameForSlot(0))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5883 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5884 | continue; |
| 5885 | } |
| 5886 | |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5887 | std::string Accumulator; |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5888 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5889 | if (I == SelIdents.size()) { |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5890 | if (!Accumulator.empty()) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5891 | Builder.AddInformativeChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5892 | Accumulator)); |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5893 | Accumulator.clear(); |
| 5894 | } |
| 5895 | } |
| 5896 | |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 5897 | Accumulator += Sel.getNameForSlot(I); |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5898 | Accumulator += ':'; |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5899 | } |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5900 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator)); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5901 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5902 | } |
| 5903 | Results.ExitScope(); |
| 5904 | |
| 5905 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5906 | CodeCompletionContext::CCC_SelectorName, |
| 5907 | Results.data(), Results.size()); |
| 5908 | } |
| 5909 | |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5910 | /// \brief Add all of the protocol declarations that we find in the given |
| 5911 | /// (translation unit) context. |
| 5912 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5913 | bool OnlyForwardDeclarations, |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5914 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5915 | typedef CodeCompletionResult Result; |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5916 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 5917 | for (const auto *D : Ctx->decls()) { |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5918 | // Record any protocols we find. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 5919 | if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(D)) |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5920 | if (!OnlyForwardDeclarations || !Proto->hasDefinition()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5921 | Results.AddResult(Result(Proto, Results.getBasePriority(Proto),nullptr), |
| 5922 | CurContext, nullptr, false); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5923 | } |
| 5924 | } |
| 5925 | |
Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 5926 | void Sema::CodeCompleteObjCProtocolReferences( |
| 5927 | ArrayRef<IdentifierLocPair> Protocols) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5928 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5929 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5930 | CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5931 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5932 | if (CodeCompleter && CodeCompleter->includeGlobals()) { |
| 5933 | Results.EnterNewScope(); |
| 5934 | |
| 5935 | // Tell the result set to ignore all of the protocols we have |
| 5936 | // already seen. |
| 5937 | // FIXME: This doesn't work when caching code-completion results. |
Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 5938 | for (const IdentifierLocPair &Pair : Protocols) |
| 5939 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Pair.first, |
| 5940 | Pair.second)) |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5941 | Results.Ignore(Protocol); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5942 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5943 | // Add all protocols. |
| 5944 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 5945 | Results); |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5946 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5947 | Results.ExitScope(); |
| 5948 | } |
| 5949 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5950 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5951 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 5952 | Results.data(),Results.size()); |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5953 | } |
| 5954 | |
| 5955 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5956 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5957 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5958 | CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5959 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5960 | if (CodeCompleter && CodeCompleter->includeGlobals()) { |
| 5961 | Results.EnterNewScope(); |
| 5962 | |
| 5963 | // Add all protocols. |
| 5964 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 5965 | Results); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5966 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5967 | Results.ExitScope(); |
| 5968 | } |
| 5969 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5970 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5971 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 5972 | Results.data(),Results.size()); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5973 | } |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5974 | |
| 5975 | /// \brief Add all of the Objective-C interface declarations that we find in |
| 5976 | /// the given (translation unit) context. |
| 5977 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, |
| 5978 | bool OnlyForwardDeclarations, |
| 5979 | bool OnlyUnimplemented, |
| 5980 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5981 | typedef CodeCompletionResult Result; |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5982 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 5983 | for (const auto *D : Ctx->decls()) { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 5984 | // Record any interfaces we find. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 5985 | if (const auto *Class = dyn_cast<ObjCInterfaceDecl>(D)) |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 5986 | if ((!OnlyForwardDeclarations || !Class->hasDefinition()) && |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 5987 | (!OnlyUnimplemented || !Class->getImplementation())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5988 | Results.AddResult(Result(Class, Results.getBasePriority(Class),nullptr), |
| 5989 | CurContext, nullptr, false); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5990 | } |
| 5991 | } |
| 5992 | |
| 5993 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5994 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5995 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5996 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5997 | Results.EnterNewScope(); |
| 5998 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5999 | if (CodeCompleter->includeGlobals()) { |
| 6000 | // Add all classes. |
| 6001 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6002 | false, Results); |
| 6003 | } |
| 6004 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6005 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6006 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6007 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6008 | CodeCompletionContext::CCC_ObjCInterfaceName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6009 | Results.data(),Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6010 | } |
| 6011 | |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6012 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName, |
| 6013 | SourceLocation ClassNameLoc) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6014 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6015 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6016 | CodeCompletionContext::CCC_ObjCInterfaceName); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6017 | Results.EnterNewScope(); |
| 6018 | |
| 6019 | // Make sure that we ignore the class we're currently defining. |
| 6020 | NamedDecl *CurClass |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6021 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6022 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6023 | Results.Ignore(CurClass); |
| 6024 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6025 | if (CodeCompleter->includeGlobals()) { |
| 6026 | // Add all classes. |
| 6027 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6028 | false, Results); |
| 6029 | } |
| 6030 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6031 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6032 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6033 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6034 | CodeCompletionContext::CCC_ObjCInterfaceName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6035 | Results.data(),Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6036 | } |
| 6037 | |
| 6038 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6039 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6040 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6041 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6042 | Results.EnterNewScope(); |
| 6043 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6044 | if (CodeCompleter->includeGlobals()) { |
| 6045 | // Add all unimplemented classes. |
| 6046 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6047 | true, Results); |
| 6048 | } |
| 6049 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6050 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6051 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6052 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6053 | CodeCompletionContext::CCC_ObjCInterfaceName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6054 | Results.data(),Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6055 | } |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6056 | |
| 6057 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6058 | IdentifierInfo *ClassName, |
| 6059 | SourceLocation ClassNameLoc) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6060 | typedef CodeCompletionResult Result; |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6061 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6062 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6063 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6064 | CodeCompletionContext::CCC_ObjCCategoryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6065 | |
| 6066 | // Ignore any categories we find that have already been implemented by this |
| 6067 | // interface. |
| 6068 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 6069 | NamedDecl *CurClass |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6070 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6071 | if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)){ |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 6072 | for (const auto *Cat : Class->visible_categories()) |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6073 | CategoryNames.insert(Cat->getIdentifier()); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6074 | } |
| 6075 | |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6076 | // Add all of the categories we know about. |
| 6077 | Results.EnterNewScope(); |
| 6078 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6079 | for (const auto *D : TU->decls()) |
| 6080 | if (const auto *Category = dyn_cast<ObjCCategoryDecl>(D)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6081 | if (CategoryNames.insert(Category->getIdentifier()).second) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6082 | Results.AddResult(Result(Category, Results.getBasePriority(Category), |
| 6083 | nullptr), |
| 6084 | CurContext, nullptr, false); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6085 | Results.ExitScope(); |
| 6086 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6087 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6088 | CodeCompletionContext::CCC_ObjCCategoryName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6089 | Results.data(),Results.size()); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6090 | } |
| 6091 | |
| 6092 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6093 | IdentifierInfo *ClassName, |
| 6094 | SourceLocation ClassNameLoc) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6095 | typedef CodeCompletionResult Result; |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6096 | |
| 6097 | // Find the corresponding interface. If we couldn't find the interface, the |
| 6098 | // program itself is ill-formed. However, we'll try to be helpful still by |
| 6099 | // providing the list of all of the categories we know about. |
| 6100 | NamedDecl *CurClass |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6101 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6102 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); |
| 6103 | if (!Class) |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6104 | return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6105 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6106 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6107 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6108 | CodeCompletionContext::CCC_ObjCCategoryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6109 | |
| 6110 | // Add all of the categories that have have corresponding interface |
| 6111 | // declarations in this class and any of its superclasses, except for |
| 6112 | // already-implemented categories in the class itself. |
| 6113 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 6114 | Results.EnterNewScope(); |
| 6115 | bool IgnoreImplemented = true; |
| 6116 | while (Class) { |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 6117 | for (const auto *Cat : Class->visible_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6118 | if ((!IgnoreImplemented || !Cat->getImplementation()) && |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6119 | CategoryNames.insert(Cat->getIdentifier()).second) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6120 | Results.AddResult(Result(Cat, Results.getBasePriority(Cat), nullptr), |
| 6121 | CurContext, nullptr, false); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6122 | } |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6123 | |
| 6124 | Class = Class->getSuperClass(); |
| 6125 | IgnoreImplemented = false; |
| 6126 | } |
| 6127 | Results.ExitScope(); |
| 6128 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6129 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6130 | CodeCompletionContext::CCC_ObjCCategoryName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6131 | Results.data(),Results.size()); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6132 | } |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6133 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6134 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6135 | CodeCompletionContext CCContext(CodeCompletionContext::CCC_Other); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6136 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6137 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6138 | CCContext); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6139 | |
| 6140 | // Figure out where this @synthesize lives. |
| 6141 | ObjCContainerDecl *Container |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6142 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6143 | if (!Container || |
| 6144 | (!isa<ObjCImplementationDecl>(Container) && |
| 6145 | !isa<ObjCCategoryImplDecl>(Container))) |
| 6146 | return; |
| 6147 | |
| 6148 | // Ignore any properties that have already been implemented. |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6149 | Container = getContainerDef(Container); |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6150 | for (const auto *D : Container->decls()) |
| 6151 | if (const auto *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6152 | Results.Ignore(PropertyImpl->getPropertyDecl()); |
| 6153 | |
| 6154 | // Add any properties that we find. |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 6155 | AddedPropertiesSet AddedProperties; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6156 | Results.EnterNewScope(); |
| 6157 | if (ObjCImplementationDecl *ClassImpl |
| 6158 | = dyn_cast<ObjCImplementationDecl>(Container)) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6159 | AddObjCProperties(CCContext, ClassImpl->getClassInterface(), false, |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 6160 | /*AllowNullaryMethods=*/false, CurContext, |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 6161 | AddedProperties, Results); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6162 | else |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6163 | AddObjCProperties(CCContext, |
| 6164 | cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 6165 | false, /*AllowNullaryMethods=*/false, CurContext, |
| 6166 | AddedProperties, Results); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6167 | Results.ExitScope(); |
| 6168 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6169 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6170 | CodeCompletionContext::CCC_Other, |
| 6171 | Results.data(),Results.size()); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6172 | } |
| 6173 | |
| 6174 | void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S, |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6175 | IdentifierInfo *PropertyName) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6176 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6177 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6178 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6179 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6180 | |
| 6181 | // Figure out where this @synthesize lives. |
| 6182 | ObjCContainerDecl *Container |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6183 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6184 | if (!Container || |
| 6185 | (!isa<ObjCImplementationDecl>(Container) && |
| 6186 | !isa<ObjCCategoryImplDecl>(Container))) |
| 6187 | return; |
| 6188 | |
| 6189 | // Figure out which interface we're looking into. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6190 | ObjCInterfaceDecl *Class = nullptr; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6191 | if (ObjCImplementationDecl *ClassImpl |
| 6192 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| 6193 | Class = ClassImpl->getClassInterface(); |
| 6194 | else |
| 6195 | Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl() |
| 6196 | ->getClassInterface(); |
| 6197 | |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6198 | // Determine the type of the property we're synthesizing. |
| 6199 | QualType PropertyType = Context.getObjCIdType(); |
| 6200 | if (Class) { |
| 6201 | if (ObjCPropertyDecl *Property |
| 6202 | = Class->FindPropertyDeclaration(PropertyName)) { |
| 6203 | PropertyType |
| 6204 | = Property->getType().getNonReferenceType().getUnqualifiedType(); |
| 6205 | |
| 6206 | // Give preference to ivars |
| 6207 | Results.setPreferredType(PropertyType); |
| 6208 | } |
| 6209 | } |
| 6210 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6211 | // Add all of the instance variables in this class and its superclasses. |
| 6212 | Results.EnterNewScope(); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6213 | bool SawSimilarlyNamedIvar = false; |
| 6214 | std::string NameWithPrefix; |
| 6215 | NameWithPrefix += '_'; |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 6216 | NameWithPrefix += PropertyName->getName(); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6217 | std::string NameWithSuffix = PropertyName->getName().str(); |
| 6218 | NameWithSuffix += '_'; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6219 | for(; Class; Class = Class->getSuperClass()) { |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6220 | for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar; |
| 6221 | Ivar = Ivar->getNextIvar()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6222 | Results.AddResult(Result(Ivar, Results.getBasePriority(Ivar), nullptr), |
| 6223 | CurContext, nullptr, false); |
| 6224 | |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6225 | // Determine whether we've seen an ivar with a name similar to the |
| 6226 | // property. |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6227 | if ((PropertyName == Ivar->getIdentifier() || |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6228 | NameWithPrefix == Ivar->getName() || |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6229 | NameWithSuffix == Ivar->getName())) { |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6230 | SawSimilarlyNamedIvar = true; |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6231 | |
| 6232 | // Reduce the priority of this result by one, to give it a slight |
| 6233 | // advantage over other results whose names don't match so closely. |
| 6234 | if (Results.size() && |
| 6235 | Results.data()[Results.size() - 1].Kind |
| 6236 | == CodeCompletionResult::RK_Declaration && |
| 6237 | Results.data()[Results.size() - 1].Declaration == Ivar) |
| 6238 | Results.data()[Results.size() - 1].Priority--; |
| 6239 | } |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6240 | } |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6241 | } |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6242 | |
| 6243 | if (!SawSimilarlyNamedIvar) { |
| 6244 | // Create ivar result _propName, that the user can use to synthesize |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6245 | // an ivar of the appropriate type. |
| 6246 | unsigned Priority = CCP_MemberDeclaration + 1; |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6247 | typedef CodeCompletionResult Result; |
| 6248 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6249 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo(), |
| 6250 | Priority,CXAvailability_Available); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6251 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6252 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6253 | Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6254 | Policy, Allocator)); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6255 | Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix)); |
| 6256 | Results.AddResult(Result(Builder.TakeString(), Priority, |
| 6257 | CXCursor_ObjCIvarDecl)); |
| 6258 | } |
| 6259 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6260 | Results.ExitScope(); |
| 6261 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6262 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6263 | CodeCompletionContext::CCC_Other, |
| 6264 | Results.data(),Results.size()); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6265 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6266 | |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6267 | // Mapping from selectors to the methods that implement that selector, along |
| 6268 | // with the "in original class" flag. |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 6269 | typedef llvm::DenseMap< |
| 6270 | Selector, llvm::PointerIntPair<ObjCMethodDecl *, 1, bool> > KnownMethodsMap; |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6271 | |
| 6272 | /// \brief Find all of the methods that reside in the given container |
| 6273 | /// (and its superclasses, protocols, etc.) that meet the given |
| 6274 | /// criteria. Insert those methods into the map of known methods, |
| 6275 | /// indexed by selector so they can be easily found. |
| 6276 | static void FindImplementableMethods(ASTContext &Context, |
| 6277 | ObjCContainerDecl *Container, |
| 6278 | bool WantInstanceMethods, |
| 6279 | QualType ReturnType, |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6280 | KnownMethodsMap &KnownMethods, |
| 6281 | bool InOriginalClass = true) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6282 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) { |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6283 | // Make sure we have a definition; that's what we'll walk. |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 6284 | if (!IFace->hasDefinition()) |
| 6285 | return; |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6286 | |
| 6287 | IFace = IFace->getDefinition(); |
| 6288 | Container = IFace; |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 6289 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6290 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6291 | = IFace->getReferencedProtocols(); |
| 6292 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6293 | E = Protocols.end(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6294 | I != E; ++I) |
| 6295 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6296 | KnownMethods, InOriginalClass); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6297 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6298 | // Add methods from any class extensions and categories. |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 6299 | for (auto *Cat : IFace->visible_categories()) { |
| 6300 | FindImplementableMethods(Context, Cat, WantInstanceMethods, ReturnType, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6301 | KnownMethods, false); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6302 | } |
| 6303 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6304 | // Visit the superclass. |
| 6305 | if (IFace->getSuperClass()) |
| 6306 | FindImplementableMethods(Context, IFace->getSuperClass(), |
| 6307 | WantInstanceMethods, ReturnType, |
| 6308 | KnownMethods, false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6309 | } |
| 6310 | |
| 6311 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 6312 | // Recurse into protocols. |
| 6313 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6314 | = Category->getReferencedProtocols(); |
| 6315 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6316 | E = Protocols.end(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6317 | I != E; ++I) |
| 6318 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6319 | KnownMethods, InOriginalClass); |
| 6320 | |
| 6321 | // If this category is the original class, jump to the interface. |
| 6322 | if (InOriginalClass && Category->getClassInterface()) |
| 6323 | FindImplementableMethods(Context, Category->getClassInterface(), |
| 6324 | WantInstanceMethods, ReturnType, KnownMethods, |
| 6325 | false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6326 | } |
| 6327 | |
| 6328 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6329 | // Make sure we have a definition; that's what we'll walk. |
| 6330 | if (!Protocol->hasDefinition()) |
| 6331 | return; |
| 6332 | Protocol = Protocol->getDefinition(); |
| 6333 | Container = Protocol; |
| 6334 | |
| 6335 | // Recurse into protocols. |
| 6336 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6337 | = Protocol->getReferencedProtocols(); |
| 6338 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 6339 | E = Protocols.end(); |
| 6340 | I != E; ++I) |
| 6341 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
| 6342 | KnownMethods, false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6343 | } |
| 6344 | |
| 6345 | // Add methods in this container. This operation occurs last because |
| 6346 | // we want the methods from this container to override any methods |
| 6347 | // we've previously seen with the same selector. |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 6348 | for (auto *M : Container->methods()) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6349 | if (M->isInstanceMethod() == WantInstanceMethods) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6350 | if (!ReturnType.isNull() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6351 | !Context.hasSameUnqualifiedType(ReturnType, M->getReturnType())) |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6352 | continue; |
| 6353 | |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 6354 | KnownMethods[M->getSelector()] = |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 6355 | KnownMethodsMap::mapped_type(M, InOriginalClass); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6356 | } |
| 6357 | } |
| 6358 | } |
| 6359 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6360 | /// \brief Add the parenthesized return or parameter type chunk to a code |
| 6361 | /// completion string. |
| 6362 | static void AddObjCPassingTypeChunk(QualType Type, |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6363 | unsigned ObjCDeclQuals, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6364 | ASTContext &Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6365 | const PrintingPolicy &Policy, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6366 | CodeCompletionBuilder &Builder) { |
| 6367 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 6368 | std::string Quals = formatObjCParamQualifiers(ObjCDeclQuals, Type); |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6369 | if (!Quals.empty()) |
| 6370 | Builder.AddTextChunk(Builder.getAllocator().CopyString(Quals)); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6371 | Builder.AddTextChunk(GetCompletionTypeString(Type, Context, Policy, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6372 | Builder.getAllocator())); |
| 6373 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6374 | } |
| 6375 | |
| 6376 | /// \brief Determine whether the given class is or inherits from a class by |
| 6377 | /// the given name. |
| 6378 | static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6379 | StringRef Name) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6380 | if (!Class) |
| 6381 | return false; |
| 6382 | |
| 6383 | if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name) |
| 6384 | return true; |
| 6385 | |
| 6386 | return InheritsFromClassNamed(Class->getSuperClass(), Name); |
| 6387 | } |
| 6388 | |
| 6389 | /// \brief Add code completions for Objective-C Key-Value Coding (KVC) and |
| 6390 | /// Key-Value Observing (KVO). |
| 6391 | static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property, |
| 6392 | bool IsInstanceMethod, |
| 6393 | QualType ReturnType, |
| 6394 | ASTContext &Context, |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6395 | VisitedSelectorSet &KnownSelectors, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6396 | ResultBuilder &Results) { |
| 6397 | IdentifierInfo *PropName = Property->getIdentifier(); |
| 6398 | if (!PropName || PropName->getLength() == 0) |
| 6399 | return; |
| 6400 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6401 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
| 6402 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6403 | // Builder that will create each code completion. |
| 6404 | typedef CodeCompletionResult Result; |
| 6405 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6406 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6407 | |
| 6408 | // The selector table. |
| 6409 | SelectorTable &Selectors = Context.Selectors; |
| 6410 | |
| 6411 | // The property name, copied into the code completion allocation region |
| 6412 | // on demand. |
| 6413 | struct KeyHolder { |
| 6414 | CodeCompletionAllocator &Allocator; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6415 | StringRef Key; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6416 | const char *CopiedKey; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6417 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6418 | KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6419 | : Allocator(Allocator), Key(Key), CopiedKey(nullptr) {} |
| 6420 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6421 | operator const char *() { |
| 6422 | if (CopiedKey) |
| 6423 | return CopiedKey; |
| 6424 | |
| 6425 | return CopiedKey = Allocator.CopyString(Key); |
| 6426 | } |
| 6427 | } Key(Allocator, PropName->getName()); |
| 6428 | |
| 6429 | // The uppercased name of the property name. |
| 6430 | std::string UpperKey = PropName->getName(); |
| 6431 | if (!UpperKey.empty()) |
Jordan Rose | 4938f27 | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 6432 | UpperKey[0] = toUppercase(UpperKey[0]); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6433 | |
| 6434 | bool ReturnTypeMatchesProperty = ReturnType.isNull() || |
| 6435 | Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(), |
| 6436 | Property->getType()); |
| 6437 | bool ReturnTypeMatchesVoid |
| 6438 | = ReturnType.isNull() || ReturnType->isVoidType(); |
| 6439 | |
| 6440 | // Add the normal accessor -(type)key. |
| 6441 | if (IsInstanceMethod && |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6442 | KnownSelectors.insert(Selectors.getNullarySelector(PropName)).second && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6443 | ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) { |
| 6444 | if (ReturnType.isNull()) |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6445 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, |
| 6446 | Context, Policy, Builder); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6447 | |
| 6448 | Builder.AddTypedTextChunk(Key); |
| 6449 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 6450 | CXCursor_ObjCInstanceMethodDecl)); |
| 6451 | } |
| 6452 | |
| 6453 | // If we have an integral or boolean property (or the user has provided |
| 6454 | // an integral or boolean return type), add the accessor -(type)isKey. |
| 6455 | if (IsInstanceMethod && |
| 6456 | ((!ReturnType.isNull() && |
| 6457 | (ReturnType->isIntegerType() || ReturnType->isBooleanType())) || |
| 6458 | (ReturnType.isNull() && |
| 6459 | (Property->getType()->isIntegerType() || |
| 6460 | Property->getType()->isBooleanType())))) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6461 | std::string SelectorName = (Twine("is") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6462 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6463 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 6464 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6465 | if (ReturnType.isNull()) { |
| 6466 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6467 | Builder.AddTextChunk("BOOL"); |
| 6468 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6469 | } |
| 6470 | |
| 6471 | Builder.AddTypedTextChunk( |
| 6472 | Allocator.CopyString(SelectorId->getName())); |
| 6473 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 6474 | CXCursor_ObjCInstanceMethodDecl)); |
| 6475 | } |
| 6476 | } |
| 6477 | |
| 6478 | // Add the normal mutator. |
| 6479 | if (IsInstanceMethod && ReturnTypeMatchesVoid && |
| 6480 | !Property->getSetterMethodDecl()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6481 | std::string SelectorName = (Twine("set") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6482 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6483 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6484 | if (ReturnType.isNull()) { |
| 6485 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6486 | Builder.AddTextChunk("void"); |
| 6487 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6488 | } |
| 6489 | |
| 6490 | Builder.AddTypedTextChunk( |
| 6491 | Allocator.CopyString(SelectorId->getName())); |
| 6492 | Builder.AddTypedTextChunk(":"); |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6493 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, |
| 6494 | Context, Policy, Builder); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6495 | Builder.AddTextChunk(Key); |
| 6496 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 6497 | CXCursor_ObjCInstanceMethodDecl)); |
| 6498 | } |
| 6499 | } |
| 6500 | |
| 6501 | // Indexed and unordered accessors |
| 6502 | unsigned IndexedGetterPriority = CCP_CodePattern; |
| 6503 | unsigned IndexedSetterPriority = CCP_CodePattern; |
| 6504 | unsigned UnorderedGetterPriority = CCP_CodePattern; |
| 6505 | unsigned UnorderedSetterPriority = CCP_CodePattern; |
| 6506 | if (const ObjCObjectPointerType *ObjCPointer |
| 6507 | = Property->getType()->getAs<ObjCObjectPointerType>()) { |
| 6508 | if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) { |
| 6509 | // If this interface type is not provably derived from a known |
| 6510 | // collection, penalize the corresponding completions. |
| 6511 | if (!InheritsFromClassNamed(IFace, "NSMutableArray")) { |
| 6512 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6513 | if (!InheritsFromClassNamed(IFace, "NSArray")) |
| 6514 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6515 | } |
| 6516 | |
| 6517 | if (!InheritsFromClassNamed(IFace, "NSMutableSet")) { |
| 6518 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6519 | if (!InheritsFromClassNamed(IFace, "NSSet")) |
| 6520 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6521 | } |
| 6522 | } |
| 6523 | } else { |
| 6524 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6525 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6526 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6527 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6528 | } |
| 6529 | |
| 6530 | // Add -(NSUInteger)countOf<key> |
| 6531 | if (IsInstanceMethod && |
| 6532 | (ReturnType.isNull() || ReturnType->isIntegerType())) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6533 | std::string SelectorName = (Twine("countOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6534 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6535 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 6536 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6537 | if (ReturnType.isNull()) { |
| 6538 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6539 | Builder.AddTextChunk("NSUInteger"); |
| 6540 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6541 | } |
| 6542 | |
| 6543 | Builder.AddTypedTextChunk( |
| 6544 | Allocator.CopyString(SelectorId->getName())); |
| 6545 | Results.AddResult(Result(Builder.TakeString(), |
| 6546 | std::min(IndexedGetterPriority, |
| 6547 | UnorderedGetterPriority), |
| 6548 | CXCursor_ObjCInstanceMethodDecl)); |
| 6549 | } |
| 6550 | } |
| 6551 | |
| 6552 | // Indexed getters |
| 6553 | // Add -(id)objectInKeyAtIndex:(NSUInteger)index |
| 6554 | if (IsInstanceMethod && |
| 6555 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6556 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6557 | = (Twine("objectIn") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6558 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6559 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6560 | if (ReturnType.isNull()) { |
| 6561 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6562 | Builder.AddTextChunk("id"); |
| 6563 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6564 | } |
| 6565 | |
| 6566 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6567 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6568 | Builder.AddTextChunk("NSUInteger"); |
| 6569 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6570 | Builder.AddTextChunk("index"); |
| 6571 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 6572 | CXCursor_ObjCInstanceMethodDecl)); |
| 6573 | } |
| 6574 | } |
| 6575 | |
| 6576 | // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes |
| 6577 | if (IsInstanceMethod && |
| 6578 | (ReturnType.isNull() || |
| 6579 | (ReturnType->isObjCObjectPointerType() && |
| 6580 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 6581 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 6582 | ->getName() == "NSArray"))) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6583 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6584 | = (Twine(Property->getName()) + "AtIndexes").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6585 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6586 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6587 | if (ReturnType.isNull()) { |
| 6588 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6589 | Builder.AddTextChunk("NSArray *"); |
| 6590 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6591 | } |
| 6592 | |
| 6593 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6594 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6595 | Builder.AddTextChunk("NSIndexSet *"); |
| 6596 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6597 | Builder.AddTextChunk("indexes"); |
| 6598 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 6599 | CXCursor_ObjCInstanceMethodDecl)); |
| 6600 | } |
| 6601 | } |
| 6602 | |
| 6603 | // Add -(void)getKey:(type **)buffer range:(NSRange)inRange |
| 6604 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6605 | std::string SelectorName = (Twine("get") + UpperKey).str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6606 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6607 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6608 | &Context.Idents.get("range") |
| 6609 | }; |
| 6610 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6611 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6612 | if (ReturnType.isNull()) { |
| 6613 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6614 | Builder.AddTextChunk("void"); |
| 6615 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6616 | } |
| 6617 | |
| 6618 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6619 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6620 | Builder.AddPlaceholderChunk("object-type"); |
| 6621 | Builder.AddTextChunk(" **"); |
| 6622 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6623 | Builder.AddTextChunk("buffer"); |
| 6624 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6625 | Builder.AddTypedTextChunk("range:"); |
| 6626 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6627 | Builder.AddTextChunk("NSRange"); |
| 6628 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6629 | Builder.AddTextChunk("inRange"); |
| 6630 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 6631 | CXCursor_ObjCInstanceMethodDecl)); |
| 6632 | } |
| 6633 | } |
| 6634 | |
| 6635 | // Mutable indexed accessors |
| 6636 | |
| 6637 | // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index |
| 6638 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6639 | std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6640 | IdentifierInfo *SelectorIds[2] = { |
| 6641 | &Context.Idents.get("insertObject"), |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6642 | &Context.Idents.get(SelectorName) |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6643 | }; |
| 6644 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6645 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6646 | if (ReturnType.isNull()) { |
| 6647 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6648 | Builder.AddTextChunk("void"); |
| 6649 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6650 | } |
| 6651 | |
| 6652 | Builder.AddTypedTextChunk("insertObject:"); |
| 6653 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6654 | Builder.AddPlaceholderChunk("object-type"); |
| 6655 | Builder.AddTextChunk(" *"); |
| 6656 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6657 | Builder.AddTextChunk("object"); |
| 6658 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6659 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6660 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6661 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 6662 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6663 | Builder.AddTextChunk("index"); |
| 6664 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6665 | CXCursor_ObjCInstanceMethodDecl)); |
| 6666 | } |
| 6667 | } |
| 6668 | |
| 6669 | // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes |
| 6670 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6671 | std::string SelectorName = (Twine("insert") + UpperKey).str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6672 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6673 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6674 | &Context.Idents.get("atIndexes") |
| 6675 | }; |
| 6676 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6677 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6678 | if (ReturnType.isNull()) { |
| 6679 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6680 | Builder.AddTextChunk("void"); |
| 6681 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6682 | } |
| 6683 | |
| 6684 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6685 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6686 | Builder.AddTextChunk("NSArray *"); |
| 6687 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6688 | Builder.AddTextChunk("array"); |
| 6689 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6690 | Builder.AddTypedTextChunk("atIndexes:"); |
| 6691 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6692 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 6693 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6694 | Builder.AddTextChunk("indexes"); |
| 6695 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6696 | CXCursor_ObjCInstanceMethodDecl)); |
| 6697 | } |
| 6698 | } |
| 6699 | |
| 6700 | // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index |
| 6701 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6702 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6703 | = (Twine("removeObjectFrom") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6704 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6705 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6706 | if (ReturnType.isNull()) { |
| 6707 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6708 | Builder.AddTextChunk("void"); |
| 6709 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6710 | } |
| 6711 | |
| 6712 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6713 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6714 | Builder.AddTextChunk("NSUInteger"); |
| 6715 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6716 | Builder.AddTextChunk("index"); |
| 6717 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6718 | CXCursor_ObjCInstanceMethodDecl)); |
| 6719 | } |
| 6720 | } |
| 6721 | |
| 6722 | // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes |
| 6723 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6724 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6725 | = (Twine("remove") + UpperKey + "AtIndexes").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6726 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6727 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6728 | if (ReturnType.isNull()) { |
| 6729 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6730 | Builder.AddTextChunk("void"); |
| 6731 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6732 | } |
| 6733 | |
| 6734 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6735 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6736 | Builder.AddTextChunk("NSIndexSet *"); |
| 6737 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6738 | Builder.AddTextChunk("indexes"); |
| 6739 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6740 | CXCursor_ObjCInstanceMethodDecl)); |
| 6741 | } |
| 6742 | } |
| 6743 | |
| 6744 | // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object |
| 6745 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6746 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6747 | = (Twine("replaceObjectIn") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6748 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6749 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6750 | &Context.Idents.get("withObject") |
| 6751 | }; |
| 6752 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6753 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6754 | if (ReturnType.isNull()) { |
| 6755 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6756 | Builder.AddTextChunk("void"); |
| 6757 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6758 | } |
| 6759 | |
| 6760 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6761 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6762 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 6763 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6764 | Builder.AddTextChunk("index"); |
| 6765 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6766 | Builder.AddTypedTextChunk("withObject:"); |
| 6767 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6768 | Builder.AddTextChunk("id"); |
| 6769 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6770 | Builder.AddTextChunk("object"); |
| 6771 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6772 | CXCursor_ObjCInstanceMethodDecl)); |
| 6773 | } |
| 6774 | } |
| 6775 | |
| 6776 | // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array |
| 6777 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6778 | std::string SelectorName1 |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6779 | = (Twine("replace") + UpperKey + "AtIndexes").str(); |
| 6780 | std::string SelectorName2 = (Twine("with") + UpperKey).str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6781 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6782 | &Context.Idents.get(SelectorName1), |
| 6783 | &Context.Idents.get(SelectorName2) |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6784 | }; |
| 6785 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6786 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).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(Allocator.CopyString(SelectorName1 + ":")); |
| 6794 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6795 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 6796 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6797 | Builder.AddTextChunk("indexes"); |
| 6798 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6799 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":")); |
| 6800 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6801 | Builder.AddTextChunk("NSArray *"); |
| 6802 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6803 | Builder.AddTextChunk("array"); |
| 6804 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6805 | CXCursor_ObjCInstanceMethodDecl)); |
| 6806 | } |
| 6807 | } |
| 6808 | |
| 6809 | // Unordered getters |
| 6810 | // - (NSEnumerator *)enumeratorOfKey |
| 6811 | if (IsInstanceMethod && |
| 6812 | (ReturnType.isNull() || |
| 6813 | (ReturnType->isObjCObjectPointerType() && |
| 6814 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 6815 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 6816 | ->getName() == "NSEnumerator"))) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6817 | std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6818 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6819 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 6820 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6821 | if (ReturnType.isNull()) { |
| 6822 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6823 | Builder.AddTextChunk("NSEnumerator *"); |
| 6824 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6825 | } |
| 6826 | |
| 6827 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 6828 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
| 6829 | CXCursor_ObjCInstanceMethodDecl)); |
| 6830 | } |
| 6831 | } |
| 6832 | |
| 6833 | // - (type *)memberOfKey:(type *)object |
| 6834 | if (IsInstanceMethod && |
| 6835 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6836 | std::string SelectorName = (Twine("memberOf") + 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.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6839 | if (ReturnType.isNull()) { |
| 6840 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6841 | Builder.AddPlaceholderChunk("object-type"); |
| 6842 | Builder.AddTextChunk(" *"); |
| 6843 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6844 | } |
| 6845 | |
| 6846 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6847 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6848 | if (ReturnType.isNull()) { |
| 6849 | Builder.AddPlaceholderChunk("object-type"); |
| 6850 | Builder.AddTextChunk(" *"); |
| 6851 | } else { |
| 6852 | Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6853 | Policy, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6854 | Builder.getAllocator())); |
| 6855 | } |
| 6856 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6857 | Builder.AddTextChunk("object"); |
| 6858 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
| 6859 | CXCursor_ObjCInstanceMethodDecl)); |
| 6860 | } |
| 6861 | } |
| 6862 | |
| 6863 | // Mutable unordered accessors |
| 6864 | // - (void)addKeyObject:(type *)object |
| 6865 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6866 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6867 | = (Twine("add") + UpperKey + Twine("Object")).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6868 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6869 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6870 | if (ReturnType.isNull()) { |
| 6871 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6872 | Builder.AddTextChunk("void"); |
| 6873 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6874 | } |
| 6875 | |
| 6876 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6877 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6878 | Builder.AddPlaceholderChunk("object-type"); |
| 6879 | Builder.AddTextChunk(" *"); |
| 6880 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6881 | Builder.AddTextChunk("object"); |
| 6882 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6883 | CXCursor_ObjCInstanceMethodDecl)); |
| 6884 | } |
| 6885 | } |
| 6886 | |
| 6887 | // - (void)addKey:(NSSet *)objects |
| 6888 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6889 | std::string SelectorName = (Twine("add") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6890 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6891 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6892 | if (ReturnType.isNull()) { |
| 6893 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6894 | Builder.AddTextChunk("void"); |
| 6895 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6896 | } |
| 6897 | |
| 6898 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6899 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6900 | Builder.AddTextChunk("NSSet *"); |
| 6901 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6902 | Builder.AddTextChunk("objects"); |
| 6903 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6904 | CXCursor_ObjCInstanceMethodDecl)); |
| 6905 | } |
| 6906 | } |
| 6907 | |
| 6908 | // - (void)removeKeyObject:(type *)object |
| 6909 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6910 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6911 | = (Twine("remove") + UpperKey + Twine("Object")).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6912 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6913 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6914 | if (ReturnType.isNull()) { |
| 6915 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6916 | Builder.AddTextChunk("void"); |
| 6917 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6918 | } |
| 6919 | |
| 6920 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6921 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6922 | Builder.AddPlaceholderChunk("object-type"); |
| 6923 | Builder.AddTextChunk(" *"); |
| 6924 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6925 | Builder.AddTextChunk("object"); |
| 6926 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6927 | CXCursor_ObjCInstanceMethodDecl)); |
| 6928 | } |
| 6929 | } |
| 6930 | |
| 6931 | // - (void)removeKey:(NSSet *)objects |
| 6932 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6933 | std::string SelectorName = (Twine("remove") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6934 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6935 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6936 | if (ReturnType.isNull()) { |
| 6937 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6938 | Builder.AddTextChunk("void"); |
| 6939 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6940 | } |
| 6941 | |
| 6942 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6943 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6944 | Builder.AddTextChunk("NSSet *"); |
| 6945 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6946 | Builder.AddTextChunk("objects"); |
| 6947 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6948 | CXCursor_ObjCInstanceMethodDecl)); |
| 6949 | } |
| 6950 | } |
| 6951 | |
| 6952 | // - (void)intersectKey:(NSSet *)objects |
| 6953 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6954 | std::string SelectorName = (Twine("intersect") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6955 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6956 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6957 | if (ReturnType.isNull()) { |
| 6958 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6959 | Builder.AddTextChunk("void"); |
| 6960 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6961 | } |
| 6962 | |
| 6963 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6964 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6965 | Builder.AddTextChunk("NSSet *"); |
| 6966 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6967 | Builder.AddTextChunk("objects"); |
| 6968 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6969 | CXCursor_ObjCInstanceMethodDecl)); |
| 6970 | } |
| 6971 | } |
| 6972 | |
| 6973 | // Key-Value Observing |
| 6974 | // + (NSSet *)keyPathsForValuesAffectingKey |
| 6975 | if (!IsInstanceMethod && |
| 6976 | (ReturnType.isNull() || |
| 6977 | (ReturnType->isObjCObjectPointerType() && |
| 6978 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 6979 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 6980 | ->getName() == "NSSet"))) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6981 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6982 | = (Twine("keyPathsForValuesAffecting") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6983 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6984 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 6985 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6986 | if (ReturnType.isNull()) { |
| 6987 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6988 | Builder.AddTextChunk("NSSet *"); |
| 6989 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6990 | } |
| 6991 | |
| 6992 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 6993 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 6994 | CXCursor_ObjCClassMethodDecl)); |
| 6995 | } |
| 6996 | } |
| 6997 | |
| 6998 | // + (BOOL)automaticallyNotifiesObserversForKey |
| 6999 | if (!IsInstanceMethod && |
| 7000 | (ReturnType.isNull() || |
| 7001 | ReturnType->isIntegerType() || |
| 7002 | ReturnType->isBooleanType())) { |
| 7003 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7004 | = (Twine("automaticallyNotifiesObserversOf") + UpperKey).str(); |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7005 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7006 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7007 | .second) { |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7008 | if (ReturnType.isNull()) { |
| 7009 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7010 | Builder.AddTextChunk("BOOL"); |
| 7011 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7012 | } |
| 7013 | |
| 7014 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 7015 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 7016 | CXCursor_ObjCClassMethodDecl)); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7017 | } |
| 7018 | } |
| 7019 | } |
| 7020 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7021 | void Sema::CodeCompleteObjCMethodDecl(Scope *S, |
| 7022 | bool IsInstanceMethod, |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7023 | ParsedType ReturnTy) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7024 | // Determine the return type of the method we're declaring, if |
| 7025 | // provided. |
| 7026 | QualType ReturnType = GetTypeFromParser(ReturnTy); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7027 | Decl *IDecl = nullptr; |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7028 | if (CurContext->isObjCContainer()) { |
| 7029 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
| 7030 | IDecl = cast<Decl>(OCD); |
| 7031 | } |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7032 | // Determine where we should start searching for methods. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7033 | ObjCContainerDecl *SearchDecl = nullptr; |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7034 | bool IsInImplementation = false; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7035 | if (Decl *D = IDecl) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7036 | if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) { |
| 7037 | SearchDecl = Impl->getClassInterface(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7038 | IsInImplementation = true; |
| 7039 | } else if (ObjCCategoryImplDecl *CatImpl |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7040 | = dyn_cast<ObjCCategoryImplDecl>(D)) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7041 | SearchDecl = CatImpl->getCategoryDecl(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7042 | IsInImplementation = true; |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7043 | } else |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7044 | SearchDecl = dyn_cast<ObjCContainerDecl>(D); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7045 | } |
| 7046 | |
| 7047 | if (!SearchDecl && S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 7048 | if (DeclContext *DC = S->getEntity()) |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7049 | SearchDecl = dyn_cast<ObjCContainerDecl>(DC); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7050 | } |
| 7051 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7052 | if (!SearchDecl) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 7053 | HandleCodeCompleteResults(this, CodeCompleter, |
| 7054 | CodeCompletionContext::CCC_Other, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7055 | nullptr, 0); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7056 | return; |
| 7057 | } |
| 7058 | |
| 7059 | // Find all of the methods that we could declare/implement here. |
| 7060 | KnownMethodsMap KnownMethods; |
| 7061 | FindImplementableMethods(Context, SearchDecl, IsInstanceMethod, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7062 | ReturnType, KnownMethods); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7063 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7064 | // Add declarations or definitions for each of the known methods. |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7065 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7066 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7067 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7068 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7069 | Results.EnterNewScope(); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7070 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7071 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
| 7072 | MEnd = KnownMethods.end(); |
| 7073 | M != MEnd; ++M) { |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 7074 | ObjCMethodDecl *Method = M->second.getPointer(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7075 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7076 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7077 | |
| 7078 | // If the result type was not already provided, add it to the |
| 7079 | // pattern as (type). |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7080 | if (ReturnType.isNull()) { |
| 7081 | QualType ResTy = Method->getSendResultType().stripObjCKindOfType(Context); |
| 7082 | AttributedType::stripOuterNullability(ResTy); |
| 7083 | AddObjCPassingTypeChunk(ResTy, |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7084 | Method->getObjCDeclQualifier(), Context, Policy, |
| 7085 | Builder); |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7086 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7087 | |
| 7088 | Selector Sel = Method->getSelector(); |
| 7089 | |
| 7090 | // Add the first part of the selector to the pattern. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7091 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 7092 | Sel.getNameForSlot(0))); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7093 | |
| 7094 | // Add parameters to the pattern. |
| 7095 | unsigned I = 0; |
| 7096 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 7097 | PEnd = Method->param_end(); |
| 7098 | P != PEnd; (void)++P, ++I) { |
| 7099 | // Add the part of the selector name. |
| 7100 | if (I == 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7101 | Builder.AddTypedTextChunk(":"); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7102 | else if (I < Sel.getNumArgs()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7103 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7104 | Builder.AddTypedTextChunk( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 7105 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7106 | } else |
| 7107 | break; |
| 7108 | |
| 7109 | // Add the parameter type. |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 7110 | QualType ParamType; |
| 7111 | if ((*P)->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 7112 | ParamType = (*P)->getType(); |
| 7113 | else |
| 7114 | ParamType = (*P)->getOriginalType(); |
Douglas Gregor | 9b7b3e9 | 2015-07-07 06:20:27 +0000 | [diff] [blame] | 7115 | ParamType = ParamType.substObjCTypeArgs(Context, {}, |
| 7116 | ObjCSubstitutionContext::Parameter); |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7117 | AttributedType::stripOuterNullability(ParamType); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 7118 | AddObjCPassingTypeChunk(ParamType, |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 7119 | (*P)->getObjCDeclQualifier(), |
| 7120 | Context, Policy, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7121 | Builder); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7122 | |
| 7123 | if (IdentifierInfo *Id = (*P)->getIdentifier()) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7124 | Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName())); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7125 | } |
| 7126 | |
| 7127 | if (Method->isVariadic()) { |
| 7128 | if (Method->param_size() > 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7129 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 7130 | Builder.AddTextChunk("..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 7131 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7132 | |
Douglas Gregor | d37c59d | 2010-05-28 00:57:46 +0000 | [diff] [blame] | 7133 | if (IsInImplementation && Results.includeCodePatterns()) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7134 | // We will be defining the method here, so add a compound statement. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7135 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7136 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 7137 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7138 | if (!Method->getReturnType()->isVoidType()) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7139 | // If the result type is not void, add a return clause. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7140 | Builder.AddTextChunk("return"); |
| 7141 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7142 | Builder.AddPlaceholderChunk("expression"); |
| 7143 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7144 | } else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7145 | Builder.AddPlaceholderChunk("statements"); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7146 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7147 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 7148 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7149 | } |
| 7150 | |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 7151 | unsigned Priority = CCP_CodePattern; |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 7152 | if (!M->second.getInt()) |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 7153 | Priority += CCD_InBaseClass; |
| 7154 | |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 7155 | Results.AddResult(Result(Builder.TakeString(), Method, Priority)); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7156 | } |
| 7157 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7158 | // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of |
| 7159 | // the properties in this class and its categories. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7160 | if (Context.getLangOpts().ObjC2) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7161 | SmallVector<ObjCContainerDecl *, 4> Containers; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7162 | Containers.push_back(SearchDecl); |
| 7163 | |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 7164 | VisitedSelectorSet KnownSelectors; |
| 7165 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
| 7166 | MEnd = KnownMethods.end(); |
| 7167 | M != MEnd; ++M) |
| 7168 | KnownSelectors.insert(M->first); |
| 7169 | |
| 7170 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7171 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl); |
| 7172 | if (!IFace) |
| 7173 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl)) |
| 7174 | IFace = Category->getClassInterface(); |
| 7175 | |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 7176 | if (IFace) |
| 7177 | for (auto *Cat : IFace->visible_categories()) |
| 7178 | Containers.push_back(Cat); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7179 | |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 7180 | for (unsigned I = 0, N = Containers.size(); I != N; ++I) |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame^] | 7181 | for (auto *P : Containers[I]->instance_properties()) |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 7182 | AddObjCKeyValueCompletions(P, IsInstanceMethod, ReturnType, Context, |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 7183 | KnownSelectors, Results); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7184 | } |
| 7185 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7186 | Results.ExitScope(); |
| 7187 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 7188 | HandleCodeCompleteResults(this, CodeCompleter, |
| 7189 | CodeCompletionContext::CCC_Other, |
| 7190 | Results.data(),Results.size()); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7191 | } |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7192 | |
| 7193 | void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S, |
| 7194 | bool IsInstanceMethod, |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7195 | bool AtParameterName, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7196 | ParsedType ReturnTy, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7197 | ArrayRef<IdentifierInfo *> SelIdents) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7198 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 7199 | // pool from the AST file. |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7200 | if (ExternalSource) { |
| 7201 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 7202 | I != N; ++I) { |
| 7203 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7204 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7205 | continue; |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7206 | |
| 7207 | ReadMethodPool(Sel); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7208 | } |
| 7209 | } |
| 7210 | |
| 7211 | // Build the set of methods we can see. |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7212 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7213 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7214 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7215 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7216 | |
| 7217 | if (ReturnTy) |
| 7218 | Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType()); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7219 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7220 | Results.EnterNewScope(); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7221 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 7222 | MEnd = MethodPool.end(); |
| 7223 | M != MEnd; ++M) { |
| 7224 | for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first : |
| 7225 | &M->second.second; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7226 | MethList && MethList->getMethod(); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 7227 | MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7228 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7229 | continue; |
| 7230 | |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7231 | if (AtParameterName) { |
| 7232 | // Suggest parameter names we've seen before. |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7233 | unsigned NumSelIdents = SelIdents.size(); |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7234 | if (NumSelIdents && |
| 7235 | NumSelIdents <= MethList->getMethod()->param_size()) { |
| 7236 | ParmVarDecl *Param = |
| 7237 | MethList->getMethod()->parameters()[NumSelIdents - 1]; |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7238 | if (Param->getIdentifier()) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7239 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7240 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7241 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7242 | Param->getIdentifier()->getName())); |
| 7243 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7244 | } |
| 7245 | } |
| 7246 | |
| 7247 | continue; |
| 7248 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7249 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7250 | Result R(MethList->getMethod(), |
| 7251 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7252 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7253 | R.AllParametersAreInformative = false; |
| 7254 | R.DeclaringEntity = true; |
| 7255 | Results.MaybeAddResult(R, CurContext); |
| 7256 | } |
| 7257 | } |
| 7258 | |
| 7259 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 7260 | HandleCodeCompleteResults(this, CodeCompleter, |
| 7261 | CodeCompletionContext::CCC_Other, |
| 7262 | Results.data(),Results.size()); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7263 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 7264 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7265 | void Sema::CodeCompletePreprocessorDirective(bool InConditional) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7266 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7267 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7268 | CodeCompletionContext::CCC_PreprocessorDirective); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7269 | Results.EnterNewScope(); |
| 7270 | |
| 7271 | // #if <condition> |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7272 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7273 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7274 | Builder.AddTypedTextChunk("if"); |
| 7275 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7276 | Builder.AddPlaceholderChunk("condition"); |
| 7277 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7278 | |
| 7279 | // #ifdef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7280 | Builder.AddTypedTextChunk("ifdef"); |
| 7281 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7282 | Builder.AddPlaceholderChunk("macro"); |
| 7283 | Results.AddResult(Builder.TakeString()); |
| 7284 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7285 | // #ifndef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7286 | Builder.AddTypedTextChunk("ifndef"); |
| 7287 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7288 | Builder.AddPlaceholderChunk("macro"); |
| 7289 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7290 | |
| 7291 | if (InConditional) { |
| 7292 | // #elif <condition> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7293 | Builder.AddTypedTextChunk("elif"); |
| 7294 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7295 | Builder.AddPlaceholderChunk("condition"); |
| 7296 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7297 | |
| 7298 | // #else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7299 | Builder.AddTypedTextChunk("else"); |
| 7300 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7301 | |
| 7302 | // #endif |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7303 | Builder.AddTypedTextChunk("endif"); |
| 7304 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7305 | } |
| 7306 | |
| 7307 | // #include "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7308 | Builder.AddTypedTextChunk("include"); |
| 7309 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7310 | Builder.AddTextChunk("\""); |
| 7311 | Builder.AddPlaceholderChunk("header"); |
| 7312 | Builder.AddTextChunk("\""); |
| 7313 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7314 | |
| 7315 | // #include <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7316 | Builder.AddTypedTextChunk("include"); |
| 7317 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7318 | Builder.AddTextChunk("<"); |
| 7319 | Builder.AddPlaceholderChunk("header"); |
| 7320 | Builder.AddTextChunk(">"); |
| 7321 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7322 | |
| 7323 | // #define <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7324 | Builder.AddTypedTextChunk("define"); |
| 7325 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7326 | Builder.AddPlaceholderChunk("macro"); |
| 7327 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7328 | |
| 7329 | // #define <macro>(<args>) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7330 | Builder.AddTypedTextChunk("define"); |
| 7331 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7332 | Builder.AddPlaceholderChunk("macro"); |
| 7333 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7334 | Builder.AddPlaceholderChunk("args"); |
| 7335 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7336 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7337 | |
| 7338 | // #undef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7339 | Builder.AddTypedTextChunk("undef"); |
| 7340 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7341 | Builder.AddPlaceholderChunk("macro"); |
| 7342 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7343 | |
| 7344 | // #line <number> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7345 | Builder.AddTypedTextChunk("line"); |
| 7346 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7347 | Builder.AddPlaceholderChunk("number"); |
| 7348 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7349 | |
| 7350 | // #line <number> "filename" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7351 | Builder.AddTypedTextChunk("line"); |
| 7352 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7353 | Builder.AddPlaceholderChunk("number"); |
| 7354 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7355 | Builder.AddTextChunk("\""); |
| 7356 | Builder.AddPlaceholderChunk("filename"); |
| 7357 | Builder.AddTextChunk("\""); |
| 7358 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7359 | |
| 7360 | // #error <message> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7361 | Builder.AddTypedTextChunk("error"); |
| 7362 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7363 | Builder.AddPlaceholderChunk("message"); |
| 7364 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7365 | |
| 7366 | // #pragma <arguments> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7367 | Builder.AddTypedTextChunk("pragma"); |
| 7368 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7369 | Builder.AddPlaceholderChunk("arguments"); |
| 7370 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7371 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7372 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7373 | // #import "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7374 | Builder.AddTypedTextChunk("import"); |
| 7375 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7376 | Builder.AddTextChunk("\""); |
| 7377 | Builder.AddPlaceholderChunk("header"); |
| 7378 | Builder.AddTextChunk("\""); |
| 7379 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7380 | |
| 7381 | // #import <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7382 | Builder.AddTypedTextChunk("import"); |
| 7383 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7384 | Builder.AddTextChunk("<"); |
| 7385 | Builder.AddPlaceholderChunk("header"); |
| 7386 | Builder.AddTextChunk(">"); |
| 7387 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7388 | } |
| 7389 | |
| 7390 | // #include_next "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7391 | Builder.AddTypedTextChunk("include_next"); |
| 7392 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7393 | Builder.AddTextChunk("\""); |
| 7394 | Builder.AddPlaceholderChunk("header"); |
| 7395 | Builder.AddTextChunk("\""); |
| 7396 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7397 | |
| 7398 | // #include_next <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7399 | Builder.AddTypedTextChunk("include_next"); |
| 7400 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7401 | Builder.AddTextChunk("<"); |
| 7402 | Builder.AddPlaceholderChunk("header"); |
| 7403 | Builder.AddTextChunk(">"); |
| 7404 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7405 | |
| 7406 | // #warning <message> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7407 | Builder.AddTypedTextChunk("warning"); |
| 7408 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7409 | Builder.AddPlaceholderChunk("message"); |
| 7410 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7411 | |
| 7412 | // Note: #ident and #sccs are such crazy anachronisms that we don't provide |
| 7413 | // completions for them. And __include_macros is a Clang-internal extension |
| 7414 | // that we don't want to encourage anyone to use. |
| 7415 | |
| 7416 | // FIXME: we don't support #assert or #unassert, so don't suggest them. |
| 7417 | Results.ExitScope(); |
| 7418 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7419 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 7420 | CodeCompletionContext::CCC_PreprocessorDirective, |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7421 | Results.data(), Results.size()); |
| 7422 | } |
| 7423 | |
| 7424 | void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) { |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7425 | CodeCompleteOrdinaryName(S, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7426 | S->getFnParent()? Sema::PCC_RecoveryInFunction |
| 7427 | : Sema::PCC_Namespace); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7428 | } |
| 7429 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7430 | void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7431 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7432 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7433 | IsDefinition? CodeCompletionContext::CCC_MacroName |
| 7434 | : CodeCompletionContext::CCC_MacroNameUse); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7435 | if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) { |
| 7436 | // Add just the names of macros, not their arguments. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7437 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7438 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7439 | Results.EnterNewScope(); |
| 7440 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 7441 | MEnd = PP.macro_end(); |
| 7442 | M != MEnd; ++M) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7443 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7444 | M->first->getName())); |
Argyrios Kyrtzidis | 5c8b1cd | 2012-09-27 00:24:09 +0000 | [diff] [blame] | 7445 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
| 7446 | CCP_CodePattern, |
| 7447 | CXCursor_MacroDefinition)); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7448 | } |
| 7449 | Results.ExitScope(); |
| 7450 | } else if (IsDefinition) { |
| 7451 | // FIXME: Can we detect when the user just wrote an include guard above? |
| 7452 | } |
| 7453 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7454 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7455 | Results.data(), Results.size()); |
| 7456 | } |
| 7457 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7458 | void Sema::CodeCompletePreprocessorExpression() { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7459 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7460 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7461 | CodeCompletionContext::CCC_PreprocessorExpression); |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7462 | |
| 7463 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 7464 | AddMacroResults(PP, Results, true); |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7465 | |
| 7466 | // defined (<macro>) |
| 7467 | Results.EnterNewScope(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7468 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7469 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7470 | Builder.AddTypedTextChunk("defined"); |
| 7471 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7472 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7473 | Builder.AddPlaceholderChunk("macro"); |
| 7474 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7475 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7476 | Results.ExitScope(); |
| 7477 | |
| 7478 | HandleCodeCompleteResults(this, CodeCompleter, |
| 7479 | CodeCompletionContext::CCC_PreprocessorExpression, |
| 7480 | Results.data(), Results.size()); |
| 7481 | } |
| 7482 | |
| 7483 | void Sema::CodeCompletePreprocessorMacroArgument(Scope *S, |
| 7484 | IdentifierInfo *Macro, |
| 7485 | MacroInfo *MacroInfo, |
| 7486 | unsigned Argument) { |
| 7487 | // FIXME: In the future, we could provide "overload" results, much like we |
| 7488 | // do for function calls. |
| 7489 | |
Argyrios Kyrtzidis | 75f6cd2 | 2011-08-18 19:41:28 +0000 | [diff] [blame] | 7490 | // Now just ignore this. There will be another code-completion callback |
| 7491 | // for the expanded tokens. |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7492 | } |
| 7493 | |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 7494 | void Sema::CodeCompleteNaturalLanguage() { |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 7495 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | ea73637 | 2010-08-25 17:10:00 +0000 | [diff] [blame] | 7496 | CodeCompletionContext::CCC_NaturalLanguage, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7497 | nullptr, 0); |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 7498 | } |
| 7499 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7500 | void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator, |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7501 | CodeCompletionTUInfo &CCTUInfo, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7502 | SmallVectorImpl<CodeCompletionResult> &Results) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7503 | ResultBuilder Builder(*this, Allocator, CCTUInfo, |
| 7504 | CodeCompletionContext::CCC_Recovery); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 7505 | if (!CodeCompleter || CodeCompleter->includeGlobals()) { |
| 7506 | CodeCompletionDeclConsumer Consumer(Builder, |
| 7507 | Context.getTranslationUnitDecl()); |
| 7508 | LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName, |
| 7509 | Consumer); |
| 7510 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 7511 | |
| 7512 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 7513 | AddMacroResults(PP, Builder, true); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 7514 | |
| 7515 | Results.clear(); |
| 7516 | Results.insert(Results.end(), |
| 7517 | Builder.data(), Builder.data() + Builder.size()); |
| 7518 | } |