Douglas Gregor | 81b747b | 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 | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 13 | #include "clang/Sema/SemaInternal.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 15 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprObjC.h" |
Jordan Rose | 223f0ff | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 17 | #include "clang/Basic/CharInfo.h" |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 18 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 19 | #include "clang/Lex/MacroInfo.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Sema/CodeCompleteConsumer.h" |
| 22 | #include "clang/Sema/ExternalSemaSource.h" |
| 23 | #include "clang/Sema/Lookup.h" |
| 24 | #include "clang/Sema/Overload.h" |
| 25 | #include "clang/Sema/Scope.h" |
| 26 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/DenseSet.h" |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallBitVector.h" |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallPtrSet.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 6a68403 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/StringSwitch.h" |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 34 | #include <list> |
| 35 | #include <map> |
| 36 | #include <vector> |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 39 | using namespace sema; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 40 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 41 | namespace { |
| 42 | /// \brief A container of code-completion results. |
| 43 | class ResultBuilder { |
| 44 | public: |
| 45 | /// \brief The type of a name-lookup filter, which can be provided to the |
| 46 | /// name-lookup routines to specify which declarations should be included in |
| 47 | /// the result set (when it returns true) and which declarations should be |
| 48 | /// filtered out (returns false). |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 49 | typedef bool (ResultBuilder::*LookupFilter)(const NamedDecl *) const; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 50 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 51 | typedef CodeCompletionResult Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 52 | |
| 53 | private: |
| 54 | /// \brief The actual results we have found. |
| 55 | std::vector<Result> Results; |
| 56 | |
| 57 | /// \brief A record of all of the declarations we have found and placed |
| 58 | /// into the result set, used to ensure that no declaration ever gets into |
| 59 | /// the result set twice. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 60 | llvm::SmallPtrSet<const Decl*, 16> AllDeclsFound; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 61 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 62 | typedef std::pair<const NamedDecl *, unsigned> DeclIndexPair; |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 63 | |
| 64 | /// \brief An entry in the shadow map, which is optimized to store |
| 65 | /// a single (declaration, index) mapping (the common case) but |
| 66 | /// can also store a list of (declaration, index) mappings. |
| 67 | class ShadowMapEntry { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 68 | typedef SmallVector<DeclIndexPair, 4> DeclIndexPairVector; |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 69 | |
| 70 | /// \brief Contains either the solitary NamedDecl * or a vector |
| 71 | /// of (declaration, index) pairs. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 72 | llvm::PointerUnion<const NamedDecl *, DeclIndexPairVector*> DeclOrVector; |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 73 | |
| 74 | /// \brief When the entry contains a single declaration, this is |
| 75 | /// the index associated with that entry. |
| 76 | unsigned SingleDeclIndex; |
| 77 | |
| 78 | public: |
| 79 | ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { } |
| 80 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 81 | void Add(const NamedDecl *ND, unsigned Index) { |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 82 | if (DeclOrVector.isNull()) { |
| 83 | // 0 - > 1 elements: just set the single element information. |
| 84 | DeclOrVector = ND; |
| 85 | SingleDeclIndex = Index; |
| 86 | return; |
| 87 | } |
| 88 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 89 | if (const NamedDecl *PrevND = |
| 90 | DeclOrVector.dyn_cast<const NamedDecl *>()) { |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 91 | // 1 -> 2 elements: create the vector of results and push in the |
| 92 | // existing declaration. |
| 93 | DeclIndexPairVector *Vec = new DeclIndexPairVector; |
| 94 | Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex)); |
| 95 | DeclOrVector = Vec; |
| 96 | } |
| 97 | |
| 98 | // Add the new element to the end of the vector. |
| 99 | DeclOrVector.get<DeclIndexPairVector*>()->push_back( |
| 100 | DeclIndexPair(ND, Index)); |
| 101 | } |
| 102 | |
| 103 | void Destroy() { |
| 104 | if (DeclIndexPairVector *Vec |
| 105 | = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) { |
| 106 | delete Vec; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 107 | DeclOrVector = ((NamedDecl *)nullptr); |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | // Iteration. |
| 112 | class iterator; |
| 113 | iterator begin() const; |
| 114 | iterator end() const; |
| 115 | }; |
| 116 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 117 | /// \brief A mapping from declaration names to the declarations that have |
| 118 | /// this name within a particular scope and their index within the list of |
| 119 | /// results. |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 120 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 121 | |
| 122 | /// \brief The semantic analysis object for which results are being |
| 123 | /// produced. |
| 124 | Sema &SemaRef; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 125 | |
| 126 | /// \brief The allocator used to allocate new code-completion strings. |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 127 | CodeCompletionAllocator &Allocator; |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 128 | |
| 129 | CodeCompletionTUInfo &CCTUInfo; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 130 | |
| 131 | /// \brief If non-NULL, a filter function used to remove any code-completion |
| 132 | /// results that are not desirable. |
| 133 | LookupFilter Filter; |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 134 | |
| 135 | /// \brief Whether we should allow declarations as |
| 136 | /// nested-name-specifiers that would otherwise be filtered out. |
| 137 | bool AllowNestedNameSpecifiers; |
| 138 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 139 | /// \brief If set, the type that we would prefer our resulting value |
| 140 | /// declarations to have. |
| 141 | /// |
| 142 | /// Closely matching the preferred type gives a boost to a result's |
| 143 | /// priority. |
| 144 | CanQualType PreferredType; |
| 145 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 146 | /// \brief A list of shadow maps, which is used to model name hiding at |
| 147 | /// different levels of, e.g., the inheritance hierarchy. |
| 148 | std::list<ShadowMap> ShadowMaps; |
| 149 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 150 | /// \brief If we're potentially referring to a C++ member function, the set |
| 151 | /// of qualifiers applied to the object type. |
| 152 | Qualifiers ObjectTypeQualifiers; |
| 153 | |
| 154 | /// \brief Whether the \p ObjectTypeQualifiers field is active. |
| 155 | bool HasObjectTypeQualifiers; |
| 156 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 157 | /// \brief The selector that we prefer. |
| 158 | Selector PreferredSelector; |
| 159 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 160 | /// \brief The completion context in which we are gathering results. |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 161 | CodeCompletionContext CompletionContext; |
| 162 | |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 163 | /// \brief If we are in an instance method definition, the \@implementation |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 164 | /// object. |
| 165 | ObjCImplementationDecl *ObjCImplementation; |
Douglas Gregor | d1f09b4 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 166 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 167 | void AdjustResultPriorityForDecl(Result &R); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 168 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 169 | void MaybeAddConstructorResults(Result R); |
| 170 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 171 | public: |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 172 | explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator, |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 173 | CodeCompletionTUInfo &CCTUInfo, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 174 | const CodeCompletionContext &CompletionContext, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 175 | LookupFilter Filter = nullptr) |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 176 | : SemaRef(SemaRef), Allocator(Allocator), CCTUInfo(CCTUInfo), |
| 177 | Filter(Filter), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 178 | AllowNestedNameSpecifiers(false), HasObjectTypeQualifiers(false), |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 179 | CompletionContext(CompletionContext), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 180 | ObjCImplementation(nullptr) |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 181 | { |
| 182 | // If this is an Objective-C instance method definition, dig out the |
| 183 | // corresponding implementation. |
| 184 | switch (CompletionContext.getKind()) { |
| 185 | case CodeCompletionContext::CCC_Expression: |
| 186 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
| 187 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
| 188 | case CodeCompletionContext::CCC_Statement: |
| 189 | case CodeCompletionContext::CCC_Recovery: |
| 190 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) |
| 191 | if (Method->isInstanceMethod()) |
| 192 | if (ObjCInterfaceDecl *Interface = Method->getClassInterface()) |
| 193 | ObjCImplementation = Interface->getImplementation(); |
| 194 | break; |
| 195 | |
| 196 | default: |
| 197 | break; |
| 198 | } |
| 199 | } |
Douglas Gregor | d1f09b4 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 200 | |
| 201 | /// \brief Determine the priority for a reference to the given declaration. |
| 202 | unsigned getBasePriority(const NamedDecl *D); |
| 203 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 204 | /// \brief Whether we should include code patterns in the completion |
| 205 | /// results. |
| 206 | bool includeCodePatterns() const { |
| 207 | return SemaRef.CodeCompleter && |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 208 | SemaRef.CodeCompleter->includeCodePatterns(); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 211 | /// \brief Set the filter used for code-completion results. |
| 212 | void setFilter(LookupFilter Filter) { |
| 213 | this->Filter = Filter; |
| 214 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 215 | |
| 216 | Result *data() { return Results.empty()? nullptr : &Results.front(); } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 217 | unsigned size() const { return Results.size(); } |
| 218 | bool empty() const { return Results.empty(); } |
| 219 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 220 | /// \brief Specify the preferred type. |
| 221 | void setPreferredType(QualType T) { |
| 222 | PreferredType = SemaRef.Context.getCanonicalType(T); |
| 223 | } |
| 224 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 225 | /// \brief Set the cv-qualifiers on the object type, for us in filtering |
| 226 | /// calls to member functions. |
| 227 | /// |
| 228 | /// When there are qualifiers in this set, they will be used to filter |
| 229 | /// out member functions that aren't available (because there will be a |
| 230 | /// cv-qualifier mismatch) or prefer functions with an exact qualifier |
| 231 | /// match. |
| 232 | void setObjectTypeQualifiers(Qualifiers Quals) { |
| 233 | ObjectTypeQualifiers = Quals; |
| 234 | HasObjectTypeQualifiers = true; |
| 235 | } |
| 236 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 237 | /// \brief Set the preferred selector. |
| 238 | /// |
| 239 | /// When an Objective-C method declaration result is added, and that |
| 240 | /// method's selector matches this preferred selector, we give that method |
| 241 | /// a slight priority boost. |
| 242 | void setPreferredSelector(Selector Sel) { |
| 243 | PreferredSelector = Sel; |
| 244 | } |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 245 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 246 | /// \brief Retrieve the code-completion context for which results are |
| 247 | /// being collected. |
| 248 | const CodeCompletionContext &getCompletionContext() const { |
| 249 | return CompletionContext; |
| 250 | } |
| 251 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 252 | /// \brief Specify whether nested-name-specifiers are allowed. |
| 253 | void allowNestedNameSpecifiers(bool Allow = true) { |
| 254 | AllowNestedNameSpecifiers = Allow; |
| 255 | } |
| 256 | |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 257 | /// \brief Return the semantic analysis object for which we are collecting |
| 258 | /// code completion results. |
| 259 | Sema &getSema() const { return SemaRef; } |
| 260 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 261 | /// \brief Retrieve the allocator used to allocate code completion strings. |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 262 | CodeCompletionAllocator &getAllocator() const { return Allocator; } |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 263 | |
| 264 | CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 265 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 266 | /// \brief Determine whether the given declaration is at all interesting |
| 267 | /// as a code-completion result. |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 268 | /// |
| 269 | /// \param ND the declaration that we are inspecting. |
| 270 | /// |
| 271 | /// \param AsNestedNameSpecifier will be set true if this declaration is |
| 272 | /// only interesting when it is a nested-name-specifier. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 273 | bool isInterestingDecl(const NamedDecl *ND, |
| 274 | bool &AsNestedNameSpecifier) const; |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 275 | |
| 276 | /// \brief Check whether the result is hidden by the Hiding declaration. |
| 277 | /// |
| 278 | /// \returns true if the result is hidden and cannot be found, false if |
| 279 | /// the hidden result could still be found. When false, \p R may be |
| 280 | /// modified to describe how the result can be found (e.g., via extra |
| 281 | /// qualification). |
| 282 | bool CheckHiddenResult(Result &R, DeclContext *CurContext, |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 283 | const NamedDecl *Hiding); |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 284 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 285 | /// \brief Add a new result to this result set (if it isn't already in one |
| 286 | /// of the shadow maps), or replace an existing result (for, e.g., a |
| 287 | /// redeclaration). |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 288 | /// |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 289 | /// \param R the result to add (if it is unique). |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 290 | /// |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 291 | /// \param CurContext the context in which this result will be named. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 292 | void MaybeAddResult(Result R, DeclContext *CurContext = nullptr); |
| 293 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 294 | /// \brief Add a new result to this result set, where we already know |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 295 | /// the hiding declaration (if any). |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 296 | /// |
| 297 | /// \param R the result to add (if it is unique). |
| 298 | /// |
| 299 | /// \param CurContext the context in which this result will be named. |
| 300 | /// |
| 301 | /// \param Hiding the declaration that hides the result. |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 302 | /// |
| 303 | /// \param InBaseClass whether the result was found in a base |
| 304 | /// class of the searched context. |
| 305 | void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding, |
| 306 | bool InBaseClass); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 307 | |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 308 | /// \brief Add a new non-declaration result to this result set. |
| 309 | void AddResult(Result R); |
| 310 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 311 | /// \brief Enter into a new scope. |
| 312 | void EnterNewScope(); |
| 313 | |
| 314 | /// \brief Exit from the current scope. |
| 315 | void ExitScope(); |
| 316 | |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 317 | /// \brief Ignore this declaration, if it is seen again. |
Dmitri Gribenko | 68a932d | 2013-02-14 13:53:30 +0000 | [diff] [blame] | 318 | void Ignore(const Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); } |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 319 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 320 | /// \name Name lookup predicates |
| 321 | /// |
| 322 | /// These predicates can be passed to the name lookup functions to filter the |
| 323 | /// results of name lookup. All of the predicates have the same type, so that |
| 324 | /// |
| 325 | //@{ |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 326 | bool IsOrdinaryName(const NamedDecl *ND) const; |
| 327 | bool IsOrdinaryNonTypeName(const NamedDecl *ND) const; |
| 328 | bool IsIntegralConstantValue(const NamedDecl *ND) const; |
| 329 | bool IsOrdinaryNonValueName(const NamedDecl *ND) const; |
| 330 | bool IsNestedNameSpecifier(const NamedDecl *ND) const; |
| 331 | bool IsEnum(const NamedDecl *ND) const; |
| 332 | bool IsClassOrStruct(const NamedDecl *ND) const; |
| 333 | bool IsUnion(const NamedDecl *ND) const; |
| 334 | bool IsNamespace(const NamedDecl *ND) const; |
| 335 | bool IsNamespaceOrAlias(const NamedDecl *ND) const; |
| 336 | bool IsType(const NamedDecl *ND) const; |
| 337 | bool IsMember(const NamedDecl *ND) const; |
| 338 | bool IsObjCIvar(const NamedDecl *ND) const; |
| 339 | bool IsObjCMessageReceiver(const NamedDecl *ND) const; |
| 340 | bool IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const; |
| 341 | bool IsObjCCollection(const NamedDecl *ND) const; |
| 342 | bool IsImpossibleToSatisfy(const NamedDecl *ND) const; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 343 | //@} |
| 344 | }; |
| 345 | } |
| 346 | |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 347 | class ResultBuilder::ShadowMapEntry::iterator { |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 348 | llvm::PointerUnion<const NamedDecl *, const DeclIndexPair *> DeclOrIterator; |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 349 | unsigned SingleDeclIndex; |
| 350 | |
| 351 | public: |
| 352 | typedef DeclIndexPair value_type; |
| 353 | typedef value_type reference; |
| 354 | typedef std::ptrdiff_t difference_type; |
| 355 | typedef std::input_iterator_tag iterator_category; |
| 356 | |
| 357 | class pointer { |
| 358 | DeclIndexPair Value; |
| 359 | |
| 360 | public: |
| 361 | pointer(const DeclIndexPair &Value) : Value(Value) { } |
| 362 | |
| 363 | const DeclIndexPair *operator->() const { |
| 364 | return &Value; |
| 365 | } |
| 366 | }; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 367 | |
| 368 | iterator() : DeclOrIterator((NamedDecl *)nullptr), SingleDeclIndex(0) {} |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 369 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 370 | iterator(const NamedDecl *SingleDecl, unsigned Index) |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 371 | : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { } |
| 372 | |
| 373 | iterator(const DeclIndexPair *Iterator) |
| 374 | : DeclOrIterator(Iterator), SingleDeclIndex(0) { } |
| 375 | |
| 376 | iterator &operator++() { |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 377 | if (DeclOrIterator.is<const NamedDecl *>()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 378 | DeclOrIterator = (NamedDecl *)nullptr; |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 379 | SingleDeclIndex = 0; |
| 380 | return *this; |
| 381 | } |
| 382 | |
| 383 | const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>(); |
| 384 | ++I; |
| 385 | DeclOrIterator = I; |
| 386 | return *this; |
| 387 | } |
| 388 | |
Chris Lattner | 66392d4 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 389 | /*iterator operator++(int) { |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 390 | iterator tmp(*this); |
| 391 | ++(*this); |
| 392 | return tmp; |
Chris Lattner | 66392d4 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 393 | }*/ |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 394 | |
| 395 | reference operator*() const { |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 396 | if (const NamedDecl *ND = DeclOrIterator.dyn_cast<const NamedDecl *>()) |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 397 | return reference(ND, SingleDeclIndex); |
| 398 | |
Douglas Gregor | d490f95 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 399 | return *DeclOrIterator.get<const DeclIndexPair*>(); |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | pointer operator->() const { |
| 403 | return pointer(**this); |
| 404 | } |
| 405 | |
| 406 | friend bool operator==(const iterator &X, const iterator &Y) { |
Douglas Gregor | d490f95 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 407 | return X.DeclOrIterator.getOpaqueValue() |
| 408 | == Y.DeclOrIterator.getOpaqueValue() && |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 409 | X.SingleDeclIndex == Y.SingleDeclIndex; |
| 410 | } |
| 411 | |
| 412 | friend bool operator!=(const iterator &X, const iterator &Y) { |
Douglas Gregor | d490f95 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 413 | return !(X == Y); |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 414 | } |
| 415 | }; |
| 416 | |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 417 | ResultBuilder::ShadowMapEntry::iterator |
| 418 | ResultBuilder::ShadowMapEntry::begin() const { |
| 419 | if (DeclOrVector.isNull()) |
| 420 | return iterator(); |
| 421 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 422 | if (const NamedDecl *ND = DeclOrVector.dyn_cast<const NamedDecl *>()) |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 423 | return iterator(ND, SingleDeclIndex); |
| 424 | |
| 425 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin()); |
| 426 | } |
| 427 | |
| 428 | ResultBuilder::ShadowMapEntry::iterator |
| 429 | ResultBuilder::ShadowMapEntry::end() const { |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 430 | if (DeclOrVector.is<const NamedDecl *>() || DeclOrVector.isNull()) |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 431 | return iterator(); |
| 432 | |
| 433 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end()); |
| 434 | } |
| 435 | |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 436 | /// \brief Compute the qualification required to get from the current context |
| 437 | /// (\p CurContext) to the target context (\p TargetContext). |
| 438 | /// |
| 439 | /// \param Context the AST context in which the qualification will be used. |
| 440 | /// |
| 441 | /// \param CurContext the context where an entity is being named, which is |
| 442 | /// typically based on the current scope. |
| 443 | /// |
| 444 | /// \param TargetContext the context in which the named entity actually |
| 445 | /// resides. |
| 446 | /// |
| 447 | /// \returns a nested name specifier that refers into the target context, or |
| 448 | /// NULL if no qualification is needed. |
| 449 | static NestedNameSpecifier * |
| 450 | getRequiredQualification(ASTContext &Context, |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 451 | const DeclContext *CurContext, |
| 452 | const DeclContext *TargetContext) { |
| 453 | SmallVector<const DeclContext *, 4> TargetParents; |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 454 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 455 | for (const DeclContext *CommonAncestor = TargetContext; |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 456 | CommonAncestor && !CommonAncestor->Encloses(CurContext); |
| 457 | CommonAncestor = CommonAncestor->getLookupParent()) { |
| 458 | if (CommonAncestor->isTransparentContext() || |
| 459 | CommonAncestor->isFunctionOrMethod()) |
| 460 | continue; |
| 461 | |
| 462 | TargetParents.push_back(CommonAncestor); |
| 463 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 464 | |
| 465 | NestedNameSpecifier *Result = nullptr; |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 466 | while (!TargetParents.empty()) { |
Robert Wilhelm | 344472e | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 467 | const DeclContext *Parent = TargetParents.pop_back_val(); |
| 468 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 469 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) { |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 470 | if (!Namespace->getIdentifier()) |
| 471 | continue; |
| 472 | |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 473 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 474 | } |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 475 | else if (const TagDecl *TD = dyn_cast<TagDecl>(Parent)) |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 476 | Result = NestedNameSpecifier::Create(Context, Result, |
| 477 | false, |
| 478 | Context.getTypeDeclType(TD).getTypePtr()); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 479 | } |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 480 | return Result; |
| 481 | } |
| 482 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 483 | /// Determine whether \p Id is a name reserved for the implementation (C99 |
| 484 | /// 7.1.3, C++ [lib.global.names]). |
| 485 | static bool isReservedName(const IdentifierInfo *Id) { |
| 486 | if (Id->getLength() < 2) |
| 487 | return false; |
| 488 | const char *Name = Id->getNameStart(); |
| 489 | return Name[0] == '_' && |
| 490 | (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z')); |
| 491 | } |
| 492 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 493 | bool ResultBuilder::isInterestingDecl(const NamedDecl *ND, |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 494 | bool &AsNestedNameSpecifier) const { |
| 495 | AsNestedNameSpecifier = false; |
| 496 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 497 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 498 | |
| 499 | // Skip unnamed entities. |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 500 | if (!ND->getDeclName()) |
| 501 | return false; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 502 | |
| 503 | // Friend declarations and declarations introduced due to friends are never |
| 504 | // added as results. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 505 | if (ND->getFriendObjectKind() == Decl::FOK_Undeclared) |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 506 | return false; |
| 507 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 508 | // Class template (partial) specializations are never added as results. |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 509 | if (isa<ClassTemplateSpecializationDecl>(ND) || |
| 510 | isa<ClassTemplatePartialSpecializationDecl>(ND)) |
| 511 | return false; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 512 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 513 | // Using declarations themselves are never added as results. |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 514 | if (isa<UsingDecl>(ND)) |
| 515 | return false; |
| 516 | |
| 517 | // Some declarations have reserved names that we don't want to ever show. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 518 | // Filter out names reserved for the implementation if they come from a |
| 519 | // system header. |
| 520 | // TODO: Add a predicate for this. |
| 521 | if (const IdentifierInfo *Id = ND->getIdentifier()) |
| 522 | if (isReservedName(Id) && |
| 523 | (ND->getLocation().isInvalid() || |
| 524 | SemaRef.SourceMgr.isInSystemHeader( |
| 525 | SemaRef.SourceMgr.getSpellingLoc(ND->getLocation())))) |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 526 | return false; |
Douglas Gregor | 9b0ba87 | 2010-11-09 03:59:40 +0000 | [diff] [blame] | 527 | |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 528 | if (Filter == &ResultBuilder::IsNestedNameSpecifier || |
| 529 | ((isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) && |
| 530 | Filter != &ResultBuilder::IsNamespace && |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 531 | Filter != &ResultBuilder::IsNamespaceOrAlias && |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 532 | Filter != nullptr)) |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 533 | AsNestedNameSpecifier = true; |
| 534 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 535 | // Filter out any unwanted results. |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 536 | if (Filter && !(this->*Filter)(ND)) { |
| 537 | // Check whether it is interesting as a nested-name-specifier. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 538 | if (AllowNestedNameSpecifiers && SemaRef.getLangOpts().CPlusPlus && |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 539 | IsNestedNameSpecifier(ND) && |
| 540 | (Filter != &ResultBuilder::IsMember || |
| 541 | (isa<CXXRecordDecl>(ND) && |
| 542 | cast<CXXRecordDecl>(ND)->isInjectedClassName()))) { |
| 543 | AsNestedNameSpecifier = true; |
| 544 | return true; |
| 545 | } |
| 546 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 547 | return false; |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 548 | } |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 549 | // ... then it must be interesting! |
| 550 | return true; |
| 551 | } |
| 552 | |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 553 | bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext, |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 554 | const NamedDecl *Hiding) { |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 555 | // In C, there is no way to refer to a hidden name. |
| 556 | // FIXME: This isn't true; we can find a tag name hidden by an ordinary |
| 557 | // name if we introduce the tag type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 558 | if (!SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 559 | return true; |
| 560 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 561 | const DeclContext *HiddenCtx = |
| 562 | R.Declaration->getDeclContext()->getRedeclContext(); |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 563 | |
| 564 | // There is no way to qualify a name declared in a function or method. |
| 565 | if (HiddenCtx->isFunctionOrMethod()) |
| 566 | return true; |
| 567 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 568 | if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext()) |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 569 | return true; |
| 570 | |
| 571 | // We can refer to the result with the appropriate qualification. Do it. |
| 572 | R.Hidden = true; |
| 573 | R.QualifierIsInformative = false; |
| 574 | |
| 575 | if (!R.Qualifier) |
| 576 | R.Qualifier = getRequiredQualification(SemaRef.Context, |
| 577 | CurContext, |
| 578 | R.Declaration->getDeclContext()); |
| 579 | return false; |
| 580 | } |
| 581 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 582 | /// \brief A simplified classification of types used to determine whether two |
| 583 | /// types are "similar enough" when adjusting priorities. |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 584 | SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 585 | switch (T->getTypeClass()) { |
| 586 | case Type::Builtin: |
| 587 | switch (cast<BuiltinType>(T)->getKind()) { |
| 588 | case BuiltinType::Void: |
| 589 | return STC_Void; |
| 590 | |
| 591 | case BuiltinType::NullPtr: |
| 592 | return STC_Pointer; |
| 593 | |
| 594 | case BuiltinType::Overload: |
| 595 | case BuiltinType::Dependent: |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 596 | return STC_Other; |
| 597 | |
| 598 | case BuiltinType::ObjCId: |
| 599 | case BuiltinType::ObjCClass: |
| 600 | case BuiltinType::ObjCSel: |
| 601 | return STC_ObjectiveC; |
| 602 | |
| 603 | default: |
| 604 | return STC_Arithmetic; |
| 605 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 606 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 607 | case Type::Complex: |
| 608 | return STC_Arithmetic; |
| 609 | |
| 610 | case Type::Pointer: |
| 611 | return STC_Pointer; |
| 612 | |
| 613 | case Type::BlockPointer: |
| 614 | return STC_Block; |
| 615 | |
| 616 | case Type::LValueReference: |
| 617 | case Type::RValueReference: |
| 618 | return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType()); |
| 619 | |
| 620 | case Type::ConstantArray: |
| 621 | case Type::IncompleteArray: |
| 622 | case Type::VariableArray: |
| 623 | case Type::DependentSizedArray: |
| 624 | return STC_Array; |
| 625 | |
| 626 | case Type::DependentSizedExtVector: |
| 627 | case Type::Vector: |
| 628 | case Type::ExtVector: |
| 629 | return STC_Arithmetic; |
| 630 | |
| 631 | case Type::FunctionProto: |
| 632 | case Type::FunctionNoProto: |
| 633 | return STC_Function; |
| 634 | |
| 635 | case Type::Record: |
| 636 | return STC_Record; |
| 637 | |
| 638 | case Type::Enum: |
| 639 | return STC_Arithmetic; |
| 640 | |
| 641 | case Type::ObjCObject: |
| 642 | case Type::ObjCInterface: |
| 643 | case Type::ObjCObjectPointer: |
| 644 | return STC_ObjectiveC; |
| 645 | |
| 646 | default: |
| 647 | return STC_Other; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /// \brief Get the type that a given expression will have if this declaration |
| 652 | /// is used as an expression in its "typical" code-completion form. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 653 | QualType clang::getDeclUsageType(ASTContext &C, const NamedDecl *ND) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 654 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 655 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 656 | if (const TypeDecl *Type = dyn_cast<TypeDecl>(ND)) |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 657 | return C.getTypeDeclType(Type); |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 658 | if (const ObjCInterfaceDecl *Iface = dyn_cast<ObjCInterfaceDecl>(ND)) |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 659 | return C.getObjCInterfaceType(Iface); |
| 660 | |
| 661 | QualType T; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 662 | if (const FunctionDecl *Function = ND->getAsFunction()) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 663 | T = Function->getCallResultType(); |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 664 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 665 | T = Method->getSendResultType(); |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 666 | else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 667 | T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext())); |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 668 | else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 669 | T = Property->getType(); |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 670 | else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND)) |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 671 | T = Value->getType(); |
| 672 | else |
| 673 | return QualType(); |
Douglas Gregor | 3e64d56 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 674 | |
| 675 | // Dig through references, function pointers, and block pointers to |
| 676 | // get down to the likely type of an expression when the entity is |
| 677 | // used. |
| 678 | do { |
| 679 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) { |
| 680 | T = Ref->getPointeeType(); |
| 681 | continue; |
| 682 | } |
| 683 | |
| 684 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 685 | if (Pointer->getPointeeType()->isFunctionType()) { |
| 686 | T = Pointer->getPointeeType(); |
| 687 | continue; |
| 688 | } |
| 689 | |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | if (const BlockPointerType *Block = T->getAs<BlockPointerType>()) { |
| 694 | T = Block->getPointeeType(); |
| 695 | continue; |
| 696 | } |
| 697 | |
| 698 | if (const FunctionType *Function = T->getAs<FunctionType>()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 699 | T = Function->getReturnType(); |
Douglas Gregor | 3e64d56 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 700 | continue; |
| 701 | } |
| 702 | |
| 703 | break; |
| 704 | } while (true); |
| 705 | |
| 706 | return T; |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Douglas Gregor | d1f09b4 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 709 | unsigned ResultBuilder::getBasePriority(const NamedDecl *ND) { |
| 710 | if (!ND) |
| 711 | return CCP_Unlikely; |
| 712 | |
| 713 | // Context-based decisions. |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 714 | const DeclContext *LexicalDC = ND->getLexicalDeclContext(); |
| 715 | if (LexicalDC->isFunctionOrMethod()) { |
Douglas Gregor | d1f09b4 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 716 | // _cmd is relatively rare |
| 717 | if (const ImplicitParamDecl *ImplicitParam = |
| 718 | dyn_cast<ImplicitParamDecl>(ND)) |
| 719 | if (ImplicitParam->getIdentifier() && |
| 720 | ImplicitParam->getIdentifier()->isStr("_cmd")) |
| 721 | return CCP_ObjC_cmd; |
| 722 | |
| 723 | return CCP_LocalDeclaration; |
| 724 | } |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 725 | |
| 726 | const DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
Douglas Gregor | d1f09b4 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 727 | if (DC->isRecord() || isa<ObjCContainerDecl>(DC)) |
| 728 | return CCP_MemberDeclaration; |
| 729 | |
| 730 | // Content-based decisions. |
| 731 | if (isa<EnumConstantDecl>(ND)) |
| 732 | return CCP_Constant; |
| 733 | |
Douglas Gregor | 626799b | 2013-01-31 05:03:46 +0000 | [diff] [blame] | 734 | // Use CCP_Type for type declarations unless we're in a statement, Objective-C |
| 735 | // message receiver, or parenthesized expression context. There, it's as |
| 736 | // likely that the user will want to write a type as other declarations. |
| 737 | if ((isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) && |
| 738 | !(CompletionContext.getKind() == CodeCompletionContext::CCC_Statement || |
| 739 | CompletionContext.getKind() |
| 740 | == CodeCompletionContext::CCC_ObjCMessageReceiver || |
| 741 | CompletionContext.getKind() |
| 742 | == CodeCompletionContext::CCC_ParenthesizedExpression)) |
Douglas Gregor | d1f09b4 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 743 | return CCP_Type; |
| 744 | |
| 745 | return CCP_Declaration; |
| 746 | } |
| 747 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 748 | void ResultBuilder::AdjustResultPriorityForDecl(Result &R) { |
| 749 | // If this is an Objective-C method declaration whose selector matches our |
| 750 | // preferred selector, give it a priority boost. |
| 751 | if (!PreferredSelector.isNull()) |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 752 | if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration)) |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 753 | if (PreferredSelector == Method->getSelector()) |
| 754 | R.Priority += CCD_SelectorMatch; |
Douglas Gregor | 08f43cd | 2010-09-20 23:11:55 +0000 | [diff] [blame] | 755 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 756 | // If we have a preferred type, adjust the priority for results with exactly- |
| 757 | // matching or nearly-matching types. |
| 758 | if (!PreferredType.isNull()) { |
| 759 | QualType T = getDeclUsageType(SemaRef.Context, R.Declaration); |
| 760 | if (!T.isNull()) { |
| 761 | CanQualType TC = SemaRef.Context.getCanonicalType(T); |
| 762 | // Check for exactly-matching types (modulo qualifiers). |
| 763 | if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) |
| 764 | R.Priority /= CCF_ExactTypeMatch; |
| 765 | // Check for nearly-matching types, based on classification of each. |
| 766 | else if ((getSimplifiedTypeClass(PreferredType) |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 767 | == getSimplifiedTypeClass(TC)) && |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 768 | !(PreferredType->isEnumeralType() && TC->isEnumeralType())) |
| 769 | R.Priority /= CCF_SimilarTypeMatch; |
| 770 | } |
| 771 | } |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 774 | void ResultBuilder::MaybeAddConstructorResults(Result R) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 775 | if (!SemaRef.getLangOpts().CPlusPlus || !R.Declaration || |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 776 | !CompletionContext.wantConstructorResults()) |
| 777 | return; |
| 778 | |
| 779 | ASTContext &Context = SemaRef.Context; |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 780 | const NamedDecl *D = R.Declaration; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 781 | const CXXRecordDecl *Record = nullptr; |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 782 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 783 | Record = ClassTemplate->getTemplatedDecl(); |
| 784 | else if ((Record = dyn_cast<CXXRecordDecl>(D))) { |
| 785 | // Skip specializations and partial specializations. |
| 786 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 787 | return; |
| 788 | } else { |
| 789 | // There are no constructors here. |
| 790 | return; |
| 791 | } |
| 792 | |
| 793 | Record = Record->getDefinition(); |
| 794 | if (!Record) |
| 795 | return; |
| 796 | |
| 797 | |
| 798 | QualType RecordTy = Context.getTypeDeclType(Record); |
| 799 | DeclarationName ConstructorName |
| 800 | = Context.DeclarationNames.getCXXConstructorName( |
| 801 | Context.getCanonicalType(RecordTy)); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 802 | DeclContext::lookup_result Ctors = Record->lookup(ConstructorName); |
| 803 | for (DeclContext::lookup_iterator I = Ctors.begin(), |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 804 | E = Ctors.end(); |
| 805 | I != E; ++I) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 806 | R.Declaration = *I; |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 807 | R.CursorKind = getCursorKindForDecl(R.Declaration); |
| 808 | Results.push_back(R); |
| 809 | } |
| 810 | } |
| 811 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 812 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { |
| 813 | assert(!ShadowMaps.empty() && "Must enter into a results scope"); |
| 814 | |
| 815 | if (R.Kind != Result::RK_Declaration) { |
| 816 | // For non-declaration results, just add the result. |
| 817 | Results.push_back(R); |
| 818 | return; |
| 819 | } |
| 820 | |
| 821 | // Look through using declarations. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 822 | if (const UsingShadowDecl *Using = |
| 823 | dyn_cast<UsingShadowDecl>(R.Declaration)) { |
Douglas Gregor | d1f09b4 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 824 | MaybeAddResult(Result(Using->getTargetDecl(), |
| 825 | getBasePriority(Using->getTargetDecl()), |
| 826 | R.Qualifier), |
| 827 | CurContext); |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 828 | return; |
| 829 | } |
| 830 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 831 | const Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 832 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 833 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 834 | bool AsNestedNameSpecifier = false; |
| 835 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 836 | return; |
| 837 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 838 | // C++ constructors are never found by name lookup. |
| 839 | if (isa<CXXConstructorDecl>(R.Declaration)) |
| 840 | return; |
| 841 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 842 | ShadowMap &SMap = ShadowMaps.back(); |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 843 | ShadowMapEntry::iterator I, IEnd; |
| 844 | ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName()); |
| 845 | if (NamePos != SMap.end()) { |
| 846 | I = NamePos->second.begin(); |
| 847 | IEnd = NamePos->second.end(); |
| 848 | } |
| 849 | |
| 850 | for (; I != IEnd; ++I) { |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 851 | const NamedDecl *ND = I->first; |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 852 | unsigned Index = I->second; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 853 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 854 | // This is a redeclaration. Always pick the newer declaration. |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 855 | Results[Index].Declaration = R.Declaration; |
| 856 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 857 | // We're done. |
| 858 | return; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | // This is a new declaration in this scope. However, check whether this |
| 863 | // declaration name is hidden by a similarly-named declaration in an outer |
| 864 | // scope. |
| 865 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 866 | --SMEnd; |
| 867 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 868 | ShadowMapEntry::iterator I, IEnd; |
| 869 | ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName()); |
| 870 | if (NamePos != SM->end()) { |
| 871 | I = NamePos->second.begin(); |
| 872 | IEnd = NamePos->second.end(); |
| 873 | } |
| 874 | for (; I != IEnd; ++I) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 875 | // A tag declaration does not hide a non-tag declaration. |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 876 | if (I->first->hasTagIdentifierNamespace() && |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 877 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 878 | Decl::IDNS_LocalExtern | Decl::IDNS_ObjCProtocol))) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 879 | continue; |
| 880 | |
| 881 | // Protocols are in distinct namespaces from everything else. |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 882 | if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 883 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 884 | I->first->getIdentifierNamespace() != IDNS) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 885 | continue; |
| 886 | |
| 887 | // The newly-added result is hidden by an entry in the shadow map. |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 888 | if (CheckHiddenResult(R, CurContext, I->first)) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 889 | return; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 890 | |
| 891 | break; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | // Make sure that any given declaration only shows up in the result set once. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 896 | if (!AllDeclsFound.insert(CanonDecl).second) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 897 | return; |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 898 | |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 899 | // If the filter is for nested-name-specifiers, then this result starts a |
| 900 | // nested-name-specifier. |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 901 | if (AsNestedNameSpecifier) { |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 902 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 903 | R.Priority = CCP_NestedNameSpecifier; |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 904 | } else |
| 905 | AdjustResultPriorityForDecl(R); |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 906 | |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 907 | // If this result is supposed to have an informative qualifier, add one. |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 908 | if (R.QualifierIsInformative && !R.Qualifier && |
| 909 | !R.StartsNestedNameSpecifier) { |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 910 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 911 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 912 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 913 | Namespace); |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 914 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 915 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 916 | false, SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 917 | else |
| 918 | R.QualifierIsInformative = false; |
| 919 | } |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 920 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 921 | // Insert this result into the set of results and into the current shadow |
| 922 | // map. |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 923 | SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 924 | Results.push_back(R); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 925 | |
| 926 | if (!AsNestedNameSpecifier) |
| 927 | MaybeAddConstructorResults(R); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 930 | void ResultBuilder::AddResult(Result R, DeclContext *CurContext, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 931 | NamedDecl *Hiding, bool InBaseClass = false) { |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 932 | if (R.Kind != Result::RK_Declaration) { |
| 933 | // For non-declaration results, just add the result. |
| 934 | Results.push_back(R); |
| 935 | return; |
| 936 | } |
| 937 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 938 | // Look through using declarations. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 939 | if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
Douglas Gregor | d1f09b4 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 940 | AddResult(Result(Using->getTargetDecl(), |
| 941 | getBasePriority(Using->getTargetDecl()), |
| 942 | R.Qualifier), |
| 943 | CurContext, Hiding); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 944 | return; |
| 945 | } |
| 946 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 947 | bool AsNestedNameSpecifier = false; |
| 948 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 949 | return; |
| 950 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 951 | // C++ constructors are never found by name lookup. |
| 952 | if (isa<CXXConstructorDecl>(R.Declaration)) |
| 953 | return; |
| 954 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 955 | if (Hiding && CheckHiddenResult(R, CurContext, Hiding)) |
| 956 | return; |
Nick Lewycky | 173a37a | 2012-04-03 21:44:08 +0000 | [diff] [blame] | 957 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 958 | // Make sure that any given declaration only shows up in the result set once. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 959 | if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()).second) |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 960 | return; |
| 961 | |
| 962 | // If the filter is for nested-name-specifiers, then this result starts a |
| 963 | // nested-name-specifier. |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 964 | if (AsNestedNameSpecifier) { |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 965 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 966 | R.Priority = CCP_NestedNameSpecifier; |
| 967 | } |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 968 | else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass && |
| 969 | isa<CXXRecordDecl>(R.Declaration->getDeclContext() |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 970 | ->getRedeclContext())) |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 971 | R.QualifierIsInformative = true; |
| 972 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 973 | // If this result is supposed to have an informative qualifier, add one. |
| 974 | if (R.QualifierIsInformative && !R.Qualifier && |
| 975 | !R.StartsNestedNameSpecifier) { |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 976 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 977 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 978 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 979 | Namespace); |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 980 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 981 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, false, |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 982 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 983 | else |
| 984 | R.QualifierIsInformative = false; |
| 985 | } |
| 986 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 987 | // Adjust the priority if this result comes from a base class. |
| 988 | if (InBaseClass) |
| 989 | R.Priority += CCD_InBaseClass; |
| 990 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 991 | AdjustResultPriorityForDecl(R); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 992 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 993 | if (HasObjectTypeQualifiers) |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 994 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(R.Declaration)) |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 995 | if (Method->isInstance()) { |
| 996 | Qualifiers MethodQuals |
| 997 | = Qualifiers::fromCVRMask(Method->getTypeQualifiers()); |
| 998 | if (ObjectTypeQualifiers == MethodQuals) |
| 999 | R.Priority += CCD_ObjectQualifierMatch; |
| 1000 | else if (ObjectTypeQualifiers - MethodQuals) { |
| 1001 | // The method cannot be invoked, because doing so would drop |
| 1002 | // qualifiers. |
| 1003 | return; |
| 1004 | } |
| 1005 | } |
| 1006 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1007 | // Insert this result into the set of results. |
| 1008 | Results.push_back(R); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1009 | |
| 1010 | if (!AsNestedNameSpecifier) |
| 1011 | MaybeAddConstructorResults(R); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1014 | void ResultBuilder::AddResult(Result R) { |
| 1015 | assert(R.Kind != Result::RK_Declaration && |
| 1016 | "Declaration results need more context"); |
| 1017 | Results.push_back(R); |
| 1018 | } |
| 1019 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1020 | /// \brief Enter into a new scope. |
| 1021 | void ResultBuilder::EnterNewScope() { |
| 1022 | ShadowMaps.push_back(ShadowMap()); |
| 1023 | } |
| 1024 | |
| 1025 | /// \brief Exit from the current scope. |
| 1026 | void ResultBuilder::ExitScope() { |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1027 | for (ShadowMap::iterator E = ShadowMaps.back().begin(), |
| 1028 | EEnd = ShadowMaps.back().end(); |
| 1029 | E != EEnd; |
| 1030 | ++E) |
| 1031 | E->second.Destroy(); |
| 1032 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1033 | ShadowMaps.pop_back(); |
| 1034 | } |
| 1035 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1036 | /// \brief Determines whether this given declaration will be found by |
| 1037 | /// ordinary name lookup. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1038 | bool ResultBuilder::IsOrdinaryName(const NamedDecl *ND) const { |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1039 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 1040 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1041 | // If name lookup finds a local extern declaration, then we are in a |
| 1042 | // context where it behaves like an ordinary name. |
| 1043 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1044 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 9b30b26 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1045 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1046 | else if (SemaRef.getLangOpts().ObjC1) { |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1047 | if (isa<ObjCIvarDecl>(ND)) |
| 1048 | return true; |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1051 | return ND->getIdentifierNamespace() & IDNS; |
| 1052 | } |
| 1053 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1054 | /// \brief Determines whether this given declaration will be found by |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1055 | /// ordinary name lookup but is not a type name. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1056 | bool ResultBuilder::IsOrdinaryNonTypeName(const NamedDecl *ND) const { |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1057 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 1058 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) |
| 1059 | return false; |
| 1060 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1061 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1062 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 9b30b26 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1063 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1064 | else if (SemaRef.getLangOpts().ObjC1) { |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1065 | if (isa<ObjCIvarDecl>(ND)) |
| 1066 | return true; |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1069 | return ND->getIdentifierNamespace() & IDNS; |
| 1070 | } |
| 1071 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1072 | bool ResultBuilder::IsIntegralConstantValue(const NamedDecl *ND) const { |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1073 | if (!IsOrdinaryNonTypeName(ND)) |
| 1074 | return 0; |
| 1075 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1076 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl())) |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1077 | if (VD->getType()->isIntegralOrEnumerationType()) |
| 1078 | return true; |
| 1079 | |
| 1080 | return false; |
| 1081 | } |
| 1082 | |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1083 | /// \brief Determines whether this given declaration will be found by |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1084 | /// ordinary name lookup. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1085 | bool ResultBuilder::IsOrdinaryNonValueName(const NamedDecl *ND) const { |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1086 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 1087 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1088 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1089 | if (SemaRef.getLangOpts().CPlusPlus) |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1090 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1091 | |
| 1092 | return (ND->getIdentifierNamespace() & IDNS) && |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1093 | !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) && |
| 1094 | !isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1097 | /// \brief Determines whether the given declaration is suitable as the |
| 1098 | /// start of a C++ nested-name-specifier, e.g., a class or namespace. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1099 | bool ResultBuilder::IsNestedNameSpecifier(const NamedDecl *ND) const { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1100 | // Allow us to find class templates, too. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1101 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1102 | ND = ClassTemplate->getTemplatedDecl(); |
| 1103 | |
| 1104 | return SemaRef.isAcceptableNestedNameSpecifier(ND); |
| 1105 | } |
| 1106 | |
| 1107 | /// \brief Determines whether the given declaration is an enumeration. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1108 | bool ResultBuilder::IsEnum(const NamedDecl *ND) const { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1109 | return isa<EnumDecl>(ND); |
| 1110 | } |
| 1111 | |
| 1112 | /// \brief Determines whether the given declaration is a class or struct. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1113 | bool ResultBuilder::IsClassOrStruct(const NamedDecl *ND) const { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1114 | // Allow us to find class templates, too. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1115 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1116 | ND = ClassTemplate->getTemplatedDecl(); |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1117 | |
| 1118 | // For purposes of this check, interfaces match too. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1119 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1120 | return RD->getTagKind() == TTK_Class || |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1121 | RD->getTagKind() == TTK_Struct || |
| 1122 | RD->getTagKind() == TTK_Interface; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1123 | |
| 1124 | return false; |
| 1125 | } |
| 1126 | |
| 1127 | /// \brief Determines whether the given declaration is a union. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1128 | bool ResultBuilder::IsUnion(const NamedDecl *ND) const { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1129 | // Allow us to find class templates, too. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1130 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1131 | ND = ClassTemplate->getTemplatedDecl(); |
| 1132 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1133 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1134 | return RD->getTagKind() == TTK_Union; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1135 | |
| 1136 | return false; |
| 1137 | } |
| 1138 | |
| 1139 | /// \brief Determines whether the given declaration is a namespace. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1140 | bool ResultBuilder::IsNamespace(const NamedDecl *ND) const { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1141 | return isa<NamespaceDecl>(ND); |
| 1142 | } |
| 1143 | |
| 1144 | /// \brief Determines whether the given declaration is a namespace or |
| 1145 | /// namespace alias. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1146 | bool ResultBuilder::IsNamespaceOrAlias(const NamedDecl *ND) const { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1147 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
| 1148 | } |
| 1149 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1150 | /// \brief Determines whether the given declaration is a type. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1151 | bool ResultBuilder::IsType(const NamedDecl *ND) const { |
| 1152 | if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND)) |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1153 | ND = Using->getTargetDecl(); |
| 1154 | |
| 1155 | return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1158 | /// \brief Determines which members of a class should be visible via |
| 1159 | /// "." or "->". Only value declarations, nested name specifiers, and |
| 1160 | /// using declarations thereof should show up. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1161 | bool ResultBuilder::IsMember(const NamedDecl *ND) const { |
| 1162 | if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND)) |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1163 | ND = Using->getTargetDecl(); |
| 1164 | |
Douglas Gregor | ce82196 | 2009-12-11 18:14:22 +0000 | [diff] [blame] | 1165 | return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) || |
| 1166 | isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1169 | static bool isObjCReceiverType(ASTContext &C, QualType T) { |
| 1170 | T = C.getCanonicalType(T); |
| 1171 | switch (T->getTypeClass()) { |
| 1172 | case Type::ObjCObject: |
| 1173 | case Type::ObjCInterface: |
| 1174 | case Type::ObjCObjectPointer: |
| 1175 | return true; |
| 1176 | |
| 1177 | case Type::Builtin: |
| 1178 | switch (cast<BuiltinType>(T)->getKind()) { |
| 1179 | case BuiltinType::ObjCId: |
| 1180 | case BuiltinType::ObjCClass: |
| 1181 | case BuiltinType::ObjCSel: |
| 1182 | return true; |
| 1183 | |
| 1184 | default: |
| 1185 | break; |
| 1186 | } |
| 1187 | return false; |
| 1188 | |
| 1189 | default: |
| 1190 | break; |
| 1191 | } |
| 1192 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1193 | if (!C.getLangOpts().CPlusPlus) |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1194 | return false; |
| 1195 | |
| 1196 | // FIXME: We could perform more analysis here to determine whether a |
| 1197 | // particular class type has any conversions to Objective-C types. For now, |
| 1198 | // just accept all class types. |
| 1199 | return T->isDependentType() || T->isRecordType(); |
| 1200 | } |
| 1201 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1202 | bool ResultBuilder::IsObjCMessageReceiver(const NamedDecl *ND) const { |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1203 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1204 | if (T.isNull()) |
| 1205 | return false; |
| 1206 | |
| 1207 | T = SemaRef.Context.getBaseElementType(T); |
| 1208 | return isObjCReceiverType(SemaRef.Context, T); |
| 1209 | } |
| 1210 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1211 | bool ResultBuilder::IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const { |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1212 | if (IsObjCMessageReceiver(ND)) |
| 1213 | return true; |
| 1214 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1215 | const VarDecl *Var = dyn_cast<VarDecl>(ND); |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1216 | if (!Var) |
| 1217 | return false; |
| 1218 | |
| 1219 | return Var->hasLocalStorage() && !Var->hasAttr<BlocksAttr>(); |
| 1220 | } |
| 1221 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1222 | bool ResultBuilder::IsObjCCollection(const NamedDecl *ND) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1223 | if ((SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryName(ND)) || |
| 1224 | (!SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryNonTypeName(ND))) |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1225 | return false; |
| 1226 | |
| 1227 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1228 | if (T.isNull()) |
| 1229 | return false; |
| 1230 | |
| 1231 | T = SemaRef.Context.getBaseElementType(T); |
| 1232 | return T->isObjCObjectType() || T->isObjCObjectPointerType() || |
| 1233 | T->isObjCIdType() || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1234 | (SemaRef.getLangOpts().CPlusPlus && T->isRecordType()); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1235 | } |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1236 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1237 | bool ResultBuilder::IsImpossibleToSatisfy(const NamedDecl *ND) const { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1238 | return false; |
| 1239 | } |
| 1240 | |
James Dennett | de23c7e | 2012-06-17 05:33:25 +0000 | [diff] [blame] | 1241 | /// \brief Determines whether the given declaration is an Objective-C |
Douglas Gregor | 80f4f4c | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1242 | /// instance variable. |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1243 | bool ResultBuilder::IsObjCIvar(const NamedDecl *ND) const { |
Douglas Gregor | 80f4f4c | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1244 | return isa<ObjCIvarDecl>(ND); |
| 1245 | } |
| 1246 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1247 | namespace { |
| 1248 | /// \brief Visible declaration consumer that adds a code-completion result |
| 1249 | /// for each visible declaration. |
| 1250 | class CodeCompletionDeclConsumer : public VisibleDeclConsumer { |
| 1251 | ResultBuilder &Results; |
| 1252 | DeclContext *CurContext; |
| 1253 | |
| 1254 | public: |
| 1255 | CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext) |
| 1256 | : Results(Results), CurContext(CurContext) { } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1257 | |
| 1258 | void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx, |
| 1259 | bool InBaseClass) override { |
Erik Verbruggen | d120596 | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1260 | bool Accessible = true; |
Douglas Gregor | 17015ef | 2011-11-03 16:51:37 +0000 | [diff] [blame] | 1261 | if (Ctx) |
| 1262 | Accessible = Results.getSema().IsSimplyAccessible(ND, Ctx); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1263 | |
| 1264 | ResultBuilder::Result Result(ND, Results.getBasePriority(ND), nullptr, |
| 1265 | false, Accessible); |
Erik Verbruggen | d120596 | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1266 | Results.AddResult(Result, CurContext, Hiding, InBaseClass); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1267 | } |
| 1268 | }; |
| 1269 | } |
| 1270 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1271 | /// \brief Add type specifiers for the current language as keyword results. |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1272 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1273 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1274 | typedef CodeCompletionResult Result; |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1275 | Results.AddResult(Result("short", CCP_Type)); |
| 1276 | Results.AddResult(Result("long", CCP_Type)); |
| 1277 | Results.AddResult(Result("signed", CCP_Type)); |
| 1278 | Results.AddResult(Result("unsigned", CCP_Type)); |
| 1279 | Results.AddResult(Result("void", CCP_Type)); |
| 1280 | Results.AddResult(Result("char", CCP_Type)); |
| 1281 | Results.AddResult(Result("int", CCP_Type)); |
| 1282 | Results.AddResult(Result("float", CCP_Type)); |
| 1283 | Results.AddResult(Result("double", CCP_Type)); |
| 1284 | Results.AddResult(Result("enum", CCP_Type)); |
| 1285 | Results.AddResult(Result("struct", CCP_Type)); |
| 1286 | Results.AddResult(Result("union", CCP_Type)); |
| 1287 | Results.AddResult(Result("const", CCP_Type)); |
| 1288 | Results.AddResult(Result("volatile", CCP_Type)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1289 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1290 | if (LangOpts.C99) { |
| 1291 | // C99-specific |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1292 | Results.AddResult(Result("_Complex", CCP_Type)); |
| 1293 | Results.AddResult(Result("_Imaginary", CCP_Type)); |
| 1294 | Results.AddResult(Result("_Bool", CCP_Type)); |
| 1295 | Results.AddResult(Result("restrict", CCP_Type)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1298 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 1299 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1300 | if (LangOpts.CPlusPlus) { |
| 1301 | // C++-specific |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 1302 | Results.AddResult(Result("bool", CCP_Type + |
| 1303 | (LangOpts.ObjC1? CCD_bool_in_ObjC : 0))); |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1304 | Results.AddResult(Result("class", CCP_Type)); |
| 1305 | Results.AddResult(Result("wchar_t", CCP_Type)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1306 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1307 | // typename qualified-id |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1308 | Builder.AddTypedTextChunk("typename"); |
| 1309 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1310 | Builder.AddPlaceholderChunk("qualifier"); |
| 1311 | Builder.AddTextChunk("::"); |
| 1312 | Builder.AddPlaceholderChunk("name"); |
| 1313 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1314 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1315 | if (LangOpts.CPlusPlus11) { |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1316 | Results.AddResult(Result("auto", CCP_Type)); |
| 1317 | Results.AddResult(Result("char16_t", CCP_Type)); |
| 1318 | Results.AddResult(Result("char32_t", CCP_Type)); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1319 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1320 | Builder.AddTypedTextChunk("decltype"); |
| 1321 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1322 | Builder.AddPlaceholderChunk("expression"); |
| 1323 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1324 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | // GNU extensions |
| 1329 | if (LangOpts.GNUMode) { |
| 1330 | // FIXME: Enable when we actually support decimal floating point. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1331 | // Results.AddResult(Result("_Decimal32")); |
| 1332 | // Results.AddResult(Result("_Decimal64")); |
| 1333 | // Results.AddResult(Result("_Decimal128")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1334 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1335 | Builder.AddTypedTextChunk("typeof"); |
| 1336 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1337 | Builder.AddPlaceholderChunk("expression"); |
| 1338 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1339 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1340 | Builder.AddTypedTextChunk("typeof"); |
| 1341 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1342 | Builder.AddPlaceholderChunk("type"); |
| 1343 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1344 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1345 | } |
| 1346 | } |
| 1347 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1348 | static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1349 | const LangOptions &LangOpts, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1350 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1351 | typedef CodeCompletionResult Result; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1352 | // Note: we don't suggest either "auto" or "register", because both |
| 1353 | // are pointless as storage specifiers. Elsewhere, we suggest "auto" |
| 1354 | // in C++0x as a type specifier. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1355 | Results.AddResult(Result("extern")); |
| 1356 | Results.AddResult(Result("static")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1359 | static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1360 | const LangOptions &LangOpts, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1361 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1362 | typedef CodeCompletionResult Result; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1363 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1364 | case Sema::PCC_Class: |
| 1365 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1366 | if (LangOpts.CPlusPlus) { |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1367 | Results.AddResult(Result("explicit")); |
| 1368 | Results.AddResult(Result("friend")); |
| 1369 | Results.AddResult(Result("mutable")); |
| 1370 | Results.AddResult(Result("virtual")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1371 | } |
| 1372 | // Fall through |
| 1373 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1374 | case Sema::PCC_ObjCInterface: |
| 1375 | case Sema::PCC_ObjCImplementation: |
| 1376 | case Sema::PCC_Namespace: |
| 1377 | case Sema::PCC_Template: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1378 | if (LangOpts.CPlusPlus || LangOpts.C99) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1379 | Results.AddResult(Result("inline")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1380 | break; |
| 1381 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1382 | case Sema::PCC_ObjCInstanceVariableList: |
| 1383 | case Sema::PCC_Expression: |
| 1384 | case Sema::PCC_Statement: |
| 1385 | case Sema::PCC_ForInit: |
| 1386 | case Sema::PCC_Condition: |
| 1387 | case Sema::PCC_RecoveryInFunction: |
| 1388 | case Sema::PCC_Type: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1389 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1390 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1391 | break; |
| 1392 | } |
| 1393 | } |
| 1394 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1395 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); |
| 1396 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); |
| 1397 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1398 | ResultBuilder &Results, |
| 1399 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1400 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1401 | ResultBuilder &Results, |
| 1402 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1403 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1404 | ResultBuilder &Results, |
| 1405 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1406 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1407 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1408 | static void AddTypedefResult(ResultBuilder &Results) { |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1409 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 1410 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1411 | Builder.AddTypedTextChunk("typedef"); |
| 1412 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1413 | Builder.AddPlaceholderChunk("type"); |
| 1414 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1415 | Builder.AddPlaceholderChunk("name"); |
| 1416 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1419 | static bool WantTypesInContext(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1420 | const LangOptions &LangOpts) { |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1421 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1422 | case Sema::PCC_Namespace: |
| 1423 | case Sema::PCC_Class: |
| 1424 | case Sema::PCC_ObjCInstanceVariableList: |
| 1425 | case Sema::PCC_Template: |
| 1426 | case Sema::PCC_MemberTemplate: |
| 1427 | case Sema::PCC_Statement: |
| 1428 | case Sema::PCC_RecoveryInFunction: |
| 1429 | case Sema::PCC_Type: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1430 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1431 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1432 | return true; |
| 1433 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1434 | case Sema::PCC_Expression: |
| 1435 | case Sema::PCC_Condition: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1436 | return LangOpts.CPlusPlus; |
| 1437 | |
| 1438 | case Sema::PCC_ObjCInterface: |
| 1439 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1440 | return false; |
| 1441 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1442 | case Sema::PCC_ForInit: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1443 | return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99; |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1444 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1445 | |
| 1446 | llvm_unreachable("Invalid ParserCompletionContext!"); |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 1449 | static PrintingPolicy getCompletionPrintingPolicy(const ASTContext &Context, |
| 1450 | const Preprocessor &PP) { |
| 1451 | PrintingPolicy Policy = Sema::getPrintingPolicy(Context, PP); |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1452 | Policy.AnonymousTagLocations = false; |
| 1453 | Policy.SuppressStrongLifetime = true; |
Douglas Gregor | 25270b6 | 2011-11-03 00:16:13 +0000 | [diff] [blame] | 1454 | Policy.SuppressUnwrittenScope = true; |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1455 | return Policy; |
| 1456 | } |
| 1457 | |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 1458 | /// \brief Retrieve a printing policy suitable for code completion. |
| 1459 | static PrintingPolicy getCompletionPrintingPolicy(Sema &S) { |
| 1460 | return getCompletionPrintingPolicy(S.Context, S.PP); |
| 1461 | } |
| 1462 | |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1463 | /// \brief Retrieve the string representation of the given type as a string |
| 1464 | /// that has the appropriate lifetime for code completion. |
| 1465 | /// |
| 1466 | /// This routine provides a fast path where we provide constant strings for |
| 1467 | /// common type names. |
| 1468 | static const char *GetCompletionTypeString(QualType T, |
| 1469 | ASTContext &Context, |
| 1470 | const PrintingPolicy &Policy, |
| 1471 | CodeCompletionAllocator &Allocator) { |
| 1472 | if (!T.getLocalQualifiers()) { |
| 1473 | // Built-in type names are constant strings. |
| 1474 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) |
Argyrios Kyrtzidis | 27a0097 | 2012-05-05 04:20:28 +0000 | [diff] [blame] | 1475 | return BT->getNameAsCString(Policy); |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1476 | |
| 1477 | // Anonymous tag types are constant strings. |
| 1478 | if (const TagType *TagT = dyn_cast<TagType>(T)) |
| 1479 | if (TagDecl *Tag = TagT->getDecl()) |
John McCall | 83972f1 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 1480 | if (!Tag->hasNameForLinkage()) { |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1481 | switch (Tag->getTagKind()) { |
| 1482 | case TTK_Struct: return "struct <anonymous>"; |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1483 | case TTK_Interface: return "__interface <anonymous>"; |
| 1484 | case TTK_Class: return "class <anonymous>"; |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1485 | case TTK_Union: return "union <anonymous>"; |
| 1486 | case TTK_Enum: return "enum <anonymous>"; |
| 1487 | } |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | // Slow path: format the type as a string. |
| 1492 | std::string Result; |
| 1493 | T.getAsStringInternal(Result, Policy); |
| 1494 | return Allocator.CopyString(Result); |
| 1495 | } |
| 1496 | |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1497 | /// \brief Add a completion for "this", if we're in a member function. |
| 1498 | static void addThisCompletion(Sema &S, ResultBuilder &Results) { |
| 1499 | QualType ThisTy = S.getCurrentThisType(); |
| 1500 | if (ThisTy.isNull()) |
| 1501 | return; |
| 1502 | |
| 1503 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1504 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1505 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
| 1506 | Builder.AddResultTypeChunk(GetCompletionTypeString(ThisTy, |
| 1507 | S.Context, |
| 1508 | Policy, |
| 1509 | Allocator)); |
| 1510 | Builder.AddTypedTextChunk("this"); |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1511 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1514 | /// \brief Add language constructs that show up for "ordinary" names. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1515 | static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1516 | Scope *S, |
| 1517 | Sema &SemaRef, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1518 | ResultBuilder &Results) { |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1519 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1520 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1521 | PrintingPolicy Policy = getCompletionPrintingPolicy(SemaRef); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1522 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1523 | typedef CodeCompletionResult Result; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1524 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1525 | case Sema::PCC_Namespace: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1526 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1527 | if (Results.includeCodePatterns()) { |
| 1528 | // namespace <identifier> { declarations } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1529 | Builder.AddTypedTextChunk("namespace"); |
| 1530 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1531 | Builder.AddPlaceholderChunk("identifier"); |
| 1532 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1533 | Builder.AddPlaceholderChunk("declarations"); |
| 1534 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1535 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1536 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1539 | // namespace identifier = identifier ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1540 | Builder.AddTypedTextChunk("namespace"); |
| 1541 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1542 | Builder.AddPlaceholderChunk("name"); |
| 1543 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
| 1544 | Builder.AddPlaceholderChunk("namespace"); |
| 1545 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1546 | |
| 1547 | // Using directives |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1548 | Builder.AddTypedTextChunk("using"); |
| 1549 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1550 | Builder.AddTextChunk("namespace"); |
| 1551 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1552 | Builder.AddPlaceholderChunk("identifier"); |
| 1553 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1554 | |
| 1555 | // asm(string-literal) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1556 | Builder.AddTypedTextChunk("asm"); |
| 1557 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1558 | Builder.AddPlaceholderChunk("string-literal"); |
| 1559 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1560 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1561 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1562 | if (Results.includeCodePatterns()) { |
| 1563 | // Explicit template instantiation |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1564 | Builder.AddTypedTextChunk("template"); |
| 1565 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1566 | Builder.AddPlaceholderChunk("declaration"); |
| 1567 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1568 | } |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1569 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1570 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1571 | if (SemaRef.getLangOpts().ObjC1) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1572 | AddObjCTopLevelResults(Results, true); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1573 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1574 | AddTypedefResult(Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1575 | // Fall through |
| 1576 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1577 | case Sema::PCC_Class: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1578 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1579 | // Using declaration |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1580 | Builder.AddTypedTextChunk("using"); |
| 1581 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1582 | Builder.AddPlaceholderChunk("qualifier"); |
| 1583 | Builder.AddTextChunk("::"); |
| 1584 | Builder.AddPlaceholderChunk("name"); |
| 1585 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1586 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1587 | // using typename qualifier::name (only in a dependent context) |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1588 | if (SemaRef.CurContext->isDependentContext()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1589 | Builder.AddTypedTextChunk("using"); |
| 1590 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1591 | Builder.AddTextChunk("typename"); |
| 1592 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1593 | Builder.AddPlaceholderChunk("qualifier"); |
| 1594 | Builder.AddTextChunk("::"); |
| 1595 | Builder.AddPlaceholderChunk("name"); |
| 1596 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1599 | if (CCC == Sema::PCC_Class) { |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1600 | AddTypedefResult(Results); |
| 1601 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1602 | // public: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1603 | Builder.AddTypedTextChunk("public"); |
Douglas Gregor | 10ccf12 | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1604 | if (Results.includeCodePatterns()) |
| 1605 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1606 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1607 | |
| 1608 | // protected: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1609 | Builder.AddTypedTextChunk("protected"); |
Douglas Gregor | 10ccf12 | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1610 | if (Results.includeCodePatterns()) |
| 1611 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1612 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1613 | |
| 1614 | // private: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1615 | Builder.AddTypedTextChunk("private"); |
Douglas Gregor | 10ccf12 | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1616 | if (Results.includeCodePatterns()) |
| 1617 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1618 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1619 | } |
| 1620 | } |
| 1621 | // Fall through |
| 1622 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1623 | case Sema::PCC_Template: |
| 1624 | case Sema::PCC_MemberTemplate: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1625 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns()) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1626 | // template < parameters > |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1627 | Builder.AddTypedTextChunk("template"); |
| 1628 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1629 | Builder.AddPlaceholderChunk("parameters"); |
| 1630 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1631 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1634 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1635 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1636 | break; |
| 1637 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1638 | case Sema::PCC_ObjCInterface: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1639 | AddObjCInterfaceResults(SemaRef.getLangOpts(), Results, true); |
| 1640 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1641 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1642 | break; |
| 1643 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1644 | case Sema::PCC_ObjCImplementation: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1645 | AddObjCImplementationResults(SemaRef.getLangOpts(), Results, true); |
| 1646 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1647 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1648 | break; |
| 1649 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1650 | case Sema::PCC_ObjCInstanceVariableList: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1651 | AddObjCVisibilityResults(SemaRef.getLangOpts(), Results, true); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1652 | break; |
| 1653 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1654 | case Sema::PCC_RecoveryInFunction: |
| 1655 | case Sema::PCC_Statement: { |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1656 | AddTypedefResult(Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1657 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1658 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns() && |
| 1659 | SemaRef.getLangOpts().CXXExceptions) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1660 | Builder.AddTypedTextChunk("try"); |
| 1661 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1662 | Builder.AddPlaceholderChunk("statements"); |
| 1663 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1664 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1665 | Builder.AddTextChunk("catch"); |
| 1666 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1667 | Builder.AddPlaceholderChunk("declaration"); |
| 1668 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1669 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1670 | Builder.AddPlaceholderChunk("statements"); |
| 1671 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1672 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1673 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1674 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1675 | if (SemaRef.getLangOpts().ObjC1) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1676 | AddObjCStatementResults(Results, true); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1677 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1678 | if (Results.includeCodePatterns()) { |
| 1679 | // if (condition) { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1680 | Builder.AddTypedTextChunk("if"); |
| 1681 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1682 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1683 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1684 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1685 | Builder.AddPlaceholderChunk("expression"); |
| 1686 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1687 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1688 | Builder.AddPlaceholderChunk("statements"); |
| 1689 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1690 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1691 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1692 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1693 | // switch (condition) { } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1694 | Builder.AddTypedTextChunk("switch"); |
| 1695 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1696 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1697 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1698 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1699 | Builder.AddPlaceholderChunk("expression"); |
| 1700 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1701 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1702 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1703 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1704 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1707 | // Switch-specific statements. |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 1708 | if (!SemaRef.getCurFunction()->SwitchStack.empty()) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1709 | // case expression: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1710 | Builder.AddTypedTextChunk("case"); |
| 1711 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1712 | Builder.AddPlaceholderChunk("expression"); |
| 1713 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1714 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1715 | |
| 1716 | // default: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1717 | Builder.AddTypedTextChunk("default"); |
| 1718 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1719 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1722 | if (Results.includeCodePatterns()) { |
| 1723 | /// while (condition) { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1724 | Builder.AddTypedTextChunk("while"); |
| 1725 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1726 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1727 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1728 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1729 | Builder.AddPlaceholderChunk("expression"); |
| 1730 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1731 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1732 | Builder.AddPlaceholderChunk("statements"); |
| 1733 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1734 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1735 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1736 | |
| 1737 | // do { statements } while ( expression ); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1738 | Builder.AddTypedTextChunk("do"); |
| 1739 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1740 | Builder.AddPlaceholderChunk("statements"); |
| 1741 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1742 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1743 | Builder.AddTextChunk("while"); |
| 1744 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1745 | Builder.AddPlaceholderChunk("expression"); |
| 1746 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1747 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1748 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1749 | // for ( for-init-statement ; condition ; expression ) { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1750 | Builder.AddTypedTextChunk("for"); |
| 1751 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1752 | if (SemaRef.getLangOpts().CPlusPlus || SemaRef.getLangOpts().C99) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1753 | Builder.AddPlaceholderChunk("init-statement"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1754 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1755 | Builder.AddPlaceholderChunk("init-expression"); |
| 1756 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1757 | Builder.AddPlaceholderChunk("condition"); |
| 1758 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1759 | Builder.AddPlaceholderChunk("inc-expression"); |
| 1760 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1761 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1762 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1763 | Builder.AddPlaceholderChunk("statements"); |
| 1764 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1765 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1766 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1767 | } |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1768 | |
| 1769 | if (S->getContinueParent()) { |
| 1770 | // continue ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1771 | Builder.AddTypedTextChunk("continue"); |
| 1772 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | if (S->getBreakParent()) { |
| 1776 | // break ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1777 | Builder.AddTypedTextChunk("break"); |
| 1778 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | // "return expression ;" or "return ;", depending on whether we |
| 1782 | // know the function is void or not. |
| 1783 | bool isVoid = false; |
| 1784 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1785 | isVoid = Function->getReturnType()->isVoidType(); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1786 | else if (ObjCMethodDecl *Method |
| 1787 | = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1788 | isVoid = Method->getReturnType()->isVoidType(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1789 | else if (SemaRef.getCurBlock() && |
| 1790 | !SemaRef.getCurBlock()->ReturnType.isNull()) |
| 1791 | isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType(); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1792 | Builder.AddTypedTextChunk("return"); |
Douglas Gregor | 9329800 | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1793 | if (!isVoid) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1794 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1795 | Builder.AddPlaceholderChunk("expression"); |
Douglas Gregor | 9329800 | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1796 | } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1797 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1798 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1799 | // goto identifier ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1800 | Builder.AddTypedTextChunk("goto"); |
| 1801 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1802 | Builder.AddPlaceholderChunk("label"); |
| 1803 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1804 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1805 | // Using directives |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1806 | Builder.AddTypedTextChunk("using"); |
| 1807 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1808 | Builder.AddTextChunk("namespace"); |
| 1809 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1810 | Builder.AddPlaceholderChunk("identifier"); |
| 1811 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | // Fall through (for statement expressions). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1815 | case Sema::PCC_ForInit: |
| 1816 | case Sema::PCC_Condition: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1817 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1818 | // Fall through: conditions and statements can have expressions. |
| 1819 | |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1820 | case Sema::PCC_ParenthesizedExpression: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1821 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1822 | CCC == Sema::PCC_ParenthesizedExpression) { |
| 1823 | // (__bridge <type>)<expression> |
| 1824 | Builder.AddTypedTextChunk("__bridge"); |
| 1825 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1826 | Builder.AddPlaceholderChunk("type"); |
| 1827 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1828 | Builder.AddPlaceholderChunk("expression"); |
| 1829 | Results.AddResult(Result(Builder.TakeString())); |
| 1830 | |
| 1831 | // (__bridge_transfer <Objective-C type>)<expression> |
| 1832 | Builder.AddTypedTextChunk("__bridge_transfer"); |
| 1833 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1834 | Builder.AddPlaceholderChunk("Objective-C type"); |
| 1835 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1836 | Builder.AddPlaceholderChunk("expression"); |
| 1837 | Results.AddResult(Result(Builder.TakeString())); |
| 1838 | |
| 1839 | // (__bridge_retained <CF type>)<expression> |
| 1840 | Builder.AddTypedTextChunk("__bridge_retained"); |
| 1841 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1842 | Builder.AddPlaceholderChunk("CF type"); |
| 1843 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1844 | Builder.AddPlaceholderChunk("expression"); |
| 1845 | Results.AddResult(Result(Builder.TakeString())); |
| 1846 | } |
| 1847 | // Fall through |
| 1848 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1849 | case Sema::PCC_Expression: { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1850 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1851 | // 'this', if we're in a non-static member function. |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1852 | addThisCompletion(SemaRef, Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1853 | |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1854 | // true |
| 1855 | Builder.AddResultTypeChunk("bool"); |
| 1856 | Builder.AddTypedTextChunk("true"); |
| 1857 | Results.AddResult(Result(Builder.TakeString())); |
| 1858 | |
| 1859 | // false |
| 1860 | Builder.AddResultTypeChunk("bool"); |
| 1861 | Builder.AddTypedTextChunk("false"); |
| 1862 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1863 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1864 | if (SemaRef.getLangOpts().RTTI) { |
Douglas Gregor | ec3310a | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1865 | // dynamic_cast < type-id > ( expression ) |
| 1866 | Builder.AddTypedTextChunk("dynamic_cast"); |
| 1867 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1868 | Builder.AddPlaceholderChunk("type"); |
| 1869 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1870 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1871 | Builder.AddPlaceholderChunk("expression"); |
| 1872 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1873 | Results.AddResult(Result(Builder.TakeString())); |
| 1874 | } |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1875 | |
| 1876 | // static_cast < type-id > ( expression ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1877 | Builder.AddTypedTextChunk("static_cast"); |
| 1878 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1879 | Builder.AddPlaceholderChunk("type"); |
| 1880 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1881 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1882 | Builder.AddPlaceholderChunk("expression"); |
| 1883 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1884 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1885 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1886 | // reinterpret_cast < type-id > ( expression ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1887 | Builder.AddTypedTextChunk("reinterpret_cast"); |
| 1888 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1889 | Builder.AddPlaceholderChunk("type"); |
| 1890 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1891 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1892 | Builder.AddPlaceholderChunk("expression"); |
| 1893 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1894 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1895 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1896 | // const_cast < type-id > ( expression ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1897 | Builder.AddTypedTextChunk("const_cast"); |
| 1898 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1899 | Builder.AddPlaceholderChunk("type"); |
| 1900 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1901 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1902 | Builder.AddPlaceholderChunk("expression"); |
| 1903 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1904 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1905 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1906 | if (SemaRef.getLangOpts().RTTI) { |
Douglas Gregor | ec3310a | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1907 | // typeid ( expression-or-type ) |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1908 | Builder.AddResultTypeChunk("std::type_info"); |
Douglas Gregor | ec3310a | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1909 | Builder.AddTypedTextChunk("typeid"); |
| 1910 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1911 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 1912 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1913 | Results.AddResult(Result(Builder.TakeString())); |
| 1914 | } |
| 1915 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1916 | // new T ( ... ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1917 | Builder.AddTypedTextChunk("new"); |
| 1918 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1919 | Builder.AddPlaceholderChunk("type"); |
| 1920 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1921 | Builder.AddPlaceholderChunk("expressions"); |
| 1922 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1923 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1924 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1925 | // new T [ ] ( ... ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1926 | Builder.AddTypedTextChunk("new"); |
| 1927 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1928 | Builder.AddPlaceholderChunk("type"); |
| 1929 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1930 | Builder.AddPlaceholderChunk("size"); |
| 1931 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 1932 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1933 | Builder.AddPlaceholderChunk("expressions"); |
| 1934 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1935 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1936 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1937 | // delete expression |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1938 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1939 | Builder.AddTypedTextChunk("delete"); |
| 1940 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1941 | Builder.AddPlaceholderChunk("expression"); |
| 1942 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1943 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1944 | // delete [] expression |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1945 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1946 | Builder.AddTypedTextChunk("delete"); |
| 1947 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1948 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1949 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 1950 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1951 | Builder.AddPlaceholderChunk("expression"); |
| 1952 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1953 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1954 | if (SemaRef.getLangOpts().CXXExceptions) { |
Douglas Gregor | ec3310a | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1955 | // throw expression |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1956 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | ec3310a | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1957 | Builder.AddTypedTextChunk("throw"); |
| 1958 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1959 | Builder.AddPlaceholderChunk("expression"); |
| 1960 | Results.AddResult(Result(Builder.TakeString())); |
| 1961 | } |
Douglas Gregor | a50216c | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1962 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1963 | // FIXME: Rethrow? |
Douglas Gregor | a50216c | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1964 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1965 | if (SemaRef.getLangOpts().CPlusPlus11) { |
Douglas Gregor | a50216c | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1966 | // nullptr |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1967 | Builder.AddResultTypeChunk("std::nullptr_t"); |
Douglas Gregor | a50216c | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1968 | Builder.AddTypedTextChunk("nullptr"); |
| 1969 | Results.AddResult(Result(Builder.TakeString())); |
| 1970 | |
| 1971 | // alignof |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1972 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | a50216c | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1973 | Builder.AddTypedTextChunk("alignof"); |
| 1974 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1975 | Builder.AddPlaceholderChunk("type"); |
| 1976 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1977 | Results.AddResult(Result(Builder.TakeString())); |
| 1978 | |
| 1979 | // noexcept |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1980 | Builder.AddResultTypeChunk("bool"); |
Douglas Gregor | a50216c | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1981 | Builder.AddTypedTextChunk("noexcept"); |
| 1982 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1983 | Builder.AddPlaceholderChunk("expression"); |
| 1984 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1985 | Results.AddResult(Result(Builder.TakeString())); |
| 1986 | |
| 1987 | // sizeof... expression |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1988 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | a50216c | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 1989 | Builder.AddTypedTextChunk("sizeof..."); |
| 1990 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1991 | Builder.AddPlaceholderChunk("parameter-pack"); |
| 1992 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1993 | Results.AddResult(Result(Builder.TakeString())); |
| 1994 | } |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1997 | if (SemaRef.getLangOpts().ObjC1) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1998 | // Add "super", if we're in an Objective-C class with a superclass. |
Ted Kremenek | 681e256 | 2010-05-31 21:43:10 +0000 | [diff] [blame] | 1999 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
| 2000 | // The interface can be NULL. |
| 2001 | if (ObjCInterfaceDecl *ID = Method->getClassInterface()) |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2002 | if (ID->getSuperClass()) { |
| 2003 | std::string SuperType; |
| 2004 | SuperType = ID->getSuperClass()->getNameAsString(); |
| 2005 | if (Method->isInstanceMethod()) |
| 2006 | SuperType += " *"; |
| 2007 | |
| 2008 | Builder.AddResultTypeChunk(Allocator.CopyString(SuperType)); |
| 2009 | Builder.AddTypedTextChunk("super"); |
| 2010 | Results.AddResult(Result(Builder.TakeString())); |
| 2011 | } |
Ted Kremenek | 681e256 | 2010-05-31 21:43:10 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2014 | AddObjCExpressionResults(Results, true); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Jordan Rose | f70a886 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2017 | if (SemaRef.getLangOpts().C11) { |
| 2018 | // _Alignof |
| 2019 | Builder.AddResultTypeChunk("size_t"); |
| 2020 | if (SemaRef.getASTContext().Idents.get("alignof").hasMacroDefinition()) |
| 2021 | Builder.AddTypedTextChunk("alignof"); |
| 2022 | else |
| 2023 | Builder.AddTypedTextChunk("_Alignof"); |
| 2024 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2025 | Builder.AddPlaceholderChunk("type"); |
| 2026 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2027 | Results.AddResult(Result(Builder.TakeString())); |
| 2028 | } |
| 2029 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2030 | // sizeof expression |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2031 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2032 | Builder.AddTypedTextChunk("sizeof"); |
| 2033 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2034 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 2035 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2036 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2037 | break; |
| 2038 | } |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 2039 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2040 | case Sema::PCC_Type: |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2041 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 2042 | break; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2045 | if (WantTypesInContext(CCC, SemaRef.getLangOpts())) |
| 2046 | AddTypeSpecifierResults(SemaRef.getLangOpts(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2047 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2048 | if (SemaRef.getLangOpts().CPlusPlus && CCC != Sema::PCC_Type) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2049 | Results.AddResult(Result("operator")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2052 | /// \brief If the given declaration has an associated type, add it as a result |
| 2053 | /// type chunk. |
| 2054 | static void AddResultTypeChunk(ASTContext &Context, |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2055 | const PrintingPolicy &Policy, |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2056 | const NamedDecl *ND, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2057 | CodeCompletionBuilder &Result) { |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2058 | if (!ND) |
| 2059 | return; |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2060 | |
| 2061 | // Skip constructors and conversion functions, which have their return types |
| 2062 | // built into their names. |
| 2063 | if (isa<CXXConstructorDecl>(ND) || isa<CXXConversionDecl>(ND)) |
| 2064 | return; |
| 2065 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2066 | // Determine the type of the declaration (if it has a type). |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2067 | QualType T; |
| 2068 | if (const FunctionDecl *Function = ND->getAsFunction()) |
| 2069 | T = Function->getReturnType(); |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2070 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2071 | T = Method->getReturnType(); |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2072 | else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2073 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); |
| 2074 | else if (isa<UnresolvedUsingValueDecl>(ND)) { |
| 2075 | /* Do nothing: ignore unresolved using declarations*/ |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2076 | } else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND)) { |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2077 | T = Value->getType(); |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2078 | } else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2079 | T = Property->getType(); |
| 2080 | |
| 2081 | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) |
| 2082 | return; |
| 2083 | |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2084 | Result.AddResultTypeChunk(GetCompletionTypeString(T, Context, Policy, |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2085 | Result.getAllocator())); |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2088 | static void MaybeAddSentinel(ASTContext &Context, |
| 2089 | const NamedDecl *FunctionOrMethod, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2090 | CodeCompletionBuilder &Result) { |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2091 | if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) |
| 2092 | if (Sentinel->getSentinel() == 0) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2093 | if (Context.getLangOpts().ObjC1 && |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2094 | Context.Idents.get("nil").hasMacroDefinition()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2095 | Result.AddTextChunk(", nil"); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2096 | else if (Context.Idents.get("NULL").hasMacroDefinition()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2097 | Result.AddTextChunk(", NULL"); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2098 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2099 | Result.AddTextChunk(", (void*)0"); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2100 | } |
| 2101 | } |
| 2102 | |
Douglas Gregor | 6fa14dd | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2103 | static std::string formatObjCParamQualifiers(unsigned ObjCQuals) { |
| 2104 | std::string Result; |
| 2105 | if (ObjCQuals & Decl::OBJC_TQ_In) |
Douglas Gregor | 6ef9209 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2106 | Result += "in "; |
Douglas Gregor | 6fa14dd | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2107 | else if (ObjCQuals & Decl::OBJC_TQ_Inout) |
Douglas Gregor | 6ef9209 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2108 | Result += "inout "; |
Douglas Gregor | 6fa14dd | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2109 | else if (ObjCQuals & Decl::OBJC_TQ_Out) |
Douglas Gregor | 6ef9209 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2110 | Result += "out "; |
Douglas Gregor | 6fa14dd | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2111 | if (ObjCQuals & Decl::OBJC_TQ_Bycopy) |
Douglas Gregor | 6ef9209 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2112 | Result += "bycopy "; |
Douglas Gregor | 6fa14dd | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2113 | else if (ObjCQuals & Decl::OBJC_TQ_Byref) |
Douglas Gregor | 6ef9209 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2114 | Result += "byref "; |
Douglas Gregor | 6fa14dd | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2115 | if (ObjCQuals & Decl::OBJC_TQ_Oneway) |
Douglas Gregor | 6ef9209 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2116 | Result += "oneway "; |
Douglas Gregor | 6fa14dd | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2117 | return Result; |
| 2118 | } |
| 2119 | |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2120 | static std::string FormatFunctionParameter(ASTContext &Context, |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2121 | const PrintingPolicy &Policy, |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2122 | const ParmVarDecl *Param, |
Douglas Gregor | ee1c68a | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2123 | bool SuppressName = false, |
| 2124 | bool SuppressBlock = false) { |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2125 | bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext()); |
| 2126 | if (Param->getType()->isDependentType() || |
| 2127 | !Param->getType()->isBlockPointerType()) { |
| 2128 | // The argument for a dependent or non-block parameter is a placeholder |
| 2129 | // containing that parameter's type. |
| 2130 | std::string Result; |
| 2131 | |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2132 | if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName) |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2133 | Result = Param->getIdentifier()->getName(); |
| 2134 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2135 | Param->getType().getAsStringInternal(Result, Policy); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2136 | |
| 2137 | if (ObjCMethodParam) { |
Douglas Gregor | 6fa14dd | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2138 | Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier()) |
| 2139 | + Result + ")"; |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2140 | if (Param->getIdentifier() && !SuppressName) |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2141 | Result += Param->getIdentifier()->getName(); |
| 2142 | } |
| 2143 | return Result; |
| 2144 | } |
| 2145 | |
| 2146 | // The argument for a block pointer parameter is a block literal with |
| 2147 | // the appropriate type. |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2148 | FunctionTypeLoc Block; |
| 2149 | FunctionProtoTypeLoc BlockProto; |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2150 | TypeLoc TL; |
| 2151 | if (TypeSourceInfo *TSInfo = Param->getTypeSourceInfo()) { |
| 2152 | TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 2153 | while (true) { |
| 2154 | // Look through typedefs. |
Douglas Gregor | ee1c68a | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2155 | if (!SuppressBlock) { |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2156 | if (TypedefTypeLoc TypedefTL = TL.getAs<TypedefTypeLoc>()) { |
| 2157 | if (TypeSourceInfo *InnerTSInfo = |
| 2158 | TypedefTL.getTypedefNameDecl()->getTypeSourceInfo()) { |
Douglas Gregor | ee1c68a | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2159 | TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 2160 | continue; |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | // Look through qualified types |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2165 | if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>()) { |
| 2166 | TL = QualifiedTL.getUnqualifiedLoc(); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2167 | continue; |
| 2168 | } |
| 2169 | } |
| 2170 | |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2171 | // Try to get the function prototype behind the block pointer type, |
| 2172 | // then we're done. |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2173 | if (BlockPointerTypeLoc BlockPtr = TL.getAs<BlockPointerTypeLoc>()) { |
| 2174 | TL = BlockPtr.getPointeeLoc().IgnoreParens(); |
| 2175 | Block = TL.getAs<FunctionTypeLoc>(); |
| 2176 | BlockProto = TL.getAs<FunctionProtoTypeLoc>(); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2177 | } |
| 2178 | break; |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | if (!Block) { |
| 2183 | // We were unable to find a FunctionProtoTypeLoc with parameter names |
| 2184 | // for the block; just use the parameter type as a placeholder. |
| 2185 | std::string Result; |
Douglas Gregor | ee1c68a | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2186 | if (!ObjCMethodParam && Param->getIdentifier()) |
| 2187 | Result = Param->getIdentifier()->getName(); |
| 2188 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2189 | Param->getType().getUnqualifiedType().getAsStringInternal(Result, Policy); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2190 | |
| 2191 | if (ObjCMethodParam) { |
Douglas Gregor | 6fa14dd | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2192 | Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier()) |
| 2193 | + Result + ")"; |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2194 | if (Param->getIdentifier()) |
| 2195 | Result += Param->getIdentifier()->getName(); |
| 2196 | } |
| 2197 | |
| 2198 | return Result; |
| 2199 | } |
Douglas Gregor | ee1c68a | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2200 | |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2201 | // We have the function prototype behind the block pointer type, as it was |
| 2202 | // written in the source. |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2203 | std::string Result; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2204 | QualType ResultType = Block.getTypePtr()->getReturnType(); |
Douglas Gregor | ee1c68a | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2205 | if (!ResultType->isVoidType() || SuppressBlock) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2206 | ResultType.getAsStringInternal(Result, Policy); |
Douglas Gregor | ee1c68a | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2207 | |
| 2208 | // Format the parameter list. |
| 2209 | std::string Params; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2210 | if (!BlockProto || Block.getNumParams() == 0) { |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2211 | if (BlockProto && BlockProto.getTypePtr()->isVariadic()) |
Douglas Gregor | ee1c68a | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2212 | Params = "(...)"; |
Douglas Gregor | c2760bc | 2010-10-02 23:49:58 +0000 | [diff] [blame] | 2213 | else |
Douglas Gregor | ee1c68a | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2214 | Params = "(void)"; |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2215 | } else { |
Douglas Gregor | ee1c68a | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2216 | Params += "("; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2217 | for (unsigned I = 0, N = Block.getNumParams(); I != N; ++I) { |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2218 | if (I) |
Douglas Gregor | ee1c68a | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2219 | Params += ", "; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2220 | Params += FormatFunctionParameter(Context, Policy, Block.getParam(I), |
| 2221 | /*SuppressName=*/false, |
Douglas Gregor | ee1c68a | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2222 | /*SuppressBlock=*/true); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2223 | |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2224 | if (I == N - 1 && BlockProto.getTypePtr()->isVariadic()) |
Douglas Gregor | ee1c68a | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2225 | Params += ", ..."; |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2226 | } |
Douglas Gregor | ee1c68a | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2227 | Params += ")"; |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2228 | } |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2229 | |
Douglas Gregor | ee1c68a | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2230 | if (SuppressBlock) { |
| 2231 | // Format as a parameter. |
| 2232 | Result = Result + " (^"; |
| 2233 | if (Param->getIdentifier()) |
| 2234 | Result += Param->getIdentifier()->getName(); |
| 2235 | Result += ")"; |
| 2236 | Result += Params; |
| 2237 | } else { |
| 2238 | // Format as a block literal argument. |
| 2239 | Result = '^' + Result; |
| 2240 | Result += Params; |
| 2241 | |
| 2242 | if (Param->getIdentifier()) |
| 2243 | Result += Param->getIdentifier()->getName(); |
| 2244 | } |
| 2245 | |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2246 | return Result; |
| 2247 | } |
| 2248 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2249 | /// \brief Add function parameter chunks to the given code completion string. |
| 2250 | static void AddFunctionParameterChunks(ASTContext &Context, |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2251 | const PrintingPolicy &Policy, |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2252 | const FunctionDecl *Function, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2253 | CodeCompletionBuilder &Result, |
| 2254 | unsigned Start = 0, |
| 2255 | bool InOptional = false) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2256 | bool FirstParameter = true; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2257 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2258 | for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) { |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2259 | const ParmVarDecl *Param = Function->getParamDecl(P); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2260 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2261 | if (Param->hasDefaultArg() && !InOptional) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2262 | // When we see an optional default argument, put that argument and |
| 2263 | // the remaining default arguments into a new, optional string. |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2264 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2265 | Result.getCodeCompletionTUInfo()); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2266 | if (!FirstParameter) |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2267 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2268 | AddFunctionParameterChunks(Context, Policy, Function, Opt, P, true); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2269 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2270 | break; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2273 | if (FirstParameter) |
| 2274 | FirstParameter = false; |
| 2275 | else |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2276 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2277 | |
| 2278 | InOptional = false; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2279 | |
| 2280 | // Format the placeholder string. |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2281 | std::string PlaceholderStr = FormatFunctionParameter(Context, Policy, |
| 2282 | Param); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2283 | |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2284 | if (Function->isVariadic() && P == N - 1) |
| 2285 | PlaceholderStr += ", ..."; |
| 2286 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2287 | // Add the placeholder string. |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2288 | Result.AddPlaceholderChunk( |
| 2289 | Result.getAllocator().CopyString(PlaceholderStr)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2290 | } |
Douglas Gregor | b3d4525 | 2009-09-22 21:42:17 +0000 | [diff] [blame] | 2291 | |
| 2292 | if (const FunctionProtoType *Proto |
| 2293 | = Function->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2294 | if (Proto->isVariadic()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2295 | if (Proto->getNumParams() == 0) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2296 | Result.AddPlaceholderChunk("..."); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2297 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2298 | MaybeAddSentinel(Context, Function, Result); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2299 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | /// \brief Add template parameter chunks to the given code completion string. |
| 2303 | static void AddTemplateParameterChunks(ASTContext &Context, |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2304 | const PrintingPolicy &Policy, |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2305 | const TemplateDecl *Template, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2306 | CodeCompletionBuilder &Result, |
| 2307 | unsigned MaxParameters = 0, |
| 2308 | unsigned Start = 0, |
| 2309 | bool InDefaultArg = false) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2310 | bool FirstParameter = true; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2311 | |
| 2312 | // Prefer to take the template parameter names from the first declaration of |
| 2313 | // the template. |
| 2314 | Template = cast<TemplateDecl>(Template->getCanonicalDecl()); |
| 2315 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2316 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2317 | TemplateParameterList::iterator PEnd = Params->end(); |
| 2318 | if (MaxParameters) |
| 2319 | PEnd = Params->begin() + MaxParameters; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2320 | for (TemplateParameterList::iterator P = Params->begin() + Start; |
| 2321 | P != PEnd; ++P) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2322 | bool HasDefaultArg = false; |
| 2323 | std::string PlaceholderStr; |
| 2324 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 2325 | if (TTP->wasDeclaredWithTypename()) |
| 2326 | PlaceholderStr = "typename"; |
| 2327 | else |
| 2328 | PlaceholderStr = "class"; |
| 2329 | |
| 2330 | if (TTP->getIdentifier()) { |
| 2331 | PlaceholderStr += ' '; |
| 2332 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2333 | } |
| 2334 | |
| 2335 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2336 | } else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2337 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2338 | if (NTTP->getIdentifier()) |
| 2339 | PlaceholderStr = NTTP->getIdentifier()->getName(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2340 | NTTP->getType().getAsStringInternal(PlaceholderStr, Policy); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2341 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2342 | } else { |
| 2343 | assert(isa<TemplateTemplateParmDecl>(*P)); |
| 2344 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 2345 | |
| 2346 | // Since putting the template argument list into the placeholder would |
| 2347 | // be very, very long, we just use an abbreviation. |
| 2348 | PlaceholderStr = "template<...> class"; |
| 2349 | if (TTP->getIdentifier()) { |
| 2350 | PlaceholderStr += ' '; |
| 2351 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2352 | } |
| 2353 | |
| 2354 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2355 | } |
| 2356 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2357 | if (HasDefaultArg && !InDefaultArg) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2358 | // When we see an optional default argument, put that argument and |
| 2359 | // the remaining default arguments into a new, optional string. |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2360 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2361 | Result.getCodeCompletionTUInfo()); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2362 | if (!FirstParameter) |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2363 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2364 | AddTemplateParameterChunks(Context, Policy, Template, Opt, MaxParameters, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2365 | P - Params->begin(), true); |
| 2366 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2367 | break; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2370 | InDefaultArg = false; |
| 2371 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2372 | if (FirstParameter) |
| 2373 | FirstParameter = false; |
| 2374 | else |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2375 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2376 | |
| 2377 | // Add the placeholder string. |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2378 | Result.AddPlaceholderChunk( |
| 2379 | Result.getAllocator().CopyString(PlaceholderStr)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2380 | } |
| 2381 | } |
| 2382 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2383 | /// \brief Add a qualifier to the given code-completion string, if the |
| 2384 | /// provided nested-name-specifier is non-NULL. |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2385 | static void |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2386 | AddQualifierToCompletionString(CodeCompletionBuilder &Result, |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2387 | NestedNameSpecifier *Qualifier, |
| 2388 | bool QualifierIsInformative, |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2389 | ASTContext &Context, |
| 2390 | const PrintingPolicy &Policy) { |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2391 | if (!Qualifier) |
| 2392 | return; |
| 2393 | |
| 2394 | std::string PrintedNNS; |
| 2395 | { |
| 2396 | llvm::raw_string_ostream OS(PrintedNNS); |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2397 | Qualifier->print(OS, Policy); |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2398 | } |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2399 | if (QualifierIsInformative) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2400 | Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2401 | else |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2402 | Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2405 | static void |
| 2406 | AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result, |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2407 | const FunctionDecl *Function) { |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2408 | const FunctionProtoType *Proto |
| 2409 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2410 | if (!Proto || !Proto->getTypeQuals()) |
| 2411 | return; |
| 2412 | |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2413 | // FIXME: Add ref-qualifier! |
| 2414 | |
| 2415 | // Handle single qualifiers without copying |
| 2416 | if (Proto->getTypeQuals() == Qualifiers::Const) { |
| 2417 | Result.AddInformativeChunk(" const"); |
| 2418 | return; |
| 2419 | } |
| 2420 | |
| 2421 | if (Proto->getTypeQuals() == Qualifiers::Volatile) { |
| 2422 | Result.AddInformativeChunk(" volatile"); |
| 2423 | return; |
| 2424 | } |
| 2425 | |
| 2426 | if (Proto->getTypeQuals() == Qualifiers::Restrict) { |
| 2427 | Result.AddInformativeChunk(" restrict"); |
| 2428 | return; |
| 2429 | } |
| 2430 | |
| 2431 | // Handle multiple qualifiers. |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2432 | std::string QualsStr; |
David Blaikie | 4ef832f | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2433 | if (Proto->isConst()) |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2434 | QualsStr += " const"; |
David Blaikie | 4ef832f | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2435 | if (Proto->isVolatile()) |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2436 | QualsStr += " volatile"; |
David Blaikie | 4ef832f | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2437 | if (Proto->isRestrict()) |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2438 | QualsStr += " restrict"; |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2439 | Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr)); |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2442 | /// \brief Add the name of the given declaration |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2443 | static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy, |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2444 | const NamedDecl *ND, |
| 2445 | CodeCompletionBuilder &Result) { |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2446 | DeclarationName Name = ND->getDeclName(); |
| 2447 | if (!Name) |
| 2448 | return; |
| 2449 | |
| 2450 | switch (Name.getNameKind()) { |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2451 | case DeclarationName::CXXOperatorName: { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2452 | const char *OperatorName = nullptr; |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2453 | switch (Name.getCXXOverloadedOperator()) { |
| 2454 | case OO_None: |
| 2455 | case OO_Conditional: |
| 2456 | case NUM_OVERLOADED_OPERATORS: |
| 2457 | OperatorName = "operator"; |
| 2458 | break; |
| 2459 | |
| 2460 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 2461 | case OO_##Name: OperatorName = "operator" Spelling; break; |
| 2462 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) |
| 2463 | #include "clang/Basic/OperatorKinds.def" |
| 2464 | |
| 2465 | case OO_New: OperatorName = "operator new"; break; |
| 2466 | case OO_Delete: OperatorName = "operator delete"; break; |
| 2467 | case OO_Array_New: OperatorName = "operator new[]"; break; |
| 2468 | case OO_Array_Delete: OperatorName = "operator delete[]"; break; |
| 2469 | case OO_Call: OperatorName = "operator()"; break; |
| 2470 | case OO_Subscript: OperatorName = "operator[]"; break; |
| 2471 | } |
| 2472 | Result.AddTypedTextChunk(OperatorName); |
| 2473 | break; |
| 2474 | } |
| 2475 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2476 | case DeclarationName::Identifier: |
| 2477 | case DeclarationName::CXXConversionFunctionName: |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2478 | case DeclarationName::CXXDestructorName: |
| 2479 | case DeclarationName::CXXLiteralOperatorName: |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2480 | Result.AddTypedTextChunk( |
| 2481 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2482 | break; |
| 2483 | |
| 2484 | case DeclarationName::CXXUsingDirective: |
| 2485 | case DeclarationName::ObjCZeroArgSelector: |
| 2486 | case DeclarationName::ObjCOneArgSelector: |
| 2487 | case DeclarationName::ObjCMultiArgSelector: |
| 2488 | break; |
| 2489 | |
| 2490 | case DeclarationName::CXXConstructorName: { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2491 | CXXRecordDecl *Record = nullptr; |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2492 | QualType Ty = Name.getCXXNameType(); |
| 2493 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) |
| 2494 | Record = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 2495 | else if (const InjectedClassNameType *InjectedTy |
| 2496 | = Ty->getAs<InjectedClassNameType>()) |
| 2497 | Record = InjectedTy->getDecl(); |
| 2498 | else { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2499 | Result.AddTypedTextChunk( |
| 2500 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2501 | break; |
| 2502 | } |
| 2503 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2504 | Result.AddTypedTextChunk( |
| 2505 | Result.getAllocator().CopyString(Record->getNameAsString())); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2506 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2507 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2508 | AddTemplateParameterChunks(Context, Policy, Template, Result); |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2509 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2510 | } |
| 2511 | break; |
| 2512 | } |
| 2513 | } |
| 2514 | } |
| 2515 | |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2516 | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString(Sema &S, |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2517 | CodeCompletionAllocator &Allocator, |
Dmitri Gribenko | d99ef53 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2518 | CodeCompletionTUInfo &CCTUInfo, |
| 2519 | bool IncludeBriefComments) { |
| 2520 | return CreateCodeCompletionString(S.Context, S.PP, Allocator, CCTUInfo, |
| 2521 | IncludeBriefComments); |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2524 | /// \brief If possible, create a new code completion string for the given |
| 2525 | /// result. |
| 2526 | /// |
| 2527 | /// \returns Either a new, heap-allocated code completion string describing |
| 2528 | /// how to use this result, or NULL to indicate that the string or name of the |
| 2529 | /// result is all that is needed. |
| 2530 | CodeCompletionString * |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2531 | CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx, |
| 2532 | Preprocessor &PP, |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2533 | CodeCompletionAllocator &Allocator, |
Dmitri Gribenko | d99ef53 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2534 | CodeCompletionTUInfo &CCTUInfo, |
| 2535 | bool IncludeBriefComments) { |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2536 | CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2537 | |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2538 | PrintingPolicy Policy = getCompletionPrintingPolicy(Ctx, PP); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2539 | if (Kind == RK_Pattern) { |
| 2540 | Pattern->Priority = Priority; |
| 2541 | Pattern->Availability = Availability; |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2542 | |
| 2543 | if (Declaration) { |
| 2544 | Result.addParentContext(Declaration->getDeclContext()); |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2545 | Pattern->ParentName = Result.getParentName(); |
Fariborz Jahanian | c02ddb2 | 2013-03-22 17:55:27 +0000 | [diff] [blame] | 2546 | // Provide code completion comment for self.GetterName where |
| 2547 | // GetterName is the getter method for a property with name |
| 2548 | // different from the property name (declared via a property |
| 2549 | // getter attribute. |
| 2550 | const NamedDecl *ND = Declaration; |
| 2551 | if (const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(ND)) |
| 2552 | if (M->isPropertyAccessor()) |
| 2553 | if (const ObjCPropertyDecl *PDecl = M->findPropertyDecl()) |
| 2554 | if (PDecl->getGetterName() == M->getSelector() && |
Fariborz Jahanian | 1686137 | 2013-03-23 01:10:45 +0000 | [diff] [blame] | 2555 | PDecl->getIdentifier() != M->getIdentifier()) { |
| 2556 | if (const RawComment *RC = |
| 2557 | Ctx.getRawCommentForAnyRedecl(M)) { |
Fariborz Jahanian | c02ddb2 | 2013-03-22 17:55:27 +0000 | [diff] [blame] | 2558 | Result.addBriefComment(RC->getBriefText(Ctx)); |
| 2559 | Pattern->BriefComment = Result.getBriefComment(); |
| 2560 | } |
Fariborz Jahanian | 1686137 | 2013-03-23 01:10:45 +0000 | [diff] [blame] | 2561 | else if (const RawComment *RC = |
| 2562 | Ctx.getRawCommentForAnyRedecl(PDecl)) { |
| 2563 | Result.addBriefComment(RC->getBriefText(Ctx)); |
| 2564 | Pattern->BriefComment = Result.getBriefComment(); |
| 2565 | } |
| 2566 | } |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2567 | } |
| 2568 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2569 | return Pattern; |
| 2570 | } |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2571 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2572 | if (Kind == RK_Keyword) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2573 | Result.AddTypedTextChunk(Keyword); |
| 2574 | return Result.TakeString(); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2575 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2576 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2577 | if (Kind == RK_Macro) { |
Argyrios Kyrtzidis | 9818a1d | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 2578 | const MacroDirective *MD = PP.getMacroDirectiveHistory(Macro); |
| 2579 | assert(MD && "Not a macro?"); |
Argyrios Kyrtzidis | c56fff7 | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 2580 | const MacroInfo *MI = MD->getMacroInfo(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2581 | assert((!MD->isDefined() || MI) && "missing MacroInfo for define"); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2582 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2583 | Result.AddTypedTextChunk( |
| 2584 | Result.getAllocator().CopyString(Macro->getName())); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2585 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2586 | if (!MI || !MI->isFunctionLike()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2587 | return Result.TakeString(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2588 | |
| 2589 | // Format a function-like macro with placeholders for the arguments. |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2590 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | e424470 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2591 | MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end(); |
Douglas Gregor | c8dc135 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2592 | |
| 2593 | // C99 variadic macros add __VA_ARGS__ at the end. Skip it. |
| 2594 | if (MI->isC99Varargs()) { |
| 2595 | --AEnd; |
| 2596 | |
| 2597 | if (A == AEnd) { |
| 2598 | Result.AddPlaceholderChunk("..."); |
| 2599 | } |
Douglas Gregor | e424470 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2600 | } |
Douglas Gregor | c8dc135 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2601 | |
Douglas Gregor | e424470 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2602 | for (MacroInfo::arg_iterator A = MI->arg_begin(); A != AEnd; ++A) { |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2603 | if (A != MI->arg_begin()) |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2604 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | c8dc135 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2605 | |
| 2606 | if (MI->isVariadic() && (A+1) == AEnd) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2607 | SmallString<32> Arg = (*A)->getName(); |
Douglas Gregor | c8dc135 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2608 | if (MI->isC99Varargs()) |
| 2609 | Arg += ", ..."; |
| 2610 | else |
| 2611 | Arg += "..."; |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2612 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | c8dc135 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2613 | break; |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2614 | } |
Douglas Gregor | c8dc135 | 2012-01-21 00:43:38 +0000 | [diff] [blame] | 2615 | |
| 2616 | // Non-variadic macros are simple. |
| 2617 | Result.AddPlaceholderChunk( |
| 2618 | Result.getAllocator().CopyString((*A)->getName())); |
Douglas Gregor | e424470 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2619 | } |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2620 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2621 | return Result.TakeString(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2624 | assert(Kind == RK_Declaration && "Missed a result kind?"); |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2625 | const NamedDecl *ND = Declaration; |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2626 | Result.addParentContext(ND->getDeclContext()); |
Dmitri Gribenko | d99ef53 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2627 | |
| 2628 | if (IncludeBriefComments) { |
| 2629 | // Add documentation comment, if it exists. |
Dmitri Gribenko | f50555e | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 2630 | if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(ND)) { |
Dmitri Gribenko | d99ef53 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2631 | Result.addBriefComment(RC->getBriefText(Ctx)); |
Fariborz Jahanian | b98f7af | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 2632 | } |
| 2633 | else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
| 2634 | if (OMD->isPropertyAccessor()) |
| 2635 | if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) |
| 2636 | if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(PDecl)) |
| 2637 | Result.addBriefComment(RC->getBriefText(Ctx)); |
Dmitri Gribenko | d99ef53 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2640 | if (StartsNestedNameSpecifier) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2641 | Result.AddTypedTextChunk( |
| 2642 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2643 | Result.AddTextChunk("::"); |
| 2644 | return Result.TakeString(); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2645 | } |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 2646 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2647 | for (const auto *I : ND->specific_attrs<AnnotateAttr>()) |
| 2648 | Result.AddAnnotation(Result.getAllocator().CopyString(I->getAnnotation())); |
| 2649 | |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2650 | AddResultTypeChunk(Ctx, Policy, ND, Result); |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2651 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2652 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2653 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2654 | Ctx, Policy); |
| 2655 | AddTypedNameChunk(Ctx, Policy, ND, Result); |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2656 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2657 | AddFunctionParameterChunks(Ctx, Policy, Function, Result); |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2658 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2659 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2660 | return Result.TakeString(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2663 | if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2664 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2665 | Ctx, Policy); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2666 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2667 | AddTypedNameChunk(Ctx, Policy, Function, Result); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2668 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2669 | // Figure out which template parameters are deduced (or have default |
| 2670 | // arguments). |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 2671 | llvm::SmallBitVector Deduced; |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2672 | Sema::MarkDeducedTemplateParameters(Ctx, FunTmpl, Deduced); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2673 | unsigned LastDeducibleArgument; |
| 2674 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
| 2675 | --LastDeducibleArgument) { |
| 2676 | if (!Deduced[LastDeducibleArgument - 1]) { |
| 2677 | // C++0x: Figure out if the template argument has a default. If so, |
| 2678 | // the user doesn't need to type this argument. |
| 2679 | // FIXME: We need to abstract template parameters better! |
| 2680 | bool HasDefaultArg = false; |
| 2681 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2682 | LastDeducibleArgument - 1); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2683 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 2684 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2685 | else if (NonTypeTemplateParmDecl *NTTP |
| 2686 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 2687 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2688 | else { |
| 2689 | assert(isa<TemplateTemplateParmDecl>(Param)); |
| 2690 | HasDefaultArg |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2691 | = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
| 2694 | if (!HasDefaultArg) |
| 2695 | break; |
| 2696 | } |
| 2697 | } |
| 2698 | |
| 2699 | if (LastDeducibleArgument) { |
| 2700 | // Some of the function template arguments cannot be deduced from a |
| 2701 | // function call, so we introduce an explicit template argument list |
| 2702 | // containing all of the arguments up to the first deducible argument. |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2703 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2704 | AddTemplateParameterChunks(Ctx, Policy, FunTmpl, Result, |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2705 | LastDeducibleArgument); |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2706 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | // Add the function parameters |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2710 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2711 | AddFunctionParameterChunks(Ctx, Policy, Function, Result); |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2712 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2713 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2714 | return Result.TakeString(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2715 | } |
| 2716 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2717 | if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2718 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2719 | Ctx, Policy); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2720 | Result.AddTypedTextChunk( |
| 2721 | Result.getAllocator().CopyString(Template->getNameAsString())); |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2722 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2723 | AddTemplateParameterChunks(Ctx, Policy, Template, Result); |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2724 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2725 | return Result.TakeString(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2728 | if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2729 | Selector Sel = Method->getSelector(); |
| 2730 | if (Sel.isUnarySelector()) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2731 | Result.AddTypedTextChunk(Result.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2732 | Sel.getNameForSlot(0))); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2733 | return Result.TakeString(); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2736 | std::string SelName = Sel.getNameForSlot(0).str(); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2737 | SelName += ':'; |
| 2738 | if (StartParameter == 0) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2739 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName)); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2740 | else { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2741 | Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName)); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2742 | |
| 2743 | // If there is only one parameter, and we're past it, add an empty |
| 2744 | // typed-text chunk since there is nothing to type. |
| 2745 | if (Method->param_size() == 1) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2746 | Result.AddTypedTextChunk(""); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2747 | } |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2748 | unsigned Idx = 0; |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2749 | for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(), |
| 2750 | PEnd = Method->param_end(); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2751 | P != PEnd; (void)++P, ++Idx) { |
| 2752 | if (Idx > 0) { |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2753 | std::string Keyword; |
| 2754 | if (Idx > StartParameter) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2755 | Result.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2756 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) |
Benjamin Kramer | a0651c5 | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 2757 | Keyword += II->getName(); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2758 | Keyword += ":"; |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2759 | if (Idx < StartParameter || AllParametersAreInformative) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2760 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword)); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2761 | else |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2762 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword)); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2763 | } |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2764 | |
| 2765 | // If we're before the starting parameter, skip the placeholder. |
| 2766 | if (Idx < StartParameter) |
| 2767 | continue; |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2768 | |
| 2769 | std::string Arg; |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2770 | |
| 2771 | if ((*P)->getType()->isBlockPointerType() && !DeclaringEntity) |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2772 | Arg = FormatFunctionParameter(Ctx, Policy, *P, true); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2773 | else { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2774 | (*P)->getType().getAsStringInternal(Arg, Policy); |
Douglas Gregor | 6fa14dd | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2775 | Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier()) |
| 2776 | + Arg + ")"; |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2777 | if (IdentifierInfo *II = (*P)->getIdentifier()) |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2778 | if (DeclaringEntity || AllParametersAreInformative) |
Benjamin Kramer | a0651c5 | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 2779 | Arg += II->getName(); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2782 | if (Method->isVariadic() && (P + 1) == PEnd) |
| 2783 | Arg += ", ..."; |
| 2784 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2785 | if (DeclaringEntity) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2786 | Result.AddTextChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2787 | else if (AllParametersAreInformative) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2788 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2789 | else |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2790 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
Douglas Gregor | 2a17af0 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2793 | if (Method->isVariadic()) { |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2794 | if (Method->param_size() == 0) { |
| 2795 | if (DeclaringEntity) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2796 | Result.AddTextChunk(", ..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2797 | else if (AllParametersAreInformative) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2798 | Result.AddInformativeChunk(", ..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2799 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2800 | Result.AddPlaceholderChunk(", ..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2801 | } |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2802 | |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2803 | MaybeAddSentinel(Ctx, Method, Result); |
Douglas Gregor | 2a17af0 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2806 | return Result.TakeString(); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2809 | if (Qualifier) |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2810 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | ea8c59a | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2811 | Ctx, Policy); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2812 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2813 | Result.AddTypedTextChunk( |
| 2814 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2815 | return Result.TakeString(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2818 | /// \brief Add function overload parameter chunks to the given code completion |
| 2819 | /// string. |
| 2820 | static void AddOverloadParameterChunks(ASTContext &Context, |
| 2821 | const PrintingPolicy &Policy, |
| 2822 | const FunctionDecl *Function, |
| 2823 | const FunctionProtoType *Prototype, |
| 2824 | CodeCompletionBuilder &Result, |
| 2825 | unsigned CurrentArg, |
| 2826 | unsigned Start = 0, |
| 2827 | bool InOptional = false) { |
| 2828 | bool FirstParameter = true; |
| 2829 | unsigned NumParams = Function ? Function->getNumParams() |
| 2830 | : Prototype->getNumParams(); |
| 2831 | |
| 2832 | for (unsigned P = Start; P != NumParams; ++P) { |
| 2833 | if (Function && Function->getParamDecl(P)->hasDefaultArg() && !InOptional) { |
| 2834 | // When we see an optional default argument, put that argument and |
| 2835 | // the remaining default arguments into a new, optional string. |
| 2836 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2837 | Result.getCodeCompletionTUInfo()); |
| 2838 | if (!FirstParameter) |
| 2839 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| 2840 | // Optional sections are nested. |
| 2841 | AddOverloadParameterChunks(Context, Policy, Function, Prototype, Opt, |
| 2842 | CurrentArg, P, /*InOptional=*/true); |
| 2843 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2844 | return; |
| 2845 | } |
| 2846 | |
| 2847 | if (FirstParameter) |
| 2848 | FirstParameter = false; |
| 2849 | else |
| 2850 | Result.AddChunk(CodeCompletionString::CK_Comma); |
| 2851 | |
| 2852 | InOptional = false; |
| 2853 | |
| 2854 | // Format the placeholder string. |
| 2855 | std::string Placeholder; |
| 2856 | if (Function) |
| 2857 | Placeholder = FormatFunctionParameter(Context, Policy, |
| 2858 | Function->getParamDecl(P)); |
| 2859 | else |
| 2860 | Placeholder = Prototype->getParamType(P).getAsString(Policy); |
| 2861 | |
| 2862 | if (P == CurrentArg) |
| 2863 | Result.AddCurrentParameterChunk( |
| 2864 | Result.getAllocator().CopyString(Placeholder)); |
| 2865 | else |
| 2866 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Placeholder)); |
| 2867 | } |
| 2868 | |
| 2869 | if (Prototype && Prototype->isVariadic()) { |
| 2870 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2871 | Result.getCodeCompletionTUInfo()); |
| 2872 | if (!FirstParameter) |
| 2873 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| 2874 | |
| 2875 | if (CurrentArg < NumParams) |
| 2876 | Opt.AddPlaceholderChunk("..."); |
| 2877 | else |
| 2878 | Opt.AddCurrentParameterChunk("..."); |
| 2879 | |
| 2880 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2881 | } |
| 2882 | } |
| 2883 | |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2884 | CodeCompletionString * |
| 2885 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2886 | unsigned CurrentArg, Sema &S, |
| 2887 | CodeCompletionAllocator &Allocator, |
| 2888 | CodeCompletionTUInfo &CCTUInfo, |
| 2889 | bool IncludeBriefComments) const { |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2890 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2891 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2892 | // FIXME: Set priority, availability appropriately. |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2893 | CodeCompletionBuilder Result(Allocator,CCTUInfo, 1, CXAvailability_Available); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2894 | FunctionDecl *FDecl = getFunction(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2895 | const FunctionProtoType *Proto |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2896 | = dyn_cast<FunctionProtoType>(getFunctionType()); |
| 2897 | if (!FDecl && !Proto) { |
| 2898 | // Function without a prototype. Just give the return type and a |
| 2899 | // highlighted ellipsis. |
| 2900 | const FunctionType *FT = getFunctionType(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2901 | Result.AddResultTypeChunk(Result.getAllocator().CopyString( |
| 2902 | FT->getReturnType().getAsString(Policy))); |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2903 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2904 | Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "..."); |
| 2905 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2906 | return Result.TakeString(); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2907 | } |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2908 | |
| 2909 | if (FDecl) { |
| 2910 | if (IncludeBriefComments && CurrentArg < FDecl->getNumParams()) |
| 2911 | if (auto RC = S.getASTContext().getRawCommentForAnyRedecl( |
| 2912 | FDecl->getParamDecl(CurrentArg))) |
| 2913 | Result.addBriefComment(RC->getBriefText(S.getASTContext())); |
| 2914 | AddResultTypeChunk(S.Context, Policy, FDecl, Result); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2915 | Result.AddTextChunk( |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2916 | Result.getAllocator().CopyString(FDecl->getNameAsString())); |
| 2917 | } else { |
| 2918 | Result.AddResultTypeChunk( |
| 2919 | Result.getAllocator().CopyString( |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2920 | Proto->getReturnType().getAsString(Policy))); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2921 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2922 | |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2923 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2924 | AddOverloadParameterChunks(S.getASTContext(), Policy, FDecl, Proto, Result, |
| 2925 | CurrentArg); |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2926 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2927 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2928 | return Result.TakeString(); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2931 | unsigned clang::getMacroUsagePriority(StringRef MacroName, |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2932 | const LangOptions &LangOpts, |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2933 | bool PreferredTypeIsPointer) { |
| 2934 | unsigned Priority = CCP_Macro; |
| 2935 | |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2936 | // Treat the "nil", "Nil" and "NULL" macros as null pointer constants. |
| 2937 | if (MacroName.equals("nil") || MacroName.equals("NULL") || |
| 2938 | MacroName.equals("Nil")) { |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2939 | Priority = CCP_Constant; |
| 2940 | if (PreferredTypeIsPointer) |
| 2941 | Priority = Priority / CCF_SimilarTypeMatch; |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2942 | } |
| 2943 | // Treat "YES", "NO", "true", and "false" as constants. |
| 2944 | else if (MacroName.equals("YES") || MacroName.equals("NO") || |
| 2945 | MacroName.equals("true") || MacroName.equals("false")) |
| 2946 | Priority = CCP_Constant; |
| 2947 | // Treat "bool" as a type. |
| 2948 | else if (MacroName.equals("bool")) |
| 2949 | Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0); |
| 2950 | |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2951 | |
| 2952 | return Priority; |
| 2953 | } |
| 2954 | |
Dmitri Gribenko | 06d8c60 | 2013-01-11 20:32:41 +0000 | [diff] [blame] | 2955 | CXCursorKind clang::getCursorKindForDecl(const Decl *D) { |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 2956 | if (!D) |
| 2957 | return CXCursor_UnexposedDecl; |
| 2958 | |
| 2959 | switch (D->getKind()) { |
| 2960 | case Decl::Enum: return CXCursor_EnumDecl; |
| 2961 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 2962 | case Decl::Field: return CXCursor_FieldDecl; |
| 2963 | case Decl::Function: |
| 2964 | return CXCursor_FunctionDecl; |
| 2965 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 2966 | case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryImplDecl; |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 2967 | case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl; |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 2968 | |
Argyrios Kyrtzidis | c15707d | 2012-01-24 21:39:26 +0000 | [diff] [blame] | 2969 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 2970 | case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl; |
| 2971 | case Decl::ObjCMethod: |
| 2972 | return cast<ObjCMethodDecl>(D)->isInstanceMethod() |
| 2973 | ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl; |
| 2974 | case Decl::CXXMethod: return CXCursor_CXXMethod; |
| 2975 | case Decl::CXXConstructor: return CXCursor_Constructor; |
| 2976 | case Decl::CXXDestructor: return CXCursor_Destructor; |
| 2977 | case Decl::CXXConversion: return CXCursor_ConversionFunction; |
| 2978 | case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl; |
Argyrios Kyrtzidis | c15707d | 2012-01-24 21:39:26 +0000 | [diff] [blame] | 2979 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 2980 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 2981 | case Decl::Typedef: return CXCursor_TypedefDecl; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2982 | case Decl::TypeAlias: return CXCursor_TypeAliasDecl; |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 2983 | case Decl::Var: return CXCursor_VarDecl; |
| 2984 | case Decl::Namespace: return CXCursor_Namespace; |
| 2985 | case Decl::NamespaceAlias: return CXCursor_NamespaceAlias; |
| 2986 | case Decl::TemplateTypeParm: return CXCursor_TemplateTypeParameter; |
| 2987 | case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter; |
| 2988 | case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter; |
| 2989 | case Decl::FunctionTemplate: return CXCursor_FunctionTemplate; |
| 2990 | case Decl::ClassTemplate: return CXCursor_ClassTemplate; |
Argyrios Kyrtzidis | 2dfdb94 | 2011-09-30 17:58:23 +0000 | [diff] [blame] | 2991 | case Decl::AccessSpec: return CXCursor_CXXAccessSpecifier; |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 2992 | case Decl::ClassTemplatePartialSpecialization: |
| 2993 | return CXCursor_ClassTemplatePartialSpecialization; |
| 2994 | case Decl::UsingDirective: return CXCursor_UsingDirective; |
Douglas Gregor | 8e5900c | 2012-04-30 23:41:16 +0000 | [diff] [blame] | 2995 | case Decl::TranslationUnit: return CXCursor_TranslationUnit; |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 2996 | |
| 2997 | case Decl::Using: |
| 2998 | case Decl::UnresolvedUsingValue: |
| 2999 | case Decl::UnresolvedUsingTypename: |
| 3000 | return CXCursor_UsingDeclaration; |
| 3001 | |
Douglas Gregor | 352697a | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3002 | case Decl::ObjCPropertyImpl: |
| 3003 | switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) { |
| 3004 | case ObjCPropertyImplDecl::Dynamic: |
| 3005 | return CXCursor_ObjCDynamicDecl; |
| 3006 | |
| 3007 | case ObjCPropertyImplDecl::Synthesize: |
| 3008 | return CXCursor_ObjCSynthesizeDecl; |
| 3009 | } |
Argyrios Kyrtzidis | 6a01012 | 2012-10-05 00:22:24 +0000 | [diff] [blame] | 3010 | |
| 3011 | case Decl::Import: |
| 3012 | return CXCursor_ModuleImportDecl; |
Douglas Gregor | 352697a | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3013 | |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3014 | default: |
Dmitri Gribenko | 06d8c60 | 2013-01-11 20:32:41 +0000 | [diff] [blame] | 3015 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3016 | switch (TD->getTagKind()) { |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3017 | case TTK_Interface: // fall through |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3018 | case TTK_Struct: return CXCursor_StructDecl; |
| 3019 | case TTK_Class: return CXCursor_ClassDecl; |
| 3020 | case TTK_Union: return CXCursor_UnionDecl; |
| 3021 | case TTK_Enum: return CXCursor_EnumDecl; |
| 3022 | } |
| 3023 | } |
| 3024 | } |
| 3025 | |
| 3026 | return CXCursor_UnexposedDecl; |
| 3027 | } |
| 3028 | |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3029 | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, |
Douglas Gregor | 3644d97 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3030 | bool IncludeUndefined, |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3031 | bool TargetTypeIsPointer = false) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3032 | typedef CodeCompletionResult Result; |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3033 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3034 | Results.EnterNewScope(); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 3035 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3036 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 3037 | MEnd = PP.macro_end(); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3038 | M != MEnd; ++M) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3039 | if (IncludeUndefined || M->first->hasMacroDefinition()) { |
| 3040 | if (MacroInfo *MI = M->second->getMacroInfo()) |
| 3041 | if (MI->isUsedForHeaderGuard()) |
| 3042 | continue; |
| 3043 | |
Douglas Gregor | 3644d97 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3044 | Results.AddResult(Result(M->first, |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3045 | getMacroUsagePriority(M->first->getName(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3046 | PP.getLangOpts(), |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3047 | TargetTypeIsPointer))); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3048 | } |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3049 | } |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 3050 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3051 | Results.ExitScope(); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 3052 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3055 | static void AddPrettyFunctionResults(const LangOptions &LangOpts, |
| 3056 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3057 | typedef CodeCompletionResult Result; |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3058 | |
| 3059 | Results.EnterNewScope(); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 3060 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3061 | Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant)); |
| 3062 | Results.AddResult(Result("__FUNCTION__", CCP_Constant)); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3063 | if (LangOpts.C99 || LangOpts.CPlusPlus11) |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3064 | Results.AddResult(Result("__func__", CCP_Constant)); |
| 3065 | Results.ExitScope(); |
| 3066 | } |
| 3067 | |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3068 | static void HandleCodeCompleteResults(Sema *S, |
| 3069 | CodeCompleteConsumer *CodeCompleter, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3070 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3071 | CodeCompletionResult *Results, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3072 | unsigned NumResults) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3073 | if (CodeCompleter) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3074 | CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3077 | static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S, |
| 3078 | Sema::ParserCompletionContext PCC) { |
| 3079 | switch (PCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3080 | case Sema::PCC_Namespace: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3081 | return CodeCompletionContext::CCC_TopLevel; |
| 3082 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3083 | case Sema::PCC_Class: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3084 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 3085 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3086 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3087 | return CodeCompletionContext::CCC_ObjCInterface; |
| 3088 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3089 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3090 | return CodeCompletionContext::CCC_ObjCImplementation; |
| 3091 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3092 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3093 | return CodeCompletionContext::CCC_ObjCIvarList; |
| 3094 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3095 | case Sema::PCC_Template: |
| 3096 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3097 | if (S.CurContext->isFileContext()) |
| 3098 | return CodeCompletionContext::CCC_TopLevel; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3099 | if (S.CurContext->isRecord()) |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3100 | return CodeCompletionContext::CCC_ClassStructUnion; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3101 | return CodeCompletionContext::CCC_Other; |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3102 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3103 | case Sema::PCC_RecoveryInFunction: |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3104 | return CodeCompletionContext::CCC_Recovery; |
Douglas Gregor | a5450a0 | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 3105 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3106 | case Sema::PCC_ForInit: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3107 | if (S.getLangOpts().CPlusPlus || S.getLangOpts().C99 || |
| 3108 | S.getLangOpts().ObjC1) |
Douglas Gregor | a5450a0 | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 3109 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
| 3110 | else |
| 3111 | return CodeCompletionContext::CCC_Expression; |
| 3112 | |
| 3113 | case Sema::PCC_Expression: |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3114 | case Sema::PCC_Condition: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3115 | return CodeCompletionContext::CCC_Expression; |
| 3116 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3117 | case Sema::PCC_Statement: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3118 | return CodeCompletionContext::CCC_Statement; |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3119 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3120 | case Sema::PCC_Type: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3121 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3122 | |
| 3123 | case Sema::PCC_ParenthesizedExpression: |
| 3124 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3125 | |
| 3126 | case Sema::PCC_LocalDeclarationSpecifiers: |
| 3127 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3128 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3129 | |
| 3130 | llvm_unreachable("Invalid ParserCompletionContext!"); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3133 | /// \brief If we're in a C++ virtual member function, add completion results |
| 3134 | /// that invoke the functions we override, since it's common to invoke the |
| 3135 | /// overridden function as well as adding new functionality. |
| 3136 | /// |
| 3137 | /// \param S The semantic analysis object for which we are generating results. |
| 3138 | /// |
| 3139 | /// \param InContext This context in which the nested-name-specifier preceding |
| 3140 | /// the code-completion point |
| 3141 | static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, |
| 3142 | ResultBuilder &Results) { |
| 3143 | // Look through blocks. |
| 3144 | DeclContext *CurContext = S.CurContext; |
| 3145 | while (isa<BlockDecl>(CurContext)) |
| 3146 | CurContext = CurContext->getParent(); |
| 3147 | |
| 3148 | |
| 3149 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext); |
| 3150 | if (!Method || !Method->isVirtual()) |
| 3151 | return; |
| 3152 | |
| 3153 | // We need to have names for all of the parameters, if we're going to |
| 3154 | // generate a forwarding call. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3155 | for (auto P : Method->params()) |
| 3156 | if (!P->getDeclName()) |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3157 | return; |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3158 | |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3159 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3160 | for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(), |
| 3161 | MEnd = Method->end_overridden_methods(); |
| 3162 | M != MEnd; ++M) { |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3163 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3164 | Results.getCodeCompletionTUInfo()); |
Dmitri Gribenko | 68a932d | 2013-02-14 13:53:30 +0000 | [diff] [blame] | 3165 | const CXXMethodDecl *Overridden = *M; |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3166 | if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl()) |
| 3167 | continue; |
| 3168 | |
| 3169 | // If we need a nested-name-specifier, add one now. |
| 3170 | if (!InContext) { |
| 3171 | NestedNameSpecifier *NNS |
| 3172 | = getRequiredQualification(S.Context, CurContext, |
| 3173 | Overridden->getDeclContext()); |
| 3174 | if (NNS) { |
| 3175 | std::string Str; |
| 3176 | llvm::raw_string_ostream OS(Str); |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3177 | NNS->print(OS, Policy); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3178 | Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str())); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3179 | } |
| 3180 | } else if (!InContext->Equals(Overridden->getDeclContext())) |
| 3181 | continue; |
| 3182 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3183 | Builder.AddTypedTextChunk(Results.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3184 | Overridden->getNameAsString())); |
| 3185 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3186 | bool FirstParam = true; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3187 | for (auto P : Method->params()) { |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3188 | if (FirstParam) |
| 3189 | FirstParam = false; |
| 3190 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3191 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3192 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3193 | Builder.AddPlaceholderChunk( |
| 3194 | Results.getAllocator().CopyString(P->getIdentifier()->getName())); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3195 | } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3196 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3197 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3198 | CCP_SuperCompletion, |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3199 | CXCursor_CXXMethod, |
| 3200 | CXAvailability_Available, |
| 3201 | Overridden)); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3202 | Results.Ignore(Overridden); |
| 3203 | } |
| 3204 | } |
| 3205 | |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3206 | void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc, |
| 3207 | ModuleIdPath Path) { |
| 3208 | typedef CodeCompletionResult Result; |
| 3209 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3210 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3211 | CodeCompletionContext::CCC_Other); |
| 3212 | Results.EnterNewScope(); |
| 3213 | |
| 3214 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3215 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3216 | typedef CodeCompletionResult Result; |
| 3217 | if (Path.empty()) { |
| 3218 | // Enumerate all top-level modules. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3219 | SmallVector<Module *, 8> Modules; |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3220 | PP.getHeaderSearchInfo().collectAllModules(Modules); |
| 3221 | for (unsigned I = 0, N = Modules.size(); I != N; ++I) { |
| 3222 | Builder.AddTypedTextChunk( |
| 3223 | Builder.getAllocator().CopyString(Modules[I]->Name)); |
| 3224 | Results.AddResult(Result(Builder.TakeString(), |
| 3225 | CCP_Declaration, |
Argyrios Kyrtzidis | fe038a3 | 2013-05-29 18:50:15 +0000 | [diff] [blame] | 3226 | CXCursor_ModuleImportDecl, |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3227 | Modules[I]->isAvailable() |
| 3228 | ? CXAvailability_Available |
| 3229 | : CXAvailability_NotAvailable)); |
| 3230 | } |
Daniel Jasper | 056ec12 | 2013-08-05 20:26:17 +0000 | [diff] [blame] | 3231 | } else if (getLangOpts().Modules) { |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3232 | // Load the named module. |
| 3233 | Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path, |
| 3234 | Module::AllVisible, |
| 3235 | /*IsInclusionDirective=*/false); |
| 3236 | // Enumerate submodules. |
| 3237 | if (Mod) { |
| 3238 | for (Module::submodule_iterator Sub = Mod->submodule_begin(), |
| 3239 | SubEnd = Mod->submodule_end(); |
| 3240 | Sub != SubEnd; ++Sub) { |
| 3241 | |
| 3242 | Builder.AddTypedTextChunk( |
| 3243 | Builder.getAllocator().CopyString((*Sub)->Name)); |
| 3244 | Results.AddResult(Result(Builder.TakeString(), |
| 3245 | CCP_Declaration, |
Argyrios Kyrtzidis | fe038a3 | 2013-05-29 18:50:15 +0000 | [diff] [blame] | 3246 | CXCursor_ModuleImportDecl, |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3247 | (*Sub)->isAvailable() |
| 3248 | ? CXAvailability_Available |
| 3249 | : CXAvailability_NotAvailable)); |
| 3250 | } |
| 3251 | } |
| 3252 | } |
| 3253 | Results.ExitScope(); |
| 3254 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 3255 | Results.data(),Results.size()); |
| 3256 | } |
| 3257 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3258 | void Sema::CodeCompleteOrdinaryName(Scope *S, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3259 | ParserCompletionContext CompletionContext) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3260 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3261 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3262 | mapCodeCompletionContext(*this, CompletionContext)); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3263 | Results.EnterNewScope(); |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 3264 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3265 | // Determine how to filter results, e.g., so that the names of |
| 3266 | // values (functions, enumerators, function templates, etc.) are |
| 3267 | // only allowed where we can have an expression. |
| 3268 | switch (CompletionContext) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3269 | case PCC_Namespace: |
| 3270 | case PCC_Class: |
| 3271 | case PCC_ObjCInterface: |
| 3272 | case PCC_ObjCImplementation: |
| 3273 | case PCC_ObjCInstanceVariableList: |
| 3274 | case PCC_Template: |
| 3275 | case PCC_MemberTemplate: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3276 | case PCC_Type: |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3277 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3278 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 3279 | break; |
| 3280 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3281 | case PCC_Statement: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3282 | case PCC_ParenthesizedExpression: |
Douglas Gregor | eb0d014 | 2010-08-24 23:58:17 +0000 | [diff] [blame] | 3283 | case PCC_Expression: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3284 | case PCC_ForInit: |
| 3285 | case PCC_Condition: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3286 | if (WantTypesInContext(CompletionContext, getLangOpts())) |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 3287 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 3288 | else |
| 3289 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3290 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3291 | if (getLangOpts().CPlusPlus) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3292 | MaybeAddOverrideCalls(*this, /*InContext=*/nullptr, Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3293 | break; |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 3294 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3295 | case PCC_RecoveryInFunction: |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 3296 | // Unfiltered |
| 3297 | break; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3300 | // If we are in a C++ non-static member function, check the qualifiers on |
| 3301 | // the member function to filter/prioritize the results list. |
| 3302 | if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext)) |
| 3303 | if (CurMethod->isInstance()) |
| 3304 | Results.setObjectTypeQualifiers( |
| 3305 | Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers())); |
| 3306 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 3307 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3308 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3309 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 2a7925c | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 3310 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3311 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); |
Douglas Gregor | 2a7925c | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 3312 | Results.ExitScope(); |
| 3313 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3314 | switch (CompletionContext) { |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3315 | case PCC_ParenthesizedExpression: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3316 | case PCC_Expression: |
| 3317 | case PCC_Statement: |
| 3318 | case PCC_RecoveryInFunction: |
| 3319 | if (S->getFnParent()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3320 | AddPrettyFunctionResults(PP.getLangOpts(), Results); |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3321 | break; |
| 3322 | |
| 3323 | case PCC_Namespace: |
| 3324 | case PCC_Class: |
| 3325 | case PCC_ObjCInterface: |
| 3326 | case PCC_ObjCImplementation: |
| 3327 | case PCC_ObjCInstanceVariableList: |
| 3328 | case PCC_Template: |
| 3329 | case PCC_MemberTemplate: |
| 3330 | case PCC_ForInit: |
| 3331 | case PCC_Condition: |
| 3332 | case PCC_Type: |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3333 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3334 | break; |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3337 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 3644d97 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3338 | AddMacroResults(PP, Results, false); |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3339 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 3340 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3341 | Results.data(),Results.size()); |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3344 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| 3345 | ParsedType Receiver, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 3346 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 3347 | bool AtArgumentExpression, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3348 | bool IsSuper, |
| 3349 | ResultBuilder &Results); |
| 3350 | |
| 3351 | void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, |
| 3352 | bool AllowNonIdentifiers, |
| 3353 | bool AllowNestedNameSpecifiers) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3354 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3355 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3356 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3357 | AllowNestedNameSpecifiers |
| 3358 | ? CodeCompletionContext::CCC_PotentiallyQualifiedName |
| 3359 | : CodeCompletionContext::CCC_Name); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3360 | Results.EnterNewScope(); |
| 3361 | |
| 3362 | // Type qualifiers can come after names. |
| 3363 | Results.AddResult(Result("const")); |
| 3364 | Results.AddResult(Result("volatile")); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3365 | if (getLangOpts().C99) |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3366 | Results.AddResult(Result("restrict")); |
| 3367 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3368 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3369 | if (AllowNonIdentifiers) { |
| 3370 | Results.AddResult(Result("operator")); |
| 3371 | } |
| 3372 | |
| 3373 | // Add nested-name-specifiers. |
| 3374 | if (AllowNestedNameSpecifiers) { |
| 3375 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3376 | Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3377 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3378 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
| 3379 | CodeCompleter->includeGlobals()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3380 | Results.setFilter(nullptr); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3381 | } |
| 3382 | } |
| 3383 | Results.ExitScope(); |
| 3384 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3385 | // If we're in a context where we might have an expression (rather than a |
| 3386 | // declaration), and what we've seen so far is an Objective-C type that could |
| 3387 | // be a receiver of a class message, this may be a class message send with |
| 3388 | // the initial opening bracket '[' missing. Add appropriate completions. |
| 3389 | if (AllowNonIdentifiers && !AllowNestedNameSpecifiers && |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3390 | DS.getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier && |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3391 | DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3392 | DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified && |
| 3393 | DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3394 | !DS.isTypeAltiVecVector() && |
| 3395 | S && |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3396 | (S->getFlags() & Scope::DeclScope) != 0 && |
| 3397 | (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope | |
| 3398 | Scope::FunctionPrototypeScope | |
| 3399 | Scope::AtCatchScope)) == 0) { |
| 3400 | ParsedType T = DS.getRepAsType(); |
| 3401 | if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType()) |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 3402 | AddClassMessageCompletions(*this, S, T, None, false, false, Results); |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
Douglas Gregor | 4497dd4 | 2010-08-24 04:59:56 +0000 | [diff] [blame] | 3405 | // Note that we intentionally suppress macro results here, since we do not |
| 3406 | // encourage using macros to produce the names of entities. |
| 3407 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3408 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3409 | Results.getCompletionContext(), |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3410 | Results.data(), Results.size()); |
| 3411 | } |
| 3412 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3413 | struct Sema::CodeCompleteExpressionData { |
| 3414 | CodeCompleteExpressionData(QualType PreferredType = QualType()) |
| 3415 | : PreferredType(PreferredType), IntegralConstantExpression(false), |
| 3416 | ObjCCollection(false) { } |
| 3417 | |
| 3418 | QualType PreferredType; |
| 3419 | bool IntegralConstantExpression; |
| 3420 | bool ObjCCollection; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3421 | SmallVector<Decl *, 4> IgnoreDecls; |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3422 | }; |
| 3423 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3424 | /// \brief Perform code-completion in an expression context when we know what |
| 3425 | /// type we're looking for. |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3426 | void Sema::CodeCompleteExpression(Scope *S, |
| 3427 | const CodeCompleteExpressionData &Data) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3428 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3429 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3430 | CodeCompletionContext::CCC_Expression); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3431 | if (Data.ObjCCollection) |
| 3432 | Results.setFilter(&ResultBuilder::IsObjCCollection); |
| 3433 | else if (Data.IntegralConstantExpression) |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3434 | Results.setFilter(&ResultBuilder::IsIntegralConstantValue); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3435 | else if (WantTypesInContext(PCC_Expression, getLangOpts())) |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3436 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 3437 | else |
| 3438 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3439 | |
| 3440 | if (!Data.PreferredType.isNull()) |
| 3441 | Results.setPreferredType(Data.PreferredType.getNonReferenceType()); |
| 3442 | |
| 3443 | // Ignore any declarations that we were told that we don't care about. |
| 3444 | for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I) |
| 3445 | Results.Ignore(Data.IgnoreDecls[I]); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3446 | |
| 3447 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3448 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3449 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3450 | |
| 3451 | Results.EnterNewScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3452 | AddOrdinaryNameResults(PCC_Expression, S, *this, Results); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3453 | Results.ExitScope(); |
| 3454 | |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3455 | bool PreferredTypeIsPointer = false; |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3456 | if (!Data.PreferredType.isNull()) |
| 3457 | PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType() |
| 3458 | || Data.PreferredType->isMemberPointerType() |
| 3459 | || Data.PreferredType->isBlockPointerType(); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3460 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3461 | if (S->getFnParent() && |
| 3462 | !Data.ObjCCollection && |
| 3463 | !Data.IntegralConstantExpression) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3464 | AddPrettyFunctionResults(PP.getLangOpts(), Results); |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3465 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3466 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 3644d97 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3467 | AddMacroResults(PP, Results, false, PreferredTypeIsPointer); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3468 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3469 | CodeCompletionContext(CodeCompletionContext::CCC_Expression, |
| 3470 | Data.PreferredType), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3471 | Results.data(),Results.size()); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3472 | } |
| 3473 | |
Douglas Gregor | ac5fd84 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 3474 | void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) { |
| 3475 | if (E.isInvalid()) |
| 3476 | CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3477 | else if (getLangOpts().ObjC1) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3478 | CodeCompleteObjCInstanceMessage(S, E.get(), None, false); |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 3479 | } |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3480 | |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3481 | /// \brief The set of properties that have already been added, referenced by |
| 3482 | /// property name. |
| 3483 | typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet; |
| 3484 | |
Douglas Gregor | b92a408 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3485 | /// \brief Retrieve the container definition, if any? |
| 3486 | static ObjCContainerDecl *getContainerDef(ObjCContainerDecl *Container) { |
| 3487 | if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 3488 | if (Interface->hasDefinition()) |
| 3489 | return Interface->getDefinition(); |
| 3490 | |
| 3491 | return Interface; |
| 3492 | } |
| 3493 | |
| 3494 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 3495 | if (Protocol->hasDefinition()) |
| 3496 | return Protocol->getDefinition(); |
| 3497 | |
| 3498 | return Protocol; |
| 3499 | } |
| 3500 | return Container; |
| 3501 | } |
| 3502 | |
| 3503 | static void AddObjCProperties(ObjCContainerDecl *Container, |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3504 | bool AllowCategories, |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3505 | bool AllowNullaryMethods, |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3506 | DeclContext *CurContext, |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3507 | AddedPropertiesSet &AddedProperties, |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3508 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3509 | typedef CodeCompletionResult Result; |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3510 | |
Douglas Gregor | b92a408 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3511 | // Retrieve the definition. |
| 3512 | Container = getContainerDef(Container); |
| 3513 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3514 | // Add properties in this container. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3515 | for (const auto *P : Container->properties()) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3516 | if (AddedProperties.insert(P->getIdentifier()).second) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3517 | Results.MaybeAddResult(Result(P, Results.getBasePriority(P), nullptr), |
Douglas Gregor | d1f09b4 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 3518 | CurContext); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3519 | |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3520 | // Add nullary methods |
| 3521 | if (AllowNullaryMethods) { |
| 3522 | ASTContext &Context = Container->getASTContext(); |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3523 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3524 | for (auto *M : Container->methods()) { |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3525 | if (M->getSelector().isUnarySelector()) |
| 3526 | if (IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3527 | if (AddedProperties.insert(Name).second) { |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3528 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3529 | Results.getCodeCompletionTUInfo()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3530 | AddResultTypeChunk(Context, Policy, M, Builder); |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3531 | Builder.AddTypedTextChunk( |
| 3532 | Results.getAllocator().CopyString(Name->getName())); |
| 3533 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3534 | Results.MaybeAddResult(Result(Builder.TakeString(), M, |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3535 | CCP_MemberDeclaration + CCD_MethodAsProperty), |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3536 | CurContext); |
| 3537 | } |
| 3538 | } |
| 3539 | } |
| 3540 | |
| 3541 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3542 | // Add properties in referenced protocols. |
| 3543 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3544 | for (auto *P : Protocol->protocols()) |
| 3545 | AddObjCProperties(P, AllowCategories, AllowNullaryMethods, CurContext, |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3546 | AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3547 | } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){ |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3548 | if (AllowCategories) { |
| 3549 | // Look through categories. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3550 | for (auto *Cat : IFace->known_categories()) |
| 3551 | AddObjCProperties(Cat, AllowCategories, AllowNullaryMethods, CurContext, |
| 3552 | AddedProperties, Results); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3553 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3554 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3555 | // Look through protocols. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3556 | for (auto *I : IFace->all_referenced_protocols()) |
| 3557 | AddObjCProperties(I, AllowCategories, AllowNullaryMethods, CurContext, |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3558 | AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3559 | |
| 3560 | // Look in the superclass. |
| 3561 | if (IFace->getSuperClass()) |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3562 | AddObjCProperties(IFace->getSuperClass(), AllowCategories, |
| 3563 | AllowNullaryMethods, CurContext, |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3564 | AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3565 | } else if (const ObjCCategoryDecl *Category |
| 3566 | = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 3567 | // Look through protocols. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3568 | for (auto *P : Category->protocols()) |
| 3569 | AddObjCProperties(P, AllowCategories, AllowNullaryMethods, CurContext, |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3570 | AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3571 | } |
| 3572 | } |
| 3573 | |
Douglas Gregor | f5cd27d | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 3574 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base, |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3575 | SourceLocation OpLoc, |
| 3576 | bool IsArrow) { |
Douglas Gregor | f5cd27d | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 3577 | if (!Base || !CodeCompleter) |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3578 | return; |
| 3579 | |
Douglas Gregor | f5cd27d | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 3580 | ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow); |
| 3581 | if (ConvertedBase.isInvalid()) |
| 3582 | return; |
| 3583 | Base = ConvertedBase.get(); |
| 3584 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3585 | typedef CodeCompletionResult Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3586 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3587 | QualType BaseType = Base->getType(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3588 | |
| 3589 | if (IsArrow) { |
| 3590 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 3591 | BaseType = Ptr->getPointeeType(); |
| 3592 | else if (BaseType->isObjCObjectPointerType()) |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3593 | /*Do nothing*/ ; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3594 | else |
| 3595 | return; |
| 3596 | } |
| 3597 | |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3598 | enum CodeCompletionContext::Kind contextKind; |
| 3599 | |
| 3600 | if (IsArrow) { |
| 3601 | contextKind = CodeCompletionContext::CCC_ArrowMemberAccess; |
| 3602 | } |
| 3603 | else { |
| 3604 | if (BaseType->isObjCObjectPointerType() || |
| 3605 | BaseType->isObjCObjectOrInterfaceType()) { |
| 3606 | contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess; |
| 3607 | } |
| 3608 | else { |
| 3609 | contextKind = CodeCompletionContext::CCC_DotMemberAccess; |
| 3610 | } |
| 3611 | } |
| 3612 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3613 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3614 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3615 | CodeCompletionContext(contextKind, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3616 | BaseType), |
| 3617 | &ResultBuilder::IsMember); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3618 | Results.EnterNewScope(); |
| 3619 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3620 | // Indicate that we are performing a member access, and the cv-qualifiers |
| 3621 | // for the base object type. |
| 3622 | Results.setObjectTypeQualifiers(BaseType.getQualifiers()); |
| 3623 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3624 | // Access to a C/C++ class, struct, or union. |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 3625 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3626 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3627 | LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer, |
| 3628 | CodeCompleter->includeGlobals()); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3629 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3630 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3631 | if (!Results.empty()) { |
| 3632 | // The "template" keyword can follow "->" or "." in the grammar. |
| 3633 | // However, we only want to suggest the template keyword if something |
| 3634 | // is dependent. |
| 3635 | bool IsDependent = BaseType->isDependentType(); |
| 3636 | if (!IsDependent) { |
| 3637 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) |
Ted Kremenek | f0d5861 | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 3638 | if (DeclContext *Ctx = DepScope->getEntity()) { |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3639 | IsDependent = Ctx->isDependentContext(); |
| 3640 | break; |
| 3641 | } |
| 3642 | } |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3643 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3644 | if (IsDependent) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3645 | Results.AddResult(Result("template")); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3646 | } |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3647 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3648 | } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) { |
| 3649 | // Objective-C property reference. |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3650 | AddedPropertiesSet AddedProperties; |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3651 | |
| 3652 | // Add property results based on our interface. |
| 3653 | const ObjCObjectPointerType *ObjCPtr |
| 3654 | = BaseType->getAsObjCInterfacePointerType(); |
| 3655 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!"); |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3656 | AddObjCProperties(ObjCPtr->getInterfaceDecl(), true, |
| 3657 | /*AllowNullaryMethods=*/true, CurContext, |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3658 | AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3659 | |
| 3660 | // Add properties from the protocols in a qualified interface. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3661 | for (auto *I : ObjCPtr->quals()) |
| 3662 | AddObjCProperties(I, true, /*AllowNullaryMethods=*/true, CurContext, |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3663 | AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3664 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3665 | (!IsArrow && BaseType->isObjCObjectType())) { |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3666 | // Objective-C instance variable access. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3667 | ObjCInterfaceDecl *Class = nullptr; |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3668 | if (const ObjCObjectPointerType *ObjCPtr |
| 3669 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 3670 | Class = ObjCPtr->getInterfaceDecl(); |
| 3671 | else |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3672 | Class = BaseType->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3673 | |
| 3674 | // Add all ivars from this class and its superclasses. |
Douglas Gregor | 80f4f4c | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 3675 | if (Class) { |
| 3676 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3677 | Results.setFilter(&ResultBuilder::IsObjCIvar); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3678 | LookupVisibleDecls(Class, LookupMemberName, Consumer, |
| 3679 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3680 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3681 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3682 | |
| 3683 | // FIXME: How do we cope with isa? |
| 3684 | |
| 3685 | Results.ExitScope(); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3686 | |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3687 | // Hand off the results found for code completion. |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3688 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3689 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3690 | Results.data(),Results.size()); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3691 | } |
| 3692 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3693 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { |
| 3694 | if (!CodeCompleter) |
| 3695 | return; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3696 | |
| 3697 | ResultBuilder::LookupFilter Filter = nullptr; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3698 | enum CodeCompletionContext::Kind ContextKind |
| 3699 | = CodeCompletionContext::CCC_Other; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3700 | switch ((DeclSpec::TST)TagSpec) { |
| 3701 | case DeclSpec::TST_enum: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3702 | Filter = &ResultBuilder::IsEnum; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3703 | ContextKind = CodeCompletionContext::CCC_EnumTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3704 | break; |
| 3705 | |
| 3706 | case DeclSpec::TST_union: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3707 | Filter = &ResultBuilder::IsUnion; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3708 | ContextKind = CodeCompletionContext::CCC_UnionTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3709 | break; |
| 3710 | |
| 3711 | case DeclSpec::TST_struct: |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3712 | case DeclSpec::TST_class: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3713 | case DeclSpec::TST_interface: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3714 | Filter = &ResultBuilder::IsClassOrStruct; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3715 | ContextKind = CodeCompletionContext::CCC_ClassOrStructTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3716 | break; |
| 3717 | |
| 3718 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3719 | llvm_unreachable("Unknown type specifier kind in CodeCompleteTag"); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3720 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3721 | |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3722 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3723 | CodeCompleter->getCodeCompletionTUInfo(), ContextKind); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3724 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 3725 | |
| 3726 | // First pass: look for tags. |
| 3727 | Results.setFilter(Filter); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3728 | LookupVisibleDecls(S, LookupTagName, Consumer, |
| 3729 | CodeCompleter->includeGlobals()); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 3730 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3731 | if (CodeCompleter->includeGlobals()) { |
| 3732 | // Second pass: look for nested name specifiers. |
| 3733 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
| 3734 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer); |
| 3735 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3736 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3737 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3738 | Results.data(),Results.size()); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3741 | void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3742 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3743 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3744 | CodeCompletionContext::CCC_TypeQualifiers); |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3745 | Results.EnterNewScope(); |
| 3746 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const)) |
| 3747 | Results.AddResult("const"); |
| 3748 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile)) |
| 3749 | Results.AddResult("volatile"); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3750 | if (getLangOpts().C99 && |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3751 | !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict)) |
| 3752 | Results.AddResult("restrict"); |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3753 | if (getLangOpts().C11 && |
| 3754 | !(DS.getTypeQualifiers() & DeclSpec::TQ_atomic)) |
| 3755 | Results.AddResult("_Atomic"); |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3756 | Results.ExitScope(); |
| 3757 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3758 | Results.getCompletionContext(), |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3759 | Results.data(), Results.size()); |
| 3760 | } |
| 3761 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3762 | void Sema::CodeCompleteCase(Scope *S) { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3763 | if (getCurFunction()->SwitchStack.empty() || !CodeCompleter) |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3764 | return; |
John McCall | a8e0cd8 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 3765 | |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3766 | SwitchStmt *Switch = getCurFunction()->SwitchStack.back(); |
John McCall | a8e0cd8 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 3767 | QualType type = Switch->getCond()->IgnoreImplicit()->getType(); |
| 3768 | if (!type->isEnumeralType()) { |
| 3769 | CodeCompleteExpressionData Data(type); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3770 | Data.IntegralConstantExpression = true; |
| 3771 | CodeCompleteExpression(S, Data); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3772 | return; |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3773 | } |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3774 | |
| 3775 | // Code-complete the cases of a switch statement over an enumeration type |
| 3776 | // by providing the list of |
John McCall | a8e0cd8 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 3777 | EnumDecl *Enum = type->castAs<EnumType>()->getDecl(); |
Douglas Gregor | b92a408 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3778 | if (EnumDecl *Def = Enum->getDefinition()) |
| 3779 | Enum = Def; |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3780 | |
| 3781 | // Determine which enumerators we have already seen in the switch statement. |
| 3782 | // FIXME: Ideally, we would also be able to look *past* the code-completion |
| 3783 | // token, in case we are code-completing in the middle of the switch and not |
| 3784 | // at the end. However, we aren't able to do so at the moment. |
| 3785 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3786 | NestedNameSpecifier *Qualifier = nullptr; |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3787 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; |
| 3788 | SC = SC->getNextSwitchCase()) { |
| 3789 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); |
| 3790 | if (!Case) |
| 3791 | continue; |
| 3792 | |
| 3793 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); |
| 3794 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal)) |
| 3795 | if (EnumConstantDecl *Enumerator |
| 3796 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 3797 | // We look into the AST of the case statement to determine which |
| 3798 | // enumerator was named. Alternatively, we could compute the value of |
| 3799 | // the integral constant expression, then compare it against the |
| 3800 | // values of each enumerator. However, value-based approach would not |
| 3801 | // work as well with C++ templates where enumerators declared within a |
| 3802 | // template are type- and value-dependent. |
| 3803 | EnumeratorsSeen.insert(Enumerator); |
| 3804 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3805 | // If this is a qualified-id, keep track of the nested-name-specifier |
| 3806 | // so that we can reproduce it as part of code completion, e.g., |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3807 | // |
| 3808 | // switch (TagD.getKind()) { |
| 3809 | // case TagDecl::TK_enum: |
| 3810 | // break; |
| 3811 | // case XXX |
| 3812 | // |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3813 | // At the XXX, our completions are TagDecl::TK_union, |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3814 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, |
| 3815 | // TK_struct, and TK_class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3816 | Qualifier = DRE->getQualifier(); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3817 | } |
| 3818 | } |
| 3819 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3820 | if (getLangOpts().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3821 | // If there are no prior enumerators in C++, check whether we have to |
| 3822 | // qualify the names of the enumerators that we suggest, because they |
| 3823 | // may not be visible in this scope. |
Douglas Gregor | b223d8c | 2012-02-01 05:02:47 +0000 | [diff] [blame] | 3824 | Qualifier = getRequiredQualification(Context, CurContext, Enum); |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3827 | // Add any enumerators that have not yet been mentioned. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3828 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3829 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3830 | CodeCompletionContext::CCC_Expression); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3831 | Results.EnterNewScope(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3832 | for (auto *E : Enum->enumerators()) { |
| 3833 | if (EnumeratorsSeen.count(E)) |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3834 | continue; |
| 3835 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3836 | CodeCompletionResult R(E, CCP_EnumInCase, Qualifier); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3837 | Results.AddResult(R, CurContext, nullptr, false); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3838 | } |
| 3839 | Results.ExitScope(); |
Douglas Gregor | 2f880e4 | 2010-04-06 20:02:15 +0000 | [diff] [blame] | 3840 | |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3841 | //We need to make sure we're setting the right context, |
| 3842 | //so only say we include macros if the code completer says we do |
| 3843 | enum CodeCompletionContext::Kind kind = CodeCompletionContext::CCC_Other; |
| 3844 | if (CodeCompleter->includeMacros()) { |
Douglas Gregor | 3644d97 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3845 | AddMacroResults(PP, Results, false); |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3846 | kind = CodeCompletionContext::CCC_OtherWithMacros; |
| 3847 | } |
| 3848 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3849 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3850 | kind, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3851 | Results.data(),Results.size()); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 3854 | static bool anyNullArguments(ArrayRef<Expr *> Args) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3855 | if (Args.size() && !Args.data()) |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3856 | return true; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3857 | |
| 3858 | for (unsigned I = 0; I != Args.size(); ++I) |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3859 | if (!Args[I]) |
| 3860 | return true; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3861 | |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3862 | return false; |
| 3863 | } |
| 3864 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3865 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; |
| 3866 | |
| 3867 | static void mergeCandidatesWithResults(Sema &SemaRef, |
| 3868 | SmallVectorImpl<ResultCandidate> &Results, |
| 3869 | OverloadCandidateSet &CandidateSet, |
| 3870 | SourceLocation Loc) { |
| 3871 | if (!CandidateSet.empty()) { |
| 3872 | // Sort the overload candidate set by placing the best overloads first. |
| 3873 | std::stable_sort( |
| 3874 | CandidateSet.begin(), CandidateSet.end(), |
| 3875 | [&](const OverloadCandidate &X, const OverloadCandidate &Y) { |
| 3876 | return isBetterOverloadCandidate(SemaRef, X, Y, Loc); |
| 3877 | }); |
| 3878 | |
| 3879 | // Add the remaining viable overload candidates as code-completion results. |
| 3880 | for (auto &Candidate : CandidateSet) |
| 3881 | if (Candidate.Viable) |
| 3882 | Results.push_back(ResultCandidate(Candidate.Function)); |
| 3883 | } |
| 3884 | } |
| 3885 | |
| 3886 | /// \brief Get the type of the Nth parameter from a given set of overload |
| 3887 | /// candidates. |
| 3888 | static QualType getParamType(Sema &SemaRef, |
| 3889 | ArrayRef<ResultCandidate> Candidates, |
| 3890 | unsigned N) { |
| 3891 | |
| 3892 | // Given the overloads 'Candidates' for a function call matching all arguments |
| 3893 | // up to N, return the type of the Nth parameter if it is the same for all |
| 3894 | // overload candidates. |
| 3895 | QualType ParamType; |
| 3896 | for (auto &Candidate : Candidates) { |
| 3897 | if (auto FType = Candidate.getFunctionType()) |
| 3898 | if (auto Proto = dyn_cast<FunctionProtoType>(FType)) |
| 3899 | if (N < Proto->getNumParams()) { |
| 3900 | if (ParamType.isNull()) |
| 3901 | ParamType = Proto->getParamType(N); |
| 3902 | else if (!SemaRef.Context.hasSameUnqualifiedType( |
| 3903 | ParamType.getNonReferenceType(), |
| 3904 | Proto->getParamType(N).getNonReferenceType())) |
| 3905 | // Otherwise return a default-constructed QualType. |
| 3906 | return QualType(); |
| 3907 | } |
| 3908 | } |
| 3909 | |
| 3910 | return ParamType; |
| 3911 | } |
| 3912 | |
| 3913 | static void CodeCompleteOverloadResults(Sema &SemaRef, Scope *S, |
| 3914 | MutableArrayRef<ResultCandidate> Candidates, |
| 3915 | unsigned CurrentArg, |
| 3916 | bool CompleteExpressionWithCurrentArg = true) { |
| 3917 | QualType ParamType; |
| 3918 | if (CompleteExpressionWithCurrentArg) |
| 3919 | ParamType = getParamType(SemaRef, Candidates, CurrentArg); |
| 3920 | |
| 3921 | if (ParamType.isNull()) |
| 3922 | SemaRef.CodeCompleteOrdinaryName(S, Sema::PCC_Expression); |
| 3923 | else |
| 3924 | SemaRef.CodeCompleteExpression(S, ParamType); |
| 3925 | |
| 3926 | if (!Candidates.empty()) |
| 3927 | SemaRef.CodeCompleter->ProcessOverloadCandidates(SemaRef, CurrentArg, |
| 3928 | Candidates.data(), |
| 3929 | Candidates.size()); |
| 3930 | } |
| 3931 | |
| 3932 | void Sema::CodeCompleteCall(Scope *S, Expr *Fn, ArrayRef<Expr *> Args) { |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3933 | if (!CodeCompleter) |
| 3934 | return; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3935 | |
| 3936 | // When we're code-completing for a call, we fall back to ordinary |
| 3937 | // name code-completion whenever we can't produce specific |
| 3938 | // results. We may want to revisit this strategy in the future, |
| 3939 | // e.g., by merging the two kinds of results. |
| 3940 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3941 | // FIXME: Provide support for variadic template functions. |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3942 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3943 | // Ignore type-dependent call expressions entirely. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3944 | if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args) || |
| 3945 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3946 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3947 | return; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3948 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3949 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3950 | // Build an overload candidate set based on the functions we find. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3951 | SourceLocation Loc = Fn->getExprLoc(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3952 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3953 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3954 | SmallVector<ResultCandidate, 8> Results; |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3955 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3956 | Expr *NakedFn = Fn->IgnoreParenCasts(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3957 | if (auto ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3958 | AddOverloadedCallCandidates(ULE, Args, CandidateSet, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3959 | /*PartialOverloading=*/true); |
| 3960 | else if (auto UME = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 3961 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
| 3962 | if (UME->hasExplicitTemplateArgs()) { |
| 3963 | UME->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 3964 | TemplateArgs = &TemplateArgsBuffer; |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3965 | } |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3966 | SmallVector<Expr *, 12> ArgExprs(1, UME->getBase()); |
| 3967 | ArgExprs.append(Args.begin(), Args.end()); |
| 3968 | UnresolvedSet<8> Decls; |
| 3969 | Decls.append(UME->decls_begin(), UME->decls_end()); |
| 3970 | AddFunctionCandidates(Decls, ArgExprs, CandidateSet, TemplateArgs, |
| 3971 | /*SuppressUsedConversions=*/false, |
| 3972 | /*PartialOverloading=*/true); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3973 | } else { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3974 | FunctionDecl *FD = nullptr; |
| 3975 | if (auto MCE = dyn_cast<MemberExpr>(NakedFn)) |
| 3976 | FD = dyn_cast<FunctionDecl>(MCE->getMemberDecl()); |
| 3977 | else if (auto DRE = dyn_cast<DeclRefExpr>(NakedFn)) |
| 3978 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 3979 | if (FD) { // We check whether it's a resolved function declaration. |
| 3980 | if (!getLangOpts().CPlusPlus || |
| 3981 | !FD->getType()->getAs<FunctionProtoType>()) |
| 3982 | Results.push_back(ResultCandidate(FD)); |
| 3983 | else |
| 3984 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, FD->getAccess()), |
| 3985 | Args, CandidateSet, |
| 3986 | /*SuppressUsedConversions=*/false, |
| 3987 | /*PartialOverloading=*/true); |
| 3988 | |
| 3989 | } else if (auto DC = NakedFn->getType()->getAsCXXRecordDecl()) { |
| 3990 | // If expression's type is CXXRecordDecl, it may overload the function |
| 3991 | // call operator, so we check if it does and add them as candidates. |
| 3992 | // A complete type is needed to lookup for member function call operators. |
| 3993 | if (!RequireCompleteType(Loc, NakedFn->getType(), 0)) { |
| 3994 | DeclarationName OpName = Context.DeclarationNames |
| 3995 | .getCXXOperatorName(OO_Call); |
| 3996 | LookupResult R(*this, OpName, Loc, LookupOrdinaryName); |
| 3997 | LookupQualifiedName(R, DC); |
| 3998 | R.suppressDiagnostics(); |
| 3999 | SmallVector<Expr *, 12> ArgExprs(1, NakedFn); |
| 4000 | ArgExprs.append(Args.begin(), Args.end()); |
| 4001 | AddFunctionCandidates(R.asUnresolvedSet(), ArgExprs, CandidateSet, |
| 4002 | /*ExplicitArgs=*/nullptr, |
| 4003 | /*SuppressUsedConversions=*/false, |
| 4004 | /*PartialOverloading=*/true); |
| 4005 | } |
| 4006 | } else { |
| 4007 | // Lastly we check whether expression's type is function pointer or |
| 4008 | // function. |
| 4009 | QualType T = NakedFn->getType(); |
| 4010 | if (!T->getPointeeType().isNull()) |
| 4011 | T = T->getPointeeType(); |
| 4012 | |
| 4013 | if (auto FP = T->getAs<FunctionProtoType>()) { |
| 4014 | if (!TooManyArguments(FP->getNumParams(), Args.size(), |
| 4015 | /*PartialOverloading=*/true) || |
| 4016 | FP->isVariadic()) |
| 4017 | Results.push_back(ResultCandidate(FP)); |
| 4018 | } else if (auto FT = T->getAs<FunctionType>()) |
| 4019 | // No prototype and declaration, it may be a K & R style function. |
| 4020 | Results.push_back(ResultCandidate(FT)); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4021 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4022 | } |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4023 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4024 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); |
| 4025 | CodeCompleteOverloadResults(*this, S, Results, Args.size(), |
| 4026 | !CandidateSet.empty()); |
| 4027 | } |
| 4028 | |
| 4029 | void Sema::CodeCompleteConstructor(Scope *S, QualType Type, SourceLocation Loc, |
| 4030 | ArrayRef<Expr *> Args) { |
| 4031 | if (!CodeCompleter) |
| 4032 | return; |
| 4033 | |
| 4034 | // A complete type is needed to lookup for constructors. |
| 4035 | if (RequireCompleteType(Loc, Type, 0)) |
| 4036 | return; |
| 4037 | |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 4038 | CXXRecordDecl *RD = Type->getAsCXXRecordDecl(); |
| 4039 | if (!RD) { |
| 4040 | CodeCompleteExpression(S, Type); |
| 4041 | return; |
| 4042 | } |
| 4043 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4044 | // FIXME: Provide support for member initializers. |
| 4045 | // FIXME: Provide support for variadic template constructors. |
| 4046 | |
| 4047 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
| 4048 | |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 4049 | for (auto C : LookupConstructors(RD)) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4050 | if (auto FD = dyn_cast<FunctionDecl>(C)) { |
| 4051 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, C->getAccess()), |
| 4052 | Args, CandidateSet, |
| 4053 | /*SuppressUsedConversions=*/false, |
| 4054 | /*PartialOverloading=*/true); |
| 4055 | } else if (auto FTD = dyn_cast<FunctionTemplateDecl>(C)) { |
| 4056 | AddTemplateOverloadCandidate(FTD, |
| 4057 | DeclAccessPair::make(FTD, C->getAccess()), |
| 4058 | /*ExplicitTemplateArgs=*/nullptr, |
| 4059 | Args, CandidateSet, |
| 4060 | /*SuppressUsedConversions=*/false, |
| 4061 | /*PartialOverloading=*/true); |
| 4062 | } |
| 4063 | } |
| 4064 | |
| 4065 | SmallVector<ResultCandidate, 8> Results; |
| 4066 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); |
| 4067 | CodeCompleteOverloadResults(*this, S, Results, Args.size()); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4068 | } |
| 4069 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4070 | void Sema::CodeCompleteInitializer(Scope *S, Decl *D) { |
| 4071 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4072 | if (!VD) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4073 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4074 | return; |
| 4075 | } |
| 4076 | |
| 4077 | CodeCompleteExpression(S, VD->getType()); |
| 4078 | } |
| 4079 | |
| 4080 | void Sema::CodeCompleteReturn(Scope *S) { |
| 4081 | QualType ResultType; |
| 4082 | if (isa<BlockDecl>(CurContext)) { |
| 4083 | if (BlockScopeInfo *BSI = getCurBlock()) |
| 4084 | ResultType = BSI->ReturnType; |
| 4085 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4086 | ResultType = Function->getReturnType(); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4087 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4088 | ResultType = Method->getReturnType(); |
| 4089 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4090 | if (ResultType.isNull()) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4091 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4092 | else |
| 4093 | CodeCompleteExpression(S, ResultType); |
| 4094 | } |
| 4095 | |
Douglas Gregor | d2d8be6 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4096 | void Sema::CodeCompleteAfterIf(Scope *S) { |
Douglas Gregor | d2d8be6 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4097 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4098 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | d2d8be6 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4099 | mapCodeCompletionContext(*this, PCC_Statement)); |
| 4100 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 4101 | Results.EnterNewScope(); |
| 4102 | |
| 4103 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4104 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4105 | CodeCompleter->includeGlobals()); |
| 4106 | |
| 4107 | AddOrdinaryNameResults(PCC_Statement, S, *this, Results); |
| 4108 | |
| 4109 | // "else" block |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4110 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4111 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | d2d8be6 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4112 | Builder.AddTypedTextChunk("else"); |
Douglas Gregor | f11641a | 2012-02-16 17:49:04 +0000 | [diff] [blame] | 4113 | if (Results.includeCodePatterns()) { |
| 4114 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4115 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4116 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4117 | Builder.AddPlaceholderChunk("statements"); |
| 4118 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4119 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4120 | } |
Douglas Gregor | d2d8be6 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4121 | Results.AddResult(Builder.TakeString()); |
| 4122 | |
| 4123 | // "else if" block |
| 4124 | Builder.AddTypedTextChunk("else"); |
| 4125 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4126 | Builder.AddTextChunk("if"); |
| 4127 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4128 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4129 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | d2d8be6 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4130 | Builder.AddPlaceholderChunk("condition"); |
| 4131 | else |
| 4132 | Builder.AddPlaceholderChunk("expression"); |
| 4133 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | f11641a | 2012-02-16 17:49:04 +0000 | [diff] [blame] | 4134 | if (Results.includeCodePatterns()) { |
| 4135 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4136 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4137 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4138 | Builder.AddPlaceholderChunk("statements"); |
| 4139 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4140 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4141 | } |
Douglas Gregor | d2d8be6 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4142 | Results.AddResult(Builder.TakeString()); |
| 4143 | |
| 4144 | Results.ExitScope(); |
| 4145 | |
| 4146 | if (S->getFnParent()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4147 | AddPrettyFunctionResults(PP.getLangOpts(), Results); |
Douglas Gregor | d2d8be6 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4148 | |
| 4149 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 3644d97 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 4150 | AddMacroResults(PP, Results, false); |
Douglas Gregor | d2d8be6 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4151 | |
| 4152 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4153 | Results.data(),Results.size()); |
| 4154 | } |
| 4155 | |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 4156 | void Sema::CodeCompleteAssignmentRHS(Scope *S, Expr *LHS) { |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4157 | if (LHS) |
| 4158 | CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType()); |
| 4159 | else |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4160 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4163 | void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4164 | bool EnteringContext) { |
| 4165 | if (!SS.getScopeRep() || !CodeCompleter) |
| 4166 | return; |
| 4167 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4168 | DeclContext *Ctx = computeDeclContext(SS, EnteringContext); |
| 4169 | if (!Ctx) |
| 4170 | return; |
Douglas Gregor | d1cd31a | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 4171 | |
| 4172 | // Try to instantiate any non-dependent declaration contexts before |
| 4173 | // we look in them. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4174 | if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx)) |
Douglas Gregor | d1cd31a | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 4175 | return; |
| 4176 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4177 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4178 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4179 | CodeCompletionContext::CCC_Name); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4180 | Results.EnterNewScope(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4181 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4182 | // The "template" keyword can follow "::" in the grammar, but only |
| 4183 | // put it into the grammar if the nested-name-specifier is dependent. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4184 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4185 | if (!Results.empty() && NNS->isDependent()) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4186 | Results.AddResult("template"); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4187 | |
| 4188 | // Add calls to overridden virtual functions, if there are any. |
| 4189 | // |
| 4190 | // FIXME: This isn't wonderful, because we don't know whether we're actually |
| 4191 | // in a context that permits expressions. This is a general issue with |
| 4192 | // qualified-id completions. |
| 4193 | if (!EnteringContext) |
| 4194 | MaybeAddOverrideCalls(*this, Ctx, Results); |
| 4195 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4196 | |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4197 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4198 | LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer); |
| 4199 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4200 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 4201 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4202 | Results.data(),Results.size()); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4203 | } |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4204 | |
| 4205 | void Sema::CodeCompleteUsing(Scope *S) { |
| 4206 | if (!CodeCompleter) |
| 4207 | return; |
| 4208 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4209 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4210 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4211 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
| 4212 | &ResultBuilder::IsNestedNameSpecifier); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4213 | Results.EnterNewScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4214 | |
| 4215 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 4216 | if (!S->isClassScope()) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4217 | Results.AddResult(CodeCompletionResult("namespace")); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4218 | |
| 4219 | // After "using", we can see anything that would start a |
| 4220 | // nested-name-specifier. |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4221 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4222 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4223 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4224 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4225 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4226 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4227 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4228 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4229 | } |
| 4230 | |
| 4231 | void Sema::CodeCompleteUsingDirective(Scope *S) { |
| 4232 | if (!CodeCompleter) |
| 4233 | return; |
| 4234 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4235 | // After "using namespace", we expect to see a namespace name or namespace |
| 4236 | // alias. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4237 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4238 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4239 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4240 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4241 | Results.EnterNewScope(); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4242 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4243 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4244 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4245 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4246 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4247 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4248 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
| 4251 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { |
| 4252 | if (!CodeCompleter) |
| 4253 | return; |
| 4254 | |
Ted Kremenek | f0d5861 | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 4255 | DeclContext *Ctx = S->getEntity(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4256 | if (!S->getParent()) |
| 4257 | Ctx = Context.getTranslationUnitDecl(); |
| 4258 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4259 | bool SuppressedGlobalResults |
| 4260 | = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx); |
| 4261 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4262 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4263 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4264 | SuppressedGlobalResults |
| 4265 | ? CodeCompletionContext::CCC_Namespace |
| 4266 | : CodeCompletionContext::CCC_Other, |
| 4267 | &ResultBuilder::IsNamespace); |
| 4268 | |
| 4269 | if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4270 | // We only want to see those namespaces that have already been defined |
| 4271 | // within this scope, because its likely that the user is creating an |
| 4272 | // extended namespace declaration. Keep track of the most recent |
| 4273 | // definition of each namespace. |
| 4274 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
| 4275 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
| 4276 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); |
| 4277 | NS != NSEnd; ++NS) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4278 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4279 | |
| 4280 | // Add the most recent definition (or extended definition) of each |
| 4281 | // namespace to the list of results. |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4282 | Results.EnterNewScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4283 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 4284 | NS = OrigToLatest.begin(), |
| 4285 | NSEnd = OrigToLatest.end(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4286 | NS != NSEnd; ++NS) |
Douglas Gregor | d1f09b4 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 4287 | Results.AddResult(CodeCompletionResult( |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4288 | NS->second, Results.getBasePriority(NS->second), |
| 4289 | nullptr), |
| 4290 | CurContext, nullptr, false); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4291 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4294 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4295 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4296 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { |
| 4300 | if (!CodeCompleter) |
| 4301 | return; |
| 4302 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4303 | // After "namespace", we expect to see a namespace or alias. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4304 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4305 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4306 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4307 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4308 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4309 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4310 | CodeCompleter->includeGlobals()); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4311 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4312 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4313 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 4316 | void Sema::CodeCompleteOperatorName(Scope *S) { |
| 4317 | if (!CodeCompleter) |
| 4318 | return; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4319 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4320 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4321 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4322 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4323 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4324 | &ResultBuilder::IsType); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4325 | Results.EnterNewScope(); |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 4326 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4327 | // Add the names of overloadable operators. |
| 4328 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 4329 | if (std::strcmp(Spelling, "?")) \ |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4330 | Results.AddResult(Result(Spelling)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4331 | #include "clang/Basic/OperatorKinds.def" |
| 4332 | |
| 4333 | // Add any type names visible from the current scope |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 4334 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4335 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4336 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4337 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4338 | |
| 4339 | // Add any type specifiers |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4340 | AddTypeSpecifierResults(getLangOpts(), Results); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4341 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4342 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4343 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4344 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4345 | Results.data(),Results.size()); |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 4346 | } |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4347 | |
Dmitri Gribenko | 572cf58 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4348 | void Sema::CodeCompleteConstructorInitializer( |
| 4349 | Decl *ConstructorD, |
| 4350 | ArrayRef <CXXCtorInitializer *> Initializers) { |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 4351 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4352 | CXXConstructorDecl *Constructor |
| 4353 | = static_cast<CXXConstructorDecl *>(ConstructorD); |
| 4354 | if (!Constructor) |
| 4355 | return; |
| 4356 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4357 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4358 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4359 | CodeCompletionContext::CCC_PotentiallyQualifiedName); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4360 | Results.EnterNewScope(); |
| 4361 | |
| 4362 | // Fill in any already-initialized fields or base classes. |
| 4363 | llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields; |
| 4364 | llvm::SmallPtrSet<CanQualType, 4> InitializedBases; |
Dmitri Gribenko | 572cf58 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4365 | for (unsigned I = 0, E = Initializers.size(); I != E; ++I) { |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4366 | if (Initializers[I]->isBaseInitializer()) |
| 4367 | InitializedBases.insert( |
| 4368 | Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0))); |
| 4369 | else |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4370 | InitializedFields.insert(cast<FieldDecl>( |
| 4371 | Initializers[I]->getAnyMember())); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4372 | } |
| 4373 | |
| 4374 | // Add completions for base classes. |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4375 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4376 | Results.getCodeCompletionTUInfo()); |
Dmitri Gribenko | 572cf58 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4377 | bool SawLastInitializer = Initializers.empty(); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4378 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4379 | for (const auto &Base : ClassDecl->bases()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4380 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) |
| 4381 | .second) { |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4382 | SawLastInitializer |
Dmitri Gribenko | 572cf58 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4383 | = !Initializers.empty() && |
| 4384 | Initializers.back()->isBaseInitializer() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4385 | Context.hasSameUnqualifiedType(Base.getType(), |
Dmitri Gribenko | 572cf58 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4386 | QualType(Initializers.back()->getBaseClass(), 0)); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4387 | continue; |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4388 | } |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4389 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4390 | Builder.AddTypedTextChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4391 | Results.getAllocator().CopyString( |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4392 | Base.getType().getAsString(Policy))); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4393 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4394 | Builder.AddPlaceholderChunk("args"); |
| 4395 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4396 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4397 | SawLastInitializer? CCP_NextInitializer |
| 4398 | : CCP_MemberDeclaration)); |
| 4399 | SawLastInitializer = false; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4400 | } |
| 4401 | |
| 4402 | // Add completions for virtual base classes. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4403 | for (const auto &Base : ClassDecl->vbases()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4404 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) |
| 4405 | .second) { |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4406 | SawLastInitializer |
Dmitri Gribenko | 572cf58 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4407 | = !Initializers.empty() && |
| 4408 | Initializers.back()->isBaseInitializer() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4409 | Context.hasSameUnqualifiedType(Base.getType(), |
Dmitri Gribenko | 572cf58 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4410 | QualType(Initializers.back()->getBaseClass(), 0)); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4411 | continue; |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4412 | } |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4413 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4414 | Builder.AddTypedTextChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4415 | Builder.getAllocator().CopyString( |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4416 | Base.getType().getAsString(Policy))); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4417 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4418 | Builder.AddPlaceholderChunk("args"); |
| 4419 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4420 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4421 | SawLastInitializer? CCP_NextInitializer |
| 4422 | : CCP_MemberDeclaration)); |
| 4423 | SawLastInitializer = false; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4424 | } |
| 4425 | |
| 4426 | // Add completions for members. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4427 | for (auto *Field : ClassDecl->fields()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4428 | if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl())) |
| 4429 | .second) { |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4430 | SawLastInitializer |
Dmitri Gribenko | 572cf58 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4431 | = !Initializers.empty() && |
| 4432 | Initializers.back()->isAnyMemberInitializer() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4433 | Initializers.back()->getAnyMember() == Field; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4434 | continue; |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4435 | } |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4436 | |
| 4437 | if (!Field->getDeclName()) |
| 4438 | continue; |
| 4439 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4440 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4441 | Field->getIdentifier()->getName())); |
| 4442 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4443 | Builder.AddPlaceholderChunk("args"); |
| 4444 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4445 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4446 | SawLastInitializer? CCP_NextInitializer |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 4447 | : CCP_MemberDeclaration, |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 4448 | CXCursor_MemberRef, |
| 4449 | CXAvailability_Available, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4450 | Field)); |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4451 | SawLastInitializer = false; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4452 | } |
| 4453 | Results.ExitScope(); |
| 4454 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4455 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4456 | Results.data(), Results.size()); |
| 4457 | } |
| 4458 | |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4459 | /// \brief Determine whether this scope denotes a namespace. |
| 4460 | static bool isNamespaceScope(Scope *S) { |
Ted Kremenek | f0d5861 | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 4461 | DeclContext *DC = S->getEntity(); |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4462 | if (!DC) |
| 4463 | return false; |
| 4464 | |
| 4465 | return DC->isFileContext(); |
| 4466 | } |
| 4467 | |
| 4468 | void Sema::CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro, |
| 4469 | bool AfterAmpersand) { |
| 4470 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4471 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4472 | CodeCompletionContext::CCC_Other); |
| 4473 | Results.EnterNewScope(); |
| 4474 | |
| 4475 | // Note what has already been captured. |
| 4476 | llvm::SmallPtrSet<IdentifierInfo *, 4> Known; |
| 4477 | bool IncludedThis = false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4478 | for (const auto &C : Intro.Captures) { |
| 4479 | if (C.Kind == LCK_This) { |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4480 | IncludedThis = true; |
| 4481 | continue; |
| 4482 | } |
| 4483 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4484 | Known.insert(C.Id); |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4485 | } |
| 4486 | |
| 4487 | // Look for other capturable variables. |
| 4488 | for (; S && !isNamespaceScope(S); S = S->getParent()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4489 | for (const auto *D : S->decls()) { |
| 4490 | const auto *Var = dyn_cast<VarDecl>(D); |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4491 | if (!Var || |
| 4492 | !Var->hasLocalStorage() || |
| 4493 | Var->hasAttr<BlocksAttr>()) |
| 4494 | continue; |
| 4495 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4496 | if (Known.insert(Var->getIdentifier()).second) |
Douglas Gregor | d1f09b4 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 4497 | Results.AddResult(CodeCompletionResult(Var, CCP_LocalDeclaration), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4498 | CurContext, nullptr, false); |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 4499 | } |
| 4500 | } |
| 4501 | |
| 4502 | // Add 'this', if it would be valid. |
| 4503 | if (!IncludedThis && !AfterAmpersand && Intro.Default != LCD_ByCopy) |
| 4504 | addThisCompletion(*this, Results); |
| 4505 | |
| 4506 | Results.ExitScope(); |
| 4507 | |
| 4508 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4509 | Results.data(), Results.size()); |
| 4510 | } |
| 4511 | |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4512 | /// Macro that optionally prepends an "@" to the string literal passed in via |
| 4513 | /// Keyword, depending on whether NeedAt is true or false. |
| 4514 | #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) ((NeedAt)? "@" Keyword : Keyword) |
| 4515 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4516 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4517 | ResultBuilder &Results, |
| 4518 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4519 | typedef CodeCompletionResult Result; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4520 | // Since we have an implementation, we can end it. |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4521 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end"))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4522 | |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4523 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4524 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4525 | if (LangOpts.ObjC2) { |
| 4526 | // @dynamic |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4527 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"dynamic")); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4528 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4529 | Builder.AddPlaceholderChunk("property"); |
| 4530 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4531 | |
| 4532 | // @synthesize |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4533 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synthesize")); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4534 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4535 | Builder.AddPlaceholderChunk("property"); |
| 4536 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4537 | } |
| 4538 | } |
| 4539 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4540 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4541 | ResultBuilder &Results, |
| 4542 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4543 | typedef CodeCompletionResult Result; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4544 | |
| 4545 | // Since we have an interface or protocol, we can end it. |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4546 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end"))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4547 | |
| 4548 | if (LangOpts.ObjC2) { |
| 4549 | // @property |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4550 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"property"))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4551 | |
| 4552 | // @required |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4553 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"required"))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4554 | |
| 4555 | // @optional |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4556 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"optional"))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4557 | } |
| 4558 | } |
| 4559 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4560 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4561 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4562 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4563 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4564 | |
| 4565 | // @class name ; |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4566 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"class")); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4567 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4568 | Builder.AddPlaceholderChunk("name"); |
| 4569 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4570 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4571 | if (Results.includeCodePatterns()) { |
| 4572 | // @interface name |
| 4573 | // FIXME: Could introduce the whole pattern, including superclasses and |
| 4574 | // such. |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4575 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"interface")); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4576 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4577 | Builder.AddPlaceholderChunk("class"); |
| 4578 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4579 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4580 | // @protocol name |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4581 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol")); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4582 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4583 | Builder.AddPlaceholderChunk("protocol"); |
| 4584 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4585 | |
| 4586 | // @implementation name |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4587 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"implementation")); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4588 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4589 | Builder.AddPlaceholderChunk("class"); |
| 4590 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4591 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4592 | |
| 4593 | // @compatibility_alias name |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4594 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"compatibility_alias")); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4595 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4596 | Builder.AddPlaceholderChunk("alias"); |
| 4597 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4598 | Builder.AddPlaceholderChunk("class"); |
| 4599 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 0689863 | 2013-03-07 23:26:24 +0000 | [diff] [blame] | 4600 | |
| 4601 | if (Results.getSema().getLangOpts().Modules) { |
| 4602 | // @import name |
| 4603 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "import")); |
| 4604 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4605 | Builder.AddPlaceholderChunk("module"); |
| 4606 | Results.AddResult(Result(Builder.TakeString())); |
| 4607 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4608 | } |
| 4609 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4610 | void Sema::CodeCompleteObjCAtDirective(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4611 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4612 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4613 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4614 | Results.EnterNewScope(); |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4615 | if (isa<ObjCImplDecl>(CurContext)) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4616 | AddObjCImplementationResults(getLangOpts(), Results, false); |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4617 | else if (CurContext->isObjCContainer()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4618 | AddObjCInterfaceResults(getLangOpts(), Results, false); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4619 | else |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4620 | AddObjCTopLevelResults(Results, false); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4621 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4622 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4623 | CodeCompletionContext::CCC_Other, |
| 4624 | Results.data(),Results.size()); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4625 | } |
| 4626 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4627 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4628 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4629 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4630 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4631 | |
| 4632 | // @encode ( type-name ) |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 4633 | const char *EncodeType = "char[]"; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4634 | if (Results.getSema().getLangOpts().CPlusPlus || |
| 4635 | Results.getSema().getLangOpts().ConstStrings) |
Jordan Rose | 1f6e22d | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4636 | EncodeType = "const char[]"; |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 4637 | Builder.AddResultTypeChunk(EncodeType); |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4638 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"encode")); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4639 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4640 | Builder.AddPlaceholderChunk("type-name"); |
| 4641 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4642 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4643 | |
| 4644 | // @protocol ( protocol-name ) |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 4645 | Builder.AddResultTypeChunk("Protocol *"); |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4646 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol")); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4647 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4648 | Builder.AddPlaceholderChunk("protocol-name"); |
| 4649 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4650 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4651 | |
| 4652 | // @selector ( selector ) |
Douglas Gregor | 8ca7208 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 4653 | Builder.AddResultTypeChunk("SEL"); |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4654 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"selector")); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4655 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4656 | Builder.AddPlaceholderChunk("selector"); |
| 4657 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4658 | Results.AddResult(Result(Builder.TakeString())); |
Jordan Rose | 1f6e22d | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4659 | |
| 4660 | // @"string" |
| 4661 | Builder.AddResultTypeChunk("NSString *"); |
| 4662 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"\"")); |
| 4663 | Builder.AddPlaceholderChunk("string"); |
| 4664 | Builder.AddTextChunk("\""); |
| 4665 | Results.AddResult(Result(Builder.TakeString())); |
| 4666 | |
Douglas Gregor | 7961589 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 4667 | // @[objects, ...] |
Jordan Rose | 1f6e22d | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4668 | Builder.AddResultTypeChunk("NSArray *"); |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4669 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"[")); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4670 | Builder.AddPlaceholderChunk("objects, ..."); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4671 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 4672 | Results.AddResult(Result(Builder.TakeString())); |
| 4673 | |
Douglas Gregor | 7961589 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 4674 | // @{key : object, ...} |
Jordan Rose | 1f6e22d | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4675 | Builder.AddResultTypeChunk("NSDictionary *"); |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4676 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"{")); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4677 | Builder.AddPlaceholderChunk("key"); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4678 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 4679 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4680 | Builder.AddPlaceholderChunk("object, ..."); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4681 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4682 | Results.AddResult(Result(Builder.TakeString())); |
Jordan Rose | 1f6e22d | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4683 | |
Douglas Gregor | 7961589 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 4684 | // @(expression) |
Jordan Rose | 1f6e22d | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4685 | Builder.AddResultTypeChunk("id"); |
| 4686 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "(")); |
Jordan Rose | 1f6e22d | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4687 | Builder.AddPlaceholderChunk("expression"); |
Jordan Rose | 1f6e22d | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 4688 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4689 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4690 | } |
| 4691 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4692 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4693 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4694 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4695 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4696 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4697 | if (Results.includeCodePatterns()) { |
| 4698 | // @try { statements } @catch ( declaration ) { statements } @finally |
| 4699 | // { statements } |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4700 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"try")); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4701 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4702 | Builder.AddPlaceholderChunk("statements"); |
| 4703 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4704 | Builder.AddTextChunk("@catch"); |
| 4705 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4706 | Builder.AddPlaceholderChunk("parameter"); |
| 4707 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4708 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4709 | Builder.AddPlaceholderChunk("statements"); |
| 4710 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4711 | Builder.AddTextChunk("@finally"); |
| 4712 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4713 | Builder.AddPlaceholderChunk("statements"); |
| 4714 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4715 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4716 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4717 | |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4718 | // @throw |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4719 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"throw")); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4720 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4721 | Builder.AddPlaceholderChunk("expression"); |
| 4722 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4723 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4724 | if (Results.includeCodePatterns()) { |
| 4725 | // @synchronized ( expression ) { statements } |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4726 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synchronized")); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4727 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4728 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4729 | Builder.AddPlaceholderChunk("expression"); |
| 4730 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4731 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4732 | Builder.AddPlaceholderChunk("statements"); |
| 4733 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4734 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4735 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4736 | } |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4737 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4738 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4739 | ResultBuilder &Results, |
| 4740 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4741 | typedef CodeCompletionResult Result; |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4742 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"private"))); |
| 4743 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"protected"))); |
| 4744 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"public"))); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4745 | if (LangOpts.ObjC2) |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4746 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"package"))); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4747 | } |
| 4748 | |
| 4749 | void Sema::CodeCompleteObjCAtVisibility(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4750 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4751 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4752 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4753 | Results.EnterNewScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4754 | AddObjCVisibilityResults(getLangOpts(), Results, false); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4755 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4756 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4757 | CodeCompletionContext::CCC_Other, |
| 4758 | Results.data(),Results.size()); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4759 | } |
| 4760 | |
| 4761 | void Sema::CodeCompleteObjCAtStatement(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4762 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4763 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4764 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4765 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4766 | AddObjCStatementResults(Results, false); |
| 4767 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4768 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4769 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4770 | CodeCompletionContext::CCC_Other, |
| 4771 | Results.data(),Results.size()); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
| 4774 | void Sema::CodeCompleteObjCAtExpression(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4775 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4776 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4777 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4778 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4779 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4780 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4781 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4782 | CodeCompletionContext::CCC_Other, |
| 4783 | Results.data(),Results.size()); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4784 | } |
| 4785 | |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4786 | /// \brief Determine whether the addition of the given flag to an Objective-C |
| 4787 | /// property's attributes will cause a conflict. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4788 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4789 | // Check if we've already added this flag. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4790 | if (Attributes & NewFlag) |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4791 | return true; |
| 4792 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4793 | Attributes |= NewFlag; |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4794 | |
| 4795 | // Check for collisions with "readonly". |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4796 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 4797 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4798 | return true; |
| 4799 | |
Jordan Rose | d7403a7 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 4800 | // Check for more than one of { assign, copy, retain, strong, weak }. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4801 | unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4802 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4803 | ObjCDeclSpec::DQ_PR_copy | |
Jordan Rose | d7403a7 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 4804 | ObjCDeclSpec::DQ_PR_retain | |
| 4805 | ObjCDeclSpec::DQ_PR_strong | |
| 4806 | ObjCDeclSpec::DQ_PR_weak); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4807 | if (AssignCopyRetMask && |
| 4808 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4809 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained && |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4810 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4811 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain && |
Jordan Rose | d7403a7 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 4812 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong && |
| 4813 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_weak) |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4814 | return true; |
| 4815 | |
| 4816 | return false; |
| 4817 | } |
| 4818 | |
Douglas Gregor | a93b108 | 2009-11-18 23:08:07 +0000 | [diff] [blame] | 4819 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4820 | if (!CodeCompleter) |
| 4821 | return; |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4822 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4823 | unsigned Attributes = ODS.getPropertyAttributes(); |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4824 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4825 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4826 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4827 | CodeCompletionContext::CCC_Other); |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4828 | Results.EnterNewScope(); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4829 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4830 | Results.AddResult(CodeCompletionResult("readonly")); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4831 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4832 | Results.AddResult(CodeCompletionResult("assign")); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4833 | if (!ObjCPropertyFlagConflicts(Attributes, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4834 | ObjCDeclSpec::DQ_PR_unsafe_unretained)) |
| 4835 | Results.AddResult(CodeCompletionResult("unsafe_unretained")); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4836 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4837 | Results.AddResult(CodeCompletionResult("readwrite")); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4838 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4839 | Results.AddResult(CodeCompletionResult("retain")); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4840 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4841 | Results.AddResult(CodeCompletionResult("strong")); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4842 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4843 | Results.AddResult(CodeCompletionResult("copy")); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4844 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4845 | Results.AddResult(CodeCompletionResult("nonatomic")); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4846 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic)) |
Fariborz Jahanian | 27f4523 | 2011-06-11 17:14:27 +0000 | [diff] [blame] | 4847 | Results.AddResult(CodeCompletionResult("atomic")); |
Jordan Rose | d7403a7 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 4848 | |
| 4849 | // Only suggest "weak" if we're compiling for ARC-with-weak-references or GC. |
John McCall | 0a7dd78 | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 4850 | if (getLangOpts().ObjCARCWeak || getLangOpts().getGC() != LangOptions::NonGC) |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4851 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_weak)) |
Jordan Rose | d7403a7 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 4852 | Results.AddResult(CodeCompletionResult("weak")); |
| 4853 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4854 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4855 | CodeCompletionBuilder Setter(Results.getAllocator(), |
| 4856 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4857 | Setter.AddTypedTextChunk("setter"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4858 | Setter.AddTextChunk("="); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4859 | Setter.AddPlaceholderChunk("method"); |
| 4860 | Results.AddResult(CodeCompletionResult(Setter.TakeString())); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 4861 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4862 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4863 | CodeCompletionBuilder Getter(Results.getAllocator(), |
| 4864 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4865 | Getter.AddTypedTextChunk("getter"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4866 | Getter.AddTextChunk("="); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4867 | Getter.AddPlaceholderChunk("method"); |
| 4868 | Results.AddResult(CodeCompletionResult(Getter.TakeString())); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 4869 | } |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4870 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4871 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4872 | CodeCompletionContext::CCC_Other, |
| 4873 | Results.data(),Results.size()); |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4874 | } |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4875 | |
James Dennett | de23c7e | 2012-06-17 05:33:25 +0000 | [diff] [blame] | 4876 | /// \brief Describes the kind of Objective-C method that we want to find |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4877 | /// via code completion. |
| 4878 | enum ObjCMethodKind { |
Dmitri Gribenko | 49fdccb | 2012-06-08 23:13:42 +0000 | [diff] [blame] | 4879 | MK_Any, ///< Any kind of method, provided it means other specified criteria. |
| 4880 | MK_ZeroArgSelector, ///< Zero-argument (unary) selector. |
| 4881 | MK_OneArgSelector ///< One-argument selector. |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4882 | }; |
| 4883 | |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4884 | static bool isAcceptableObjCSelector(Selector Sel, |
| 4885 | ObjCMethodKind WantKind, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 4886 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4887 | bool AllowSameLength = true) { |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 4888 | unsigned NumSelIdents = SelIdents.size(); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4889 | if (NumSelIdents > Sel.getNumArgs()) |
| 4890 | return false; |
| 4891 | |
| 4892 | switch (WantKind) { |
| 4893 | case MK_Any: break; |
| 4894 | case MK_ZeroArgSelector: return Sel.isUnarySelector(); |
| 4895 | case MK_OneArgSelector: return Sel.getNumArgs() == 1; |
| 4896 | } |
| 4897 | |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4898 | if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs()) |
| 4899 | return false; |
| 4900 | |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4901 | for (unsigned I = 0; I != NumSelIdents; ++I) |
| 4902 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) |
| 4903 | return false; |
| 4904 | |
| 4905 | return true; |
| 4906 | } |
| 4907 | |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4908 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, |
| 4909 | ObjCMethodKind WantKind, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 4910 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4911 | bool AllowSameLength = true) { |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4912 | return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 4913 | AllowSameLength); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4914 | } |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4915 | |
| 4916 | namespace { |
| 4917 | /// \brief A set of selectors, which is used to avoid introducing multiple |
| 4918 | /// completions with the same selector into the result set. |
| 4919 | typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet; |
| 4920 | } |
| 4921 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4922 | /// \brief Add all of the Objective-C methods in the given Objective-C |
| 4923 | /// container to the set of results. |
| 4924 | /// |
| 4925 | /// The container will be a class, protocol, category, or implementation of |
| 4926 | /// any of the above. This mether will recurse to include methods from |
| 4927 | /// the superclasses of classes along with their categories, protocols, and |
| 4928 | /// implementations. |
| 4929 | /// |
| 4930 | /// \param Container the container in which we'll look to find methods. |
| 4931 | /// |
James Dennett | a40f792 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 4932 | /// \param WantInstanceMethods Whether to add instance methods (only); if |
| 4933 | /// false, this routine will add factory methods (only). |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4934 | /// |
| 4935 | /// \param CurContext the context in which we're performing the lookup that |
| 4936 | /// finds methods. |
| 4937 | /// |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4938 | /// \param AllowSameLength Whether we allow a method to be added to the list |
| 4939 | /// when it has the same number of parameters as we have selector identifiers. |
| 4940 | /// |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4941 | /// \param Results the structure into which we'll add results. |
| 4942 | static void AddObjCMethods(ObjCContainerDecl *Container, |
| 4943 | bool WantInstanceMethods, |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4944 | ObjCMethodKind WantKind, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 4945 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4946 | DeclContext *CurContext, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4947 | VisitedSelectorSet &Selectors, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4948 | bool AllowSameLength, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4949 | ResultBuilder &Results, |
| 4950 | bool InOriginalClass = true) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4951 | typedef CodeCompletionResult Result; |
Douglas Gregor | b92a408 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 4952 | Container = getContainerDef(Container); |
Douglas Gregor | 5824b80 | 2013-01-30 06:58:39 +0000 | [diff] [blame] | 4953 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); |
| 4954 | bool isRootClass = IFace && !IFace->getSuperClass(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4955 | for (auto *M : Container->methods()) { |
Douglas Gregor | 5824b80 | 2013-01-30 06:58:39 +0000 | [diff] [blame] | 4956 | // The instance methods on the root class can be messaged via the |
| 4957 | // metaclass. |
| 4958 | if (M->isInstanceMethod() == WantInstanceMethods || |
| 4959 | (isRootClass && !WantInstanceMethods)) { |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4960 | // Check whether the selector identifiers we've been given are a |
| 4961 | // subset of the identifiers for this particular method. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4962 | if (!isAcceptableObjCMethod(M, WantKind, SelIdents, AllowSameLength)) |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4963 | continue; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4964 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4965 | if (!Selectors.insert(M->getSelector()).second) |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4966 | continue; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4967 | |
| 4968 | Result R = Result(M, Results.getBasePriority(M), nullptr); |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 4969 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4970 | R.AllParametersAreInformative = (WantKind != MK_Any); |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4971 | if (!InOriginalClass) |
| 4972 | R.Priority += CCD_InBaseClass; |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4973 | Results.MaybeAddResult(R, CurContext); |
| 4974 | } |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4975 | } |
| 4976 | |
Douglas Gregor | e396c7b | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 4977 | // Visit the protocols of protocols. |
| 4978 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 4979 | if (Protocol->hasDefinition()) { |
| 4980 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 4981 | = Protocol->getReferencedProtocols(); |
| 4982 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 4983 | E = Protocols.end(); |
| 4984 | I != E; ++I) |
| 4985 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 4986 | CurContext, Selectors, AllowSameLength, Results, false); |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 4987 | } |
Douglas Gregor | e396c7b | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 4990 | if (!IFace || !IFace->hasDefinition()) |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4991 | return; |
| 4992 | |
| 4993 | // Add methods in protocols. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4994 | for (auto *I : IFace->protocols()) |
| 4995 | AddObjCMethods(I, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4996 | CurContext, Selectors, AllowSameLength, Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4997 | |
| 4998 | // Add methods in categories. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4999 | for (auto *CatDecl : IFace->known_categories()) { |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5000 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5001 | CurContext, Selectors, AllowSameLength, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5002 | Results, InOriginalClass); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5003 | |
| 5004 | // Add a categories protocol methods. |
| 5005 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5006 | = CatDecl->getReferencedProtocols(); |
| 5007 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 5008 | E = Protocols.end(); |
| 5009 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5010 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5011 | CurContext, Selectors, AllowSameLength, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5012 | Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5013 | |
| 5014 | // Add methods in category implementations. |
| 5015 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5016 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5017 | CurContext, Selectors, AllowSameLength, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5018 | Results, InOriginalClass); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5019 | } |
| 5020 | |
| 5021 | // Add methods in superclass. |
| 5022 | if (IFace->getSuperClass()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5023 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5024 | SelIdents, CurContext, Selectors, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5025 | AllowSameLength, Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5026 | |
| 5027 | // Add methods in our implementation, if any. |
| 5028 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5029 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5030 | CurContext, Selectors, AllowSameLength, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5031 | Results, InOriginalClass); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5032 | } |
| 5033 | |
| 5034 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5035 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S) { |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5036 | // Try to find the interface where getters might live. |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5037 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5038 | if (!Class) { |
| 5039 | if (ObjCCategoryDecl *Category |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5040 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5041 | Class = Category->getClassInterface(); |
| 5042 | |
| 5043 | if (!Class) |
| 5044 | return; |
| 5045 | } |
| 5046 | |
| 5047 | // Find all of the potential getters. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5048 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5049 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5050 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5051 | Results.EnterNewScope(); |
| 5052 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5053 | VisitedSelectorSet Selectors; |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5054 | AddObjCMethods(Class, true, MK_ZeroArgSelector, None, CurContext, Selectors, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5055 | /*AllowSameLength=*/true, Results); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5056 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5057 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5058 | CodeCompletionContext::CCC_Other, |
| 5059 | Results.data(),Results.size()); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5060 | } |
| 5061 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5062 | void Sema::CodeCompleteObjCPropertySetter(Scope *S) { |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5063 | // Try to find the interface where setters might live. |
| 5064 | ObjCInterfaceDecl *Class |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5065 | = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5066 | if (!Class) { |
| 5067 | if (ObjCCategoryDecl *Category |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5068 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5069 | Class = Category->getClassInterface(); |
| 5070 | |
| 5071 | if (!Class) |
| 5072 | return; |
| 5073 | } |
| 5074 | |
| 5075 | // Find all of the potential getters. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5076 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5077 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5078 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5079 | Results.EnterNewScope(); |
| 5080 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5081 | VisitedSelectorSet Selectors; |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5082 | AddObjCMethods(Class, true, MK_OneArgSelector, None, CurContext, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5083 | Selectors, /*AllowSameLength=*/true, Results); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5084 | |
| 5085 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5086 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5087 | CodeCompletionContext::CCC_Other, |
| 5088 | Results.data(),Results.size()); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5089 | } |
| 5090 | |
Douglas Gregor | afc4578 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5091 | void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS, |
| 5092 | bool IsParameter) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5093 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5094 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5095 | CodeCompletionContext::CCC_Type); |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5096 | Results.EnterNewScope(); |
| 5097 | |
| 5098 | // Add context-sensitive, Objective-C parameter-passing keywords. |
| 5099 | bool AddedInOut = false; |
| 5100 | if ((DS.getObjCDeclQualifier() & |
| 5101 | (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 5102 | Results.AddResult("in"); |
| 5103 | Results.AddResult("inout"); |
| 5104 | AddedInOut = true; |
| 5105 | } |
| 5106 | if ((DS.getObjCDeclQualifier() & |
| 5107 | (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 5108 | Results.AddResult("out"); |
| 5109 | if (!AddedInOut) |
| 5110 | Results.AddResult("inout"); |
| 5111 | } |
| 5112 | if ((DS.getObjCDeclQualifier() & |
| 5113 | (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref | |
| 5114 | ObjCDeclSpec::DQ_Oneway)) == 0) { |
| 5115 | Results.AddResult("bycopy"); |
| 5116 | Results.AddResult("byref"); |
| 5117 | Results.AddResult("oneway"); |
| 5118 | } |
| 5119 | |
Douglas Gregor | afc4578 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5120 | // If we're completing the return type of an Objective-C method and the |
| 5121 | // identifier IBAction refers to a macro, provide a completion item for |
| 5122 | // an action, e.g., |
| 5123 | // IBAction)<#selector#>:(id)sender |
| 5124 | if (DS.getObjCDeclQualifier() == 0 && !IsParameter && |
| 5125 | Context.Idents.get("IBAction").hasMacroDefinition()) { |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5126 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5127 | Results.getCodeCompletionTUInfo(), |
| 5128 | CCP_CodePattern, CXAvailability_Available); |
Douglas Gregor | afc4578 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5129 | Builder.AddTypedTextChunk("IBAction"); |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5130 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | afc4578 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5131 | Builder.AddPlaceholderChunk("selector"); |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5132 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 5133 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | afc4578 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5134 | Builder.AddTextChunk("id"); |
Benjamin Kramer | 1eb18af | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5135 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | afc4578 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5136 | Builder.AddTextChunk("sender"); |
| 5137 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 5138 | } |
Douglas Gregor | 31aa577 | 2013-01-30 07:11:43 +0000 | [diff] [blame] | 5139 | |
| 5140 | // If we're completing the return type, provide 'instancetype'. |
| 5141 | if (!IsParameter) { |
| 5142 | Results.AddResult(CodeCompletionResult("instancetype")); |
| 5143 | } |
Douglas Gregor | afc4578 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5144 | |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5145 | // Add various builtin type names and specifiers. |
| 5146 | AddOrdinaryNameResults(PCC_Type, S, *this, Results); |
| 5147 | Results.ExitScope(); |
| 5148 | |
| 5149 | // Add the various type names |
| 5150 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 5151 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 5152 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 5153 | CodeCompleter->includeGlobals()); |
| 5154 | |
| 5155 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 3644d97 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 5156 | AddMacroResults(PP, Results, false); |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5157 | |
| 5158 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5159 | CodeCompletionContext::CCC_Type, |
| 5160 | Results.data(), Results.size()); |
| 5161 | } |
| 5162 | |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5163 | /// \brief When we have an expression with type "id", we may assume |
| 5164 | /// that it has some more-specific class type based on knowledge of |
| 5165 | /// common uses of Objective-C. This routine returns that class type, |
| 5166 | /// or NULL if no better result could be determined. |
| 5167 | static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) { |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 5168 | ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E); |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5169 | if (!Msg) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5170 | return nullptr; |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5171 | |
| 5172 | Selector Sel = Msg->getSelector(); |
| 5173 | if (Sel.isNull()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5174 | return nullptr; |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5175 | |
| 5176 | IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0); |
| 5177 | if (!Id) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5178 | return nullptr; |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5179 | |
| 5180 | ObjCMethodDecl *Method = Msg->getMethodDecl(); |
| 5181 | if (!Method) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5182 | return nullptr; |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5183 | |
| 5184 | // Determine the class that we're sending the message to. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5185 | ObjCInterfaceDecl *IFace = nullptr; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 5186 | switch (Msg->getReceiverKind()) { |
| 5187 | case ObjCMessageExpr::Class: |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5188 | if (const ObjCObjectType *ObjType |
| 5189 | = Msg->getClassReceiver()->getAs<ObjCObjectType>()) |
| 5190 | IFace = ObjType->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 5191 | break; |
| 5192 | |
| 5193 | case ObjCMessageExpr::Instance: { |
| 5194 | QualType T = Msg->getInstanceReceiver()->getType(); |
| 5195 | if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) |
| 5196 | IFace = Ptr->getInterfaceDecl(); |
| 5197 | break; |
| 5198 | } |
| 5199 | |
| 5200 | case ObjCMessageExpr::SuperInstance: |
| 5201 | case ObjCMessageExpr::SuperClass: |
| 5202 | break; |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5203 | } |
| 5204 | |
| 5205 | if (!IFace) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5206 | return nullptr; |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5207 | |
| 5208 | ObjCInterfaceDecl *Super = IFace->getSuperClass(); |
| 5209 | if (Method->isInstanceMethod()) |
| 5210 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 5211 | .Case("retain", IFace) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5212 | .Case("strong", IFace) |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5213 | .Case("autorelease", IFace) |
| 5214 | .Case("copy", IFace) |
| 5215 | .Case("copyWithZone", IFace) |
| 5216 | .Case("mutableCopy", IFace) |
| 5217 | .Case("mutableCopyWithZone", IFace) |
| 5218 | .Case("awakeFromCoder", IFace) |
| 5219 | .Case("replacementObjectFromCoder", IFace) |
| 5220 | .Case("class", IFace) |
| 5221 | .Case("classForCoder", IFace) |
| 5222 | .Case("superclass", Super) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5223 | .Default(nullptr); |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5224 | |
| 5225 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 5226 | .Case("new", IFace) |
| 5227 | .Case("alloc", IFace) |
| 5228 | .Case("allocWithZone", IFace) |
| 5229 | .Case("class", IFace) |
| 5230 | .Case("superclass", Super) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5231 | .Default(nullptr); |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5232 | } |
| 5233 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5234 | // Add a special completion for a message send to "super", which fills in the |
| 5235 | // most likely case of forwarding all of our arguments to the superclass |
| 5236 | // function. |
| 5237 | /// |
| 5238 | /// \param S The semantic analysis object. |
| 5239 | /// |
Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 5240 | /// \param NeedSuperKeyword Whether we need to prefix this completion with |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5241 | /// the "super" keyword. Otherwise, we just need to provide the arguments. |
| 5242 | /// |
| 5243 | /// \param SelIdents The identifiers in the selector that have already been |
| 5244 | /// provided as arguments for a send to "super". |
| 5245 | /// |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5246 | /// \param Results The set of results to augment. |
| 5247 | /// |
| 5248 | /// \returns the Objective-C method declaration that would be invoked by |
| 5249 | /// this "super" completion. If NULL, no completion was added. |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5250 | static ObjCMethodDecl *AddSuperSendCompletion( |
| 5251 | Sema &S, bool NeedSuperKeyword, |
| 5252 | ArrayRef<IdentifierInfo *> SelIdents, |
| 5253 | ResultBuilder &Results) { |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5254 | ObjCMethodDecl *CurMethod = S.getCurMethodDecl(); |
| 5255 | if (!CurMethod) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5256 | return nullptr; |
| 5257 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5258 | ObjCInterfaceDecl *Class = CurMethod->getClassInterface(); |
| 5259 | if (!Class) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5260 | return nullptr; |
| 5261 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5262 | // Try to find a superclass method with the same selector. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5263 | ObjCMethodDecl *SuperMethod = nullptr; |
Douglas Gregor | 78bcd91 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5264 | while ((Class = Class->getSuperClass()) && !SuperMethod) { |
| 5265 | // Check in the class |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5266 | SuperMethod = Class->getMethod(CurMethod->getSelector(), |
| 5267 | CurMethod->isInstanceMethod()); |
| 5268 | |
Douglas Gregor | 78bcd91 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5269 | // Check in categories or class extensions. |
| 5270 | if (!SuperMethod) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5271 | for (const auto *Cat : Class->known_categories()) { |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5272 | if ((SuperMethod = Cat->getMethod(CurMethod->getSelector(), |
Douglas Gregor | 78bcd91 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5273 | CurMethod->isInstanceMethod()))) |
| 5274 | break; |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5275 | } |
Douglas Gregor | 78bcd91 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5276 | } |
| 5277 | } |
| 5278 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5279 | if (!SuperMethod) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5280 | return nullptr; |
| 5281 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5282 | // Check whether the superclass method has the same signature. |
| 5283 | if (CurMethod->param_size() != SuperMethod->param_size() || |
| 5284 | CurMethod->isVariadic() != SuperMethod->isVariadic()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5285 | return nullptr; |
| 5286 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5287 | for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(), |
| 5288 | CurPEnd = CurMethod->param_end(), |
| 5289 | SuperP = SuperMethod->param_begin(); |
| 5290 | CurP != CurPEnd; ++CurP, ++SuperP) { |
| 5291 | // Make sure the parameter types are compatible. |
| 5292 | if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(), |
| 5293 | (*SuperP)->getType())) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5294 | return nullptr; |
| 5295 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5296 | // Make sure we have a parameter name to forward! |
| 5297 | if (!(*CurP)->getIdentifier()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5298 | return nullptr; |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5299 | } |
| 5300 | |
| 5301 | // We have a superclass method. Now, form the send-to-super completion. |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5302 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5303 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5304 | |
| 5305 | // Give this completion a return type. |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 5306 | AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod, |
| 5307 | Builder); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5308 | |
| 5309 | // If we need the "super" keyword, add it (plus some spacing). |
| 5310 | if (NeedSuperKeyword) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5311 | Builder.AddTypedTextChunk("super"); |
| 5312 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5313 | } |
| 5314 | |
| 5315 | Selector Sel = CurMethod->getSelector(); |
| 5316 | if (Sel.isUnarySelector()) { |
| 5317 | if (NeedSuperKeyword) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5318 | Builder.AddTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5319 | Sel.getNameForSlot(0))); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5320 | else |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5321 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5322 | Sel.getNameForSlot(0))); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5323 | } else { |
| 5324 | ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(); |
| 5325 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) { |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5326 | if (I > SelIdents.size()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5327 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5328 | |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5329 | if (I < SelIdents.size()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5330 | Builder.AddInformativeChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5331 | Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5332 | Sel.getNameForSlot(I) + ":")); |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5333 | else if (NeedSuperKeyword || I > SelIdents.size()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5334 | Builder.AddTextChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5335 | Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5336 | Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5337 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5338 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5339 | } else { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5340 | Builder.AddTypedTextChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5341 | Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5342 | Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5343 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5344 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5345 | } |
| 5346 | } |
| 5347 | } |
| 5348 | |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 5349 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), SuperMethod, |
| 5350 | CCP_SuperCompletion)); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5351 | return SuperMethod; |
| 5352 | } |
| 5353 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5354 | void Sema::CodeCompleteObjCMessageReceiver(Scope *S) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5355 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5356 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5357 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5358 | CodeCompletionContext::CCC_ObjCMessageReceiver, |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5359 | getLangOpts().CPlusPlus11 |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5360 | ? &ResultBuilder::IsObjCMessageReceiverOrLambdaCapture |
| 5361 | : &ResultBuilder::IsObjCMessageReceiver); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5362 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5363 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 5364 | Results.EnterNewScope(); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5365 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 5366 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5367 | |
| 5368 | // If we are in an Objective-C method inside a class that has a superclass, |
| 5369 | // add "super" as an option. |
| 5370 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 5371 | if (ObjCInterfaceDecl *Iface = Method->getClassInterface()) |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5372 | if (Iface->getSuperClass()) { |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5373 | Results.AddResult(Result("super")); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5374 | |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5375 | AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, None, Results); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5376 | } |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5377 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5378 | if (getLangOpts().CPlusPlus11) |
Douglas Gregor | 81f3bff | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5379 | addThisCompletion(*this, Results); |
| 5380 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5381 | Results.ExitScope(); |
| 5382 | |
| 5383 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 3644d97 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 5384 | AddMacroResults(PP, Results, false); |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 5385 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5386 | Results.data(), Results.size()); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5387 | |
| 5388 | } |
| 5389 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5390 | void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5391 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5392 | bool AtArgumentExpression) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5393 | ObjCInterfaceDecl *CDecl = nullptr; |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5394 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 5395 | // Figure out which interface we're in. |
| 5396 | CDecl = CurMethod->getClassInterface(); |
| 5397 | if (!CDecl) |
| 5398 | return; |
| 5399 | |
| 5400 | // Find the superclass of this class. |
| 5401 | CDecl = CDecl->getSuperClass(); |
| 5402 | if (!CDecl) |
| 5403 | return; |
| 5404 | |
| 5405 | if (CurMethod->isInstanceMethod()) { |
| 5406 | // We are inside an instance method, which means that the message |
| 5407 | // send [super ...] is actually calling an instance method on the |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5408 | // current object. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5409 | return CodeCompleteObjCInstanceMessage(S, nullptr, SelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5410 | AtArgumentExpression, |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5411 | CDecl); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | // Fall through to send to the superclass in CDecl. |
| 5415 | } else { |
| 5416 | // "super" may be the name of a type or variable. Figure out which |
| 5417 | // it is. |
Argyrios Kyrtzidis | 57f8da5 | 2013-03-14 22:56:43 +0000 | [diff] [blame] | 5418 | IdentifierInfo *Super = getSuperIdentifier(); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5419 | NamedDecl *ND = LookupSingleName(S, Super, SuperLoc, |
| 5420 | LookupOrdinaryName); |
| 5421 | if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) { |
| 5422 | // "super" names an interface. Use it. |
| 5423 | } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5424 | if (const ObjCObjectType *Iface |
| 5425 | = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>()) |
| 5426 | CDecl = Iface->getInterface(); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5427 | } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) { |
| 5428 | // "super" names an unresolved type; we can't be more specific. |
| 5429 | } else { |
| 5430 | // Assume that "super" names some kind of value and parse that way. |
| 5431 | CXXScopeSpec SS; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 5432 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5433 | UnqualifiedId id; |
| 5434 | id.setIdentifier(Super, SuperLoc); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 5435 | ExprResult SuperExpr = ActOnIdExpression(S, SS, TemplateKWLoc, id, |
| 5436 | false, false); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5437 | return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(), |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5438 | SelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5439 | AtArgumentExpression); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5440 | } |
| 5441 | |
| 5442 | // Fall through |
| 5443 | } |
| 5444 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5445 | ParsedType Receiver; |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5446 | if (CDecl) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5447 | Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5448 | return CodeCompleteObjCClassMessage(S, Receiver, SelIdents, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5449 | AtArgumentExpression, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5450 | /*IsSuper=*/true); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5451 | } |
| 5452 | |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5453 | /// \brief Given a set of code-completion results for the argument of a message |
| 5454 | /// send, determine the preferred type (if any) for that argument expression. |
| 5455 | static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results, |
| 5456 | unsigned NumSelIdents) { |
| 5457 | typedef CodeCompletionResult Result; |
| 5458 | ASTContext &Context = Results.getSema().Context; |
| 5459 | |
| 5460 | QualType PreferredType; |
| 5461 | unsigned BestPriority = CCP_Unlikely * 2; |
| 5462 | Result *ResultsData = Results.data(); |
| 5463 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 5464 | Result &R = ResultsData[I]; |
| 5465 | if (R.Kind == Result::RK_Declaration && |
| 5466 | isa<ObjCMethodDecl>(R.Declaration)) { |
| 5467 | if (R.Priority <= BestPriority) { |
Dmitri Gribenko | 89cf425 | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 5468 | const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration); |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5469 | if (NumSelIdents <= Method->param_size()) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 5470 | QualType MyPreferredType = Method->parameters()[NumSelIdents - 1] |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5471 | ->getType(); |
| 5472 | if (R.Priority < BestPriority || PreferredType.isNull()) { |
| 5473 | BestPriority = R.Priority; |
| 5474 | PreferredType = MyPreferredType; |
| 5475 | } else if (!Context.hasSameUnqualifiedType(PreferredType, |
| 5476 | MyPreferredType)) { |
| 5477 | PreferredType = QualType(); |
| 5478 | } |
| 5479 | } |
| 5480 | } |
| 5481 | } |
| 5482 | } |
| 5483 | |
| 5484 | return PreferredType; |
| 5485 | } |
| 5486 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5487 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| 5488 | ParsedType Receiver, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5489 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5490 | bool AtArgumentExpression, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5491 | bool IsSuper, |
| 5492 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5493 | typedef CodeCompletionResult Result; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5494 | ObjCInterfaceDecl *CDecl = nullptr; |
| 5495 | |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 5496 | // If the given name refers to an interface type, retrieve the |
| 5497 | // corresponding declaration. |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5498 | if (Receiver) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5499 | QualType T = SemaRef.GetTypeFromParser(Receiver, nullptr); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5500 | if (!T.isNull()) |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5501 | if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>()) |
| 5502 | CDecl = Interface->getInterface(); |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 5503 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5504 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5505 | // Add all of the factory methods in this Objective-C class, its protocols, |
| 5506 | // superclasses, categories, implementation, etc. |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5507 | Results.EnterNewScope(); |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5508 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5509 | // If this is a send-to-super, try to add the special "super" send |
| 5510 | // completion. |
| 5511 | if (IsSuper) { |
| 5512 | if (ObjCMethodDecl *SuperMethod |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5513 | = AddSuperSendCompletion(SemaRef, false, SelIdents, Results)) |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5514 | Results.Ignore(SuperMethod); |
| 5515 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5516 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 5517 | // If we're inside an Objective-C method definition, prefer its selector to |
| 5518 | // others. |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5519 | if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl()) |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 5520 | Results.setPreferredSelector(CurMethod->getSelector()); |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5521 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5522 | VisitedSelectorSet Selectors; |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5523 | if (CDecl) |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5524 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5525 | SemaRef.CurContext, Selectors, AtArgumentExpression, |
| 5526 | Results); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5527 | else { |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5528 | // We're messaging "id" as a type; provide all class/factory methods. |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5529 | |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5530 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 5531 | // pool from the AST file. |
Axel Naumann | 0ec56b7 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 5532 | if (SemaRef.getExternalSource()) { |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5533 | for (uint32_t I = 0, |
Axel Naumann | 0ec56b7 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 5534 | N = SemaRef.getExternalSource()->GetNumExternalSelectors(); |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 5535 | I != N; ++I) { |
Axel Naumann | 0ec56b7 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 5536 | Selector Sel = SemaRef.getExternalSource()->GetExternalSelector(I); |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5537 | if (Sel.isNull() || SemaRef.MethodPool.count(Sel)) |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5538 | continue; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5539 | |
| 5540 | SemaRef.ReadMethodPool(Sel); |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5541 | } |
| 5542 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5543 | |
| 5544 | for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(), |
| 5545 | MEnd = SemaRef.MethodPool.end(); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5546 | M != MEnd; ++M) { |
| 5547 | for (ObjCMethodList *MethList = &M->second.second; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5548 | MethList && MethList->getMethod(); |
Argyrios Kyrtzidis | 2e3d8c0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 5549 | MethList = MethList->getNext()) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5550 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5551 | continue; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5552 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5553 | Result R(MethList->getMethod(), |
| 5554 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5555 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5556 | R.AllParametersAreInformative = false; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5557 | Results.MaybeAddResult(R, SemaRef.CurContext); |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5558 | } |
| 5559 | } |
| 5560 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5561 | |
| 5562 | Results.ExitScope(); |
| 5563 | } |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5564 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5565 | void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5566 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5567 | bool AtArgumentExpression, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5568 | bool IsSuper) { |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5569 | |
| 5570 | QualType T = this->GetTypeFromParser(Receiver); |
| 5571 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5572 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5573 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5574 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5575 | T, SelIdents)); |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5576 | |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5577 | AddClassMessageCompletions(*this, S, Receiver, SelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5578 | AtArgumentExpression, IsSuper, Results); |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5579 | |
| 5580 | // If we're actually at the argument expression (rather than prior to the |
| 5581 | // selector), we're actually performing code completion for an expression. |
| 5582 | // Determine whether we have a single, best method. If so, we can |
| 5583 | // code-complete the expression using the corresponding parameter type as |
| 5584 | // our preferred type, improving completion results. |
| 5585 | if (AtArgumentExpression) { |
| 5586 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5587 | SelIdents.size()); |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5588 | if (PreferredType.isNull()) |
| 5589 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 5590 | else |
| 5591 | CodeCompleteExpression(S, PreferredType); |
| 5592 | return; |
| 5593 | } |
| 5594 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5595 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5596 | Results.getCompletionContext(), |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5597 | Results.data(), Results.size()); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5598 | } |
| 5599 | |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 5600 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5601 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5602 | bool AtArgumentExpression, |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5603 | ObjCInterfaceDecl *Super) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5604 | typedef CodeCompletionResult Result; |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5605 | |
| 5606 | Expr *RecExpr = static_cast<Expr *>(Receiver); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5607 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5608 | // If necessary, apply function/array conversion to the receiver. |
| 5609 | // C99 6.7.5.3p[7,8]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5610 | if (RecExpr) { |
| 5611 | ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr); |
| 5612 | if (Conv.isInvalid()) // conversion failed. bail. |
| 5613 | return; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 5614 | RecExpr = Conv.get(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5615 | } |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5616 | QualType ReceiverType = RecExpr? RecExpr->getType() |
| 5617 | : Super? Context.getObjCObjectPointerType( |
| 5618 | Context.getObjCInterfaceType(Super)) |
| 5619 | : Context.getObjCIdType(); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5620 | |
Douglas Gregor | da89264 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 5621 | // If we're messaging an expression with type "id" or "Class", check |
| 5622 | // whether we know something special about the receiver that allows |
| 5623 | // us to assume a more-specific receiver type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5624 | if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) { |
Douglas Gregor | da89264 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 5625 | if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) { |
| 5626 | if (ReceiverType->isObjCClassType()) |
| 5627 | return CodeCompleteObjCClassMessage(S, |
| 5628 | ParsedType::make(Context.getObjCInterfaceType(IFace)), |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5629 | SelIdents, |
Douglas Gregor | da89264 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 5630 | AtArgumentExpression, Super); |
| 5631 | |
| 5632 | ReceiverType = Context.getObjCObjectPointerType( |
| 5633 | Context.getObjCInterfaceType(IFace)); |
| 5634 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5635 | } else if (RecExpr && getLangOpts().CPlusPlus) { |
| 5636 | ExprResult Conv = PerformContextuallyConvertToObjCPointer(RecExpr); |
| 5637 | if (Conv.isUsable()) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 5638 | RecExpr = Conv.get(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5639 | ReceiverType = RecExpr->getType(); |
| 5640 | } |
| 5641 | } |
Douglas Gregor | da89264 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 5642 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5643 | // Build the set of methods we can see. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5644 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5645 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5646 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5647 | ReceiverType, SelIdents)); |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5648 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5649 | Results.EnterNewScope(); |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5650 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5651 | // If this is a send-to-super, try to add the special "super" send |
| 5652 | // completion. |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5653 | if (Super) { |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5654 | if (ObjCMethodDecl *SuperMethod |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5655 | = AddSuperSendCompletion(*this, false, SelIdents, Results)) |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5656 | Results.Ignore(SuperMethod); |
| 5657 | } |
| 5658 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 5659 | // If we're inside an Objective-C method definition, prefer its selector to |
| 5660 | // others. |
| 5661 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 5662 | Results.setPreferredSelector(CurMethod->getSelector()); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5663 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5664 | // Keep track of the selectors we've already added. |
| 5665 | VisitedSelectorSet Selectors; |
| 5666 | |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5667 | // Handle messages to Class. This really isn't a message to an instance |
| 5668 | // method, so we treat it the same way we would treat a message send to a |
| 5669 | // class method. |
| 5670 | if (ReceiverType->isObjCClassType() || |
| 5671 | ReceiverType->isObjCQualifiedClassType()) { |
| 5672 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 5673 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5674 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5675 | CurContext, Selectors, AtArgumentExpression, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5676 | } |
| 5677 | } |
| 5678 | // Handle messages to a qualified ID ("id<foo>"). |
| 5679 | else if (const ObjCObjectPointerType *QualID |
| 5680 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 5681 | // Search protocols for instance methods. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5682 | for (auto *I : QualID->quals()) |
| 5683 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5684 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5685 | } |
| 5686 | // Handle messages to a pointer to interface type. |
| 5687 | else if (const ObjCObjectPointerType *IFacePtr |
| 5688 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 5689 | // Search the class, its superclasses, etc., for instance methods. |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5690 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5691 | CurContext, Selectors, AtArgumentExpression, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5692 | Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5693 | |
| 5694 | // Search protocols for instance methods. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5695 | for (auto *I : IFacePtr->quals()) |
| 5696 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5697 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5698 | } |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5699 | // Handle messages to "id". |
| 5700 | else if (ReceiverType->isObjCIdType()) { |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5701 | // We're messaging "id", so provide all instance methods we know |
| 5702 | // about as code-completion results. |
| 5703 | |
| 5704 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 5705 | // pool from the AST file. |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5706 | if (ExternalSource) { |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 5707 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 5708 | I != N; ++I) { |
| 5709 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5710 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5711 | continue; |
| 5712 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5713 | ReadMethodPool(Sel); |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5714 | } |
| 5715 | } |
| 5716 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5717 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 5718 | MEnd = MethodPool.end(); |
| 5719 | M != MEnd; ++M) { |
| 5720 | for (ObjCMethodList *MethList = &M->second.first; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5721 | MethList && MethList->getMethod(); |
Argyrios Kyrtzidis | 2e3d8c0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 5722 | MethList = MethList->getNext()) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5723 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5724 | continue; |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5725 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5726 | if (!Selectors.insert(MethList->getMethod()->getSelector()).second) |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5727 | continue; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5728 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5729 | Result R(MethList->getMethod(), |
| 5730 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5731 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5732 | R.AllParametersAreInformative = false; |
| 5733 | Results.MaybeAddResult(R, CurContext); |
| 5734 | } |
| 5735 | } |
| 5736 | } |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5737 | Results.ExitScope(); |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5738 | |
| 5739 | |
| 5740 | // If we're actually at the argument expression (rather than prior to the |
| 5741 | // selector), we're actually performing code completion for an expression. |
| 5742 | // Determine whether we have a single, best method. If so, we can |
| 5743 | // code-complete the expression using the corresponding parameter type as |
| 5744 | // our preferred type, improving completion results. |
| 5745 | if (AtArgumentExpression) { |
| 5746 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5747 | SelIdents.size()); |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5748 | if (PreferredType.isNull()) |
| 5749 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 5750 | else |
| 5751 | CodeCompleteExpression(S, PreferredType); |
| 5752 | return; |
| 5753 | } |
| 5754 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5755 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5756 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5757 | Results.data(),Results.size()); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5758 | } |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5759 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 5760 | void Sema::CodeCompleteObjCForCollection(Scope *S, |
| 5761 | DeclGroupPtrTy IterationVar) { |
| 5762 | CodeCompleteExpressionData Data; |
| 5763 | Data.ObjCCollection = true; |
| 5764 | |
| 5765 | if (IterationVar.getAsOpaquePtr()) { |
Serge Pavlov | 1806239 | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 5766 | DeclGroupRef DG = IterationVar.get(); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 5767 | for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) { |
| 5768 | if (*I) |
| 5769 | Data.IgnoreDecls.push_back(*I); |
| 5770 | } |
| 5771 | } |
| 5772 | |
| 5773 | CodeCompleteExpression(S, Data); |
| 5774 | } |
| 5775 | |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5776 | void Sema::CodeCompleteObjCSelector(Scope *S, |
| 5777 | ArrayRef<IdentifierInfo *> SelIdents) { |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5778 | // If we have an external source, load the entire class method |
| 5779 | // pool from the AST file. |
| 5780 | if (ExternalSource) { |
| 5781 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 5782 | I != N; ++I) { |
| 5783 | Selector Sel = ExternalSource->GetExternalSelector(I); |
| 5784 | if (Sel.isNull() || MethodPool.count(Sel)) |
| 5785 | continue; |
| 5786 | |
| 5787 | ReadMethodPool(Sel); |
| 5788 | } |
| 5789 | } |
| 5790 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5791 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5792 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5793 | CodeCompletionContext::CCC_SelectorName); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5794 | Results.EnterNewScope(); |
| 5795 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 5796 | MEnd = MethodPool.end(); |
| 5797 | M != MEnd; ++M) { |
| 5798 | |
| 5799 | Selector Sel = M->first; |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5800 | if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents)) |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5801 | continue; |
| 5802 | |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5803 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5804 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5805 | if (Sel.isUnarySelector()) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5806 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5807 | Sel.getNameForSlot(0))); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5808 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5809 | continue; |
| 5810 | } |
| 5811 | |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5812 | std::string Accumulator; |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5813 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) { |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5814 | if (I == SelIdents.size()) { |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5815 | if (!Accumulator.empty()) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5816 | Builder.AddInformativeChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5817 | Accumulator)); |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5818 | Accumulator.clear(); |
| 5819 | } |
| 5820 | } |
| 5821 | |
Benjamin Kramer | a0651c5 | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 5822 | Accumulator += Sel.getNameForSlot(I); |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5823 | Accumulator += ':'; |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5824 | } |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5825 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator)); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5826 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5827 | } |
| 5828 | Results.ExitScope(); |
| 5829 | |
| 5830 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5831 | CodeCompletionContext::CCC_SelectorName, |
| 5832 | Results.data(), Results.size()); |
| 5833 | } |
| 5834 | |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5835 | /// \brief Add all of the protocol declarations that we find in the given |
| 5836 | /// (translation unit) context. |
| 5837 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5838 | bool OnlyForwardDeclarations, |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5839 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5840 | typedef CodeCompletionResult Result; |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5841 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5842 | for (const auto *D : Ctx->decls()) { |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5843 | // Record any protocols we find. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5844 | if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(D)) |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5845 | if (!OnlyForwardDeclarations || !Proto->hasDefinition()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5846 | Results.AddResult(Result(Proto, Results.getBasePriority(Proto),nullptr), |
| 5847 | CurContext, nullptr, false); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5848 | } |
| 5849 | } |
| 5850 | |
| 5851 | void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols, |
| 5852 | unsigned NumProtocols) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5853 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5854 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5855 | CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5856 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5857 | if (CodeCompleter && CodeCompleter->includeGlobals()) { |
| 5858 | Results.EnterNewScope(); |
| 5859 | |
| 5860 | // Tell the result set to ignore all of the protocols we have |
| 5861 | // already seen. |
| 5862 | // FIXME: This doesn't work when caching code-completion results. |
| 5863 | for (unsigned I = 0; I != NumProtocols; ++I) |
| 5864 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first, |
| 5865 | Protocols[I].second)) |
| 5866 | Results.Ignore(Protocol); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5867 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5868 | // Add all protocols. |
| 5869 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 5870 | Results); |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5871 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5872 | Results.ExitScope(); |
| 5873 | } |
| 5874 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5875 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5876 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 5877 | Results.data(),Results.size()); |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5878 | } |
| 5879 | |
| 5880 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5881 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5882 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5883 | CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5884 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5885 | if (CodeCompleter && CodeCompleter->includeGlobals()) { |
| 5886 | Results.EnterNewScope(); |
| 5887 | |
| 5888 | // Add all protocols. |
| 5889 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 5890 | Results); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5891 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5892 | Results.ExitScope(); |
| 5893 | } |
| 5894 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5895 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5896 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 5897 | Results.data(),Results.size()); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5898 | } |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5899 | |
| 5900 | /// \brief Add all of the Objective-C interface declarations that we find in |
| 5901 | /// the given (translation unit) context. |
| 5902 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, |
| 5903 | bool OnlyForwardDeclarations, |
| 5904 | bool OnlyUnimplemented, |
| 5905 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5906 | typedef CodeCompletionResult Result; |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5907 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5908 | for (const auto *D : Ctx->decls()) { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 5909 | // Record any interfaces we find. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5910 | if (const auto *Class = dyn_cast<ObjCInterfaceDecl>(D)) |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 5911 | if ((!OnlyForwardDeclarations || !Class->hasDefinition()) && |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 5912 | (!OnlyUnimplemented || !Class->getImplementation())) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5913 | Results.AddResult(Result(Class, Results.getBasePriority(Class),nullptr), |
| 5914 | CurContext, nullptr, false); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5915 | } |
| 5916 | } |
| 5917 | |
| 5918 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5919 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5920 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5921 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5922 | Results.EnterNewScope(); |
| 5923 | |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5924 | if (CodeCompleter->includeGlobals()) { |
| 5925 | // Add all classes. |
| 5926 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 5927 | false, Results); |
| 5928 | } |
| 5929 | |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5930 | Results.ExitScope(); |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5931 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5932 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5933 | CodeCompletionContext::CCC_ObjCInterfaceName, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5934 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5935 | } |
| 5936 | |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5937 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName, |
| 5938 | SourceLocation ClassNameLoc) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5939 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5940 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5941 | CodeCompletionContext::CCC_ObjCInterfaceName); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5942 | Results.EnterNewScope(); |
| 5943 | |
| 5944 | // Make sure that we ignore the class we're currently defining. |
| 5945 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5946 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5947 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5948 | Results.Ignore(CurClass); |
| 5949 | |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5950 | if (CodeCompleter->includeGlobals()) { |
| 5951 | // Add all classes. |
| 5952 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 5953 | false, Results); |
| 5954 | } |
| 5955 | |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5956 | Results.ExitScope(); |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5957 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5958 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5959 | CodeCompletionContext::CCC_ObjCInterfaceName, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5960 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5961 | } |
| 5962 | |
| 5963 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5964 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5965 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5966 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5967 | Results.EnterNewScope(); |
| 5968 | |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5969 | if (CodeCompleter->includeGlobals()) { |
| 5970 | // Add all unimplemented classes. |
| 5971 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 5972 | true, Results); |
| 5973 | } |
| 5974 | |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5975 | Results.ExitScope(); |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5976 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5977 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5978 | CodeCompletionContext::CCC_ObjCInterfaceName, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5979 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5980 | } |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5981 | |
| 5982 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5983 | IdentifierInfo *ClassName, |
| 5984 | SourceLocation ClassNameLoc) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5985 | typedef CodeCompletionResult Result; |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5986 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5987 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5988 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 5989 | CodeCompletionContext::CCC_ObjCCategoryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5990 | |
| 5991 | // Ignore any categories we find that have already been implemented by this |
| 5992 | // interface. |
| 5993 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 5994 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5995 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5996 | if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)){ |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5997 | for (const auto *Cat : Class->visible_categories()) |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5998 | CategoryNames.insert(Cat->getIdentifier()); |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5999 | } |
| 6000 | |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6001 | // Add all of the categories we know about. |
| 6002 | Results.EnterNewScope(); |
| 6003 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6004 | for (const auto *D : TU->decls()) |
| 6005 | if (const auto *Category = dyn_cast<ObjCCategoryDecl>(D)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6006 | if (CategoryNames.insert(Category->getIdentifier()).second) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6007 | Results.AddResult(Result(Category, Results.getBasePriority(Category), |
| 6008 | nullptr), |
| 6009 | CurContext, nullptr, false); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6010 | Results.ExitScope(); |
| 6011 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6012 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6013 | CodeCompletionContext::CCC_ObjCCategoryName, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6014 | Results.data(),Results.size()); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6015 | } |
| 6016 | |
| 6017 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6018 | IdentifierInfo *ClassName, |
| 6019 | SourceLocation ClassNameLoc) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6020 | typedef CodeCompletionResult Result; |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6021 | |
| 6022 | // Find the corresponding interface. If we couldn't find the interface, the |
| 6023 | // program itself is ill-formed. However, we'll try to be helpful still by |
| 6024 | // providing the list of all of the categories we know about. |
| 6025 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6026 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6027 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); |
| 6028 | if (!Class) |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6029 | return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6030 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6031 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6032 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6033 | CodeCompletionContext::CCC_ObjCCategoryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6034 | |
| 6035 | // Add all of the categories that have have corresponding interface |
| 6036 | // declarations in this class and any of its superclasses, except for |
| 6037 | // already-implemented categories in the class itself. |
| 6038 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 6039 | Results.EnterNewScope(); |
| 6040 | bool IgnoreImplemented = true; |
| 6041 | while (Class) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6042 | for (const auto *Cat : Class->visible_categories()) { |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6043 | if ((!IgnoreImplemented || !Cat->getImplementation()) && |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6044 | CategoryNames.insert(Cat->getIdentifier()).second) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6045 | Results.AddResult(Result(Cat, Results.getBasePriority(Cat), nullptr), |
| 6046 | CurContext, nullptr, false); |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6047 | } |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6048 | |
| 6049 | Class = Class->getSuperClass(); |
| 6050 | IgnoreImplemented = false; |
| 6051 | } |
| 6052 | Results.ExitScope(); |
| 6053 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6054 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6055 | CodeCompletionContext::CCC_ObjCCategoryName, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6056 | Results.data(),Results.size()); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6057 | } |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6058 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6059 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6060 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6061 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6062 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6063 | |
| 6064 | // Figure out where this @synthesize lives. |
| 6065 | ObjCContainerDecl *Container |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6066 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6067 | if (!Container || |
| 6068 | (!isa<ObjCImplementationDecl>(Container) && |
| 6069 | !isa<ObjCCategoryImplDecl>(Container))) |
| 6070 | return; |
| 6071 | |
| 6072 | // Ignore any properties that have already been implemented. |
Douglas Gregor | b92a408 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6073 | Container = getContainerDef(Container); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6074 | for (const auto *D : Container->decls()) |
| 6075 | if (const auto *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6076 | Results.Ignore(PropertyImpl->getPropertyDecl()); |
| 6077 | |
| 6078 | // Add any properties that we find. |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 6079 | AddedPropertiesSet AddedProperties; |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6080 | Results.EnterNewScope(); |
| 6081 | if (ObjCImplementationDecl *ClassImpl |
| 6082 | = dyn_cast<ObjCImplementationDecl>(Container)) |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 6083 | AddObjCProperties(ClassImpl->getClassInterface(), false, |
| 6084 | /*AllowNullaryMethods=*/false, CurContext, |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 6085 | AddedProperties, Results); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6086 | else |
| 6087 | AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 6088 | false, /*AllowNullaryMethods=*/false, CurContext, |
| 6089 | AddedProperties, Results); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6090 | Results.ExitScope(); |
| 6091 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6092 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6093 | CodeCompletionContext::CCC_Other, |
| 6094 | Results.data(),Results.size()); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6095 | } |
| 6096 | |
| 6097 | void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S, |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6098 | IdentifierInfo *PropertyName) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6099 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6100 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6101 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6102 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6103 | |
| 6104 | // Figure out where this @synthesize lives. |
| 6105 | ObjCContainerDecl *Container |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6106 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6107 | if (!Container || |
| 6108 | (!isa<ObjCImplementationDecl>(Container) && |
| 6109 | !isa<ObjCCategoryImplDecl>(Container))) |
| 6110 | return; |
| 6111 | |
| 6112 | // Figure out which interface we're looking into. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6113 | ObjCInterfaceDecl *Class = nullptr; |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6114 | if (ObjCImplementationDecl *ClassImpl |
| 6115 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| 6116 | Class = ClassImpl->getClassInterface(); |
| 6117 | else |
| 6118 | Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl() |
| 6119 | ->getClassInterface(); |
| 6120 | |
Douglas Gregor | e842605 | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6121 | // Determine the type of the property we're synthesizing. |
| 6122 | QualType PropertyType = Context.getObjCIdType(); |
| 6123 | if (Class) { |
| 6124 | if (ObjCPropertyDecl *Property |
| 6125 | = Class->FindPropertyDeclaration(PropertyName)) { |
| 6126 | PropertyType |
| 6127 | = Property->getType().getNonReferenceType().getUnqualifiedType(); |
| 6128 | |
| 6129 | // Give preference to ivars |
| 6130 | Results.setPreferredType(PropertyType); |
| 6131 | } |
| 6132 | } |
| 6133 | |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6134 | // Add all of the instance variables in this class and its superclasses. |
| 6135 | Results.EnterNewScope(); |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6136 | bool SawSimilarlyNamedIvar = false; |
| 6137 | std::string NameWithPrefix; |
| 6138 | NameWithPrefix += '_'; |
Benjamin Kramer | a0651c5 | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 6139 | NameWithPrefix += PropertyName->getName(); |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6140 | std::string NameWithSuffix = PropertyName->getName().str(); |
| 6141 | NameWithSuffix += '_'; |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6142 | for(; Class; Class = Class->getSuperClass()) { |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6143 | for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar; |
| 6144 | Ivar = Ivar->getNextIvar()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6145 | Results.AddResult(Result(Ivar, Results.getBasePriority(Ivar), nullptr), |
| 6146 | CurContext, nullptr, false); |
| 6147 | |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6148 | // Determine whether we've seen an ivar with a name similar to the |
| 6149 | // property. |
Douglas Gregor | e842605 | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6150 | if ((PropertyName == Ivar->getIdentifier() || |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6151 | NameWithPrefix == Ivar->getName() || |
Douglas Gregor | e842605 | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6152 | NameWithSuffix == Ivar->getName())) { |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6153 | SawSimilarlyNamedIvar = true; |
Douglas Gregor | e842605 | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6154 | |
| 6155 | // Reduce the priority of this result by one, to give it a slight |
| 6156 | // advantage over other results whose names don't match so closely. |
| 6157 | if (Results.size() && |
| 6158 | Results.data()[Results.size() - 1].Kind |
| 6159 | == CodeCompletionResult::RK_Declaration && |
| 6160 | Results.data()[Results.size() - 1].Declaration == Ivar) |
| 6161 | Results.data()[Results.size() - 1].Priority--; |
| 6162 | } |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6163 | } |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6164 | } |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6165 | |
| 6166 | if (!SawSimilarlyNamedIvar) { |
| 6167 | // Create ivar result _propName, that the user can use to synthesize |
Douglas Gregor | e842605 | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6168 | // an ivar of the appropriate type. |
| 6169 | unsigned Priority = CCP_MemberDeclaration + 1; |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6170 | typedef CodeCompletionResult Result; |
| 6171 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6172 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo(), |
| 6173 | Priority,CXAvailability_Available); |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6174 | |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6175 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Douglas Gregor | e842605 | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6176 | Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context, |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6177 | Policy, Allocator)); |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6178 | Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix)); |
| 6179 | Results.AddResult(Result(Builder.TakeString(), Priority, |
| 6180 | CXCursor_ObjCIvarDecl)); |
| 6181 | } |
| 6182 | |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6183 | Results.ExitScope(); |
| 6184 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6185 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6186 | CodeCompletionContext::CCC_Other, |
| 6187 | Results.data(),Results.size()); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6188 | } |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6189 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6190 | // Mapping from selectors to the methods that implement that selector, along |
| 6191 | // with the "in original class" flag. |
Benjamin Kramer | e103979 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 6192 | typedef llvm::DenseMap< |
| 6193 | Selector, llvm::PointerIntPair<ObjCMethodDecl *, 1, bool> > KnownMethodsMap; |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6194 | |
| 6195 | /// \brief Find all of the methods that reside in the given container |
| 6196 | /// (and its superclasses, protocols, etc.) that meet the given |
| 6197 | /// criteria. Insert those methods into the map of known methods, |
| 6198 | /// indexed by selector so they can be easily found. |
| 6199 | static void FindImplementableMethods(ASTContext &Context, |
| 6200 | ObjCContainerDecl *Container, |
| 6201 | bool WantInstanceMethods, |
| 6202 | QualType ReturnType, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6203 | KnownMethodsMap &KnownMethods, |
| 6204 | bool InOriginalClass = true) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6205 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) { |
Douglas Gregor | b92a408 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6206 | // Make sure we have a definition; that's what we'll walk. |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 6207 | if (!IFace->hasDefinition()) |
| 6208 | return; |
Douglas Gregor | b92a408 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6209 | |
| 6210 | IFace = IFace->getDefinition(); |
| 6211 | Container = IFace; |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 6212 | |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6213 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6214 | = IFace->getReferencedProtocols(); |
| 6215 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6216 | E = Protocols.end(); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6217 | I != E; ++I) |
| 6218 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6219 | KnownMethods, InOriginalClass); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6220 | |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6221 | // Add methods from any class extensions and categories. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6222 | for (auto *Cat : IFace->visible_categories()) { |
| 6223 | FindImplementableMethods(Context, Cat, WantInstanceMethods, ReturnType, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6224 | KnownMethods, false); |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6225 | } |
| 6226 | |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6227 | // Visit the superclass. |
| 6228 | if (IFace->getSuperClass()) |
| 6229 | FindImplementableMethods(Context, IFace->getSuperClass(), |
| 6230 | WantInstanceMethods, ReturnType, |
| 6231 | KnownMethods, false); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6232 | } |
| 6233 | |
| 6234 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 6235 | // Recurse into protocols. |
| 6236 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6237 | = Category->getReferencedProtocols(); |
| 6238 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6239 | E = Protocols.end(); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6240 | I != E; ++I) |
| 6241 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6242 | KnownMethods, InOriginalClass); |
| 6243 | |
| 6244 | // If this category is the original class, jump to the interface. |
| 6245 | if (InOriginalClass && Category->getClassInterface()) |
| 6246 | FindImplementableMethods(Context, Category->getClassInterface(), |
| 6247 | WantInstanceMethods, ReturnType, KnownMethods, |
| 6248 | false); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6249 | } |
| 6250 | |
| 6251 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Douglas Gregor | b92a408 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6252 | // Make sure we have a definition; that's what we'll walk. |
| 6253 | if (!Protocol->hasDefinition()) |
| 6254 | return; |
| 6255 | Protocol = Protocol->getDefinition(); |
| 6256 | Container = Protocol; |
| 6257 | |
| 6258 | // Recurse into protocols. |
| 6259 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6260 | = Protocol->getReferencedProtocols(); |
| 6261 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 6262 | E = Protocols.end(); |
| 6263 | I != E; ++I) |
| 6264 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
| 6265 | KnownMethods, false); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6266 | } |
| 6267 | |
| 6268 | // Add methods in this container. This operation occurs last because |
| 6269 | // we want the methods from this container to override any methods |
| 6270 | // we've previously seen with the same selector. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6271 | for (auto *M : Container->methods()) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6272 | if (M->isInstanceMethod() == WantInstanceMethods) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6273 | if (!ReturnType.isNull() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6274 | !Context.hasSameUnqualifiedType(ReturnType, M->getReturnType())) |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6275 | continue; |
| 6276 | |
Benjamin Kramer | e103979 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 6277 | KnownMethods[M->getSelector()] = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6278 | KnownMethodsMap::mapped_type(M, InOriginalClass); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6279 | } |
| 6280 | } |
| 6281 | } |
| 6282 | |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6283 | /// \brief Add the parenthesized return or parameter type chunk to a code |
| 6284 | /// completion string. |
| 6285 | static void AddObjCPassingTypeChunk(QualType Type, |
Douglas Gregor | 90f5f47 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6286 | unsigned ObjCDeclQuals, |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6287 | ASTContext &Context, |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6288 | const PrintingPolicy &Policy, |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6289 | CodeCompletionBuilder &Builder) { |
| 6290 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | 90f5f47 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6291 | std::string Quals = formatObjCParamQualifiers(ObjCDeclQuals); |
| 6292 | if (!Quals.empty()) |
| 6293 | Builder.AddTextChunk(Builder.getAllocator().CopyString(Quals)); |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6294 | Builder.AddTextChunk(GetCompletionTypeString(Type, Context, Policy, |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6295 | Builder.getAllocator())); |
| 6296 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6297 | } |
| 6298 | |
| 6299 | /// \brief Determine whether the given class is or inherits from a class by |
| 6300 | /// the given name. |
| 6301 | static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6302 | StringRef Name) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6303 | if (!Class) |
| 6304 | return false; |
| 6305 | |
| 6306 | if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name) |
| 6307 | return true; |
| 6308 | |
| 6309 | return InheritsFromClassNamed(Class->getSuperClass(), Name); |
| 6310 | } |
| 6311 | |
| 6312 | /// \brief Add code completions for Objective-C Key-Value Coding (KVC) and |
| 6313 | /// Key-Value Observing (KVO). |
| 6314 | static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property, |
| 6315 | bool IsInstanceMethod, |
| 6316 | QualType ReturnType, |
| 6317 | ASTContext &Context, |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6318 | VisitedSelectorSet &KnownSelectors, |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6319 | ResultBuilder &Results) { |
| 6320 | IdentifierInfo *PropName = Property->getIdentifier(); |
| 6321 | if (!PropName || PropName->getLength() == 0) |
| 6322 | return; |
| 6323 | |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6324 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
| 6325 | |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6326 | // Builder that will create each code completion. |
| 6327 | typedef CodeCompletionResult Result; |
| 6328 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6329 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6330 | |
| 6331 | // The selector table. |
| 6332 | SelectorTable &Selectors = Context.Selectors; |
| 6333 | |
| 6334 | // The property name, copied into the code completion allocation region |
| 6335 | // on demand. |
| 6336 | struct KeyHolder { |
| 6337 | CodeCompletionAllocator &Allocator; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6338 | StringRef Key; |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6339 | const char *CopiedKey; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6340 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6341 | KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6342 | : Allocator(Allocator), Key(Key), CopiedKey(nullptr) {} |
| 6343 | |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6344 | operator const char *() { |
| 6345 | if (CopiedKey) |
| 6346 | return CopiedKey; |
| 6347 | |
| 6348 | return CopiedKey = Allocator.CopyString(Key); |
| 6349 | } |
| 6350 | } Key(Allocator, PropName->getName()); |
| 6351 | |
| 6352 | // The uppercased name of the property name. |
| 6353 | std::string UpperKey = PropName->getName(); |
| 6354 | if (!UpperKey.empty()) |
Jordan Rose | 223f0ff | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 6355 | UpperKey[0] = toUppercase(UpperKey[0]); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6356 | |
| 6357 | bool ReturnTypeMatchesProperty = ReturnType.isNull() || |
| 6358 | Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(), |
| 6359 | Property->getType()); |
| 6360 | bool ReturnTypeMatchesVoid |
| 6361 | = ReturnType.isNull() || ReturnType->isVoidType(); |
| 6362 | |
| 6363 | // Add the normal accessor -(type)key. |
| 6364 | if (IsInstanceMethod && |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6365 | KnownSelectors.insert(Selectors.getNullarySelector(PropName)).second && |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6366 | ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) { |
| 6367 | if (ReturnType.isNull()) |
Douglas Gregor | 90f5f47 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6368 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, |
| 6369 | Context, Policy, Builder); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6370 | |
| 6371 | Builder.AddTypedTextChunk(Key); |
| 6372 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 6373 | CXCursor_ObjCInstanceMethodDecl)); |
| 6374 | } |
| 6375 | |
| 6376 | // If we have an integral or boolean property (or the user has provided |
| 6377 | // an integral or boolean return type), add the accessor -(type)isKey. |
| 6378 | if (IsInstanceMethod && |
| 6379 | ((!ReturnType.isNull() && |
| 6380 | (ReturnType->isIntegerType() || ReturnType->isBooleanType())) || |
| 6381 | (ReturnType.isNull() && |
| 6382 | (Property->getType()->isIntegerType() || |
| 6383 | Property->getType()->isBooleanType())))) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6384 | std::string SelectorName = (Twine("is") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6385 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6386 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 6387 | .second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6388 | if (ReturnType.isNull()) { |
| 6389 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6390 | Builder.AddTextChunk("BOOL"); |
| 6391 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6392 | } |
| 6393 | |
| 6394 | Builder.AddTypedTextChunk( |
| 6395 | Allocator.CopyString(SelectorId->getName())); |
| 6396 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 6397 | CXCursor_ObjCInstanceMethodDecl)); |
| 6398 | } |
| 6399 | } |
| 6400 | |
| 6401 | // Add the normal mutator. |
| 6402 | if (IsInstanceMethod && ReturnTypeMatchesVoid && |
| 6403 | !Property->getSetterMethodDecl()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6404 | std::string SelectorName = (Twine("set") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6405 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6406 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6407 | if (ReturnType.isNull()) { |
| 6408 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6409 | Builder.AddTextChunk("void"); |
| 6410 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6411 | } |
| 6412 | |
| 6413 | Builder.AddTypedTextChunk( |
| 6414 | Allocator.CopyString(SelectorId->getName())); |
| 6415 | Builder.AddTypedTextChunk(":"); |
Douglas Gregor | 90f5f47 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6416 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, |
| 6417 | Context, Policy, Builder); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6418 | Builder.AddTextChunk(Key); |
| 6419 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 6420 | CXCursor_ObjCInstanceMethodDecl)); |
| 6421 | } |
| 6422 | } |
| 6423 | |
| 6424 | // Indexed and unordered accessors |
| 6425 | unsigned IndexedGetterPriority = CCP_CodePattern; |
| 6426 | unsigned IndexedSetterPriority = CCP_CodePattern; |
| 6427 | unsigned UnorderedGetterPriority = CCP_CodePattern; |
| 6428 | unsigned UnorderedSetterPriority = CCP_CodePattern; |
| 6429 | if (const ObjCObjectPointerType *ObjCPointer |
| 6430 | = Property->getType()->getAs<ObjCObjectPointerType>()) { |
| 6431 | if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) { |
| 6432 | // If this interface type is not provably derived from a known |
| 6433 | // collection, penalize the corresponding completions. |
| 6434 | if (!InheritsFromClassNamed(IFace, "NSMutableArray")) { |
| 6435 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6436 | if (!InheritsFromClassNamed(IFace, "NSArray")) |
| 6437 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6438 | } |
| 6439 | |
| 6440 | if (!InheritsFromClassNamed(IFace, "NSMutableSet")) { |
| 6441 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6442 | if (!InheritsFromClassNamed(IFace, "NSSet")) |
| 6443 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6444 | } |
| 6445 | } |
| 6446 | } else { |
| 6447 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6448 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6449 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6450 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6451 | } |
| 6452 | |
| 6453 | // Add -(NSUInteger)countOf<key> |
| 6454 | if (IsInstanceMethod && |
| 6455 | (ReturnType.isNull() || ReturnType->isIntegerType())) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6456 | std::string SelectorName = (Twine("countOf") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6457 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6458 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 6459 | .second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6460 | if (ReturnType.isNull()) { |
| 6461 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6462 | Builder.AddTextChunk("NSUInteger"); |
| 6463 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6464 | } |
| 6465 | |
| 6466 | Builder.AddTypedTextChunk( |
| 6467 | Allocator.CopyString(SelectorId->getName())); |
| 6468 | Results.AddResult(Result(Builder.TakeString(), |
| 6469 | std::min(IndexedGetterPriority, |
| 6470 | UnorderedGetterPriority), |
| 6471 | CXCursor_ObjCInstanceMethodDecl)); |
| 6472 | } |
| 6473 | } |
| 6474 | |
| 6475 | // Indexed getters |
| 6476 | // Add -(id)objectInKeyAtIndex:(NSUInteger)index |
| 6477 | if (IsInstanceMethod && |
| 6478 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6479 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6480 | = (Twine("objectIn") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6481 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6482 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6483 | if (ReturnType.isNull()) { |
| 6484 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6485 | Builder.AddTextChunk("id"); |
| 6486 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6487 | } |
| 6488 | |
| 6489 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6490 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6491 | Builder.AddTextChunk("NSUInteger"); |
| 6492 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6493 | Builder.AddTextChunk("index"); |
| 6494 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 6495 | CXCursor_ObjCInstanceMethodDecl)); |
| 6496 | } |
| 6497 | } |
| 6498 | |
| 6499 | // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes |
| 6500 | if (IsInstanceMethod && |
| 6501 | (ReturnType.isNull() || |
| 6502 | (ReturnType->isObjCObjectPointerType() && |
| 6503 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 6504 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 6505 | ->getName() == "NSArray"))) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6506 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6507 | = (Twine(Property->getName()) + "AtIndexes").str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6508 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6509 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6510 | if (ReturnType.isNull()) { |
| 6511 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6512 | Builder.AddTextChunk("NSArray *"); |
| 6513 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6514 | } |
| 6515 | |
| 6516 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6517 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6518 | Builder.AddTextChunk("NSIndexSet *"); |
| 6519 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6520 | Builder.AddTextChunk("indexes"); |
| 6521 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 6522 | CXCursor_ObjCInstanceMethodDecl)); |
| 6523 | } |
| 6524 | } |
| 6525 | |
| 6526 | // Add -(void)getKey:(type **)buffer range:(NSRange)inRange |
| 6527 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6528 | std::string SelectorName = (Twine("get") + UpperKey).str(); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6529 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6530 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6531 | &Context.Idents.get("range") |
| 6532 | }; |
| 6533 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6534 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6535 | if (ReturnType.isNull()) { |
| 6536 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6537 | Builder.AddTextChunk("void"); |
| 6538 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6539 | } |
| 6540 | |
| 6541 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6542 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6543 | Builder.AddPlaceholderChunk("object-type"); |
| 6544 | Builder.AddTextChunk(" **"); |
| 6545 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6546 | Builder.AddTextChunk("buffer"); |
| 6547 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6548 | Builder.AddTypedTextChunk("range:"); |
| 6549 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6550 | Builder.AddTextChunk("NSRange"); |
| 6551 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6552 | Builder.AddTextChunk("inRange"); |
| 6553 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 6554 | CXCursor_ObjCInstanceMethodDecl)); |
| 6555 | } |
| 6556 | } |
| 6557 | |
| 6558 | // Mutable indexed accessors |
| 6559 | |
| 6560 | // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index |
| 6561 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6562 | std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6563 | IdentifierInfo *SelectorIds[2] = { |
| 6564 | &Context.Idents.get("insertObject"), |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6565 | &Context.Idents.get(SelectorName) |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6566 | }; |
| 6567 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6568 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6569 | if (ReturnType.isNull()) { |
| 6570 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6571 | Builder.AddTextChunk("void"); |
| 6572 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6573 | } |
| 6574 | |
| 6575 | Builder.AddTypedTextChunk("insertObject:"); |
| 6576 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6577 | Builder.AddPlaceholderChunk("object-type"); |
| 6578 | Builder.AddTextChunk(" *"); |
| 6579 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6580 | Builder.AddTextChunk("object"); |
| 6581 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6582 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6583 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6584 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 6585 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6586 | Builder.AddTextChunk("index"); |
| 6587 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6588 | CXCursor_ObjCInstanceMethodDecl)); |
| 6589 | } |
| 6590 | } |
| 6591 | |
| 6592 | // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes |
| 6593 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6594 | std::string SelectorName = (Twine("insert") + UpperKey).str(); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6595 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6596 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6597 | &Context.Idents.get("atIndexes") |
| 6598 | }; |
| 6599 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6600 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6601 | if (ReturnType.isNull()) { |
| 6602 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6603 | Builder.AddTextChunk("void"); |
| 6604 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6605 | } |
| 6606 | |
| 6607 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6608 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6609 | Builder.AddTextChunk("NSArray *"); |
| 6610 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6611 | Builder.AddTextChunk("array"); |
| 6612 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6613 | Builder.AddTypedTextChunk("atIndexes:"); |
| 6614 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6615 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 6616 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6617 | Builder.AddTextChunk("indexes"); |
| 6618 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6619 | CXCursor_ObjCInstanceMethodDecl)); |
| 6620 | } |
| 6621 | } |
| 6622 | |
| 6623 | // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index |
| 6624 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6625 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6626 | = (Twine("removeObjectFrom") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6627 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6628 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6629 | if (ReturnType.isNull()) { |
| 6630 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6631 | Builder.AddTextChunk("void"); |
| 6632 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6633 | } |
| 6634 | |
| 6635 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6636 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6637 | Builder.AddTextChunk("NSUInteger"); |
| 6638 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6639 | Builder.AddTextChunk("index"); |
| 6640 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6641 | CXCursor_ObjCInstanceMethodDecl)); |
| 6642 | } |
| 6643 | } |
| 6644 | |
| 6645 | // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes |
| 6646 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6647 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6648 | = (Twine("remove") + UpperKey + "AtIndexes").str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6649 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6650 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6651 | if (ReturnType.isNull()) { |
| 6652 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6653 | Builder.AddTextChunk("void"); |
| 6654 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6655 | } |
| 6656 | |
| 6657 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6658 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6659 | Builder.AddTextChunk("NSIndexSet *"); |
| 6660 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6661 | Builder.AddTextChunk("indexes"); |
| 6662 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6663 | CXCursor_ObjCInstanceMethodDecl)); |
| 6664 | } |
| 6665 | } |
| 6666 | |
| 6667 | // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object |
| 6668 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6669 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6670 | = (Twine("replaceObjectIn") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6671 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6672 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6673 | &Context.Idents.get("withObject") |
| 6674 | }; |
| 6675 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6676 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6677 | if (ReturnType.isNull()) { |
| 6678 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6679 | Builder.AddTextChunk("void"); |
| 6680 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6681 | } |
| 6682 | |
| 6683 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6684 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6685 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 6686 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6687 | Builder.AddTextChunk("index"); |
| 6688 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6689 | Builder.AddTypedTextChunk("withObject:"); |
| 6690 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6691 | Builder.AddTextChunk("id"); |
| 6692 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6693 | Builder.AddTextChunk("object"); |
| 6694 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6695 | CXCursor_ObjCInstanceMethodDecl)); |
| 6696 | } |
| 6697 | } |
| 6698 | |
| 6699 | // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array |
| 6700 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6701 | std::string SelectorName1 |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6702 | = (Twine("replace") + UpperKey + "AtIndexes").str(); |
| 6703 | std::string SelectorName2 = (Twine("with") + UpperKey).str(); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6704 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6705 | &Context.Idents.get(SelectorName1), |
| 6706 | &Context.Idents.get(SelectorName2) |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6707 | }; |
| 6708 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6709 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6710 | if (ReturnType.isNull()) { |
| 6711 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6712 | Builder.AddTextChunk("void"); |
| 6713 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6714 | } |
| 6715 | |
| 6716 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":")); |
| 6717 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6718 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 6719 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6720 | Builder.AddTextChunk("indexes"); |
| 6721 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6722 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":")); |
| 6723 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6724 | Builder.AddTextChunk("NSArray *"); |
| 6725 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6726 | Builder.AddTextChunk("array"); |
| 6727 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6728 | CXCursor_ObjCInstanceMethodDecl)); |
| 6729 | } |
| 6730 | } |
| 6731 | |
| 6732 | // Unordered getters |
| 6733 | // - (NSEnumerator *)enumeratorOfKey |
| 6734 | if (IsInstanceMethod && |
| 6735 | (ReturnType.isNull() || |
| 6736 | (ReturnType->isObjCObjectPointerType() && |
| 6737 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 6738 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 6739 | ->getName() == "NSEnumerator"))) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6740 | std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6741 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6742 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 6743 | .second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6744 | if (ReturnType.isNull()) { |
| 6745 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6746 | Builder.AddTextChunk("NSEnumerator *"); |
| 6747 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6748 | } |
| 6749 | |
| 6750 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 6751 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
| 6752 | CXCursor_ObjCInstanceMethodDecl)); |
| 6753 | } |
| 6754 | } |
| 6755 | |
| 6756 | // - (type *)memberOfKey:(type *)object |
| 6757 | if (IsInstanceMethod && |
| 6758 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6759 | std::string SelectorName = (Twine("memberOf") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6760 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6761 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6762 | if (ReturnType.isNull()) { |
| 6763 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6764 | Builder.AddPlaceholderChunk("object-type"); |
| 6765 | Builder.AddTextChunk(" *"); |
| 6766 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6767 | } |
| 6768 | |
| 6769 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6770 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6771 | if (ReturnType.isNull()) { |
| 6772 | Builder.AddPlaceholderChunk("object-type"); |
| 6773 | Builder.AddTextChunk(" *"); |
| 6774 | } else { |
| 6775 | Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context, |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6776 | Policy, |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6777 | Builder.getAllocator())); |
| 6778 | } |
| 6779 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6780 | Builder.AddTextChunk("object"); |
| 6781 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
| 6782 | CXCursor_ObjCInstanceMethodDecl)); |
| 6783 | } |
| 6784 | } |
| 6785 | |
| 6786 | // Mutable unordered accessors |
| 6787 | // - (void)addKeyObject:(type *)object |
| 6788 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6789 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6790 | = (Twine("add") + UpperKey + Twine("Object")).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6791 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6792 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6793 | if (ReturnType.isNull()) { |
| 6794 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6795 | Builder.AddTextChunk("void"); |
| 6796 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6797 | } |
| 6798 | |
| 6799 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6800 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6801 | Builder.AddPlaceholderChunk("object-type"); |
| 6802 | Builder.AddTextChunk(" *"); |
| 6803 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6804 | Builder.AddTextChunk("object"); |
| 6805 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6806 | CXCursor_ObjCInstanceMethodDecl)); |
| 6807 | } |
| 6808 | } |
| 6809 | |
| 6810 | // - (void)addKey:(NSSet *)objects |
| 6811 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6812 | std::string SelectorName = (Twine("add") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6813 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6814 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6815 | if (ReturnType.isNull()) { |
| 6816 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6817 | Builder.AddTextChunk("void"); |
| 6818 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6819 | } |
| 6820 | |
| 6821 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6822 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6823 | Builder.AddTextChunk("NSSet *"); |
| 6824 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6825 | Builder.AddTextChunk("objects"); |
| 6826 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6827 | CXCursor_ObjCInstanceMethodDecl)); |
| 6828 | } |
| 6829 | } |
| 6830 | |
| 6831 | // - (void)removeKeyObject:(type *)object |
| 6832 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6833 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6834 | = (Twine("remove") + UpperKey + Twine("Object")).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6835 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6836 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6837 | if (ReturnType.isNull()) { |
| 6838 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6839 | Builder.AddTextChunk("void"); |
| 6840 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6841 | } |
| 6842 | |
| 6843 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6844 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6845 | Builder.AddPlaceholderChunk("object-type"); |
| 6846 | Builder.AddTextChunk(" *"); |
| 6847 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6848 | Builder.AddTextChunk("object"); |
| 6849 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6850 | CXCursor_ObjCInstanceMethodDecl)); |
| 6851 | } |
| 6852 | } |
| 6853 | |
| 6854 | // - (void)removeKey:(NSSet *)objects |
| 6855 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6856 | std::string SelectorName = (Twine("remove") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6857 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6858 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6859 | if (ReturnType.isNull()) { |
| 6860 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6861 | Builder.AddTextChunk("void"); |
| 6862 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6863 | } |
| 6864 | |
| 6865 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6866 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6867 | Builder.AddTextChunk("NSSet *"); |
| 6868 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6869 | Builder.AddTextChunk("objects"); |
| 6870 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6871 | CXCursor_ObjCInstanceMethodDecl)); |
| 6872 | } |
| 6873 | } |
| 6874 | |
| 6875 | // - (void)intersectKey:(NSSet *)objects |
| 6876 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6877 | std::string SelectorName = (Twine("intersect") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6878 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6879 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6880 | if (ReturnType.isNull()) { |
| 6881 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6882 | Builder.AddTextChunk("void"); |
| 6883 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6884 | } |
| 6885 | |
| 6886 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6887 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6888 | Builder.AddTextChunk("NSSet *"); |
| 6889 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6890 | Builder.AddTextChunk("objects"); |
| 6891 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6892 | CXCursor_ObjCInstanceMethodDecl)); |
| 6893 | } |
| 6894 | } |
| 6895 | |
| 6896 | // Key-Value Observing |
| 6897 | // + (NSSet *)keyPathsForValuesAffectingKey |
| 6898 | if (!IsInstanceMethod && |
| 6899 | (ReturnType.isNull() || |
| 6900 | (ReturnType->isObjCObjectPointerType() && |
| 6901 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 6902 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 6903 | ->getName() == "NSSet"))) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6904 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6905 | = (Twine("keyPathsForValuesAffecting") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6906 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6907 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 6908 | .second) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6909 | if (ReturnType.isNull()) { |
| 6910 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6911 | Builder.AddTextChunk("NSSet *"); |
| 6912 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6913 | } |
| 6914 | |
| 6915 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 6916 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Douglas Gregor | 3f828d1 | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 6917 | CXCursor_ObjCClassMethodDecl)); |
| 6918 | } |
| 6919 | } |
| 6920 | |
| 6921 | // + (BOOL)automaticallyNotifiesObserversForKey |
| 6922 | if (!IsInstanceMethod && |
| 6923 | (ReturnType.isNull() || |
| 6924 | ReturnType->isIntegerType() || |
| 6925 | ReturnType->isBooleanType())) { |
| 6926 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6927 | = (Twine("automaticallyNotifiesObserversOf") + UpperKey).str(); |
Douglas Gregor | 3f828d1 | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 6928 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6929 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 6930 | .second) { |
Douglas Gregor | 3f828d1 | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 6931 | if (ReturnType.isNull()) { |
| 6932 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6933 | Builder.AddTextChunk("BOOL"); |
| 6934 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6935 | } |
| 6936 | |
| 6937 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 6938 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 6939 | CXCursor_ObjCClassMethodDecl)); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6940 | } |
| 6941 | } |
| 6942 | } |
| 6943 | |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6944 | void Sema::CodeCompleteObjCMethodDecl(Scope *S, |
| 6945 | bool IsInstanceMethod, |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6946 | ParsedType ReturnTy) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6947 | // Determine the return type of the method we're declaring, if |
| 6948 | // provided. |
| 6949 | QualType ReturnType = GetTypeFromParser(ReturnTy); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6950 | Decl *IDecl = nullptr; |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6951 | if (CurContext->isObjCContainer()) { |
| 6952 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
| 6953 | IDecl = cast<Decl>(OCD); |
| 6954 | } |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6955 | // Determine where we should start searching for methods. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6956 | ObjCContainerDecl *SearchDecl = nullptr; |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6957 | bool IsInImplementation = false; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6958 | if (Decl *D = IDecl) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6959 | if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) { |
| 6960 | SearchDecl = Impl->getClassInterface(); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6961 | IsInImplementation = true; |
| 6962 | } else if (ObjCCategoryImplDecl *CatImpl |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6963 | = dyn_cast<ObjCCategoryImplDecl>(D)) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6964 | SearchDecl = CatImpl->getCategoryDecl(); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6965 | IsInImplementation = true; |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6966 | } else |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6967 | SearchDecl = dyn_cast<ObjCContainerDecl>(D); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6968 | } |
| 6969 | |
| 6970 | if (!SearchDecl && S) { |
Ted Kremenek | f0d5861 | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 6971 | if (DeclContext *DC = S->getEntity()) |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6972 | SearchDecl = dyn_cast<ObjCContainerDecl>(DC); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6973 | } |
| 6974 | |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6975 | if (!SearchDecl) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6976 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6977 | CodeCompletionContext::CCC_Other, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6978 | nullptr, 0); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6979 | return; |
| 6980 | } |
| 6981 | |
| 6982 | // Find all of the methods that we could declare/implement here. |
| 6983 | KnownMethodsMap KnownMethods; |
| 6984 | FindImplementableMethods(Context, SearchDecl, IsInstanceMethod, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6985 | ReturnType, KnownMethods); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6986 | |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6987 | // Add declarations or definitions for each of the known methods. |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6988 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6989 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6990 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6991 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6992 | Results.EnterNewScope(); |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6993 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6994 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
| 6995 | MEnd = KnownMethods.end(); |
| 6996 | M != MEnd; ++M) { |
Benjamin Kramer | e103979 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 6997 | ObjCMethodDecl *Method = M->second.getPointer(); |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6998 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 6999 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7000 | |
| 7001 | // If the result type was not already provided, add it to the |
| 7002 | // pattern as (type). |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7003 | if (ReturnType.isNull()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7004 | AddObjCPassingTypeChunk(Method->getReturnType(), |
| 7005 | Method->getObjCDeclQualifier(), Context, Policy, |
| 7006 | Builder); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7007 | |
| 7008 | Selector Sel = Method->getSelector(); |
| 7009 | |
| 7010 | // Add the first part of the selector to the pattern. |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7011 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 7012 | Sel.getNameForSlot(0))); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7013 | |
| 7014 | // Add parameters to the pattern. |
| 7015 | unsigned I = 0; |
| 7016 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 7017 | PEnd = Method->param_end(); |
| 7018 | P != PEnd; (void)++P, ++I) { |
| 7019 | // Add the part of the selector name. |
| 7020 | if (I == 0) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7021 | Builder.AddTypedTextChunk(":"); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7022 | else if (I < Sel.getNumArgs()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7023 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7024 | Builder.AddTypedTextChunk( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 7025 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7026 | } else |
| 7027 | break; |
| 7028 | |
| 7029 | // Add the parameter type. |
Douglas Gregor | 90f5f47 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 7030 | AddObjCPassingTypeChunk((*P)->getOriginalType(), |
| 7031 | (*P)->getObjCDeclQualifier(), |
| 7032 | Context, Policy, |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7033 | Builder); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7034 | |
| 7035 | if (IdentifierInfo *Id = (*P)->getIdentifier()) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7036 | Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName())); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7037 | } |
| 7038 | |
| 7039 | if (Method->isVariadic()) { |
| 7040 | if (Method->param_size() > 0) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7041 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 7042 | Builder.AddTextChunk("..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 7043 | } |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7044 | |
Douglas Gregor | 447107d | 2010-05-28 00:57:46 +0000 | [diff] [blame] | 7045 | if (IsInImplementation && Results.includeCodePatterns()) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7046 | // We will be defining the method here, so add a compound statement. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7047 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7048 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 7049 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7050 | if (!Method->getReturnType()->isVoidType()) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7051 | // If the result type is not void, add a return clause. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7052 | Builder.AddTextChunk("return"); |
| 7053 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7054 | Builder.AddPlaceholderChunk("expression"); |
| 7055 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7056 | } else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7057 | Builder.AddPlaceholderChunk("statements"); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7058 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7059 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 7060 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7061 | } |
| 7062 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 7063 | unsigned Priority = CCP_CodePattern; |
Benjamin Kramer | e103979 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 7064 | if (!M->second.getInt()) |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 7065 | Priority += CCD_InBaseClass; |
| 7066 | |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 7067 | Results.AddResult(Result(Builder.TakeString(), Method, Priority)); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7068 | } |
| 7069 | |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7070 | // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of |
| 7071 | // the properties in this class and its categories. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7072 | if (Context.getLangOpts().ObjC2) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7073 | SmallVector<ObjCContainerDecl *, 4> Containers; |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7074 | Containers.push_back(SearchDecl); |
| 7075 | |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 7076 | VisitedSelectorSet KnownSelectors; |
| 7077 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
| 7078 | MEnd = KnownMethods.end(); |
| 7079 | M != MEnd; ++M) |
| 7080 | KnownSelectors.insert(M->first); |
| 7081 | |
| 7082 | |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7083 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl); |
| 7084 | if (!IFace) |
| 7085 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl)) |
| 7086 | IFace = Category->getClassInterface(); |
| 7087 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7088 | if (IFace) |
| 7089 | for (auto *Cat : IFace->visible_categories()) |
| 7090 | Containers.push_back(Cat); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7091 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7092 | for (unsigned I = 0, N = Containers.size(); I != N; ++I) |
| 7093 | for (auto *P : Containers[I]->properties()) |
| 7094 | AddObjCKeyValueCompletions(P, IsInstanceMethod, ReturnType, Context, |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 7095 | KnownSelectors, Results); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7096 | } |
| 7097 | |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7098 | Results.ExitScope(); |
| 7099 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 7100 | HandleCodeCompleteResults(this, CodeCompleter, |
| 7101 | CodeCompletionContext::CCC_Other, |
| 7102 | Results.data(),Results.size()); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7103 | } |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7104 | |
| 7105 | void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S, |
| 7106 | bool IsInstanceMethod, |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7107 | bool AtParameterName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7108 | ParsedType ReturnTy, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7109 | ArrayRef<IdentifierInfo *> SelIdents) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7110 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 7111 | // pool from the AST file. |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7112 | if (ExternalSource) { |
| 7113 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 7114 | I != N; ++I) { |
| 7115 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7116 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7117 | continue; |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7118 | |
| 7119 | ReadMethodPool(Sel); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7120 | } |
| 7121 | } |
| 7122 | |
| 7123 | // Build the set of methods we can see. |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7124 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7125 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7126 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7127 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7128 | |
| 7129 | if (ReturnTy) |
| 7130 | Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType()); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7131 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7132 | Results.EnterNewScope(); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7133 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 7134 | MEnd = MethodPool.end(); |
| 7135 | M != MEnd; ++M) { |
| 7136 | for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first : |
| 7137 | &M->second.second; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 7138 | MethList && MethList->getMethod(); |
Argyrios Kyrtzidis | 2e3d8c0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 7139 | MethList = MethList->getNext()) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 7140 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7141 | continue; |
| 7142 | |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7143 | if (AtParameterName) { |
| 7144 | // Suggest parameter names we've seen before. |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7145 | unsigned NumSelIdents = SelIdents.size(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 7146 | if (NumSelIdents && |
| 7147 | NumSelIdents <= MethList->getMethod()->param_size()) { |
| 7148 | ParmVarDecl *Param = |
| 7149 | MethList->getMethod()->parameters()[NumSelIdents - 1]; |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7150 | if (Param->getIdentifier()) { |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7151 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7152 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7153 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7154 | Param->getIdentifier()->getName())); |
| 7155 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7156 | } |
| 7157 | } |
| 7158 | |
| 7159 | continue; |
| 7160 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7161 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 7162 | Result R(MethList->getMethod(), |
| 7163 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7164 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7165 | R.AllParametersAreInformative = false; |
| 7166 | R.DeclaringEntity = true; |
| 7167 | Results.MaybeAddResult(R, CurContext); |
| 7168 | } |
| 7169 | } |
| 7170 | |
| 7171 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 7172 | HandleCodeCompleteResults(this, CodeCompleter, |
| 7173 | CodeCompletionContext::CCC_Other, |
| 7174 | Results.data(),Results.size()); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7175 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 7176 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7177 | void Sema::CodeCompletePreprocessorDirective(bool InConditional) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7178 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7179 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7180 | CodeCompletionContext::CCC_PreprocessorDirective); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7181 | Results.EnterNewScope(); |
| 7182 | |
| 7183 | // #if <condition> |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7184 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7185 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7186 | Builder.AddTypedTextChunk("if"); |
| 7187 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7188 | Builder.AddPlaceholderChunk("condition"); |
| 7189 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7190 | |
| 7191 | // #ifdef <macro> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7192 | Builder.AddTypedTextChunk("ifdef"); |
| 7193 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7194 | Builder.AddPlaceholderChunk("macro"); |
| 7195 | Results.AddResult(Builder.TakeString()); |
| 7196 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7197 | // #ifndef <macro> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7198 | Builder.AddTypedTextChunk("ifndef"); |
| 7199 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7200 | Builder.AddPlaceholderChunk("macro"); |
| 7201 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7202 | |
| 7203 | if (InConditional) { |
| 7204 | // #elif <condition> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7205 | Builder.AddTypedTextChunk("elif"); |
| 7206 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7207 | Builder.AddPlaceholderChunk("condition"); |
| 7208 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7209 | |
| 7210 | // #else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7211 | Builder.AddTypedTextChunk("else"); |
| 7212 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7213 | |
| 7214 | // #endif |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7215 | Builder.AddTypedTextChunk("endif"); |
| 7216 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7217 | } |
| 7218 | |
| 7219 | // #include "header" |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7220 | Builder.AddTypedTextChunk("include"); |
| 7221 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7222 | Builder.AddTextChunk("\""); |
| 7223 | Builder.AddPlaceholderChunk("header"); |
| 7224 | Builder.AddTextChunk("\""); |
| 7225 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7226 | |
| 7227 | // #include <header> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7228 | Builder.AddTypedTextChunk("include"); |
| 7229 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7230 | Builder.AddTextChunk("<"); |
| 7231 | Builder.AddPlaceholderChunk("header"); |
| 7232 | Builder.AddTextChunk(">"); |
| 7233 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7234 | |
| 7235 | // #define <macro> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7236 | Builder.AddTypedTextChunk("define"); |
| 7237 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7238 | Builder.AddPlaceholderChunk("macro"); |
| 7239 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7240 | |
| 7241 | // #define <macro>(<args>) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7242 | Builder.AddTypedTextChunk("define"); |
| 7243 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7244 | Builder.AddPlaceholderChunk("macro"); |
| 7245 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7246 | Builder.AddPlaceholderChunk("args"); |
| 7247 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7248 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7249 | |
| 7250 | // #undef <macro> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7251 | Builder.AddTypedTextChunk("undef"); |
| 7252 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7253 | Builder.AddPlaceholderChunk("macro"); |
| 7254 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7255 | |
| 7256 | // #line <number> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7257 | Builder.AddTypedTextChunk("line"); |
| 7258 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7259 | Builder.AddPlaceholderChunk("number"); |
| 7260 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7261 | |
| 7262 | // #line <number> "filename" |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7263 | Builder.AddTypedTextChunk("line"); |
| 7264 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7265 | Builder.AddPlaceholderChunk("number"); |
| 7266 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7267 | Builder.AddTextChunk("\""); |
| 7268 | Builder.AddPlaceholderChunk("filename"); |
| 7269 | Builder.AddTextChunk("\""); |
| 7270 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7271 | |
| 7272 | // #error <message> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7273 | Builder.AddTypedTextChunk("error"); |
| 7274 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7275 | Builder.AddPlaceholderChunk("message"); |
| 7276 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7277 | |
| 7278 | // #pragma <arguments> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7279 | Builder.AddTypedTextChunk("pragma"); |
| 7280 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7281 | Builder.AddPlaceholderChunk("arguments"); |
| 7282 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7283 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7284 | if (getLangOpts().ObjC1) { |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7285 | // #import "header" |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7286 | Builder.AddTypedTextChunk("import"); |
| 7287 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7288 | Builder.AddTextChunk("\""); |
| 7289 | Builder.AddPlaceholderChunk("header"); |
| 7290 | Builder.AddTextChunk("\""); |
| 7291 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7292 | |
| 7293 | // #import <header> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7294 | Builder.AddTypedTextChunk("import"); |
| 7295 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7296 | Builder.AddTextChunk("<"); |
| 7297 | Builder.AddPlaceholderChunk("header"); |
| 7298 | Builder.AddTextChunk(">"); |
| 7299 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7300 | } |
| 7301 | |
| 7302 | // #include_next "header" |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7303 | Builder.AddTypedTextChunk("include_next"); |
| 7304 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7305 | Builder.AddTextChunk("\""); |
| 7306 | Builder.AddPlaceholderChunk("header"); |
| 7307 | Builder.AddTextChunk("\""); |
| 7308 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7309 | |
| 7310 | // #include_next <header> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7311 | Builder.AddTypedTextChunk("include_next"); |
| 7312 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7313 | Builder.AddTextChunk("<"); |
| 7314 | Builder.AddPlaceholderChunk("header"); |
| 7315 | Builder.AddTextChunk(">"); |
| 7316 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7317 | |
| 7318 | // #warning <message> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7319 | Builder.AddTypedTextChunk("warning"); |
| 7320 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7321 | Builder.AddPlaceholderChunk("message"); |
| 7322 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7323 | |
| 7324 | // Note: #ident and #sccs are such crazy anachronisms that we don't provide |
| 7325 | // completions for them. And __include_macros is a Clang-internal extension |
| 7326 | // that we don't want to encourage anyone to use. |
| 7327 | |
| 7328 | // FIXME: we don't support #assert or #unassert, so don't suggest them. |
| 7329 | Results.ExitScope(); |
| 7330 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7331 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 7332 | CodeCompletionContext::CCC_PreprocessorDirective, |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7333 | Results.data(), Results.size()); |
| 7334 | } |
| 7335 | |
| 7336 | void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) { |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7337 | CodeCompleteOrdinaryName(S, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7338 | S->getFnParent()? Sema::PCC_RecoveryInFunction |
| 7339 | : Sema::PCC_Namespace); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7340 | } |
| 7341 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7342 | void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7343 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7344 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7345 | IsDefinition? CodeCompletionContext::CCC_MacroName |
| 7346 | : CodeCompletionContext::CCC_MacroNameUse); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7347 | if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) { |
| 7348 | // Add just the names of macros, not their arguments. |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7349 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7350 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7351 | Results.EnterNewScope(); |
| 7352 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 7353 | MEnd = PP.macro_end(); |
| 7354 | M != MEnd; ++M) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7355 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7356 | M->first->getName())); |
Argyrios Kyrtzidis | c04bb92 | 2012-09-27 00:24:09 +0000 | [diff] [blame] | 7357 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
| 7358 | CCP_CodePattern, |
| 7359 | CXCursor_MacroDefinition)); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7360 | } |
| 7361 | Results.ExitScope(); |
| 7362 | } else if (IsDefinition) { |
| 7363 | // FIXME: Can we detect when the user just wrote an include guard above? |
| 7364 | } |
| 7365 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7366 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7367 | Results.data(), Results.size()); |
| 7368 | } |
| 7369 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7370 | void Sema::CodeCompletePreprocessorExpression() { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7371 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7372 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7373 | CodeCompletionContext::CCC_PreprocessorExpression); |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7374 | |
| 7375 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
Douglas Gregor | 3644d97 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 7376 | AddMacroResults(PP, Results, true); |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7377 | |
| 7378 | // defined (<macro>) |
| 7379 | Results.EnterNewScope(); |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7380 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7381 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7382 | Builder.AddTypedTextChunk("defined"); |
| 7383 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7384 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7385 | Builder.AddPlaceholderChunk("macro"); |
| 7386 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7387 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7388 | Results.ExitScope(); |
| 7389 | |
| 7390 | HandleCodeCompleteResults(this, CodeCompleter, |
| 7391 | CodeCompletionContext::CCC_PreprocessorExpression, |
| 7392 | Results.data(), Results.size()); |
| 7393 | } |
| 7394 | |
| 7395 | void Sema::CodeCompletePreprocessorMacroArgument(Scope *S, |
| 7396 | IdentifierInfo *Macro, |
| 7397 | MacroInfo *MacroInfo, |
| 7398 | unsigned Argument) { |
| 7399 | // FIXME: In the future, we could provide "overload" results, much like we |
| 7400 | // do for function calls. |
| 7401 | |
Argyrios Kyrtzidis | 5c5f03e | 2011-08-18 19:41:28 +0000 | [diff] [blame] | 7402 | // Now just ignore this. There will be another code-completion callback |
| 7403 | // for the expanded tokens. |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7404 | } |
| 7405 | |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 7406 | void Sema::CodeCompleteNaturalLanguage() { |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 7407 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | af1c6b5 | 2010-08-25 17:10:00 +0000 | [diff] [blame] | 7408 | CodeCompletionContext::CCC_NaturalLanguage, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7409 | nullptr, 0); |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 7410 | } |
| 7411 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7412 | void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator, |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7413 | CodeCompletionTUInfo &CCTUInfo, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7414 | SmallVectorImpl<CodeCompletionResult> &Results) { |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7415 | ResultBuilder Builder(*this, Allocator, CCTUInfo, |
| 7416 | CodeCompletionContext::CCC_Recovery); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 7417 | if (!CodeCompleter || CodeCompleter->includeGlobals()) { |
| 7418 | CodeCompletionDeclConsumer Consumer(Builder, |
| 7419 | Context.getTranslationUnitDecl()); |
| 7420 | LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName, |
| 7421 | Consumer); |
| 7422 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 7423 | |
| 7424 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
Douglas Gregor | 3644d97 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 7425 | AddMacroResults(PP, Builder, true); |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 7426 | |
| 7427 | Results.clear(); |
| 7428 | Results.insert(Results.end(), |
| 7429 | Builder.data(), Builder.data() + Builder.size()); |
| 7430 | } |