Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1 | //===---------------- SemaCodeComplete.cpp - Code Completion ----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the code-completion semantic actions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 13 | #include "clang/Sema/SemaInternal.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 15 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprObjC.h" |
Jordan Rose | 4938f27 | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 17 | #include "clang/Basic/CharInfo.h" |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 18 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 19 | #include "clang/Lex/MacroInfo.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Sema/CodeCompleteConsumer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Sema/Lookup.h" |
| 23 | #include "clang/Sema/Overload.h" |
| 24 | #include "clang/Sema/Scope.h" |
| 25 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/DenseSet.h" |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallBitVector.h" |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallPtrSet.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | e6688e6 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringSwitch.h" |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 33 | #include <list> |
| 34 | #include <map> |
| 35 | #include <vector> |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace clang; |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 38 | using namespace sema; |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 39 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 40 | namespace { |
| 41 | /// \brief A container of code-completion results. |
| 42 | class ResultBuilder { |
| 43 | public: |
| 44 | /// \brief The type of a name-lookup filter, which can be provided to the |
| 45 | /// name-lookup routines to specify which declarations should be included in |
| 46 | /// the result set (when it returns true) and which declarations should be |
| 47 | /// filtered out (returns false). |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 48 | typedef bool (ResultBuilder::*LookupFilter)(const NamedDecl *) const; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 49 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 50 | typedef CodeCompletionResult Result; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | /// \brief The actual results we have found. |
| 54 | std::vector<Result> Results; |
| 55 | |
| 56 | /// \brief A record of all of the declarations we have found and placed |
| 57 | /// into the result set, used to ensure that no declaration ever gets into |
| 58 | /// the result set twice. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 59 | llvm::SmallPtrSet<const Decl*, 16> AllDeclsFound; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 60 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 61 | typedef std::pair<const NamedDecl *, unsigned> DeclIndexPair; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 62 | |
| 63 | /// \brief An entry in the shadow map, which is optimized to store |
| 64 | /// a single (declaration, index) mapping (the common case) but |
| 65 | /// can also store a list of (declaration, index) mappings. |
| 66 | class ShadowMapEntry { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 67 | typedef SmallVector<DeclIndexPair, 4> DeclIndexPairVector; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 68 | |
| 69 | /// \brief Contains either the solitary NamedDecl * or a vector |
| 70 | /// of (declaration, index) pairs. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 71 | llvm::PointerUnion<const NamedDecl *, DeclIndexPairVector*> DeclOrVector; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 72 | |
| 73 | /// \brief When the entry contains a single declaration, this is |
| 74 | /// the index associated with that entry. |
| 75 | unsigned SingleDeclIndex; |
| 76 | |
| 77 | public: |
| 78 | ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { } |
| 79 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 80 | void Add(const NamedDecl *ND, unsigned Index) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 81 | if (DeclOrVector.isNull()) { |
| 82 | // 0 - > 1 elements: just set the single element information. |
| 83 | DeclOrVector = ND; |
| 84 | SingleDeclIndex = Index; |
| 85 | return; |
| 86 | } |
| 87 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 88 | if (const NamedDecl *PrevND = |
| 89 | DeclOrVector.dyn_cast<const NamedDecl *>()) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 90 | // 1 -> 2 elements: create the vector of results and push in the |
| 91 | // existing declaration. |
| 92 | DeclIndexPairVector *Vec = new DeclIndexPairVector; |
| 93 | Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex)); |
| 94 | DeclOrVector = Vec; |
| 95 | } |
| 96 | |
| 97 | // Add the new element to the end of the vector. |
| 98 | DeclOrVector.get<DeclIndexPairVector*>()->push_back( |
| 99 | DeclIndexPair(ND, Index)); |
| 100 | } |
| 101 | |
| 102 | void Destroy() { |
| 103 | if (DeclIndexPairVector *Vec |
| 104 | = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) { |
| 105 | delete Vec; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 106 | DeclOrVector = ((NamedDecl *)nullptr); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | // Iteration. |
| 111 | class iterator; |
| 112 | iterator begin() const; |
| 113 | iterator end() const; |
| 114 | }; |
| 115 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 116 | /// \brief A mapping from declaration names to the declarations that have |
| 117 | /// this name within a particular scope and their index within the list of |
| 118 | /// results. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 119 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 120 | |
| 121 | /// \brief The semantic analysis object for which results are being |
| 122 | /// produced. |
| 123 | Sema &SemaRef; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 124 | |
| 125 | /// \brief The allocator used to allocate new code-completion strings. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 126 | CodeCompletionAllocator &Allocator; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 127 | |
| 128 | CodeCompletionTUInfo &CCTUInfo; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 129 | |
| 130 | /// \brief If non-NULL, a filter function used to remove any code-completion |
| 131 | /// results that are not desirable. |
| 132 | LookupFilter Filter; |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 133 | |
| 134 | /// \brief Whether we should allow declarations as |
| 135 | /// nested-name-specifiers that would otherwise be filtered out. |
| 136 | bool AllowNestedNameSpecifiers; |
| 137 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 138 | /// \brief If set, the type that we would prefer our resulting value |
| 139 | /// declarations to have. |
| 140 | /// |
| 141 | /// Closely matching the preferred type gives a boost to a result's |
| 142 | /// priority. |
| 143 | CanQualType PreferredType; |
| 144 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 145 | /// \brief A list of shadow maps, which is used to model name hiding at |
| 146 | /// different levels of, e.g., the inheritance hierarchy. |
| 147 | std::list<ShadowMap> ShadowMaps; |
| 148 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 149 | /// \brief If we're potentially referring to a C++ member function, the set |
| 150 | /// of qualifiers applied to the object type. |
| 151 | Qualifiers ObjectTypeQualifiers; |
| 152 | |
| 153 | /// \brief Whether the \p ObjectTypeQualifiers field is active. |
| 154 | bool HasObjectTypeQualifiers; |
| 155 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 156 | /// \brief The selector that we prefer. |
| 157 | Selector PreferredSelector; |
| 158 | |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 159 | /// \brief The completion context in which we are gathering results. |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 160 | CodeCompletionContext CompletionContext; |
| 161 | |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 162 | /// \brief If we are in an instance method definition, the \@implementation |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 163 | /// object. |
| 164 | ObjCImplementationDecl *ObjCImplementation; |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 165 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 166 | void AdjustResultPriorityForDecl(Result &R); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 167 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 168 | void MaybeAddConstructorResults(Result R); |
| 169 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 170 | public: |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 171 | explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator, |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 172 | CodeCompletionTUInfo &CCTUInfo, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 173 | const CodeCompletionContext &CompletionContext, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 174 | LookupFilter Filter = nullptr) |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 175 | : SemaRef(SemaRef), Allocator(Allocator), CCTUInfo(CCTUInfo), |
| 176 | Filter(Filter), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 177 | AllowNestedNameSpecifiers(false), HasObjectTypeQualifiers(false), |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 178 | CompletionContext(CompletionContext), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 179 | ObjCImplementation(nullptr) |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 180 | { |
| 181 | // If this is an Objective-C instance method definition, dig out the |
| 182 | // corresponding implementation. |
| 183 | switch (CompletionContext.getKind()) { |
| 184 | case CodeCompletionContext::CCC_Expression: |
| 185 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
| 186 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
| 187 | case CodeCompletionContext::CCC_Statement: |
| 188 | case CodeCompletionContext::CCC_Recovery: |
| 189 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) |
| 190 | if (Method->isInstanceMethod()) |
| 191 | if (ObjCInterfaceDecl *Interface = Method->getClassInterface()) |
| 192 | ObjCImplementation = Interface->getImplementation(); |
| 193 | break; |
| 194 | |
| 195 | default: |
| 196 | break; |
| 197 | } |
| 198 | } |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 199 | |
| 200 | /// \brief Determine the priority for a reference to the given declaration. |
| 201 | unsigned getBasePriority(const NamedDecl *D); |
| 202 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 203 | /// \brief Whether we should include code patterns in the completion |
| 204 | /// results. |
| 205 | bool includeCodePatterns() const { |
| 206 | return SemaRef.CodeCompleter && |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 207 | SemaRef.CodeCompleter->includeCodePatterns(); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 210 | /// \brief Set the filter used for code-completion results. |
| 211 | void setFilter(LookupFilter Filter) { |
| 212 | this->Filter = Filter; |
| 213 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 214 | |
| 215 | Result *data() { return Results.empty()? nullptr : &Results.front(); } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 216 | unsigned size() const { return Results.size(); } |
| 217 | bool empty() const { return Results.empty(); } |
| 218 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 219 | /// \brief Specify the preferred type. |
| 220 | void setPreferredType(QualType T) { |
| 221 | PreferredType = SemaRef.Context.getCanonicalType(T); |
| 222 | } |
| 223 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 224 | /// \brief Set the cv-qualifiers on the object type, for us in filtering |
| 225 | /// calls to member functions. |
| 226 | /// |
| 227 | /// When there are qualifiers in this set, they will be used to filter |
| 228 | /// out member functions that aren't available (because there will be a |
| 229 | /// cv-qualifier mismatch) or prefer functions with an exact qualifier |
| 230 | /// match. |
| 231 | void setObjectTypeQualifiers(Qualifiers Quals) { |
| 232 | ObjectTypeQualifiers = Quals; |
| 233 | HasObjectTypeQualifiers = true; |
| 234 | } |
| 235 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 236 | /// \brief Set the preferred selector. |
| 237 | /// |
| 238 | /// When an Objective-C method declaration result is added, and that |
| 239 | /// method's selector matches this preferred selector, we give that method |
| 240 | /// a slight priority boost. |
| 241 | void setPreferredSelector(Selector Sel) { |
| 242 | PreferredSelector = Sel; |
| 243 | } |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 244 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 245 | /// \brief Retrieve the code-completion context for which results are |
| 246 | /// being collected. |
| 247 | const CodeCompletionContext &getCompletionContext() const { |
| 248 | return CompletionContext; |
| 249 | } |
| 250 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 251 | /// \brief Specify whether nested-name-specifiers are allowed. |
| 252 | void allowNestedNameSpecifiers(bool Allow = true) { |
| 253 | AllowNestedNameSpecifiers = Allow; |
| 254 | } |
| 255 | |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 256 | /// \brief Return the semantic analysis object for which we are collecting |
| 257 | /// code completion results. |
| 258 | Sema &getSema() const { return SemaRef; } |
| 259 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 260 | /// \brief Retrieve the allocator used to allocate code completion strings. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 261 | CodeCompletionAllocator &getAllocator() const { return Allocator; } |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 262 | |
| 263 | CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 264 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 265 | /// \brief Determine whether the given declaration is at all interesting |
| 266 | /// as a code-completion result. |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 267 | /// |
| 268 | /// \param ND the declaration that we are inspecting. |
| 269 | /// |
| 270 | /// \param AsNestedNameSpecifier will be set true if this declaration is |
| 271 | /// only interesting when it is a nested-name-specifier. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 272 | bool isInterestingDecl(const NamedDecl *ND, |
| 273 | bool &AsNestedNameSpecifier) const; |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 274 | |
| 275 | /// \brief Check whether the result is hidden by the Hiding declaration. |
| 276 | /// |
| 277 | /// \returns true if the result is hidden and cannot be found, false if |
| 278 | /// the hidden result could still be found. When false, \p R may be |
| 279 | /// modified to describe how the result can be found (e.g., via extra |
| 280 | /// qualification). |
| 281 | bool CheckHiddenResult(Result &R, DeclContext *CurContext, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 282 | const NamedDecl *Hiding); |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 283 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 284 | /// \brief Add a new result to this result set (if it isn't already in one |
| 285 | /// of the shadow maps), or replace an existing result (for, e.g., a |
| 286 | /// redeclaration). |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 287 | /// |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 288 | /// \param R the result to add (if it is unique). |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 289 | /// |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 290 | /// \param CurContext the context in which this result will be named. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 291 | void MaybeAddResult(Result R, DeclContext *CurContext = nullptr); |
| 292 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 293 | /// \brief Add a new result to this result set, where we already know |
Yaron Keren | 8fbe4398 | 2014-11-14 18:33:42 +0000 | [diff] [blame] | 294 | /// the hiding declaration (if any). |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 295 | /// |
| 296 | /// \param R the result to add (if it is unique). |
| 297 | /// |
| 298 | /// \param CurContext the context in which this result will be named. |
| 299 | /// |
| 300 | /// \param Hiding the declaration that hides the result. |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 301 | /// |
| 302 | /// \param InBaseClass whether the result was found in a base |
| 303 | /// class of the searched context. |
| 304 | void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding, |
| 305 | bool InBaseClass); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 306 | |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 307 | /// \brief Add a new non-declaration result to this result set. |
| 308 | void AddResult(Result R); |
| 309 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 310 | /// \brief Enter into a new scope. |
| 311 | void EnterNewScope(); |
| 312 | |
| 313 | /// \brief Exit from the current scope. |
| 314 | void ExitScope(); |
| 315 | |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 316 | /// \brief Ignore this declaration, if it is seen again. |
Dmitri Gribenko | 6cfb153 | 2013-02-14 13:53:30 +0000 | [diff] [blame] | 317 | void Ignore(const Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); } |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 318 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 319 | /// \name Name lookup predicates |
| 320 | /// |
| 321 | /// These predicates can be passed to the name lookup functions to filter the |
| 322 | /// results of name lookup. All of the predicates have the same type, so that |
| 323 | /// |
| 324 | //@{ |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 325 | bool IsOrdinaryName(const NamedDecl *ND) const; |
| 326 | bool IsOrdinaryNonTypeName(const NamedDecl *ND) const; |
| 327 | bool IsIntegralConstantValue(const NamedDecl *ND) const; |
| 328 | bool IsOrdinaryNonValueName(const NamedDecl *ND) const; |
| 329 | bool IsNestedNameSpecifier(const NamedDecl *ND) const; |
| 330 | bool IsEnum(const NamedDecl *ND) const; |
| 331 | bool IsClassOrStruct(const NamedDecl *ND) const; |
| 332 | bool IsUnion(const NamedDecl *ND) const; |
| 333 | bool IsNamespace(const NamedDecl *ND) const; |
| 334 | bool IsNamespaceOrAlias(const NamedDecl *ND) const; |
| 335 | bool IsType(const NamedDecl *ND) const; |
| 336 | bool IsMember(const NamedDecl *ND) const; |
| 337 | bool IsObjCIvar(const NamedDecl *ND) const; |
| 338 | bool IsObjCMessageReceiver(const NamedDecl *ND) const; |
| 339 | bool IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const; |
| 340 | bool IsObjCCollection(const NamedDecl *ND) const; |
| 341 | bool IsImpossibleToSatisfy(const NamedDecl *ND) const; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 342 | //@} |
| 343 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 344 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 345 | |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 346 | class ResultBuilder::ShadowMapEntry::iterator { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 347 | llvm::PointerUnion<const NamedDecl *, const DeclIndexPair *> DeclOrIterator; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 348 | unsigned SingleDeclIndex; |
| 349 | |
| 350 | public: |
| 351 | typedef DeclIndexPair value_type; |
| 352 | typedef value_type reference; |
| 353 | typedef std::ptrdiff_t difference_type; |
| 354 | typedef std::input_iterator_tag iterator_category; |
| 355 | |
| 356 | class pointer { |
| 357 | DeclIndexPair Value; |
| 358 | |
| 359 | public: |
| 360 | pointer(const DeclIndexPair &Value) : Value(Value) { } |
| 361 | |
| 362 | const DeclIndexPair *operator->() const { |
| 363 | return &Value; |
| 364 | } |
| 365 | }; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 366 | |
| 367 | iterator() : DeclOrIterator((NamedDecl *)nullptr), SingleDeclIndex(0) {} |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 368 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 369 | iterator(const NamedDecl *SingleDecl, unsigned Index) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 370 | : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { } |
| 371 | |
| 372 | iterator(const DeclIndexPair *Iterator) |
| 373 | : DeclOrIterator(Iterator), SingleDeclIndex(0) { } |
| 374 | |
| 375 | iterator &operator++() { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 376 | if (DeclOrIterator.is<const NamedDecl *>()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 377 | DeclOrIterator = (NamedDecl *)nullptr; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 378 | SingleDeclIndex = 0; |
| 379 | return *this; |
| 380 | } |
| 381 | |
| 382 | const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>(); |
| 383 | ++I; |
| 384 | DeclOrIterator = I; |
| 385 | return *this; |
| 386 | } |
| 387 | |
Chris Lattner | 9795b39 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 388 | /*iterator operator++(int) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 389 | iterator tmp(*this); |
| 390 | ++(*this); |
| 391 | return tmp; |
Chris Lattner | 9795b39 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 392 | }*/ |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 393 | |
| 394 | reference operator*() const { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 395 | if (const NamedDecl *ND = DeclOrIterator.dyn_cast<const NamedDecl *>()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 396 | return reference(ND, SingleDeclIndex); |
| 397 | |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 398 | return *DeclOrIterator.get<const DeclIndexPair*>(); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | pointer operator->() const { |
| 402 | return pointer(**this); |
| 403 | } |
| 404 | |
| 405 | friend bool operator==(const iterator &X, const iterator &Y) { |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 406 | return X.DeclOrIterator.getOpaqueValue() |
| 407 | == Y.DeclOrIterator.getOpaqueValue() && |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 408 | X.SingleDeclIndex == Y.SingleDeclIndex; |
| 409 | } |
| 410 | |
| 411 | friend bool operator!=(const iterator &X, const iterator &Y) { |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 412 | return !(X == Y); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 413 | } |
| 414 | }; |
| 415 | |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 416 | ResultBuilder::ShadowMapEntry::iterator |
| 417 | ResultBuilder::ShadowMapEntry::begin() const { |
| 418 | if (DeclOrVector.isNull()) |
| 419 | return iterator(); |
| 420 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 421 | if (const NamedDecl *ND = DeclOrVector.dyn_cast<const NamedDecl *>()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 422 | return iterator(ND, SingleDeclIndex); |
| 423 | |
| 424 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin()); |
| 425 | } |
| 426 | |
| 427 | ResultBuilder::ShadowMapEntry::iterator |
| 428 | ResultBuilder::ShadowMapEntry::end() const { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 429 | if (DeclOrVector.is<const NamedDecl *>() || DeclOrVector.isNull()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 430 | return iterator(); |
| 431 | |
| 432 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end()); |
| 433 | } |
| 434 | |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 435 | /// \brief Compute the qualification required to get from the current context |
| 436 | /// (\p CurContext) to the target context (\p TargetContext). |
| 437 | /// |
| 438 | /// \param Context the AST context in which the qualification will be used. |
| 439 | /// |
| 440 | /// \param CurContext the context where an entity is being named, which is |
| 441 | /// typically based on the current scope. |
| 442 | /// |
| 443 | /// \param TargetContext the context in which the named entity actually |
| 444 | /// resides. |
| 445 | /// |
| 446 | /// \returns a nested name specifier that refers into the target context, or |
| 447 | /// NULL if no qualification is needed. |
| 448 | static NestedNameSpecifier * |
| 449 | getRequiredQualification(ASTContext &Context, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 450 | const DeclContext *CurContext, |
| 451 | const DeclContext *TargetContext) { |
| 452 | SmallVector<const DeclContext *, 4> TargetParents; |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 453 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 454 | for (const DeclContext *CommonAncestor = TargetContext; |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 455 | CommonAncestor && !CommonAncestor->Encloses(CurContext); |
| 456 | CommonAncestor = CommonAncestor->getLookupParent()) { |
| 457 | if (CommonAncestor->isTransparentContext() || |
| 458 | CommonAncestor->isFunctionOrMethod()) |
| 459 | continue; |
| 460 | |
| 461 | TargetParents.push_back(CommonAncestor); |
| 462 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 463 | |
| 464 | NestedNameSpecifier *Result = nullptr; |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 465 | while (!TargetParents.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 466 | const DeclContext *Parent = TargetParents.pop_back_val(); |
| 467 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 468 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) { |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 469 | if (!Namespace->getIdentifier()) |
| 470 | continue; |
| 471 | |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 472 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 473 | } |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 474 | else if (const TagDecl *TD = dyn_cast<TagDecl>(Parent)) |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 475 | Result = NestedNameSpecifier::Create(Context, Result, |
| 476 | false, |
| 477 | Context.getTypeDeclType(TD).getTypePtr()); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 478 | } |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 479 | return Result; |
| 480 | } |
| 481 | |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 482 | /// Determine whether \p Id is a name reserved for the implementation (C99 |
| 483 | /// 7.1.3, C++ [lib.global.names]). |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 484 | static bool isReservedName(const IdentifierInfo *Id, |
| 485 | bool doubleUnderscoreOnly = false) { |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 486 | if (Id->getLength() < 2) |
| 487 | return false; |
| 488 | const char *Name = Id->getNameStart(); |
| 489 | return Name[0] == '_' && |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 490 | (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z' && |
| 491 | !doubleUnderscoreOnly)); |
| 492 | } |
| 493 | |
| 494 | // Some declarations have reserved names that we don't want to ever show. |
| 495 | // Filter out names reserved for the implementation if they come from a |
| 496 | // system header. |
| 497 | static bool shouldIgnoreDueToReservedName(const NamedDecl *ND, Sema &SemaRef) { |
| 498 | const IdentifierInfo *Id = ND->getIdentifier(); |
| 499 | if (!Id) |
| 500 | return false; |
| 501 | |
| 502 | // Ignore reserved names for compiler provided decls. |
| 503 | if (isReservedName(Id) && ND->getLocation().isInvalid()) |
| 504 | return true; |
| 505 | |
| 506 | // For system headers ignore only double-underscore names. |
| 507 | // This allows for system headers providing private symbols with a single |
| 508 | // underscore. |
| 509 | if (isReservedName(Id, /*doubleUnderscoreOnly=*/true) && |
| 510 | SemaRef.SourceMgr.isInSystemHeader( |
| 511 | SemaRef.SourceMgr.getSpellingLoc(ND->getLocation()))) |
| 512 | return true; |
| 513 | |
| 514 | return false; |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 517 | bool ResultBuilder::isInterestingDecl(const NamedDecl *ND, |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 518 | bool &AsNestedNameSpecifier) const { |
| 519 | AsNestedNameSpecifier = false; |
| 520 | |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 521 | auto *Named = ND; |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 522 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 523 | |
| 524 | // Skip unnamed entities. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 525 | if (!ND->getDeclName()) |
| 526 | return false; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 527 | |
| 528 | // Friend declarations and declarations introduced due to friends are never |
| 529 | // added as results. |
Richard Smith | 6eece29 | 2015-01-15 02:27:20 +0000 | [diff] [blame] | 530 | if (ND->getFriendObjectKind() == Decl::FOK_Undeclared) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 531 | return false; |
| 532 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 533 | // Class template (partial) specializations are never added as results. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 534 | if (isa<ClassTemplateSpecializationDecl>(ND) || |
| 535 | isa<ClassTemplatePartialSpecializationDecl>(ND)) |
| 536 | return false; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 537 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 538 | // Using declarations themselves are never added as results. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 539 | if (isa<UsingDecl>(ND)) |
| 540 | return false; |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 541 | |
| 542 | if (shouldIgnoreDueToReservedName(ND, SemaRef)) |
| 543 | return false; |
Douglas Gregor | 2927c0c | 2010-11-09 03:59:40 +0000 | [diff] [blame] | 544 | |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 545 | if (Filter == &ResultBuilder::IsNestedNameSpecifier || |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 546 | (isa<NamespaceDecl>(ND) && |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 547 | Filter != &ResultBuilder::IsNamespace && |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 548 | Filter != &ResultBuilder::IsNamespaceOrAlias && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 549 | Filter != nullptr)) |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 550 | AsNestedNameSpecifier = true; |
| 551 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 552 | // Filter out any unwanted results. |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 553 | if (Filter && !(this->*Filter)(Named)) { |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 554 | // Check whether it is interesting as a nested-name-specifier. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 555 | if (AllowNestedNameSpecifiers && SemaRef.getLangOpts().CPlusPlus && |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 556 | IsNestedNameSpecifier(ND) && |
| 557 | (Filter != &ResultBuilder::IsMember || |
| 558 | (isa<CXXRecordDecl>(ND) && |
| 559 | cast<CXXRecordDecl>(ND)->isInjectedClassName()))) { |
| 560 | AsNestedNameSpecifier = true; |
| 561 | return true; |
| 562 | } |
| 563 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 564 | return false; |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 565 | } |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 566 | // ... then it must be interesting! |
| 567 | return true; |
| 568 | } |
| 569 | |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 570 | bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 571 | const NamedDecl *Hiding) { |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 572 | // In C, there is no way to refer to a hidden name. |
| 573 | // FIXME: This isn't true; we can find a tag name hidden by an ordinary |
| 574 | // name if we introduce the tag type. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 575 | if (!SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 576 | return true; |
| 577 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 578 | const DeclContext *HiddenCtx = |
| 579 | R.Declaration->getDeclContext()->getRedeclContext(); |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 580 | |
| 581 | // There is no way to qualify a name declared in a function or method. |
| 582 | if (HiddenCtx->isFunctionOrMethod()) |
| 583 | return true; |
| 584 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 585 | if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext()) |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 586 | return true; |
| 587 | |
| 588 | // We can refer to the result with the appropriate qualification. Do it. |
| 589 | R.Hidden = true; |
| 590 | R.QualifierIsInformative = false; |
| 591 | |
| 592 | if (!R.Qualifier) |
| 593 | R.Qualifier = getRequiredQualification(SemaRef.Context, |
| 594 | CurContext, |
| 595 | R.Declaration->getDeclContext()); |
| 596 | return false; |
| 597 | } |
| 598 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 599 | /// \brief A simplified classification of types used to determine whether two |
| 600 | /// types are "similar enough" when adjusting priorities. |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 601 | SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 602 | switch (T->getTypeClass()) { |
| 603 | case Type::Builtin: |
| 604 | switch (cast<BuiltinType>(T)->getKind()) { |
| 605 | case BuiltinType::Void: |
| 606 | return STC_Void; |
| 607 | |
| 608 | case BuiltinType::NullPtr: |
| 609 | return STC_Pointer; |
| 610 | |
| 611 | case BuiltinType::Overload: |
| 612 | case BuiltinType::Dependent: |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 613 | return STC_Other; |
| 614 | |
| 615 | case BuiltinType::ObjCId: |
| 616 | case BuiltinType::ObjCClass: |
| 617 | case BuiltinType::ObjCSel: |
| 618 | return STC_ObjectiveC; |
| 619 | |
| 620 | default: |
| 621 | return STC_Arithmetic; |
| 622 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 623 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 624 | case Type::Complex: |
| 625 | return STC_Arithmetic; |
| 626 | |
| 627 | case Type::Pointer: |
| 628 | return STC_Pointer; |
| 629 | |
| 630 | case Type::BlockPointer: |
| 631 | return STC_Block; |
| 632 | |
| 633 | case Type::LValueReference: |
| 634 | case Type::RValueReference: |
| 635 | return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType()); |
| 636 | |
| 637 | case Type::ConstantArray: |
| 638 | case Type::IncompleteArray: |
| 639 | case Type::VariableArray: |
| 640 | case Type::DependentSizedArray: |
| 641 | return STC_Array; |
| 642 | |
| 643 | case Type::DependentSizedExtVector: |
| 644 | case Type::Vector: |
| 645 | case Type::ExtVector: |
| 646 | return STC_Arithmetic; |
| 647 | |
| 648 | case Type::FunctionProto: |
| 649 | case Type::FunctionNoProto: |
| 650 | return STC_Function; |
| 651 | |
| 652 | case Type::Record: |
| 653 | return STC_Record; |
| 654 | |
| 655 | case Type::Enum: |
| 656 | return STC_Arithmetic; |
| 657 | |
| 658 | case Type::ObjCObject: |
| 659 | case Type::ObjCInterface: |
| 660 | case Type::ObjCObjectPointer: |
| 661 | return STC_ObjectiveC; |
| 662 | |
| 663 | default: |
| 664 | return STC_Other; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | /// \brief Get the type that a given expression will have if this declaration |
| 669 | /// is used as an expression in its "typical" code-completion form. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 670 | QualType clang::getDeclUsageType(ASTContext &C, const NamedDecl *ND) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 671 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 672 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 673 | if (const TypeDecl *Type = dyn_cast<TypeDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 674 | return C.getTypeDeclType(Type); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 675 | if (const ObjCInterfaceDecl *Iface = dyn_cast<ObjCInterfaceDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 676 | return C.getObjCInterfaceType(Iface); |
| 677 | |
| 678 | QualType T; |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 679 | if (const FunctionDecl *Function = ND->getAsFunction()) |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 680 | T = Function->getCallResultType(); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 681 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 682 | T = Method->getSendResultType(); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 683 | else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 684 | T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext())); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 685 | else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 686 | T = Property->getType(); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 687 | else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 688 | T = Value->getType(); |
| 689 | else |
| 690 | return QualType(); |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 691 | |
| 692 | // Dig through references, function pointers, and block pointers to |
| 693 | // get down to the likely type of an expression when the entity is |
| 694 | // used. |
| 695 | do { |
| 696 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) { |
| 697 | T = Ref->getPointeeType(); |
| 698 | continue; |
| 699 | } |
| 700 | |
| 701 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 702 | if (Pointer->getPointeeType()->isFunctionType()) { |
| 703 | T = Pointer->getPointeeType(); |
| 704 | continue; |
| 705 | } |
| 706 | |
| 707 | break; |
| 708 | } |
| 709 | |
| 710 | if (const BlockPointerType *Block = T->getAs<BlockPointerType>()) { |
| 711 | T = Block->getPointeeType(); |
| 712 | continue; |
| 713 | } |
| 714 | |
| 715 | if (const FunctionType *Function = T->getAs<FunctionType>()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 716 | T = Function->getReturnType(); |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 717 | continue; |
| 718 | } |
| 719 | |
| 720 | break; |
| 721 | } while (true); |
| 722 | |
| 723 | return T; |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 726 | unsigned ResultBuilder::getBasePriority(const NamedDecl *ND) { |
| 727 | if (!ND) |
| 728 | return CCP_Unlikely; |
| 729 | |
| 730 | // Context-based decisions. |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 731 | const DeclContext *LexicalDC = ND->getLexicalDeclContext(); |
| 732 | if (LexicalDC->isFunctionOrMethod()) { |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 733 | // _cmd is relatively rare |
| 734 | if (const ImplicitParamDecl *ImplicitParam = |
| 735 | dyn_cast<ImplicitParamDecl>(ND)) |
| 736 | if (ImplicitParam->getIdentifier() && |
| 737 | ImplicitParam->getIdentifier()->isStr("_cmd")) |
| 738 | return CCP_ObjC_cmd; |
| 739 | |
| 740 | return CCP_LocalDeclaration; |
| 741 | } |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 742 | |
| 743 | const DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 744 | if (DC->isRecord() || isa<ObjCContainerDecl>(DC)) |
| 745 | return CCP_MemberDeclaration; |
| 746 | |
| 747 | // Content-based decisions. |
| 748 | if (isa<EnumConstantDecl>(ND)) |
| 749 | return CCP_Constant; |
| 750 | |
Douglas Gregor | 52e0de4 | 2013-01-31 05:03:46 +0000 | [diff] [blame] | 751 | // Use CCP_Type for type declarations unless we're in a statement, Objective-C |
| 752 | // message receiver, or parenthesized expression context. There, it's as |
| 753 | // likely that the user will want to write a type as other declarations. |
| 754 | if ((isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) && |
| 755 | !(CompletionContext.getKind() == CodeCompletionContext::CCC_Statement || |
| 756 | CompletionContext.getKind() |
| 757 | == CodeCompletionContext::CCC_ObjCMessageReceiver || |
| 758 | CompletionContext.getKind() |
| 759 | == CodeCompletionContext::CCC_ParenthesizedExpression)) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 760 | return CCP_Type; |
| 761 | |
| 762 | return CCP_Declaration; |
| 763 | } |
| 764 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 765 | void ResultBuilder::AdjustResultPriorityForDecl(Result &R) { |
| 766 | // If this is an Objective-C method declaration whose selector matches our |
| 767 | // preferred selector, give it a priority boost. |
| 768 | if (!PreferredSelector.isNull()) |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 769 | if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration)) |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 770 | if (PreferredSelector == Method->getSelector()) |
| 771 | R.Priority += CCD_SelectorMatch; |
Douglas Gregor | 5fb901d | 2010-09-20 23:11:55 +0000 | [diff] [blame] | 772 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 773 | // If we have a preferred type, adjust the priority for results with exactly- |
| 774 | // matching or nearly-matching types. |
| 775 | if (!PreferredType.isNull()) { |
| 776 | QualType T = getDeclUsageType(SemaRef.Context, R.Declaration); |
| 777 | if (!T.isNull()) { |
| 778 | CanQualType TC = SemaRef.Context.getCanonicalType(T); |
| 779 | // Check for exactly-matching types (modulo qualifiers). |
| 780 | if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) |
| 781 | R.Priority /= CCF_ExactTypeMatch; |
| 782 | // Check for nearly-matching types, based on classification of each. |
| 783 | else if ((getSimplifiedTypeClass(PreferredType) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 784 | == getSimplifiedTypeClass(TC)) && |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 785 | !(PreferredType->isEnumeralType() && TC->isEnumeralType())) |
| 786 | R.Priority /= CCF_SimilarTypeMatch; |
| 787 | } |
| 788 | } |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 791 | void ResultBuilder::MaybeAddConstructorResults(Result R) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 792 | if (!SemaRef.getLangOpts().CPlusPlus || !R.Declaration || |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 793 | !CompletionContext.wantConstructorResults()) |
| 794 | return; |
| 795 | |
| 796 | ASTContext &Context = SemaRef.Context; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 797 | const NamedDecl *D = R.Declaration; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 798 | const CXXRecordDecl *Record = nullptr; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 799 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 800 | Record = ClassTemplate->getTemplatedDecl(); |
| 801 | else if ((Record = dyn_cast<CXXRecordDecl>(D))) { |
| 802 | // Skip specializations and partial specializations. |
| 803 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 804 | return; |
| 805 | } else { |
| 806 | // There are no constructors here. |
| 807 | return; |
| 808 | } |
| 809 | |
| 810 | Record = Record->getDefinition(); |
| 811 | if (!Record) |
| 812 | return; |
| 813 | |
| 814 | |
| 815 | QualType RecordTy = Context.getTypeDeclType(Record); |
| 816 | DeclarationName ConstructorName |
| 817 | = Context.DeclarationNames.getCXXConstructorName( |
| 818 | Context.getCanonicalType(RecordTy)); |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 819 | DeclContext::lookup_result Ctors = Record->lookup(ConstructorName); |
| 820 | for (DeclContext::lookup_iterator I = Ctors.begin(), |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 821 | E = Ctors.end(); |
| 822 | I != E; ++I) { |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 823 | R.Declaration = *I; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 824 | R.CursorKind = getCursorKindForDecl(R.Declaration); |
| 825 | Results.push_back(R); |
| 826 | } |
| 827 | } |
| 828 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 829 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { |
| 830 | assert(!ShadowMaps.empty() && "Must enter into a results scope"); |
| 831 | |
| 832 | if (R.Kind != Result::RK_Declaration) { |
| 833 | // For non-declaration results, just add the result. |
| 834 | Results.push_back(R); |
| 835 | return; |
| 836 | } |
| 837 | |
| 838 | // Look through using declarations. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 839 | if (const UsingShadowDecl *Using = |
| 840 | dyn_cast<UsingShadowDecl>(R.Declaration)) { |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 841 | MaybeAddResult(Result(Using->getTargetDecl(), |
| 842 | getBasePriority(Using->getTargetDecl()), |
| 843 | R.Qualifier), |
| 844 | CurContext); |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 845 | return; |
| 846 | } |
| 847 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 848 | const Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 849 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 850 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 851 | bool AsNestedNameSpecifier = false; |
| 852 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 853 | return; |
| 854 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 855 | // C++ constructors are never found by name lookup. |
| 856 | if (isa<CXXConstructorDecl>(R.Declaration)) |
| 857 | return; |
| 858 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 859 | ShadowMap &SMap = ShadowMaps.back(); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 860 | ShadowMapEntry::iterator I, IEnd; |
| 861 | ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName()); |
| 862 | if (NamePos != SMap.end()) { |
| 863 | I = NamePos->second.begin(); |
| 864 | IEnd = NamePos->second.end(); |
| 865 | } |
| 866 | |
| 867 | for (; I != IEnd; ++I) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 868 | const NamedDecl *ND = I->first; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 869 | unsigned Index = I->second; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 870 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 871 | // This is a redeclaration. Always pick the newer declaration. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 872 | Results[Index].Declaration = R.Declaration; |
| 873 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 874 | // We're done. |
| 875 | return; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | // This is a new declaration in this scope. However, check whether this |
| 880 | // declaration name is hidden by a similarly-named declaration in an outer |
| 881 | // scope. |
| 882 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 883 | --SMEnd; |
| 884 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 885 | ShadowMapEntry::iterator I, IEnd; |
| 886 | ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName()); |
| 887 | if (NamePos != SM->end()) { |
| 888 | I = NamePos->second.begin(); |
| 889 | IEnd = NamePos->second.end(); |
| 890 | } |
| 891 | for (; I != IEnd; ++I) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 892 | // A tag declaration does not hide a non-tag declaration. |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 893 | if (I->first->hasTagIdentifierNamespace() && |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 894 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 895 | Decl::IDNS_LocalExtern | Decl::IDNS_ObjCProtocol))) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 896 | continue; |
| 897 | |
| 898 | // Protocols are in distinct namespaces from everything else. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 899 | if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 900 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 901 | I->first->getIdentifierNamespace() != IDNS) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 902 | continue; |
| 903 | |
| 904 | // The newly-added result is hidden by an entry in the shadow map. |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 905 | if (CheckHiddenResult(R, CurContext, I->first)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 906 | return; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 907 | |
| 908 | break; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | // Make sure that any given declaration only shows up in the result set once. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 913 | if (!AllDeclsFound.insert(CanonDecl).second) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 914 | return; |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 915 | |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 916 | // If the filter is for nested-name-specifiers, then this result starts a |
| 917 | // nested-name-specifier. |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 918 | if (AsNestedNameSpecifier) { |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 919 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 920 | R.Priority = CCP_NestedNameSpecifier; |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 921 | } else |
| 922 | AdjustResultPriorityForDecl(R); |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 923 | |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 924 | // If this result is supposed to have an informative qualifier, add one. |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 925 | if (R.QualifierIsInformative && !R.Qualifier && |
| 926 | !R.StartsNestedNameSpecifier) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 927 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 928 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 929 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 930 | Namespace); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 931 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 932 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 933 | false, SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 934 | else |
| 935 | R.QualifierIsInformative = false; |
| 936 | } |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 937 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 938 | // Insert this result into the set of results and into the current shadow |
| 939 | // map. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 940 | SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 941 | Results.push_back(R); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 942 | |
| 943 | if (!AsNestedNameSpecifier) |
| 944 | MaybeAddConstructorResults(R); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 947 | void ResultBuilder::AddResult(Result R, DeclContext *CurContext, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 948 | NamedDecl *Hiding, bool InBaseClass = false) { |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 949 | if (R.Kind != Result::RK_Declaration) { |
| 950 | // For non-declaration results, just add the result. |
| 951 | Results.push_back(R); |
| 952 | return; |
| 953 | } |
| 954 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 955 | // Look through using declarations. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 956 | if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 957 | AddResult(Result(Using->getTargetDecl(), |
| 958 | getBasePriority(Using->getTargetDecl()), |
| 959 | R.Qualifier), |
| 960 | CurContext, Hiding); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 961 | return; |
| 962 | } |
| 963 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 964 | bool AsNestedNameSpecifier = false; |
| 965 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 966 | return; |
| 967 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 968 | // C++ constructors are never found by name lookup. |
| 969 | if (isa<CXXConstructorDecl>(R.Declaration)) |
| 970 | return; |
| 971 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 972 | if (Hiding && CheckHiddenResult(R, CurContext, Hiding)) |
| 973 | return; |
Nick Lewycky | c392148 | 2012-04-03 21:44:08 +0000 | [diff] [blame] | 974 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 975 | // Make sure that any given declaration only shows up in the result set once. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 976 | if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()).second) |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 977 | return; |
| 978 | |
| 979 | // If the filter is for nested-name-specifiers, then this result starts a |
| 980 | // nested-name-specifier. |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 981 | if (AsNestedNameSpecifier) { |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 982 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 983 | R.Priority = CCP_NestedNameSpecifier; |
| 984 | } |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 985 | else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass && |
| 986 | isa<CXXRecordDecl>(R.Declaration->getDeclContext() |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 987 | ->getRedeclContext())) |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 988 | R.QualifierIsInformative = true; |
| 989 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 990 | // If this result is supposed to have an informative qualifier, add one. |
| 991 | if (R.QualifierIsInformative && !R.Qualifier && |
| 992 | !R.StartsNestedNameSpecifier) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 993 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 994 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 995 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 996 | Namespace); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 997 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 998 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, false, |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 999 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1000 | else |
| 1001 | R.QualifierIsInformative = false; |
| 1002 | } |
| 1003 | |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1004 | // Adjust the priority if this result comes from a base class. |
| 1005 | if (InBaseClass) |
| 1006 | R.Priority += CCD_InBaseClass; |
| 1007 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 1008 | AdjustResultPriorityForDecl(R); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1009 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1010 | if (HasObjectTypeQualifiers) |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1011 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(R.Declaration)) |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1012 | if (Method->isInstance()) { |
| 1013 | Qualifiers MethodQuals |
| 1014 | = Qualifiers::fromCVRMask(Method->getTypeQualifiers()); |
| 1015 | if (ObjectTypeQualifiers == MethodQuals) |
| 1016 | R.Priority += CCD_ObjectQualifierMatch; |
| 1017 | else if (ObjectTypeQualifiers - MethodQuals) { |
| 1018 | // The method cannot be invoked, because doing so would drop |
| 1019 | // qualifiers. |
| 1020 | return; |
| 1021 | } |
| 1022 | } |
| 1023 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1024 | // Insert this result into the set of results. |
| 1025 | Results.push_back(R); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1026 | |
| 1027 | if (!AsNestedNameSpecifier) |
| 1028 | MaybeAddConstructorResults(R); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1031 | void ResultBuilder::AddResult(Result R) { |
| 1032 | assert(R.Kind != Result::RK_Declaration && |
| 1033 | "Declaration results need more context"); |
| 1034 | Results.push_back(R); |
| 1035 | } |
| 1036 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1037 | /// \brief Enter into a new scope. |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1038 | void ResultBuilder::EnterNewScope() { ShadowMaps.emplace_back(); } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1039 | |
| 1040 | /// \brief Exit from the current scope. |
| 1041 | void ResultBuilder::ExitScope() { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1042 | for (ShadowMap::iterator E = ShadowMaps.back().begin(), |
| 1043 | EEnd = ShadowMaps.back().end(); |
| 1044 | E != EEnd; |
| 1045 | ++E) |
| 1046 | E->second.Destroy(); |
| 1047 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1048 | ShadowMaps.pop_back(); |
| 1049 | } |
| 1050 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1051 | /// \brief Determines whether this given declaration will be found by |
| 1052 | /// ordinary name lookup. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1053 | bool ResultBuilder::IsOrdinaryName(const NamedDecl *ND) const { |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1054 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 1055 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1056 | // If name lookup finds a local extern declaration, then we are in a |
| 1057 | // context where it behaves like an ordinary name. |
| 1058 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1059 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 9858ed5 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1060 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1061 | else if (SemaRef.getLangOpts().ObjC1) { |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1062 | if (isa<ObjCIvarDecl>(ND)) |
| 1063 | return true; |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1066 | return ND->getIdentifierNamespace() & IDNS; |
| 1067 | } |
| 1068 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1069 | /// \brief Determines whether this given declaration will be found by |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1070 | /// ordinary name lookup but is not a type name. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1071 | bool ResultBuilder::IsOrdinaryNonTypeName(const NamedDecl *ND) const { |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1072 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 1073 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) |
| 1074 | return false; |
| 1075 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1076 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1077 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 9858ed5 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1078 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1079 | else if (SemaRef.getLangOpts().ObjC1) { |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1080 | if (isa<ObjCIvarDecl>(ND)) |
| 1081 | return true; |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1084 | return ND->getIdentifierNamespace() & IDNS; |
| 1085 | } |
| 1086 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1087 | bool ResultBuilder::IsIntegralConstantValue(const NamedDecl *ND) const { |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1088 | if (!IsOrdinaryNonTypeName(ND)) |
| 1089 | return 0; |
| 1090 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1091 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl())) |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1092 | if (VD->getType()->isIntegralOrEnumerationType()) |
| 1093 | return true; |
| 1094 | |
| 1095 | return false; |
| 1096 | } |
| 1097 | |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1098 | /// \brief Determines whether this given declaration will be found by |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1099 | /// ordinary name lookup. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1100 | bool ResultBuilder::IsOrdinaryNonValueName(const NamedDecl *ND) const { |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1101 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 1102 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1103 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1104 | if (SemaRef.getLangOpts().CPlusPlus) |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1105 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1106 | |
| 1107 | return (ND->getIdentifierNamespace() & IDNS) && |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1108 | !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) && |
| 1109 | !isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1112 | /// \brief Determines whether the given declaration is suitable as the |
| 1113 | /// start of a C++ nested-name-specifier, e.g., a class or namespace. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1114 | bool ResultBuilder::IsNestedNameSpecifier(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1115 | // Allow us to find class templates, too. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1116 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1117 | ND = ClassTemplate->getTemplatedDecl(); |
| 1118 | |
| 1119 | return SemaRef.isAcceptableNestedNameSpecifier(ND); |
| 1120 | } |
| 1121 | |
| 1122 | /// \brief Determines whether the given declaration is an enumeration. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1123 | bool ResultBuilder::IsEnum(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1124 | return isa<EnumDecl>(ND); |
| 1125 | } |
| 1126 | |
| 1127 | /// \brief Determines whether the given declaration is a class or struct. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1128 | bool ResultBuilder::IsClassOrStruct(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1129 | // Allow us to find class templates, too. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1130 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1131 | ND = ClassTemplate->getTemplatedDecl(); |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1132 | |
| 1133 | // For purposes of this check, interfaces match too. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1134 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1135 | return RD->getTagKind() == TTK_Class || |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1136 | RD->getTagKind() == TTK_Struct || |
| 1137 | RD->getTagKind() == TTK_Interface; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1138 | |
| 1139 | return false; |
| 1140 | } |
| 1141 | |
| 1142 | /// \brief Determines whether the given declaration is a union. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1143 | bool ResultBuilder::IsUnion(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1144 | // Allow us to find class templates, too. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1145 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1146 | ND = ClassTemplate->getTemplatedDecl(); |
| 1147 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1148 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1149 | return RD->getTagKind() == TTK_Union; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1150 | |
| 1151 | return false; |
| 1152 | } |
| 1153 | |
| 1154 | /// \brief Determines whether the given declaration is a namespace. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1155 | bool ResultBuilder::IsNamespace(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1156 | return isa<NamespaceDecl>(ND); |
| 1157 | } |
| 1158 | |
| 1159 | /// \brief Determines whether the given declaration is a namespace or |
| 1160 | /// namespace alias. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1161 | bool ResultBuilder::IsNamespaceOrAlias(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1162 | return isa<NamespaceDecl>(ND->getUnderlyingDecl()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1165 | /// \brief Determines whether the given declaration is a type. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1166 | bool ResultBuilder::IsType(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1167 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1168 | return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1171 | /// \brief Determines which members of a class should be visible via |
| 1172 | /// "." or "->". Only value declarations, nested name specifiers, and |
| 1173 | /// using declarations thereof should show up. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1174 | bool ResultBuilder::IsMember(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1175 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 7078839 | 2009-12-11 18:14:22 +0000 | [diff] [blame] | 1176 | return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) || |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1177 | isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1180 | static bool isObjCReceiverType(ASTContext &C, QualType T) { |
| 1181 | T = C.getCanonicalType(T); |
| 1182 | switch (T->getTypeClass()) { |
| 1183 | case Type::ObjCObject: |
| 1184 | case Type::ObjCInterface: |
| 1185 | case Type::ObjCObjectPointer: |
| 1186 | return true; |
| 1187 | |
| 1188 | case Type::Builtin: |
| 1189 | switch (cast<BuiltinType>(T)->getKind()) { |
| 1190 | case BuiltinType::ObjCId: |
| 1191 | case BuiltinType::ObjCClass: |
| 1192 | case BuiltinType::ObjCSel: |
| 1193 | return true; |
| 1194 | |
| 1195 | default: |
| 1196 | break; |
| 1197 | } |
| 1198 | return false; |
| 1199 | |
| 1200 | default: |
| 1201 | break; |
| 1202 | } |
| 1203 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1204 | if (!C.getLangOpts().CPlusPlus) |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1205 | return false; |
| 1206 | |
| 1207 | // FIXME: We could perform more analysis here to determine whether a |
| 1208 | // particular class type has any conversions to Objective-C types. For now, |
| 1209 | // just accept all class types. |
| 1210 | return T->isDependentType() || T->isRecordType(); |
| 1211 | } |
| 1212 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1213 | bool ResultBuilder::IsObjCMessageReceiver(const NamedDecl *ND) const { |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1214 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1215 | if (T.isNull()) |
| 1216 | return false; |
| 1217 | |
| 1218 | T = SemaRef.Context.getBaseElementType(T); |
| 1219 | return isObjCReceiverType(SemaRef.Context, T); |
| 1220 | } |
| 1221 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1222 | bool ResultBuilder::IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const { |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1223 | if (IsObjCMessageReceiver(ND)) |
| 1224 | return true; |
| 1225 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1226 | const VarDecl *Var = dyn_cast<VarDecl>(ND); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1227 | if (!Var) |
| 1228 | return false; |
| 1229 | |
| 1230 | return Var->hasLocalStorage() && !Var->hasAttr<BlocksAttr>(); |
| 1231 | } |
| 1232 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1233 | bool ResultBuilder::IsObjCCollection(const NamedDecl *ND) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1234 | if ((SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryName(ND)) || |
| 1235 | (!SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryNonTypeName(ND))) |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1236 | return false; |
| 1237 | |
| 1238 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1239 | if (T.isNull()) |
| 1240 | return false; |
| 1241 | |
| 1242 | T = SemaRef.Context.getBaseElementType(T); |
| 1243 | return T->isObjCObjectType() || T->isObjCObjectPointerType() || |
| 1244 | T->isObjCIdType() || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1245 | (SemaRef.getLangOpts().CPlusPlus && T->isRecordType()); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1246 | } |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1247 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1248 | bool ResultBuilder::IsImpossibleToSatisfy(const NamedDecl *ND) const { |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1249 | return false; |
| 1250 | } |
| 1251 | |
James Dennett | f124387 | 2012-06-17 05:33:25 +0000 | [diff] [blame] | 1252 | /// \brief Determines whether the given declaration is an Objective-C |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1253 | /// instance variable. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1254 | bool ResultBuilder::IsObjCIvar(const NamedDecl *ND) const { |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1255 | return isa<ObjCIvarDecl>(ND); |
| 1256 | } |
| 1257 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1258 | namespace { |
| 1259 | /// \brief Visible declaration consumer that adds a code-completion result |
| 1260 | /// for each visible declaration. |
| 1261 | class CodeCompletionDeclConsumer : public VisibleDeclConsumer { |
| 1262 | ResultBuilder &Results; |
| 1263 | DeclContext *CurContext; |
| 1264 | |
| 1265 | public: |
| 1266 | CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext) |
| 1267 | : Results(Results), CurContext(CurContext) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1268 | |
| 1269 | void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx, |
| 1270 | bool InBaseClass) override { |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1271 | bool Accessible = true; |
Douglas Gregor | 03ba188 | 2011-11-03 16:51:37 +0000 | [diff] [blame] | 1272 | if (Ctx) |
| 1273 | Accessible = Results.getSema().IsSimplyAccessible(ND, Ctx); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1274 | |
| 1275 | ResultBuilder::Result Result(ND, Results.getBasePriority(ND), nullptr, |
| 1276 | false, Accessible); |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1277 | Results.AddResult(Result, CurContext, Hiding, InBaseClass); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1278 | } |
| 1279 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1280 | } |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1281 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1282 | /// \brief Add type specifiers for the current language as keyword results. |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1283 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1284 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1285 | typedef CodeCompletionResult Result; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1286 | Results.AddResult(Result("short", CCP_Type)); |
| 1287 | Results.AddResult(Result("long", CCP_Type)); |
| 1288 | Results.AddResult(Result("signed", CCP_Type)); |
| 1289 | Results.AddResult(Result("unsigned", CCP_Type)); |
| 1290 | Results.AddResult(Result("void", CCP_Type)); |
| 1291 | Results.AddResult(Result("char", CCP_Type)); |
| 1292 | Results.AddResult(Result("int", CCP_Type)); |
| 1293 | Results.AddResult(Result("float", CCP_Type)); |
| 1294 | Results.AddResult(Result("double", CCP_Type)); |
| 1295 | Results.AddResult(Result("enum", CCP_Type)); |
| 1296 | Results.AddResult(Result("struct", CCP_Type)); |
| 1297 | Results.AddResult(Result("union", CCP_Type)); |
| 1298 | Results.AddResult(Result("const", CCP_Type)); |
| 1299 | Results.AddResult(Result("volatile", CCP_Type)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1300 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1301 | if (LangOpts.C99) { |
| 1302 | // C99-specific |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1303 | Results.AddResult(Result("_Complex", CCP_Type)); |
| 1304 | Results.AddResult(Result("_Imaginary", CCP_Type)); |
| 1305 | Results.AddResult(Result("_Bool", CCP_Type)); |
| 1306 | Results.AddResult(Result("restrict", CCP_Type)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1309 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 1310 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1311 | if (LangOpts.CPlusPlus) { |
| 1312 | // C++-specific |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 1313 | Results.AddResult(Result("bool", CCP_Type + |
| 1314 | (LangOpts.ObjC1? CCD_bool_in_ObjC : 0))); |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1315 | Results.AddResult(Result("class", CCP_Type)); |
| 1316 | Results.AddResult(Result("wchar_t", CCP_Type)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1317 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1318 | // typename qualified-id |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1319 | Builder.AddTypedTextChunk("typename"); |
| 1320 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1321 | Builder.AddPlaceholderChunk("qualifier"); |
| 1322 | Builder.AddTextChunk("::"); |
| 1323 | Builder.AddPlaceholderChunk("name"); |
| 1324 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1325 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1326 | if (LangOpts.CPlusPlus11) { |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1327 | Results.AddResult(Result("auto", CCP_Type)); |
| 1328 | Results.AddResult(Result("char16_t", CCP_Type)); |
| 1329 | Results.AddResult(Result("char32_t", CCP_Type)); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1330 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1331 | Builder.AddTypedTextChunk("decltype"); |
| 1332 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1333 | Builder.AddPlaceholderChunk("expression"); |
| 1334 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1335 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | // GNU extensions |
| 1340 | if (LangOpts.GNUMode) { |
| 1341 | // FIXME: Enable when we actually support decimal floating point. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1342 | // Results.AddResult(Result("_Decimal32")); |
| 1343 | // Results.AddResult(Result("_Decimal64")); |
| 1344 | // Results.AddResult(Result("_Decimal128")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1345 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1346 | Builder.AddTypedTextChunk("typeof"); |
| 1347 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1348 | Builder.AddPlaceholderChunk("expression"); |
| 1349 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1350 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1351 | Builder.AddTypedTextChunk("typeof"); |
| 1352 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1353 | Builder.AddPlaceholderChunk("type"); |
| 1354 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1355 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1356 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 1357 | |
| 1358 | // Nullability |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 1359 | Results.AddResult(Result("_Nonnull", CCP_Type)); |
| 1360 | Results.AddResult(Result("_Null_unspecified", CCP_Type)); |
| 1361 | Results.AddResult(Result("_Nullable", CCP_Type)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1364 | static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1365 | const LangOptions &LangOpts, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1366 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1367 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1368 | // Note: we don't suggest either "auto" or "register", because both |
| 1369 | // are pointless as storage specifiers. Elsewhere, we suggest "auto" |
| 1370 | // in C++0x as a type specifier. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1371 | Results.AddResult(Result("extern")); |
| 1372 | Results.AddResult(Result("static")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1375 | static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1376 | const LangOptions &LangOpts, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1377 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1378 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1379 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1380 | case Sema::PCC_Class: |
| 1381 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1382 | if (LangOpts.CPlusPlus) { |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1383 | Results.AddResult(Result("explicit")); |
| 1384 | Results.AddResult(Result("friend")); |
| 1385 | Results.AddResult(Result("mutable")); |
| 1386 | Results.AddResult(Result("virtual")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1387 | } |
| 1388 | // Fall through |
| 1389 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1390 | case Sema::PCC_ObjCInterface: |
| 1391 | case Sema::PCC_ObjCImplementation: |
| 1392 | case Sema::PCC_Namespace: |
| 1393 | case Sema::PCC_Template: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1394 | if (LangOpts.CPlusPlus || LangOpts.C99) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1395 | Results.AddResult(Result("inline")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1396 | break; |
| 1397 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1398 | case Sema::PCC_ObjCInstanceVariableList: |
| 1399 | case Sema::PCC_Expression: |
| 1400 | case Sema::PCC_Statement: |
| 1401 | case Sema::PCC_ForInit: |
| 1402 | case Sema::PCC_Condition: |
| 1403 | case Sema::PCC_RecoveryInFunction: |
| 1404 | case Sema::PCC_Type: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1405 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1406 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1407 | break; |
| 1408 | } |
| 1409 | } |
| 1410 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1411 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); |
| 1412 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); |
| 1413 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1414 | ResultBuilder &Results, |
| 1415 | bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1416 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1417 | ResultBuilder &Results, |
| 1418 | bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1419 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1420 | ResultBuilder &Results, |
| 1421 | bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1422 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1423 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1424 | static void AddTypedefResult(ResultBuilder &Results) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1425 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 1426 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1427 | Builder.AddTypedTextChunk("typedef"); |
| 1428 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1429 | Builder.AddPlaceholderChunk("type"); |
| 1430 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1431 | Builder.AddPlaceholderChunk("name"); |
| 1432 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1435 | static bool WantTypesInContext(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1436 | const LangOptions &LangOpts) { |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1437 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1438 | case Sema::PCC_Namespace: |
| 1439 | case Sema::PCC_Class: |
| 1440 | case Sema::PCC_ObjCInstanceVariableList: |
| 1441 | case Sema::PCC_Template: |
| 1442 | case Sema::PCC_MemberTemplate: |
| 1443 | case Sema::PCC_Statement: |
| 1444 | case Sema::PCC_RecoveryInFunction: |
| 1445 | case Sema::PCC_Type: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1446 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1447 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1448 | return true; |
| 1449 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1450 | case Sema::PCC_Expression: |
| 1451 | case Sema::PCC_Condition: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1452 | return LangOpts.CPlusPlus; |
| 1453 | |
| 1454 | case Sema::PCC_ObjCInterface: |
| 1455 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1456 | return false; |
| 1457 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1458 | case Sema::PCC_ForInit: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1459 | return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99; |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1460 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1461 | |
| 1462 | llvm_unreachable("Invalid ParserCompletionContext!"); |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 1465 | static PrintingPolicy getCompletionPrintingPolicy(const ASTContext &Context, |
| 1466 | const Preprocessor &PP) { |
| 1467 | PrintingPolicy Policy = Sema::getPrintingPolicy(Context, PP); |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1468 | Policy.AnonymousTagLocations = false; |
| 1469 | Policy.SuppressStrongLifetime = true; |
Douglas Gregor | 2e10cf9 | 2011-11-03 00:16:13 +0000 | [diff] [blame] | 1470 | Policy.SuppressUnwrittenScope = true; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1471 | return Policy; |
| 1472 | } |
| 1473 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 1474 | /// \brief Retrieve a printing policy suitable for code completion. |
| 1475 | static PrintingPolicy getCompletionPrintingPolicy(Sema &S) { |
| 1476 | return getCompletionPrintingPolicy(S.Context, S.PP); |
| 1477 | } |
| 1478 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1479 | /// \brief Retrieve the string representation of the given type as a string |
| 1480 | /// that has the appropriate lifetime for code completion. |
| 1481 | /// |
| 1482 | /// This routine provides a fast path where we provide constant strings for |
| 1483 | /// common type names. |
| 1484 | static const char *GetCompletionTypeString(QualType T, |
| 1485 | ASTContext &Context, |
| 1486 | const PrintingPolicy &Policy, |
| 1487 | CodeCompletionAllocator &Allocator) { |
| 1488 | if (!T.getLocalQualifiers()) { |
| 1489 | // Built-in type names are constant strings. |
| 1490 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) |
Argyrios Kyrtzidis | bbff3da | 2012-05-05 04:20:28 +0000 | [diff] [blame] | 1491 | return BT->getNameAsCString(Policy); |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1492 | |
| 1493 | // Anonymous tag types are constant strings. |
| 1494 | if (const TagType *TagT = dyn_cast<TagType>(T)) |
| 1495 | if (TagDecl *Tag = TagT->getDecl()) |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 1496 | if (!Tag->hasNameForLinkage()) { |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1497 | switch (Tag->getTagKind()) { |
| 1498 | case TTK_Struct: return "struct <anonymous>"; |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1499 | case TTK_Interface: return "__interface <anonymous>"; |
| 1500 | case TTK_Class: return "class <anonymous>"; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1501 | case TTK_Union: return "union <anonymous>"; |
| 1502 | case TTK_Enum: return "enum <anonymous>"; |
| 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | // Slow path: format the type as a string. |
| 1508 | std::string Result; |
| 1509 | T.getAsStringInternal(Result, Policy); |
| 1510 | return Allocator.CopyString(Result); |
| 1511 | } |
| 1512 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1513 | /// \brief Add a completion for "this", if we're in a member function. |
| 1514 | static void addThisCompletion(Sema &S, ResultBuilder &Results) { |
| 1515 | QualType ThisTy = S.getCurrentThisType(); |
| 1516 | if (ThisTy.isNull()) |
| 1517 | return; |
| 1518 | |
| 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 | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1521 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
| 1522 | Builder.AddResultTypeChunk(GetCompletionTypeString(ThisTy, |
| 1523 | S.Context, |
| 1524 | Policy, |
| 1525 | Allocator)); |
| 1526 | Builder.AddTypedTextChunk("this"); |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1527 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1530 | /// \brief Add language constructs that show up for "ordinary" names. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1531 | static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1532 | Scope *S, |
| 1533 | Sema &SemaRef, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1534 | ResultBuilder &Results) { |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1535 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1536 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1537 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1538 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1539 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1540 | case Sema::PCC_Namespace: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1541 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1542 | if (Results.includeCodePatterns()) { |
| 1543 | // namespace <identifier> { declarations } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1544 | Builder.AddTypedTextChunk("namespace"); |
| 1545 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1546 | Builder.AddPlaceholderChunk("identifier"); |
| 1547 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1548 | Builder.AddPlaceholderChunk("declarations"); |
| 1549 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1550 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1551 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1554 | // namespace identifier = identifier ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1555 | Builder.AddTypedTextChunk("namespace"); |
| 1556 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1557 | Builder.AddPlaceholderChunk("name"); |
| 1558 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
| 1559 | Builder.AddPlaceholderChunk("namespace"); |
| 1560 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1561 | |
| 1562 | // Using directives |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1563 | Builder.AddTypedTextChunk("using"); |
| 1564 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1565 | Builder.AddTextChunk("namespace"); |
| 1566 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1567 | Builder.AddPlaceholderChunk("identifier"); |
| 1568 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1569 | |
| 1570 | // asm(string-literal) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1571 | Builder.AddTypedTextChunk("asm"); |
| 1572 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1573 | Builder.AddPlaceholderChunk("string-literal"); |
| 1574 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1575 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1576 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1577 | if (Results.includeCodePatterns()) { |
| 1578 | // Explicit template instantiation |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1579 | Builder.AddTypedTextChunk("template"); |
| 1580 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1581 | Builder.AddPlaceholderChunk("declaration"); |
| 1582 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1583 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1584 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1585 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1586 | if (SemaRef.getLangOpts().ObjC1) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1587 | AddObjCTopLevelResults(Results, true); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1588 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1589 | AddTypedefResult(Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1590 | // Fall through |
| 1591 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1592 | case Sema::PCC_Class: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1593 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1594 | // Using declaration |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1595 | Builder.AddTypedTextChunk("using"); |
| 1596 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1597 | Builder.AddPlaceholderChunk("qualifier"); |
| 1598 | Builder.AddTextChunk("::"); |
| 1599 | Builder.AddPlaceholderChunk("name"); |
| 1600 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1601 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1602 | // using typename qualifier::name (only in a dependent context) |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1603 | if (SemaRef.CurContext->isDependentContext()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1604 | Builder.AddTypedTextChunk("using"); |
| 1605 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1606 | Builder.AddTextChunk("typename"); |
| 1607 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1608 | Builder.AddPlaceholderChunk("qualifier"); |
| 1609 | Builder.AddTextChunk("::"); |
| 1610 | Builder.AddPlaceholderChunk("name"); |
| 1611 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1614 | if (CCC == Sema::PCC_Class) { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1615 | AddTypedefResult(Results); |
| 1616 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1617 | // public: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1618 | Builder.AddTypedTextChunk("public"); |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1619 | if (Results.includeCodePatterns()) |
| 1620 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1621 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1622 | |
| 1623 | // protected: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1624 | Builder.AddTypedTextChunk("protected"); |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1625 | if (Results.includeCodePatterns()) |
| 1626 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1627 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1628 | |
| 1629 | // private: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1630 | Builder.AddTypedTextChunk("private"); |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1631 | if (Results.includeCodePatterns()) |
| 1632 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1633 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1634 | } |
| 1635 | } |
| 1636 | // Fall through |
| 1637 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1638 | case Sema::PCC_Template: |
| 1639 | case Sema::PCC_MemberTemplate: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1640 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns()) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1641 | // template < parameters > |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1642 | Builder.AddTypedTextChunk("template"); |
| 1643 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1644 | Builder.AddPlaceholderChunk("parameters"); |
| 1645 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1646 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1649 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1650 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1651 | break; |
| 1652 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1653 | case Sema::PCC_ObjCInterface: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1654 | AddObjCInterfaceResults(SemaRef.getLangOpts(), Results, true); |
| 1655 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1656 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1657 | break; |
| 1658 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1659 | case Sema::PCC_ObjCImplementation: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1660 | AddObjCImplementationResults(SemaRef.getLangOpts(), Results, true); |
| 1661 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1662 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1663 | break; |
| 1664 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1665 | case Sema::PCC_ObjCInstanceVariableList: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1666 | AddObjCVisibilityResults(SemaRef.getLangOpts(), Results, true); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1667 | break; |
| 1668 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1669 | case Sema::PCC_RecoveryInFunction: |
| 1670 | case Sema::PCC_Statement: { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1671 | AddTypedefResult(Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1672 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1673 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns() && |
| 1674 | SemaRef.getLangOpts().CXXExceptions) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1675 | Builder.AddTypedTextChunk("try"); |
| 1676 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1677 | Builder.AddPlaceholderChunk("statements"); |
| 1678 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1679 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1680 | Builder.AddTextChunk("catch"); |
| 1681 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1682 | Builder.AddPlaceholderChunk("declaration"); |
| 1683 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1684 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1685 | Builder.AddPlaceholderChunk("statements"); |
| 1686 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1687 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1688 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1689 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1690 | if (SemaRef.getLangOpts().ObjC1) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1691 | AddObjCStatementResults(Results, true); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1692 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1693 | if (Results.includeCodePatterns()) { |
| 1694 | // if (condition) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1695 | Builder.AddTypedTextChunk("if"); |
| 1696 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1697 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1698 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1699 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1700 | Builder.AddPlaceholderChunk("expression"); |
| 1701 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1702 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1703 | Builder.AddPlaceholderChunk("statements"); |
| 1704 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1705 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1706 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1707 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1708 | // switch (condition) { } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1709 | Builder.AddTypedTextChunk("switch"); |
| 1710 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1711 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1712 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1713 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1714 | Builder.AddPlaceholderChunk("expression"); |
| 1715 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1716 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1717 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1718 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1719 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1722 | // Switch-specific statements. |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 1723 | if (!SemaRef.getCurFunction()->SwitchStack.empty()) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1724 | // case expression: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1725 | Builder.AddTypedTextChunk("case"); |
| 1726 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1727 | Builder.AddPlaceholderChunk("expression"); |
| 1728 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1729 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1730 | |
| 1731 | // default: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1732 | Builder.AddTypedTextChunk("default"); |
| 1733 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1734 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1737 | if (Results.includeCodePatterns()) { |
| 1738 | /// while (condition) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1739 | Builder.AddTypedTextChunk("while"); |
| 1740 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1741 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1742 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1743 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1744 | Builder.AddPlaceholderChunk("expression"); |
| 1745 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1746 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1747 | Builder.AddPlaceholderChunk("statements"); |
| 1748 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1749 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1750 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1751 | |
| 1752 | // do { statements } while ( expression ); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1753 | Builder.AddTypedTextChunk("do"); |
| 1754 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1755 | Builder.AddPlaceholderChunk("statements"); |
| 1756 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1757 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1758 | Builder.AddTextChunk("while"); |
| 1759 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1760 | Builder.AddPlaceholderChunk("expression"); |
| 1761 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1762 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1763 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1764 | // for ( for-init-statement ; condition ; expression ) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1765 | Builder.AddTypedTextChunk("for"); |
| 1766 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1767 | if (SemaRef.getLangOpts().CPlusPlus || SemaRef.getLangOpts().C99) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1768 | Builder.AddPlaceholderChunk("init-statement"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1769 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1770 | Builder.AddPlaceholderChunk("init-expression"); |
| 1771 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1772 | Builder.AddPlaceholderChunk("condition"); |
| 1773 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1774 | Builder.AddPlaceholderChunk("inc-expression"); |
| 1775 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1776 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1777 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1778 | Builder.AddPlaceholderChunk("statements"); |
| 1779 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1780 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1781 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1782 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1783 | |
| 1784 | if (S->getContinueParent()) { |
| 1785 | // continue ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1786 | Builder.AddTypedTextChunk("continue"); |
| 1787 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | if (S->getBreakParent()) { |
| 1791 | // break ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1792 | Builder.AddTypedTextChunk("break"); |
| 1793 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | // "return expression ;" or "return ;", depending on whether we |
| 1797 | // know the function is void or not. |
| 1798 | bool isVoid = false; |
| 1799 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1800 | isVoid = Function->getReturnType()->isVoidType(); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1801 | else if (ObjCMethodDecl *Method |
| 1802 | = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1803 | isVoid = Method->getReturnType()->isVoidType(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1804 | else if (SemaRef.getCurBlock() && |
| 1805 | !SemaRef.getCurBlock()->ReturnType.isNull()) |
| 1806 | isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType(); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1807 | Builder.AddTypedTextChunk("return"); |
Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1808 | if (!isVoid) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1809 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1810 | Builder.AddPlaceholderChunk("expression"); |
Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1811 | } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1812 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1813 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1814 | // goto identifier ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1815 | Builder.AddTypedTextChunk("goto"); |
| 1816 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1817 | Builder.AddPlaceholderChunk("label"); |
| 1818 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1819 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1820 | // Using directives |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1821 | Builder.AddTypedTextChunk("using"); |
| 1822 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1823 | Builder.AddTextChunk("namespace"); |
| 1824 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1825 | Builder.AddPlaceholderChunk("identifier"); |
| 1826 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | // Fall through (for statement expressions). |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1830 | case Sema::PCC_ForInit: |
| 1831 | case Sema::PCC_Condition: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1832 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1833 | // Fall through: conditions and statements can have expressions. |
| 1834 | |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1835 | case Sema::PCC_ParenthesizedExpression: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1836 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1837 | CCC == Sema::PCC_ParenthesizedExpression) { |
| 1838 | // (__bridge <type>)<expression> |
| 1839 | Builder.AddTypedTextChunk("__bridge"); |
| 1840 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1841 | Builder.AddPlaceholderChunk("type"); |
| 1842 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1843 | Builder.AddPlaceholderChunk("expression"); |
| 1844 | Results.AddResult(Result(Builder.TakeString())); |
| 1845 | |
| 1846 | // (__bridge_transfer <Objective-C type>)<expression> |
| 1847 | Builder.AddTypedTextChunk("__bridge_transfer"); |
| 1848 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1849 | Builder.AddPlaceholderChunk("Objective-C type"); |
| 1850 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1851 | Builder.AddPlaceholderChunk("expression"); |
| 1852 | Results.AddResult(Result(Builder.TakeString())); |
| 1853 | |
| 1854 | // (__bridge_retained <CF type>)<expression> |
| 1855 | Builder.AddTypedTextChunk("__bridge_retained"); |
| 1856 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1857 | Builder.AddPlaceholderChunk("CF type"); |
| 1858 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1859 | Builder.AddPlaceholderChunk("expression"); |
| 1860 | Results.AddResult(Result(Builder.TakeString())); |
| 1861 | } |
| 1862 | // Fall through |
| 1863 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1864 | case Sema::PCC_Expression: { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1865 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1866 | // 'this', if we're in a non-static member function. |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1867 | addThisCompletion(SemaRef, Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1868 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1869 | // true |
| 1870 | Builder.AddResultTypeChunk("bool"); |
| 1871 | Builder.AddTypedTextChunk("true"); |
| 1872 | Results.AddResult(Result(Builder.TakeString())); |
| 1873 | |
| 1874 | // false |
| 1875 | Builder.AddResultTypeChunk("bool"); |
| 1876 | Builder.AddTypedTextChunk("false"); |
| 1877 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1878 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1879 | if (SemaRef.getLangOpts().RTTI) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1880 | // dynamic_cast < type-id > ( expression ) |
| 1881 | Builder.AddTypedTextChunk("dynamic_cast"); |
| 1882 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1883 | Builder.AddPlaceholderChunk("type"); |
| 1884 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1885 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1886 | Builder.AddPlaceholderChunk("expression"); |
| 1887 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1888 | Results.AddResult(Result(Builder.TakeString())); |
| 1889 | } |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1890 | |
| 1891 | // static_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1892 | Builder.AddTypedTextChunk("static_cast"); |
| 1893 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1894 | Builder.AddPlaceholderChunk("type"); |
| 1895 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1896 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1897 | Builder.AddPlaceholderChunk("expression"); |
| 1898 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1899 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1900 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1901 | // reinterpret_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1902 | Builder.AddTypedTextChunk("reinterpret_cast"); |
| 1903 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1904 | Builder.AddPlaceholderChunk("type"); |
| 1905 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1906 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1907 | Builder.AddPlaceholderChunk("expression"); |
| 1908 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1909 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1910 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1911 | // const_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1912 | Builder.AddTypedTextChunk("const_cast"); |
| 1913 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1914 | Builder.AddPlaceholderChunk("type"); |
| 1915 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1916 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1917 | Builder.AddPlaceholderChunk("expression"); |
| 1918 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1919 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1920 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1921 | if (SemaRef.getLangOpts().RTTI) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1922 | // typeid ( expression-or-type ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1923 | Builder.AddResultTypeChunk("std::type_info"); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1924 | Builder.AddTypedTextChunk("typeid"); |
| 1925 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1926 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 1927 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1928 | Results.AddResult(Result(Builder.TakeString())); |
| 1929 | } |
| 1930 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1931 | // new T ( ... ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1932 | Builder.AddTypedTextChunk("new"); |
| 1933 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1934 | Builder.AddPlaceholderChunk("type"); |
| 1935 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1936 | Builder.AddPlaceholderChunk("expressions"); |
| 1937 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1938 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1939 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1940 | // new T [ ] ( ... ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1941 | Builder.AddTypedTextChunk("new"); |
| 1942 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1943 | Builder.AddPlaceholderChunk("type"); |
| 1944 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1945 | Builder.AddPlaceholderChunk("size"); |
| 1946 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 1947 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1948 | Builder.AddPlaceholderChunk("expressions"); |
| 1949 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1950 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1951 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1952 | // delete expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1953 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1954 | Builder.AddTypedTextChunk("delete"); |
| 1955 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1956 | Builder.AddPlaceholderChunk("expression"); |
| 1957 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1958 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1959 | // delete [] expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1960 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1961 | Builder.AddTypedTextChunk("delete"); |
| 1962 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1963 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1964 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 1965 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1966 | Builder.AddPlaceholderChunk("expression"); |
| 1967 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1968 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1969 | if (SemaRef.getLangOpts().CXXExceptions) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1970 | // throw expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1971 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1972 | Builder.AddTypedTextChunk("throw"); |
| 1973 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1974 | Builder.AddPlaceholderChunk("expression"); |
| 1975 | Results.AddResult(Result(Builder.TakeString())); |
| 1976 | } |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1977 | |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1978 | // FIXME: Rethrow? |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1979 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1980 | if (SemaRef.getLangOpts().CPlusPlus11) { |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1981 | // nullptr |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1982 | Builder.AddResultTypeChunk("std::nullptr_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1983 | Builder.AddTypedTextChunk("nullptr"); |
| 1984 | Results.AddResult(Result(Builder.TakeString())); |
| 1985 | |
| 1986 | // alignof |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1987 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1988 | Builder.AddTypedTextChunk("alignof"); |
| 1989 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1990 | Builder.AddPlaceholderChunk("type"); |
| 1991 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1992 | Results.AddResult(Result(Builder.TakeString())); |
| 1993 | |
| 1994 | // noexcept |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1995 | Builder.AddResultTypeChunk("bool"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1996 | Builder.AddTypedTextChunk("noexcept"); |
| 1997 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1998 | Builder.AddPlaceholderChunk("expression"); |
| 1999 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2000 | Results.AddResult(Result(Builder.TakeString())); |
| 2001 | |
| 2002 | // sizeof... expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2003 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2004 | Builder.AddTypedTextChunk("sizeof..."); |
| 2005 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2006 | Builder.AddPlaceholderChunk("parameter-pack"); |
| 2007 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2008 | Results.AddResult(Result(Builder.TakeString())); |
| 2009 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2012 | if (SemaRef.getLangOpts().ObjC1) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2013 | // 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] | 2014 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
| 2015 | // The interface can be NULL. |
| 2016 | if (ObjCInterfaceDecl *ID = Method->getClassInterface()) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2017 | if (ID->getSuperClass()) { |
| 2018 | std::string SuperType; |
| 2019 | SuperType = ID->getSuperClass()->getNameAsString(); |
| 2020 | if (Method->isInstanceMethod()) |
| 2021 | SuperType += " *"; |
| 2022 | |
| 2023 | Builder.AddResultTypeChunk(Allocator.CopyString(SuperType)); |
| 2024 | Builder.AddTypedTextChunk("super"); |
| 2025 | Results.AddResult(Result(Builder.TakeString())); |
| 2026 | } |
Ted Kremenek | 305a0a7 | 2010-05-31 21:43:10 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2029 | AddObjCExpressionResults(Results, true); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2032 | if (SemaRef.getLangOpts().C11) { |
| 2033 | // _Alignof |
| 2034 | Builder.AddResultTypeChunk("size_t"); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2035 | if (SemaRef.PP.isMacroDefined("alignof")) |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2036 | Builder.AddTypedTextChunk("alignof"); |
| 2037 | else |
| 2038 | Builder.AddTypedTextChunk("_Alignof"); |
| 2039 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2040 | Builder.AddPlaceholderChunk("type"); |
| 2041 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2042 | Results.AddResult(Result(Builder.TakeString())); |
| 2043 | } |
| 2044 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2045 | // sizeof expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2046 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2047 | Builder.AddTypedTextChunk("sizeof"); |
| 2048 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2049 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 2050 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2051 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2052 | break; |
| 2053 | } |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 2054 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2055 | case Sema::PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2056 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 2057 | break; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2060 | if (WantTypesInContext(CCC, SemaRef.getLangOpts())) |
| 2061 | AddTypeSpecifierResults(SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2062 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2063 | if (SemaRef.getLangOpts().CPlusPlus && CCC != Sema::PCC_Type) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2064 | Results.AddResult(Result("operator")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2067 | /// \brief If the given declaration has an associated type, add it as a result |
| 2068 | /// type chunk. |
| 2069 | static void AddResultTypeChunk(ASTContext &Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2070 | const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2071 | const NamedDecl *ND, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2072 | QualType BaseType, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2073 | CodeCompletionBuilder &Result) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2074 | if (!ND) |
| 2075 | return; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2076 | |
| 2077 | // Skip constructors and conversion functions, which have their return types |
| 2078 | // built into their names. |
| 2079 | if (isa<CXXConstructorDecl>(ND) || isa<CXXConversionDecl>(ND)) |
| 2080 | return; |
| 2081 | |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2082 | // Determine the type of the declaration (if it has a type). |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 2083 | QualType T; |
| 2084 | if (const FunctionDecl *Function = ND->getAsFunction()) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2085 | T = Function->getReturnType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2086 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
| 2087 | if (!BaseType.isNull()) |
| 2088 | T = Method->getSendResultType(BaseType); |
| 2089 | else |
| 2090 | T = Method->getReturnType(); |
| 2091 | } else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2092 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); |
| 2093 | else if (isa<UnresolvedUsingValueDecl>(ND)) { |
| 2094 | /* Do nothing: ignore unresolved using declarations*/ |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2095 | } else if (const ObjCIvarDecl *Ivar = dyn_cast<ObjCIvarDecl>(ND)) { |
| 2096 | if (!BaseType.isNull()) |
| 2097 | T = Ivar->getUsageType(BaseType); |
| 2098 | else |
| 2099 | T = Ivar->getType(); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2100 | } else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND)) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2101 | T = Value->getType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2102 | } else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) { |
| 2103 | if (!BaseType.isNull()) |
| 2104 | T = Property->getUsageType(BaseType); |
| 2105 | else |
| 2106 | T = Property->getType(); |
| 2107 | } |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2108 | |
| 2109 | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) |
| 2110 | return; |
| 2111 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2112 | Result.AddResultTypeChunk(GetCompletionTypeString(T, Context, Policy, |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2113 | Result.getAllocator())); |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2116 | static void MaybeAddSentinel(Preprocessor &PP, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2117 | const NamedDecl *FunctionOrMethod, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2118 | CodeCompletionBuilder &Result) { |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2119 | if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) |
| 2120 | if (Sentinel->getSentinel() == 0) { |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2121 | if (PP.getLangOpts().ObjC1 && PP.isMacroDefined("nil")) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2122 | Result.AddTextChunk(", nil"); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2123 | else if (PP.isMacroDefined("NULL")) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2124 | Result.AddTextChunk(", NULL"); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2125 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2126 | Result.AddTextChunk(", (void*)0"); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2127 | } |
| 2128 | } |
| 2129 | |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2130 | static std::string formatObjCParamQualifiers(unsigned ObjCQuals, |
| 2131 | QualType &Type) { |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2132 | std::string Result; |
| 2133 | if (ObjCQuals & Decl::OBJC_TQ_In) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2134 | Result += "in "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2135 | else if (ObjCQuals & Decl::OBJC_TQ_Inout) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2136 | Result += "inout "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2137 | else if (ObjCQuals & Decl::OBJC_TQ_Out) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2138 | Result += "out "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2139 | if (ObjCQuals & Decl::OBJC_TQ_Bycopy) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2140 | Result += "bycopy "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2141 | else if (ObjCQuals & Decl::OBJC_TQ_Byref) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2142 | Result += "byref "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2143 | if (ObjCQuals & Decl::OBJC_TQ_Oneway) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2144 | Result += "oneway "; |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2145 | if (ObjCQuals & Decl::OBJC_TQ_CSNullability) { |
| 2146 | if (auto nullability = AttributedType::stripOuterNullability(Type)) { |
| 2147 | switch (*nullability) { |
| 2148 | case NullabilityKind::NonNull: |
| 2149 | Result += "nonnull "; |
| 2150 | break; |
| 2151 | |
| 2152 | case NullabilityKind::Nullable: |
| 2153 | Result += "nullable "; |
| 2154 | break; |
| 2155 | |
| 2156 | case NullabilityKind::Unspecified: |
| 2157 | Result += "null_unspecified "; |
| 2158 | break; |
| 2159 | } |
| 2160 | } |
| 2161 | } |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2162 | return Result; |
| 2163 | } |
| 2164 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2165 | static std::string FormatFunctionParameter(const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2166 | const ParmVarDecl *Param, |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2167 | bool SuppressName = false, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2168 | bool SuppressBlock = false, |
| 2169 | Optional<ArrayRef<QualType>> ObjCSubsts = None) { |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2170 | bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext()); |
| 2171 | if (Param->getType()->isDependentType() || |
| 2172 | !Param->getType()->isBlockPointerType()) { |
| 2173 | // The argument for a dependent or non-block parameter is a placeholder |
| 2174 | // containing that parameter's type. |
| 2175 | std::string Result; |
| 2176 | |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2177 | if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName) |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2178 | Result = Param->getIdentifier()->getName(); |
| 2179 | |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2180 | QualType Type = Param->getType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2181 | if (ObjCSubsts) |
| 2182 | Type = Type.substObjCTypeArgs(Param->getASTContext(), *ObjCSubsts, |
| 2183 | ObjCSubstitutionContext::Parameter); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2184 | if (ObjCMethodParam) { |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2185 | Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier(), |
| 2186 | Type); |
| 2187 | Result += Type.getAsString(Policy) + ")"; |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2188 | if (Param->getIdentifier() && !SuppressName) |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2189 | Result += Param->getIdentifier()->getName(); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2190 | } else { |
| 2191 | Type.getAsStringInternal(Result, Policy); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2192 | } |
| 2193 | return Result; |
| 2194 | } |
| 2195 | |
| 2196 | // The argument for a block pointer parameter is a block literal with |
| 2197 | // the appropriate type. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2198 | FunctionTypeLoc Block; |
| 2199 | FunctionProtoTypeLoc BlockProto; |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2200 | TypeLoc TL; |
| 2201 | if (TypeSourceInfo *TSInfo = Param->getTypeSourceInfo()) { |
| 2202 | TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 2203 | while (true) { |
| 2204 | // Look through typedefs. |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2205 | if (!SuppressBlock) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2206 | if (TypedefTypeLoc TypedefTL = TL.getAs<TypedefTypeLoc>()) { |
| 2207 | if (TypeSourceInfo *InnerTSInfo = |
| 2208 | TypedefTL.getTypedefNameDecl()->getTypeSourceInfo()) { |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2209 | TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 2210 | continue; |
| 2211 | } |
| 2212 | } |
| 2213 | |
| 2214 | // Look through qualified types |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2215 | if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>()) { |
| 2216 | TL = QualifiedTL.getUnqualifiedLoc(); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2217 | continue; |
| 2218 | } |
Douglas Gregor | 4c850f3 | 2015-07-07 06:20:22 +0000 | [diff] [blame] | 2219 | |
| 2220 | if (AttributedTypeLoc AttrTL = TL.getAs<AttributedTypeLoc>()) { |
| 2221 | TL = AttrTL.getModifiedLoc(); |
| 2222 | continue; |
| 2223 | } |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2226 | // Try to get the function prototype behind the block pointer type, |
| 2227 | // then we're done. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2228 | if (BlockPointerTypeLoc BlockPtr = TL.getAs<BlockPointerTypeLoc>()) { |
| 2229 | TL = BlockPtr.getPointeeLoc().IgnoreParens(); |
| 2230 | Block = TL.getAs<FunctionTypeLoc>(); |
| 2231 | BlockProto = TL.getAs<FunctionProtoTypeLoc>(); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2232 | } |
| 2233 | break; |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | if (!Block) { |
| 2238 | // We were unable to find a FunctionProtoTypeLoc with parameter names |
| 2239 | // for the block; just use the parameter type as a placeholder. |
| 2240 | std::string Result; |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2241 | if (!ObjCMethodParam && Param->getIdentifier()) |
| 2242 | Result = Param->getIdentifier()->getName(); |
| 2243 | |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2244 | QualType Type = Param->getType().getUnqualifiedType(); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2245 | |
| 2246 | if (ObjCMethodParam) { |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2247 | Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier(), |
| 2248 | Type); |
| 2249 | Result += Type.getAsString(Policy) + Result + ")"; |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2250 | if (Param->getIdentifier()) |
| 2251 | Result += Param->getIdentifier()->getName(); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2252 | } else { |
| 2253 | Type.getAsStringInternal(Result, Policy); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | return Result; |
| 2257 | } |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2258 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2259 | // We have the function prototype behind the block pointer type, as it was |
| 2260 | // written in the source. |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2261 | std::string Result; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2262 | QualType ResultType = Block.getTypePtr()->getReturnType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2263 | if (ObjCSubsts) |
| 2264 | ResultType = ResultType.substObjCTypeArgs(Param->getASTContext(), |
| 2265 | *ObjCSubsts, |
| 2266 | ObjCSubstitutionContext::Result); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2267 | if (!ResultType->isVoidType() || SuppressBlock) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2268 | ResultType.getAsStringInternal(Result, Policy); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2269 | |
| 2270 | // Format the parameter list. |
| 2271 | std::string Params; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2272 | if (!BlockProto || Block.getNumParams() == 0) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2273 | if (BlockProto && BlockProto.getTypePtr()->isVariadic()) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2274 | Params = "(...)"; |
Douglas Gregor | af25cfa | 2010-10-02 23:49:58 +0000 | [diff] [blame] | 2275 | else |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2276 | Params = "(void)"; |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2277 | } else { |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2278 | Params += "("; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2279 | for (unsigned I = 0, N = Block.getNumParams(); I != N; ++I) { |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2280 | if (I) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2281 | Params += ", "; |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2282 | Params += FormatFunctionParameter(Policy, Block.getParam(I), |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2283 | /*SuppressName=*/false, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2284 | /*SuppressBlock=*/true, |
| 2285 | ObjCSubsts); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2286 | |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2287 | if (I == N - 1 && BlockProto.getTypePtr()->isVariadic()) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2288 | Params += ", ..."; |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2289 | } |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2290 | Params += ")"; |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2291 | } |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2292 | |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2293 | if (SuppressBlock) { |
| 2294 | // Format as a parameter. |
| 2295 | Result = Result + " (^"; |
| 2296 | if (Param->getIdentifier()) |
| 2297 | Result += Param->getIdentifier()->getName(); |
| 2298 | Result += ")"; |
| 2299 | Result += Params; |
| 2300 | } else { |
| 2301 | // Format as a block literal argument. |
| 2302 | Result = '^' + Result; |
| 2303 | Result += Params; |
| 2304 | |
| 2305 | if (Param->getIdentifier()) |
| 2306 | Result += Param->getIdentifier()->getName(); |
| 2307 | } |
| 2308 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2309 | return Result; |
| 2310 | } |
| 2311 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2312 | /// \brief Add function parameter chunks to the given code completion string. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2313 | static void AddFunctionParameterChunks(Preprocessor &PP, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2314 | const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2315 | const FunctionDecl *Function, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2316 | CodeCompletionBuilder &Result, |
| 2317 | unsigned Start = 0, |
| 2318 | bool InOptional = false) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2319 | bool FirstParameter = true; |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2320 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2321 | for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2322 | const ParmVarDecl *Param = Function->getParamDecl(P); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2323 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2324 | if (Param->hasDefaultArg() && !InOptional) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2325 | // When we see an optional default argument, put that argument and |
| 2326 | // the remaining default arguments into a new, optional string. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2327 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2328 | Result.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2329 | if (!FirstParameter) |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2330 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2331 | AddFunctionParameterChunks(PP, Policy, Function, Opt, P, true); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2332 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2333 | break; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2336 | if (FirstParameter) |
| 2337 | FirstParameter = false; |
| 2338 | else |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2339 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2340 | |
| 2341 | InOptional = false; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2342 | |
| 2343 | // Format the placeholder string. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2344 | std::string PlaceholderStr = FormatFunctionParameter(Policy, Param); |
| 2345 | |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2346 | if (Function->isVariadic() && P == N - 1) |
| 2347 | PlaceholderStr += ", ..."; |
| 2348 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2349 | // Add the placeholder string. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2350 | Result.AddPlaceholderChunk( |
| 2351 | Result.getAllocator().CopyString(PlaceholderStr)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2352 | } |
Douglas Gregor | ba44903 | 2009-09-22 21:42:17 +0000 | [diff] [blame] | 2353 | |
| 2354 | if (const FunctionProtoType *Proto |
| 2355 | = Function->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2356 | if (Proto->isVariadic()) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2357 | if (Proto->getNumParams() == 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2358 | Result.AddPlaceholderChunk("..."); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2359 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2360 | MaybeAddSentinel(PP, Function, Result); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2361 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | /// \brief Add template parameter chunks to the given code completion string. |
| 2365 | static void AddTemplateParameterChunks(ASTContext &Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2366 | const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2367 | const TemplateDecl *Template, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2368 | CodeCompletionBuilder &Result, |
| 2369 | unsigned MaxParameters = 0, |
| 2370 | unsigned Start = 0, |
| 2371 | bool InDefaultArg = false) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2372 | bool FirstParameter = true; |
Richard Smith | 6eece29 | 2015-01-15 02:27:20 +0000 | [diff] [blame] | 2373 | |
| 2374 | // Prefer to take the template parameter names from the first declaration of |
| 2375 | // the template. |
| 2376 | Template = cast<TemplateDecl>(Template->getCanonicalDecl()); |
| 2377 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2378 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2379 | TemplateParameterList::iterator PEnd = Params->end(); |
| 2380 | if (MaxParameters) |
| 2381 | PEnd = Params->begin() + MaxParameters; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2382 | for (TemplateParameterList::iterator P = Params->begin() + Start; |
| 2383 | P != PEnd; ++P) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2384 | bool HasDefaultArg = false; |
| 2385 | std::string PlaceholderStr; |
| 2386 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 2387 | if (TTP->wasDeclaredWithTypename()) |
| 2388 | PlaceholderStr = "typename"; |
| 2389 | else |
| 2390 | PlaceholderStr = "class"; |
| 2391 | |
| 2392 | if (TTP->getIdentifier()) { |
| 2393 | PlaceholderStr += ' '; |
| 2394 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2395 | } |
| 2396 | |
| 2397 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2398 | } else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2399 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2400 | if (NTTP->getIdentifier()) |
| 2401 | PlaceholderStr = NTTP->getIdentifier()->getName(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2402 | NTTP->getType().getAsStringInternal(PlaceholderStr, Policy); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2403 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2404 | } else { |
| 2405 | assert(isa<TemplateTemplateParmDecl>(*P)); |
| 2406 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 2407 | |
| 2408 | // Since putting the template argument list into the placeholder would |
| 2409 | // be very, very long, we just use an abbreviation. |
| 2410 | PlaceholderStr = "template<...> class"; |
| 2411 | if (TTP->getIdentifier()) { |
| 2412 | PlaceholderStr += ' '; |
| 2413 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2414 | } |
| 2415 | |
| 2416 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2417 | } |
| 2418 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2419 | if (HasDefaultArg && !InDefaultArg) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2420 | // When we see an optional default argument, put that argument and |
| 2421 | // the remaining default arguments into a new, optional string. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2422 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2423 | Result.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2424 | if (!FirstParameter) |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2425 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2426 | AddTemplateParameterChunks(Context, Policy, Template, Opt, MaxParameters, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2427 | P - Params->begin(), true); |
| 2428 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2429 | break; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2432 | InDefaultArg = false; |
| 2433 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2434 | if (FirstParameter) |
| 2435 | FirstParameter = false; |
| 2436 | else |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2437 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2438 | |
| 2439 | // Add the placeholder string. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2440 | Result.AddPlaceholderChunk( |
| 2441 | Result.getAllocator().CopyString(PlaceholderStr)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2442 | } |
| 2443 | } |
| 2444 | |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2445 | /// \brief Add a qualifier to the given code-completion string, if the |
| 2446 | /// provided nested-name-specifier is non-NULL. |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2447 | static void |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2448 | AddQualifierToCompletionString(CodeCompletionBuilder &Result, |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2449 | NestedNameSpecifier *Qualifier, |
| 2450 | bool QualifierIsInformative, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2451 | ASTContext &Context, |
| 2452 | const PrintingPolicy &Policy) { |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2453 | if (!Qualifier) |
| 2454 | return; |
| 2455 | |
| 2456 | std::string PrintedNNS; |
| 2457 | { |
| 2458 | llvm::raw_string_ostream OS(PrintedNNS); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2459 | Qualifier->print(OS, Policy); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2460 | } |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2461 | if (QualifierIsInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2462 | Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2463 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2464 | Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2467 | static void |
| 2468 | AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2469 | const FunctionDecl *Function) { |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2470 | const FunctionProtoType *Proto |
| 2471 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2472 | if (!Proto || !Proto->getTypeQuals()) |
| 2473 | return; |
| 2474 | |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2475 | // FIXME: Add ref-qualifier! |
| 2476 | |
| 2477 | // Handle single qualifiers without copying |
| 2478 | if (Proto->getTypeQuals() == Qualifiers::Const) { |
| 2479 | Result.AddInformativeChunk(" const"); |
| 2480 | return; |
| 2481 | } |
| 2482 | |
| 2483 | if (Proto->getTypeQuals() == Qualifiers::Volatile) { |
| 2484 | Result.AddInformativeChunk(" volatile"); |
| 2485 | return; |
| 2486 | } |
| 2487 | |
| 2488 | if (Proto->getTypeQuals() == Qualifiers::Restrict) { |
| 2489 | Result.AddInformativeChunk(" restrict"); |
| 2490 | return; |
| 2491 | } |
| 2492 | |
| 2493 | // Handle multiple qualifiers. |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2494 | std::string QualsStr; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2495 | if (Proto->isConst()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2496 | QualsStr += " const"; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2497 | if (Proto->isVolatile()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2498 | QualsStr += " volatile"; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2499 | if (Proto->isRestrict()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2500 | QualsStr += " restrict"; |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2501 | Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr)); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2504 | /// \brief Add the name of the given declaration |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2505 | static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2506 | const NamedDecl *ND, |
| 2507 | CodeCompletionBuilder &Result) { |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2508 | DeclarationName Name = ND->getDeclName(); |
| 2509 | if (!Name) |
| 2510 | return; |
| 2511 | |
| 2512 | switch (Name.getNameKind()) { |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2513 | case DeclarationName::CXXOperatorName: { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2514 | const char *OperatorName = nullptr; |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2515 | switch (Name.getCXXOverloadedOperator()) { |
| 2516 | case OO_None: |
| 2517 | case OO_Conditional: |
| 2518 | case NUM_OVERLOADED_OPERATORS: |
| 2519 | OperatorName = "operator"; |
| 2520 | break; |
| 2521 | |
| 2522 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 2523 | case OO_##Name: OperatorName = "operator" Spelling; break; |
| 2524 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) |
| 2525 | #include "clang/Basic/OperatorKinds.def" |
| 2526 | |
| 2527 | case OO_New: OperatorName = "operator new"; break; |
| 2528 | case OO_Delete: OperatorName = "operator delete"; break; |
| 2529 | case OO_Array_New: OperatorName = "operator new[]"; break; |
| 2530 | case OO_Array_Delete: OperatorName = "operator delete[]"; break; |
| 2531 | case OO_Call: OperatorName = "operator()"; break; |
| 2532 | case OO_Subscript: OperatorName = "operator[]"; break; |
| 2533 | } |
| 2534 | Result.AddTypedTextChunk(OperatorName); |
| 2535 | break; |
| 2536 | } |
| 2537 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2538 | case DeclarationName::Identifier: |
| 2539 | case DeclarationName::CXXConversionFunctionName: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2540 | case DeclarationName::CXXDestructorName: |
| 2541 | case DeclarationName::CXXLiteralOperatorName: |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2542 | Result.AddTypedTextChunk( |
| 2543 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2544 | break; |
| 2545 | |
| 2546 | case DeclarationName::CXXUsingDirective: |
| 2547 | case DeclarationName::ObjCZeroArgSelector: |
| 2548 | case DeclarationName::ObjCOneArgSelector: |
| 2549 | case DeclarationName::ObjCMultiArgSelector: |
| 2550 | break; |
| 2551 | |
| 2552 | case DeclarationName::CXXConstructorName: { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2553 | CXXRecordDecl *Record = nullptr; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2554 | QualType Ty = Name.getCXXNameType(); |
| 2555 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) |
| 2556 | Record = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 2557 | else if (const InjectedClassNameType *InjectedTy |
| 2558 | = Ty->getAs<InjectedClassNameType>()) |
| 2559 | Record = InjectedTy->getDecl(); |
| 2560 | else { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2561 | Result.AddTypedTextChunk( |
| 2562 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2563 | break; |
| 2564 | } |
| 2565 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2566 | Result.AddTypedTextChunk( |
| 2567 | Result.getAllocator().CopyString(Record->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2568 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2569 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2570 | AddTemplateParameterChunks(Context, Policy, Template, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2571 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2572 | } |
| 2573 | break; |
| 2574 | } |
| 2575 | } |
| 2576 | } |
| 2577 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2578 | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString(Sema &S, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2579 | const CodeCompletionContext &CCContext, |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2580 | CodeCompletionAllocator &Allocator, |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2581 | CodeCompletionTUInfo &CCTUInfo, |
| 2582 | bool IncludeBriefComments) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2583 | return CreateCodeCompletionString(S.Context, S.PP, CCContext, Allocator, |
| 2584 | CCTUInfo, IncludeBriefComments); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2587 | /// \brief If possible, create a new code completion string for the given |
| 2588 | /// result. |
| 2589 | /// |
| 2590 | /// \returns Either a new, heap-allocated code completion string describing |
| 2591 | /// how to use this result, or NULL to indicate that the string or name of the |
| 2592 | /// result is all that is needed. |
| 2593 | CodeCompletionString * |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2594 | CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx, |
| 2595 | Preprocessor &PP, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2596 | const CodeCompletionContext &CCContext, |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2597 | CodeCompletionAllocator &Allocator, |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2598 | CodeCompletionTUInfo &CCTUInfo, |
| 2599 | bool IncludeBriefComments) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2600 | CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2601 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2602 | PrintingPolicy Policy = getCompletionPrintingPolicy(Ctx, PP); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2603 | if (Kind == RK_Pattern) { |
| 2604 | Pattern->Priority = Priority; |
| 2605 | Pattern->Availability = Availability; |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2606 | |
| 2607 | if (Declaration) { |
| 2608 | Result.addParentContext(Declaration->getDeclContext()); |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2609 | Pattern->ParentName = Result.getParentName(); |
Fariborz Jahanian | 1fcf492 | 2013-03-22 17:55:27 +0000 | [diff] [blame] | 2610 | // Provide code completion comment for self.GetterName where |
| 2611 | // GetterName is the getter method for a property with name |
| 2612 | // different from the property name (declared via a property |
| 2613 | // getter attribute. |
| 2614 | const NamedDecl *ND = Declaration; |
| 2615 | if (const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(ND)) |
| 2616 | if (M->isPropertyAccessor()) |
| 2617 | if (const ObjCPropertyDecl *PDecl = M->findPropertyDecl()) |
| 2618 | if (PDecl->getGetterName() == M->getSelector() && |
Fariborz Jahanian | be8bc67 | 2013-03-23 01:10:45 +0000 | [diff] [blame] | 2619 | PDecl->getIdentifier() != M->getIdentifier()) { |
| 2620 | if (const RawComment *RC = |
| 2621 | Ctx.getRawCommentForAnyRedecl(M)) { |
Fariborz Jahanian | 1fcf492 | 2013-03-22 17:55:27 +0000 | [diff] [blame] | 2622 | Result.addBriefComment(RC->getBriefText(Ctx)); |
| 2623 | Pattern->BriefComment = Result.getBriefComment(); |
| 2624 | } |
Fariborz Jahanian | be8bc67 | 2013-03-23 01:10:45 +0000 | [diff] [blame] | 2625 | else if (const RawComment *RC = |
| 2626 | Ctx.getRawCommentForAnyRedecl(PDecl)) { |
| 2627 | Result.addBriefComment(RC->getBriefText(Ctx)); |
| 2628 | Pattern->BriefComment = Result.getBriefComment(); |
| 2629 | } |
| 2630 | } |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2633 | return Pattern; |
| 2634 | } |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2635 | |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2636 | if (Kind == RK_Keyword) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2637 | Result.AddTypedTextChunk(Keyword); |
| 2638 | return Result.TakeString(); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2639 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2640 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2641 | if (Kind == RK_Macro) { |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2642 | const MacroInfo *MI = PP.getMacroInfo(Macro); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2643 | Result.AddTypedTextChunk( |
| 2644 | Result.getAllocator().CopyString(Macro->getName())); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2645 | |
Ben Langmuir | c28ce3a | 2014-09-30 20:00:18 +0000 | [diff] [blame] | 2646 | if (!MI || !MI->isFunctionLike()) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2647 | return Result.TakeString(); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2648 | |
| 2649 | // Format a function-like macro with placeholders for the arguments. |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2650 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | 0c50531 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2651 | MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end(); |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2652 | |
| 2653 | // C99 variadic macros add __VA_ARGS__ at the end. Skip it. |
| 2654 | if (MI->isC99Varargs()) { |
| 2655 | --AEnd; |
| 2656 | |
| 2657 | if (A == AEnd) { |
| 2658 | Result.AddPlaceholderChunk("..."); |
| 2659 | } |
Douglas Gregor | 0c50531 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2660 | } |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2661 | |
Douglas Gregor | 0c50531 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2662 | for (MacroInfo::arg_iterator A = MI->arg_begin(); A != AEnd; ++A) { |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2663 | if (A != MI->arg_begin()) |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2664 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2665 | |
| 2666 | if (MI->isVariadic() && (A+1) == AEnd) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2667 | SmallString<32> Arg = (*A)->getName(); |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2668 | if (MI->isC99Varargs()) |
| 2669 | Arg += ", ..."; |
| 2670 | else |
| 2671 | Arg += "..."; |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2672 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2673 | break; |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2674 | } |
Douglas Gregor | 3aa5526 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2675 | |
| 2676 | // Non-variadic macros are simple. |
| 2677 | Result.AddPlaceholderChunk( |
| 2678 | Result.getAllocator().CopyString((*A)->getName())); |
Douglas Gregor | 0c50531 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2679 | } |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2680 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2681 | return Result.TakeString(); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2684 | assert(Kind == RK_Declaration && "Missed a result kind?"); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2685 | const NamedDecl *ND = Declaration; |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2686 | Result.addParentContext(ND->getDeclContext()); |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2687 | |
| 2688 | if (IncludeBriefComments) { |
| 2689 | // Add documentation comment, if it exists. |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 2690 | if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(ND)) { |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2691 | Result.addBriefComment(RC->getBriefText(Ctx)); |
Fariborz Jahanian | 15a0b55 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 2692 | } |
| 2693 | else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
| 2694 | if (OMD->isPropertyAccessor()) |
| 2695 | if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) |
| 2696 | if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(PDecl)) |
| 2697 | Result.addBriefComment(RC->getBriefText(Ctx)); |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2700 | if (StartsNestedNameSpecifier) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2701 | Result.AddTypedTextChunk( |
| 2702 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2703 | Result.AddTextChunk("::"); |
| 2704 | return Result.TakeString(); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2705 | } |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 2706 | |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2707 | for (const auto *I : ND->specific_attrs<AnnotateAttr>()) |
| 2708 | Result.AddAnnotation(Result.getAllocator().CopyString(I->getAnnotation())); |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 2709 | |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2710 | AddResultTypeChunk(Ctx, Policy, ND, CCContext.getBaseType(), Result); |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2711 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2712 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2713 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2714 | Ctx, Policy); |
| 2715 | AddTypedNameChunk(Ctx, Policy, ND, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2716 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2717 | AddFunctionParameterChunks(PP, Policy, Function, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2718 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2719 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2720 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2723 | if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2724 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2725 | Ctx, Policy); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2726 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2727 | AddTypedNameChunk(Ctx, Policy, Function, Result); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2728 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2729 | // Figure out which template parameters are deduced (or have default |
| 2730 | // arguments). |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 2731 | llvm::SmallBitVector Deduced; |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2732 | Sema::MarkDeducedTemplateParameters(Ctx, FunTmpl, Deduced); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2733 | unsigned LastDeducibleArgument; |
| 2734 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
| 2735 | --LastDeducibleArgument) { |
| 2736 | if (!Deduced[LastDeducibleArgument - 1]) { |
| 2737 | // C++0x: Figure out if the template argument has a default. If so, |
| 2738 | // the user doesn't need to type this argument. |
| 2739 | // FIXME: We need to abstract template parameters better! |
| 2740 | bool HasDefaultArg = false; |
| 2741 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2742 | LastDeducibleArgument - 1); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2743 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 2744 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2745 | else if (NonTypeTemplateParmDecl *NTTP |
| 2746 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 2747 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2748 | else { |
| 2749 | assert(isa<TemplateTemplateParmDecl>(Param)); |
| 2750 | HasDefaultArg |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2751 | = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
| 2754 | if (!HasDefaultArg) |
| 2755 | break; |
| 2756 | } |
| 2757 | } |
| 2758 | |
| 2759 | if (LastDeducibleArgument) { |
| 2760 | // Some of the function template arguments cannot be deduced from a |
| 2761 | // function call, so we introduce an explicit template argument list |
| 2762 | // containing all of the arguments up to the first deducible argument. |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2763 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2764 | AddTemplateParameterChunks(Ctx, Policy, FunTmpl, Result, |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2765 | LastDeducibleArgument); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2766 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | // Add the function parameters |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2770 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2771 | AddFunctionParameterChunks(PP, Policy, Function, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2772 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2773 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2774 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2777 | if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2778 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2779 | Ctx, Policy); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2780 | Result.AddTypedTextChunk( |
| 2781 | Result.getAllocator().CopyString(Template->getNameAsString())); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2782 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2783 | AddTemplateParameterChunks(Ctx, Policy, Template, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2784 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2785 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2788 | if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2789 | Selector Sel = Method->getSelector(); |
| 2790 | if (Sel.isUnarySelector()) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2791 | Result.AddTypedTextChunk(Result.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2792 | Sel.getNameForSlot(0))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2793 | return Result.TakeString(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2796 | std::string SelName = Sel.getNameForSlot(0).str(); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2797 | SelName += ':'; |
| 2798 | if (StartParameter == 0) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2799 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName)); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2800 | else { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2801 | Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName)); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2802 | |
| 2803 | // If there is only one parameter, and we're past it, add an empty |
| 2804 | // typed-text chunk since there is nothing to type. |
| 2805 | if (Method->param_size() == 1) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2806 | Result.AddTypedTextChunk(""); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2807 | } |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2808 | unsigned Idx = 0; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2809 | for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(), |
| 2810 | PEnd = Method->param_end(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2811 | P != PEnd; (void)++P, ++Idx) { |
| 2812 | if (Idx > 0) { |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2813 | std::string Keyword; |
| 2814 | if (Idx > StartParameter) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2815 | Result.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2816 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 2817 | Keyword += II->getName(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2818 | Keyword += ":"; |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2819 | if (Idx < StartParameter || AllParametersAreInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2820 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword)); |
Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2821 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2822 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword)); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2823 | } |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2824 | |
| 2825 | // If we're before the starting parameter, skip the placeholder. |
| 2826 | if (Idx < StartParameter) |
| 2827 | continue; |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2828 | |
| 2829 | std::string Arg; |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2830 | QualType ParamType = (*P)->getType(); |
| 2831 | Optional<ArrayRef<QualType>> ObjCSubsts; |
| 2832 | if (!CCContext.getBaseType().isNull()) |
| 2833 | ObjCSubsts = CCContext.getBaseType()->getObjCSubstitutions(Method); |
| 2834 | |
| 2835 | if (ParamType->isBlockPointerType() && !DeclaringEntity) |
| 2836 | Arg = FormatFunctionParameter(Policy, *P, true, |
| 2837 | /*SuppressBlock=*/false, |
| 2838 | ObjCSubsts); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2839 | else { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2840 | if (ObjCSubsts) |
| 2841 | ParamType = ParamType.substObjCTypeArgs(Ctx, *ObjCSubsts, |
| 2842 | ObjCSubstitutionContext::Parameter); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2843 | Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier(), |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2844 | ParamType); |
| 2845 | Arg += ParamType.getAsString(Policy) + ")"; |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2846 | if (IdentifierInfo *II = (*P)->getIdentifier()) |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2847 | if (DeclaringEntity || AllParametersAreInformative) |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 2848 | Arg += II->getName(); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2851 | if (Method->isVariadic() && (P + 1) == PEnd) |
| 2852 | Arg += ", ..."; |
| 2853 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2854 | if (DeclaringEntity) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2855 | Result.AddTextChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2856 | else if (AllParametersAreInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2857 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2858 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2859 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2862 | if (Method->isVariadic()) { |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2863 | if (Method->param_size() == 0) { |
| 2864 | if (DeclaringEntity) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2865 | Result.AddTextChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2866 | else if (AllParametersAreInformative) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2867 | Result.AddInformativeChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2868 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2869 | Result.AddPlaceholderChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2870 | } |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2871 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2872 | MaybeAddSentinel(PP, Method, Result); |
Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2875 | return Result.TakeString(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2876 | } |
| 2877 | |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2878 | if (Qualifier) |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2879 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2880 | Ctx, Policy); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2881 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2882 | Result.AddTypedTextChunk( |
| 2883 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2884 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2887 | /// \brief Add function overload parameter chunks to the given code completion |
| 2888 | /// string. |
| 2889 | static void AddOverloadParameterChunks(ASTContext &Context, |
| 2890 | const PrintingPolicy &Policy, |
| 2891 | const FunctionDecl *Function, |
| 2892 | const FunctionProtoType *Prototype, |
| 2893 | CodeCompletionBuilder &Result, |
| 2894 | unsigned CurrentArg, |
| 2895 | unsigned Start = 0, |
| 2896 | bool InOptional = false) { |
| 2897 | bool FirstParameter = true; |
| 2898 | unsigned NumParams = Function ? Function->getNumParams() |
| 2899 | : Prototype->getNumParams(); |
| 2900 | |
| 2901 | for (unsigned P = Start; P != NumParams; ++P) { |
| 2902 | if (Function && Function->getParamDecl(P)->hasDefaultArg() && !InOptional) { |
| 2903 | // When we see an optional default argument, put that argument and |
| 2904 | // the remaining default arguments into a new, optional string. |
| 2905 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2906 | Result.getCodeCompletionTUInfo()); |
| 2907 | if (!FirstParameter) |
| 2908 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| 2909 | // Optional sections are nested. |
| 2910 | AddOverloadParameterChunks(Context, Policy, Function, Prototype, Opt, |
| 2911 | CurrentArg, P, /*InOptional=*/true); |
| 2912 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2913 | return; |
| 2914 | } |
| 2915 | |
| 2916 | if (FirstParameter) |
| 2917 | FirstParameter = false; |
| 2918 | else |
| 2919 | Result.AddChunk(CodeCompletionString::CK_Comma); |
| 2920 | |
| 2921 | InOptional = false; |
| 2922 | |
| 2923 | // Format the placeholder string. |
| 2924 | std::string Placeholder; |
| 2925 | if (Function) |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2926 | Placeholder = FormatFunctionParameter(Policy, Function->getParamDecl(P)); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2927 | else |
| 2928 | Placeholder = Prototype->getParamType(P).getAsString(Policy); |
| 2929 | |
| 2930 | if (P == CurrentArg) |
| 2931 | Result.AddCurrentParameterChunk( |
| 2932 | Result.getAllocator().CopyString(Placeholder)); |
| 2933 | else |
| 2934 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Placeholder)); |
| 2935 | } |
| 2936 | |
| 2937 | if (Prototype && Prototype->isVariadic()) { |
| 2938 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2939 | Result.getCodeCompletionTUInfo()); |
| 2940 | if (!FirstParameter) |
| 2941 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| 2942 | |
| 2943 | if (CurrentArg < NumParams) |
| 2944 | Opt.AddPlaceholderChunk("..."); |
| 2945 | else |
| 2946 | Opt.AddCurrentParameterChunk("..."); |
| 2947 | |
| 2948 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2949 | } |
| 2950 | } |
| 2951 | |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2952 | CodeCompletionString * |
| 2953 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2954 | unsigned CurrentArg, Sema &S, |
| 2955 | CodeCompletionAllocator &Allocator, |
| 2956 | CodeCompletionTUInfo &CCTUInfo, |
| 2957 | bool IncludeBriefComments) const { |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2958 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2959 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2960 | // FIXME: Set priority, availability appropriately. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2961 | CodeCompletionBuilder Result(Allocator,CCTUInfo, 1, CXAvailability_Available); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2962 | FunctionDecl *FDecl = getFunction(); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2963 | const FunctionProtoType *Proto |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2964 | = dyn_cast<FunctionProtoType>(getFunctionType()); |
| 2965 | if (!FDecl && !Proto) { |
| 2966 | // Function without a prototype. Just give the return type and a |
| 2967 | // highlighted ellipsis. |
| 2968 | const FunctionType *FT = getFunctionType(); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2969 | Result.AddResultTypeChunk(Result.getAllocator().CopyString( |
| 2970 | FT->getReturnType().getAsString(Policy))); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2971 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2972 | Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "..."); |
| 2973 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2974 | return Result.TakeString(); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2975 | } |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2976 | |
| 2977 | if (FDecl) { |
| 2978 | if (IncludeBriefComments && CurrentArg < FDecl->getNumParams()) |
| 2979 | if (auto RC = S.getASTContext().getRawCommentForAnyRedecl( |
| 2980 | FDecl->getParamDecl(CurrentArg))) |
| 2981 | Result.addBriefComment(RC->getBriefText(S.getASTContext())); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2982 | AddResultTypeChunk(S.Context, Policy, FDecl, QualType(), Result); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2983 | Result.AddTextChunk( |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2984 | Result.getAllocator().CopyString(FDecl->getNameAsString())); |
| 2985 | } else { |
| 2986 | Result.AddResultTypeChunk( |
| 2987 | Result.getAllocator().CopyString( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2988 | Proto->getReturnType().getAsString(Policy))); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2989 | } |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2990 | |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2991 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2992 | AddOverloadParameterChunks(S.getASTContext(), Policy, FDecl, Proto, Result, |
| 2993 | CurrentArg); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2994 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 2995 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2996 | return Result.TakeString(); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2999 | unsigned clang::getMacroUsagePriority(StringRef MacroName, |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3000 | const LangOptions &LangOpts, |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3001 | bool PreferredTypeIsPointer) { |
| 3002 | unsigned Priority = CCP_Macro; |
| 3003 | |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3004 | // Treat the "nil", "Nil" and "NULL" macros as null pointer constants. |
| 3005 | if (MacroName.equals("nil") || MacroName.equals("NULL") || |
| 3006 | MacroName.equals("Nil")) { |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3007 | Priority = CCP_Constant; |
| 3008 | if (PreferredTypeIsPointer) |
| 3009 | Priority = Priority / CCF_SimilarTypeMatch; |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3010 | } |
| 3011 | // Treat "YES", "NO", "true", and "false" as constants. |
| 3012 | else if (MacroName.equals("YES") || MacroName.equals("NO") || |
| 3013 | MacroName.equals("true") || MacroName.equals("false")) |
| 3014 | Priority = CCP_Constant; |
| 3015 | // Treat "bool" as a type. |
| 3016 | else if (MacroName.equals("bool")) |
| 3017 | Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0); |
| 3018 | |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3019 | |
| 3020 | return Priority; |
| 3021 | } |
| 3022 | |
Dmitri Gribenko | bdc80de | 2013-01-11 20:32:41 +0000 | [diff] [blame] | 3023 | CXCursorKind clang::getCursorKindForDecl(const Decl *D) { |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3024 | if (!D) |
| 3025 | return CXCursor_UnexposedDecl; |
| 3026 | |
| 3027 | switch (D->getKind()) { |
| 3028 | case Decl::Enum: return CXCursor_EnumDecl; |
| 3029 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 3030 | case Decl::Field: return CXCursor_FieldDecl; |
| 3031 | case Decl::Function: |
| 3032 | return CXCursor_FunctionDecl; |
| 3033 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 3034 | case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryImplDecl; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3035 | case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl; |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 3036 | |
Argyrios Kyrtzidis | 3698cef | 2012-01-24 21:39:26 +0000 | [diff] [blame] | 3037 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3038 | case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl; |
| 3039 | case Decl::ObjCMethod: |
| 3040 | return cast<ObjCMethodDecl>(D)->isInstanceMethod() |
| 3041 | ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl; |
| 3042 | case Decl::CXXMethod: return CXCursor_CXXMethod; |
| 3043 | case Decl::CXXConstructor: return CXCursor_Constructor; |
| 3044 | case Decl::CXXDestructor: return CXCursor_Destructor; |
| 3045 | case Decl::CXXConversion: return CXCursor_ConversionFunction; |
| 3046 | case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl; |
Argyrios Kyrtzidis | 3698cef | 2012-01-24 21:39:26 +0000 | [diff] [blame] | 3047 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3048 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 3049 | case Decl::Typedef: return CXCursor_TypedefDecl; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3050 | case Decl::TypeAlias: return CXCursor_TypeAliasDecl; |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 3051 | case Decl::TypeAliasTemplate: return CXCursor_TypeAliasTemplateDecl; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3052 | case Decl::Var: return CXCursor_VarDecl; |
| 3053 | case Decl::Namespace: return CXCursor_Namespace; |
| 3054 | case Decl::NamespaceAlias: return CXCursor_NamespaceAlias; |
| 3055 | case Decl::TemplateTypeParm: return CXCursor_TemplateTypeParameter; |
| 3056 | case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter; |
| 3057 | case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter; |
| 3058 | case Decl::FunctionTemplate: return CXCursor_FunctionTemplate; |
| 3059 | case Decl::ClassTemplate: return CXCursor_ClassTemplate; |
Argyrios Kyrtzidis | 12afd70 | 2011-09-30 17:58:23 +0000 | [diff] [blame] | 3060 | case Decl::AccessSpec: return CXCursor_CXXAccessSpecifier; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3061 | case Decl::ClassTemplatePartialSpecialization: |
| 3062 | return CXCursor_ClassTemplatePartialSpecialization; |
| 3063 | case Decl::UsingDirective: return CXCursor_UsingDirective; |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 3064 | case Decl::StaticAssert: return CXCursor_StaticAssert; |
Douglas Gregor | 3e653b3 | 2012-04-30 23:41:16 +0000 | [diff] [blame] | 3065 | case Decl::TranslationUnit: return CXCursor_TranslationUnit; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3066 | |
| 3067 | case Decl::Using: |
| 3068 | case Decl::UnresolvedUsingValue: |
| 3069 | case Decl::UnresolvedUsingTypename: |
| 3070 | return CXCursor_UsingDeclaration; |
| 3071 | |
Douglas Gregor | 4cd6596 | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3072 | case Decl::ObjCPropertyImpl: |
| 3073 | switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) { |
| 3074 | case ObjCPropertyImplDecl::Dynamic: |
| 3075 | return CXCursor_ObjCDynamicDecl; |
| 3076 | |
| 3077 | case ObjCPropertyImplDecl::Synthesize: |
| 3078 | return CXCursor_ObjCSynthesizeDecl; |
| 3079 | } |
Argyrios Kyrtzidis | 50e5b1d | 2012-10-05 00:22:24 +0000 | [diff] [blame] | 3080 | |
| 3081 | case Decl::Import: |
| 3082 | return CXCursor_ModuleImportDecl; |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3083 | |
| 3084 | case Decl::ObjCTypeParam: return CXCursor_TemplateTypeParameter; |
| 3085 | |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3086 | default: |
Dmitri Gribenko | bdc80de | 2013-01-11 20:32:41 +0000 | [diff] [blame] | 3087 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3088 | switch (TD->getTagKind()) { |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3089 | case TTK_Interface: // fall through |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3090 | case TTK_Struct: return CXCursor_StructDecl; |
| 3091 | case TTK_Class: return CXCursor_ClassDecl; |
| 3092 | case TTK_Union: return CXCursor_UnionDecl; |
| 3093 | case TTK_Enum: return CXCursor_EnumDecl; |
| 3094 | } |
| 3095 | } |
| 3096 | } |
| 3097 | |
| 3098 | return CXCursor_UnexposedDecl; |
| 3099 | } |
| 3100 | |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3101 | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3102 | bool IncludeUndefined, |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3103 | bool TargetTypeIsPointer = false) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3104 | typedef CodeCompletionResult Result; |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3105 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3106 | Results.EnterNewScope(); |
Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 3107 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3108 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 3109 | MEnd = PP.macro_end(); |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3110 | M != MEnd; ++M) { |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 3111 | auto MD = PP.getMacroDefinition(M->first); |
| 3112 | if (IncludeUndefined || MD) { |
| 3113 | if (MacroInfo *MI = MD.getMacroInfo()) |
Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 3114 | if (MI->isUsedForHeaderGuard()) |
| 3115 | continue; |
| 3116 | |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3117 | Results.AddResult(Result(M->first, |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3118 | getMacroUsagePriority(M->first->getName(), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3119 | PP.getLangOpts(), |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3120 | TargetTypeIsPointer))); |
Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 3121 | } |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3122 | } |
Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 3123 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3124 | Results.ExitScope(); |
Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 3125 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3128 | static void AddPrettyFunctionResults(const LangOptions &LangOpts, |
| 3129 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3130 | typedef CodeCompletionResult Result; |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3131 | |
| 3132 | Results.EnterNewScope(); |
Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 3133 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3134 | Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant)); |
| 3135 | Results.AddResult(Result("__FUNCTION__", CCP_Constant)); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3136 | if (LangOpts.C99 || LangOpts.CPlusPlus11) |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3137 | Results.AddResult(Result("__func__", CCP_Constant)); |
| 3138 | Results.ExitScope(); |
| 3139 | } |
| 3140 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3141 | static void HandleCodeCompleteResults(Sema *S, |
| 3142 | CodeCompleteConsumer *CodeCompleter, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3143 | CodeCompletionContext Context, |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3144 | CodeCompletionResult *Results, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3145 | unsigned NumResults) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3146 | if (CodeCompleter) |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3147 | CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3148 | } |
| 3149 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3150 | static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S, |
| 3151 | Sema::ParserCompletionContext PCC) { |
| 3152 | switch (PCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3153 | case Sema::PCC_Namespace: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3154 | return CodeCompletionContext::CCC_TopLevel; |
| 3155 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3156 | case Sema::PCC_Class: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3157 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 3158 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3159 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3160 | return CodeCompletionContext::CCC_ObjCInterface; |
| 3161 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3162 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3163 | return CodeCompletionContext::CCC_ObjCImplementation; |
| 3164 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3165 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3166 | return CodeCompletionContext::CCC_ObjCIvarList; |
| 3167 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3168 | case Sema::PCC_Template: |
| 3169 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3170 | if (S.CurContext->isFileContext()) |
| 3171 | return CodeCompletionContext::CCC_TopLevel; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3172 | if (S.CurContext->isRecord()) |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3173 | return CodeCompletionContext::CCC_ClassStructUnion; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3174 | return CodeCompletionContext::CCC_Other; |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3175 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3176 | case Sema::PCC_RecoveryInFunction: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3177 | return CodeCompletionContext::CCC_Recovery; |
Douglas Gregor | c769d6e | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 3178 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3179 | case Sema::PCC_ForInit: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3180 | if (S.getLangOpts().CPlusPlus || S.getLangOpts().C99 || |
| 3181 | S.getLangOpts().ObjC1) |
Douglas Gregor | c769d6e | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 3182 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
| 3183 | else |
| 3184 | return CodeCompletionContext::CCC_Expression; |
| 3185 | |
| 3186 | case Sema::PCC_Expression: |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3187 | case Sema::PCC_Condition: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3188 | return CodeCompletionContext::CCC_Expression; |
| 3189 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3190 | case Sema::PCC_Statement: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3191 | return CodeCompletionContext::CCC_Statement; |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3192 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3193 | case Sema::PCC_Type: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3194 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3195 | |
| 3196 | case Sema::PCC_ParenthesizedExpression: |
| 3197 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3198 | |
| 3199 | case Sema::PCC_LocalDeclarationSpecifiers: |
| 3200 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3201 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3202 | |
| 3203 | llvm_unreachable("Invalid ParserCompletionContext!"); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3206 | /// \brief If we're in a C++ virtual member function, add completion results |
| 3207 | /// that invoke the functions we override, since it's common to invoke the |
| 3208 | /// overridden function as well as adding new functionality. |
| 3209 | /// |
| 3210 | /// \param S The semantic analysis object for which we are generating results. |
| 3211 | /// |
| 3212 | /// \param InContext This context in which the nested-name-specifier preceding |
| 3213 | /// the code-completion point |
| 3214 | static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, |
| 3215 | ResultBuilder &Results) { |
| 3216 | // Look through blocks. |
| 3217 | DeclContext *CurContext = S.CurContext; |
| 3218 | while (isa<BlockDecl>(CurContext)) |
| 3219 | CurContext = CurContext->getParent(); |
| 3220 | |
| 3221 | |
| 3222 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext); |
| 3223 | if (!Method || !Method->isVirtual()) |
| 3224 | return; |
| 3225 | |
| 3226 | // We need to have names for all of the parameters, if we're going to |
| 3227 | // generate a forwarding call. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3228 | for (auto P : Method->parameters()) |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3229 | if (!P->getDeclName()) |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3230 | return; |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3231 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3232 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3233 | for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(), |
| 3234 | MEnd = Method->end_overridden_methods(); |
| 3235 | M != MEnd; ++M) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3236 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3237 | Results.getCodeCompletionTUInfo()); |
Dmitri Gribenko | 6cfb153 | 2013-02-14 13:53:30 +0000 | [diff] [blame] | 3238 | const CXXMethodDecl *Overridden = *M; |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3239 | if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl()) |
| 3240 | continue; |
| 3241 | |
| 3242 | // If we need a nested-name-specifier, add one now. |
| 3243 | if (!InContext) { |
| 3244 | NestedNameSpecifier *NNS |
| 3245 | = getRequiredQualification(S.Context, CurContext, |
| 3246 | Overridden->getDeclContext()); |
| 3247 | if (NNS) { |
| 3248 | std::string Str; |
| 3249 | llvm::raw_string_ostream OS(Str); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3250 | NNS->print(OS, Policy); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3251 | Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str())); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3252 | } |
| 3253 | } else if (!InContext->Equals(Overridden->getDeclContext())) |
| 3254 | continue; |
| 3255 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3256 | Builder.AddTypedTextChunk(Results.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3257 | Overridden->getNameAsString())); |
| 3258 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3259 | bool FirstParam = true; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3260 | for (auto P : Method->parameters()) { |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3261 | if (FirstParam) |
| 3262 | FirstParam = false; |
| 3263 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3264 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3265 | |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3266 | Builder.AddPlaceholderChunk( |
| 3267 | Results.getAllocator().CopyString(P->getIdentifier()->getName())); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3268 | } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3269 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3270 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3271 | CCP_SuperCompletion, |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3272 | CXCursor_CXXMethod, |
| 3273 | CXAvailability_Available, |
| 3274 | Overridden)); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3275 | Results.Ignore(Overridden); |
| 3276 | } |
| 3277 | } |
| 3278 | |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3279 | void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc, |
| 3280 | ModuleIdPath Path) { |
| 3281 | typedef CodeCompletionResult Result; |
| 3282 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3283 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3284 | CodeCompletionContext::CCC_Other); |
| 3285 | Results.EnterNewScope(); |
| 3286 | |
| 3287 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3288 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3289 | typedef CodeCompletionResult Result; |
| 3290 | if (Path.empty()) { |
| 3291 | // Enumerate all top-level modules. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3292 | SmallVector<Module *, 8> Modules; |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3293 | PP.getHeaderSearchInfo().collectAllModules(Modules); |
| 3294 | for (unsigned I = 0, N = Modules.size(); I != N; ++I) { |
| 3295 | Builder.AddTypedTextChunk( |
| 3296 | Builder.getAllocator().CopyString(Modules[I]->Name)); |
| 3297 | Results.AddResult(Result(Builder.TakeString(), |
| 3298 | CCP_Declaration, |
Argyrios Kyrtzidis | 345d05f | 2013-05-29 18:50:15 +0000 | [diff] [blame] | 3299 | CXCursor_ModuleImportDecl, |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3300 | Modules[I]->isAvailable() |
| 3301 | ? CXAvailability_Available |
| 3302 | : CXAvailability_NotAvailable)); |
| 3303 | } |
Daniel Jasper | 07e6c40 | 2013-08-05 20:26:17 +0000 | [diff] [blame] | 3304 | } else if (getLangOpts().Modules) { |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3305 | // Load the named module. |
| 3306 | Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path, |
| 3307 | Module::AllVisible, |
| 3308 | /*IsInclusionDirective=*/false); |
| 3309 | // Enumerate submodules. |
| 3310 | if (Mod) { |
| 3311 | for (Module::submodule_iterator Sub = Mod->submodule_begin(), |
| 3312 | SubEnd = Mod->submodule_end(); |
| 3313 | Sub != SubEnd; ++Sub) { |
| 3314 | |
| 3315 | Builder.AddTypedTextChunk( |
| 3316 | Builder.getAllocator().CopyString((*Sub)->Name)); |
| 3317 | Results.AddResult(Result(Builder.TakeString(), |
| 3318 | CCP_Declaration, |
Argyrios Kyrtzidis | 345d05f | 2013-05-29 18:50:15 +0000 | [diff] [blame] | 3319 | CXCursor_ModuleImportDecl, |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3320 | (*Sub)->isAvailable() |
| 3321 | ? CXAvailability_Available |
| 3322 | : CXAvailability_NotAvailable)); |
| 3323 | } |
| 3324 | } |
| 3325 | } |
| 3326 | Results.ExitScope(); |
| 3327 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 3328 | Results.data(),Results.size()); |
| 3329 | } |
| 3330 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3331 | void Sema::CodeCompleteOrdinaryName(Scope *S, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3332 | ParserCompletionContext CompletionContext) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3333 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3334 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3335 | mapCodeCompletionContext(*this, CompletionContext)); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3336 | Results.EnterNewScope(); |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 3337 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3338 | // Determine how to filter results, e.g., so that the names of |
| 3339 | // values (functions, enumerators, function templates, etc.) are |
| 3340 | // only allowed where we can have an expression. |
| 3341 | switch (CompletionContext) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3342 | case PCC_Namespace: |
| 3343 | case PCC_Class: |
| 3344 | case PCC_ObjCInterface: |
| 3345 | case PCC_ObjCImplementation: |
| 3346 | case PCC_ObjCInstanceVariableList: |
| 3347 | case PCC_Template: |
| 3348 | case PCC_MemberTemplate: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3349 | case PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3350 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3351 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 3352 | break; |
| 3353 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3354 | case PCC_Statement: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3355 | case PCC_ParenthesizedExpression: |
Douglas Gregor | 4d755e8 | 2010-08-24 23:58:17 +0000 | [diff] [blame] | 3356 | case PCC_Expression: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3357 | case PCC_ForInit: |
| 3358 | case PCC_Condition: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3359 | if (WantTypesInContext(CompletionContext, getLangOpts())) |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 3360 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 3361 | else |
| 3362 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3363 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3364 | if (getLangOpts().CPlusPlus) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3365 | MaybeAddOverrideCalls(*this, /*InContext=*/nullptr, Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3366 | break; |
Douglas Gregor | 6da3db4 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 3367 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3368 | case PCC_RecoveryInFunction: |
Douglas Gregor | 6da3db4 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 3369 | // Unfiltered |
| 3370 | break; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3373 | // If we are in a C++ non-static member function, check the qualifiers on |
| 3374 | // the member function to filter/prioritize the results list. |
| 3375 | if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext)) |
| 3376 | if (CurMethod->isInstance()) |
| 3377 | Results.setObjectTypeQualifiers( |
| 3378 | Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers())); |
| 3379 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 3380 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3381 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3382 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 3383 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3384 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); |
Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 3385 | Results.ExitScope(); |
| 3386 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3387 | switch (CompletionContext) { |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3388 | case PCC_ParenthesizedExpression: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3389 | case PCC_Expression: |
| 3390 | case PCC_Statement: |
| 3391 | case PCC_RecoveryInFunction: |
| 3392 | if (S->getFnParent()) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 3393 | AddPrettyFunctionResults(getLangOpts(), Results); |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3394 | break; |
| 3395 | |
| 3396 | case PCC_Namespace: |
| 3397 | case PCC_Class: |
| 3398 | case PCC_ObjCInterface: |
| 3399 | case PCC_ObjCImplementation: |
| 3400 | case PCC_ObjCInstanceVariableList: |
| 3401 | case PCC_Template: |
| 3402 | case PCC_MemberTemplate: |
| 3403 | case PCC_ForInit: |
| 3404 | case PCC_Condition: |
| 3405 | case PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3406 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3407 | break; |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3410 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3411 | AddMacroResults(PP, Results, false); |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3412 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 3413 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3414 | Results.data(),Results.size()); |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 3415 | } |
| 3416 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3417 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| 3418 | ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 3419 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 3420 | bool AtArgumentExpression, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3421 | bool IsSuper, |
| 3422 | ResultBuilder &Results); |
| 3423 | |
| 3424 | void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, |
| 3425 | bool AllowNonIdentifiers, |
| 3426 | bool AllowNestedNameSpecifiers) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3427 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3428 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3429 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3430 | AllowNestedNameSpecifiers |
| 3431 | ? CodeCompletionContext::CCC_PotentiallyQualifiedName |
| 3432 | : CodeCompletionContext::CCC_Name); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3433 | Results.EnterNewScope(); |
| 3434 | |
| 3435 | // Type qualifiers can come after names. |
| 3436 | Results.AddResult(Result("const")); |
| 3437 | Results.AddResult(Result("volatile")); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3438 | if (getLangOpts().C99) |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3439 | Results.AddResult(Result("restrict")); |
| 3440 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3441 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3442 | if (AllowNonIdentifiers) { |
| 3443 | Results.AddResult(Result("operator")); |
| 3444 | } |
| 3445 | |
| 3446 | // Add nested-name-specifiers. |
| 3447 | if (AllowNestedNameSpecifiers) { |
| 3448 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3449 | Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3450 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3451 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
| 3452 | CodeCompleter->includeGlobals()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3453 | Results.setFilter(nullptr); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3454 | } |
| 3455 | } |
| 3456 | Results.ExitScope(); |
| 3457 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3458 | // If we're in a context where we might have an expression (rather than a |
| 3459 | // declaration), and what we've seen so far is an Objective-C type that could |
| 3460 | // be a receiver of a class message, this may be a class message send with |
| 3461 | // the initial opening bracket '[' missing. Add appropriate completions. |
| 3462 | if (AllowNonIdentifiers && !AllowNestedNameSpecifiers && |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3463 | DS.getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3464 | DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3465 | DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified && |
| 3466 | DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3467 | !DS.isTypeAltiVecVector() && |
| 3468 | S && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3469 | (S->getFlags() & Scope::DeclScope) != 0 && |
| 3470 | (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope | |
| 3471 | Scope::FunctionPrototypeScope | |
| 3472 | Scope::AtCatchScope)) == 0) { |
| 3473 | ParsedType T = DS.getRepAsType(); |
| 3474 | if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType()) |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 3475 | AddClassMessageCompletions(*this, S, T, None, false, false, Results); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3476 | } |
| 3477 | |
Douglas Gregor | 56ccce0 | 2010-08-24 04:59:56 +0000 | [diff] [blame] | 3478 | // Note that we intentionally suppress macro results here, since we do not |
| 3479 | // encourage using macros to produce the names of entities. |
| 3480 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3481 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3482 | Results.getCompletionContext(), |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3483 | Results.data(), Results.size()); |
| 3484 | } |
| 3485 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3486 | struct Sema::CodeCompleteExpressionData { |
| 3487 | CodeCompleteExpressionData(QualType PreferredType = QualType()) |
| 3488 | : PreferredType(PreferredType), IntegralConstantExpression(false), |
| 3489 | ObjCCollection(false) { } |
| 3490 | |
| 3491 | QualType PreferredType; |
| 3492 | bool IntegralConstantExpression; |
| 3493 | bool ObjCCollection; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3494 | SmallVector<Decl *, 4> IgnoreDecls; |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3495 | }; |
| 3496 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3497 | /// \brief Perform code-completion in an expression context when we know what |
| 3498 | /// type we're looking for. |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3499 | void Sema::CodeCompleteExpression(Scope *S, |
| 3500 | const CodeCompleteExpressionData &Data) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3501 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3502 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3503 | CodeCompletionContext::CCC_Expression); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3504 | if (Data.ObjCCollection) |
| 3505 | Results.setFilter(&ResultBuilder::IsObjCCollection); |
| 3506 | else if (Data.IntegralConstantExpression) |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3507 | Results.setFilter(&ResultBuilder::IsIntegralConstantValue); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3508 | else if (WantTypesInContext(PCC_Expression, getLangOpts())) |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3509 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 3510 | else |
| 3511 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3512 | |
| 3513 | if (!Data.PreferredType.isNull()) |
| 3514 | Results.setPreferredType(Data.PreferredType.getNonReferenceType()); |
| 3515 | |
| 3516 | // Ignore any declarations that we were told that we don't care about. |
| 3517 | for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I) |
| 3518 | Results.Ignore(Data.IgnoreDecls[I]); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3519 | |
| 3520 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3521 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3522 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3523 | |
| 3524 | Results.EnterNewScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3525 | AddOrdinaryNameResults(PCC_Expression, S, *this, Results); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3526 | Results.ExitScope(); |
| 3527 | |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3528 | bool PreferredTypeIsPointer = false; |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3529 | if (!Data.PreferredType.isNull()) |
| 3530 | PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType() |
| 3531 | || Data.PreferredType->isMemberPointerType() |
| 3532 | || Data.PreferredType->isBlockPointerType(); |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3533 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3534 | if (S->getFnParent() && |
| 3535 | !Data.ObjCCollection && |
| 3536 | !Data.IntegralConstantExpression) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 3537 | AddPrettyFunctionResults(getLangOpts(), Results); |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3538 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3539 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3540 | AddMacroResults(PP, Results, false, PreferredTypeIsPointer); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3541 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3542 | CodeCompletionContext(CodeCompletionContext::CCC_Expression, |
| 3543 | Data.PreferredType), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3544 | Results.data(),Results.size()); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3545 | } |
| 3546 | |
Douglas Gregor | eda7e54 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 3547 | void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) { |
| 3548 | if (E.isInvalid()) |
| 3549 | CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3550 | else if (getLangOpts().ObjC1) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3551 | CodeCompleteObjCInstanceMessage(S, E.get(), None, false); |
Douglas Gregor | ed0b69d | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 3552 | } |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3553 | |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3554 | /// \brief The set of properties that have already been added, referenced by |
| 3555 | /// property name. |
| 3556 | typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet; |
| 3557 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3558 | /// \brief Retrieve the container definition, if any? |
| 3559 | static ObjCContainerDecl *getContainerDef(ObjCContainerDecl *Container) { |
| 3560 | if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 3561 | if (Interface->hasDefinition()) |
| 3562 | return Interface->getDefinition(); |
| 3563 | |
| 3564 | return Interface; |
| 3565 | } |
| 3566 | |
| 3567 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 3568 | if (Protocol->hasDefinition()) |
| 3569 | return Protocol->getDefinition(); |
| 3570 | |
| 3571 | return Protocol; |
| 3572 | } |
| 3573 | return Container; |
| 3574 | } |
| 3575 | |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3576 | static void AddObjCProperties(const CodeCompletionContext &CCContext, |
| 3577 | ObjCContainerDecl *Container, |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3578 | bool AllowCategories, |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3579 | bool AllowNullaryMethods, |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3580 | DeclContext *CurContext, |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3581 | AddedPropertiesSet &AddedProperties, |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3582 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3583 | typedef CodeCompletionResult Result; |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3584 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3585 | // Retrieve the definition. |
| 3586 | Container = getContainerDef(Container); |
| 3587 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3588 | // Add properties in this container. |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 3589 | for (const auto *P : Container->instance_properties()) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 3590 | if (AddedProperties.insert(P->getIdentifier()).second) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3591 | Results.MaybeAddResult(Result(P, Results.getBasePriority(P), nullptr), |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 3592 | CurContext); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3593 | |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3594 | // Add nullary methods |
| 3595 | if (AllowNullaryMethods) { |
| 3596 | ASTContext &Context = Container->getASTContext(); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3597 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 3598 | for (auto *M : Container->methods()) { |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3599 | if (M->getSelector().isUnarySelector()) |
| 3600 | if (IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 3601 | if (AddedProperties.insert(Name).second) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3602 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3603 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3604 | AddResultTypeChunk(Context, Policy, M, CCContext.getBaseType(), |
| 3605 | Builder); |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3606 | Builder.AddTypedTextChunk( |
| 3607 | Results.getAllocator().CopyString(Name->getName())); |
| 3608 | |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 3609 | Results.MaybeAddResult(Result(Builder.TakeString(), M, |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3610 | CCP_MemberDeclaration + CCD_MethodAsProperty), |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3611 | CurContext); |
| 3612 | } |
| 3613 | } |
| 3614 | } |
| 3615 | |
| 3616 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3617 | // Add properties in referenced protocols. |
| 3618 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 3619 | for (auto *P : Protocol->protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3620 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, |
| 3621 | CurContext, AddedProperties, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3622 | } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){ |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3623 | if (AllowCategories) { |
| 3624 | // Look through categories. |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 3625 | for (auto *Cat : IFace->known_categories()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3626 | AddObjCProperties(CCContext, Cat, AllowCategories, AllowNullaryMethods, |
| 3627 | CurContext, AddedProperties, Results); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3628 | } |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 3629 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3630 | // Look through protocols. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 3631 | for (auto *I : IFace->all_referenced_protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3632 | AddObjCProperties(CCContext, I, AllowCategories, AllowNullaryMethods, |
| 3633 | CurContext, AddedProperties, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3634 | |
| 3635 | // Look in the superclass. |
| 3636 | if (IFace->getSuperClass()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3637 | AddObjCProperties(CCContext, IFace->getSuperClass(), AllowCategories, |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3638 | AllowNullaryMethods, CurContext, |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3639 | AddedProperties, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3640 | } else if (const ObjCCategoryDecl *Category |
| 3641 | = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 3642 | // Look through protocols. |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 3643 | for (auto *P : Category->protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3644 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, |
| 3645 | CurContext, AddedProperties, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3646 | } |
| 3647 | } |
| 3648 | |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 3649 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base, |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3650 | SourceLocation OpLoc, |
| 3651 | bool IsArrow) { |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 3652 | if (!Base || !CodeCompleter) |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3653 | return; |
| 3654 | |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 3655 | ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow); |
| 3656 | if (ConvertedBase.isInvalid()) |
| 3657 | return; |
| 3658 | Base = ConvertedBase.get(); |
| 3659 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3660 | typedef CodeCompletionResult Result; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3661 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3662 | QualType BaseType = Base->getType(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3663 | |
| 3664 | if (IsArrow) { |
| 3665 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 3666 | BaseType = Ptr->getPointeeType(); |
| 3667 | else if (BaseType->isObjCObjectPointerType()) |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3668 | /*Do nothing*/ ; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3669 | else |
| 3670 | return; |
| 3671 | } |
| 3672 | |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3673 | enum CodeCompletionContext::Kind contextKind; |
| 3674 | |
| 3675 | if (IsArrow) { |
| 3676 | contextKind = CodeCompletionContext::CCC_ArrowMemberAccess; |
| 3677 | } |
| 3678 | else { |
| 3679 | if (BaseType->isObjCObjectPointerType() || |
| 3680 | BaseType->isObjCObjectOrInterfaceType()) { |
| 3681 | contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess; |
| 3682 | } |
| 3683 | else { |
| 3684 | contextKind = CodeCompletionContext::CCC_DotMemberAccess; |
| 3685 | } |
| 3686 | } |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3687 | |
| 3688 | CodeCompletionContext CCContext(contextKind, BaseType); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3689 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3690 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3691 | CCContext, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3692 | &ResultBuilder::IsMember); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3693 | Results.EnterNewScope(); |
| 3694 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3695 | // Indicate that we are performing a member access, and the cv-qualifiers |
| 3696 | // for the base object type. |
| 3697 | Results.setObjectTypeQualifiers(BaseType.getQualifiers()); |
| 3698 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3699 | // Access to a C/C++ class, struct, or union. |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 3700 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3701 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3702 | LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer, |
| 3703 | CodeCompleter->includeGlobals()); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3704 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3705 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3706 | if (!Results.empty()) { |
| 3707 | // The "template" keyword can follow "->" or "." in the grammar. |
| 3708 | // However, we only want to suggest the template keyword if something |
| 3709 | // is dependent. |
| 3710 | bool IsDependent = BaseType->isDependentType(); |
| 3711 | if (!IsDependent) { |
| 3712 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 3713 | if (DeclContext *Ctx = DepScope->getEntity()) { |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3714 | IsDependent = Ctx->isDependentContext(); |
| 3715 | break; |
| 3716 | } |
| 3717 | } |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3718 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3719 | if (IsDependent) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3720 | Results.AddResult(Result("template")); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3721 | } |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3722 | } |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3723 | } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) { |
| 3724 | // Objective-C property reference. |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3725 | AddedPropertiesSet AddedProperties; |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3726 | |
| 3727 | // Add property results based on our interface. |
| 3728 | const ObjCObjectPointerType *ObjCPtr |
| 3729 | = BaseType->getAsObjCInterfacePointerType(); |
| 3730 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!"); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3731 | AddObjCProperties(CCContext, ObjCPtr->getInterfaceDecl(), true, |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3732 | /*AllowNullaryMethods=*/true, CurContext, |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3733 | AddedProperties, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3734 | |
| 3735 | // Add properties from the protocols in a qualified interface. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 3736 | for (auto *I : ObjCPtr->quals()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3737 | AddObjCProperties(CCContext, I, true, /*AllowNullaryMethods=*/true, |
| 3738 | CurContext, AddedProperties, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3739 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3740 | (!IsArrow && BaseType->isObjCObjectType())) { |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3741 | // Objective-C instance variable access. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3742 | ObjCInterfaceDecl *Class = nullptr; |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3743 | if (const ObjCObjectPointerType *ObjCPtr |
| 3744 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 3745 | Class = ObjCPtr->getInterfaceDecl(); |
| 3746 | else |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3747 | Class = BaseType->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3748 | |
| 3749 | // Add all ivars from this class and its superclasses. |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 3750 | if (Class) { |
| 3751 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3752 | Results.setFilter(&ResultBuilder::IsObjCIvar); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3753 | LookupVisibleDecls(Class, LookupMemberName, Consumer, |
| 3754 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3755 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3756 | } |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3757 | |
| 3758 | // FIXME: How do we cope with isa? |
| 3759 | |
| 3760 | Results.ExitScope(); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3761 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3762 | // Hand off the results found for code completion. |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3763 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3764 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3765 | Results.data(),Results.size()); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3766 | } |
| 3767 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3768 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { |
| 3769 | if (!CodeCompleter) |
| 3770 | return; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3771 | |
| 3772 | ResultBuilder::LookupFilter Filter = nullptr; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3773 | enum CodeCompletionContext::Kind ContextKind |
| 3774 | = CodeCompletionContext::CCC_Other; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3775 | switch ((DeclSpec::TST)TagSpec) { |
| 3776 | case DeclSpec::TST_enum: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3777 | Filter = &ResultBuilder::IsEnum; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3778 | ContextKind = CodeCompletionContext::CCC_EnumTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3779 | break; |
| 3780 | |
| 3781 | case DeclSpec::TST_union: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3782 | Filter = &ResultBuilder::IsUnion; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3783 | ContextKind = CodeCompletionContext::CCC_UnionTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3784 | break; |
| 3785 | |
| 3786 | case DeclSpec::TST_struct: |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3787 | case DeclSpec::TST_class: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3788 | case DeclSpec::TST_interface: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3789 | Filter = &ResultBuilder::IsClassOrStruct; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3790 | ContextKind = CodeCompletionContext::CCC_ClassOrStructTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3791 | break; |
| 3792 | |
| 3793 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3794 | llvm_unreachable("Unknown type specifier kind in CodeCompleteTag"); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3795 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3796 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3797 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3798 | CodeCompleter->getCodeCompletionTUInfo(), ContextKind); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3799 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 3800 | |
| 3801 | // First pass: look for tags. |
| 3802 | Results.setFilter(Filter); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3803 | LookupVisibleDecls(S, LookupTagName, Consumer, |
| 3804 | CodeCompleter->includeGlobals()); |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 3805 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3806 | if (CodeCompleter->includeGlobals()) { |
| 3807 | // Second pass: look for nested name specifiers. |
| 3808 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
| 3809 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer); |
| 3810 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3811 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3812 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3813 | Results.data(),Results.size()); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3814 | } |
| 3815 | |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3816 | void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3817 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3818 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3819 | CodeCompletionContext::CCC_TypeQualifiers); |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3820 | Results.EnterNewScope(); |
| 3821 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const)) |
| 3822 | Results.AddResult("const"); |
| 3823 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile)) |
| 3824 | Results.AddResult("volatile"); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3825 | if (getLangOpts().C99 && |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3826 | !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict)) |
| 3827 | Results.AddResult("restrict"); |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3828 | if (getLangOpts().C11 && |
| 3829 | !(DS.getTypeQualifiers() & DeclSpec::TQ_atomic)) |
| 3830 | Results.AddResult("_Atomic"); |
Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 3831 | if (getLangOpts().MSVCCompat && |
| 3832 | !(DS.getTypeQualifiers() & DeclSpec::TQ_unaligned)) |
| 3833 | Results.AddResult("__unaligned"); |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3834 | Results.ExitScope(); |
| 3835 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3836 | Results.getCompletionContext(), |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3837 | Results.data(), Results.size()); |
| 3838 | } |
| 3839 | |
Benjamin Kramer | 72dae62 | 2016-02-18 15:30:24 +0000 | [diff] [blame] | 3840 | void Sema::CodeCompleteBracketDeclarator(Scope *S) { |
| 3841 | CodeCompleteExpression(S, QualType(getASTContext().getSizeType())); |
| 3842 | } |
| 3843 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3844 | void Sema::CodeCompleteCase(Scope *S) { |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3845 | if (getCurFunction()->SwitchStack.empty() || !CodeCompleter) |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3846 | return; |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 3847 | |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3848 | SwitchStmt *Switch = getCurFunction()->SwitchStack.back(); |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 3849 | QualType type = Switch->getCond()->IgnoreImplicit()->getType(); |
| 3850 | if (!type->isEnumeralType()) { |
| 3851 | CodeCompleteExpressionData Data(type); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3852 | Data.IntegralConstantExpression = true; |
| 3853 | CodeCompleteExpression(S, Data); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3854 | return; |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3855 | } |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3856 | |
| 3857 | // Code-complete the cases of a switch statement over an enumeration type |
| 3858 | // by providing the list of |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 3859 | EnumDecl *Enum = type->castAs<EnumType>()->getDecl(); |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3860 | if (EnumDecl *Def = Enum->getDefinition()) |
| 3861 | Enum = Def; |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3862 | |
| 3863 | // Determine which enumerators we have already seen in the switch statement. |
| 3864 | // FIXME: Ideally, we would also be able to look *past* the code-completion |
| 3865 | // token, in case we are code-completing in the middle of the switch and not |
| 3866 | // at the end. However, we aren't able to do so at the moment. |
| 3867 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3868 | NestedNameSpecifier *Qualifier = nullptr; |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3869 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; |
| 3870 | SC = SC->getNextSwitchCase()) { |
| 3871 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); |
| 3872 | if (!Case) |
| 3873 | continue; |
| 3874 | |
| 3875 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); |
| 3876 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal)) |
| 3877 | if (EnumConstantDecl *Enumerator |
| 3878 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 3879 | // We look into the AST of the case statement to determine which |
| 3880 | // enumerator was named. Alternatively, we could compute the value of |
| 3881 | // the integral constant expression, then compare it against the |
| 3882 | // values of each enumerator. However, value-based approach would not |
| 3883 | // work as well with C++ templates where enumerators declared within a |
| 3884 | // template are type- and value-dependent. |
| 3885 | EnumeratorsSeen.insert(Enumerator); |
| 3886 | |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3887 | // If this is a qualified-id, keep track of the nested-name-specifier |
| 3888 | // 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] | 3889 | // |
| 3890 | // switch (TagD.getKind()) { |
| 3891 | // case TagDecl::TK_enum: |
| 3892 | // break; |
| 3893 | // case XXX |
| 3894 | // |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3895 | // At the XXX, our completions are TagDecl::TK_union, |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3896 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, |
| 3897 | // TK_struct, and TK_class. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3898 | Qualifier = DRE->getQualifier(); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3899 | } |
| 3900 | } |
| 3901 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3902 | if (getLangOpts().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3903 | // If there are no prior enumerators in C++, check whether we have to |
| 3904 | // qualify the names of the enumerators that we suggest, because they |
| 3905 | // may not be visible in this scope. |
Douglas Gregor | d3cebdb | 2012-02-01 05:02:47 +0000 | [diff] [blame] | 3906 | Qualifier = getRequiredQualification(Context, CurContext, Enum); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3907 | } |
| 3908 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3909 | // Add any enumerators that have not yet been mentioned. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3910 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3911 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3912 | CodeCompletionContext::CCC_Expression); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3913 | Results.EnterNewScope(); |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 3914 | for (auto *E : Enum->enumerators()) { |
| 3915 | if (EnumeratorsSeen.count(E)) |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3916 | continue; |
| 3917 | |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 3918 | CodeCompletionResult R(E, CCP_EnumInCase, Qualifier); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3919 | Results.AddResult(R, CurContext, nullptr, false); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3920 | } |
| 3921 | Results.ExitScope(); |
Douglas Gregor | 28556092 | 2010-04-06 20:02:15 +0000 | [diff] [blame] | 3922 | |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3923 | //We need to make sure we're setting the right context, |
| 3924 | //so only say we include macros if the code completer says we do |
| 3925 | enum CodeCompletionContext::Kind kind = CodeCompletionContext::CCC_Other; |
| 3926 | if (CodeCompleter->includeMacros()) { |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3927 | AddMacroResults(PP, Results, false); |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3928 | kind = CodeCompletionContext::CCC_OtherWithMacros; |
| 3929 | } |
| 3930 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3931 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3932 | kind, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3933 | Results.data(),Results.size()); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 3936 | static bool anyNullArguments(ArrayRef<Expr *> Args) { |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3937 | if (Args.size() && !Args.data()) |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3938 | return true; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3939 | |
| 3940 | for (unsigned I = 0; I != Args.size(); ++I) |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3941 | if (!Args[I]) |
| 3942 | return true; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3943 | |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3944 | return false; |
| 3945 | } |
| 3946 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3947 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; |
| 3948 | |
Francisco Lopes da Silva | 8cafefa | 2015-01-29 05:54:59 +0000 | [diff] [blame] | 3949 | static void mergeCandidatesWithResults(Sema &SemaRef, |
| 3950 | SmallVectorImpl<ResultCandidate> &Results, |
| 3951 | OverloadCandidateSet &CandidateSet, |
| 3952 | SourceLocation Loc) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3953 | if (!CandidateSet.empty()) { |
| 3954 | // Sort the overload candidate set by placing the best overloads first. |
| 3955 | std::stable_sort( |
| 3956 | CandidateSet.begin(), CandidateSet.end(), |
| 3957 | [&](const OverloadCandidate &X, const OverloadCandidate &Y) { |
| 3958 | return isBetterOverloadCandidate(SemaRef, X, Y, Loc); |
| 3959 | }); |
| 3960 | |
| 3961 | // Add the remaining viable overload candidates as code-completion results. |
| 3962 | for (auto &Candidate : CandidateSet) |
| 3963 | if (Candidate.Viable) |
| 3964 | Results.push_back(ResultCandidate(Candidate.Function)); |
| 3965 | } |
| 3966 | } |
| 3967 | |
| 3968 | /// \brief Get the type of the Nth parameter from a given set of overload |
| 3969 | /// candidates. |
Francisco Lopes da Silva | 8cafefa | 2015-01-29 05:54:59 +0000 | [diff] [blame] | 3970 | static QualType getParamType(Sema &SemaRef, |
| 3971 | ArrayRef<ResultCandidate> Candidates, |
| 3972 | unsigned N) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3973 | |
| 3974 | // Given the overloads 'Candidates' for a function call matching all arguments |
| 3975 | // up to N, return the type of the Nth parameter if it is the same for all |
| 3976 | // overload candidates. |
| 3977 | QualType ParamType; |
| 3978 | for (auto &Candidate : Candidates) { |
| 3979 | if (auto FType = Candidate.getFunctionType()) |
| 3980 | if (auto Proto = dyn_cast<FunctionProtoType>(FType)) |
| 3981 | if (N < Proto->getNumParams()) { |
| 3982 | if (ParamType.isNull()) |
| 3983 | ParamType = Proto->getParamType(N); |
| 3984 | else if (!SemaRef.Context.hasSameUnqualifiedType( |
| 3985 | ParamType.getNonReferenceType(), |
| 3986 | Proto->getParamType(N).getNonReferenceType())) |
| 3987 | // Otherwise return a default-constructed QualType. |
| 3988 | return QualType(); |
| 3989 | } |
| 3990 | } |
| 3991 | |
| 3992 | return ParamType; |
| 3993 | } |
| 3994 | |
Francisco Lopes da Silva | 8cafefa | 2015-01-29 05:54:59 +0000 | [diff] [blame] | 3995 | static void CodeCompleteOverloadResults(Sema &SemaRef, Scope *S, |
| 3996 | MutableArrayRef<ResultCandidate> Candidates, |
| 3997 | unsigned CurrentArg, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3998 | bool CompleteExpressionWithCurrentArg = true) { |
| 3999 | QualType ParamType; |
| 4000 | if (CompleteExpressionWithCurrentArg) |
| 4001 | ParamType = getParamType(SemaRef, Candidates, CurrentArg); |
| 4002 | |
| 4003 | if (ParamType.isNull()) |
| 4004 | SemaRef.CodeCompleteOrdinaryName(S, Sema::PCC_Expression); |
| 4005 | else |
| 4006 | SemaRef.CodeCompleteExpression(S, ParamType); |
| 4007 | |
| 4008 | if (!Candidates.empty()) |
| 4009 | SemaRef.CodeCompleter->ProcessOverloadCandidates(SemaRef, CurrentArg, |
| 4010 | Candidates.data(), |
| 4011 | Candidates.size()); |
| 4012 | } |
| 4013 | |
| 4014 | void Sema::CodeCompleteCall(Scope *S, Expr *Fn, ArrayRef<Expr *> Args) { |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4015 | if (!CodeCompleter) |
| 4016 | return; |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4017 | |
| 4018 | // When we're code-completing for a call, we fall back to ordinary |
| 4019 | // name code-completion whenever we can't produce specific |
| 4020 | // results. We may want to revisit this strategy in the future, |
| 4021 | // e.g., by merging the two kinds of results. |
| 4022 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4023 | // FIXME: Provide support for variadic template functions. |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4024 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4025 | // Ignore type-dependent call expressions entirely. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4026 | if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args) || |
| 4027 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4028 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4029 | return; |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4030 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4031 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4032 | // Build an overload candidate set based on the functions we find. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4033 | SourceLocation Loc = Fn->getExprLoc(); |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 4034 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4035 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4036 | SmallVector<ResultCandidate, 8> Results; |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 4037 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4038 | Expr *NakedFn = Fn->IgnoreParenCasts(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4039 | if (auto ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4040 | AddOverloadedCallCandidates(ULE, Args, CandidateSet, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4041 | /*PartialOverloading=*/true); |
| 4042 | else if (auto UME = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 4043 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
| 4044 | if (UME->hasExplicitTemplateArgs()) { |
| 4045 | UME->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 4046 | TemplateArgs = &TemplateArgsBuffer; |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 4047 | } |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4048 | SmallVector<Expr *, 12> ArgExprs(1, UME->getBase()); |
| 4049 | ArgExprs.append(Args.begin(), Args.end()); |
| 4050 | UnresolvedSet<8> Decls; |
| 4051 | Decls.append(UME->decls_begin(), UME->decls_end()); |
| 4052 | AddFunctionCandidates(Decls, ArgExprs, CandidateSet, TemplateArgs, |
| 4053 | /*SuppressUsedConversions=*/false, |
| 4054 | /*PartialOverloading=*/true); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4055 | } else { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4056 | FunctionDecl *FD = nullptr; |
| 4057 | if (auto MCE = dyn_cast<MemberExpr>(NakedFn)) |
| 4058 | FD = dyn_cast<FunctionDecl>(MCE->getMemberDecl()); |
| 4059 | else if (auto DRE = dyn_cast<DeclRefExpr>(NakedFn)) |
| 4060 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4061 | 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] | 4062 | if (!getLangOpts().CPlusPlus || |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4063 | !FD->getType()->getAs<FunctionProtoType>()) |
| 4064 | Results.push_back(ResultCandidate(FD)); |
| 4065 | else |
| 4066 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, FD->getAccess()), |
| 4067 | Args, CandidateSet, |
| 4068 | /*SuppressUsedConversions=*/false, |
| 4069 | /*PartialOverloading=*/true); |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4070 | |
| 4071 | } else if (auto DC = NakedFn->getType()->getAsCXXRecordDecl()) { |
| 4072 | // If expression's type is CXXRecordDecl, it may overload the function |
| 4073 | // 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] | 4074 | // A complete type is needed to lookup for member function call operators. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4075 | if (isCompleteType(Loc, NakedFn->getType())) { |
Francisco Lopes da Silva | a349a8a | 2015-01-25 08:47:59 +0000 | [diff] [blame] | 4076 | DeclarationName OpName = Context.DeclarationNames |
| 4077 | .getCXXOperatorName(OO_Call); |
| 4078 | LookupResult R(*this, OpName, Loc, LookupOrdinaryName); |
| 4079 | LookupQualifiedName(R, DC); |
| 4080 | R.suppressDiagnostics(); |
| 4081 | SmallVector<Expr *, 12> ArgExprs(1, NakedFn); |
| 4082 | ArgExprs.append(Args.begin(), Args.end()); |
| 4083 | AddFunctionCandidates(R.asUnresolvedSet(), ArgExprs, CandidateSet, |
| 4084 | /*ExplicitArgs=*/nullptr, |
| 4085 | /*SuppressUsedConversions=*/false, |
| 4086 | /*PartialOverloading=*/true); |
| 4087 | } |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4088 | } else { |
| 4089 | // Lastly we check whether expression's type is function pointer or |
| 4090 | // function. |
| 4091 | QualType T = NakedFn->getType(); |
| 4092 | if (!T->getPointeeType().isNull()) |
| 4093 | T = T->getPointeeType(); |
| 4094 | |
| 4095 | if (auto FP = T->getAs<FunctionProtoType>()) { |
| 4096 | if (!TooManyArguments(FP->getNumParams(), Args.size(), |
Francisco Lopes da Silva | 62a9a4f | 2015-01-23 13:17:51 +0000 | [diff] [blame] | 4097 | /*PartialOverloading=*/true) || |
| 4098 | FP->isVariadic()) |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4099 | Results.push_back(ResultCandidate(FP)); |
| 4100 | } else if (auto FT = T->getAs<FunctionType>()) |
Francisco Lopes da Silva | 62a9a4f | 2015-01-23 13:17:51 +0000 | [diff] [blame] | 4101 | // 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] | 4102 | Results.push_back(ResultCandidate(FT)); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4103 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4104 | } |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4105 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4106 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); |
| 4107 | CodeCompleteOverloadResults(*this, S, Results, Args.size(), |
| 4108 | !CandidateSet.empty()); |
| 4109 | } |
| 4110 | |
| 4111 | void Sema::CodeCompleteConstructor(Scope *S, QualType Type, SourceLocation Loc, |
| 4112 | ArrayRef<Expr *> Args) { |
| 4113 | if (!CodeCompleter) |
| 4114 | return; |
| 4115 | |
| 4116 | // A complete type is needed to lookup for constructors. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4117 | if (!isCompleteType(Loc, Type)) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4118 | return; |
| 4119 | |
Argyrios Kyrtzidis | ee1d76f | 2015-03-13 07:39:30 +0000 | [diff] [blame] | 4120 | CXXRecordDecl *RD = Type->getAsCXXRecordDecl(); |
| 4121 | if (!RD) { |
| 4122 | CodeCompleteExpression(S, Type); |
| 4123 | return; |
| 4124 | } |
| 4125 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4126 | // FIXME: Provide support for member initializers. |
| 4127 | // FIXME: Provide support for variadic template constructors. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4128 | |
| 4129 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
| 4130 | |
Argyrios Kyrtzidis | ee1d76f | 2015-03-13 07:39:30 +0000 | [diff] [blame] | 4131 | for (auto C : LookupConstructors(RD)) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4132 | if (auto FD = dyn_cast<FunctionDecl>(C)) { |
| 4133 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, C->getAccess()), |
| 4134 | Args, CandidateSet, |
| 4135 | /*SuppressUsedConversions=*/false, |
| 4136 | /*PartialOverloading=*/true); |
| 4137 | } else if (auto FTD = dyn_cast<FunctionTemplateDecl>(C)) { |
| 4138 | AddTemplateOverloadCandidate(FTD, |
| 4139 | DeclAccessPair::make(FTD, C->getAccess()), |
| 4140 | /*ExplicitTemplateArgs=*/nullptr, |
| 4141 | Args, CandidateSet, |
| 4142 | /*SuppressUsedConversions=*/false, |
| 4143 | /*PartialOverloading=*/true); |
| 4144 | } |
| 4145 | } |
| 4146 | |
| 4147 | SmallVector<ResultCandidate, 8> Results; |
| 4148 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); |
| 4149 | CodeCompleteOverloadResults(*this, S, Results, Args.size()); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4150 | } |
| 4151 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4152 | void Sema::CodeCompleteInitializer(Scope *S, Decl *D) { |
| 4153 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4154 | if (!VD) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4155 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4156 | return; |
| 4157 | } |
| 4158 | |
| 4159 | CodeCompleteExpression(S, VD->getType()); |
| 4160 | } |
| 4161 | |
| 4162 | void Sema::CodeCompleteReturn(Scope *S) { |
| 4163 | QualType ResultType; |
| 4164 | if (isa<BlockDecl>(CurContext)) { |
| 4165 | if (BlockScopeInfo *BSI = getCurBlock()) |
| 4166 | ResultType = BSI->ReturnType; |
| 4167 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4168 | ResultType = Function->getReturnType(); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4169 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4170 | ResultType = Method->getReturnType(); |
| 4171 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4172 | if (ResultType.isNull()) |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4173 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4174 | else |
| 4175 | CodeCompleteExpression(S, ResultType); |
| 4176 | } |
| 4177 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4178 | void Sema::CodeCompleteAfterIf(Scope *S) { |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4179 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4180 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4181 | mapCodeCompletionContext(*this, PCC_Statement)); |
| 4182 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 4183 | Results.EnterNewScope(); |
| 4184 | |
| 4185 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4186 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4187 | CodeCompleter->includeGlobals()); |
| 4188 | |
| 4189 | AddOrdinaryNameResults(PCC_Statement, S, *this, Results); |
| 4190 | |
| 4191 | // "else" block |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4192 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4193 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4194 | Builder.AddTypedTextChunk("else"); |
Douglas Gregor | 3a5d6c2 | 2012-02-16 17:49:04 +0000 | [diff] [blame] | 4195 | if (Results.includeCodePatterns()) { |
| 4196 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4197 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4198 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4199 | Builder.AddPlaceholderChunk("statements"); |
| 4200 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4201 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4202 | } |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4203 | Results.AddResult(Builder.TakeString()); |
| 4204 | |
| 4205 | // "else if" block |
| 4206 | Builder.AddTypedTextChunk("else"); |
| 4207 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4208 | Builder.AddTextChunk("if"); |
| 4209 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4210 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4211 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4212 | Builder.AddPlaceholderChunk("condition"); |
| 4213 | else |
| 4214 | Builder.AddPlaceholderChunk("expression"); |
| 4215 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 3a5d6c2 | 2012-02-16 17:49:04 +0000 | [diff] [blame] | 4216 | if (Results.includeCodePatterns()) { |
| 4217 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4218 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4219 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4220 | Builder.AddPlaceholderChunk("statements"); |
| 4221 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4222 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4223 | } |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4224 | Results.AddResult(Builder.TakeString()); |
| 4225 | |
| 4226 | Results.ExitScope(); |
| 4227 | |
| 4228 | if (S->getFnParent()) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 4229 | AddPrettyFunctionResults(getLangOpts(), Results); |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4230 | |
| 4231 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 4232 | AddMacroResults(PP, Results, false); |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4233 | |
| 4234 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4235 | Results.data(),Results.size()); |
| 4236 | } |
| 4237 | |
Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 4238 | void Sema::CodeCompleteAssignmentRHS(Scope *S, Expr *LHS) { |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4239 | if (LHS) |
| 4240 | CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType()); |
| 4241 | else |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4242 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4243 | } |
| 4244 | |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4245 | void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4246 | bool EnteringContext) { |
| 4247 | if (!SS.getScopeRep() || !CodeCompleter) |
| 4248 | return; |
| 4249 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4250 | DeclContext *Ctx = computeDeclContext(SS, EnteringContext); |
| 4251 | if (!Ctx) |
| 4252 | return; |
Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 4253 | |
| 4254 | // Try to instantiate any non-dependent declaration contexts before |
| 4255 | // we look in them. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4256 | if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx)) |
Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 4257 | return; |
| 4258 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4259 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4260 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4261 | CodeCompletionContext::CCC_Name); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4262 | Results.EnterNewScope(); |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4263 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4264 | // The "template" keyword can follow "::" in the grammar, but only |
| 4265 | // put it into the grammar if the nested-name-specifier is dependent. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4266 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4267 | if (!Results.empty() && NNS->isDependent()) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4268 | Results.AddResult("template"); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4269 | |
| 4270 | // Add calls to overridden virtual functions, if there are any. |
| 4271 | // |
| 4272 | // FIXME: This isn't wonderful, because we don't know whether we're actually |
| 4273 | // in a context that permits expressions. This is a general issue with |
| 4274 | // qualified-id completions. |
| 4275 | if (!EnteringContext) |
| 4276 | MaybeAddOverrideCalls(*this, Ctx, Results); |
| 4277 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4278 | |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4279 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4280 | LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer); |
| 4281 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4282 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | c1679ec | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 4283 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4284 | Results.data(),Results.size()); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4285 | } |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4286 | |
| 4287 | void Sema::CodeCompleteUsing(Scope *S) { |
| 4288 | if (!CodeCompleter) |
| 4289 | return; |
| 4290 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4291 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4292 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4293 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
| 4294 | &ResultBuilder::IsNestedNameSpecifier); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4295 | Results.EnterNewScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4296 | |
| 4297 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 4298 | if (!S->isClassScope()) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4299 | Results.AddResult(CodeCompletionResult("namespace")); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4300 | |
| 4301 | // After "using", we can see anything that would start a |
| 4302 | // nested-name-specifier. |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4303 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4304 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4305 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4306 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4307 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4308 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4309 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4310 | Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | void Sema::CodeCompleteUsingDirective(Scope *S) { |
| 4314 | if (!CodeCompleter) |
| 4315 | return; |
| 4316 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4317 | // After "using namespace", we expect to see a namespace name or namespace |
| 4318 | // alias. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4319 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4320 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4321 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4322 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4323 | Results.EnterNewScope(); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4324 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4325 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4326 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4327 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4328 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4329 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4330 | Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
| 4333 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { |
| 4334 | if (!CodeCompleter) |
| 4335 | return; |
| 4336 | |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 4337 | DeclContext *Ctx = S->getEntity(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4338 | if (!S->getParent()) |
| 4339 | Ctx = Context.getTranslationUnitDecl(); |
| 4340 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4341 | bool SuppressedGlobalResults |
| 4342 | = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx); |
| 4343 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4344 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4345 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4346 | SuppressedGlobalResults |
| 4347 | ? CodeCompletionContext::CCC_Namespace |
| 4348 | : CodeCompletionContext::CCC_Other, |
| 4349 | &ResultBuilder::IsNamespace); |
| 4350 | |
| 4351 | if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4352 | // We only want to see those namespaces that have already been defined |
| 4353 | // within this scope, because its likely that the user is creating an |
| 4354 | // extended namespace declaration. Keep track of the most recent |
| 4355 | // definition of each namespace. |
| 4356 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
| 4357 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
| 4358 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); |
| 4359 | NS != NSEnd; ++NS) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4360 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4361 | |
| 4362 | // Add the most recent definition (or extended definition) of each |
| 4363 | // namespace to the list of results. |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4364 | Results.EnterNewScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4365 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 4366 | NS = OrigToLatest.begin(), |
| 4367 | NSEnd = OrigToLatest.end(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4368 | NS != NSEnd; ++NS) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 4369 | Results.AddResult(CodeCompletionResult( |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4370 | NS->second, Results.getBasePriority(NS->second), |
| 4371 | nullptr), |
| 4372 | CurContext, nullptr, false); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4373 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4376 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4377 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4378 | Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4379 | } |
| 4380 | |
| 4381 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { |
| 4382 | if (!CodeCompleter) |
| 4383 | return; |
| 4384 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4385 | // After "namespace", we expect to see a namespace or alias. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4386 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4387 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4388 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4389 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4390 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4391 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4392 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4393 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4394 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4395 | Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4396 | } |
| 4397 | |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 4398 | void Sema::CodeCompleteOperatorName(Scope *S) { |
| 4399 | if (!CodeCompleter) |
| 4400 | return; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4401 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4402 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4403 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4404 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4405 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4406 | &ResultBuilder::IsType); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4407 | Results.EnterNewScope(); |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 4408 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4409 | // Add the names of overloadable operators. |
| 4410 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 4411 | if (std::strcmp(Spelling, "?")) \ |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4412 | Results.AddResult(Result(Spelling)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4413 | #include "clang/Basic/OperatorKinds.def" |
| 4414 | |
| 4415 | // Add any type names visible from the current scope |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 4416 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4417 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4418 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4419 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4420 | |
| 4421 | // Add any type specifiers |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4422 | AddTypeSpecifierResults(getLangOpts(), Results); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4423 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4424 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4425 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4426 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4427 | Results.data(),Results.size()); |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 4428 | } |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4429 | |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4430 | void Sema::CodeCompleteConstructorInitializer( |
| 4431 | Decl *ConstructorD, |
| 4432 | ArrayRef <CXXCtorInitializer *> Initializers) { |
Benjamin Kramer | a4f8df0 | 2015-07-09 15:31:10 +0000 | [diff] [blame] | 4433 | if (!ConstructorD) |
| 4434 | return; |
| 4435 | |
| 4436 | AdjustDeclIfTemplate(ConstructorD); |
| 4437 | |
| 4438 | CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4439 | if (!Constructor) |
| 4440 | return; |
| 4441 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4442 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4443 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4444 | CodeCompletionContext::CCC_PotentiallyQualifiedName); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4445 | Results.EnterNewScope(); |
| 4446 | |
| 4447 | // Fill in any already-initialized fields or base classes. |
| 4448 | llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields; |
| 4449 | llvm::SmallPtrSet<CanQualType, 4> InitializedBases; |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4450 | for (unsigned I = 0, E = Initializers.size(); I != E; ++I) { |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4451 | if (Initializers[I]->isBaseInitializer()) |
| 4452 | InitializedBases.insert( |
| 4453 | Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0))); |
| 4454 | else |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4455 | InitializedFields.insert(cast<FieldDecl>( |
| 4456 | Initializers[I]->getAnyMember())); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4457 | } |
| 4458 | |
| 4459 | // Add completions for base classes. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4460 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4461 | Results.getCodeCompletionTUInfo()); |
Benjamin Kramer | a4f8df0 | 2015-07-09 15:31:10 +0000 | [diff] [blame] | 4462 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4463 | bool SawLastInitializer = Initializers.empty(); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4464 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4465 | for (const auto &Base : ClassDecl->bases()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4466 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) |
| 4467 | .second) { |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4468 | SawLastInitializer |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4469 | = !Initializers.empty() && |
| 4470 | Initializers.back()->isBaseInitializer() && |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4471 | Context.hasSameUnqualifiedType(Base.getType(), |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4472 | QualType(Initializers.back()->getBaseClass(), 0)); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4473 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4474 | } |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4475 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4476 | Builder.AddTypedTextChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4477 | Results.getAllocator().CopyString( |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4478 | Base.getType().getAsString(Policy))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4479 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4480 | Builder.AddPlaceholderChunk("args"); |
| 4481 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4482 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4483 | SawLastInitializer? CCP_NextInitializer |
| 4484 | : CCP_MemberDeclaration)); |
| 4485 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | // Add completions for virtual base classes. |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4489 | for (const auto &Base : ClassDecl->vbases()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4490 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) |
| 4491 | .second) { |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4492 | SawLastInitializer |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4493 | = !Initializers.empty() && |
| 4494 | Initializers.back()->isBaseInitializer() && |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4495 | Context.hasSameUnqualifiedType(Base.getType(), |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4496 | QualType(Initializers.back()->getBaseClass(), 0)); |
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 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4500 | Builder.AddTypedTextChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4501 | Builder.getAllocator().CopyString( |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4502 | Base.getType().getAsString(Policy))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4503 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4504 | Builder.AddPlaceholderChunk("args"); |
| 4505 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4506 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4507 | SawLastInitializer? CCP_NextInitializer |
| 4508 | : CCP_MemberDeclaration)); |
| 4509 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4510 | } |
| 4511 | |
| 4512 | // Add completions for members. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4513 | for (auto *Field : ClassDecl->fields()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4514 | if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl())) |
| 4515 | .second) { |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4516 | SawLastInitializer |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4517 | = !Initializers.empty() && |
| 4518 | Initializers.back()->isAnyMemberInitializer() && |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4519 | Initializers.back()->getAnyMember() == Field; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4520 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4521 | } |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4522 | |
| 4523 | if (!Field->getDeclName()) |
| 4524 | continue; |
| 4525 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4526 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4527 | Field->getIdentifier()->getName())); |
| 4528 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4529 | Builder.AddPlaceholderChunk("args"); |
| 4530 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4531 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4532 | SawLastInitializer? CCP_NextInitializer |
Douglas Gregor | f3af311 | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 4533 | : CCP_MemberDeclaration, |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 4534 | CXCursor_MemberRef, |
| 4535 | CXAvailability_Available, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4536 | Field)); |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4537 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4538 | } |
| 4539 | Results.ExitScope(); |
| 4540 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4541 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4542 | Results.data(), Results.size()); |
| 4543 | } |
| 4544 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4545 | /// \brief Determine whether this scope denotes a namespace. |
| 4546 | static bool isNamespaceScope(Scope *S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 4547 | DeclContext *DC = S->getEntity(); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4548 | if (!DC) |
| 4549 | return false; |
| 4550 | |
| 4551 | return DC->isFileContext(); |
| 4552 | } |
| 4553 | |
| 4554 | void Sema::CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro, |
| 4555 | bool AfterAmpersand) { |
| 4556 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4557 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4558 | CodeCompletionContext::CCC_Other); |
| 4559 | Results.EnterNewScope(); |
| 4560 | |
| 4561 | // Note what has already been captured. |
| 4562 | llvm::SmallPtrSet<IdentifierInfo *, 4> Known; |
| 4563 | bool IncludedThis = false; |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 4564 | for (const auto &C : Intro.Captures) { |
| 4565 | if (C.Kind == LCK_This) { |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4566 | IncludedThis = true; |
| 4567 | continue; |
| 4568 | } |
| 4569 | |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 4570 | Known.insert(C.Id); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | // Look for other capturable variables. |
| 4574 | for (; S && !isNamespaceScope(S); S = S->getParent()) { |
Aaron Ballman | 35c5495 | 2014-03-17 16:55:25 +0000 | [diff] [blame] | 4575 | for (const auto *D : S->decls()) { |
| 4576 | const auto *Var = dyn_cast<VarDecl>(D); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4577 | if (!Var || |
| 4578 | !Var->hasLocalStorage() || |
| 4579 | Var->hasAttr<BlocksAttr>()) |
| 4580 | continue; |
| 4581 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4582 | if (Known.insert(Var->getIdentifier()).second) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 4583 | Results.AddResult(CodeCompletionResult(Var, CCP_LocalDeclaration), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4584 | CurContext, nullptr, false); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4585 | } |
| 4586 | } |
| 4587 | |
| 4588 | // Add 'this', if it would be valid. |
| 4589 | if (!IncludedThis && !AfterAmpersand && Intro.Default != LCD_ByCopy) |
| 4590 | addThisCompletion(*this, Results); |
| 4591 | |
| 4592 | Results.ExitScope(); |
| 4593 | |
| 4594 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4595 | Results.data(), Results.size()); |
| 4596 | } |
| 4597 | |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4598 | /// Macro that optionally prepends an "@" to the string literal passed in via |
| 4599 | /// Keyword, depending on whether NeedAt is true or false. |
| 4600 | #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) ((NeedAt)? "@" Keyword : Keyword) |
| 4601 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4602 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4603 | ResultBuilder &Results, |
| 4604 | bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4605 | typedef CodeCompletionResult Result; |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4606 | // Since we have an implementation, we can end it. |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4607 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4608 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4609 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4610 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4611 | if (LangOpts.ObjC2) { |
| 4612 | // @dynamic |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4613 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"dynamic")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4614 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4615 | Builder.AddPlaceholderChunk("property"); |
| 4616 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4617 | |
| 4618 | // @synthesize |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4619 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synthesize")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4620 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4621 | Builder.AddPlaceholderChunk("property"); |
| 4622 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4623 | } |
| 4624 | } |
| 4625 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4626 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4627 | ResultBuilder &Results, |
| 4628 | bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4629 | typedef CodeCompletionResult Result; |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4630 | |
| 4631 | // Since we have an interface or protocol, we can end it. |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4632 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4633 | |
| 4634 | if (LangOpts.ObjC2) { |
| 4635 | // @property |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4636 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"property"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4637 | |
| 4638 | // @required |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4639 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"required"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4640 | |
| 4641 | // @optional |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4642 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"optional"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4643 | } |
| 4644 | } |
| 4645 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4646 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4647 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4648 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4649 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4650 | |
| 4651 | // @class name ; |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4652 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"class")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4653 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4654 | Builder.AddPlaceholderChunk("name"); |
| 4655 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4656 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4657 | if (Results.includeCodePatterns()) { |
| 4658 | // @interface name |
| 4659 | // FIXME: Could introduce the whole pattern, including superclasses and |
| 4660 | // such. |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4661 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"interface")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4662 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4663 | Builder.AddPlaceholderChunk("class"); |
| 4664 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4665 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4666 | // @protocol name |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4667 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4668 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4669 | Builder.AddPlaceholderChunk("protocol"); |
| 4670 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4671 | |
| 4672 | // @implementation name |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4673 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"implementation")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4674 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4675 | Builder.AddPlaceholderChunk("class"); |
| 4676 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4677 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4678 | |
| 4679 | // @compatibility_alias name |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4680 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"compatibility_alias")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4681 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4682 | Builder.AddPlaceholderChunk("alias"); |
| 4683 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4684 | Builder.AddPlaceholderChunk("class"); |
| 4685 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 61e3681 | 2013-03-07 23:26:24 +0000 | [diff] [blame] | 4686 | |
| 4687 | if (Results.getSema().getLangOpts().Modules) { |
| 4688 | // @import name |
| 4689 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "import")); |
| 4690 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4691 | Builder.AddPlaceholderChunk("module"); |
| 4692 | Results.AddResult(Result(Builder.TakeString())); |
| 4693 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4696 | void Sema::CodeCompleteObjCAtDirective(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4697 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4698 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4699 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4700 | Results.EnterNewScope(); |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4701 | if (isa<ObjCImplDecl>(CurContext)) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4702 | AddObjCImplementationResults(getLangOpts(), Results, false); |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4703 | else if (CurContext->isObjCContainer()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4704 | AddObjCInterfaceResults(getLangOpts(), Results, false); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4705 | else |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4706 | AddObjCTopLevelResults(Results, false); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4707 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4708 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4709 | CodeCompletionContext::CCC_Other, |
| 4710 | Results.data(),Results.size()); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4711 | } |
| 4712 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4713 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4714 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4715 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4716 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4717 | |
| 4718 | // @encode ( type-name ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 4719 | const char *EncodeType = "char[]"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4720 | if (Results.getSema().getLangOpts().CPlusPlus || |
| 4721 | Results.getSema().getLangOpts().ConstStrings) |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4722 | EncodeType = "const char[]"; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 4723 | Builder.AddResultTypeChunk(EncodeType); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4724 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"encode")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4725 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4726 | Builder.AddPlaceholderChunk("type-name"); |
| 4727 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4728 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4729 | |
| 4730 | // @protocol ( protocol-name ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 4731 | Builder.AddResultTypeChunk("Protocol *"); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4732 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4733 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4734 | Builder.AddPlaceholderChunk("protocol-name"); |
| 4735 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4736 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4737 | |
| 4738 | // @selector ( selector ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 4739 | Builder.AddResultTypeChunk("SEL"); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4740 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"selector")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4741 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4742 | Builder.AddPlaceholderChunk("selector"); |
| 4743 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4744 | Results.AddResult(Result(Builder.TakeString())); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4745 | |
| 4746 | // @"string" |
| 4747 | Builder.AddResultTypeChunk("NSString *"); |
| 4748 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"\"")); |
| 4749 | Builder.AddPlaceholderChunk("string"); |
| 4750 | Builder.AddTextChunk("\""); |
| 4751 | Results.AddResult(Result(Builder.TakeString())); |
| 4752 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 4753 | // @[objects, ...] |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4754 | Builder.AddResultTypeChunk("NSArray *"); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4755 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"[")); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4756 | Builder.AddPlaceholderChunk("objects, ..."); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4757 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 4758 | Results.AddResult(Result(Builder.TakeString())); |
| 4759 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 4760 | // @{key : object, ...} |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4761 | Builder.AddResultTypeChunk("NSDictionary *"); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4762 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"{")); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4763 | Builder.AddPlaceholderChunk("key"); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4764 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 4765 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4766 | Builder.AddPlaceholderChunk("object, ..."); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4767 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4768 | Results.AddResult(Result(Builder.TakeString())); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4769 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 4770 | // @(expression) |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4771 | Builder.AddResultTypeChunk("id"); |
| 4772 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "(")); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4773 | Builder.AddPlaceholderChunk("expression"); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4774 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4775 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4776 | } |
| 4777 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4778 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4779 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4780 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4781 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4782 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4783 | if (Results.includeCodePatterns()) { |
| 4784 | // @try { statements } @catch ( declaration ) { statements } @finally |
| 4785 | // { statements } |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4786 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"try")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4787 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4788 | Builder.AddPlaceholderChunk("statements"); |
| 4789 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4790 | Builder.AddTextChunk("@catch"); |
| 4791 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4792 | Builder.AddPlaceholderChunk("parameter"); |
| 4793 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4794 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4795 | Builder.AddPlaceholderChunk("statements"); |
| 4796 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4797 | Builder.AddTextChunk("@finally"); |
| 4798 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4799 | Builder.AddPlaceholderChunk("statements"); |
| 4800 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4801 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4802 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4803 | |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4804 | // @throw |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4805 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"throw")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4806 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4807 | Builder.AddPlaceholderChunk("expression"); |
| 4808 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4809 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4810 | if (Results.includeCodePatterns()) { |
| 4811 | // @synchronized ( expression ) { statements } |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4812 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synchronized")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4813 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4814 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4815 | Builder.AddPlaceholderChunk("expression"); |
| 4816 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4817 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4818 | Builder.AddPlaceholderChunk("statements"); |
| 4819 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4820 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4821 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4822 | } |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4823 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4824 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4825 | ResultBuilder &Results, |
| 4826 | bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4827 | typedef CodeCompletionResult Result; |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4828 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"private"))); |
| 4829 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"protected"))); |
| 4830 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"public"))); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4831 | if (LangOpts.ObjC2) |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4832 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"package"))); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4833 | } |
| 4834 | |
| 4835 | void Sema::CodeCompleteObjCAtVisibility(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4836 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4837 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4838 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4839 | Results.EnterNewScope(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4840 | AddObjCVisibilityResults(getLangOpts(), Results, false); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4841 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4842 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4843 | CodeCompletionContext::CCC_Other, |
| 4844 | Results.data(),Results.size()); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4845 | } |
| 4846 | |
| 4847 | void Sema::CodeCompleteObjCAtStatement(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4848 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4849 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4850 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4851 | Results.EnterNewScope(); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4852 | AddObjCStatementResults(Results, false); |
| 4853 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4854 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4855 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4856 | CodeCompletionContext::CCC_Other, |
| 4857 | Results.data(),Results.size()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4858 | } |
| 4859 | |
| 4860 | void Sema::CodeCompleteObjCAtExpression(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4861 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4862 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4863 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4864 | Results.EnterNewScope(); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4865 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4866 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4867 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4868 | CodeCompletionContext::CCC_Other, |
| 4869 | Results.data(),Results.size()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4870 | } |
| 4871 | |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4872 | /// \brief Determine whether the addition of the given flag to an Objective-C |
| 4873 | /// property's attributes will cause a conflict. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4874 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4875 | // Check if we've already added this flag. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4876 | if (Attributes & NewFlag) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4877 | return true; |
| 4878 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4879 | Attributes |= NewFlag; |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4880 | |
| 4881 | // Check for collisions with "readonly". |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4882 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 4883 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4884 | return true; |
| 4885 | |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 4886 | // Check for more than one of { assign, copy, retain, strong, weak }. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4887 | unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4888 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4889 | ObjCDeclSpec::DQ_PR_copy | |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 4890 | ObjCDeclSpec::DQ_PR_retain | |
| 4891 | ObjCDeclSpec::DQ_PR_strong | |
| 4892 | ObjCDeclSpec::DQ_PR_weak); |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4893 | if (AssignCopyRetMask && |
| 4894 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4895 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained && |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4896 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4897 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain && |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 4898 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong && |
| 4899 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_weak) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4900 | return true; |
| 4901 | |
| 4902 | return false; |
| 4903 | } |
| 4904 | |
Douglas Gregor | 36029f4 | 2009-11-18 23:08:07 +0000 | [diff] [blame] | 4905 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4906 | if (!CodeCompleter) |
| 4907 | return; |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4908 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4909 | unsigned Attributes = ODS.getPropertyAttributes(); |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4910 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4911 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4912 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4913 | CodeCompletionContext::CCC_Other); |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4914 | Results.EnterNewScope(); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4915 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4916 | Results.AddResult(CodeCompletionResult("readonly")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4917 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4918 | Results.AddResult(CodeCompletionResult("assign")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4919 | if (!ObjCPropertyFlagConflicts(Attributes, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4920 | ObjCDeclSpec::DQ_PR_unsafe_unretained)) |
| 4921 | Results.AddResult(CodeCompletionResult("unsafe_unretained")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4922 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4923 | Results.AddResult(CodeCompletionResult("readwrite")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4924 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4925 | Results.AddResult(CodeCompletionResult("retain")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4926 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4927 | Results.AddResult(CodeCompletionResult("strong")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4928 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4929 | Results.AddResult(CodeCompletionResult("copy")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4930 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4931 | Results.AddResult(CodeCompletionResult("nonatomic")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4932 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic)) |
Fariborz Jahanian | 1c2d29e | 2011-06-11 17:14:27 +0000 | [diff] [blame] | 4933 | Results.AddResult(CodeCompletionResult("atomic")); |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 4934 | |
| 4935 | // 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] | 4936 | if (getLangOpts().ObjCWeak || getLangOpts().getGC() != LangOptions::NonGC) |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4937 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_weak)) |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 4938 | Results.AddResult(CodeCompletionResult("weak")); |
| 4939 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4940 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4941 | CodeCompletionBuilder Setter(Results.getAllocator(), |
| 4942 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4943 | Setter.AddTypedTextChunk("setter"); |
Argyrios Kyrtzidis | 7bbb881 | 2014-02-20 07:55:15 +0000 | [diff] [blame] | 4944 | Setter.AddTextChunk("="); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4945 | Setter.AddPlaceholderChunk("method"); |
| 4946 | Results.AddResult(CodeCompletionResult(Setter.TakeString())); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 4947 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4948 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4949 | CodeCompletionBuilder Getter(Results.getAllocator(), |
| 4950 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4951 | Getter.AddTypedTextChunk("getter"); |
Argyrios Kyrtzidis | 7bbb881 | 2014-02-20 07:55:15 +0000 | [diff] [blame] | 4952 | Getter.AddTextChunk("="); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4953 | Getter.AddPlaceholderChunk("method"); |
| 4954 | Results.AddResult(CodeCompletionResult(Getter.TakeString())); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 4955 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 4956 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nullability)) { |
| 4957 | Results.AddResult(CodeCompletionResult("nonnull")); |
| 4958 | Results.AddResult(CodeCompletionResult("nullable")); |
| 4959 | Results.AddResult(CodeCompletionResult("null_unspecified")); |
| 4960 | Results.AddResult(CodeCompletionResult("null_resettable")); |
| 4961 | } |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4962 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4963 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4964 | CodeCompletionContext::CCC_Other, |
| 4965 | Results.data(),Results.size()); |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4966 | } |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4967 | |
James Dennett | f124387 | 2012-06-17 05:33:25 +0000 | [diff] [blame] | 4968 | /// \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] | 4969 | /// via code completion. |
| 4970 | enum ObjCMethodKind { |
Dmitri Gribenko | 4280e5c | 2012-06-08 23:13:42 +0000 | [diff] [blame] | 4971 | MK_Any, ///< Any kind of method, provided it means other specified criteria. |
| 4972 | MK_ZeroArgSelector, ///< Zero-argument (unary) selector. |
| 4973 | MK_OneArgSelector ///< One-argument selector. |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4974 | }; |
| 4975 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4976 | static bool isAcceptableObjCSelector(Selector Sel, |
| 4977 | ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 4978 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4979 | bool AllowSameLength = true) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 4980 | unsigned NumSelIdents = SelIdents.size(); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4981 | if (NumSelIdents > Sel.getNumArgs()) |
| 4982 | return false; |
| 4983 | |
| 4984 | switch (WantKind) { |
| 4985 | case MK_Any: break; |
| 4986 | case MK_ZeroArgSelector: return Sel.isUnarySelector(); |
| 4987 | case MK_OneArgSelector: return Sel.getNumArgs() == 1; |
| 4988 | } |
| 4989 | |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4990 | if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs()) |
| 4991 | return false; |
| 4992 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4993 | for (unsigned I = 0; I != NumSelIdents; ++I) |
| 4994 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) |
| 4995 | return false; |
| 4996 | |
| 4997 | return true; |
| 4998 | } |
| 4999 | |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5000 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, |
| 5001 | ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5002 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5003 | bool AllowSameLength = true) { |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5004 | return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5005 | AllowSameLength); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5006 | } |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5007 | |
| 5008 | namespace { |
| 5009 | /// \brief A set of selectors, which is used to avoid introducing multiple |
| 5010 | /// completions with the same selector into the result set. |
| 5011 | typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet; |
| 5012 | } |
| 5013 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5014 | /// \brief Add all of the Objective-C methods in the given Objective-C |
| 5015 | /// container to the set of results. |
| 5016 | /// |
| 5017 | /// The container will be a class, protocol, category, or implementation of |
| 5018 | /// any of the above. This mether will recurse to include methods from |
| 5019 | /// the superclasses of classes along with their categories, protocols, and |
| 5020 | /// implementations. |
| 5021 | /// |
| 5022 | /// \param Container the container in which we'll look to find methods. |
| 5023 | /// |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5024 | /// \param WantInstanceMethods Whether to add instance methods (only); if |
| 5025 | /// false, this routine will add factory methods (only). |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5026 | /// |
| 5027 | /// \param CurContext the context in which we're performing the lookup that |
| 5028 | /// finds methods. |
| 5029 | /// |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5030 | /// \param AllowSameLength Whether we allow a method to be added to the list |
| 5031 | /// when it has the same number of parameters as we have selector identifiers. |
| 5032 | /// |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5033 | /// \param Results the structure into which we'll add results. |
| 5034 | static void AddObjCMethods(ObjCContainerDecl *Container, |
| 5035 | bool WantInstanceMethods, |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5036 | ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5037 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5038 | DeclContext *CurContext, |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5039 | VisitedSelectorSet &Selectors, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5040 | bool AllowSameLength, |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5041 | ResultBuilder &Results, |
| 5042 | bool InOriginalClass = true) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5043 | typedef CodeCompletionResult Result; |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 5044 | Container = getContainerDef(Container); |
Douglas Gregor | 41778c3 | 2013-01-30 06:58:39 +0000 | [diff] [blame] | 5045 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); |
| 5046 | bool isRootClass = IFace && !IFace->getSuperClass(); |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 5047 | for (auto *M : Container->methods()) { |
Douglas Gregor | 41778c3 | 2013-01-30 06:58:39 +0000 | [diff] [blame] | 5048 | // The instance methods on the root class can be messaged via the |
| 5049 | // metaclass. |
| 5050 | if (M->isInstanceMethod() == WantInstanceMethods || |
| 5051 | (isRootClass && !WantInstanceMethods)) { |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5052 | // Check whether the selector identifiers we've been given are a |
| 5053 | // subset of the identifiers for this particular method. |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 5054 | if (!isAcceptableObjCMethod(M, WantKind, SelIdents, AllowSameLength)) |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5055 | continue; |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5056 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 5057 | if (!Selectors.insert(M->getSelector()).second) |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5058 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5059 | |
| 5060 | Result R = Result(M, Results.getBasePriority(M), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5061 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5062 | R.AllParametersAreInformative = (WantKind != MK_Any); |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5063 | if (!InOriginalClass) |
| 5064 | R.Priority += CCD_InBaseClass; |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5065 | Results.MaybeAddResult(R, CurContext); |
| 5066 | } |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5067 | } |
| 5068 | |
Douglas Gregor | f37c949 | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 5069 | // Visit the protocols of protocols. |
| 5070 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5071 | if (Protocol->hasDefinition()) { |
| 5072 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5073 | = Protocol->getReferencedProtocols(); |
| 5074 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 5075 | E = Protocols.end(); |
| 5076 | I != E; ++I) |
| 5077 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5078 | CurContext, Selectors, AllowSameLength, Results, false); |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5079 | } |
Douglas Gregor | f37c949 | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 5080 | } |
| 5081 | |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 5082 | if (!IFace || !IFace->hasDefinition()) |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5083 | return; |
| 5084 | |
| 5085 | // Add methods in protocols. |
Aaron Ballman | a49c506 | 2014-03-13 20:29:09 +0000 | [diff] [blame] | 5086 | for (auto *I : IFace->protocols()) |
| 5087 | AddObjCMethods(I, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5088 | CurContext, Selectors, AllowSameLength, Results, false); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5089 | |
| 5090 | // Add methods in categories. |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 5091 | for (auto *CatDecl : IFace->known_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5092 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5093 | CurContext, Selectors, AllowSameLength, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5094 | Results, InOriginalClass); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5095 | |
| 5096 | // Add a categories protocol methods. |
| 5097 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5098 | = CatDecl->getReferencedProtocols(); |
| 5099 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 5100 | E = Protocols.end(); |
| 5101 | I != E; ++I) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5102 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5103 | CurContext, Selectors, AllowSameLength, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5104 | Results, false); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5105 | |
| 5106 | // Add methods in category implementations. |
| 5107 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5108 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5109 | CurContext, Selectors, AllowSameLength, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5110 | Results, InOriginalClass); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5111 | } |
| 5112 | |
| 5113 | // Add methods in superclass. |
| 5114 | if (IFace->getSuperClass()) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5115 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5116 | SelIdents, CurContext, Selectors, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5117 | AllowSameLength, Results, false); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5118 | |
| 5119 | // Add methods in our implementation, if any. |
| 5120 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5121 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5122 | CurContext, Selectors, AllowSameLength, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5123 | Results, InOriginalClass); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5124 | } |
| 5125 | |
| 5126 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5127 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S) { |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5128 | // Try to find the interface where getters might live. |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5129 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5130 | if (!Class) { |
| 5131 | if (ObjCCategoryDecl *Category |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5132 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5133 | Class = Category->getClassInterface(); |
| 5134 | |
| 5135 | if (!Class) |
| 5136 | return; |
| 5137 | } |
| 5138 | |
| 5139 | // Find all of the potential getters. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5140 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5141 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5142 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5143 | Results.EnterNewScope(); |
| 5144 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5145 | VisitedSelectorSet Selectors; |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5146 | AddObjCMethods(Class, true, MK_ZeroArgSelector, None, CurContext, Selectors, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5147 | /*AllowSameLength=*/true, Results); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5148 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5149 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5150 | CodeCompletionContext::CCC_Other, |
| 5151 | Results.data(),Results.size()); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5152 | } |
| 5153 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5154 | void Sema::CodeCompleteObjCPropertySetter(Scope *S) { |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5155 | // Try to find the interface where setters might live. |
| 5156 | ObjCInterfaceDecl *Class |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5157 | = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5158 | if (!Class) { |
| 5159 | if (ObjCCategoryDecl *Category |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5160 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5161 | Class = Category->getClassInterface(); |
| 5162 | |
| 5163 | if (!Class) |
| 5164 | return; |
| 5165 | } |
| 5166 | |
| 5167 | // Find all of the potential getters. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5168 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5169 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5170 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5171 | Results.EnterNewScope(); |
| 5172 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5173 | VisitedSelectorSet Selectors; |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5174 | AddObjCMethods(Class, true, MK_OneArgSelector, None, CurContext, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5175 | Selectors, /*AllowSameLength=*/true, Results); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5176 | |
| 5177 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5178 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5179 | CodeCompletionContext::CCC_Other, |
| 5180 | Results.data(),Results.size()); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5181 | } |
| 5182 | |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5183 | void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS, |
| 5184 | bool IsParameter) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5185 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5186 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5187 | CodeCompletionContext::CCC_Type); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5188 | Results.EnterNewScope(); |
| 5189 | |
| 5190 | // Add context-sensitive, Objective-C parameter-passing keywords. |
| 5191 | bool AddedInOut = false; |
| 5192 | if ((DS.getObjCDeclQualifier() & |
| 5193 | (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 5194 | Results.AddResult("in"); |
| 5195 | Results.AddResult("inout"); |
| 5196 | AddedInOut = true; |
| 5197 | } |
| 5198 | if ((DS.getObjCDeclQualifier() & |
| 5199 | (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 5200 | Results.AddResult("out"); |
| 5201 | if (!AddedInOut) |
| 5202 | Results.AddResult("inout"); |
| 5203 | } |
| 5204 | if ((DS.getObjCDeclQualifier() & |
| 5205 | (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref | |
| 5206 | ObjCDeclSpec::DQ_Oneway)) == 0) { |
| 5207 | Results.AddResult("bycopy"); |
| 5208 | Results.AddResult("byref"); |
| 5209 | Results.AddResult("oneway"); |
| 5210 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 5211 | if ((DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability) == 0) { |
| 5212 | Results.AddResult("nonnull"); |
| 5213 | Results.AddResult("nullable"); |
| 5214 | Results.AddResult("null_unspecified"); |
| 5215 | } |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5216 | |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5217 | // If we're completing the return type of an Objective-C method and the |
| 5218 | // identifier IBAction refers to a macro, provide a completion item for |
| 5219 | // an action, e.g., |
| 5220 | // IBAction)<#selector#>:(id)sender |
| 5221 | if (DS.getObjCDeclQualifier() == 0 && !IsParameter && |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 5222 | PP.isMacroDefined("IBAction")) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5223 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5224 | Results.getCodeCompletionTUInfo(), |
| 5225 | CCP_CodePattern, CXAvailability_Available); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5226 | Builder.AddTypedTextChunk("IBAction"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5227 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5228 | Builder.AddPlaceholderChunk("selector"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5229 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 5230 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5231 | Builder.AddTextChunk("id"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5232 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5233 | Builder.AddTextChunk("sender"); |
| 5234 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 5235 | } |
Douglas Gregor | ed1f597 | 2013-01-30 07:11:43 +0000 | [diff] [blame] | 5236 | |
| 5237 | // If we're completing the return type, provide 'instancetype'. |
| 5238 | if (!IsParameter) { |
| 5239 | Results.AddResult(CodeCompletionResult("instancetype")); |
| 5240 | } |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5241 | |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5242 | // Add various builtin type names and specifiers. |
| 5243 | AddOrdinaryNameResults(PCC_Type, S, *this, Results); |
| 5244 | Results.ExitScope(); |
| 5245 | |
| 5246 | // Add the various type names |
| 5247 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 5248 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 5249 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 5250 | CodeCompleter->includeGlobals()); |
| 5251 | |
| 5252 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 5253 | AddMacroResults(PP, Results, false); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5254 | |
| 5255 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5256 | CodeCompletionContext::CCC_Type, |
| 5257 | Results.data(), Results.size()); |
| 5258 | } |
| 5259 | |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5260 | /// \brief When we have an expression with type "id", we may assume |
| 5261 | /// that it has some more-specific class type based on knowledge of |
| 5262 | /// common uses of Objective-C. This routine returns that class type, |
| 5263 | /// or NULL if no better result could be determined. |
| 5264 | static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) { |
Douglas Gregor | ed0b69d | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 5265 | ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5266 | if (!Msg) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5267 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5268 | |
| 5269 | Selector Sel = Msg->getSelector(); |
| 5270 | if (Sel.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5271 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5272 | |
| 5273 | IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0); |
| 5274 | if (!Id) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5275 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5276 | |
| 5277 | ObjCMethodDecl *Method = Msg->getMethodDecl(); |
| 5278 | if (!Method) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5279 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5280 | |
| 5281 | // Determine the class that we're sending the message to. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5282 | ObjCInterfaceDecl *IFace = nullptr; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 5283 | switch (Msg->getReceiverKind()) { |
| 5284 | case ObjCMessageExpr::Class: |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5285 | if (const ObjCObjectType *ObjType |
| 5286 | = Msg->getClassReceiver()->getAs<ObjCObjectType>()) |
| 5287 | IFace = ObjType->getInterface(); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 5288 | break; |
| 5289 | |
| 5290 | case ObjCMessageExpr::Instance: { |
| 5291 | QualType T = Msg->getInstanceReceiver()->getType(); |
| 5292 | if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) |
| 5293 | IFace = Ptr->getInterfaceDecl(); |
| 5294 | break; |
| 5295 | } |
| 5296 | |
| 5297 | case ObjCMessageExpr::SuperInstance: |
| 5298 | case ObjCMessageExpr::SuperClass: |
| 5299 | break; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5300 | } |
| 5301 | |
| 5302 | if (!IFace) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5303 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5304 | |
| 5305 | ObjCInterfaceDecl *Super = IFace->getSuperClass(); |
| 5306 | if (Method->isInstanceMethod()) |
| 5307 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 5308 | .Case("retain", IFace) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5309 | .Case("strong", IFace) |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5310 | .Case("autorelease", IFace) |
| 5311 | .Case("copy", IFace) |
| 5312 | .Case("copyWithZone", IFace) |
| 5313 | .Case("mutableCopy", IFace) |
| 5314 | .Case("mutableCopyWithZone", IFace) |
| 5315 | .Case("awakeFromCoder", IFace) |
| 5316 | .Case("replacementObjectFromCoder", IFace) |
| 5317 | .Case("class", IFace) |
| 5318 | .Case("classForCoder", IFace) |
| 5319 | .Case("superclass", Super) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5320 | .Default(nullptr); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5321 | |
| 5322 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 5323 | .Case("new", IFace) |
| 5324 | .Case("alloc", IFace) |
| 5325 | .Case("allocWithZone", IFace) |
| 5326 | .Case("class", IFace) |
| 5327 | .Case("superclass", Super) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5328 | .Default(nullptr); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5329 | } |
| 5330 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5331 | // Add a special completion for a message send to "super", which fills in the |
| 5332 | // most likely case of forwarding all of our arguments to the superclass |
| 5333 | // function. |
| 5334 | /// |
| 5335 | /// \param S The semantic analysis object. |
| 5336 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 5337 | /// \param NeedSuperKeyword Whether we need to prefix this completion with |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5338 | /// the "super" keyword. Otherwise, we just need to provide the arguments. |
| 5339 | /// |
| 5340 | /// \param SelIdents The identifiers in the selector that have already been |
| 5341 | /// provided as arguments for a send to "super". |
| 5342 | /// |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5343 | /// \param Results The set of results to augment. |
| 5344 | /// |
| 5345 | /// \returns the Objective-C method declaration that would be invoked by |
| 5346 | /// this "super" completion. If NULL, no completion was added. |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5347 | static ObjCMethodDecl *AddSuperSendCompletion( |
| 5348 | Sema &S, bool NeedSuperKeyword, |
| 5349 | ArrayRef<IdentifierInfo *> SelIdents, |
| 5350 | ResultBuilder &Results) { |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5351 | ObjCMethodDecl *CurMethod = S.getCurMethodDecl(); |
| 5352 | if (!CurMethod) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5353 | return nullptr; |
| 5354 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5355 | ObjCInterfaceDecl *Class = CurMethod->getClassInterface(); |
| 5356 | if (!Class) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5357 | return nullptr; |
| 5358 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5359 | // Try to find a superclass method with the same selector. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5360 | ObjCMethodDecl *SuperMethod = nullptr; |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5361 | while ((Class = Class->getSuperClass()) && !SuperMethod) { |
| 5362 | // Check in the class |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5363 | SuperMethod = Class->getMethod(CurMethod->getSelector(), |
| 5364 | CurMethod->isInstanceMethod()); |
| 5365 | |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5366 | // Check in categories or class extensions. |
| 5367 | if (!SuperMethod) { |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 5368 | for (const auto *Cat : Class->known_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5369 | if ((SuperMethod = Cat->getMethod(CurMethod->getSelector(), |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5370 | CurMethod->isInstanceMethod()))) |
| 5371 | break; |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5372 | } |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5373 | } |
| 5374 | } |
| 5375 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5376 | if (!SuperMethod) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5377 | return nullptr; |
| 5378 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5379 | // Check whether the superclass method has the same signature. |
| 5380 | if (CurMethod->param_size() != SuperMethod->param_size() || |
| 5381 | CurMethod->isVariadic() != SuperMethod->isVariadic()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5382 | return nullptr; |
| 5383 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5384 | for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(), |
| 5385 | CurPEnd = CurMethod->param_end(), |
| 5386 | SuperP = SuperMethod->param_begin(); |
| 5387 | CurP != CurPEnd; ++CurP, ++SuperP) { |
| 5388 | // Make sure the parameter types are compatible. |
| 5389 | if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(), |
| 5390 | (*SuperP)->getType())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5391 | return nullptr; |
| 5392 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5393 | // Make sure we have a parameter name to forward! |
| 5394 | if (!(*CurP)->getIdentifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5395 | return nullptr; |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5396 | } |
| 5397 | |
| 5398 | // We have a superclass method. Now, form the send-to-super completion. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5399 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5400 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5401 | |
| 5402 | // Give this completion a return type. |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 5403 | AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod, |
| 5404 | Results.getCompletionContext().getBaseType(), |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 5405 | Builder); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5406 | |
| 5407 | // If we need the "super" keyword, add it (plus some spacing). |
| 5408 | if (NeedSuperKeyword) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5409 | Builder.AddTypedTextChunk("super"); |
| 5410 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5411 | } |
| 5412 | |
| 5413 | Selector Sel = CurMethod->getSelector(); |
| 5414 | if (Sel.isUnarySelector()) { |
| 5415 | if (NeedSuperKeyword) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5416 | Builder.AddTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5417 | Sel.getNameForSlot(0))); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5418 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5419 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5420 | Sel.getNameForSlot(0))); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5421 | } else { |
| 5422 | ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(); |
| 5423 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5424 | if (I > SelIdents.size()) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5425 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5426 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5427 | if (I < SelIdents.size()) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5428 | Builder.AddInformativeChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5429 | Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5430 | Sel.getNameForSlot(I) + ":")); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5431 | else if (NeedSuperKeyword || I > SelIdents.size()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5432 | Builder.AddTextChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5433 | Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5434 | Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5435 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5436 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5437 | } else { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5438 | Builder.AddTypedTextChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5439 | Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5440 | Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5441 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5442 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5443 | } |
| 5444 | } |
| 5445 | } |
| 5446 | |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 5447 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), SuperMethod, |
| 5448 | CCP_SuperCompletion)); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5449 | return SuperMethod; |
| 5450 | } |
| 5451 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5452 | void Sema::CodeCompleteObjCMessageReceiver(Scope *S) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5453 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5454 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5455 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5456 | CodeCompletionContext::CCC_ObjCMessageReceiver, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5457 | getLangOpts().CPlusPlus11 |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5458 | ? &ResultBuilder::IsObjCMessageReceiverOrLambdaCapture |
| 5459 | : &ResultBuilder::IsObjCMessageReceiver); |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5460 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5461 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 5462 | Results.EnterNewScope(); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5463 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 5464 | CodeCompleter->includeGlobals()); |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5465 | |
| 5466 | // If we are in an Objective-C method inside a class that has a superclass, |
| 5467 | // add "super" as an option. |
| 5468 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 5469 | if (ObjCInterfaceDecl *Iface = Method->getClassInterface()) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5470 | if (Iface->getSuperClass()) { |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5471 | Results.AddResult(Result("super")); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5472 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5473 | AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, None, Results); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5474 | } |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5475 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5476 | if (getLangOpts().CPlusPlus11) |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5477 | addThisCompletion(*this, Results); |
| 5478 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5479 | Results.ExitScope(); |
| 5480 | |
| 5481 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 5482 | AddMacroResults(PP, Results, false); |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 5483 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5484 | Results.data(), Results.size()); |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5485 | |
| 5486 | } |
| 5487 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5488 | void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5489 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5490 | bool AtArgumentExpression) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5491 | ObjCInterfaceDecl *CDecl = nullptr; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5492 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 5493 | // Figure out which interface we're in. |
| 5494 | CDecl = CurMethod->getClassInterface(); |
| 5495 | if (!CDecl) |
| 5496 | return; |
| 5497 | |
| 5498 | // Find the superclass of this class. |
| 5499 | CDecl = CDecl->getSuperClass(); |
| 5500 | if (!CDecl) |
| 5501 | return; |
| 5502 | |
| 5503 | if (CurMethod->isInstanceMethod()) { |
| 5504 | // We are inside an instance method, which means that the message |
| 5505 | // send [super ...] is actually calling an instance method on the |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5506 | // current object. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5507 | return CodeCompleteObjCInstanceMessage(S, nullptr, SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5508 | AtArgumentExpression, |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5509 | CDecl); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5510 | } |
| 5511 | |
| 5512 | // Fall through to send to the superclass in CDecl. |
| 5513 | } else { |
| 5514 | // "super" may be the name of a type or variable. Figure out which |
| 5515 | // it is. |
Argyrios Kyrtzidis | 3e56dd4 | 2013-03-14 22:56:43 +0000 | [diff] [blame] | 5516 | IdentifierInfo *Super = getSuperIdentifier(); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5517 | NamedDecl *ND = LookupSingleName(S, Super, SuperLoc, |
| 5518 | LookupOrdinaryName); |
| 5519 | if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) { |
| 5520 | // "super" names an interface. Use it. |
| 5521 | } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5522 | if (const ObjCObjectType *Iface |
| 5523 | = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>()) |
| 5524 | CDecl = Iface->getInterface(); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5525 | } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) { |
| 5526 | // "super" names an unresolved type; we can't be more specific. |
| 5527 | } else { |
| 5528 | // Assume that "super" names some kind of value and parse that way. |
| 5529 | CXXScopeSpec SS; |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 5530 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5531 | UnqualifiedId id; |
| 5532 | id.setIdentifier(Super, SuperLoc); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 5533 | ExprResult SuperExpr = ActOnIdExpression(S, SS, TemplateKWLoc, id, |
| 5534 | false, false); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5535 | return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(), |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5536 | SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5537 | AtArgumentExpression); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5538 | } |
| 5539 | |
| 5540 | // Fall through |
| 5541 | } |
| 5542 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5543 | ParsedType Receiver; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5544 | if (CDecl) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5545 | Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl)); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5546 | return CodeCompleteObjCClassMessage(S, Receiver, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5547 | AtArgumentExpression, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5548 | /*IsSuper=*/true); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5549 | } |
| 5550 | |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5551 | /// \brief Given a set of code-completion results for the argument of a message |
| 5552 | /// send, determine the preferred type (if any) for that argument expression. |
| 5553 | static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results, |
| 5554 | unsigned NumSelIdents) { |
| 5555 | typedef CodeCompletionResult Result; |
| 5556 | ASTContext &Context = Results.getSema().Context; |
| 5557 | |
| 5558 | QualType PreferredType; |
| 5559 | unsigned BestPriority = CCP_Unlikely * 2; |
| 5560 | Result *ResultsData = Results.data(); |
| 5561 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 5562 | Result &R = ResultsData[I]; |
| 5563 | if (R.Kind == Result::RK_Declaration && |
| 5564 | isa<ObjCMethodDecl>(R.Declaration)) { |
| 5565 | if (R.Priority <= BestPriority) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 5566 | const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5567 | if (NumSelIdents <= Method->param_size()) { |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 5568 | QualType MyPreferredType = Method->parameters()[NumSelIdents - 1] |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5569 | ->getType(); |
| 5570 | if (R.Priority < BestPriority || PreferredType.isNull()) { |
| 5571 | BestPriority = R.Priority; |
| 5572 | PreferredType = MyPreferredType; |
| 5573 | } else if (!Context.hasSameUnqualifiedType(PreferredType, |
| 5574 | MyPreferredType)) { |
| 5575 | PreferredType = QualType(); |
| 5576 | } |
| 5577 | } |
| 5578 | } |
| 5579 | } |
| 5580 | } |
| 5581 | |
| 5582 | return PreferredType; |
| 5583 | } |
| 5584 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5585 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| 5586 | ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5587 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5588 | bool AtArgumentExpression, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5589 | bool IsSuper, |
| 5590 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5591 | typedef CodeCompletionResult Result; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5592 | ObjCInterfaceDecl *CDecl = nullptr; |
| 5593 | |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 5594 | // If the given name refers to an interface type, retrieve the |
| 5595 | // corresponding declaration. |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5596 | if (Receiver) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5597 | QualType T = SemaRef.GetTypeFromParser(Receiver, nullptr); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5598 | if (!T.isNull()) |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5599 | if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>()) |
| 5600 | CDecl = Interface->getInterface(); |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 5601 | } |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5602 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5603 | // Add all of the factory methods in this Objective-C class, its protocols, |
| 5604 | // superclasses, categories, implementation, etc. |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5605 | Results.EnterNewScope(); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5606 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5607 | // If this is a send-to-super, try to add the special "super" send |
| 5608 | // completion. |
| 5609 | if (IsSuper) { |
| 5610 | if (ObjCMethodDecl *SuperMethod |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5611 | = AddSuperSendCompletion(SemaRef, false, SelIdents, Results)) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5612 | Results.Ignore(SuperMethod); |
| 5613 | } |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5614 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 5615 | // If we're inside an Objective-C method definition, prefer its selector to |
| 5616 | // others. |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5617 | if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl()) |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 5618 | Results.setPreferredSelector(CurMethod->getSelector()); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5619 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5620 | VisitedSelectorSet Selectors; |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5621 | if (CDecl) |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5622 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5623 | SemaRef.CurContext, Selectors, AtArgumentExpression, |
| 5624 | Results); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5625 | else { |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5626 | // We're messaging "id" as a type; provide all class/factory methods. |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5627 | |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5628 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 5629 | // pool from the AST file. |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 5630 | if (SemaRef.getExternalSource()) { |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5631 | for (uint32_t I = 0, |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 5632 | N = SemaRef.getExternalSource()->GetNumExternalSelectors(); |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 5633 | I != N; ++I) { |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 5634 | Selector Sel = SemaRef.getExternalSource()->GetExternalSelector(I); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5635 | if (Sel.isNull() || SemaRef.MethodPool.count(Sel)) |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5636 | continue; |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5637 | |
| 5638 | SemaRef.ReadMethodPool(Sel); |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5639 | } |
| 5640 | } |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5641 | |
| 5642 | for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(), |
| 5643 | MEnd = SemaRef.MethodPool.end(); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5644 | M != MEnd; ++M) { |
| 5645 | for (ObjCMethodList *MethList = &M->second.second; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5646 | MethList && MethList->getMethod(); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 5647 | MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5648 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5649 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5650 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5651 | Result R(MethList->getMethod(), |
| 5652 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5653 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5654 | R.AllParametersAreInformative = false; |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5655 | Results.MaybeAddResult(R, SemaRef.CurContext); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5656 | } |
| 5657 | } |
| 5658 | } |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5659 | |
| 5660 | Results.ExitScope(); |
| 5661 | } |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5662 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5663 | void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5664 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5665 | bool AtArgumentExpression, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5666 | bool IsSuper) { |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5667 | |
| 5668 | QualType T = this->GetTypeFromParser(Receiver); |
| 5669 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5670 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5671 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5672 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5673 | T, SelIdents)); |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5674 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5675 | AddClassMessageCompletions(*this, S, Receiver, SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5676 | AtArgumentExpression, IsSuper, Results); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5677 | |
| 5678 | // If we're actually at the argument expression (rather than prior to the |
| 5679 | // selector), we're actually performing code completion for an expression. |
| 5680 | // Determine whether we have a single, best method. If so, we can |
| 5681 | // code-complete the expression using the corresponding parameter type as |
| 5682 | // our preferred type, improving completion results. |
| 5683 | if (AtArgumentExpression) { |
| 5684 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5685 | SelIdents.size()); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5686 | if (PreferredType.isNull()) |
| 5687 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 5688 | else |
| 5689 | CodeCompleteExpression(S, PreferredType); |
| 5690 | return; |
| 5691 | } |
| 5692 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5693 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5694 | Results.getCompletionContext(), |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5695 | Results.data(), Results.size()); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5696 | } |
| 5697 | |
Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 5698 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5699 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5700 | bool AtArgumentExpression, |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5701 | ObjCInterfaceDecl *Super) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5702 | typedef CodeCompletionResult Result; |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5703 | |
| 5704 | Expr *RecExpr = static_cast<Expr *>(Receiver); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5705 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5706 | // If necessary, apply function/array conversion to the receiver. |
| 5707 | // C99 6.7.5.3p[7,8]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5708 | if (RecExpr) { |
| 5709 | ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr); |
| 5710 | if (Conv.isInvalid()) // conversion failed. bail. |
| 5711 | return; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5712 | RecExpr = Conv.get(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5713 | } |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5714 | QualType ReceiverType = RecExpr? RecExpr->getType() |
| 5715 | : Super? Context.getObjCObjectPointerType( |
| 5716 | Context.getObjCInterfaceType(Super)) |
| 5717 | : Context.getObjCIdType(); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5718 | |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 5719 | // If we're messaging an expression with type "id" or "Class", check |
| 5720 | // whether we know something special about the receiver that allows |
| 5721 | // us to assume a more-specific receiver type. |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 5722 | if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) { |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 5723 | if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) { |
| 5724 | if (ReceiverType->isObjCClassType()) |
| 5725 | return CodeCompleteObjCClassMessage(S, |
| 5726 | ParsedType::make(Context.getObjCInterfaceType(IFace)), |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5727 | SelIdents, |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 5728 | AtArgumentExpression, Super); |
| 5729 | |
| 5730 | ReceiverType = Context.getObjCObjectPointerType( |
| 5731 | Context.getObjCInterfaceType(IFace)); |
| 5732 | } |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 5733 | } else if (RecExpr && getLangOpts().CPlusPlus) { |
| 5734 | ExprResult Conv = PerformContextuallyConvertToObjCPointer(RecExpr); |
| 5735 | if (Conv.isUsable()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5736 | RecExpr = Conv.get(); |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 5737 | ReceiverType = RecExpr->getType(); |
| 5738 | } |
| 5739 | } |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 5740 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5741 | // Build the set of methods we can see. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5742 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5743 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5744 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5745 | ReceiverType, SelIdents)); |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5746 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5747 | Results.EnterNewScope(); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5748 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5749 | // If this is a send-to-super, try to add the special "super" send |
| 5750 | // completion. |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5751 | if (Super) { |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5752 | if (ObjCMethodDecl *SuperMethod |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5753 | = AddSuperSendCompletion(*this, false, SelIdents, Results)) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5754 | Results.Ignore(SuperMethod); |
| 5755 | } |
| 5756 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 5757 | // If we're inside an Objective-C method definition, prefer its selector to |
| 5758 | // others. |
| 5759 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 5760 | Results.setPreferredSelector(CurMethod->getSelector()); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5761 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5762 | // Keep track of the selectors we've already added. |
| 5763 | VisitedSelectorSet Selectors; |
| 5764 | |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5765 | // Handle messages to Class. This really isn't a message to an instance |
| 5766 | // method, so we treat it the same way we would treat a message send to a |
| 5767 | // class method. |
| 5768 | if (ReceiverType->isObjCClassType() || |
| 5769 | ReceiverType->isObjCQualifiedClassType()) { |
| 5770 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 5771 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5772 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5773 | CurContext, Selectors, AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5774 | } |
| 5775 | } |
| 5776 | // Handle messages to a qualified ID ("id<foo>"). |
| 5777 | else if (const ObjCObjectPointerType *QualID |
| 5778 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 5779 | // Search protocols for instance methods. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 5780 | for (auto *I : QualID->quals()) |
| 5781 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5782 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5783 | } |
| 5784 | // Handle messages to a pointer to interface type. |
| 5785 | else if (const ObjCObjectPointerType *IFacePtr |
| 5786 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 5787 | // Search the class, its superclasses, etc., for instance methods. |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5788 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5789 | CurContext, Selectors, AtArgumentExpression, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5790 | Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5791 | |
| 5792 | // Search protocols for instance methods. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 5793 | for (auto *I : IFacePtr->quals()) |
| 5794 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5795 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5796 | } |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5797 | // Handle messages to "id". |
| 5798 | else if (ReceiverType->isObjCIdType()) { |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5799 | // We're messaging "id", so provide all instance methods we know |
| 5800 | // about as code-completion results. |
| 5801 | |
| 5802 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 5803 | // pool from the AST file. |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5804 | if (ExternalSource) { |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 5805 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 5806 | I != N; ++I) { |
| 5807 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5808 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5809 | continue; |
| 5810 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5811 | ReadMethodPool(Sel); |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5812 | } |
| 5813 | } |
| 5814 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5815 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 5816 | MEnd = MethodPool.end(); |
| 5817 | M != MEnd; ++M) { |
| 5818 | for (ObjCMethodList *MethList = &M->second.first; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5819 | MethList && MethList->getMethod(); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 5820 | MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5821 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5822 | continue; |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5823 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5824 | if (!Selectors.insert(MethList->getMethod()->getSelector()).second) |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5825 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5826 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 5827 | Result R(MethList->getMethod(), |
| 5828 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5829 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5830 | R.AllParametersAreInformative = false; |
| 5831 | Results.MaybeAddResult(R, CurContext); |
| 5832 | } |
| 5833 | } |
| 5834 | } |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5835 | Results.ExitScope(); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5836 | |
| 5837 | |
| 5838 | // If we're actually at the argument expression (rather than prior to the |
| 5839 | // selector), we're actually performing code completion for an expression. |
| 5840 | // Determine whether we have a single, best method. If so, we can |
| 5841 | // code-complete the expression using the corresponding parameter type as |
| 5842 | // our preferred type, improving completion results. |
| 5843 | if (AtArgumentExpression) { |
| 5844 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5845 | SelIdents.size()); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5846 | if (PreferredType.isNull()) |
| 5847 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 5848 | else |
| 5849 | CodeCompleteExpression(S, PreferredType); |
| 5850 | return; |
| 5851 | } |
| 5852 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5853 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5854 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5855 | Results.data(),Results.size()); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5856 | } |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5857 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 5858 | void Sema::CodeCompleteObjCForCollection(Scope *S, |
| 5859 | DeclGroupPtrTy IterationVar) { |
| 5860 | CodeCompleteExpressionData Data; |
| 5861 | Data.ObjCCollection = true; |
| 5862 | |
| 5863 | if (IterationVar.getAsOpaquePtr()) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 5864 | DeclGroupRef DG = IterationVar.get(); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 5865 | for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) { |
| 5866 | if (*I) |
| 5867 | Data.IgnoreDecls.push_back(*I); |
| 5868 | } |
| 5869 | } |
| 5870 | |
| 5871 | CodeCompleteExpression(S, Data); |
| 5872 | } |
| 5873 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5874 | void Sema::CodeCompleteObjCSelector(Scope *S, |
| 5875 | ArrayRef<IdentifierInfo *> SelIdents) { |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5876 | // If we have an external source, load the entire class method |
| 5877 | // pool from the AST file. |
| 5878 | if (ExternalSource) { |
| 5879 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 5880 | I != N; ++I) { |
| 5881 | Selector Sel = ExternalSource->GetExternalSelector(I); |
| 5882 | if (Sel.isNull() || MethodPool.count(Sel)) |
| 5883 | continue; |
| 5884 | |
| 5885 | ReadMethodPool(Sel); |
| 5886 | } |
| 5887 | } |
| 5888 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5889 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5890 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5891 | CodeCompletionContext::CCC_SelectorName); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5892 | Results.EnterNewScope(); |
| 5893 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 5894 | MEnd = MethodPool.end(); |
| 5895 | M != MEnd; ++M) { |
| 5896 | |
| 5897 | Selector Sel = M->first; |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5898 | if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents)) |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5899 | continue; |
| 5900 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5901 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5902 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5903 | if (Sel.isUnarySelector()) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5904 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5905 | Sel.getNameForSlot(0))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5906 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5907 | continue; |
| 5908 | } |
| 5909 | |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5910 | std::string Accumulator; |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5911 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5912 | if (I == SelIdents.size()) { |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5913 | if (!Accumulator.empty()) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5914 | Builder.AddInformativeChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5915 | Accumulator)); |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5916 | Accumulator.clear(); |
| 5917 | } |
| 5918 | } |
| 5919 | |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 5920 | Accumulator += Sel.getNameForSlot(I); |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5921 | Accumulator += ':'; |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5922 | } |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5923 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator)); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5924 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5925 | } |
| 5926 | Results.ExitScope(); |
| 5927 | |
| 5928 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5929 | CodeCompletionContext::CCC_SelectorName, |
| 5930 | Results.data(), Results.size()); |
| 5931 | } |
| 5932 | |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5933 | /// \brief Add all of the protocol declarations that we find in the given |
| 5934 | /// (translation unit) context. |
| 5935 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5936 | bool OnlyForwardDeclarations, |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5937 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5938 | typedef CodeCompletionResult Result; |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5939 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 5940 | for (const auto *D : Ctx->decls()) { |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5941 | // Record any protocols we find. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 5942 | if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(D)) |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5943 | if (!OnlyForwardDeclarations || !Proto->hasDefinition()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5944 | Results.AddResult(Result(Proto, Results.getBasePriority(Proto),nullptr), |
| 5945 | CurContext, nullptr, false); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5946 | } |
| 5947 | } |
| 5948 | |
Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 5949 | void Sema::CodeCompleteObjCProtocolReferences( |
| 5950 | ArrayRef<IdentifierLocPair> Protocols) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5951 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5952 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5953 | CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5954 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5955 | if (CodeCompleter && CodeCompleter->includeGlobals()) { |
| 5956 | Results.EnterNewScope(); |
| 5957 | |
| 5958 | // Tell the result set to ignore all of the protocols we have |
| 5959 | // already seen. |
| 5960 | // FIXME: This doesn't work when caching code-completion results. |
Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 5961 | for (const IdentifierLocPair &Pair : Protocols) |
| 5962 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Pair.first, |
| 5963 | Pair.second)) |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5964 | Results.Ignore(Protocol); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5965 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5966 | // Add all protocols. |
| 5967 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 5968 | Results); |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5969 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5970 | Results.ExitScope(); |
| 5971 | } |
| 5972 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5973 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5974 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 5975 | Results.data(),Results.size()); |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5976 | } |
| 5977 | |
| 5978 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5979 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5980 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5981 | CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5982 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5983 | if (CodeCompleter && CodeCompleter->includeGlobals()) { |
| 5984 | Results.EnterNewScope(); |
| 5985 | |
| 5986 | // Add all protocols. |
| 5987 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 5988 | Results); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5989 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5990 | Results.ExitScope(); |
| 5991 | } |
| 5992 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5993 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5994 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 5995 | Results.data(),Results.size()); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5996 | } |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5997 | |
| 5998 | /// \brief Add all of the Objective-C interface declarations that we find in |
| 5999 | /// the given (translation unit) context. |
| 6000 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, |
| 6001 | bool OnlyForwardDeclarations, |
| 6002 | bool OnlyUnimplemented, |
| 6003 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6004 | typedef CodeCompletionResult Result; |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6005 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6006 | for (const auto *D : Ctx->decls()) { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 6007 | // Record any interfaces we find. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6008 | if (const auto *Class = dyn_cast<ObjCInterfaceDecl>(D)) |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 6009 | if ((!OnlyForwardDeclarations || !Class->hasDefinition()) && |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 6010 | (!OnlyUnimplemented || !Class->getImplementation())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6011 | Results.AddResult(Result(Class, Results.getBasePriority(Class),nullptr), |
| 6012 | CurContext, nullptr, false); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6013 | } |
| 6014 | } |
| 6015 | |
| 6016 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6017 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6018 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6019 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6020 | Results.EnterNewScope(); |
| 6021 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6022 | if (CodeCompleter->includeGlobals()) { |
| 6023 | // Add all classes. |
| 6024 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6025 | false, Results); |
| 6026 | } |
| 6027 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6028 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6029 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6030 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6031 | CodeCompletionContext::CCC_ObjCInterfaceName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6032 | Results.data(),Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6033 | } |
| 6034 | |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6035 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName, |
| 6036 | SourceLocation ClassNameLoc) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6037 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6038 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6039 | CodeCompletionContext::CCC_ObjCInterfaceName); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6040 | Results.EnterNewScope(); |
| 6041 | |
| 6042 | // Make sure that we ignore the class we're currently defining. |
| 6043 | NamedDecl *CurClass |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6044 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6045 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6046 | Results.Ignore(CurClass); |
| 6047 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6048 | if (CodeCompleter->includeGlobals()) { |
| 6049 | // Add all classes. |
| 6050 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6051 | false, Results); |
| 6052 | } |
| 6053 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6054 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6055 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6056 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6057 | CodeCompletionContext::CCC_ObjCInterfaceName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6058 | Results.data(),Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6059 | } |
| 6060 | |
| 6061 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { |
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 | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6064 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6065 | Results.EnterNewScope(); |
| 6066 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6067 | if (CodeCompleter->includeGlobals()) { |
| 6068 | // Add all unimplemented classes. |
| 6069 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6070 | true, Results); |
| 6071 | } |
| 6072 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6073 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6074 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6075 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6076 | CodeCompletionContext::CCC_ObjCInterfaceName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6077 | Results.data(),Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6078 | } |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6079 | |
| 6080 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6081 | IdentifierInfo *ClassName, |
| 6082 | SourceLocation ClassNameLoc) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6083 | typedef CodeCompletionResult Result; |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6084 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6085 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6086 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6087 | CodeCompletionContext::CCC_ObjCCategoryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6088 | |
| 6089 | // Ignore any categories we find that have already been implemented by this |
| 6090 | // interface. |
| 6091 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 6092 | NamedDecl *CurClass |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6093 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6094 | if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)){ |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 6095 | for (const auto *Cat : Class->visible_categories()) |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6096 | CategoryNames.insert(Cat->getIdentifier()); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6097 | } |
| 6098 | |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6099 | // Add all of the categories we know about. |
| 6100 | Results.EnterNewScope(); |
| 6101 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6102 | for (const auto *D : TU->decls()) |
| 6103 | if (const auto *Category = dyn_cast<ObjCCategoryDecl>(D)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6104 | if (CategoryNames.insert(Category->getIdentifier()).second) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6105 | Results.AddResult(Result(Category, Results.getBasePriority(Category), |
| 6106 | nullptr), |
| 6107 | CurContext, nullptr, false); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6108 | Results.ExitScope(); |
| 6109 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6110 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6111 | CodeCompletionContext::CCC_ObjCCategoryName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6112 | Results.data(),Results.size()); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6113 | } |
| 6114 | |
| 6115 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6116 | IdentifierInfo *ClassName, |
| 6117 | SourceLocation ClassNameLoc) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6118 | typedef CodeCompletionResult Result; |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6119 | |
| 6120 | // Find the corresponding interface. If we couldn't find the interface, the |
| 6121 | // program itself is ill-formed. However, we'll try to be helpful still by |
| 6122 | // providing the list of all of the categories we know about. |
| 6123 | NamedDecl *CurClass |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6124 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6125 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); |
| 6126 | if (!Class) |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6127 | return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6128 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6129 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6130 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6131 | CodeCompletionContext::CCC_ObjCCategoryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6132 | |
| 6133 | // Add all of the categories that have have corresponding interface |
| 6134 | // declarations in this class and any of its superclasses, except for |
| 6135 | // already-implemented categories in the class itself. |
| 6136 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 6137 | Results.EnterNewScope(); |
| 6138 | bool IgnoreImplemented = true; |
| 6139 | while (Class) { |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 6140 | for (const auto *Cat : Class->visible_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6141 | if ((!IgnoreImplemented || !Cat->getImplementation()) && |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6142 | CategoryNames.insert(Cat->getIdentifier()).second) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6143 | Results.AddResult(Result(Cat, Results.getBasePriority(Cat), nullptr), |
| 6144 | CurContext, nullptr, false); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6145 | } |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6146 | |
| 6147 | Class = Class->getSuperClass(); |
| 6148 | IgnoreImplemented = false; |
| 6149 | } |
| 6150 | Results.ExitScope(); |
| 6151 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6152 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6153 | CodeCompletionContext::CCC_ObjCCategoryName, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6154 | Results.data(),Results.size()); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6155 | } |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6156 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6157 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6158 | CodeCompletionContext CCContext(CodeCompletionContext::CCC_Other); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6159 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6160 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6161 | CCContext); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6162 | |
| 6163 | // Figure out where this @synthesize lives. |
| 6164 | ObjCContainerDecl *Container |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6165 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6166 | if (!Container || |
| 6167 | (!isa<ObjCImplementationDecl>(Container) && |
| 6168 | !isa<ObjCCategoryImplDecl>(Container))) |
| 6169 | return; |
| 6170 | |
| 6171 | // Ignore any properties that have already been implemented. |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6172 | Container = getContainerDef(Container); |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6173 | for (const auto *D : Container->decls()) |
| 6174 | if (const auto *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6175 | Results.Ignore(PropertyImpl->getPropertyDecl()); |
| 6176 | |
| 6177 | // Add any properties that we find. |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 6178 | AddedPropertiesSet AddedProperties; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6179 | Results.EnterNewScope(); |
| 6180 | if (ObjCImplementationDecl *ClassImpl |
| 6181 | = dyn_cast<ObjCImplementationDecl>(Container)) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6182 | AddObjCProperties(CCContext, ClassImpl->getClassInterface(), false, |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 6183 | /*AllowNullaryMethods=*/false, CurContext, |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 6184 | AddedProperties, Results); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6185 | else |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6186 | AddObjCProperties(CCContext, |
| 6187 | cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 6188 | false, /*AllowNullaryMethods=*/false, CurContext, |
| 6189 | AddedProperties, Results); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6190 | Results.ExitScope(); |
| 6191 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6192 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6193 | CodeCompletionContext::CCC_Other, |
| 6194 | Results.data(),Results.size()); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6195 | } |
| 6196 | |
| 6197 | void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S, |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6198 | IdentifierInfo *PropertyName) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6199 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6200 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6201 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6202 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6203 | |
| 6204 | // Figure out where this @synthesize lives. |
| 6205 | ObjCContainerDecl *Container |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6206 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6207 | if (!Container || |
| 6208 | (!isa<ObjCImplementationDecl>(Container) && |
| 6209 | !isa<ObjCCategoryImplDecl>(Container))) |
| 6210 | return; |
| 6211 | |
| 6212 | // Figure out which interface we're looking into. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6213 | ObjCInterfaceDecl *Class = nullptr; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6214 | if (ObjCImplementationDecl *ClassImpl |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 6215 | = dyn_cast<ObjCImplementationDecl>(Container)) |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6216 | Class = ClassImpl->getClassInterface(); |
| 6217 | else |
| 6218 | Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl() |
| 6219 | ->getClassInterface(); |
| 6220 | |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6221 | // Determine the type of the property we're synthesizing. |
| 6222 | QualType PropertyType = Context.getObjCIdType(); |
| 6223 | if (Class) { |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 6224 | if (ObjCPropertyDecl *Property = Class->FindPropertyDeclaration( |
| 6225 | PropertyName, ObjCPropertyQueryKind::OBJC_PR_query_instance)) { |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6226 | PropertyType |
| 6227 | = Property->getType().getNonReferenceType().getUnqualifiedType(); |
| 6228 | |
| 6229 | // Give preference to ivars |
| 6230 | Results.setPreferredType(PropertyType); |
| 6231 | } |
| 6232 | } |
| 6233 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6234 | // Add all of the instance variables in this class and its superclasses. |
| 6235 | Results.EnterNewScope(); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6236 | bool SawSimilarlyNamedIvar = false; |
| 6237 | std::string NameWithPrefix; |
| 6238 | NameWithPrefix += '_'; |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 6239 | NameWithPrefix += PropertyName->getName(); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6240 | std::string NameWithSuffix = PropertyName->getName().str(); |
| 6241 | NameWithSuffix += '_'; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6242 | for(; Class; Class = Class->getSuperClass()) { |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6243 | for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar; |
| 6244 | Ivar = Ivar->getNextIvar()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6245 | Results.AddResult(Result(Ivar, Results.getBasePriority(Ivar), nullptr), |
| 6246 | CurContext, nullptr, false); |
| 6247 | |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6248 | // Determine whether we've seen an ivar with a name similar to the |
| 6249 | // property. |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6250 | if ((PropertyName == Ivar->getIdentifier() || |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6251 | NameWithPrefix == Ivar->getName() || |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6252 | NameWithSuffix == Ivar->getName())) { |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6253 | SawSimilarlyNamedIvar = true; |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6254 | |
| 6255 | // Reduce the priority of this result by one, to give it a slight |
| 6256 | // advantage over other results whose names don't match so closely. |
| 6257 | if (Results.size() && |
| 6258 | Results.data()[Results.size() - 1].Kind |
| 6259 | == CodeCompletionResult::RK_Declaration && |
| 6260 | Results.data()[Results.size() - 1].Declaration == Ivar) |
| 6261 | Results.data()[Results.size() - 1].Priority--; |
| 6262 | } |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6263 | } |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6264 | } |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6265 | |
| 6266 | if (!SawSimilarlyNamedIvar) { |
| 6267 | // Create ivar result _propName, that the user can use to synthesize |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6268 | // an ivar of the appropriate type. |
| 6269 | unsigned Priority = CCP_MemberDeclaration + 1; |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6270 | typedef CodeCompletionResult Result; |
| 6271 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6272 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo(), |
| 6273 | Priority,CXAvailability_Available); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6274 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6275 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6276 | Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6277 | Policy, Allocator)); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6278 | Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix)); |
| 6279 | Results.AddResult(Result(Builder.TakeString(), Priority, |
| 6280 | CXCursor_ObjCIvarDecl)); |
| 6281 | } |
| 6282 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6283 | Results.ExitScope(); |
| 6284 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6285 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6286 | CodeCompletionContext::CCC_Other, |
| 6287 | Results.data(),Results.size()); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6288 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6289 | |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6290 | // Mapping from selectors to the methods that implement that selector, along |
| 6291 | // with the "in original class" flag. |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 6292 | typedef llvm::DenseMap< |
| 6293 | Selector, llvm::PointerIntPair<ObjCMethodDecl *, 1, bool> > KnownMethodsMap; |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6294 | |
| 6295 | /// \brief Find all of the methods that reside in the given container |
| 6296 | /// (and its superclasses, protocols, etc.) that meet the given |
| 6297 | /// criteria. Insert those methods into the map of known methods, |
| 6298 | /// indexed by selector so they can be easily found. |
| 6299 | static void FindImplementableMethods(ASTContext &Context, |
| 6300 | ObjCContainerDecl *Container, |
| 6301 | bool WantInstanceMethods, |
| 6302 | QualType ReturnType, |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6303 | KnownMethodsMap &KnownMethods, |
| 6304 | bool InOriginalClass = true) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6305 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) { |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6306 | // Make sure we have a definition; that's what we'll walk. |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 6307 | if (!IFace->hasDefinition()) |
| 6308 | return; |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6309 | |
| 6310 | IFace = IFace->getDefinition(); |
| 6311 | Container = IFace; |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 6312 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6313 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6314 | = IFace->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); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6320 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6321 | // Add methods from any class extensions and categories. |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 6322 | for (auto *Cat : IFace->visible_categories()) { |
| 6323 | FindImplementableMethods(Context, Cat, WantInstanceMethods, ReturnType, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6324 | KnownMethods, false); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6325 | } |
| 6326 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6327 | // Visit the superclass. |
| 6328 | if (IFace->getSuperClass()) |
| 6329 | FindImplementableMethods(Context, IFace->getSuperClass(), |
| 6330 | WantInstanceMethods, ReturnType, |
| 6331 | KnownMethods, false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6332 | } |
| 6333 | |
| 6334 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 6335 | // Recurse into protocols. |
| 6336 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6337 | = Category->getReferencedProtocols(); |
| 6338 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6339 | E = Protocols.end(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6340 | I != E; ++I) |
| 6341 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6342 | KnownMethods, InOriginalClass); |
| 6343 | |
| 6344 | // If this category is the original class, jump to the interface. |
| 6345 | if (InOriginalClass && Category->getClassInterface()) |
| 6346 | FindImplementableMethods(Context, Category->getClassInterface(), |
| 6347 | WantInstanceMethods, ReturnType, KnownMethods, |
| 6348 | false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6349 | } |
| 6350 | |
| 6351 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6352 | // Make sure we have a definition; that's what we'll walk. |
| 6353 | if (!Protocol->hasDefinition()) |
| 6354 | return; |
| 6355 | Protocol = Protocol->getDefinition(); |
| 6356 | Container = Protocol; |
| 6357 | |
| 6358 | // Recurse into protocols. |
| 6359 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6360 | = Protocol->getReferencedProtocols(); |
| 6361 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 6362 | E = Protocols.end(); |
| 6363 | I != E; ++I) |
| 6364 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
| 6365 | KnownMethods, false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6366 | } |
| 6367 | |
| 6368 | // Add methods in this container. This operation occurs last because |
| 6369 | // we want the methods from this container to override any methods |
| 6370 | // we've previously seen with the same selector. |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 6371 | for (auto *M : Container->methods()) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6372 | if (M->isInstanceMethod() == WantInstanceMethods) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6373 | if (!ReturnType.isNull() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6374 | !Context.hasSameUnqualifiedType(ReturnType, M->getReturnType())) |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6375 | continue; |
| 6376 | |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 6377 | KnownMethods[M->getSelector()] = |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 6378 | KnownMethodsMap::mapped_type(M, InOriginalClass); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6379 | } |
| 6380 | } |
| 6381 | } |
| 6382 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6383 | /// \brief Add the parenthesized return or parameter type chunk to a code |
| 6384 | /// completion string. |
| 6385 | static void AddObjCPassingTypeChunk(QualType Type, |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6386 | unsigned ObjCDeclQuals, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6387 | ASTContext &Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6388 | const PrintingPolicy &Policy, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6389 | CodeCompletionBuilder &Builder) { |
| 6390 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 6391 | std::string Quals = formatObjCParamQualifiers(ObjCDeclQuals, Type); |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6392 | if (!Quals.empty()) |
| 6393 | Builder.AddTextChunk(Builder.getAllocator().CopyString(Quals)); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6394 | Builder.AddTextChunk(GetCompletionTypeString(Type, Context, Policy, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6395 | Builder.getAllocator())); |
| 6396 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6397 | } |
| 6398 | |
| 6399 | /// \brief Determine whether the given class is or inherits from a class by |
| 6400 | /// the given name. |
| 6401 | static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6402 | StringRef Name) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6403 | if (!Class) |
| 6404 | return false; |
| 6405 | |
| 6406 | if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name) |
| 6407 | return true; |
| 6408 | |
| 6409 | return InheritsFromClassNamed(Class->getSuperClass(), Name); |
| 6410 | } |
| 6411 | |
| 6412 | /// \brief Add code completions for Objective-C Key-Value Coding (KVC) and |
| 6413 | /// Key-Value Observing (KVO). |
| 6414 | static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property, |
| 6415 | bool IsInstanceMethod, |
| 6416 | QualType ReturnType, |
| 6417 | ASTContext &Context, |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6418 | VisitedSelectorSet &KnownSelectors, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6419 | ResultBuilder &Results) { |
| 6420 | IdentifierInfo *PropName = Property->getIdentifier(); |
| 6421 | if (!PropName || PropName->getLength() == 0) |
| 6422 | return; |
| 6423 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6424 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
| 6425 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6426 | // Builder that will create each code completion. |
| 6427 | typedef CodeCompletionResult Result; |
| 6428 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6429 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6430 | |
| 6431 | // The selector table. |
| 6432 | SelectorTable &Selectors = Context.Selectors; |
| 6433 | |
| 6434 | // The property name, copied into the code completion allocation region |
| 6435 | // on demand. |
| 6436 | struct KeyHolder { |
| 6437 | CodeCompletionAllocator &Allocator; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6438 | StringRef Key; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6439 | const char *CopiedKey; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6440 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6441 | KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6442 | : Allocator(Allocator), Key(Key), CopiedKey(nullptr) {} |
| 6443 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6444 | operator const char *() { |
| 6445 | if (CopiedKey) |
| 6446 | return CopiedKey; |
| 6447 | |
| 6448 | return CopiedKey = Allocator.CopyString(Key); |
| 6449 | } |
| 6450 | } Key(Allocator, PropName->getName()); |
| 6451 | |
| 6452 | // The uppercased name of the property name. |
| 6453 | std::string UpperKey = PropName->getName(); |
| 6454 | if (!UpperKey.empty()) |
Jordan Rose | 4938f27 | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 6455 | UpperKey[0] = toUppercase(UpperKey[0]); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6456 | |
| 6457 | bool ReturnTypeMatchesProperty = ReturnType.isNull() || |
| 6458 | Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(), |
| 6459 | Property->getType()); |
| 6460 | bool ReturnTypeMatchesVoid |
| 6461 | = ReturnType.isNull() || ReturnType->isVoidType(); |
| 6462 | |
| 6463 | // Add the normal accessor -(type)key. |
| 6464 | if (IsInstanceMethod && |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6465 | KnownSelectors.insert(Selectors.getNullarySelector(PropName)).second && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6466 | ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) { |
| 6467 | if (ReturnType.isNull()) |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6468 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, |
| 6469 | Context, Policy, Builder); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6470 | |
| 6471 | Builder.AddTypedTextChunk(Key); |
| 6472 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 6473 | CXCursor_ObjCInstanceMethodDecl)); |
| 6474 | } |
| 6475 | |
| 6476 | // If we have an integral or boolean property (or the user has provided |
| 6477 | // an integral or boolean return type), add the accessor -(type)isKey. |
| 6478 | if (IsInstanceMethod && |
| 6479 | ((!ReturnType.isNull() && |
| 6480 | (ReturnType->isIntegerType() || ReturnType->isBooleanType())) || |
| 6481 | (ReturnType.isNull() && |
| 6482 | (Property->getType()->isIntegerType() || |
| 6483 | Property->getType()->isBooleanType())))) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6484 | std::string SelectorName = (Twine("is") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6485 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6486 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 6487 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6488 | if (ReturnType.isNull()) { |
| 6489 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6490 | Builder.AddTextChunk("BOOL"); |
| 6491 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6492 | } |
| 6493 | |
| 6494 | Builder.AddTypedTextChunk( |
| 6495 | Allocator.CopyString(SelectorId->getName())); |
| 6496 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 6497 | CXCursor_ObjCInstanceMethodDecl)); |
| 6498 | } |
| 6499 | } |
| 6500 | |
| 6501 | // Add the normal mutator. |
| 6502 | if (IsInstanceMethod && ReturnTypeMatchesVoid && |
| 6503 | !Property->getSetterMethodDecl()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6504 | std::string SelectorName = (Twine("set") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6505 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6506 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6507 | if (ReturnType.isNull()) { |
| 6508 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6509 | Builder.AddTextChunk("void"); |
| 6510 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6511 | } |
| 6512 | |
| 6513 | Builder.AddTypedTextChunk( |
| 6514 | Allocator.CopyString(SelectorId->getName())); |
| 6515 | Builder.AddTypedTextChunk(":"); |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6516 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, |
| 6517 | Context, Policy, Builder); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6518 | Builder.AddTextChunk(Key); |
| 6519 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 6520 | CXCursor_ObjCInstanceMethodDecl)); |
| 6521 | } |
| 6522 | } |
| 6523 | |
| 6524 | // Indexed and unordered accessors |
| 6525 | unsigned IndexedGetterPriority = CCP_CodePattern; |
| 6526 | unsigned IndexedSetterPriority = CCP_CodePattern; |
| 6527 | unsigned UnorderedGetterPriority = CCP_CodePattern; |
| 6528 | unsigned UnorderedSetterPriority = CCP_CodePattern; |
| 6529 | if (const ObjCObjectPointerType *ObjCPointer |
| 6530 | = Property->getType()->getAs<ObjCObjectPointerType>()) { |
| 6531 | if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) { |
| 6532 | // If this interface type is not provably derived from a known |
| 6533 | // collection, penalize the corresponding completions. |
| 6534 | if (!InheritsFromClassNamed(IFace, "NSMutableArray")) { |
| 6535 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6536 | if (!InheritsFromClassNamed(IFace, "NSArray")) |
| 6537 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6538 | } |
| 6539 | |
| 6540 | if (!InheritsFromClassNamed(IFace, "NSMutableSet")) { |
| 6541 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6542 | if (!InheritsFromClassNamed(IFace, "NSSet")) |
| 6543 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6544 | } |
| 6545 | } |
| 6546 | } else { |
| 6547 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6548 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6549 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6550 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6551 | } |
| 6552 | |
| 6553 | // Add -(NSUInteger)countOf<key> |
| 6554 | if (IsInstanceMethod && |
| 6555 | (ReturnType.isNull() || ReturnType->isIntegerType())) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6556 | std::string SelectorName = (Twine("countOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6557 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6558 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 6559 | .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("NSUInteger"); |
| 6563 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6564 | } |
| 6565 | |
| 6566 | Builder.AddTypedTextChunk( |
| 6567 | Allocator.CopyString(SelectorId->getName())); |
| 6568 | Results.AddResult(Result(Builder.TakeString(), |
| 6569 | std::min(IndexedGetterPriority, |
| 6570 | UnorderedGetterPriority), |
| 6571 | CXCursor_ObjCInstanceMethodDecl)); |
| 6572 | } |
| 6573 | } |
| 6574 | |
| 6575 | // Indexed getters |
| 6576 | // Add -(id)objectInKeyAtIndex:(NSUInteger)index |
| 6577 | if (IsInstanceMethod && |
| 6578 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6579 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6580 | = (Twine("objectIn") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6581 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6582 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6583 | if (ReturnType.isNull()) { |
| 6584 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6585 | Builder.AddTextChunk("id"); |
| 6586 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6587 | } |
| 6588 | |
| 6589 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6590 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6591 | Builder.AddTextChunk("NSUInteger"); |
| 6592 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6593 | Builder.AddTextChunk("index"); |
| 6594 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 6595 | CXCursor_ObjCInstanceMethodDecl)); |
| 6596 | } |
| 6597 | } |
| 6598 | |
| 6599 | // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes |
| 6600 | if (IsInstanceMethod && |
| 6601 | (ReturnType.isNull() || |
| 6602 | (ReturnType->isObjCObjectPointerType() && |
| 6603 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 6604 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 6605 | ->getName() == "NSArray"))) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6606 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6607 | = (Twine(Property->getName()) + "AtIndexes").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6608 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6609 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6610 | if (ReturnType.isNull()) { |
| 6611 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6612 | Builder.AddTextChunk("NSArray *"); |
| 6613 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6614 | } |
| 6615 | |
| 6616 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6617 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6618 | Builder.AddTextChunk("NSIndexSet *"); |
| 6619 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6620 | Builder.AddTextChunk("indexes"); |
| 6621 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 6622 | CXCursor_ObjCInstanceMethodDecl)); |
| 6623 | } |
| 6624 | } |
| 6625 | |
| 6626 | // Add -(void)getKey:(type **)buffer range:(NSRange)inRange |
| 6627 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6628 | std::string SelectorName = (Twine("get") + UpperKey).str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6629 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6630 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6631 | &Context.Idents.get("range") |
| 6632 | }; |
| 6633 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6634 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6635 | if (ReturnType.isNull()) { |
| 6636 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6637 | Builder.AddTextChunk("void"); |
| 6638 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6639 | } |
| 6640 | |
| 6641 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6642 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6643 | Builder.AddPlaceholderChunk("object-type"); |
| 6644 | Builder.AddTextChunk(" **"); |
| 6645 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6646 | Builder.AddTextChunk("buffer"); |
| 6647 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6648 | Builder.AddTypedTextChunk("range:"); |
| 6649 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6650 | Builder.AddTextChunk("NSRange"); |
| 6651 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6652 | Builder.AddTextChunk("inRange"); |
| 6653 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 6654 | CXCursor_ObjCInstanceMethodDecl)); |
| 6655 | } |
| 6656 | } |
| 6657 | |
| 6658 | // Mutable indexed accessors |
| 6659 | |
| 6660 | // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index |
| 6661 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6662 | std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6663 | IdentifierInfo *SelectorIds[2] = { |
| 6664 | &Context.Idents.get("insertObject"), |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6665 | &Context.Idents.get(SelectorName) |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6666 | }; |
| 6667 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6668 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6669 | if (ReturnType.isNull()) { |
| 6670 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6671 | Builder.AddTextChunk("void"); |
| 6672 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6673 | } |
| 6674 | |
| 6675 | Builder.AddTypedTextChunk("insertObject:"); |
| 6676 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6677 | Builder.AddPlaceholderChunk("object-type"); |
| 6678 | Builder.AddTextChunk(" *"); |
| 6679 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6680 | Builder.AddTextChunk("object"); |
| 6681 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6682 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6683 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6684 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 6685 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6686 | Builder.AddTextChunk("index"); |
| 6687 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6688 | CXCursor_ObjCInstanceMethodDecl)); |
| 6689 | } |
| 6690 | } |
| 6691 | |
| 6692 | // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes |
| 6693 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6694 | std::string SelectorName = (Twine("insert") + UpperKey).str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6695 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6696 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6697 | &Context.Idents.get("atIndexes") |
| 6698 | }; |
| 6699 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6700 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6701 | if (ReturnType.isNull()) { |
| 6702 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6703 | Builder.AddTextChunk("void"); |
| 6704 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6705 | } |
| 6706 | |
| 6707 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6708 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6709 | Builder.AddTextChunk("NSArray *"); |
| 6710 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6711 | Builder.AddTextChunk("array"); |
| 6712 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6713 | Builder.AddTypedTextChunk("atIndexes:"); |
| 6714 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6715 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 6716 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6717 | Builder.AddTextChunk("indexes"); |
| 6718 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6719 | CXCursor_ObjCInstanceMethodDecl)); |
| 6720 | } |
| 6721 | } |
| 6722 | |
| 6723 | // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index |
| 6724 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6725 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6726 | = (Twine("removeObjectFrom") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6727 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6728 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6729 | if (ReturnType.isNull()) { |
| 6730 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6731 | Builder.AddTextChunk("void"); |
| 6732 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6733 | } |
| 6734 | |
| 6735 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6736 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6737 | Builder.AddTextChunk("NSUInteger"); |
| 6738 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6739 | Builder.AddTextChunk("index"); |
| 6740 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6741 | CXCursor_ObjCInstanceMethodDecl)); |
| 6742 | } |
| 6743 | } |
| 6744 | |
| 6745 | // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes |
| 6746 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6747 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6748 | = (Twine("remove") + UpperKey + "AtIndexes").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6749 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6750 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6751 | if (ReturnType.isNull()) { |
| 6752 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6753 | Builder.AddTextChunk("void"); |
| 6754 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6755 | } |
| 6756 | |
| 6757 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6758 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6759 | Builder.AddTextChunk("NSIndexSet *"); |
| 6760 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6761 | Builder.AddTextChunk("indexes"); |
| 6762 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6763 | CXCursor_ObjCInstanceMethodDecl)); |
| 6764 | } |
| 6765 | } |
| 6766 | |
| 6767 | // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object |
| 6768 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6769 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6770 | = (Twine("replaceObjectIn") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6771 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6772 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6773 | &Context.Idents.get("withObject") |
| 6774 | }; |
| 6775 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6776 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6777 | if (ReturnType.isNull()) { |
| 6778 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6779 | Builder.AddTextChunk("void"); |
| 6780 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6781 | } |
| 6782 | |
| 6783 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6784 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6785 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 6786 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6787 | Builder.AddTextChunk("index"); |
| 6788 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6789 | Builder.AddTypedTextChunk("withObject:"); |
| 6790 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6791 | Builder.AddTextChunk("id"); |
| 6792 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6793 | Builder.AddTextChunk("object"); |
| 6794 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6795 | CXCursor_ObjCInstanceMethodDecl)); |
| 6796 | } |
| 6797 | } |
| 6798 | |
| 6799 | // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array |
| 6800 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6801 | std::string SelectorName1 |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6802 | = (Twine("replace") + UpperKey + "AtIndexes").str(); |
| 6803 | std::string SelectorName2 = (Twine("with") + UpperKey).str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6804 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6805 | &Context.Idents.get(SelectorName1), |
| 6806 | &Context.Idents.get(SelectorName2) |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6807 | }; |
| 6808 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6809 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6810 | if (ReturnType.isNull()) { |
| 6811 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6812 | Builder.AddTextChunk("void"); |
| 6813 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6814 | } |
| 6815 | |
| 6816 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":")); |
| 6817 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6818 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 6819 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6820 | Builder.AddTextChunk("indexes"); |
| 6821 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6822 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":")); |
| 6823 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6824 | Builder.AddTextChunk("NSArray *"); |
| 6825 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6826 | Builder.AddTextChunk("array"); |
| 6827 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6828 | CXCursor_ObjCInstanceMethodDecl)); |
| 6829 | } |
| 6830 | } |
| 6831 | |
| 6832 | // Unordered getters |
| 6833 | // - (NSEnumerator *)enumeratorOfKey |
| 6834 | if (IsInstanceMethod && |
| 6835 | (ReturnType.isNull() || |
| 6836 | (ReturnType->isObjCObjectPointerType() && |
| 6837 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 6838 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 6839 | ->getName() == "NSEnumerator"))) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6840 | std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6841 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6842 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 6843 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6844 | if (ReturnType.isNull()) { |
| 6845 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6846 | Builder.AddTextChunk("NSEnumerator *"); |
| 6847 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6848 | } |
| 6849 | |
| 6850 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 6851 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
| 6852 | CXCursor_ObjCInstanceMethodDecl)); |
| 6853 | } |
| 6854 | } |
| 6855 | |
| 6856 | // - (type *)memberOfKey:(type *)object |
| 6857 | if (IsInstanceMethod && |
| 6858 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6859 | std::string SelectorName = (Twine("memberOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6860 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6861 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6862 | if (ReturnType.isNull()) { |
| 6863 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6864 | Builder.AddPlaceholderChunk("object-type"); |
| 6865 | Builder.AddTextChunk(" *"); |
| 6866 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6867 | } |
| 6868 | |
| 6869 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6870 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6871 | if (ReturnType.isNull()) { |
| 6872 | Builder.AddPlaceholderChunk("object-type"); |
| 6873 | Builder.AddTextChunk(" *"); |
| 6874 | } else { |
| 6875 | Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6876 | Policy, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6877 | Builder.getAllocator())); |
| 6878 | } |
| 6879 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6880 | Builder.AddTextChunk("object"); |
| 6881 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
| 6882 | CXCursor_ObjCInstanceMethodDecl)); |
| 6883 | } |
| 6884 | } |
| 6885 | |
| 6886 | // Mutable unordered accessors |
| 6887 | // - (void)addKeyObject:(type *)object |
| 6888 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6889 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6890 | = (Twine("add") + UpperKey + Twine("Object")).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6891 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6892 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6893 | if (ReturnType.isNull()) { |
| 6894 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6895 | Builder.AddTextChunk("void"); |
| 6896 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6897 | } |
| 6898 | |
| 6899 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6900 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6901 | Builder.AddPlaceholderChunk("object-type"); |
| 6902 | Builder.AddTextChunk(" *"); |
| 6903 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6904 | Builder.AddTextChunk("object"); |
| 6905 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6906 | CXCursor_ObjCInstanceMethodDecl)); |
| 6907 | } |
| 6908 | } |
| 6909 | |
| 6910 | // - (void)addKey:(NSSet *)objects |
| 6911 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6912 | std::string SelectorName = (Twine("add") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6913 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6914 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6915 | if (ReturnType.isNull()) { |
| 6916 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6917 | Builder.AddTextChunk("void"); |
| 6918 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6919 | } |
| 6920 | |
| 6921 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6922 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6923 | Builder.AddTextChunk("NSSet *"); |
| 6924 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6925 | Builder.AddTextChunk("objects"); |
| 6926 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6927 | CXCursor_ObjCInstanceMethodDecl)); |
| 6928 | } |
| 6929 | } |
| 6930 | |
| 6931 | // - (void)removeKeyObject:(type *)object |
| 6932 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6933 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6934 | = (Twine("remove") + UpperKey + Twine("Object")).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6935 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6936 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6937 | if (ReturnType.isNull()) { |
| 6938 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6939 | Builder.AddTextChunk("void"); |
| 6940 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6941 | } |
| 6942 | |
| 6943 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6944 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6945 | Builder.AddPlaceholderChunk("object-type"); |
| 6946 | Builder.AddTextChunk(" *"); |
| 6947 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6948 | Builder.AddTextChunk("object"); |
| 6949 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6950 | CXCursor_ObjCInstanceMethodDecl)); |
| 6951 | } |
| 6952 | } |
| 6953 | |
| 6954 | // - (void)removeKey:(NSSet *)objects |
| 6955 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6956 | std::string SelectorName = (Twine("remove") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6957 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6958 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6959 | if (ReturnType.isNull()) { |
| 6960 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6961 | Builder.AddTextChunk("void"); |
| 6962 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6963 | } |
| 6964 | |
| 6965 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6966 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6967 | Builder.AddTextChunk("NSSet *"); |
| 6968 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6969 | Builder.AddTextChunk("objects"); |
| 6970 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6971 | CXCursor_ObjCInstanceMethodDecl)); |
| 6972 | } |
| 6973 | } |
| 6974 | |
| 6975 | // - (void)intersectKey:(NSSet *)objects |
| 6976 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6977 | std::string SelectorName = (Twine("intersect") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6978 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6979 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6980 | if (ReturnType.isNull()) { |
| 6981 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6982 | Builder.AddTextChunk("void"); |
| 6983 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6984 | } |
| 6985 | |
| 6986 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6987 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6988 | Builder.AddTextChunk("NSSet *"); |
| 6989 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6990 | Builder.AddTextChunk("objects"); |
| 6991 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6992 | CXCursor_ObjCInstanceMethodDecl)); |
| 6993 | } |
| 6994 | } |
| 6995 | |
| 6996 | // Key-Value Observing |
| 6997 | // + (NSSet *)keyPathsForValuesAffectingKey |
| 6998 | if (!IsInstanceMethod && |
| 6999 | (ReturnType.isNull() || |
| 7000 | (ReturnType->isObjCObjectPointerType() && |
| 7001 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 7002 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 7003 | ->getName() == "NSSet"))) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7004 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7005 | = (Twine("keyPathsForValuesAffecting") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7006 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7007 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7008 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7009 | if (ReturnType.isNull()) { |
| 7010 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7011 | Builder.AddTextChunk("NSSet *"); |
| 7012 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7013 | } |
| 7014 | |
| 7015 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 7016 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7017 | CXCursor_ObjCClassMethodDecl)); |
| 7018 | } |
| 7019 | } |
| 7020 | |
| 7021 | // + (BOOL)automaticallyNotifiesObserversForKey |
| 7022 | if (!IsInstanceMethod && |
| 7023 | (ReturnType.isNull() || |
| 7024 | ReturnType->isIntegerType() || |
| 7025 | ReturnType->isBooleanType())) { |
| 7026 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7027 | = (Twine("automaticallyNotifiesObserversOf") + UpperKey).str(); |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7028 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7029 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7030 | .second) { |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7031 | if (ReturnType.isNull()) { |
| 7032 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7033 | Builder.AddTextChunk("BOOL"); |
| 7034 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7035 | } |
| 7036 | |
| 7037 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 7038 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 7039 | CXCursor_ObjCClassMethodDecl)); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7040 | } |
| 7041 | } |
| 7042 | } |
| 7043 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7044 | void Sema::CodeCompleteObjCMethodDecl(Scope *S, |
| 7045 | bool IsInstanceMethod, |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7046 | ParsedType ReturnTy) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7047 | // Determine the return type of the method we're declaring, if |
| 7048 | // provided. |
| 7049 | QualType ReturnType = GetTypeFromParser(ReturnTy); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7050 | Decl *IDecl = nullptr; |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7051 | if (CurContext->isObjCContainer()) { |
| 7052 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
| 7053 | IDecl = cast<Decl>(OCD); |
| 7054 | } |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7055 | // Determine where we should start searching for methods. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7056 | ObjCContainerDecl *SearchDecl = nullptr; |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7057 | bool IsInImplementation = false; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7058 | if (Decl *D = IDecl) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7059 | if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) { |
| 7060 | SearchDecl = Impl->getClassInterface(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7061 | IsInImplementation = true; |
| 7062 | } else if (ObjCCategoryImplDecl *CatImpl |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7063 | = dyn_cast<ObjCCategoryImplDecl>(D)) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7064 | SearchDecl = CatImpl->getCategoryDecl(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7065 | IsInImplementation = true; |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7066 | } else |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7067 | SearchDecl = dyn_cast<ObjCContainerDecl>(D); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7068 | } |
| 7069 | |
| 7070 | if (!SearchDecl && S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 7071 | if (DeclContext *DC = S->getEntity()) |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7072 | SearchDecl = dyn_cast<ObjCContainerDecl>(DC); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7073 | } |
| 7074 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7075 | if (!SearchDecl) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 7076 | HandleCodeCompleteResults(this, CodeCompleter, |
| 7077 | CodeCompletionContext::CCC_Other, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7078 | nullptr, 0); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7079 | return; |
| 7080 | } |
| 7081 | |
| 7082 | // Find all of the methods that we could declare/implement here. |
| 7083 | KnownMethodsMap KnownMethods; |
| 7084 | FindImplementableMethods(Context, SearchDecl, IsInstanceMethod, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7085 | ReturnType, KnownMethods); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7086 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7087 | // Add declarations or definitions for each of the known methods. |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7088 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7089 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7090 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7091 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7092 | Results.EnterNewScope(); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7093 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7094 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
| 7095 | MEnd = KnownMethods.end(); |
| 7096 | M != MEnd; ++M) { |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 7097 | ObjCMethodDecl *Method = M->second.getPointer(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7098 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7099 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7100 | |
| 7101 | // If the result type was not already provided, add it to the |
| 7102 | // pattern as (type). |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7103 | if (ReturnType.isNull()) { |
| 7104 | QualType ResTy = Method->getSendResultType().stripObjCKindOfType(Context); |
| 7105 | AttributedType::stripOuterNullability(ResTy); |
| 7106 | AddObjCPassingTypeChunk(ResTy, |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7107 | Method->getObjCDeclQualifier(), Context, Policy, |
| 7108 | Builder); |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7109 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7110 | |
| 7111 | Selector Sel = Method->getSelector(); |
| 7112 | |
| 7113 | // Add the first part of the selector to the pattern. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7114 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 7115 | Sel.getNameForSlot(0))); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7116 | |
| 7117 | // Add parameters to the pattern. |
| 7118 | unsigned I = 0; |
| 7119 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 7120 | PEnd = Method->param_end(); |
| 7121 | P != PEnd; (void)++P, ++I) { |
| 7122 | // Add the part of the selector name. |
| 7123 | if (I == 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7124 | Builder.AddTypedTextChunk(":"); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7125 | else if (I < Sel.getNumArgs()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7126 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7127 | Builder.AddTypedTextChunk( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 7128 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7129 | } else |
| 7130 | break; |
| 7131 | |
| 7132 | // Add the parameter type. |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 7133 | QualType ParamType; |
| 7134 | if ((*P)->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 7135 | ParamType = (*P)->getType(); |
| 7136 | else |
| 7137 | ParamType = (*P)->getOriginalType(); |
Douglas Gregor | 9b7b3e9 | 2015-07-07 06:20:27 +0000 | [diff] [blame] | 7138 | ParamType = ParamType.substObjCTypeArgs(Context, {}, |
| 7139 | ObjCSubstitutionContext::Parameter); |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7140 | AttributedType::stripOuterNullability(ParamType); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 7141 | AddObjCPassingTypeChunk(ParamType, |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 7142 | (*P)->getObjCDeclQualifier(), |
| 7143 | Context, Policy, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7144 | Builder); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7145 | |
| 7146 | if (IdentifierInfo *Id = (*P)->getIdentifier()) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7147 | Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName())); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7148 | } |
| 7149 | |
| 7150 | if (Method->isVariadic()) { |
| 7151 | if (Method->param_size() > 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7152 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 7153 | Builder.AddTextChunk("..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 7154 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7155 | |
Douglas Gregor | d37c59d | 2010-05-28 00:57:46 +0000 | [diff] [blame] | 7156 | if (IsInImplementation && Results.includeCodePatterns()) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7157 | // We will be defining the method here, so add a compound statement. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7158 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7159 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 7160 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7161 | if (!Method->getReturnType()->isVoidType()) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7162 | // If the result type is not void, add a return clause. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7163 | Builder.AddTextChunk("return"); |
| 7164 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7165 | Builder.AddPlaceholderChunk("expression"); |
| 7166 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7167 | } else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7168 | Builder.AddPlaceholderChunk("statements"); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7169 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7170 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 7171 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7172 | } |
| 7173 | |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 7174 | unsigned Priority = CCP_CodePattern; |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 7175 | if (!M->second.getInt()) |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 7176 | Priority += CCD_InBaseClass; |
| 7177 | |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 7178 | Results.AddResult(Result(Builder.TakeString(), Method, Priority)); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7179 | } |
| 7180 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7181 | // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of |
| 7182 | // the properties in this class and its categories. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7183 | if (Context.getLangOpts().ObjC2) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7184 | SmallVector<ObjCContainerDecl *, 4> Containers; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7185 | Containers.push_back(SearchDecl); |
| 7186 | |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 7187 | VisitedSelectorSet KnownSelectors; |
| 7188 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
| 7189 | MEnd = KnownMethods.end(); |
| 7190 | M != MEnd; ++M) |
| 7191 | KnownSelectors.insert(M->first); |
| 7192 | |
| 7193 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7194 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl); |
| 7195 | if (!IFace) |
| 7196 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl)) |
| 7197 | IFace = Category->getClassInterface(); |
| 7198 | |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 7199 | if (IFace) |
| 7200 | for (auto *Cat : IFace->visible_categories()) |
| 7201 | Containers.push_back(Cat); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7202 | |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 7203 | for (unsigned I = 0, N = Containers.size(); I != N; ++I) |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 7204 | for (auto *P : Containers[I]->instance_properties()) |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 7205 | AddObjCKeyValueCompletions(P, IsInstanceMethod, ReturnType, Context, |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 7206 | KnownSelectors, Results); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7207 | } |
| 7208 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7209 | Results.ExitScope(); |
| 7210 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 7211 | HandleCodeCompleteResults(this, CodeCompleter, |
| 7212 | CodeCompletionContext::CCC_Other, |
| 7213 | Results.data(),Results.size()); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7214 | } |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7215 | |
| 7216 | void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S, |
| 7217 | bool IsInstanceMethod, |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7218 | bool AtParameterName, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7219 | ParsedType ReturnTy, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7220 | ArrayRef<IdentifierInfo *> SelIdents) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7221 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 7222 | // pool from the AST file. |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7223 | if (ExternalSource) { |
| 7224 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 7225 | I != N; ++I) { |
| 7226 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7227 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7228 | continue; |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7229 | |
| 7230 | ReadMethodPool(Sel); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7231 | } |
| 7232 | } |
| 7233 | |
| 7234 | // Build the set of methods we can see. |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7235 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7236 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7237 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7238 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7239 | |
| 7240 | if (ReturnTy) |
| 7241 | Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType()); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7242 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7243 | Results.EnterNewScope(); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7244 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 7245 | MEnd = MethodPool.end(); |
| 7246 | M != MEnd; ++M) { |
| 7247 | for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first : |
| 7248 | &M->second.second; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7249 | MethList && MethList->getMethod(); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 7250 | MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7251 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7252 | continue; |
| 7253 | |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7254 | if (AtParameterName) { |
| 7255 | // Suggest parameter names we've seen before. |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7256 | unsigned NumSelIdents = SelIdents.size(); |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7257 | if (NumSelIdents && |
| 7258 | NumSelIdents <= MethList->getMethod()->param_size()) { |
| 7259 | ParmVarDecl *Param = |
| 7260 | MethList->getMethod()->parameters()[NumSelIdents - 1]; |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7261 | if (Param->getIdentifier()) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7262 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7263 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7264 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7265 | Param->getIdentifier()->getName())); |
| 7266 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7267 | } |
| 7268 | } |
| 7269 | |
| 7270 | continue; |
| 7271 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7272 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7273 | Result R(MethList->getMethod(), |
| 7274 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7275 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7276 | R.AllParametersAreInformative = false; |
| 7277 | R.DeclaringEntity = true; |
| 7278 | Results.MaybeAddResult(R, CurContext); |
| 7279 | } |
| 7280 | } |
| 7281 | |
| 7282 | Results.ExitScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 7283 | HandleCodeCompleteResults(this, CodeCompleter, |
| 7284 | CodeCompletionContext::CCC_Other, |
| 7285 | Results.data(),Results.size()); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7286 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 7287 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7288 | void Sema::CodeCompletePreprocessorDirective(bool InConditional) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7289 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7290 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7291 | CodeCompletionContext::CCC_PreprocessorDirective); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7292 | Results.EnterNewScope(); |
| 7293 | |
| 7294 | // #if <condition> |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7295 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7296 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7297 | Builder.AddTypedTextChunk("if"); |
| 7298 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7299 | Builder.AddPlaceholderChunk("condition"); |
| 7300 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7301 | |
| 7302 | // #ifdef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7303 | Builder.AddTypedTextChunk("ifdef"); |
| 7304 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7305 | Builder.AddPlaceholderChunk("macro"); |
| 7306 | Results.AddResult(Builder.TakeString()); |
| 7307 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7308 | // #ifndef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7309 | Builder.AddTypedTextChunk("ifndef"); |
| 7310 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7311 | Builder.AddPlaceholderChunk("macro"); |
| 7312 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7313 | |
| 7314 | if (InConditional) { |
| 7315 | // #elif <condition> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7316 | Builder.AddTypedTextChunk("elif"); |
| 7317 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7318 | Builder.AddPlaceholderChunk("condition"); |
| 7319 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7320 | |
| 7321 | // #else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7322 | Builder.AddTypedTextChunk("else"); |
| 7323 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7324 | |
| 7325 | // #endif |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7326 | Builder.AddTypedTextChunk("endif"); |
| 7327 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7328 | } |
| 7329 | |
| 7330 | // #include "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7331 | Builder.AddTypedTextChunk("include"); |
| 7332 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7333 | Builder.AddTextChunk("\""); |
| 7334 | Builder.AddPlaceholderChunk("header"); |
| 7335 | Builder.AddTextChunk("\""); |
| 7336 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7337 | |
| 7338 | // #include <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7339 | Builder.AddTypedTextChunk("include"); |
| 7340 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7341 | Builder.AddTextChunk("<"); |
| 7342 | Builder.AddPlaceholderChunk("header"); |
| 7343 | Builder.AddTextChunk(">"); |
| 7344 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7345 | |
| 7346 | // #define <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7347 | Builder.AddTypedTextChunk("define"); |
| 7348 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7349 | Builder.AddPlaceholderChunk("macro"); |
| 7350 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7351 | |
| 7352 | // #define <macro>(<args>) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7353 | Builder.AddTypedTextChunk("define"); |
| 7354 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7355 | Builder.AddPlaceholderChunk("macro"); |
| 7356 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7357 | Builder.AddPlaceholderChunk("args"); |
| 7358 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7359 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7360 | |
| 7361 | // #undef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7362 | Builder.AddTypedTextChunk("undef"); |
| 7363 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7364 | Builder.AddPlaceholderChunk("macro"); |
| 7365 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7366 | |
| 7367 | // #line <number> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7368 | Builder.AddTypedTextChunk("line"); |
| 7369 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7370 | Builder.AddPlaceholderChunk("number"); |
| 7371 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7372 | |
| 7373 | // #line <number> "filename" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7374 | Builder.AddTypedTextChunk("line"); |
| 7375 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7376 | Builder.AddPlaceholderChunk("number"); |
| 7377 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7378 | Builder.AddTextChunk("\""); |
| 7379 | Builder.AddPlaceholderChunk("filename"); |
| 7380 | Builder.AddTextChunk("\""); |
| 7381 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7382 | |
| 7383 | // #error <message> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7384 | Builder.AddTypedTextChunk("error"); |
| 7385 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7386 | Builder.AddPlaceholderChunk("message"); |
| 7387 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7388 | |
| 7389 | // #pragma <arguments> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7390 | Builder.AddTypedTextChunk("pragma"); |
| 7391 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7392 | Builder.AddPlaceholderChunk("arguments"); |
| 7393 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7394 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7395 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7396 | // #import "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7397 | Builder.AddTypedTextChunk("import"); |
| 7398 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7399 | Builder.AddTextChunk("\""); |
| 7400 | Builder.AddPlaceholderChunk("header"); |
| 7401 | Builder.AddTextChunk("\""); |
| 7402 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7403 | |
| 7404 | // #import <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7405 | Builder.AddTypedTextChunk("import"); |
| 7406 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7407 | Builder.AddTextChunk("<"); |
| 7408 | Builder.AddPlaceholderChunk("header"); |
| 7409 | Builder.AddTextChunk(">"); |
| 7410 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7411 | } |
| 7412 | |
| 7413 | // #include_next "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7414 | Builder.AddTypedTextChunk("include_next"); |
| 7415 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7416 | Builder.AddTextChunk("\""); |
| 7417 | Builder.AddPlaceholderChunk("header"); |
| 7418 | Builder.AddTextChunk("\""); |
| 7419 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7420 | |
| 7421 | // #include_next <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7422 | Builder.AddTypedTextChunk("include_next"); |
| 7423 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7424 | Builder.AddTextChunk("<"); |
| 7425 | Builder.AddPlaceholderChunk("header"); |
| 7426 | Builder.AddTextChunk(">"); |
| 7427 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7428 | |
| 7429 | // #warning <message> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7430 | Builder.AddTypedTextChunk("warning"); |
| 7431 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7432 | Builder.AddPlaceholderChunk("message"); |
| 7433 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7434 | |
| 7435 | // Note: #ident and #sccs are such crazy anachronisms that we don't provide |
| 7436 | // completions for them. And __include_macros is a Clang-internal extension |
| 7437 | // that we don't want to encourage anyone to use. |
| 7438 | |
| 7439 | // FIXME: we don't support #assert or #unassert, so don't suggest them. |
| 7440 | Results.ExitScope(); |
| 7441 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7442 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 7443 | CodeCompletionContext::CCC_PreprocessorDirective, |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7444 | Results.data(), Results.size()); |
| 7445 | } |
| 7446 | |
| 7447 | void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) { |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7448 | CodeCompleteOrdinaryName(S, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7449 | S->getFnParent()? Sema::PCC_RecoveryInFunction |
| 7450 | : Sema::PCC_Namespace); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7451 | } |
| 7452 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7453 | void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7454 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7455 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7456 | IsDefinition? CodeCompletionContext::CCC_MacroName |
| 7457 | : CodeCompletionContext::CCC_MacroNameUse); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7458 | if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) { |
| 7459 | // Add just the names of macros, not their arguments. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7460 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7461 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7462 | Results.EnterNewScope(); |
| 7463 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 7464 | MEnd = PP.macro_end(); |
| 7465 | M != MEnd; ++M) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7466 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7467 | M->first->getName())); |
Argyrios Kyrtzidis | 5c8b1cd | 2012-09-27 00:24:09 +0000 | [diff] [blame] | 7468 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
| 7469 | CCP_CodePattern, |
| 7470 | CXCursor_MacroDefinition)); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7471 | } |
| 7472 | Results.ExitScope(); |
| 7473 | } else if (IsDefinition) { |
| 7474 | // FIXME: Can we detect when the user just wrote an include guard above? |
| 7475 | } |
| 7476 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7477 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7478 | Results.data(), Results.size()); |
| 7479 | } |
| 7480 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7481 | void Sema::CodeCompletePreprocessorExpression() { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7482 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7483 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7484 | CodeCompletionContext::CCC_PreprocessorExpression); |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7485 | |
| 7486 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 7487 | AddMacroResults(PP, Results, true); |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7488 | |
| 7489 | // defined (<macro>) |
| 7490 | Results.EnterNewScope(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7491 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7492 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7493 | Builder.AddTypedTextChunk("defined"); |
| 7494 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7495 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7496 | Builder.AddPlaceholderChunk("macro"); |
| 7497 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7498 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7499 | Results.ExitScope(); |
| 7500 | |
| 7501 | HandleCodeCompleteResults(this, CodeCompleter, |
| 7502 | CodeCompletionContext::CCC_PreprocessorExpression, |
| 7503 | Results.data(), Results.size()); |
| 7504 | } |
| 7505 | |
| 7506 | void Sema::CodeCompletePreprocessorMacroArgument(Scope *S, |
| 7507 | IdentifierInfo *Macro, |
| 7508 | MacroInfo *MacroInfo, |
| 7509 | unsigned Argument) { |
| 7510 | // FIXME: In the future, we could provide "overload" results, much like we |
| 7511 | // do for function calls. |
| 7512 | |
Argyrios Kyrtzidis | 75f6cd2 | 2011-08-18 19:41:28 +0000 | [diff] [blame] | 7513 | // Now just ignore this. There will be another code-completion callback |
| 7514 | // for the expanded tokens. |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7515 | } |
| 7516 | |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 7517 | void Sema::CodeCompleteNaturalLanguage() { |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 7518 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | ea73637 | 2010-08-25 17:10:00 +0000 | [diff] [blame] | 7519 | CodeCompletionContext::CCC_NaturalLanguage, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7520 | nullptr, 0); |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 7521 | } |
| 7522 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7523 | void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator, |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7524 | CodeCompletionTUInfo &CCTUInfo, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7525 | SmallVectorImpl<CodeCompletionResult> &Results) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7526 | ResultBuilder Builder(*this, Allocator, CCTUInfo, |
| 7527 | CodeCompletionContext::CCC_Recovery); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 7528 | if (!CodeCompleter || CodeCompleter->includeGlobals()) { |
| 7529 | CodeCompletionDeclConsumer Consumer(Builder, |
| 7530 | Context.getTranslationUnitDecl()); |
| 7531 | LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName, |
| 7532 | Consumer); |
| 7533 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 7534 | |
| 7535 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 7536 | AddMacroResults(PP, Builder, true); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 7537 | |
| 7538 | Results.clear(); |
| 7539 | Results.insert(Results.end(), |
| 7540 | Builder.data(), Builder.data() + Builder.size()); |
| 7541 | } |