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" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Lookup.h" |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Overload.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 16 | #include "clang/Sema/CodeCompleteConsumer.h" |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ExternalSemaSource.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Scope.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ScopeInfo.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 23 | #include "clang/Lex/MacroInfo.h" |
| 24 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 6a68403 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringSwitch.h" |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 30 | #include <list> |
| 31 | #include <map> |
| 32 | #include <vector> |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 35 | using namespace sema; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 36 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 37 | namespace { |
| 38 | /// \brief A container of code-completion results. |
| 39 | class ResultBuilder { |
| 40 | public: |
| 41 | /// \brief The type of a name-lookup filter, which can be provided to the |
| 42 | /// name-lookup routines to specify which declarations should be included in |
| 43 | /// the result set (when it returns true) and which declarations should be |
| 44 | /// filtered out (returns false). |
| 45 | typedef bool (ResultBuilder::*LookupFilter)(NamedDecl *) const; |
| 46 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 47 | typedef CodeCompletionResult Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 48 | |
| 49 | private: |
| 50 | /// \brief The actual results we have found. |
| 51 | std::vector<Result> Results; |
| 52 | |
| 53 | /// \brief A record of all of the declarations we have found and placed |
| 54 | /// into the result set, used to ensure that no declaration ever gets into |
| 55 | /// the result set twice. |
| 56 | llvm::SmallPtrSet<Decl*, 16> AllDeclsFound; |
| 57 | |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 58 | typedef std::pair<NamedDecl *, unsigned> DeclIndexPair; |
| 59 | |
| 60 | /// \brief An entry in the shadow map, which is optimized to store |
| 61 | /// a single (declaration, index) mapping (the common case) but |
| 62 | /// can also store a list of (declaration, index) mappings. |
| 63 | class ShadowMapEntry { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 64 | typedef SmallVector<DeclIndexPair, 4> DeclIndexPairVector; |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 65 | |
| 66 | /// \brief Contains either the solitary NamedDecl * or a vector |
| 67 | /// of (declaration, index) pairs. |
| 68 | llvm::PointerUnion<NamedDecl *, DeclIndexPairVector*> DeclOrVector; |
| 69 | |
| 70 | /// \brief When the entry contains a single declaration, this is |
| 71 | /// the index associated with that entry. |
| 72 | unsigned SingleDeclIndex; |
| 73 | |
| 74 | public: |
| 75 | ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { } |
| 76 | |
| 77 | void Add(NamedDecl *ND, unsigned Index) { |
| 78 | if (DeclOrVector.isNull()) { |
| 79 | // 0 - > 1 elements: just set the single element information. |
| 80 | DeclOrVector = ND; |
| 81 | SingleDeclIndex = Index; |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | if (NamedDecl *PrevND = DeclOrVector.dyn_cast<NamedDecl *>()) { |
| 86 | // 1 -> 2 elements: create the vector of results and push in the |
| 87 | // existing declaration. |
| 88 | DeclIndexPairVector *Vec = new DeclIndexPairVector; |
| 89 | Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex)); |
| 90 | DeclOrVector = Vec; |
| 91 | } |
| 92 | |
| 93 | // Add the new element to the end of the vector. |
| 94 | DeclOrVector.get<DeclIndexPairVector*>()->push_back( |
| 95 | DeclIndexPair(ND, Index)); |
| 96 | } |
| 97 | |
| 98 | void Destroy() { |
| 99 | if (DeclIndexPairVector *Vec |
| 100 | = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) { |
| 101 | delete Vec; |
| 102 | DeclOrVector = ((NamedDecl *)0); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Iteration. |
| 107 | class iterator; |
| 108 | iterator begin() const; |
| 109 | iterator end() const; |
| 110 | }; |
| 111 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 112 | /// \brief A mapping from declaration names to the declarations that have |
| 113 | /// this name within a particular scope and their index within the list of |
| 114 | /// results. |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 115 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 116 | |
| 117 | /// \brief The semantic analysis object for which results are being |
| 118 | /// produced. |
| 119 | Sema &SemaRef; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 120 | |
| 121 | /// \brief The allocator used to allocate new code-completion strings. |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 122 | CodeCompletionAllocator &Allocator; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 123 | |
| 124 | /// \brief If non-NULL, a filter function used to remove any code-completion |
| 125 | /// results that are not desirable. |
| 126 | LookupFilter Filter; |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 127 | |
| 128 | /// \brief Whether we should allow declarations as |
| 129 | /// nested-name-specifiers that would otherwise be filtered out. |
| 130 | bool AllowNestedNameSpecifiers; |
| 131 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 132 | /// \brief If set, the type that we would prefer our resulting value |
| 133 | /// declarations to have. |
| 134 | /// |
| 135 | /// Closely matching the preferred type gives a boost to a result's |
| 136 | /// priority. |
| 137 | CanQualType PreferredType; |
| 138 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 139 | /// \brief A list of shadow maps, which is used to model name hiding at |
| 140 | /// different levels of, e.g., the inheritance hierarchy. |
| 141 | std::list<ShadowMap> ShadowMaps; |
| 142 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 143 | /// \brief If we're potentially referring to a C++ member function, the set |
| 144 | /// of qualifiers applied to the object type. |
| 145 | Qualifiers ObjectTypeQualifiers; |
| 146 | |
| 147 | /// \brief Whether the \p ObjectTypeQualifiers field is active. |
| 148 | bool HasObjectTypeQualifiers; |
| 149 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 150 | /// \brief The selector that we prefer. |
| 151 | Selector PreferredSelector; |
| 152 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 153 | /// \brief The completion context in which we are gathering results. |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 154 | CodeCompletionContext CompletionContext; |
| 155 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 156 | /// \brief If we are in an instance method definition, the @implementation |
| 157 | /// object. |
| 158 | ObjCImplementationDecl *ObjCImplementation; |
| 159 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 160 | void AdjustResultPriorityForDecl(Result &R); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 161 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 162 | void MaybeAddConstructorResults(Result R); |
| 163 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 164 | public: |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 165 | explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 166 | const CodeCompletionContext &CompletionContext, |
| 167 | LookupFilter Filter = 0) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 168 | : SemaRef(SemaRef), Allocator(Allocator), Filter(Filter), |
| 169 | AllowNestedNameSpecifiers(false), HasObjectTypeQualifiers(false), |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 170 | CompletionContext(CompletionContext), |
| 171 | ObjCImplementation(0) |
| 172 | { |
| 173 | // If this is an Objective-C instance method definition, dig out the |
| 174 | // corresponding implementation. |
| 175 | switch (CompletionContext.getKind()) { |
| 176 | case CodeCompletionContext::CCC_Expression: |
| 177 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
| 178 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
| 179 | case CodeCompletionContext::CCC_Statement: |
| 180 | case CodeCompletionContext::CCC_Recovery: |
| 181 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) |
| 182 | if (Method->isInstanceMethod()) |
| 183 | if (ObjCInterfaceDecl *Interface = Method->getClassInterface()) |
| 184 | ObjCImplementation = Interface->getImplementation(); |
| 185 | break; |
| 186 | |
| 187 | default: |
| 188 | break; |
| 189 | } |
| 190 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 191 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 192 | /// \brief Whether we should include code patterns in the completion |
| 193 | /// results. |
| 194 | bool includeCodePatterns() const { |
| 195 | return SemaRef.CodeCompleter && |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 196 | SemaRef.CodeCompleter->includeCodePatterns(); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 199 | /// \brief Set the filter used for code-completion results. |
| 200 | void setFilter(LookupFilter Filter) { |
| 201 | this->Filter = Filter; |
| 202 | } |
| 203 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 204 | Result *data() { return Results.empty()? 0 : &Results.front(); } |
| 205 | unsigned size() const { return Results.size(); } |
| 206 | bool empty() const { return Results.empty(); } |
| 207 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 208 | /// \brief Specify the preferred type. |
| 209 | void setPreferredType(QualType T) { |
| 210 | PreferredType = SemaRef.Context.getCanonicalType(T); |
| 211 | } |
| 212 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 213 | /// \brief Set the cv-qualifiers on the object type, for us in filtering |
| 214 | /// calls to member functions. |
| 215 | /// |
| 216 | /// When there are qualifiers in this set, they will be used to filter |
| 217 | /// out member functions that aren't available (because there will be a |
| 218 | /// cv-qualifier mismatch) or prefer functions with an exact qualifier |
| 219 | /// match. |
| 220 | void setObjectTypeQualifiers(Qualifiers Quals) { |
| 221 | ObjectTypeQualifiers = Quals; |
| 222 | HasObjectTypeQualifiers = true; |
| 223 | } |
| 224 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 225 | /// \brief Set the preferred selector. |
| 226 | /// |
| 227 | /// When an Objective-C method declaration result is added, and that |
| 228 | /// method's selector matches this preferred selector, we give that method |
| 229 | /// a slight priority boost. |
| 230 | void setPreferredSelector(Selector Sel) { |
| 231 | PreferredSelector = Sel; |
| 232 | } |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 233 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 234 | /// \brief Retrieve the code-completion context for which results are |
| 235 | /// being collected. |
| 236 | const CodeCompletionContext &getCompletionContext() const { |
| 237 | return CompletionContext; |
| 238 | } |
| 239 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 240 | /// \brief Specify whether nested-name-specifiers are allowed. |
| 241 | void allowNestedNameSpecifiers(bool Allow = true) { |
| 242 | AllowNestedNameSpecifiers = Allow; |
| 243 | } |
| 244 | |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 245 | /// \brief Return the semantic analysis object for which we are collecting |
| 246 | /// code completion results. |
| 247 | Sema &getSema() const { return SemaRef; } |
| 248 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 249 | /// \brief Retrieve the allocator used to allocate code completion strings. |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 250 | CodeCompletionAllocator &getAllocator() const { return Allocator; } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 251 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 252 | /// \brief Determine whether the given declaration is at all interesting |
| 253 | /// as a code-completion result. |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 254 | /// |
| 255 | /// \param ND the declaration that we are inspecting. |
| 256 | /// |
| 257 | /// \param AsNestedNameSpecifier will be set true if this declaration is |
| 258 | /// only interesting when it is a nested-name-specifier. |
| 259 | bool isInterestingDecl(NamedDecl *ND, bool &AsNestedNameSpecifier) const; |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 260 | |
| 261 | /// \brief Check whether the result is hidden by the Hiding declaration. |
| 262 | /// |
| 263 | /// \returns true if the result is hidden and cannot be found, false if |
| 264 | /// the hidden result could still be found. When false, \p R may be |
| 265 | /// modified to describe how the result can be found (e.g., via extra |
| 266 | /// qualification). |
| 267 | bool CheckHiddenResult(Result &R, DeclContext *CurContext, |
| 268 | NamedDecl *Hiding); |
| 269 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 270 | /// \brief Add a new result to this result set (if it isn't already in one |
| 271 | /// of the shadow maps), or replace an existing result (for, e.g., a |
| 272 | /// redeclaration). |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 273 | /// |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 274 | /// \param CurContext the result to add (if it is unique). |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 275 | /// |
| 276 | /// \param R the context in which this result will be named. |
| 277 | void MaybeAddResult(Result R, DeclContext *CurContext = 0); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 278 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 279 | /// \brief Add a new result to this result set, where we already know |
| 280 | /// the hiding declation (if any). |
| 281 | /// |
| 282 | /// \param R the result to add (if it is unique). |
| 283 | /// |
| 284 | /// \param CurContext the context in which this result will be named. |
| 285 | /// |
| 286 | /// \param Hiding the declaration that hides the result. |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 287 | /// |
| 288 | /// \param InBaseClass whether the result was found in a base |
| 289 | /// class of the searched context. |
| 290 | void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding, |
| 291 | bool InBaseClass); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 292 | |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 293 | /// \brief Add a new non-declaration result to this result set. |
| 294 | void AddResult(Result R); |
| 295 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 296 | /// \brief Enter into a new scope. |
| 297 | void EnterNewScope(); |
| 298 | |
| 299 | /// \brief Exit from the current scope. |
| 300 | void ExitScope(); |
| 301 | |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 302 | /// \brief Ignore this declaration, if it is seen again. |
| 303 | void Ignore(Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); } |
| 304 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 305 | /// \name Name lookup predicates |
| 306 | /// |
| 307 | /// These predicates can be passed to the name lookup functions to filter the |
| 308 | /// results of name lookup. All of the predicates have the same type, so that |
| 309 | /// |
| 310 | //@{ |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 311 | bool IsOrdinaryName(NamedDecl *ND) const; |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 312 | bool IsOrdinaryNonTypeName(NamedDecl *ND) const; |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 313 | bool IsIntegralConstantValue(NamedDecl *ND) const; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 314 | bool IsOrdinaryNonValueName(NamedDecl *ND) const; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 315 | bool IsNestedNameSpecifier(NamedDecl *ND) const; |
| 316 | bool IsEnum(NamedDecl *ND) const; |
| 317 | bool IsClassOrStruct(NamedDecl *ND) const; |
| 318 | bool IsUnion(NamedDecl *ND) const; |
| 319 | bool IsNamespace(NamedDecl *ND) const; |
| 320 | bool IsNamespaceOrAlias(NamedDecl *ND) const; |
| 321 | bool IsType(NamedDecl *ND) const; |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 322 | bool IsMember(NamedDecl *ND) const; |
Douglas Gregor | 80f4f4c | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 323 | bool IsObjCIvar(NamedDecl *ND) const; |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 324 | bool IsObjCMessageReceiver(NamedDecl *ND) const; |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 325 | bool IsObjCCollection(NamedDecl *ND) const; |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 326 | bool IsImpossibleToSatisfy(NamedDecl *ND) const; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 327 | //@} |
| 328 | }; |
| 329 | } |
| 330 | |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 331 | class ResultBuilder::ShadowMapEntry::iterator { |
| 332 | llvm::PointerUnion<NamedDecl*, const DeclIndexPair*> DeclOrIterator; |
| 333 | unsigned SingleDeclIndex; |
| 334 | |
| 335 | public: |
| 336 | typedef DeclIndexPair value_type; |
| 337 | typedef value_type reference; |
| 338 | typedef std::ptrdiff_t difference_type; |
| 339 | typedef std::input_iterator_tag iterator_category; |
| 340 | |
| 341 | class pointer { |
| 342 | DeclIndexPair Value; |
| 343 | |
| 344 | public: |
| 345 | pointer(const DeclIndexPair &Value) : Value(Value) { } |
| 346 | |
| 347 | const DeclIndexPair *operator->() const { |
| 348 | return &Value; |
| 349 | } |
| 350 | }; |
| 351 | |
| 352 | iterator() : DeclOrIterator((NamedDecl *)0), SingleDeclIndex(0) { } |
| 353 | |
| 354 | iterator(NamedDecl *SingleDecl, unsigned Index) |
| 355 | : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { } |
| 356 | |
| 357 | iterator(const DeclIndexPair *Iterator) |
| 358 | : DeclOrIterator(Iterator), SingleDeclIndex(0) { } |
| 359 | |
| 360 | iterator &operator++() { |
| 361 | if (DeclOrIterator.is<NamedDecl *>()) { |
| 362 | DeclOrIterator = (NamedDecl *)0; |
| 363 | SingleDeclIndex = 0; |
| 364 | return *this; |
| 365 | } |
| 366 | |
| 367 | const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>(); |
| 368 | ++I; |
| 369 | DeclOrIterator = I; |
| 370 | return *this; |
| 371 | } |
| 372 | |
Chris Lattner | 66392d4 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 373 | /*iterator operator++(int) { |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 374 | iterator tmp(*this); |
| 375 | ++(*this); |
| 376 | return tmp; |
Chris Lattner | 66392d4 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 377 | }*/ |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 378 | |
| 379 | reference operator*() const { |
| 380 | if (NamedDecl *ND = DeclOrIterator.dyn_cast<NamedDecl *>()) |
| 381 | return reference(ND, SingleDeclIndex); |
| 382 | |
Douglas Gregor | d490f95 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 383 | return *DeclOrIterator.get<const DeclIndexPair*>(); |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | pointer operator->() const { |
| 387 | return pointer(**this); |
| 388 | } |
| 389 | |
| 390 | friend bool operator==(const iterator &X, const iterator &Y) { |
Douglas Gregor | d490f95 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 391 | return X.DeclOrIterator.getOpaqueValue() |
| 392 | == Y.DeclOrIterator.getOpaqueValue() && |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 393 | X.SingleDeclIndex == Y.SingleDeclIndex; |
| 394 | } |
| 395 | |
| 396 | friend bool operator!=(const iterator &X, const iterator &Y) { |
Douglas Gregor | d490f95 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 397 | return !(X == Y); |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 398 | } |
| 399 | }; |
| 400 | |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 401 | ResultBuilder::ShadowMapEntry::iterator |
| 402 | ResultBuilder::ShadowMapEntry::begin() const { |
| 403 | if (DeclOrVector.isNull()) |
| 404 | return iterator(); |
| 405 | |
| 406 | if (NamedDecl *ND = DeclOrVector.dyn_cast<NamedDecl *>()) |
| 407 | return iterator(ND, SingleDeclIndex); |
| 408 | |
| 409 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin()); |
| 410 | } |
| 411 | |
| 412 | ResultBuilder::ShadowMapEntry::iterator |
| 413 | ResultBuilder::ShadowMapEntry::end() const { |
| 414 | if (DeclOrVector.is<NamedDecl *>() || DeclOrVector.isNull()) |
| 415 | return iterator(); |
| 416 | |
| 417 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end()); |
| 418 | } |
| 419 | |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 420 | /// \brief Compute the qualification required to get from the current context |
| 421 | /// (\p CurContext) to the target context (\p TargetContext). |
| 422 | /// |
| 423 | /// \param Context the AST context in which the qualification will be used. |
| 424 | /// |
| 425 | /// \param CurContext the context where an entity is being named, which is |
| 426 | /// typically based on the current scope. |
| 427 | /// |
| 428 | /// \param TargetContext the context in which the named entity actually |
| 429 | /// resides. |
| 430 | /// |
| 431 | /// \returns a nested name specifier that refers into the target context, or |
| 432 | /// NULL if no qualification is needed. |
| 433 | static NestedNameSpecifier * |
| 434 | getRequiredQualification(ASTContext &Context, |
| 435 | DeclContext *CurContext, |
| 436 | DeclContext *TargetContext) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 437 | SmallVector<DeclContext *, 4> TargetParents; |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 438 | |
| 439 | for (DeclContext *CommonAncestor = TargetContext; |
| 440 | CommonAncestor && !CommonAncestor->Encloses(CurContext); |
| 441 | CommonAncestor = CommonAncestor->getLookupParent()) { |
| 442 | if (CommonAncestor->isTransparentContext() || |
| 443 | CommonAncestor->isFunctionOrMethod()) |
| 444 | continue; |
| 445 | |
| 446 | TargetParents.push_back(CommonAncestor); |
| 447 | } |
| 448 | |
| 449 | NestedNameSpecifier *Result = 0; |
| 450 | while (!TargetParents.empty()) { |
| 451 | DeclContext *Parent = TargetParents.back(); |
| 452 | TargetParents.pop_back(); |
| 453 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 454 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) { |
| 455 | if (!Namespace->getIdentifier()) |
| 456 | continue; |
| 457 | |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 458 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 459 | } |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 460 | else if (TagDecl *TD = dyn_cast<TagDecl>(Parent)) |
| 461 | Result = NestedNameSpecifier::Create(Context, Result, |
| 462 | false, |
| 463 | Context.getTypeDeclType(TD).getTypePtr()); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 464 | } |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 465 | return Result; |
| 466 | } |
| 467 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 468 | bool ResultBuilder::isInterestingDecl(NamedDecl *ND, |
| 469 | bool &AsNestedNameSpecifier) const { |
| 470 | AsNestedNameSpecifier = false; |
| 471 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 472 | ND = ND->getUnderlyingDecl(); |
| 473 | unsigned IDNS = ND->getIdentifierNamespace(); |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 474 | |
| 475 | // Skip unnamed entities. |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 476 | if (!ND->getDeclName()) |
| 477 | return false; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 478 | |
| 479 | // Friend declarations and declarations introduced due to friends are never |
| 480 | // added as results. |
John McCall | 92b7f70 | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 481 | if (IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend)) |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 482 | return false; |
| 483 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 484 | // Class template (partial) specializations are never added as results. |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 485 | if (isa<ClassTemplateSpecializationDecl>(ND) || |
| 486 | isa<ClassTemplatePartialSpecializationDecl>(ND)) |
| 487 | return false; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 488 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 489 | // Using declarations themselves are never added as results. |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 490 | if (isa<UsingDecl>(ND)) |
| 491 | return false; |
| 492 | |
| 493 | // Some declarations have reserved names that we don't want to ever show. |
| 494 | if (const IdentifierInfo *Id = ND->getIdentifier()) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 495 | // __va_list_tag is a freak of nature. Find it and skip it. |
| 496 | if (Id->isStr("__va_list_tag") || Id->isStr("__builtin_va_list")) |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 497 | return false; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 498 | |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 499 | // Filter out names reserved for the implementation (C99 7.1.3, |
Douglas Gregor | 797efb5 | 2010-07-14 17:44:04 +0000 | [diff] [blame] | 500 | // C++ [lib.global.names]) if they come from a system header. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 501 | // |
| 502 | // FIXME: Add predicate for this. |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 503 | if (Id->getLength() >= 2) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 504 | const char *Name = Id->getNameStart(); |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 505 | if (Name[0] == '_' && |
Douglas Gregor | 797efb5 | 2010-07-14 17:44:04 +0000 | [diff] [blame] | 506 | (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z')) && |
| 507 | (ND->getLocation().isInvalid() || |
| 508 | SemaRef.SourceMgr.isInSystemHeader( |
| 509 | SemaRef.SourceMgr.getSpellingLoc(ND->getLocation())))) |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 510 | return false; |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 511 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 512 | } |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 513 | |
Douglas Gregor | 9b0ba87 | 2010-11-09 03:59:40 +0000 | [diff] [blame] | 514 | // Skip out-of-line declarations and definitions. |
| 515 | // NOTE: Unless it's an Objective-C property, method, or ivar, where |
| 516 | // the contexts can be messy. |
| 517 | if (!ND->getDeclContext()->Equals(ND->getLexicalDeclContext()) && |
| 518 | !(isa<ObjCPropertyDecl>(ND) || isa<ObjCIvarDecl>(ND) || |
| 519 | isa<ObjCMethodDecl>(ND))) |
| 520 | return false; |
| 521 | |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 522 | if (Filter == &ResultBuilder::IsNestedNameSpecifier || |
| 523 | ((isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) && |
| 524 | Filter != &ResultBuilder::IsNamespace && |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 525 | Filter != &ResultBuilder::IsNamespaceOrAlias && |
| 526 | Filter != 0)) |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 527 | AsNestedNameSpecifier = true; |
| 528 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 529 | // Filter out any unwanted results. |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 530 | if (Filter && !(this->*Filter)(ND)) { |
| 531 | // Check whether it is interesting as a nested-name-specifier. |
| 532 | if (AllowNestedNameSpecifiers && SemaRef.getLangOptions().CPlusPlus && |
| 533 | IsNestedNameSpecifier(ND) && |
| 534 | (Filter != &ResultBuilder::IsMember || |
| 535 | (isa<CXXRecordDecl>(ND) && |
| 536 | cast<CXXRecordDecl>(ND)->isInjectedClassName()))) { |
| 537 | AsNestedNameSpecifier = true; |
| 538 | return true; |
| 539 | } |
| 540 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 541 | return false; |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 542 | } |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 543 | // ... then it must be interesting! |
| 544 | return true; |
| 545 | } |
| 546 | |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 547 | bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext, |
| 548 | NamedDecl *Hiding) { |
| 549 | // In C, there is no way to refer to a hidden name. |
| 550 | // FIXME: This isn't true; we can find a tag name hidden by an ordinary |
| 551 | // name if we introduce the tag type. |
| 552 | if (!SemaRef.getLangOptions().CPlusPlus) |
| 553 | return true; |
| 554 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 555 | DeclContext *HiddenCtx = R.Declaration->getDeclContext()->getRedeclContext(); |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 556 | |
| 557 | // There is no way to qualify a name declared in a function or method. |
| 558 | if (HiddenCtx->isFunctionOrMethod()) |
| 559 | return true; |
| 560 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 561 | if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext()) |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 562 | return true; |
| 563 | |
| 564 | // We can refer to the result with the appropriate qualification. Do it. |
| 565 | R.Hidden = true; |
| 566 | R.QualifierIsInformative = false; |
| 567 | |
| 568 | if (!R.Qualifier) |
| 569 | R.Qualifier = getRequiredQualification(SemaRef.Context, |
| 570 | CurContext, |
| 571 | R.Declaration->getDeclContext()); |
| 572 | return false; |
| 573 | } |
| 574 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 575 | /// \brief A simplified classification of types used to determine whether two |
| 576 | /// types are "similar enough" when adjusting priorities. |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 577 | SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 578 | switch (T->getTypeClass()) { |
| 579 | case Type::Builtin: |
| 580 | switch (cast<BuiltinType>(T)->getKind()) { |
| 581 | case BuiltinType::Void: |
| 582 | return STC_Void; |
| 583 | |
| 584 | case BuiltinType::NullPtr: |
| 585 | return STC_Pointer; |
| 586 | |
| 587 | case BuiltinType::Overload: |
| 588 | case BuiltinType::Dependent: |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 589 | return STC_Other; |
| 590 | |
| 591 | case BuiltinType::ObjCId: |
| 592 | case BuiltinType::ObjCClass: |
| 593 | case BuiltinType::ObjCSel: |
| 594 | return STC_ObjectiveC; |
| 595 | |
| 596 | default: |
| 597 | return STC_Arithmetic; |
| 598 | } |
| 599 | return STC_Other; |
| 600 | |
| 601 | case Type::Complex: |
| 602 | return STC_Arithmetic; |
| 603 | |
| 604 | case Type::Pointer: |
| 605 | return STC_Pointer; |
| 606 | |
| 607 | case Type::BlockPointer: |
| 608 | return STC_Block; |
| 609 | |
| 610 | case Type::LValueReference: |
| 611 | case Type::RValueReference: |
| 612 | return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType()); |
| 613 | |
| 614 | case Type::ConstantArray: |
| 615 | case Type::IncompleteArray: |
| 616 | case Type::VariableArray: |
| 617 | case Type::DependentSizedArray: |
| 618 | return STC_Array; |
| 619 | |
| 620 | case Type::DependentSizedExtVector: |
| 621 | case Type::Vector: |
| 622 | case Type::ExtVector: |
| 623 | return STC_Arithmetic; |
| 624 | |
| 625 | case Type::FunctionProto: |
| 626 | case Type::FunctionNoProto: |
| 627 | return STC_Function; |
| 628 | |
| 629 | case Type::Record: |
| 630 | return STC_Record; |
| 631 | |
| 632 | case Type::Enum: |
| 633 | return STC_Arithmetic; |
| 634 | |
| 635 | case Type::ObjCObject: |
| 636 | case Type::ObjCInterface: |
| 637 | case Type::ObjCObjectPointer: |
| 638 | return STC_ObjectiveC; |
| 639 | |
| 640 | default: |
| 641 | return STC_Other; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | /// \brief Get the type that a given expression will have if this declaration |
| 646 | /// is used as an expression in its "typical" code-completion form. |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 647 | QualType clang::getDeclUsageType(ASTContext &C, NamedDecl *ND) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 648 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 649 | |
| 650 | if (TypeDecl *Type = dyn_cast<TypeDecl>(ND)) |
| 651 | return C.getTypeDeclType(Type); |
| 652 | if (ObjCInterfaceDecl *Iface = dyn_cast<ObjCInterfaceDecl>(ND)) |
| 653 | return C.getObjCInterfaceType(Iface); |
| 654 | |
| 655 | QualType T; |
| 656 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 657 | T = Function->getCallResultType(); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 658 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 659 | T = Method->getSendResultType(); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 660 | else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 661 | T = FunTmpl->getTemplatedDecl()->getCallResultType(); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 662 | else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
| 663 | T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext())); |
| 664 | else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
| 665 | T = Property->getType(); |
| 666 | else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND)) |
| 667 | T = Value->getType(); |
| 668 | else |
| 669 | return QualType(); |
Douglas Gregor | 3e64d56 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 670 | |
| 671 | // Dig through references, function pointers, and block pointers to |
| 672 | // get down to the likely type of an expression when the entity is |
| 673 | // used. |
| 674 | do { |
| 675 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) { |
| 676 | T = Ref->getPointeeType(); |
| 677 | continue; |
| 678 | } |
| 679 | |
| 680 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 681 | if (Pointer->getPointeeType()->isFunctionType()) { |
| 682 | T = Pointer->getPointeeType(); |
| 683 | continue; |
| 684 | } |
| 685 | |
| 686 | break; |
| 687 | } |
| 688 | |
| 689 | if (const BlockPointerType *Block = T->getAs<BlockPointerType>()) { |
| 690 | T = Block->getPointeeType(); |
| 691 | continue; |
| 692 | } |
| 693 | |
| 694 | if (const FunctionType *Function = T->getAs<FunctionType>()) { |
| 695 | T = Function->getResultType(); |
| 696 | continue; |
| 697 | } |
| 698 | |
| 699 | break; |
| 700 | } while (true); |
| 701 | |
| 702 | return T; |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 705 | void ResultBuilder::AdjustResultPriorityForDecl(Result &R) { |
| 706 | // If this is an Objective-C method declaration whose selector matches our |
| 707 | // preferred selector, give it a priority boost. |
| 708 | if (!PreferredSelector.isNull()) |
| 709 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration)) |
| 710 | if (PreferredSelector == Method->getSelector()) |
| 711 | R.Priority += CCD_SelectorMatch; |
Douglas Gregor | 08f43cd | 2010-09-20 23:11:55 +0000 | [diff] [blame] | 712 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 713 | // If we have a preferred type, adjust the priority for results with exactly- |
| 714 | // matching or nearly-matching types. |
| 715 | if (!PreferredType.isNull()) { |
| 716 | QualType T = getDeclUsageType(SemaRef.Context, R.Declaration); |
| 717 | if (!T.isNull()) { |
| 718 | CanQualType TC = SemaRef.Context.getCanonicalType(T); |
| 719 | // Check for exactly-matching types (modulo qualifiers). |
| 720 | if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) |
| 721 | R.Priority /= CCF_ExactTypeMatch; |
| 722 | // Check for nearly-matching types, based on classification of each. |
| 723 | else if ((getSimplifiedTypeClass(PreferredType) |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 724 | == getSimplifiedTypeClass(TC)) && |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 725 | !(PreferredType->isEnumeralType() && TC->isEnumeralType())) |
| 726 | R.Priority /= CCF_SimilarTypeMatch; |
| 727 | } |
| 728 | } |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 731 | void ResultBuilder::MaybeAddConstructorResults(Result R) { |
| 732 | if (!SemaRef.getLangOptions().CPlusPlus || !R.Declaration || |
| 733 | !CompletionContext.wantConstructorResults()) |
| 734 | return; |
| 735 | |
| 736 | ASTContext &Context = SemaRef.Context; |
| 737 | NamedDecl *D = R.Declaration; |
| 738 | CXXRecordDecl *Record = 0; |
| 739 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) |
| 740 | Record = ClassTemplate->getTemplatedDecl(); |
| 741 | else if ((Record = dyn_cast<CXXRecordDecl>(D))) { |
| 742 | // Skip specializations and partial specializations. |
| 743 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 744 | return; |
| 745 | } else { |
| 746 | // There are no constructors here. |
| 747 | return; |
| 748 | } |
| 749 | |
| 750 | Record = Record->getDefinition(); |
| 751 | if (!Record) |
| 752 | return; |
| 753 | |
| 754 | |
| 755 | QualType RecordTy = Context.getTypeDeclType(Record); |
| 756 | DeclarationName ConstructorName |
| 757 | = Context.DeclarationNames.getCXXConstructorName( |
| 758 | Context.getCanonicalType(RecordTy)); |
| 759 | for (DeclContext::lookup_result Ctors = Record->lookup(ConstructorName); |
| 760 | Ctors.first != Ctors.second; ++Ctors.first) { |
| 761 | R.Declaration = *Ctors.first; |
| 762 | R.CursorKind = getCursorKindForDecl(R.Declaration); |
| 763 | Results.push_back(R); |
| 764 | } |
| 765 | } |
| 766 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 767 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { |
| 768 | assert(!ShadowMaps.empty() && "Must enter into a results scope"); |
| 769 | |
| 770 | if (R.Kind != Result::RK_Declaration) { |
| 771 | // For non-declaration results, just add the result. |
| 772 | Results.push_back(R); |
| 773 | return; |
| 774 | } |
| 775 | |
| 776 | // Look through using declarations. |
| 777 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
| 778 | MaybeAddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext); |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
| 783 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 784 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 785 | bool AsNestedNameSpecifier = false; |
| 786 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 787 | return; |
| 788 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 789 | // C++ constructors are never found by name lookup. |
| 790 | if (isa<CXXConstructorDecl>(R.Declaration)) |
| 791 | return; |
| 792 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 793 | ShadowMap &SMap = ShadowMaps.back(); |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 794 | ShadowMapEntry::iterator I, IEnd; |
| 795 | ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName()); |
| 796 | if (NamePos != SMap.end()) { |
| 797 | I = NamePos->second.begin(); |
| 798 | IEnd = NamePos->second.end(); |
| 799 | } |
| 800 | |
| 801 | for (; I != IEnd; ++I) { |
| 802 | NamedDecl *ND = I->first; |
| 803 | unsigned Index = I->second; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 804 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 805 | // This is a redeclaration. Always pick the newer declaration. |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 806 | Results[Index].Declaration = R.Declaration; |
| 807 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 808 | // We're done. |
| 809 | return; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | // This is a new declaration in this scope. However, check whether this |
| 814 | // declaration name is hidden by a similarly-named declaration in an outer |
| 815 | // scope. |
| 816 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 817 | --SMEnd; |
| 818 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 819 | ShadowMapEntry::iterator I, IEnd; |
| 820 | ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName()); |
| 821 | if (NamePos != SM->end()) { |
| 822 | I = NamePos->second.begin(); |
| 823 | IEnd = NamePos->second.end(); |
| 824 | } |
| 825 | for (; I != IEnd; ++I) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 826 | // A tag declaration does not hide a non-tag declaration. |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 827 | if (I->first->hasTagIdentifierNamespace() && |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 828 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 829 | Decl::IDNS_ObjCProtocol))) |
| 830 | continue; |
| 831 | |
| 832 | // Protocols are in distinct namespaces from everything else. |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 833 | if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 834 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 835 | I->first->getIdentifierNamespace() != IDNS) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 836 | continue; |
| 837 | |
| 838 | // 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] | 839 | if (CheckHiddenResult(R, CurContext, I->first)) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 840 | return; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 841 | |
| 842 | break; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | // Make sure that any given declaration only shows up in the result set once. |
| 847 | if (!AllDeclsFound.insert(CanonDecl)) |
| 848 | return; |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 849 | |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 850 | // If the filter is for nested-name-specifiers, then this result starts a |
| 851 | // nested-name-specifier. |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 852 | if (AsNestedNameSpecifier) { |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 853 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 854 | R.Priority = CCP_NestedNameSpecifier; |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 855 | } else |
| 856 | AdjustResultPriorityForDecl(R); |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 857 | |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 858 | // If this result is supposed to have an informative qualifier, add one. |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 859 | if (R.QualifierIsInformative && !R.Qualifier && |
| 860 | !R.StartsNestedNameSpecifier) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 861 | DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 862 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 863 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace); |
| 864 | else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
| 865 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false, |
| 866 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
| 867 | else |
| 868 | R.QualifierIsInformative = false; |
| 869 | } |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 870 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 871 | // Insert this result into the set of results and into the current shadow |
| 872 | // map. |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 873 | SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 874 | Results.push_back(R); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 875 | |
| 876 | if (!AsNestedNameSpecifier) |
| 877 | MaybeAddConstructorResults(R); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 880 | void ResultBuilder::AddResult(Result R, DeclContext *CurContext, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 881 | NamedDecl *Hiding, bool InBaseClass = false) { |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 882 | if (R.Kind != Result::RK_Declaration) { |
| 883 | // For non-declaration results, just add the result. |
| 884 | Results.push_back(R); |
| 885 | return; |
| 886 | } |
| 887 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 888 | // Look through using declarations. |
| 889 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
| 890 | AddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext, Hiding); |
| 891 | return; |
| 892 | } |
| 893 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 894 | bool AsNestedNameSpecifier = false; |
| 895 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 896 | return; |
| 897 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 898 | // C++ constructors are never found by name lookup. |
| 899 | if (isa<CXXConstructorDecl>(R.Declaration)) |
| 900 | return; |
| 901 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 902 | if (Hiding && CheckHiddenResult(R, CurContext, Hiding)) |
| 903 | return; |
| 904 | |
| 905 | // Make sure that any given declaration only shows up in the result set once. |
| 906 | if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl())) |
| 907 | return; |
| 908 | |
| 909 | // If the filter is for nested-name-specifiers, then this result starts a |
| 910 | // nested-name-specifier. |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 911 | if (AsNestedNameSpecifier) { |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 912 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 913 | R.Priority = CCP_NestedNameSpecifier; |
| 914 | } |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 915 | else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass && |
| 916 | isa<CXXRecordDecl>(R.Declaration->getDeclContext() |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 917 | ->getRedeclContext())) |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 918 | R.QualifierIsInformative = true; |
| 919 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 920 | // If this result is supposed to have an informative qualifier, add one. |
| 921 | if (R.QualifierIsInformative && !R.Qualifier && |
| 922 | !R.StartsNestedNameSpecifier) { |
| 923 | DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 924 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 925 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace); |
| 926 | else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
| 927 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false, |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 928 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 929 | else |
| 930 | R.QualifierIsInformative = false; |
| 931 | } |
| 932 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 933 | // Adjust the priority if this result comes from a base class. |
| 934 | if (InBaseClass) |
| 935 | R.Priority += CCD_InBaseClass; |
| 936 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 937 | AdjustResultPriorityForDecl(R); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 938 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 939 | if (HasObjectTypeQualifiers) |
| 940 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(R.Declaration)) |
| 941 | if (Method->isInstance()) { |
| 942 | Qualifiers MethodQuals |
| 943 | = Qualifiers::fromCVRMask(Method->getTypeQualifiers()); |
| 944 | if (ObjectTypeQualifiers == MethodQuals) |
| 945 | R.Priority += CCD_ObjectQualifierMatch; |
| 946 | else if (ObjectTypeQualifiers - MethodQuals) { |
| 947 | // The method cannot be invoked, because doing so would drop |
| 948 | // qualifiers. |
| 949 | return; |
| 950 | } |
| 951 | } |
| 952 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 953 | // Insert this result into the set of results. |
| 954 | Results.push_back(R); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 955 | |
| 956 | if (!AsNestedNameSpecifier) |
| 957 | MaybeAddConstructorResults(R); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 960 | void ResultBuilder::AddResult(Result R) { |
| 961 | assert(R.Kind != Result::RK_Declaration && |
| 962 | "Declaration results need more context"); |
| 963 | Results.push_back(R); |
| 964 | } |
| 965 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 966 | /// \brief Enter into a new scope. |
| 967 | void ResultBuilder::EnterNewScope() { |
| 968 | ShadowMaps.push_back(ShadowMap()); |
| 969 | } |
| 970 | |
| 971 | /// \brief Exit from the current scope. |
| 972 | void ResultBuilder::ExitScope() { |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 973 | for (ShadowMap::iterator E = ShadowMaps.back().begin(), |
| 974 | EEnd = ShadowMaps.back().end(); |
| 975 | E != EEnd; |
| 976 | ++E) |
| 977 | E->second.Destroy(); |
| 978 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 979 | ShadowMaps.pop_back(); |
| 980 | } |
| 981 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 982 | /// \brief Determines whether this given declaration will be found by |
| 983 | /// ordinary name lookup. |
| 984 | bool ResultBuilder::IsOrdinaryName(NamedDecl *ND) const { |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 985 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 986 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 987 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 988 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 9b30b26 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 989 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 990 | else if (SemaRef.getLangOptions().ObjC1) { |
| 991 | if (isa<ObjCIvarDecl>(ND)) |
| 992 | return true; |
| 993 | if (isa<ObjCPropertyDecl>(ND) && |
| 994 | SemaRef.canSynthesizeProvisionalIvar(cast<ObjCPropertyDecl>(ND))) |
| 995 | return true; |
| 996 | } |
| 997 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 998 | return ND->getIdentifierNamespace() & IDNS; |
| 999 | } |
| 1000 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1001 | /// \brief Determines whether this given declaration will be found by |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1002 | /// ordinary name lookup but is not a type name. |
| 1003 | bool ResultBuilder::IsOrdinaryNonTypeName(NamedDecl *ND) const { |
| 1004 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 1005 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) |
| 1006 | return false; |
| 1007 | |
| 1008 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 1009 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 9b30b26 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1010 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1011 | else if (SemaRef.getLangOptions().ObjC1) { |
| 1012 | if (isa<ObjCIvarDecl>(ND)) |
| 1013 | return true; |
| 1014 | if (isa<ObjCPropertyDecl>(ND) && |
| 1015 | SemaRef.canSynthesizeProvisionalIvar(cast<ObjCPropertyDecl>(ND))) |
| 1016 | return true; |
| 1017 | } |
| 1018 | |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1019 | return ND->getIdentifierNamespace() & IDNS; |
| 1020 | } |
| 1021 | |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1022 | bool ResultBuilder::IsIntegralConstantValue(NamedDecl *ND) const { |
| 1023 | if (!IsOrdinaryNonTypeName(ND)) |
| 1024 | return 0; |
| 1025 | |
| 1026 | if (ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl())) |
| 1027 | if (VD->getType()->isIntegralOrEnumerationType()) |
| 1028 | return true; |
| 1029 | |
| 1030 | return false; |
| 1031 | } |
| 1032 | |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1033 | /// \brief Determines whether this given declaration will be found by |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1034 | /// ordinary name lookup. |
| 1035 | bool ResultBuilder::IsOrdinaryNonValueName(NamedDecl *ND) const { |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1036 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 1037 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1038 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 1039 | if (SemaRef.getLangOptions().CPlusPlus) |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1040 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1041 | |
| 1042 | return (ND->getIdentifierNamespace() & IDNS) && |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1043 | !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) && |
| 1044 | !isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1047 | /// \brief Determines whether the given declaration is suitable as the |
| 1048 | /// start of a C++ nested-name-specifier, e.g., a class or namespace. |
| 1049 | bool ResultBuilder::IsNestedNameSpecifier(NamedDecl *ND) const { |
| 1050 | // Allow us to find class templates, too. |
| 1051 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 1052 | ND = ClassTemplate->getTemplatedDecl(); |
| 1053 | |
| 1054 | return SemaRef.isAcceptableNestedNameSpecifier(ND); |
| 1055 | } |
| 1056 | |
| 1057 | /// \brief Determines whether the given declaration is an enumeration. |
| 1058 | bool ResultBuilder::IsEnum(NamedDecl *ND) const { |
| 1059 | return isa<EnumDecl>(ND); |
| 1060 | } |
| 1061 | |
| 1062 | /// \brief Determines whether the given declaration is a class or struct. |
| 1063 | bool ResultBuilder::IsClassOrStruct(NamedDecl *ND) const { |
| 1064 | // Allow us to find class templates, too. |
| 1065 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 1066 | ND = ClassTemplate->getTemplatedDecl(); |
| 1067 | |
| 1068 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1069 | return RD->getTagKind() == TTK_Class || |
| 1070 | RD->getTagKind() == TTK_Struct; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1071 | |
| 1072 | return false; |
| 1073 | } |
| 1074 | |
| 1075 | /// \brief Determines whether the given declaration is a union. |
| 1076 | bool ResultBuilder::IsUnion(NamedDecl *ND) const { |
| 1077 | // Allow us to find class templates, too. |
| 1078 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 1079 | ND = ClassTemplate->getTemplatedDecl(); |
| 1080 | |
| 1081 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1082 | return RD->getTagKind() == TTK_Union; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1083 | |
| 1084 | return false; |
| 1085 | } |
| 1086 | |
| 1087 | /// \brief Determines whether the given declaration is a namespace. |
| 1088 | bool ResultBuilder::IsNamespace(NamedDecl *ND) const { |
| 1089 | return isa<NamespaceDecl>(ND); |
| 1090 | } |
| 1091 | |
| 1092 | /// \brief Determines whether the given declaration is a namespace or |
| 1093 | /// namespace alias. |
| 1094 | bool ResultBuilder::IsNamespaceOrAlias(NamedDecl *ND) const { |
| 1095 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
| 1096 | } |
| 1097 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1098 | /// \brief Determines whether the given declaration is a type. |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1099 | bool ResultBuilder::IsType(NamedDecl *ND) const { |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1100 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND)) |
| 1101 | ND = Using->getTargetDecl(); |
| 1102 | |
| 1103 | return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1106 | /// \brief Determines which members of a class should be visible via |
| 1107 | /// "." or "->". Only value declarations, nested name specifiers, and |
| 1108 | /// using declarations thereof should show up. |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1109 | bool ResultBuilder::IsMember(NamedDecl *ND) const { |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1110 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND)) |
| 1111 | ND = Using->getTargetDecl(); |
| 1112 | |
Douglas Gregor | ce82196 | 2009-12-11 18:14:22 +0000 | [diff] [blame] | 1113 | return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) || |
| 1114 | isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1117 | static bool isObjCReceiverType(ASTContext &C, QualType T) { |
| 1118 | T = C.getCanonicalType(T); |
| 1119 | switch (T->getTypeClass()) { |
| 1120 | case Type::ObjCObject: |
| 1121 | case Type::ObjCInterface: |
| 1122 | case Type::ObjCObjectPointer: |
| 1123 | return true; |
| 1124 | |
| 1125 | case Type::Builtin: |
| 1126 | switch (cast<BuiltinType>(T)->getKind()) { |
| 1127 | case BuiltinType::ObjCId: |
| 1128 | case BuiltinType::ObjCClass: |
| 1129 | case BuiltinType::ObjCSel: |
| 1130 | return true; |
| 1131 | |
| 1132 | default: |
| 1133 | break; |
| 1134 | } |
| 1135 | return false; |
| 1136 | |
| 1137 | default: |
| 1138 | break; |
| 1139 | } |
| 1140 | |
| 1141 | if (!C.getLangOptions().CPlusPlus) |
| 1142 | return false; |
| 1143 | |
| 1144 | // FIXME: We could perform more analysis here to determine whether a |
| 1145 | // particular class type has any conversions to Objective-C types. For now, |
| 1146 | // just accept all class types. |
| 1147 | return T->isDependentType() || T->isRecordType(); |
| 1148 | } |
| 1149 | |
| 1150 | bool ResultBuilder::IsObjCMessageReceiver(NamedDecl *ND) const { |
| 1151 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1152 | if (T.isNull()) |
| 1153 | return false; |
| 1154 | |
| 1155 | T = SemaRef.Context.getBaseElementType(T); |
| 1156 | return isObjCReceiverType(SemaRef.Context, T); |
| 1157 | } |
| 1158 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1159 | bool ResultBuilder::IsObjCCollection(NamedDecl *ND) const { |
| 1160 | if ((SemaRef.getLangOptions().CPlusPlus && !IsOrdinaryName(ND)) || |
| 1161 | (!SemaRef.getLangOptions().CPlusPlus && !IsOrdinaryNonTypeName(ND))) |
| 1162 | return false; |
| 1163 | |
| 1164 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1165 | if (T.isNull()) |
| 1166 | return false; |
| 1167 | |
| 1168 | T = SemaRef.Context.getBaseElementType(T); |
| 1169 | return T->isObjCObjectType() || T->isObjCObjectPointerType() || |
| 1170 | T->isObjCIdType() || |
| 1171 | (SemaRef.getLangOptions().CPlusPlus && T->isRecordType()); |
| 1172 | } |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1173 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1174 | bool ResultBuilder::IsImpossibleToSatisfy(NamedDecl *ND) const { |
| 1175 | return false; |
| 1176 | } |
| 1177 | |
Douglas Gregor | 80f4f4c | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1178 | /// \rief Determines whether the given declaration is an Objective-C |
| 1179 | /// instance variable. |
| 1180 | bool ResultBuilder::IsObjCIvar(NamedDecl *ND) const { |
| 1181 | return isa<ObjCIvarDecl>(ND); |
| 1182 | } |
| 1183 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1184 | namespace { |
| 1185 | /// \brief Visible declaration consumer that adds a code-completion result |
| 1186 | /// for each visible declaration. |
| 1187 | class CodeCompletionDeclConsumer : public VisibleDeclConsumer { |
| 1188 | ResultBuilder &Results; |
| 1189 | DeclContext *CurContext; |
| 1190 | |
| 1191 | public: |
| 1192 | CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext) |
| 1193 | : Results(Results), CurContext(CurContext) { } |
| 1194 | |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 1195 | virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, bool InBaseClass) { |
| 1196 | Results.AddResult(ND, CurContext, Hiding, InBaseClass); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1197 | } |
| 1198 | }; |
| 1199 | } |
| 1200 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1201 | /// \brief Add type specifiers for the current language as keyword results. |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1202 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1203 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1204 | typedef CodeCompletionResult Result; |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1205 | Results.AddResult(Result("short", CCP_Type)); |
| 1206 | Results.AddResult(Result("long", CCP_Type)); |
| 1207 | Results.AddResult(Result("signed", CCP_Type)); |
| 1208 | Results.AddResult(Result("unsigned", CCP_Type)); |
| 1209 | Results.AddResult(Result("void", CCP_Type)); |
| 1210 | Results.AddResult(Result("char", CCP_Type)); |
| 1211 | Results.AddResult(Result("int", CCP_Type)); |
| 1212 | Results.AddResult(Result("float", CCP_Type)); |
| 1213 | Results.AddResult(Result("double", CCP_Type)); |
| 1214 | Results.AddResult(Result("enum", CCP_Type)); |
| 1215 | Results.AddResult(Result("struct", CCP_Type)); |
| 1216 | Results.AddResult(Result("union", CCP_Type)); |
| 1217 | Results.AddResult(Result("const", CCP_Type)); |
| 1218 | Results.AddResult(Result("volatile", CCP_Type)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1219 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1220 | if (LangOpts.C99) { |
| 1221 | // C99-specific |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1222 | Results.AddResult(Result("_Complex", CCP_Type)); |
| 1223 | Results.AddResult(Result("_Imaginary", CCP_Type)); |
| 1224 | Results.AddResult(Result("_Bool", CCP_Type)); |
| 1225 | Results.AddResult(Result("restrict", CCP_Type)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1228 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1229 | if (LangOpts.CPlusPlus) { |
| 1230 | // C++-specific |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 1231 | Results.AddResult(Result("bool", CCP_Type + |
| 1232 | (LangOpts.ObjC1? CCD_bool_in_ObjC : 0))); |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1233 | Results.AddResult(Result("class", CCP_Type)); |
| 1234 | Results.AddResult(Result("wchar_t", CCP_Type)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1235 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1236 | // typename qualified-id |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1237 | Builder.AddTypedTextChunk("typename"); |
| 1238 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1239 | Builder.AddPlaceholderChunk("qualifier"); |
| 1240 | Builder.AddTextChunk("::"); |
| 1241 | Builder.AddPlaceholderChunk("name"); |
| 1242 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1243 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1244 | if (LangOpts.CPlusPlus0x) { |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1245 | Results.AddResult(Result("auto", CCP_Type)); |
| 1246 | Results.AddResult(Result("char16_t", CCP_Type)); |
| 1247 | Results.AddResult(Result("char32_t", CCP_Type)); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1248 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1249 | Builder.AddTypedTextChunk("decltype"); |
| 1250 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1251 | Builder.AddPlaceholderChunk("expression"); |
| 1252 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1253 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | // GNU extensions |
| 1258 | if (LangOpts.GNUMode) { |
| 1259 | // FIXME: Enable when we actually support decimal floating point. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1260 | // Results.AddResult(Result("_Decimal32")); |
| 1261 | // Results.AddResult(Result("_Decimal64")); |
| 1262 | // Results.AddResult(Result("_Decimal128")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1263 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1264 | Builder.AddTypedTextChunk("typeof"); |
| 1265 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1266 | Builder.AddPlaceholderChunk("expression"); |
| 1267 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1268 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1269 | Builder.AddTypedTextChunk("typeof"); |
| 1270 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1271 | Builder.AddPlaceholderChunk("type"); |
| 1272 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1273 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1277 | static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1278 | const LangOptions &LangOpts, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1279 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1280 | typedef CodeCompletionResult Result; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1281 | // Note: we don't suggest either "auto" or "register", because both |
| 1282 | // are pointless as storage specifiers. Elsewhere, we suggest "auto" |
| 1283 | // in C++0x as a type specifier. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1284 | Results.AddResult(Result("extern")); |
| 1285 | Results.AddResult(Result("static")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1288 | static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1289 | const LangOptions &LangOpts, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1290 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1291 | typedef CodeCompletionResult Result; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1292 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1293 | case Sema::PCC_Class: |
| 1294 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1295 | if (LangOpts.CPlusPlus) { |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1296 | Results.AddResult(Result("explicit")); |
| 1297 | Results.AddResult(Result("friend")); |
| 1298 | Results.AddResult(Result("mutable")); |
| 1299 | Results.AddResult(Result("virtual")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1300 | } |
| 1301 | // Fall through |
| 1302 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1303 | case Sema::PCC_ObjCInterface: |
| 1304 | case Sema::PCC_ObjCImplementation: |
| 1305 | case Sema::PCC_Namespace: |
| 1306 | case Sema::PCC_Template: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1307 | if (LangOpts.CPlusPlus || LangOpts.C99) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1308 | Results.AddResult(Result("inline")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1309 | break; |
| 1310 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1311 | case Sema::PCC_ObjCInstanceVariableList: |
| 1312 | case Sema::PCC_Expression: |
| 1313 | case Sema::PCC_Statement: |
| 1314 | case Sema::PCC_ForInit: |
| 1315 | case Sema::PCC_Condition: |
| 1316 | case Sema::PCC_RecoveryInFunction: |
| 1317 | case Sema::PCC_Type: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1318 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1319 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1320 | break; |
| 1321 | } |
| 1322 | } |
| 1323 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1324 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); |
| 1325 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); |
| 1326 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1327 | ResultBuilder &Results, |
| 1328 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1329 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1330 | ResultBuilder &Results, |
| 1331 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1332 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1333 | ResultBuilder &Results, |
| 1334 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1335 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1336 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1337 | static void AddTypedefResult(ResultBuilder &Results) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1338 | CodeCompletionBuilder Builder(Results.getAllocator()); |
| 1339 | Builder.AddTypedTextChunk("typedef"); |
| 1340 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1341 | Builder.AddPlaceholderChunk("type"); |
| 1342 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1343 | Builder.AddPlaceholderChunk("name"); |
| 1344 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1347 | static bool WantTypesInContext(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1348 | const LangOptions &LangOpts) { |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1349 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1350 | case Sema::PCC_Namespace: |
| 1351 | case Sema::PCC_Class: |
| 1352 | case Sema::PCC_ObjCInstanceVariableList: |
| 1353 | case Sema::PCC_Template: |
| 1354 | case Sema::PCC_MemberTemplate: |
| 1355 | case Sema::PCC_Statement: |
| 1356 | case Sema::PCC_RecoveryInFunction: |
| 1357 | case Sema::PCC_Type: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1358 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1359 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1360 | return true; |
| 1361 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1362 | case Sema::PCC_Expression: |
| 1363 | case Sema::PCC_Condition: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1364 | return LangOpts.CPlusPlus; |
| 1365 | |
| 1366 | case Sema::PCC_ObjCInterface: |
| 1367 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1368 | return false; |
| 1369 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1370 | case Sema::PCC_ForInit: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1371 | return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99; |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | return false; |
| 1375 | } |
| 1376 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1377 | /// \brief Add language constructs that show up for "ordinary" names. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1378 | static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1379 | Scope *S, |
| 1380 | Sema &SemaRef, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1381 | ResultBuilder &Results) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1382 | CodeCompletionBuilder Builder(Results.getAllocator()); |
| 1383 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1384 | typedef CodeCompletionResult Result; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1385 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1386 | case Sema::PCC_Namespace: |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1387 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1388 | if (Results.includeCodePatterns()) { |
| 1389 | // namespace <identifier> { declarations } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1390 | Builder.AddTypedTextChunk("namespace"); |
| 1391 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1392 | Builder.AddPlaceholderChunk("identifier"); |
| 1393 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1394 | Builder.AddPlaceholderChunk("declarations"); |
| 1395 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1396 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1397 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1400 | // namespace identifier = identifier ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1401 | Builder.AddTypedTextChunk("namespace"); |
| 1402 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1403 | Builder.AddPlaceholderChunk("name"); |
| 1404 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
| 1405 | Builder.AddPlaceholderChunk("namespace"); |
| 1406 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1407 | |
| 1408 | // Using directives |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1409 | Builder.AddTypedTextChunk("using"); |
| 1410 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1411 | Builder.AddTextChunk("namespace"); |
| 1412 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1413 | Builder.AddPlaceholderChunk("identifier"); |
| 1414 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1415 | |
| 1416 | // asm(string-literal) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1417 | Builder.AddTypedTextChunk("asm"); |
| 1418 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1419 | Builder.AddPlaceholderChunk("string-literal"); |
| 1420 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1421 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1422 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1423 | if (Results.includeCodePatterns()) { |
| 1424 | // Explicit template instantiation |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1425 | Builder.AddTypedTextChunk("template"); |
| 1426 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1427 | Builder.AddPlaceholderChunk("declaration"); |
| 1428 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1429 | } |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1430 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1431 | |
| 1432 | if (SemaRef.getLangOptions().ObjC1) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1433 | AddObjCTopLevelResults(Results, true); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1434 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1435 | AddTypedefResult(Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1436 | // Fall through |
| 1437 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1438 | case Sema::PCC_Class: |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1439 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1440 | // Using declaration |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1441 | Builder.AddTypedTextChunk("using"); |
| 1442 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1443 | Builder.AddPlaceholderChunk("qualifier"); |
| 1444 | Builder.AddTextChunk("::"); |
| 1445 | Builder.AddPlaceholderChunk("name"); |
| 1446 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1447 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1448 | // using typename qualifier::name (only in a dependent context) |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1449 | if (SemaRef.CurContext->isDependentContext()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1450 | Builder.AddTypedTextChunk("using"); |
| 1451 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1452 | Builder.AddTextChunk("typename"); |
| 1453 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1454 | Builder.AddPlaceholderChunk("qualifier"); |
| 1455 | Builder.AddTextChunk("::"); |
| 1456 | Builder.AddPlaceholderChunk("name"); |
| 1457 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1460 | if (CCC == Sema::PCC_Class) { |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1461 | AddTypedefResult(Results); |
| 1462 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1463 | // public: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1464 | Builder.AddTypedTextChunk("public"); |
| 1465 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1466 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1467 | |
| 1468 | // protected: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1469 | Builder.AddTypedTextChunk("protected"); |
| 1470 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1471 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1472 | |
| 1473 | // private: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1474 | Builder.AddTypedTextChunk("private"); |
| 1475 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1476 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | // Fall through |
| 1480 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1481 | case Sema::PCC_Template: |
| 1482 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1483 | if (SemaRef.getLangOptions().CPlusPlus && Results.includeCodePatterns()) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1484 | // template < parameters > |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1485 | Builder.AddTypedTextChunk("template"); |
| 1486 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1487 | Builder.AddPlaceholderChunk("parameters"); |
| 1488 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1489 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1492 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 1493 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1494 | break; |
| 1495 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1496 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1497 | AddObjCInterfaceResults(SemaRef.getLangOptions(), Results, true); |
| 1498 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 1499 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1500 | break; |
| 1501 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1502 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1503 | AddObjCImplementationResults(SemaRef.getLangOptions(), Results, true); |
| 1504 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 1505 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1506 | break; |
| 1507 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1508 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1509 | AddObjCVisibilityResults(SemaRef.getLangOptions(), Results, true); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1510 | break; |
| 1511 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1512 | case Sema::PCC_RecoveryInFunction: |
| 1513 | case Sema::PCC_Statement: { |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1514 | AddTypedefResult(Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1515 | |
Douglas Gregor | ec3310a | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1516 | if (SemaRef.getLangOptions().CPlusPlus && Results.includeCodePatterns() && |
| 1517 | SemaRef.getLangOptions().CXXExceptions) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1518 | Builder.AddTypedTextChunk("try"); |
| 1519 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1520 | Builder.AddPlaceholderChunk("statements"); |
| 1521 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1522 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1523 | Builder.AddTextChunk("catch"); |
| 1524 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1525 | Builder.AddPlaceholderChunk("declaration"); |
| 1526 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1527 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1528 | Builder.AddPlaceholderChunk("statements"); |
| 1529 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1530 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1531 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1532 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1533 | if (SemaRef.getLangOptions().ObjC1) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1534 | AddObjCStatementResults(Results, true); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1535 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1536 | if (Results.includeCodePatterns()) { |
| 1537 | // if (condition) { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1538 | Builder.AddTypedTextChunk("if"); |
| 1539 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1540 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1541 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1542 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1543 | Builder.AddPlaceholderChunk("expression"); |
| 1544 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1545 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1546 | Builder.AddPlaceholderChunk("statements"); |
| 1547 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1548 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1549 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1550 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1551 | // switch (condition) { } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1552 | Builder.AddTypedTextChunk("switch"); |
| 1553 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1554 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1555 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1556 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1557 | Builder.AddPlaceholderChunk("expression"); |
| 1558 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1559 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1560 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1561 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1562 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1565 | // Switch-specific statements. |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 1566 | if (!SemaRef.getCurFunction()->SwitchStack.empty()) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1567 | // case expression: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1568 | Builder.AddTypedTextChunk("case"); |
| 1569 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1570 | Builder.AddPlaceholderChunk("expression"); |
| 1571 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1572 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1573 | |
| 1574 | // default: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1575 | Builder.AddTypedTextChunk("default"); |
| 1576 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1577 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1580 | if (Results.includeCodePatterns()) { |
| 1581 | /// while (condition) { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1582 | Builder.AddTypedTextChunk("while"); |
| 1583 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1584 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1585 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1586 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1587 | Builder.AddPlaceholderChunk("expression"); |
| 1588 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1589 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1590 | Builder.AddPlaceholderChunk("statements"); |
| 1591 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1592 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1593 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1594 | |
| 1595 | // do { statements } while ( expression ); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1596 | Builder.AddTypedTextChunk("do"); |
| 1597 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1598 | Builder.AddPlaceholderChunk("statements"); |
| 1599 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1600 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1601 | Builder.AddTextChunk("while"); |
| 1602 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1603 | Builder.AddPlaceholderChunk("expression"); |
| 1604 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1605 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1606 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1607 | // for ( for-init-statement ; condition ; expression ) { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1608 | Builder.AddTypedTextChunk("for"); |
| 1609 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1610 | if (SemaRef.getLangOptions().CPlusPlus || SemaRef.getLangOptions().C99) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1611 | Builder.AddPlaceholderChunk("init-statement"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1612 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1613 | Builder.AddPlaceholderChunk("init-expression"); |
| 1614 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1615 | Builder.AddPlaceholderChunk("condition"); |
| 1616 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1617 | Builder.AddPlaceholderChunk("inc-expression"); |
| 1618 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1619 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1620 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1621 | Builder.AddPlaceholderChunk("statements"); |
| 1622 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1623 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1624 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1625 | } |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1626 | |
| 1627 | if (S->getContinueParent()) { |
| 1628 | // continue ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1629 | Builder.AddTypedTextChunk("continue"); |
| 1630 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | if (S->getBreakParent()) { |
| 1634 | // break ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1635 | Builder.AddTypedTextChunk("break"); |
| 1636 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | // "return expression ;" or "return ;", depending on whether we |
| 1640 | // know the function is void or not. |
| 1641 | bool isVoid = false; |
| 1642 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) |
| 1643 | isVoid = Function->getResultType()->isVoidType(); |
| 1644 | else if (ObjCMethodDecl *Method |
| 1645 | = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) |
| 1646 | isVoid = Method->getResultType()->isVoidType(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1647 | else if (SemaRef.getCurBlock() && |
| 1648 | !SemaRef.getCurBlock()->ReturnType.isNull()) |
| 1649 | isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType(); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1650 | Builder.AddTypedTextChunk("return"); |
Douglas Gregor | 9329800 | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1651 | if (!isVoid) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1652 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1653 | Builder.AddPlaceholderChunk("expression"); |
Douglas Gregor | 9329800 | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1654 | } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1655 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1656 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1657 | // goto identifier ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1658 | Builder.AddTypedTextChunk("goto"); |
| 1659 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1660 | Builder.AddPlaceholderChunk("label"); |
| 1661 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1662 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1663 | // Using directives |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1664 | Builder.AddTypedTextChunk("using"); |
| 1665 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1666 | Builder.AddTextChunk("namespace"); |
| 1667 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1668 | Builder.AddPlaceholderChunk("identifier"); |
| 1669 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | // Fall through (for statement expressions). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1673 | case Sema::PCC_ForInit: |
| 1674 | case Sema::PCC_Condition: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1675 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1676 | // Fall through: conditions and statements can have expressions. |
| 1677 | |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1678 | case Sema::PCC_ParenthesizedExpression: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1679 | if (SemaRef.getLangOptions().ObjCAutoRefCount && |
| 1680 | CCC == Sema::PCC_ParenthesizedExpression) { |
| 1681 | // (__bridge <type>)<expression> |
| 1682 | Builder.AddTypedTextChunk("__bridge"); |
| 1683 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1684 | Builder.AddPlaceholderChunk("type"); |
| 1685 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1686 | Builder.AddPlaceholderChunk("expression"); |
| 1687 | Results.AddResult(Result(Builder.TakeString())); |
| 1688 | |
| 1689 | // (__bridge_transfer <Objective-C type>)<expression> |
| 1690 | Builder.AddTypedTextChunk("__bridge_transfer"); |
| 1691 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1692 | Builder.AddPlaceholderChunk("Objective-C type"); |
| 1693 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1694 | Builder.AddPlaceholderChunk("expression"); |
| 1695 | Results.AddResult(Result(Builder.TakeString())); |
| 1696 | |
| 1697 | // (__bridge_retained <CF type>)<expression> |
| 1698 | Builder.AddTypedTextChunk("__bridge_retained"); |
| 1699 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1700 | Builder.AddPlaceholderChunk("CF type"); |
| 1701 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1702 | Builder.AddPlaceholderChunk("expression"); |
| 1703 | Results.AddResult(Result(Builder.TakeString())); |
| 1704 | } |
| 1705 | // Fall through |
| 1706 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1707 | case Sema::PCC_Expression: { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1708 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 1709 | // 'this', if we're in a non-static member function. |
| 1710 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) |
| 1711 | if (!Method->isStatic()) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1712 | Results.AddResult(Result("this")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1713 | |
| 1714 | // true, false |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1715 | Results.AddResult(Result("true")); |
| 1716 | Results.AddResult(Result("false")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1717 | |
Douglas Gregor | ec3310a | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1718 | if (SemaRef.getLangOptions().RTTI) { |
| 1719 | // dynamic_cast < type-id > ( expression ) |
| 1720 | Builder.AddTypedTextChunk("dynamic_cast"); |
| 1721 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1722 | Builder.AddPlaceholderChunk("type"); |
| 1723 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1724 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1725 | Builder.AddPlaceholderChunk("expression"); |
| 1726 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1727 | Results.AddResult(Result(Builder.TakeString())); |
| 1728 | } |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1729 | |
| 1730 | // static_cast < type-id > ( expression ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1731 | Builder.AddTypedTextChunk("static_cast"); |
| 1732 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1733 | Builder.AddPlaceholderChunk("type"); |
| 1734 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1735 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1736 | Builder.AddPlaceholderChunk("expression"); |
| 1737 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1738 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1739 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1740 | // reinterpret_cast < type-id > ( expression ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1741 | Builder.AddTypedTextChunk("reinterpret_cast"); |
| 1742 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1743 | Builder.AddPlaceholderChunk("type"); |
| 1744 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1745 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1746 | Builder.AddPlaceholderChunk("expression"); |
| 1747 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1748 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1749 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1750 | // const_cast < type-id > ( expression ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1751 | Builder.AddTypedTextChunk("const_cast"); |
| 1752 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1753 | Builder.AddPlaceholderChunk("type"); |
| 1754 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1755 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1756 | Builder.AddPlaceholderChunk("expression"); |
| 1757 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1758 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1759 | |
Douglas Gregor | ec3310a | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1760 | if (SemaRef.getLangOptions().RTTI) { |
| 1761 | // typeid ( expression-or-type ) |
| 1762 | Builder.AddTypedTextChunk("typeid"); |
| 1763 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1764 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 1765 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1766 | Results.AddResult(Result(Builder.TakeString())); |
| 1767 | } |
| 1768 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1769 | // new T ( ... ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1770 | Builder.AddTypedTextChunk("new"); |
| 1771 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1772 | Builder.AddPlaceholderChunk("type"); |
| 1773 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1774 | Builder.AddPlaceholderChunk("expressions"); |
| 1775 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1776 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1777 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1778 | // new T [ ] ( ... ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1779 | Builder.AddTypedTextChunk("new"); |
| 1780 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1781 | Builder.AddPlaceholderChunk("type"); |
| 1782 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1783 | Builder.AddPlaceholderChunk("size"); |
| 1784 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 1785 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1786 | Builder.AddPlaceholderChunk("expressions"); |
| 1787 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1788 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1789 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1790 | // delete expression |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1791 | Builder.AddTypedTextChunk("delete"); |
| 1792 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1793 | Builder.AddPlaceholderChunk("expression"); |
| 1794 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1795 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1796 | // delete [] expression |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1797 | Builder.AddTypedTextChunk("delete"); |
| 1798 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1799 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1800 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 1801 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1802 | Builder.AddPlaceholderChunk("expression"); |
| 1803 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1804 | |
Douglas Gregor | ec3310a | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1805 | if (SemaRef.getLangOptions().CXXExceptions) { |
| 1806 | // throw expression |
| 1807 | Builder.AddTypedTextChunk("throw"); |
| 1808 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1809 | Builder.AddPlaceholderChunk("expression"); |
| 1810 | Results.AddResult(Result(Builder.TakeString())); |
| 1811 | } |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1812 | |
| 1813 | // FIXME: Rethrow? |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | if (SemaRef.getLangOptions().ObjC1) { |
| 1817 | // 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] | 1818 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
| 1819 | // The interface can be NULL. |
| 1820 | if (ObjCInterfaceDecl *ID = Method->getClassInterface()) |
| 1821 | if (ID->getSuperClass()) |
| 1822 | Results.AddResult(Result("super")); |
| 1823 | } |
| 1824 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1825 | AddObjCExpressionResults(Results, true); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1828 | // sizeof expression |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1829 | Builder.AddTypedTextChunk("sizeof"); |
| 1830 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1831 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 1832 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1833 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1834 | break; |
| 1835 | } |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1836 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1837 | case Sema::PCC_Type: |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1838 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1839 | break; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1842 | if (WantTypesInContext(CCC, SemaRef.getLangOptions())) |
| 1843 | AddTypeSpecifierResults(SemaRef.getLangOptions(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1844 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1845 | if (SemaRef.getLangOptions().CPlusPlus && CCC != Sema::PCC_Type) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1846 | Results.AddResult(Result("operator")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 1849 | /// \brief Retrieve the string representation of the given type as a string |
| 1850 | /// that has the appropriate lifetime for code completion. |
| 1851 | /// |
| 1852 | /// This routine provides a fast path where we provide constant strings for |
| 1853 | /// common type names. |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1854 | static const char *GetCompletionTypeString(QualType T, |
| 1855 | ASTContext &Context, |
| 1856 | CodeCompletionAllocator &Allocator) { |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 1857 | PrintingPolicy Policy(Context.PrintingPolicy); |
| 1858 | Policy.AnonymousTagLocations = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1859 | Policy.SuppressStrongLifetime = true; |
| 1860 | |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 1861 | if (!T.getLocalQualifiers()) { |
| 1862 | // Built-in type names are constant strings. |
| 1863 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) |
| 1864 | return BT->getName(Context.getLangOptions()); |
| 1865 | |
| 1866 | // Anonymous tag types are constant strings. |
| 1867 | if (const TagType *TagT = dyn_cast<TagType>(T)) |
| 1868 | if (TagDecl *Tag = TagT->getDecl()) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1869 | if (!Tag->getIdentifier() && !Tag->getTypedefNameForAnonDecl()) { |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 1870 | switch (Tag->getTagKind()) { |
| 1871 | case TTK_Struct: return "struct <anonymous>"; |
| 1872 | case TTK_Class: return "class <anonymous>"; |
| 1873 | case TTK_Union: return "union <anonymous>"; |
| 1874 | case TTK_Enum: return "enum <anonymous>"; |
| 1875 | } |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | // Slow path: format the type as a string. |
| 1880 | std::string Result; |
| 1881 | T.getAsStringInternal(Result, Policy); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 1882 | return Allocator.CopyString(Result); |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1885 | /// \brief If the given declaration has an associated type, add it as a result |
| 1886 | /// type chunk. |
| 1887 | static void AddResultTypeChunk(ASTContext &Context, |
| 1888 | NamedDecl *ND, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1889 | CodeCompletionBuilder &Result) { |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1890 | if (!ND) |
| 1891 | return; |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1892 | |
| 1893 | // Skip constructors and conversion functions, which have their return types |
| 1894 | // built into their names. |
| 1895 | if (isa<CXXConstructorDecl>(ND) || isa<CXXConversionDecl>(ND)) |
| 1896 | return; |
| 1897 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1898 | // Determine the type of the declaration (if it has a type). |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1899 | QualType T; |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1900 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) |
| 1901 | T = Function->getResultType(); |
| 1902 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
| 1903 | T = Method->getResultType(); |
| 1904 | else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) |
| 1905 | T = FunTmpl->getTemplatedDecl()->getResultType(); |
| 1906 | else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
| 1907 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); |
| 1908 | else if (isa<UnresolvedUsingValueDecl>(ND)) { |
| 1909 | /* Do nothing: ignore unresolved using declarations*/ |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1910 | } else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND)) { |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1911 | T = Value->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1912 | } else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1913 | T = Property->getType(); |
| 1914 | |
| 1915 | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) |
| 1916 | return; |
| 1917 | |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 1918 | Result.AddResultTypeChunk(GetCompletionTypeString(T, Context, |
| 1919 | Result.getAllocator())); |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1922 | static void MaybeAddSentinel(ASTContext &Context, NamedDecl *FunctionOrMethod, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1923 | CodeCompletionBuilder &Result) { |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1924 | if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) |
| 1925 | if (Sentinel->getSentinel() == 0) { |
| 1926 | if (Context.getLangOptions().ObjC1 && |
| 1927 | Context.Idents.get("nil").hasMacroDefinition()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1928 | Result.AddTextChunk(", nil"); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1929 | else if (Context.Idents.get("NULL").hasMacroDefinition()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1930 | Result.AddTextChunk(", NULL"); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1931 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1932 | Result.AddTextChunk(", (void*)0"); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | |
Douglas Gregor | 6fa14dd | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 1936 | static void appendWithSpace(std::string &Result, StringRef Text) { |
| 1937 | if (!Result.empty()) |
| 1938 | Result += ' '; |
| 1939 | Result += Text.str(); |
| 1940 | } |
| 1941 | static std::string formatObjCParamQualifiers(unsigned ObjCQuals) { |
| 1942 | std::string Result; |
| 1943 | if (ObjCQuals & Decl::OBJC_TQ_In) |
| 1944 | appendWithSpace(Result, "in"); |
| 1945 | else if (ObjCQuals & Decl::OBJC_TQ_Inout) |
| 1946 | appendWithSpace(Result, "inout"); |
| 1947 | else if (ObjCQuals & Decl::OBJC_TQ_Out) |
| 1948 | appendWithSpace(Result, "out"); |
| 1949 | if (ObjCQuals & Decl::OBJC_TQ_Bycopy) |
| 1950 | appendWithSpace(Result, "bycopy"); |
| 1951 | else if (ObjCQuals & Decl::OBJC_TQ_Byref) |
| 1952 | appendWithSpace(Result, "byref"); |
| 1953 | if (ObjCQuals & Decl::OBJC_TQ_Oneway) |
| 1954 | appendWithSpace(Result, "oneway"); |
| 1955 | return Result; |
| 1956 | } |
| 1957 | |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1958 | static std::string FormatFunctionParameter(ASTContext &Context, |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 1959 | ParmVarDecl *Param, |
| 1960 | bool SuppressName = false) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1961 | PrintingPolicy Policy(Context.PrintingPolicy); |
| 1962 | Policy.AnonymousTagLocations = false; |
| 1963 | Policy.SuppressStrongLifetime = true; |
| 1964 | |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1965 | bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext()); |
| 1966 | if (Param->getType()->isDependentType() || |
| 1967 | !Param->getType()->isBlockPointerType()) { |
| 1968 | // The argument for a dependent or non-block parameter is a placeholder |
| 1969 | // containing that parameter's type. |
| 1970 | std::string Result; |
| 1971 | |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 1972 | if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName) |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1973 | Result = Param->getIdentifier()->getName(); |
| 1974 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1975 | Param->getType().getAsStringInternal(Result, Policy); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1976 | |
| 1977 | if (ObjCMethodParam) { |
Douglas Gregor | 6fa14dd | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 1978 | Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier()) |
| 1979 | + Result + ")"; |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 1980 | if (Param->getIdentifier() && !SuppressName) |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1981 | Result += Param->getIdentifier()->getName(); |
| 1982 | } |
| 1983 | return Result; |
| 1984 | } |
| 1985 | |
| 1986 | // The argument for a block pointer parameter is a block literal with |
| 1987 | // the appropriate type. |
Douglas Gregor | 830072c | 2011-02-15 22:37:09 +0000 | [diff] [blame] | 1988 | FunctionTypeLoc *Block = 0; |
| 1989 | FunctionProtoTypeLoc *BlockProto = 0; |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1990 | TypeLoc TL; |
| 1991 | if (TypeSourceInfo *TSInfo = Param->getTypeSourceInfo()) { |
| 1992 | TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 1993 | while (true) { |
| 1994 | // Look through typedefs. |
| 1995 | if (TypedefTypeLoc *TypedefTL = dyn_cast<TypedefTypeLoc>(&TL)) { |
| 1996 | if (TypeSourceInfo *InnerTSInfo |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1997 | = TypedefTL->getTypedefNameDecl()->getTypeSourceInfo()) { |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1998 | TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 1999 | continue; |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | // Look through qualified types |
| 2004 | if (QualifiedTypeLoc *QualifiedTL = dyn_cast<QualifiedTypeLoc>(&TL)) { |
| 2005 | TL = QualifiedTL->getUnqualifiedLoc(); |
| 2006 | continue; |
| 2007 | } |
| 2008 | |
| 2009 | // Try to get the function prototype behind the block pointer type, |
| 2010 | // then we're done. |
| 2011 | if (BlockPointerTypeLoc *BlockPtr |
| 2012 | = dyn_cast<BlockPointerTypeLoc>(&TL)) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 2013 | TL = BlockPtr->getPointeeLoc().IgnoreParens(); |
Douglas Gregor | 830072c | 2011-02-15 22:37:09 +0000 | [diff] [blame] | 2014 | Block = dyn_cast<FunctionTypeLoc>(&TL); |
| 2015 | BlockProto = dyn_cast<FunctionProtoTypeLoc>(&TL); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2016 | } |
| 2017 | break; |
| 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | if (!Block) { |
| 2022 | // We were unable to find a FunctionProtoTypeLoc with parameter names |
| 2023 | // for the block; just use the parameter type as a placeholder. |
| 2024 | std::string Result; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2025 | Param->getType().getUnqualifiedType().getAsStringInternal(Result, Policy); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2026 | |
| 2027 | if (ObjCMethodParam) { |
Douglas Gregor | 6fa14dd | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2028 | Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier()) |
| 2029 | + Result + ")"; |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2030 | if (Param->getIdentifier()) |
| 2031 | Result += Param->getIdentifier()->getName(); |
| 2032 | } |
| 2033 | |
| 2034 | return Result; |
| 2035 | } |
| 2036 | |
| 2037 | // We have the function prototype behind the block pointer type, as it was |
| 2038 | // written in the source. |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2039 | std::string Result; |
| 2040 | QualType ResultType = Block->getTypePtr()->getResultType(); |
| 2041 | if (!ResultType->isVoidType()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2042 | ResultType.getAsStringInternal(Result, Policy); |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2043 | |
| 2044 | Result = '^' + Result; |
Douglas Gregor | 830072c | 2011-02-15 22:37:09 +0000 | [diff] [blame] | 2045 | if (!BlockProto || Block->getNumArgs() == 0) { |
| 2046 | if (BlockProto && BlockProto->getTypePtr()->isVariadic()) |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2047 | Result += "(...)"; |
Douglas Gregor | c2760bc | 2010-10-02 23:49:58 +0000 | [diff] [blame] | 2048 | else |
| 2049 | Result += "(void)"; |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2050 | } else { |
| 2051 | Result += "("; |
| 2052 | for (unsigned I = 0, N = Block->getNumArgs(); I != N; ++I) { |
| 2053 | if (I) |
| 2054 | Result += ", "; |
| 2055 | Result += FormatFunctionParameter(Context, Block->getArg(I)); |
| 2056 | |
Douglas Gregor | 830072c | 2011-02-15 22:37:09 +0000 | [diff] [blame] | 2057 | if (I == N - 1 && BlockProto->getTypePtr()->isVariadic()) |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2058 | Result += ", ..."; |
| 2059 | } |
| 2060 | Result += ")"; |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2061 | } |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2062 | |
Douglas Gregor | c2760bc | 2010-10-02 23:49:58 +0000 | [diff] [blame] | 2063 | if (Param->getIdentifier()) |
| 2064 | Result += Param->getIdentifier()->getName(); |
| 2065 | |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2066 | return Result; |
| 2067 | } |
| 2068 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2069 | /// \brief Add function parameter chunks to the given code completion string. |
| 2070 | static void AddFunctionParameterChunks(ASTContext &Context, |
| 2071 | FunctionDecl *Function, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2072 | CodeCompletionBuilder &Result, |
| 2073 | unsigned Start = 0, |
| 2074 | bool InOptional = false) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2075 | typedef CodeCompletionString::Chunk Chunk; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2076 | bool FirstParameter = true; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2077 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2078 | for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2079 | ParmVarDecl *Param = Function->getParamDecl(P); |
| 2080 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2081 | if (Param->hasDefaultArg() && !InOptional) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2082 | // When we see an optional default argument, put that argument and |
| 2083 | // the remaining default arguments into a new, optional string. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2084 | CodeCompletionBuilder Opt(Result.getAllocator()); |
| 2085 | if (!FirstParameter) |
| 2086 | Opt.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
| 2087 | AddFunctionParameterChunks(Context, Function, Opt, P, true); |
| 2088 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2089 | break; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2092 | if (FirstParameter) |
| 2093 | FirstParameter = false; |
| 2094 | else |
| 2095 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
| 2096 | |
| 2097 | InOptional = false; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2098 | |
| 2099 | // Format the placeholder string. |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2100 | std::string PlaceholderStr = FormatFunctionParameter(Context, Param); |
| 2101 | |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2102 | if (Function->isVariadic() && P == N - 1) |
| 2103 | PlaceholderStr += ", ..."; |
| 2104 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2105 | // Add the placeholder string. |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2106 | Result.AddPlaceholderChunk( |
| 2107 | Result.getAllocator().CopyString(PlaceholderStr)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2108 | } |
Douglas Gregor | b3d4525 | 2009-09-22 21:42:17 +0000 | [diff] [blame] | 2109 | |
| 2110 | if (const FunctionProtoType *Proto |
| 2111 | = Function->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2112 | if (Proto->isVariadic()) { |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2113 | if (Proto->getNumArgs() == 0) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2114 | Result.AddPlaceholderChunk("..."); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2115 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2116 | MaybeAddSentinel(Context, Function, Result); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2117 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
| 2120 | /// \brief Add template parameter chunks to the given code completion string. |
| 2121 | static void AddTemplateParameterChunks(ASTContext &Context, |
| 2122 | TemplateDecl *Template, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2123 | CodeCompletionBuilder &Result, |
| 2124 | unsigned MaxParameters = 0, |
| 2125 | unsigned Start = 0, |
| 2126 | bool InDefaultArg = false) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2127 | PrintingPolicy Policy(Context.PrintingPolicy); |
| 2128 | Policy.AnonymousTagLocations = false; |
| 2129 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2130 | typedef CodeCompletionString::Chunk Chunk; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2131 | bool FirstParameter = true; |
| 2132 | |
| 2133 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2134 | TemplateParameterList::iterator PEnd = Params->end(); |
| 2135 | if (MaxParameters) |
| 2136 | PEnd = Params->begin() + MaxParameters; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2137 | for (TemplateParameterList::iterator P = Params->begin() + Start; |
| 2138 | P != PEnd; ++P) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2139 | bool HasDefaultArg = false; |
| 2140 | std::string PlaceholderStr; |
| 2141 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 2142 | if (TTP->wasDeclaredWithTypename()) |
| 2143 | PlaceholderStr = "typename"; |
| 2144 | else |
| 2145 | PlaceholderStr = "class"; |
| 2146 | |
| 2147 | if (TTP->getIdentifier()) { |
| 2148 | PlaceholderStr += ' '; |
| 2149 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2150 | } |
| 2151 | |
| 2152 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2153 | } else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2154 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2155 | if (NTTP->getIdentifier()) |
| 2156 | PlaceholderStr = NTTP->getIdentifier()->getName(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2157 | NTTP->getType().getAsStringInternal(PlaceholderStr, Policy); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2158 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2159 | } else { |
| 2160 | assert(isa<TemplateTemplateParmDecl>(*P)); |
| 2161 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 2162 | |
| 2163 | // Since putting the template argument list into the placeholder would |
| 2164 | // be very, very long, we just use an abbreviation. |
| 2165 | PlaceholderStr = "template<...> class"; |
| 2166 | if (TTP->getIdentifier()) { |
| 2167 | PlaceholderStr += ' '; |
| 2168 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2169 | } |
| 2170 | |
| 2171 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2172 | } |
| 2173 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2174 | if (HasDefaultArg && !InDefaultArg) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2175 | // When we see an optional default argument, put that argument and |
| 2176 | // the remaining default arguments into a new, optional string. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2177 | CodeCompletionBuilder Opt(Result.getAllocator()); |
| 2178 | if (!FirstParameter) |
| 2179 | Opt.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
| 2180 | AddTemplateParameterChunks(Context, Template, Opt, MaxParameters, |
| 2181 | P - Params->begin(), true); |
| 2182 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2183 | break; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2186 | InDefaultArg = false; |
| 2187 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2188 | if (FirstParameter) |
| 2189 | FirstParameter = false; |
| 2190 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2191 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2192 | |
| 2193 | // Add the placeholder string. |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2194 | Result.AddPlaceholderChunk( |
| 2195 | Result.getAllocator().CopyString(PlaceholderStr)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2196 | } |
| 2197 | } |
| 2198 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2199 | /// \brief Add a qualifier to the given code-completion string, if the |
| 2200 | /// provided nested-name-specifier is non-NULL. |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2201 | static void |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2202 | AddQualifierToCompletionString(CodeCompletionBuilder &Result, |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2203 | NestedNameSpecifier *Qualifier, |
| 2204 | bool QualifierIsInformative, |
| 2205 | ASTContext &Context) { |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2206 | if (!Qualifier) |
| 2207 | return; |
| 2208 | |
| 2209 | std::string PrintedNNS; |
| 2210 | { |
| 2211 | llvm::raw_string_ostream OS(PrintedNNS); |
| 2212 | Qualifier->print(OS, Context.PrintingPolicy); |
| 2213 | } |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2214 | if (QualifierIsInformative) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2215 | Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2216 | else |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2217 | Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2220 | static void |
| 2221 | AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result, |
| 2222 | FunctionDecl *Function) { |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2223 | const FunctionProtoType *Proto |
| 2224 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2225 | if (!Proto || !Proto->getTypeQuals()) |
| 2226 | return; |
| 2227 | |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2228 | // FIXME: Add ref-qualifier! |
| 2229 | |
| 2230 | // Handle single qualifiers without copying |
| 2231 | if (Proto->getTypeQuals() == Qualifiers::Const) { |
| 2232 | Result.AddInformativeChunk(" const"); |
| 2233 | return; |
| 2234 | } |
| 2235 | |
| 2236 | if (Proto->getTypeQuals() == Qualifiers::Volatile) { |
| 2237 | Result.AddInformativeChunk(" volatile"); |
| 2238 | return; |
| 2239 | } |
| 2240 | |
| 2241 | if (Proto->getTypeQuals() == Qualifiers::Restrict) { |
| 2242 | Result.AddInformativeChunk(" restrict"); |
| 2243 | return; |
| 2244 | } |
| 2245 | |
| 2246 | // Handle multiple qualifiers. |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2247 | std::string QualsStr; |
| 2248 | if (Proto->getTypeQuals() & Qualifiers::Const) |
| 2249 | QualsStr += " const"; |
| 2250 | if (Proto->getTypeQuals() & Qualifiers::Volatile) |
| 2251 | QualsStr += " volatile"; |
| 2252 | if (Proto->getTypeQuals() & Qualifiers::Restrict) |
| 2253 | QualsStr += " restrict"; |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2254 | Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr)); |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2257 | /// \brief Add the name of the given declaration |
| 2258 | static void AddTypedNameChunk(ASTContext &Context, NamedDecl *ND, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2259 | CodeCompletionBuilder &Result) { |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2260 | typedef CodeCompletionString::Chunk Chunk; |
| 2261 | |
| 2262 | DeclarationName Name = ND->getDeclName(); |
| 2263 | if (!Name) |
| 2264 | return; |
| 2265 | |
| 2266 | switch (Name.getNameKind()) { |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2267 | case DeclarationName::CXXOperatorName: { |
| 2268 | const char *OperatorName = 0; |
| 2269 | switch (Name.getCXXOverloadedOperator()) { |
| 2270 | case OO_None: |
| 2271 | case OO_Conditional: |
| 2272 | case NUM_OVERLOADED_OPERATORS: |
| 2273 | OperatorName = "operator"; |
| 2274 | break; |
| 2275 | |
| 2276 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 2277 | case OO_##Name: OperatorName = "operator" Spelling; break; |
| 2278 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) |
| 2279 | #include "clang/Basic/OperatorKinds.def" |
| 2280 | |
| 2281 | case OO_New: OperatorName = "operator new"; break; |
| 2282 | case OO_Delete: OperatorName = "operator delete"; break; |
| 2283 | case OO_Array_New: OperatorName = "operator new[]"; break; |
| 2284 | case OO_Array_Delete: OperatorName = "operator delete[]"; break; |
| 2285 | case OO_Call: OperatorName = "operator()"; break; |
| 2286 | case OO_Subscript: OperatorName = "operator[]"; break; |
| 2287 | } |
| 2288 | Result.AddTypedTextChunk(OperatorName); |
| 2289 | break; |
| 2290 | } |
| 2291 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2292 | case DeclarationName::Identifier: |
| 2293 | case DeclarationName::CXXConversionFunctionName: |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2294 | case DeclarationName::CXXDestructorName: |
| 2295 | case DeclarationName::CXXLiteralOperatorName: |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2296 | Result.AddTypedTextChunk( |
| 2297 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2298 | break; |
| 2299 | |
| 2300 | case DeclarationName::CXXUsingDirective: |
| 2301 | case DeclarationName::ObjCZeroArgSelector: |
| 2302 | case DeclarationName::ObjCOneArgSelector: |
| 2303 | case DeclarationName::ObjCMultiArgSelector: |
| 2304 | break; |
| 2305 | |
| 2306 | case DeclarationName::CXXConstructorName: { |
| 2307 | CXXRecordDecl *Record = 0; |
| 2308 | QualType Ty = Name.getCXXNameType(); |
| 2309 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) |
| 2310 | Record = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 2311 | else if (const InjectedClassNameType *InjectedTy |
| 2312 | = Ty->getAs<InjectedClassNameType>()) |
| 2313 | Record = InjectedTy->getDecl(); |
| 2314 | else { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2315 | Result.AddTypedTextChunk( |
| 2316 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2317 | break; |
| 2318 | } |
| 2319 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2320 | Result.AddTypedTextChunk( |
| 2321 | Result.getAllocator().CopyString(Record->getNameAsString())); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2322 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2323 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2324 | AddTemplateParameterChunks(Context, Template, Result); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2325 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2326 | } |
| 2327 | break; |
| 2328 | } |
| 2329 | } |
| 2330 | } |
| 2331 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2332 | /// \brief If possible, create a new code completion string for the given |
| 2333 | /// result. |
| 2334 | /// |
| 2335 | /// \returns Either a new, heap-allocated code completion string describing |
| 2336 | /// how to use this result, or NULL to indicate that the string or name of the |
| 2337 | /// result is all that is needed. |
| 2338 | CodeCompletionString * |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2339 | CodeCompletionResult::CreateCodeCompletionString(Sema &S, |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2340 | CodeCompletionAllocator &Allocator) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2341 | typedef CodeCompletionString::Chunk Chunk; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2342 | CodeCompletionBuilder Result(Allocator, Priority, Availability); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2343 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2344 | PrintingPolicy Policy(S.Context.PrintingPolicy); |
| 2345 | Policy.AnonymousTagLocations = false; |
| 2346 | Policy.SuppressStrongLifetime = true; |
| 2347 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2348 | if (Kind == RK_Pattern) { |
| 2349 | Pattern->Priority = Priority; |
| 2350 | Pattern->Availability = Availability; |
| 2351 | return Pattern; |
| 2352 | } |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2353 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2354 | if (Kind == RK_Keyword) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2355 | Result.AddTypedTextChunk(Keyword); |
| 2356 | return Result.TakeString(); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2357 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2358 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2359 | if (Kind == RK_Macro) { |
| 2360 | MacroInfo *MI = S.PP.getMacroInfo(Macro); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2361 | assert(MI && "Not a macro?"); |
| 2362 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2363 | Result.AddTypedTextChunk( |
| 2364 | Result.getAllocator().CopyString(Macro->getName())); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2365 | |
| 2366 | if (!MI->isFunctionLike()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2367 | return Result.TakeString(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2368 | |
| 2369 | // Format a function-like macro with placeholders for the arguments. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2370 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | e424470 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2371 | bool CombineVariadicArgument = false; |
| 2372 | MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end(); |
| 2373 | if (MI->isVariadic() && AEnd - A > 1) { |
| 2374 | AEnd -= 2; |
| 2375 | CombineVariadicArgument = true; |
| 2376 | } |
| 2377 | for (MacroInfo::arg_iterator A = MI->arg_begin(); A != AEnd; ++A) { |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2378 | if (A != MI->arg_begin()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2379 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2380 | |
Douglas Gregor | e424470 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2381 | if (!MI->isVariadic() || A + 1 != AEnd) { |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2382 | // Non-variadic argument. |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2383 | Result.AddPlaceholderChunk( |
| 2384 | Result.getAllocator().CopyString((*A)->getName())); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2385 | continue; |
| 2386 | } |
| 2387 | |
Douglas Gregor | e424470 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2388 | // Variadic argument; cope with the difference between GNU and C99 |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2389 | // variadic macros, providing a single placeholder for the rest of the |
| 2390 | // arguments. |
| 2391 | if ((*A)->isStr("__VA_ARGS__")) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2392 | Result.AddPlaceholderChunk("..."); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2393 | else { |
| 2394 | std::string Arg = (*A)->getName(); |
| 2395 | Arg += "..."; |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2396 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2397 | } |
| 2398 | } |
Douglas Gregor | e424470 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2399 | |
| 2400 | if (CombineVariadicArgument) { |
| 2401 | // Handle the next-to-last argument, combining it with the variadic |
| 2402 | // argument. |
| 2403 | std::string LastArg = (*A)->getName(); |
| 2404 | ++A; |
| 2405 | if ((*A)->isStr("__VA_ARGS__")) |
| 2406 | LastArg += ", ..."; |
| 2407 | else |
| 2408 | LastArg += ", " + (*A)->getName().str() + "..."; |
| 2409 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(LastArg)); |
| 2410 | } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2411 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
| 2412 | return Result.TakeString(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2415 | assert(Kind == RK_Declaration && "Missed a result kind?"); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2416 | NamedDecl *ND = Declaration; |
| 2417 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2418 | if (StartsNestedNameSpecifier) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2419 | Result.AddTypedTextChunk( |
| 2420 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2421 | Result.AddTextChunk("::"); |
| 2422 | return Result.TakeString(); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2425 | AddResultTypeChunk(S.Context, ND, Result); |
| 2426 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2427 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2428 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2429 | S.Context); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2430 | AddTypedNameChunk(S.Context, ND, Result); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2431 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2432 | AddFunctionParameterChunks(S.Context, Function, Result); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2433 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2434 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2435 | return Result.TakeString(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2439 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2440 | S.Context); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2441 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2442 | AddTypedNameChunk(S.Context, Function, Result); |
| 2443 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2444 | // Figure out which template parameters are deduced (or have default |
| 2445 | // arguments). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2446 | SmallVector<bool, 16> Deduced; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2447 | S.MarkDeducedTemplateParameters(FunTmpl, Deduced); |
| 2448 | unsigned LastDeducibleArgument; |
| 2449 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
| 2450 | --LastDeducibleArgument) { |
| 2451 | if (!Deduced[LastDeducibleArgument - 1]) { |
| 2452 | // C++0x: Figure out if the template argument has a default. If so, |
| 2453 | // the user doesn't need to type this argument. |
| 2454 | // FIXME: We need to abstract template parameters better! |
| 2455 | bool HasDefaultArg = false; |
| 2456 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2457 | LastDeducibleArgument - 1); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2458 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 2459 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2460 | else if (NonTypeTemplateParmDecl *NTTP |
| 2461 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 2462 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2463 | else { |
| 2464 | assert(isa<TemplateTemplateParmDecl>(Param)); |
| 2465 | HasDefaultArg |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2466 | = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
| 2469 | if (!HasDefaultArg) |
| 2470 | break; |
| 2471 | } |
| 2472 | } |
| 2473 | |
| 2474 | if (LastDeducibleArgument) { |
| 2475 | // Some of the function template arguments cannot be deduced from a |
| 2476 | // function call, so we introduce an explicit template argument list |
| 2477 | // containing all of the arguments up to the first deducible argument. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2478 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2479 | AddTemplateParameterChunks(S.Context, FunTmpl, Result, |
| 2480 | LastDeducibleArgument); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2481 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | // Add the function parameters |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2485 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2486 | AddFunctionParameterChunks(S.Context, Function, Result); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2487 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2488 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2489 | return Result.TakeString(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2493 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2494 | S.Context); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2495 | Result.AddTypedTextChunk( |
| 2496 | Result.getAllocator().CopyString(Template->getNameAsString())); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2497 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2498 | AddTemplateParameterChunks(S.Context, Template, Result); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2499 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
| 2500 | return Result.TakeString(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2503 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2504 | Selector Sel = Method->getSelector(); |
| 2505 | if (Sel.isUnarySelector()) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2506 | Result.AddTypedTextChunk(Result.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2507 | Sel.getNameForSlot(0))); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2508 | return Result.TakeString(); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2511 | std::string SelName = Sel.getNameForSlot(0).str(); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2512 | SelName += ':'; |
| 2513 | if (StartParameter == 0) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2514 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName)); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2515 | else { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2516 | Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName)); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2517 | |
| 2518 | // If there is only one parameter, and we're past it, add an empty |
| 2519 | // typed-text chunk since there is nothing to type. |
| 2520 | if (Method->param_size() == 1) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2521 | Result.AddTypedTextChunk(""); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2522 | } |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2523 | unsigned Idx = 0; |
| 2524 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 2525 | PEnd = Method->param_end(); |
| 2526 | P != PEnd; (void)++P, ++Idx) { |
| 2527 | if (Idx > 0) { |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2528 | std::string Keyword; |
| 2529 | if (Idx > StartParameter) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2530 | Result.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2531 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) |
Benjamin Kramer | a0651c5 | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 2532 | Keyword += II->getName(); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2533 | Keyword += ":"; |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2534 | if (Idx < StartParameter || AllParametersAreInformative) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2535 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword)); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2536 | else |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2537 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword)); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2538 | } |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2539 | |
| 2540 | // If we're before the starting parameter, skip the placeholder. |
| 2541 | if (Idx < StartParameter) |
| 2542 | continue; |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2543 | |
| 2544 | std::string Arg; |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2545 | |
| 2546 | if ((*P)->getType()->isBlockPointerType() && !DeclaringEntity) |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2547 | Arg = FormatFunctionParameter(S.Context, *P, true); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2548 | else { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2549 | (*P)->getType().getAsStringInternal(Arg, Policy); |
Douglas Gregor | 6fa14dd | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2550 | Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier()) |
| 2551 | + Arg + ")"; |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2552 | if (IdentifierInfo *II = (*P)->getIdentifier()) |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2553 | if (DeclaringEntity || AllParametersAreInformative) |
Benjamin Kramer | a0651c5 | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 2554 | Arg += II->getName(); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2557 | if (Method->isVariadic() && (P + 1) == PEnd) |
| 2558 | Arg += ", ..."; |
| 2559 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2560 | if (DeclaringEntity) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2561 | Result.AddTextChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2562 | else if (AllParametersAreInformative) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2563 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2564 | else |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2565 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
Douglas Gregor | 2a17af0 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2568 | if (Method->isVariadic()) { |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2569 | if (Method->param_size() == 0) { |
| 2570 | if (DeclaringEntity) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2571 | Result.AddTextChunk(", ..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2572 | else if (AllParametersAreInformative) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2573 | Result.AddInformativeChunk(", ..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2574 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2575 | Result.AddPlaceholderChunk(", ..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2576 | } |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2577 | |
| 2578 | MaybeAddSentinel(S.Context, Method, Result); |
Douglas Gregor | 2a17af0 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2581 | return Result.TakeString(); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2584 | if (Qualifier) |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2585 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2586 | S.Context); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2587 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2588 | Result.AddTypedTextChunk( |
| 2589 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2590 | return Result.TakeString(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2593 | CodeCompletionString * |
| 2594 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( |
| 2595 | unsigned CurrentArg, |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 2596 | Sema &S, |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2597 | CodeCompletionAllocator &Allocator) const { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2598 | typedef CodeCompletionString::Chunk Chunk; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2599 | PrintingPolicy Policy(S.Context.PrintingPolicy); |
| 2600 | Policy.AnonymousTagLocations = false; |
| 2601 | Policy.SuppressStrongLifetime = true; |
| 2602 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2603 | // FIXME: Set priority, availability appropriately. |
| 2604 | CodeCompletionBuilder Result(Allocator, 1, CXAvailability_Available); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2605 | FunctionDecl *FDecl = getFunction(); |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2606 | AddResultTypeChunk(S.Context, FDecl, Result); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2607 | const FunctionProtoType *Proto |
| 2608 | = dyn_cast<FunctionProtoType>(getFunctionType()); |
| 2609 | if (!FDecl && !Proto) { |
| 2610 | // Function without a prototype. Just give the return type and a |
| 2611 | // highlighted ellipsis. |
| 2612 | const FunctionType *FT = getFunctionType(); |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2613 | Result.AddTextChunk(GetCompletionTypeString(FT->getResultType(), |
| 2614 | S.Context, |
| 2615 | Result.getAllocator())); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2616 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
| 2617 | Result.AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "...")); |
| 2618 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
| 2619 | return Result.TakeString(); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
| 2622 | if (FDecl) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2623 | Result.AddTextChunk( |
| 2624 | Result.getAllocator().CopyString(FDecl->getNameAsString())); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2625 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2626 | Result.AddTextChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2627 | Result.getAllocator().CopyString( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2628 | Proto->getResultType().getAsString(Policy))); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2629 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2630 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2631 | unsigned NumParams = FDecl? FDecl->getNumParams() : Proto->getNumArgs(); |
| 2632 | for (unsigned I = 0; I != NumParams; ++I) { |
| 2633 | if (I) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2634 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2635 | |
| 2636 | std::string ArgString; |
| 2637 | QualType ArgType; |
| 2638 | |
| 2639 | if (FDecl) { |
| 2640 | ArgString = FDecl->getParamDecl(I)->getNameAsString(); |
| 2641 | ArgType = FDecl->getParamDecl(I)->getOriginalType(); |
| 2642 | } else { |
| 2643 | ArgType = Proto->getArgType(I); |
| 2644 | } |
| 2645 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2646 | ArgType.getAsStringInternal(ArgString, Policy); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2647 | |
| 2648 | if (I == CurrentArg) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2649 | Result.AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2650 | Result.getAllocator().CopyString(ArgString))); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2651 | else |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2652 | Result.AddTextChunk(Result.getAllocator().CopyString(ArgString)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
| 2655 | if (Proto && Proto->isVariadic()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2656 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2657 | if (CurrentArg < NumParams) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2658 | Result.AddTextChunk("..."); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2659 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2660 | Result.AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "...")); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2661 | } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2662 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2663 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2664 | return Result.TakeString(); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2667 | unsigned clang::getMacroUsagePriority(StringRef MacroName, |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2668 | const LangOptions &LangOpts, |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2669 | bool PreferredTypeIsPointer) { |
| 2670 | unsigned Priority = CCP_Macro; |
| 2671 | |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2672 | // Treat the "nil", "Nil" and "NULL" macros as null pointer constants. |
| 2673 | if (MacroName.equals("nil") || MacroName.equals("NULL") || |
| 2674 | MacroName.equals("Nil")) { |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2675 | Priority = CCP_Constant; |
| 2676 | if (PreferredTypeIsPointer) |
| 2677 | Priority = Priority / CCF_SimilarTypeMatch; |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2678 | } |
| 2679 | // Treat "YES", "NO", "true", and "false" as constants. |
| 2680 | else if (MacroName.equals("YES") || MacroName.equals("NO") || |
| 2681 | MacroName.equals("true") || MacroName.equals("false")) |
| 2682 | Priority = CCP_Constant; |
| 2683 | // Treat "bool" as a type. |
| 2684 | else if (MacroName.equals("bool")) |
| 2685 | Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0); |
| 2686 | |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2687 | |
| 2688 | return Priority; |
| 2689 | } |
| 2690 | |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 2691 | CXCursorKind clang::getCursorKindForDecl(Decl *D) { |
| 2692 | if (!D) |
| 2693 | return CXCursor_UnexposedDecl; |
| 2694 | |
| 2695 | switch (D->getKind()) { |
| 2696 | case Decl::Enum: return CXCursor_EnumDecl; |
| 2697 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 2698 | case Decl::Field: return CXCursor_FieldDecl; |
| 2699 | case Decl::Function: |
| 2700 | return CXCursor_FunctionDecl; |
| 2701 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 2702 | case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryImplDecl; |
| 2703 | case Decl::ObjCClass: |
| 2704 | // FIXME |
| 2705 | return CXCursor_UnexposedDecl; |
| 2706 | case Decl::ObjCForwardProtocol: |
| 2707 | // FIXME |
| 2708 | return CXCursor_UnexposedDecl; |
| 2709 | case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl; |
| 2710 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
| 2711 | case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl; |
| 2712 | case Decl::ObjCMethod: |
| 2713 | return cast<ObjCMethodDecl>(D)->isInstanceMethod() |
| 2714 | ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl; |
| 2715 | case Decl::CXXMethod: return CXCursor_CXXMethod; |
| 2716 | case Decl::CXXConstructor: return CXCursor_Constructor; |
| 2717 | case Decl::CXXDestructor: return CXCursor_Destructor; |
| 2718 | case Decl::CXXConversion: return CXCursor_ConversionFunction; |
| 2719 | case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl; |
| 2720 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
| 2721 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 2722 | case Decl::Typedef: return CXCursor_TypedefDecl; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2723 | case Decl::TypeAlias: return CXCursor_TypeAliasDecl; |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 2724 | case Decl::Var: return CXCursor_VarDecl; |
| 2725 | case Decl::Namespace: return CXCursor_Namespace; |
| 2726 | case Decl::NamespaceAlias: return CXCursor_NamespaceAlias; |
| 2727 | case Decl::TemplateTypeParm: return CXCursor_TemplateTypeParameter; |
| 2728 | case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter; |
| 2729 | case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter; |
| 2730 | case Decl::FunctionTemplate: return CXCursor_FunctionTemplate; |
| 2731 | case Decl::ClassTemplate: return CXCursor_ClassTemplate; |
| 2732 | case Decl::ClassTemplatePartialSpecialization: |
| 2733 | return CXCursor_ClassTemplatePartialSpecialization; |
| 2734 | case Decl::UsingDirective: return CXCursor_UsingDirective; |
| 2735 | |
| 2736 | case Decl::Using: |
| 2737 | case Decl::UnresolvedUsingValue: |
| 2738 | case Decl::UnresolvedUsingTypename: |
| 2739 | return CXCursor_UsingDeclaration; |
| 2740 | |
Douglas Gregor | 352697a | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 2741 | case Decl::ObjCPropertyImpl: |
| 2742 | switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) { |
| 2743 | case ObjCPropertyImplDecl::Dynamic: |
| 2744 | return CXCursor_ObjCDynamicDecl; |
| 2745 | |
| 2746 | case ObjCPropertyImplDecl::Synthesize: |
| 2747 | return CXCursor_ObjCSynthesizeDecl; |
| 2748 | } |
| 2749 | break; |
| 2750 | |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 2751 | default: |
| 2752 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 2753 | switch (TD->getTagKind()) { |
| 2754 | case TTK_Struct: return CXCursor_StructDecl; |
| 2755 | case TTK_Class: return CXCursor_ClassDecl; |
| 2756 | case TTK_Union: return CXCursor_UnionDecl; |
| 2757 | case TTK_Enum: return CXCursor_EnumDecl; |
| 2758 | } |
| 2759 | } |
| 2760 | } |
| 2761 | |
| 2762 | return CXCursor_UnexposedDecl; |
| 2763 | } |
| 2764 | |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2765 | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, |
| 2766 | bool TargetTypeIsPointer = false) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2767 | typedef CodeCompletionResult Result; |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2768 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2769 | Results.EnterNewScope(); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2770 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2771 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 2772 | MEnd = PP.macro_end(); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2773 | M != MEnd; ++M) { |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2774 | Results.AddResult(Result(M->first, |
| 2775 | getMacroUsagePriority(M->first->getName(), |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2776 | PP.getLangOptions(), |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2777 | TargetTypeIsPointer))); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2778 | } |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2779 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2780 | Results.ExitScope(); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2781 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2784 | static void AddPrettyFunctionResults(const LangOptions &LangOpts, |
| 2785 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2786 | typedef CodeCompletionResult Result; |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2787 | |
| 2788 | Results.EnterNewScope(); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2789 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2790 | Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant)); |
| 2791 | Results.AddResult(Result("__FUNCTION__", CCP_Constant)); |
| 2792 | if (LangOpts.C99 || LangOpts.CPlusPlus0x) |
| 2793 | Results.AddResult(Result("__func__", CCP_Constant)); |
| 2794 | Results.ExitScope(); |
| 2795 | } |
| 2796 | |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2797 | static void HandleCodeCompleteResults(Sema *S, |
| 2798 | CodeCompleteConsumer *CodeCompleter, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2799 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2800 | CodeCompletionResult *Results, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2801 | unsigned NumResults) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2802 | if (CodeCompleter) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2803 | CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2806 | static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S, |
| 2807 | Sema::ParserCompletionContext PCC) { |
| 2808 | switch (PCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2809 | case Sema::PCC_Namespace: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2810 | return CodeCompletionContext::CCC_TopLevel; |
| 2811 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2812 | case Sema::PCC_Class: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2813 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 2814 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2815 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2816 | return CodeCompletionContext::CCC_ObjCInterface; |
| 2817 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2818 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2819 | return CodeCompletionContext::CCC_ObjCImplementation; |
| 2820 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2821 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2822 | return CodeCompletionContext::CCC_ObjCIvarList; |
| 2823 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2824 | case Sema::PCC_Template: |
| 2825 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2826 | if (S.CurContext->isFileContext()) |
| 2827 | return CodeCompletionContext::CCC_TopLevel; |
| 2828 | else if (S.CurContext->isRecord()) |
| 2829 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 2830 | else |
| 2831 | return CodeCompletionContext::CCC_Other; |
| 2832 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2833 | case Sema::PCC_RecoveryInFunction: |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2834 | return CodeCompletionContext::CCC_Recovery; |
Douglas Gregor | a5450a0 | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 2835 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2836 | case Sema::PCC_ForInit: |
Douglas Gregor | a5450a0 | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 2837 | if (S.getLangOptions().CPlusPlus || S.getLangOptions().C99 || |
| 2838 | S.getLangOptions().ObjC1) |
| 2839 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
| 2840 | else |
| 2841 | return CodeCompletionContext::CCC_Expression; |
| 2842 | |
| 2843 | case Sema::PCC_Expression: |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2844 | case Sema::PCC_Condition: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2845 | return CodeCompletionContext::CCC_Expression; |
| 2846 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2847 | case Sema::PCC_Statement: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2848 | return CodeCompletionContext::CCC_Statement; |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2849 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2850 | case Sema::PCC_Type: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2851 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2852 | |
| 2853 | case Sema::PCC_ParenthesizedExpression: |
| 2854 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2855 | |
| 2856 | case Sema::PCC_LocalDeclarationSpecifiers: |
| 2857 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | return CodeCompletionContext::CCC_Other; |
| 2861 | } |
| 2862 | |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2863 | /// \brief If we're in a C++ virtual member function, add completion results |
| 2864 | /// that invoke the functions we override, since it's common to invoke the |
| 2865 | /// overridden function as well as adding new functionality. |
| 2866 | /// |
| 2867 | /// \param S The semantic analysis object for which we are generating results. |
| 2868 | /// |
| 2869 | /// \param InContext This context in which the nested-name-specifier preceding |
| 2870 | /// the code-completion point |
| 2871 | static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, |
| 2872 | ResultBuilder &Results) { |
| 2873 | // Look through blocks. |
| 2874 | DeclContext *CurContext = S.CurContext; |
| 2875 | while (isa<BlockDecl>(CurContext)) |
| 2876 | CurContext = CurContext->getParent(); |
| 2877 | |
| 2878 | |
| 2879 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext); |
| 2880 | if (!Method || !Method->isVirtual()) |
| 2881 | return; |
| 2882 | |
| 2883 | // We need to have names for all of the parameters, if we're going to |
| 2884 | // generate a forwarding call. |
| 2885 | for (CXXMethodDecl::param_iterator P = Method->param_begin(), |
| 2886 | PEnd = Method->param_end(); |
| 2887 | P != PEnd; |
| 2888 | ++P) { |
| 2889 | if (!(*P)->getDeclName()) |
| 2890 | return; |
| 2891 | } |
| 2892 | |
| 2893 | for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(), |
| 2894 | MEnd = Method->end_overridden_methods(); |
| 2895 | M != MEnd; ++M) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2896 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2897 | CXXMethodDecl *Overridden = const_cast<CXXMethodDecl *>(*M); |
| 2898 | if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl()) |
| 2899 | continue; |
| 2900 | |
| 2901 | // If we need a nested-name-specifier, add one now. |
| 2902 | if (!InContext) { |
| 2903 | NestedNameSpecifier *NNS |
| 2904 | = getRequiredQualification(S.Context, CurContext, |
| 2905 | Overridden->getDeclContext()); |
| 2906 | if (NNS) { |
| 2907 | std::string Str; |
| 2908 | llvm::raw_string_ostream OS(Str); |
| 2909 | NNS->print(OS, S.Context.PrintingPolicy); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2910 | Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str())); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2911 | } |
| 2912 | } else if (!InContext->Equals(Overridden->getDeclContext())) |
| 2913 | continue; |
| 2914 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2915 | Builder.AddTypedTextChunk(Results.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2916 | Overridden->getNameAsString())); |
| 2917 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2918 | bool FirstParam = true; |
| 2919 | for (CXXMethodDecl::param_iterator P = Method->param_begin(), |
| 2920 | PEnd = Method->param_end(); |
| 2921 | P != PEnd; ++P) { |
| 2922 | if (FirstParam) |
| 2923 | FirstParam = false; |
| 2924 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2925 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2926 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2927 | Builder.AddPlaceholderChunk(Results.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2928 | (*P)->getIdentifier()->getName())); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2929 | } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2930 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2931 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2932 | CCP_SuperCompletion, |
| 2933 | CXCursor_CXXMethod)); |
| 2934 | Results.Ignore(Overridden); |
| 2935 | } |
| 2936 | } |
| 2937 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2938 | void Sema::CodeCompleteOrdinaryName(Scope *S, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2939 | ParserCompletionContext CompletionContext) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2940 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2941 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2942 | mapCodeCompletionContext(*this, CompletionContext)); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2943 | Results.EnterNewScope(); |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 2944 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2945 | // Determine how to filter results, e.g., so that the names of |
| 2946 | // values (functions, enumerators, function templates, etc.) are |
| 2947 | // only allowed where we can have an expression. |
| 2948 | switch (CompletionContext) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2949 | case PCC_Namespace: |
| 2950 | case PCC_Class: |
| 2951 | case PCC_ObjCInterface: |
| 2952 | case PCC_ObjCImplementation: |
| 2953 | case PCC_ObjCInstanceVariableList: |
| 2954 | case PCC_Template: |
| 2955 | case PCC_MemberTemplate: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2956 | case PCC_Type: |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2957 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2958 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 2959 | break; |
| 2960 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2961 | case PCC_Statement: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2962 | case PCC_ParenthesizedExpression: |
Douglas Gregor | eb0d014 | 2010-08-24 23:58:17 +0000 | [diff] [blame] | 2963 | case PCC_Expression: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2964 | case PCC_ForInit: |
| 2965 | case PCC_Condition: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 2966 | if (WantTypesInContext(CompletionContext, getLangOptions())) |
| 2967 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 2968 | else |
| 2969 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2970 | |
| 2971 | if (getLangOptions().CPlusPlus) |
| 2972 | MaybeAddOverrideCalls(*this, /*InContext=*/0, Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2973 | break; |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 2974 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2975 | case PCC_RecoveryInFunction: |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 2976 | // Unfiltered |
| 2977 | break; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 2980 | // If we are in a C++ non-static member function, check the qualifiers on |
| 2981 | // the member function to filter/prioritize the results list. |
| 2982 | if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext)) |
| 2983 | if (CurMethod->isInstance()) |
| 2984 | Results.setObjectTypeQualifiers( |
| 2985 | Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers())); |
| 2986 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 2987 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2988 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 2989 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 2a7925c | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 2990 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2991 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); |
Douglas Gregor | 2a7925c | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 2992 | Results.ExitScope(); |
| 2993 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2994 | switch (CompletionContext) { |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2995 | case PCC_ParenthesizedExpression: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2996 | case PCC_Expression: |
| 2997 | case PCC_Statement: |
| 2998 | case PCC_RecoveryInFunction: |
| 2999 | if (S->getFnParent()) |
| 3000 | AddPrettyFunctionResults(PP.getLangOptions(), Results); |
| 3001 | break; |
| 3002 | |
| 3003 | case PCC_Namespace: |
| 3004 | case PCC_Class: |
| 3005 | case PCC_ObjCInterface: |
| 3006 | case PCC_ObjCImplementation: |
| 3007 | case PCC_ObjCInstanceVariableList: |
| 3008 | case PCC_Template: |
| 3009 | case PCC_MemberTemplate: |
| 3010 | case PCC_ForInit: |
| 3011 | case PCC_Condition: |
| 3012 | case PCC_Type: |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3013 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3014 | break; |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3017 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3018 | AddMacroResults(PP, Results); |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3019 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 3020 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3021 | Results.data(),Results.size()); |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3024 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| 3025 | ParsedType Receiver, |
| 3026 | IdentifierInfo **SelIdents, |
| 3027 | unsigned NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 3028 | bool AtArgumentExpression, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3029 | bool IsSuper, |
| 3030 | ResultBuilder &Results); |
| 3031 | |
| 3032 | void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, |
| 3033 | bool AllowNonIdentifiers, |
| 3034 | bool AllowNestedNameSpecifiers) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3035 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3036 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3037 | AllowNestedNameSpecifiers |
| 3038 | ? CodeCompletionContext::CCC_PotentiallyQualifiedName |
| 3039 | : CodeCompletionContext::CCC_Name); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3040 | Results.EnterNewScope(); |
| 3041 | |
| 3042 | // Type qualifiers can come after names. |
| 3043 | Results.AddResult(Result("const")); |
| 3044 | Results.AddResult(Result("volatile")); |
| 3045 | if (getLangOptions().C99) |
| 3046 | Results.AddResult(Result("restrict")); |
| 3047 | |
| 3048 | if (getLangOptions().CPlusPlus) { |
| 3049 | if (AllowNonIdentifiers) { |
| 3050 | Results.AddResult(Result("operator")); |
| 3051 | } |
| 3052 | |
| 3053 | // Add nested-name-specifiers. |
| 3054 | if (AllowNestedNameSpecifiers) { |
| 3055 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3056 | Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3057 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3058 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
| 3059 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3060 | Results.setFilter(0); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3061 | } |
| 3062 | } |
| 3063 | Results.ExitScope(); |
| 3064 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3065 | // If we're in a context where we might have an expression (rather than a |
| 3066 | // declaration), and what we've seen so far is an Objective-C type that could |
| 3067 | // be a receiver of a class message, this may be a class message send with |
| 3068 | // the initial opening bracket '[' missing. Add appropriate completions. |
| 3069 | if (AllowNonIdentifiers && !AllowNestedNameSpecifiers && |
| 3070 | DS.getTypeSpecType() == DeclSpec::TST_typename && |
| 3071 | DS.getStorageClassSpecAsWritten() == DeclSpec::SCS_unspecified && |
| 3072 | !DS.isThreadSpecified() && !DS.isExternInLinkageSpec() && |
| 3073 | DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified && |
| 3074 | DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 3075 | DS.getTypeQualifiers() == 0 && |
| 3076 | S && |
| 3077 | (S->getFlags() & Scope::DeclScope) != 0 && |
| 3078 | (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope | |
| 3079 | Scope::FunctionPrototypeScope | |
| 3080 | Scope::AtCatchScope)) == 0) { |
| 3081 | ParsedType T = DS.getRepAsType(); |
| 3082 | if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType()) |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 3083 | AddClassMessageCompletions(*this, S, T, 0, 0, false, false, Results); |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3084 | } |
| 3085 | |
Douglas Gregor | 4497dd4 | 2010-08-24 04:59:56 +0000 | [diff] [blame] | 3086 | // Note that we intentionally suppress macro results here, since we do not |
| 3087 | // encourage using macros to produce the names of entities. |
| 3088 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3089 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3090 | Results.getCompletionContext(), |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3091 | Results.data(), Results.size()); |
| 3092 | } |
| 3093 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3094 | struct Sema::CodeCompleteExpressionData { |
| 3095 | CodeCompleteExpressionData(QualType PreferredType = QualType()) |
| 3096 | : PreferredType(PreferredType), IntegralConstantExpression(false), |
| 3097 | ObjCCollection(false) { } |
| 3098 | |
| 3099 | QualType PreferredType; |
| 3100 | bool IntegralConstantExpression; |
| 3101 | bool ObjCCollection; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3102 | SmallVector<Decl *, 4> IgnoreDecls; |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3103 | }; |
| 3104 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3105 | /// \brief Perform code-completion in an expression context when we know what |
| 3106 | /// type we're looking for. |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3107 | /// |
| 3108 | /// \param IntegralConstantExpression Only permit integral constant |
| 3109 | /// expressions. |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3110 | void Sema::CodeCompleteExpression(Scope *S, |
| 3111 | const CodeCompleteExpressionData &Data) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3112 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3113 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3114 | CodeCompletionContext::CCC_Expression); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3115 | if (Data.ObjCCollection) |
| 3116 | Results.setFilter(&ResultBuilder::IsObjCCollection); |
| 3117 | else if (Data.IntegralConstantExpression) |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3118 | Results.setFilter(&ResultBuilder::IsIntegralConstantValue); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3119 | else if (WantTypesInContext(PCC_Expression, getLangOptions())) |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3120 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 3121 | else |
| 3122 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3123 | |
| 3124 | if (!Data.PreferredType.isNull()) |
| 3125 | Results.setPreferredType(Data.PreferredType.getNonReferenceType()); |
| 3126 | |
| 3127 | // Ignore any declarations that we were told that we don't care about. |
| 3128 | for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I) |
| 3129 | Results.Ignore(Data.IgnoreDecls[I]); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3130 | |
| 3131 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3132 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3133 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3134 | |
| 3135 | Results.EnterNewScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3136 | AddOrdinaryNameResults(PCC_Expression, S, *this, Results); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3137 | Results.ExitScope(); |
| 3138 | |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3139 | bool PreferredTypeIsPointer = false; |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3140 | if (!Data.PreferredType.isNull()) |
| 3141 | PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType() |
| 3142 | || Data.PreferredType->isMemberPointerType() |
| 3143 | || Data.PreferredType->isBlockPointerType(); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3144 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3145 | if (S->getFnParent() && |
| 3146 | !Data.ObjCCollection && |
| 3147 | !Data.IntegralConstantExpression) |
| 3148 | AddPrettyFunctionResults(PP.getLangOptions(), Results); |
| 3149 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3150 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3151 | AddMacroResults(PP, Results, PreferredTypeIsPointer); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3152 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3153 | CodeCompletionContext(CodeCompletionContext::CCC_Expression, |
| 3154 | Data.PreferredType), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3155 | Results.data(),Results.size()); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
Douglas Gregor | ac5fd84 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 3158 | void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) { |
| 3159 | if (E.isInvalid()) |
| 3160 | CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction); |
| 3161 | else if (getLangOptions().ObjC1) |
| 3162 | CodeCompleteObjCInstanceMessage(S, E.take(), 0, 0, false); |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 3163 | } |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3164 | |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3165 | /// \brief The set of properties that have already been added, referenced by |
| 3166 | /// property name. |
| 3167 | typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet; |
| 3168 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3169 | static void AddObjCProperties(ObjCContainerDecl *Container, |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3170 | bool AllowCategories, |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3171 | bool AllowNullaryMethods, |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3172 | DeclContext *CurContext, |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3173 | AddedPropertiesSet &AddedProperties, |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3174 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3175 | typedef CodeCompletionResult Result; |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3176 | |
| 3177 | // Add properties in this container. |
| 3178 | for (ObjCContainerDecl::prop_iterator P = Container->prop_begin(), |
| 3179 | PEnd = Container->prop_end(); |
| 3180 | P != PEnd; |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3181 | ++P) { |
| 3182 | if (AddedProperties.insert(P->getIdentifier())) |
| 3183 | Results.MaybeAddResult(Result(*P, 0), CurContext); |
| 3184 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3185 | |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3186 | // Add nullary methods |
| 3187 | if (AllowNullaryMethods) { |
| 3188 | ASTContext &Context = Container->getASTContext(); |
| 3189 | for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), |
| 3190 | MEnd = Container->meth_end(); |
| 3191 | M != MEnd; ++M) { |
| 3192 | if (M->getSelector().isUnarySelector()) |
| 3193 | if (IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0)) |
| 3194 | if (AddedProperties.insert(Name)) { |
| 3195 | CodeCompletionBuilder Builder(Results.getAllocator()); |
| 3196 | AddResultTypeChunk(Context, *M, Builder); |
| 3197 | Builder.AddTypedTextChunk( |
| 3198 | Results.getAllocator().CopyString(Name->getName())); |
| 3199 | |
| 3200 | CXAvailabilityKind Availability = CXAvailability_Available; |
| 3201 | switch (M->getAvailability()) { |
| 3202 | case AR_Available: |
| 3203 | case AR_NotYetIntroduced: |
| 3204 | Availability = CXAvailability_Available; |
| 3205 | break; |
| 3206 | |
| 3207 | case AR_Deprecated: |
| 3208 | Availability = CXAvailability_Deprecated; |
| 3209 | break; |
| 3210 | |
| 3211 | case AR_Unavailable: |
| 3212 | Availability = CXAvailability_NotAvailable; |
| 3213 | break; |
| 3214 | } |
| 3215 | |
| 3216 | Results.MaybeAddResult(Result(Builder.TakeString(), |
| 3217 | CCP_MemberDeclaration + CCD_MethodAsProperty, |
| 3218 | M->isInstanceMethod() |
| 3219 | ? CXCursor_ObjCInstanceMethodDecl |
| 3220 | : CXCursor_ObjCClassMethodDecl, |
| 3221 | Availability), |
| 3222 | CurContext); |
| 3223 | } |
| 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3228 | // Add properties in referenced protocols. |
| 3229 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 3230 | for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(), |
| 3231 | PEnd = Protocol->protocol_end(); |
| 3232 | P != PEnd; ++P) |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3233 | AddObjCProperties(*P, AllowCategories, AllowNullaryMethods, CurContext, |
| 3234 | AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3235 | } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){ |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3236 | if (AllowCategories) { |
| 3237 | // Look through categories. |
| 3238 | for (ObjCCategoryDecl *Category = IFace->getCategoryList(); |
| 3239 | Category; Category = Category->getNextClassCategory()) |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3240 | AddObjCProperties(Category, AllowCategories, AllowNullaryMethods, |
| 3241 | CurContext, AddedProperties, Results); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3242 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3243 | |
| 3244 | // Look through protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 3245 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 3246 | I = IFace->all_referenced_protocol_begin(), |
| 3247 | E = IFace->all_referenced_protocol_end(); I != E; ++I) |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3248 | AddObjCProperties(*I, AllowCategories, AllowNullaryMethods, CurContext, |
| 3249 | AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3250 | |
| 3251 | // Look in the superclass. |
| 3252 | if (IFace->getSuperClass()) |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3253 | AddObjCProperties(IFace->getSuperClass(), AllowCategories, |
| 3254 | AllowNullaryMethods, CurContext, |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3255 | AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3256 | } else if (const ObjCCategoryDecl *Category |
| 3257 | = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 3258 | // Look through protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 3259 | for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(), |
| 3260 | PEnd = Category->protocol_end(); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3261 | P != PEnd; ++P) |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3262 | AddObjCProperties(*P, AllowCategories, AllowNullaryMethods, CurContext, |
| 3263 | AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3264 | } |
| 3265 | } |
| 3266 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3267 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, ExprTy *BaseE, |
| 3268 | SourceLocation OpLoc, |
| 3269 | bool IsArrow) { |
| 3270 | if (!BaseE || !CodeCompleter) |
| 3271 | return; |
| 3272 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3273 | typedef CodeCompletionResult Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3274 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3275 | Expr *Base = static_cast<Expr *>(BaseE); |
| 3276 | QualType BaseType = Base->getType(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3277 | |
| 3278 | if (IsArrow) { |
| 3279 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 3280 | BaseType = Ptr->getPointeeType(); |
| 3281 | else if (BaseType->isObjCObjectPointerType()) |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3282 | /*Do nothing*/ ; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3283 | else |
| 3284 | return; |
| 3285 | } |
| 3286 | |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3287 | enum CodeCompletionContext::Kind contextKind; |
| 3288 | |
| 3289 | if (IsArrow) { |
| 3290 | contextKind = CodeCompletionContext::CCC_ArrowMemberAccess; |
| 3291 | } |
| 3292 | else { |
| 3293 | if (BaseType->isObjCObjectPointerType() || |
| 3294 | BaseType->isObjCObjectOrInterfaceType()) { |
| 3295 | contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess; |
| 3296 | } |
| 3297 | else { |
| 3298 | contextKind = CodeCompletionContext::CCC_DotMemberAccess; |
| 3299 | } |
| 3300 | } |
| 3301 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3302 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3303 | CodeCompletionContext(contextKind, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3304 | BaseType), |
| 3305 | &ResultBuilder::IsMember); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3306 | Results.EnterNewScope(); |
| 3307 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3308 | // Indicate that we are performing a member access, and the cv-qualifiers |
| 3309 | // for the base object type. |
| 3310 | Results.setObjectTypeQualifiers(BaseType.getQualifiers()); |
| 3311 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3312 | // Access to a C/C++ class, struct, or union. |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 3313 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3314 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3315 | LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer, |
| 3316 | CodeCompleter->includeGlobals()); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3317 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3318 | if (getLangOptions().CPlusPlus) { |
| 3319 | if (!Results.empty()) { |
| 3320 | // The "template" keyword can follow "->" or "." in the grammar. |
| 3321 | // However, we only want to suggest the template keyword if something |
| 3322 | // is dependent. |
| 3323 | bool IsDependent = BaseType->isDependentType(); |
| 3324 | if (!IsDependent) { |
| 3325 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) |
| 3326 | if (DeclContext *Ctx = (DeclContext *)DepScope->getEntity()) { |
| 3327 | IsDependent = Ctx->isDependentContext(); |
| 3328 | break; |
| 3329 | } |
| 3330 | } |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3331 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3332 | if (IsDependent) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3333 | Results.AddResult(Result("template")); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3334 | } |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3335 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3336 | } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) { |
| 3337 | // Objective-C property reference. |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3338 | AddedPropertiesSet AddedProperties; |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3339 | |
| 3340 | // Add property results based on our interface. |
| 3341 | const ObjCObjectPointerType *ObjCPtr |
| 3342 | = BaseType->getAsObjCInterfacePointerType(); |
| 3343 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!"); |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3344 | AddObjCProperties(ObjCPtr->getInterfaceDecl(), true, |
| 3345 | /*AllowNullaryMethods=*/true, CurContext, |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3346 | AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3347 | |
| 3348 | // Add properties from the protocols in a qualified interface. |
| 3349 | for (ObjCObjectPointerType::qual_iterator I = ObjCPtr->qual_begin(), |
| 3350 | E = ObjCPtr->qual_end(); |
| 3351 | I != E; ++I) |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3352 | AddObjCProperties(*I, true, /*AllowNullaryMethods=*/true, CurContext, |
| 3353 | AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3354 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3355 | (!IsArrow && BaseType->isObjCObjectType())) { |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3356 | // Objective-C instance variable access. |
| 3357 | ObjCInterfaceDecl *Class = 0; |
| 3358 | if (const ObjCObjectPointerType *ObjCPtr |
| 3359 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 3360 | Class = ObjCPtr->getInterfaceDecl(); |
| 3361 | else |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3362 | Class = BaseType->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3363 | |
| 3364 | // Add all ivars from this class and its superclasses. |
Douglas Gregor | 80f4f4c | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 3365 | if (Class) { |
| 3366 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3367 | Results.setFilter(&ResultBuilder::IsObjCIvar); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3368 | LookupVisibleDecls(Class, LookupMemberName, Consumer, |
| 3369 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3370 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3371 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3372 | |
| 3373 | // FIXME: How do we cope with isa? |
| 3374 | |
| 3375 | Results.ExitScope(); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3376 | |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3377 | // Hand off the results found for code completion. |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3378 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3379 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3380 | Results.data(),Results.size()); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3383 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { |
| 3384 | if (!CodeCompleter) |
| 3385 | return; |
| 3386 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3387 | typedef CodeCompletionResult Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3388 | ResultBuilder::LookupFilter Filter = 0; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3389 | enum CodeCompletionContext::Kind ContextKind |
| 3390 | = CodeCompletionContext::CCC_Other; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3391 | switch ((DeclSpec::TST)TagSpec) { |
| 3392 | case DeclSpec::TST_enum: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3393 | Filter = &ResultBuilder::IsEnum; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3394 | ContextKind = CodeCompletionContext::CCC_EnumTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3395 | break; |
| 3396 | |
| 3397 | case DeclSpec::TST_union: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3398 | Filter = &ResultBuilder::IsUnion; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3399 | ContextKind = CodeCompletionContext::CCC_UnionTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3400 | break; |
| 3401 | |
| 3402 | case DeclSpec::TST_struct: |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3403 | case DeclSpec::TST_class: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3404 | Filter = &ResultBuilder::IsClassOrStruct; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3405 | ContextKind = CodeCompletionContext::CCC_ClassOrStructTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3406 | break; |
| 3407 | |
| 3408 | default: |
| 3409 | assert(false && "Unknown type specifier kind in CodeCompleteTag"); |
| 3410 | return; |
| 3411 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3412 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3413 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), ContextKind); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3414 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 3415 | |
| 3416 | // First pass: look for tags. |
| 3417 | Results.setFilter(Filter); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3418 | LookupVisibleDecls(S, LookupTagName, Consumer, |
| 3419 | CodeCompleter->includeGlobals()); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 3420 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3421 | if (CodeCompleter->includeGlobals()) { |
| 3422 | // Second pass: look for nested name specifiers. |
| 3423 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
| 3424 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer); |
| 3425 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3426 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3427 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3428 | Results.data(),Results.size()); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3431 | void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3432 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3433 | CodeCompletionContext::CCC_TypeQualifiers); |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3434 | Results.EnterNewScope(); |
| 3435 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const)) |
| 3436 | Results.AddResult("const"); |
| 3437 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile)) |
| 3438 | Results.AddResult("volatile"); |
| 3439 | if (getLangOptions().C99 && |
| 3440 | !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict)) |
| 3441 | Results.AddResult("restrict"); |
| 3442 | Results.ExitScope(); |
| 3443 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3444 | Results.getCompletionContext(), |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3445 | Results.data(), Results.size()); |
| 3446 | } |
| 3447 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3448 | void Sema::CodeCompleteCase(Scope *S) { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3449 | if (getCurFunction()->SwitchStack.empty() || !CodeCompleter) |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3450 | return; |
John McCall | a8e0cd8 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 3451 | |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3452 | SwitchStmt *Switch = getCurFunction()->SwitchStack.back(); |
John McCall | a8e0cd8 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 3453 | QualType type = Switch->getCond()->IgnoreImplicit()->getType(); |
| 3454 | if (!type->isEnumeralType()) { |
| 3455 | CodeCompleteExpressionData Data(type); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3456 | Data.IntegralConstantExpression = true; |
| 3457 | CodeCompleteExpression(S, Data); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3458 | return; |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3459 | } |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3460 | |
| 3461 | // Code-complete the cases of a switch statement over an enumeration type |
| 3462 | // by providing the list of |
John McCall | a8e0cd8 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 3463 | EnumDecl *Enum = type->castAs<EnumType>()->getDecl(); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3464 | |
| 3465 | // Determine which enumerators we have already seen in the switch statement. |
| 3466 | // FIXME: Ideally, we would also be able to look *past* the code-completion |
| 3467 | // token, in case we are code-completing in the middle of the switch and not |
| 3468 | // at the end. However, we aren't able to do so at the moment. |
| 3469 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3470 | NestedNameSpecifier *Qualifier = 0; |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3471 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; |
| 3472 | SC = SC->getNextSwitchCase()) { |
| 3473 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); |
| 3474 | if (!Case) |
| 3475 | continue; |
| 3476 | |
| 3477 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); |
| 3478 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal)) |
| 3479 | if (EnumConstantDecl *Enumerator |
| 3480 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 3481 | // We look into the AST of the case statement to determine which |
| 3482 | // enumerator was named. Alternatively, we could compute the value of |
| 3483 | // the integral constant expression, then compare it against the |
| 3484 | // values of each enumerator. However, value-based approach would not |
| 3485 | // work as well with C++ templates where enumerators declared within a |
| 3486 | // template are type- and value-dependent. |
| 3487 | EnumeratorsSeen.insert(Enumerator); |
| 3488 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3489 | // If this is a qualified-id, keep track of the nested-name-specifier |
| 3490 | // 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] | 3491 | // |
| 3492 | // switch (TagD.getKind()) { |
| 3493 | // case TagDecl::TK_enum: |
| 3494 | // break; |
| 3495 | // case XXX |
| 3496 | // |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3497 | // At the XXX, our completions are TagDecl::TK_union, |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3498 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, |
| 3499 | // TK_struct, and TK_class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3500 | Qualifier = DRE->getQualifier(); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3501 | } |
| 3502 | } |
| 3503 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3504 | if (getLangOptions().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { |
| 3505 | // If there are no prior enumerators in C++, check whether we have to |
| 3506 | // qualify the names of the enumerators that we suggest, because they |
| 3507 | // may not be visible in this scope. |
| 3508 | Qualifier = getRequiredQualification(Context, CurContext, |
| 3509 | Enum->getDeclContext()); |
| 3510 | |
| 3511 | // FIXME: Scoped enums need to start with "EnumDecl" as the context! |
| 3512 | } |
| 3513 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3514 | // Add any enumerators that have not yet been mentioned. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3515 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3516 | CodeCompletionContext::CCC_Expression); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3517 | Results.EnterNewScope(); |
| 3518 | for (EnumDecl::enumerator_iterator E = Enum->enumerator_begin(), |
| 3519 | EEnd = Enum->enumerator_end(); |
| 3520 | E != EEnd; ++E) { |
| 3521 | if (EnumeratorsSeen.count(*E)) |
| 3522 | continue; |
| 3523 | |
Douglas Gregor | 5c722c70 | 2011-02-18 23:30:37 +0000 | [diff] [blame] | 3524 | CodeCompletionResult R(*E, Qualifier); |
| 3525 | R.Priority = CCP_EnumInCase; |
| 3526 | Results.AddResult(R, CurContext, 0, false); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3527 | } |
| 3528 | Results.ExitScope(); |
Douglas Gregor | 2f880e4 | 2010-04-06 20:02:15 +0000 | [diff] [blame] | 3529 | |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3530 | //We need to make sure we're setting the right context, |
| 3531 | //so only say we include macros if the code completer says we do |
| 3532 | enum CodeCompletionContext::Kind kind = CodeCompletionContext::CCC_Other; |
| 3533 | if (CodeCompleter->includeMacros()) { |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3534 | AddMacroResults(PP, Results); |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3535 | kind = CodeCompletionContext::CCC_OtherWithMacros; |
| 3536 | } |
| 3537 | |
| 3538 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3539 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3540 | kind, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3541 | Results.data(),Results.size()); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3544 | namespace { |
| 3545 | struct IsBetterOverloadCandidate { |
| 3546 | Sema &S; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3547 | SourceLocation Loc; |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3548 | |
| 3549 | public: |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3550 | explicit IsBetterOverloadCandidate(Sema &S, SourceLocation Loc) |
| 3551 | : S(S), Loc(Loc) { } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3552 | |
| 3553 | bool |
| 3554 | operator()(const OverloadCandidate &X, const OverloadCandidate &Y) const { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3555 | return isBetterOverloadCandidate(S, X, Y, Loc); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3556 | } |
| 3557 | }; |
| 3558 | } |
| 3559 | |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3560 | static bool anyNullArguments(Expr **Args, unsigned NumArgs) { |
| 3561 | if (NumArgs && !Args) |
| 3562 | return true; |
| 3563 | |
| 3564 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3565 | if (!Args[I]) |
| 3566 | return true; |
| 3567 | |
| 3568 | return false; |
| 3569 | } |
| 3570 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3571 | void Sema::CodeCompleteCall(Scope *S, ExprTy *FnIn, |
| 3572 | ExprTy **ArgsIn, unsigned NumArgs) { |
| 3573 | if (!CodeCompleter) |
| 3574 | return; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3575 | |
| 3576 | // When we're code-completing for a call, we fall back to ordinary |
| 3577 | // name code-completion whenever we can't produce specific |
| 3578 | // results. We may want to revisit this strategy in the future, |
| 3579 | // e.g., by merging the two kinds of results. |
| 3580 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3581 | Expr *Fn = (Expr *)FnIn; |
| 3582 | Expr **Args = (Expr **)ArgsIn; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3583 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3584 | // Ignore type-dependent call expressions entirely. |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3585 | if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args, NumArgs) || |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3586 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3587 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3588 | return; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3589 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3590 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3591 | // Build an overload candidate set based on the functions we find. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3592 | SourceLocation Loc = Fn->getExprLoc(); |
| 3593 | OverloadCandidateSet CandidateSet(Loc); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3594 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3595 | // FIXME: What if we're calling something that isn't a function declaration? |
| 3596 | // FIXME: What if we're calling a pseudo-destructor? |
| 3597 | // FIXME: What if we're calling a member function? |
| 3598 | |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3599 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3600 | SmallVector<ResultCandidate, 8> Results; |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3601 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3602 | Expr *NakedFn = Fn->IgnoreParenCasts(); |
| 3603 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) |
| 3604 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet, |
| 3605 | /*PartialOverloading=*/ true); |
| 3606 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(NakedFn)) { |
| 3607 | FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3608 | if (FDecl) { |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3609 | if (!getLangOptions().CPlusPlus || |
| 3610 | !FDecl->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3611 | Results.push_back(ResultCandidate(FDecl)); |
| 3612 | else |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3613 | // FIXME: access? |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3614 | AddOverloadCandidate(FDecl, DeclAccessPair::make(FDecl, AS_none), |
| 3615 | Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 3616 | false, /*PartialOverloading*/true); |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3617 | } |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3618 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3619 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3620 | QualType ParamType; |
| 3621 | |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3622 | if (!CandidateSet.empty()) { |
| 3623 | // Sort the overload candidate set by placing the best overloads first. |
| 3624 | std::stable_sort(CandidateSet.begin(), CandidateSet.end(), |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3625 | IsBetterOverloadCandidate(*this, Loc)); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3626 | |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3627 | // Add the remaining viable overload candidates as code-completion reslults. |
| 3628 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3629 | CandEnd = CandidateSet.end(); |
| 3630 | Cand != CandEnd; ++Cand) { |
| 3631 | if (Cand->Viable) |
| 3632 | Results.push_back(ResultCandidate(Cand->Function)); |
| 3633 | } |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3634 | |
| 3635 | // From the viable candidates, try to determine the type of this parameter. |
| 3636 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 3637 | if (const FunctionType *FType = Results[I].getFunctionType()) |
| 3638 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FType)) |
| 3639 | if (NumArgs < Proto->getNumArgs()) { |
| 3640 | if (ParamType.isNull()) |
| 3641 | ParamType = Proto->getArgType(NumArgs); |
| 3642 | else if (!Context.hasSameUnqualifiedType( |
| 3643 | ParamType.getNonReferenceType(), |
| 3644 | Proto->getArgType(NumArgs).getNonReferenceType())) { |
| 3645 | ParamType = QualType(); |
| 3646 | break; |
| 3647 | } |
| 3648 | } |
| 3649 | } |
| 3650 | } else { |
| 3651 | // Try to determine the parameter type from the type of the expression |
| 3652 | // being called. |
| 3653 | QualType FunctionType = Fn->getType(); |
| 3654 | if (const PointerType *Ptr = FunctionType->getAs<PointerType>()) |
| 3655 | FunctionType = Ptr->getPointeeType(); |
| 3656 | else if (const BlockPointerType *BlockPtr |
| 3657 | = FunctionType->getAs<BlockPointerType>()) |
| 3658 | FunctionType = BlockPtr->getPointeeType(); |
| 3659 | else if (const MemberPointerType *MemPtr |
| 3660 | = FunctionType->getAs<MemberPointerType>()) |
| 3661 | FunctionType = MemPtr->getPointeeType(); |
| 3662 | |
| 3663 | if (const FunctionProtoType *Proto |
| 3664 | = FunctionType->getAs<FunctionProtoType>()) { |
| 3665 | if (NumArgs < Proto->getNumArgs()) |
| 3666 | ParamType = Proto->getArgType(NumArgs); |
| 3667 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3668 | } |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3669 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3670 | if (ParamType.isNull()) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3671 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3672 | else |
| 3673 | CodeCompleteExpression(S, ParamType); |
| 3674 | |
Douglas Gregor | 2e4c7a5 | 2010-04-06 20:19:47 +0000 | [diff] [blame] | 3675 | if (!Results.empty()) |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3676 | CodeCompleter->ProcessOverloadCandidates(*this, NumArgs, Results.data(), |
| 3677 | Results.size()); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3678 | } |
| 3679 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3680 | void Sema::CodeCompleteInitializer(Scope *S, Decl *D) { |
| 3681 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3682 | if (!VD) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3683 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3684 | return; |
| 3685 | } |
| 3686 | |
| 3687 | CodeCompleteExpression(S, VD->getType()); |
| 3688 | } |
| 3689 | |
| 3690 | void Sema::CodeCompleteReturn(Scope *S) { |
| 3691 | QualType ResultType; |
| 3692 | if (isa<BlockDecl>(CurContext)) { |
| 3693 | if (BlockScopeInfo *BSI = getCurBlock()) |
| 3694 | ResultType = BSI->ReturnType; |
| 3695 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext)) |
| 3696 | ResultType = Function->getResultType(); |
| 3697 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext)) |
| 3698 | ResultType = Method->getResultType(); |
| 3699 | |
| 3700 | if (ResultType.isNull()) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3701 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3702 | else |
| 3703 | CodeCompleteExpression(S, ResultType); |
| 3704 | } |
| 3705 | |
Douglas Gregor | d2d8be6 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 3706 | void Sema::CodeCompleteAfterIf(Scope *S) { |
| 3707 | typedef CodeCompletionResult Result; |
| 3708 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3709 | mapCodeCompletionContext(*this, PCC_Statement)); |
| 3710 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 3711 | Results.EnterNewScope(); |
| 3712 | |
| 3713 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3714 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3715 | CodeCompleter->includeGlobals()); |
| 3716 | |
| 3717 | AddOrdinaryNameResults(PCC_Statement, S, *this, Results); |
| 3718 | |
| 3719 | // "else" block |
| 3720 | CodeCompletionBuilder Builder(Results.getAllocator()); |
| 3721 | Builder.AddTypedTextChunk("else"); |
| 3722 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3723 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3724 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 3725 | Builder.AddPlaceholderChunk("statements"); |
| 3726 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 3727 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 3728 | Results.AddResult(Builder.TakeString()); |
| 3729 | |
| 3730 | // "else if" block |
| 3731 | Builder.AddTypedTextChunk("else"); |
| 3732 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3733 | Builder.AddTextChunk("if"); |
| 3734 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3735 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3736 | if (getLangOptions().CPlusPlus) |
| 3737 | Builder.AddPlaceholderChunk("condition"); |
| 3738 | else |
| 3739 | Builder.AddPlaceholderChunk("expression"); |
| 3740 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3741 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3742 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3743 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 3744 | Builder.AddPlaceholderChunk("statements"); |
| 3745 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 3746 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 3747 | Results.AddResult(Builder.TakeString()); |
| 3748 | |
| 3749 | Results.ExitScope(); |
| 3750 | |
| 3751 | if (S->getFnParent()) |
| 3752 | AddPrettyFunctionResults(PP.getLangOptions(), Results); |
| 3753 | |
| 3754 | if (CodeCompleter->includeMacros()) |
| 3755 | AddMacroResults(PP, Results); |
| 3756 | |
| 3757 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 3758 | Results.data(),Results.size()); |
| 3759 | } |
| 3760 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3761 | void Sema::CodeCompleteAssignmentRHS(Scope *S, ExprTy *LHS) { |
| 3762 | if (LHS) |
| 3763 | CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType()); |
| 3764 | else |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3765 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3766 | } |
| 3767 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3768 | void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3769 | bool EnteringContext) { |
| 3770 | if (!SS.getScopeRep() || !CodeCompleter) |
| 3771 | return; |
| 3772 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3773 | DeclContext *Ctx = computeDeclContext(SS, EnteringContext); |
| 3774 | if (!Ctx) |
| 3775 | return; |
Douglas Gregor | d1cd31a | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 3776 | |
| 3777 | // Try to instantiate any non-dependent declaration contexts before |
| 3778 | // we look in them. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3779 | if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx)) |
Douglas Gregor | d1cd31a | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 3780 | return; |
| 3781 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3782 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3783 | CodeCompletionContext::CCC_Name); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3784 | Results.EnterNewScope(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3785 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3786 | // The "template" keyword can follow "::" in the grammar, but only |
| 3787 | // put it into the grammar if the nested-name-specifier is dependent. |
| 3788 | NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 3789 | if (!Results.empty() && NNS->isDependent()) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3790 | Results.AddResult("template"); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3791 | |
| 3792 | // Add calls to overridden virtual functions, if there are any. |
| 3793 | // |
| 3794 | // FIXME: This isn't wonderful, because we don't know whether we're actually |
| 3795 | // in a context that permits expressions. This is a general issue with |
| 3796 | // qualified-id completions. |
| 3797 | if (!EnteringContext) |
| 3798 | MaybeAddOverrideCalls(*this, Ctx, Results); |
| 3799 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3800 | |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3801 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3802 | LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer); |
| 3803 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3804 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 3805 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3806 | Results.data(),Results.size()); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3807 | } |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3808 | |
| 3809 | void Sema::CodeCompleteUsing(Scope *S) { |
| 3810 | if (!CodeCompleter) |
| 3811 | return; |
| 3812 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3813 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3814 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
| 3815 | &ResultBuilder::IsNestedNameSpecifier); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3816 | Results.EnterNewScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3817 | |
| 3818 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 3819 | if (!S->isClassScope()) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3820 | Results.AddResult(CodeCompletionResult("namespace")); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3821 | |
| 3822 | // After "using", we can see anything that would start a |
| 3823 | // nested-name-specifier. |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3824 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3825 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3826 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3827 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3828 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3829 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3830 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3831 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | void Sema::CodeCompleteUsingDirective(Scope *S) { |
| 3835 | if (!CodeCompleter) |
| 3836 | return; |
| 3837 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3838 | // After "using namespace", we expect to see a namespace name or namespace |
| 3839 | // alias. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3840 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3841 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3842 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3843 | Results.EnterNewScope(); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3844 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3845 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3846 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3847 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3848 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3849 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3850 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
| 3853 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { |
| 3854 | if (!CodeCompleter) |
| 3855 | return; |
| 3856 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3857 | DeclContext *Ctx = (DeclContext *)S->getEntity(); |
| 3858 | if (!S->getParent()) |
| 3859 | Ctx = Context.getTranslationUnitDecl(); |
| 3860 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3861 | bool SuppressedGlobalResults |
| 3862 | = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx); |
| 3863 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3864 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3865 | SuppressedGlobalResults |
| 3866 | ? CodeCompletionContext::CCC_Namespace |
| 3867 | : CodeCompletionContext::CCC_Other, |
| 3868 | &ResultBuilder::IsNamespace); |
| 3869 | |
| 3870 | if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3871 | // We only want to see those namespaces that have already been defined |
| 3872 | // within this scope, because its likely that the user is creating an |
| 3873 | // extended namespace declaration. Keep track of the most recent |
| 3874 | // definition of each namespace. |
| 3875 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
| 3876 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
| 3877 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); |
| 3878 | NS != NSEnd; ++NS) |
| 3879 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
| 3880 | |
| 3881 | // Add the most recent definition (or extended definition) of each |
| 3882 | // namespace to the list of results. |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3883 | Results.EnterNewScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3884 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
| 3885 | NS = OrigToLatest.begin(), NSEnd = OrigToLatest.end(); |
| 3886 | NS != NSEnd; ++NS) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3887 | Results.AddResult(CodeCompletionResult(NS->second, 0), |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 3888 | CurContext, 0, false); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3889 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3890 | } |
| 3891 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3892 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3893 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3894 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
| 3897 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { |
| 3898 | if (!CodeCompleter) |
| 3899 | return; |
| 3900 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3901 | // After "namespace", we expect to see a namespace or alias. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3902 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3903 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3904 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3905 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3906 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3907 | CodeCompleter->includeGlobals()); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3908 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3909 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3910 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3911 | } |
| 3912 | |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3913 | void Sema::CodeCompleteOperatorName(Scope *S) { |
| 3914 | if (!CodeCompleter) |
| 3915 | return; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3916 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3917 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3918 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3919 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3920 | &ResultBuilder::IsType); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3921 | Results.EnterNewScope(); |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3922 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3923 | // Add the names of overloadable operators. |
| 3924 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 3925 | if (std::strcmp(Spelling, "?")) \ |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3926 | Results.AddResult(Result(Spelling)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3927 | #include "clang/Basic/OperatorKinds.def" |
| 3928 | |
| 3929 | // Add any type names visible from the current scope |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 3930 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3931 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3932 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3933 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3934 | |
| 3935 | // Add any type specifiers |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3936 | AddTypeSpecifierResults(getLangOptions(), Results); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3937 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3938 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3939 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3940 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3941 | Results.data(),Results.size()); |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3942 | } |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3943 | |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3944 | void Sema::CodeCompleteConstructorInitializer(Decl *ConstructorD, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3945 | CXXCtorInitializer** Initializers, |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3946 | unsigned NumInitializers) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3947 | PrintingPolicy Policy(Context.PrintingPolicy); |
| 3948 | Policy.AnonymousTagLocations = false; |
| 3949 | Policy.SuppressStrongLifetime = true; |
| 3950 | |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3951 | CXXConstructorDecl *Constructor |
| 3952 | = static_cast<CXXConstructorDecl *>(ConstructorD); |
| 3953 | if (!Constructor) |
| 3954 | return; |
| 3955 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3956 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3957 | CodeCompletionContext::CCC_PotentiallyQualifiedName); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3958 | Results.EnterNewScope(); |
| 3959 | |
| 3960 | // Fill in any already-initialized fields or base classes. |
| 3961 | llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields; |
| 3962 | llvm::SmallPtrSet<CanQualType, 4> InitializedBases; |
| 3963 | for (unsigned I = 0; I != NumInitializers; ++I) { |
| 3964 | if (Initializers[I]->isBaseInitializer()) |
| 3965 | InitializedBases.insert( |
| 3966 | Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0))); |
| 3967 | else |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3968 | InitializedFields.insert(cast<FieldDecl>( |
| 3969 | Initializers[I]->getAnyMember())); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
| 3972 | // Add completions for base classes. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3973 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3974 | bool SawLastInitializer = (NumInitializers == 0); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3975 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 3976 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3977 | BaseEnd = ClassDecl->bases_end(); |
| 3978 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3979 | if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) { |
| 3980 | SawLastInitializer |
| 3981 | = NumInitializers > 0 && |
| 3982 | Initializers[NumInitializers - 1]->isBaseInitializer() && |
| 3983 | Context.hasSameUnqualifiedType(Base->getType(), |
| 3984 | QualType(Initializers[NumInitializers - 1]->getBaseClass(), 0)); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3985 | continue; |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3986 | } |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3987 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3988 | Builder.AddTypedTextChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3989 | Results.getAllocator().CopyString( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3990 | Base->getType().getAsString(Policy))); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3991 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3992 | Builder.AddPlaceholderChunk("args"); |
| 3993 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3994 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3995 | SawLastInitializer? CCP_NextInitializer |
| 3996 | : CCP_MemberDeclaration)); |
| 3997 | SawLastInitializer = false; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
| 4000 | // Add completions for virtual base classes. |
| 4001 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 4002 | BaseEnd = ClassDecl->vbases_end(); |
| 4003 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4004 | if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) { |
| 4005 | SawLastInitializer |
| 4006 | = NumInitializers > 0 && |
| 4007 | Initializers[NumInitializers - 1]->isBaseInitializer() && |
| 4008 | Context.hasSameUnqualifiedType(Base->getType(), |
| 4009 | QualType(Initializers[NumInitializers - 1]->getBaseClass(), 0)); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4010 | continue; |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4011 | } |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4012 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4013 | Builder.AddTypedTextChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4014 | Builder.getAllocator().CopyString( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4015 | Base->getType().getAsString(Policy))); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4016 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4017 | Builder.AddPlaceholderChunk("args"); |
| 4018 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4019 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4020 | SawLastInitializer? CCP_NextInitializer |
| 4021 | : CCP_MemberDeclaration)); |
| 4022 | SawLastInitializer = false; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
| 4025 | // Add completions for members. |
| 4026 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4027 | FieldEnd = ClassDecl->field_end(); |
| 4028 | Field != FieldEnd; ++Field) { |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4029 | if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl()))) { |
| 4030 | SawLastInitializer |
| 4031 | = NumInitializers > 0 && |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4032 | Initializers[NumInitializers - 1]->isAnyMemberInitializer() && |
| 4033 | Initializers[NumInitializers - 1]->getAnyMember() == *Field; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4034 | continue; |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4035 | } |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4036 | |
| 4037 | if (!Field->getDeclName()) |
| 4038 | continue; |
| 4039 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4040 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4041 | Field->getIdentifier()->getName())); |
| 4042 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4043 | Builder.AddPlaceholderChunk("args"); |
| 4044 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4045 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4046 | SawLastInitializer? CCP_NextInitializer |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 4047 | : CCP_MemberDeclaration, |
| 4048 | CXCursor_MemberRef)); |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4049 | SawLastInitializer = false; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4050 | } |
| 4051 | Results.ExitScope(); |
| 4052 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4053 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4054 | Results.data(), Results.size()); |
| 4055 | } |
| 4056 | |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4057 | // Macro that expands to @Keyword or Keyword, depending on whether NeedAt is |
| 4058 | // true or false. |
| 4059 | #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) NeedAt? "@" #Keyword : #Keyword |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4060 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4061 | ResultBuilder &Results, |
| 4062 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4063 | typedef CodeCompletionResult Result; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4064 | // Since we have an implementation, we can end it. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4065 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4066 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4067 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4068 | if (LangOpts.ObjC2) { |
| 4069 | // @dynamic |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4070 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,dynamic)); |
| 4071 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4072 | Builder.AddPlaceholderChunk("property"); |
| 4073 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4074 | |
| 4075 | // @synthesize |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4076 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synthesize)); |
| 4077 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4078 | Builder.AddPlaceholderChunk("property"); |
| 4079 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4080 | } |
| 4081 | } |
| 4082 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4083 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4084 | ResultBuilder &Results, |
| 4085 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4086 | typedef CodeCompletionResult Result; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4087 | |
| 4088 | // Since we have an interface or protocol, we can end it. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4089 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4090 | |
| 4091 | if (LangOpts.ObjC2) { |
| 4092 | // @property |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4093 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,property))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4094 | |
| 4095 | // @required |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4096 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,required))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4097 | |
| 4098 | // @optional |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4099 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,optional))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4100 | } |
| 4101 | } |
| 4102 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4103 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4104 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4105 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4106 | |
| 4107 | // @class name ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4108 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,class)); |
| 4109 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4110 | Builder.AddPlaceholderChunk("name"); |
| 4111 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4112 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4113 | if (Results.includeCodePatterns()) { |
| 4114 | // @interface name |
| 4115 | // FIXME: Could introduce the whole pattern, including superclasses and |
| 4116 | // such. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4117 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,interface)); |
| 4118 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4119 | Builder.AddPlaceholderChunk("class"); |
| 4120 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4121 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4122 | // @protocol name |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4123 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol)); |
| 4124 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4125 | Builder.AddPlaceholderChunk("protocol"); |
| 4126 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4127 | |
| 4128 | // @implementation name |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4129 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,implementation)); |
| 4130 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4131 | Builder.AddPlaceholderChunk("class"); |
| 4132 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4133 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4134 | |
| 4135 | // @compatibility_alias name |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4136 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,compatibility_alias)); |
| 4137 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4138 | Builder.AddPlaceholderChunk("alias"); |
| 4139 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4140 | Builder.AddPlaceholderChunk("class"); |
| 4141 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4142 | } |
| 4143 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4144 | void Sema::CodeCompleteObjCAtDirective(Scope *S) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4145 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4146 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4147 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4148 | Results.EnterNewScope(); |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4149 | if (isa<ObjCImplDecl>(CurContext)) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4150 | AddObjCImplementationResults(getLangOptions(), Results, false); |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4151 | else if (CurContext->isObjCContainer()) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4152 | AddObjCInterfaceResults(getLangOptions(), Results, false); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4153 | else |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4154 | AddObjCTopLevelResults(Results, false); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4155 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4156 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4157 | CodeCompletionContext::CCC_Other, |
| 4158 | Results.data(),Results.size()); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4159 | } |
| 4160 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4161 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4162 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4163 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4164 | |
| 4165 | // @encode ( type-name ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4166 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,encode)); |
| 4167 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4168 | Builder.AddPlaceholderChunk("type-name"); |
| 4169 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4170 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4171 | |
| 4172 | // @protocol ( protocol-name ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4173 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol)); |
| 4174 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4175 | Builder.AddPlaceholderChunk("protocol-name"); |
| 4176 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4177 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4178 | |
| 4179 | // @selector ( selector ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4180 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,selector)); |
| 4181 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4182 | Builder.AddPlaceholderChunk("selector"); |
| 4183 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4184 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4187 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4188 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4189 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4190 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4191 | if (Results.includeCodePatterns()) { |
| 4192 | // @try { statements } @catch ( declaration ) { statements } @finally |
| 4193 | // { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4194 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,try)); |
| 4195 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4196 | Builder.AddPlaceholderChunk("statements"); |
| 4197 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4198 | Builder.AddTextChunk("@catch"); |
| 4199 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4200 | Builder.AddPlaceholderChunk("parameter"); |
| 4201 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4202 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4203 | Builder.AddPlaceholderChunk("statements"); |
| 4204 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4205 | Builder.AddTextChunk("@finally"); |
| 4206 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4207 | Builder.AddPlaceholderChunk("statements"); |
| 4208 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4209 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4210 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4211 | |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4212 | // @throw |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4213 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,throw)); |
| 4214 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4215 | Builder.AddPlaceholderChunk("expression"); |
| 4216 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4217 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4218 | if (Results.includeCodePatterns()) { |
| 4219 | // @synchronized ( expression ) { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4220 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synchronized)); |
| 4221 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4222 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4223 | Builder.AddPlaceholderChunk("expression"); |
| 4224 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 4225 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4226 | Builder.AddPlaceholderChunk("statements"); |
| 4227 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4228 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4229 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4230 | } |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4231 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4232 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4233 | ResultBuilder &Results, |
| 4234 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4235 | typedef CodeCompletionResult Result; |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4236 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,private))); |
| 4237 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,protected))); |
| 4238 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,public))); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4239 | if (LangOpts.ObjC2) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4240 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,package))); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
| 4243 | void Sema::CodeCompleteObjCAtVisibility(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4244 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4245 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4246 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4247 | AddObjCVisibilityResults(getLangOptions(), Results, false); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4248 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4249 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4250 | CodeCompletionContext::CCC_Other, |
| 4251 | Results.data(),Results.size()); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | void Sema::CodeCompleteObjCAtStatement(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4255 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4256 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4257 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4258 | AddObjCStatementResults(Results, false); |
| 4259 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4260 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4261 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4262 | CodeCompletionContext::CCC_Other, |
| 4263 | Results.data(),Results.size()); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | void Sema::CodeCompleteObjCAtExpression(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4267 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4268 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4269 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4270 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4271 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4272 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4273 | CodeCompletionContext::CCC_Other, |
| 4274 | Results.data(),Results.size()); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4277 | /// \brief Determine whether the addition of the given flag to an Objective-C |
| 4278 | /// property's attributes will cause a conflict. |
| 4279 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { |
| 4280 | // Check if we've already added this flag. |
| 4281 | if (Attributes & NewFlag) |
| 4282 | return true; |
| 4283 | |
| 4284 | Attributes |= NewFlag; |
| 4285 | |
| 4286 | // Check for collisions with "readonly". |
| 4287 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 4288 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | |
| 4289 | ObjCDeclSpec::DQ_PR_assign | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4290 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4291 | ObjCDeclSpec::DQ_PR_copy | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4292 | ObjCDeclSpec::DQ_PR_retain | |
| 4293 | ObjCDeclSpec::DQ_PR_strong))) |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4294 | return true; |
| 4295 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4296 | // Check for more than one of { assign, copy, retain, strong }. |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4297 | unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4298 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4299 | ObjCDeclSpec::DQ_PR_copy | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4300 | ObjCDeclSpec::DQ_PR_retain| |
| 4301 | ObjCDeclSpec::DQ_PR_strong); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4302 | if (AssignCopyRetMask && |
| 4303 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4304 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained && |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4305 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4306 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain && |
| 4307 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong) |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4308 | return true; |
| 4309 | |
| 4310 | return false; |
| 4311 | } |
| 4312 | |
Douglas Gregor | a93b108 | 2009-11-18 23:08:07 +0000 | [diff] [blame] | 4313 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4314 | if (!CodeCompleter) |
| 4315 | return; |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4316 | |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4317 | unsigned Attributes = ODS.getPropertyAttributes(); |
| 4318 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4319 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4320 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4321 | CodeCompletionContext::CCC_Other); |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4322 | Results.EnterNewScope(); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4323 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4324 | Results.AddResult(CodeCompletionResult("readonly")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4325 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4326 | Results.AddResult(CodeCompletionResult("assign")); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4327 | if (!ObjCPropertyFlagConflicts(Attributes, |
| 4328 | ObjCDeclSpec::DQ_PR_unsafe_unretained)) |
| 4329 | Results.AddResult(CodeCompletionResult("unsafe_unretained")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4330 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4331 | Results.AddResult(CodeCompletionResult("readwrite")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4332 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4333 | Results.AddResult(CodeCompletionResult("retain")); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4334 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong)) |
| 4335 | Results.AddResult(CodeCompletionResult("strong")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4336 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4337 | Results.AddResult(CodeCompletionResult("copy")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4338 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4339 | Results.AddResult(CodeCompletionResult("nonatomic")); |
Fariborz Jahanian | 27f4523 | 2011-06-11 17:14:27 +0000 | [diff] [blame] | 4340 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic)) |
| 4341 | Results.AddResult(CodeCompletionResult("atomic")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4342 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4343 | CodeCompletionBuilder Setter(Results.getAllocator()); |
| 4344 | Setter.AddTypedTextChunk("setter"); |
| 4345 | Setter.AddTextChunk(" = "); |
| 4346 | Setter.AddPlaceholderChunk("method"); |
| 4347 | Results.AddResult(CodeCompletionResult(Setter.TakeString())); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 4348 | } |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4349 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4350 | CodeCompletionBuilder Getter(Results.getAllocator()); |
| 4351 | Getter.AddTypedTextChunk("getter"); |
| 4352 | Getter.AddTextChunk(" = "); |
| 4353 | Getter.AddPlaceholderChunk("method"); |
| 4354 | Results.AddResult(CodeCompletionResult(Getter.TakeString())); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 4355 | } |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4356 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4357 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4358 | CodeCompletionContext::CCC_Other, |
| 4359 | Results.data(),Results.size()); |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4360 | } |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4361 | |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4362 | /// \brief Descripts the kind of Objective-C method that we want to find |
| 4363 | /// via code completion. |
| 4364 | enum ObjCMethodKind { |
| 4365 | MK_Any, //< Any kind of method, provided it means other specified criteria. |
| 4366 | MK_ZeroArgSelector, //< Zero-argument (unary) selector. |
| 4367 | MK_OneArgSelector //< One-argument selector. |
| 4368 | }; |
| 4369 | |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4370 | static bool isAcceptableObjCSelector(Selector Sel, |
| 4371 | ObjCMethodKind WantKind, |
| 4372 | IdentifierInfo **SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4373 | unsigned NumSelIdents, |
| 4374 | bool AllowSameLength = true) { |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4375 | if (NumSelIdents > Sel.getNumArgs()) |
| 4376 | return false; |
| 4377 | |
| 4378 | switch (WantKind) { |
| 4379 | case MK_Any: break; |
| 4380 | case MK_ZeroArgSelector: return Sel.isUnarySelector(); |
| 4381 | case MK_OneArgSelector: return Sel.getNumArgs() == 1; |
| 4382 | } |
| 4383 | |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4384 | if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs()) |
| 4385 | return false; |
| 4386 | |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4387 | for (unsigned I = 0; I != NumSelIdents; ++I) |
| 4388 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) |
| 4389 | return false; |
| 4390 | |
| 4391 | return true; |
| 4392 | } |
| 4393 | |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4394 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, |
| 4395 | ObjCMethodKind WantKind, |
| 4396 | IdentifierInfo **SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4397 | unsigned NumSelIdents, |
| 4398 | bool AllowSameLength = true) { |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4399 | return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4400 | NumSelIdents, AllowSameLength); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4401 | } |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4402 | |
| 4403 | namespace { |
| 4404 | /// \brief A set of selectors, which is used to avoid introducing multiple |
| 4405 | /// completions with the same selector into the result set. |
| 4406 | typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet; |
| 4407 | } |
| 4408 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4409 | /// \brief Add all of the Objective-C methods in the given Objective-C |
| 4410 | /// container to the set of results. |
| 4411 | /// |
| 4412 | /// The container will be a class, protocol, category, or implementation of |
| 4413 | /// any of the above. This mether will recurse to include methods from |
| 4414 | /// the superclasses of classes along with their categories, protocols, and |
| 4415 | /// implementations. |
| 4416 | /// |
| 4417 | /// \param Container the container in which we'll look to find methods. |
| 4418 | /// |
| 4419 | /// \param WantInstance whether to add instance methods (only); if false, this |
| 4420 | /// routine will add factory methods (only). |
| 4421 | /// |
| 4422 | /// \param CurContext the context in which we're performing the lookup that |
| 4423 | /// finds methods. |
| 4424 | /// |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4425 | /// \param AllowSameLength Whether we allow a method to be added to the list |
| 4426 | /// when it has the same number of parameters as we have selector identifiers. |
| 4427 | /// |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4428 | /// \param Results the structure into which we'll add results. |
| 4429 | static void AddObjCMethods(ObjCContainerDecl *Container, |
| 4430 | bool WantInstanceMethods, |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4431 | ObjCMethodKind WantKind, |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4432 | IdentifierInfo **SelIdents, |
| 4433 | unsigned NumSelIdents, |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4434 | DeclContext *CurContext, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4435 | VisitedSelectorSet &Selectors, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4436 | bool AllowSameLength, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4437 | ResultBuilder &Results, |
| 4438 | bool InOriginalClass = true) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4439 | typedef CodeCompletionResult Result; |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4440 | for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), |
| 4441 | MEnd = Container->meth_end(); |
| 4442 | M != MEnd; ++M) { |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4443 | if ((*M)->isInstanceMethod() == WantInstanceMethods) { |
| 4444 | // Check whether the selector identifiers we've been given are a |
| 4445 | // subset of the identifiers for this particular method. |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4446 | if (!isAcceptableObjCMethod(*M, WantKind, SelIdents, NumSelIdents, |
| 4447 | AllowSameLength)) |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4448 | continue; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4449 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4450 | if (!Selectors.insert((*M)->getSelector())) |
| 4451 | continue; |
| 4452 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4453 | Result R = Result(*M, 0); |
| 4454 | R.StartParameter = NumSelIdents; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4455 | R.AllParametersAreInformative = (WantKind != MK_Any); |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4456 | if (!InOriginalClass) |
| 4457 | R.Priority += CCD_InBaseClass; |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4458 | Results.MaybeAddResult(R, CurContext); |
| 4459 | } |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4460 | } |
| 4461 | |
Douglas Gregor | e396c7b | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 4462 | // Visit the protocols of protocols. |
| 4463 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 4464 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 4465 | = Protocol->getReferencedProtocols(); |
| 4466 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 4467 | E = Protocols.end(); |
| 4468 | I != E; ++I) |
| 4469 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4470 | CurContext, Selectors, AllowSameLength, Results, false); |
Douglas Gregor | e396c7b | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4473 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); |
| 4474 | if (!IFace) |
| 4475 | return; |
| 4476 | |
| 4477 | // Add methods in protocols. |
| 4478 | const ObjCList<ObjCProtocolDecl> &Protocols= IFace->getReferencedProtocols(); |
| 4479 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 4480 | E = Protocols.end(); |
| 4481 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4482 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4483 | CurContext, Selectors, AllowSameLength, Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4484 | |
| 4485 | // Add methods in categories. |
| 4486 | for (ObjCCategoryDecl *CatDecl = IFace->getCategoryList(); CatDecl; |
| 4487 | CatDecl = CatDecl->getNextClassCategory()) { |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4488 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4489 | NumSelIdents, CurContext, Selectors, AllowSameLength, |
| 4490 | Results, InOriginalClass); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4491 | |
| 4492 | // Add a categories protocol methods. |
| 4493 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 4494 | = CatDecl->getReferencedProtocols(); |
| 4495 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 4496 | E = Protocols.end(); |
| 4497 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4498 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4499 | NumSelIdents, CurContext, Selectors, AllowSameLength, |
| 4500 | Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4501 | |
| 4502 | // Add methods in category implementations. |
| 4503 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4504 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4505 | NumSelIdents, CurContext, Selectors, AllowSameLength, |
| 4506 | Results, InOriginalClass); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
| 4509 | // Add methods in superclass. |
| 4510 | if (IFace->getSuperClass()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4511 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4512 | SelIdents, NumSelIdents, CurContext, Selectors, |
| 4513 | AllowSameLength, Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4514 | |
| 4515 | // Add methods in our implementation, if any. |
| 4516 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4517 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4518 | NumSelIdents, CurContext, Selectors, AllowSameLength, |
| 4519 | Results, InOriginalClass); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4523 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4524 | typedef CodeCompletionResult Result; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4525 | |
| 4526 | // Try to find the interface where getters might live. |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4527 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4528 | if (!Class) { |
| 4529 | if (ObjCCategoryDecl *Category |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4530 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4531 | Class = Category->getClassInterface(); |
| 4532 | |
| 4533 | if (!Class) |
| 4534 | return; |
| 4535 | } |
| 4536 | |
| 4537 | // Find all of the potential getters. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4538 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4539 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4540 | Results.EnterNewScope(); |
| 4541 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4542 | VisitedSelectorSet Selectors; |
| 4543 | AddObjCMethods(Class, true, MK_ZeroArgSelector, 0, 0, CurContext, Selectors, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4544 | /*AllowSameLength=*/true, Results); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4545 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4546 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4547 | CodeCompletionContext::CCC_Other, |
| 4548 | Results.data(),Results.size()); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4549 | } |
| 4550 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4551 | void Sema::CodeCompleteObjCPropertySetter(Scope *S) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4552 | typedef CodeCompletionResult Result; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4553 | |
| 4554 | // Try to find the interface where setters might live. |
| 4555 | ObjCInterfaceDecl *Class |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4556 | = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4557 | if (!Class) { |
| 4558 | if (ObjCCategoryDecl *Category |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4559 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4560 | Class = Category->getClassInterface(); |
| 4561 | |
| 4562 | if (!Class) |
| 4563 | return; |
| 4564 | } |
| 4565 | |
| 4566 | // Find all of the potential getters. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4567 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4568 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4569 | Results.EnterNewScope(); |
| 4570 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4571 | VisitedSelectorSet Selectors; |
| 4572 | AddObjCMethods(Class, true, MK_OneArgSelector, 0, 0, CurContext, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4573 | Selectors, /*AllowSameLength=*/true, Results); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4574 | |
| 4575 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4576 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4577 | CodeCompletionContext::CCC_Other, |
| 4578 | Results.data(),Results.size()); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4579 | } |
| 4580 | |
Douglas Gregor | afc4578 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 4581 | void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS, |
| 4582 | bool IsParameter) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4583 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4584 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4585 | CodeCompletionContext::CCC_Type); |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 4586 | Results.EnterNewScope(); |
| 4587 | |
| 4588 | // Add context-sensitive, Objective-C parameter-passing keywords. |
| 4589 | bool AddedInOut = false; |
| 4590 | if ((DS.getObjCDeclQualifier() & |
| 4591 | (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 4592 | Results.AddResult("in"); |
| 4593 | Results.AddResult("inout"); |
| 4594 | AddedInOut = true; |
| 4595 | } |
| 4596 | if ((DS.getObjCDeclQualifier() & |
| 4597 | (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 4598 | Results.AddResult("out"); |
| 4599 | if (!AddedInOut) |
| 4600 | Results.AddResult("inout"); |
| 4601 | } |
| 4602 | if ((DS.getObjCDeclQualifier() & |
| 4603 | (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref | |
| 4604 | ObjCDeclSpec::DQ_Oneway)) == 0) { |
| 4605 | Results.AddResult("bycopy"); |
| 4606 | Results.AddResult("byref"); |
| 4607 | Results.AddResult("oneway"); |
| 4608 | } |
| 4609 | |
Douglas Gregor | afc4578 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 4610 | // If we're completing the return type of an Objective-C method and the |
| 4611 | // identifier IBAction refers to a macro, provide a completion item for |
| 4612 | // an action, e.g., |
| 4613 | // IBAction)<#selector#>:(id)sender |
| 4614 | if (DS.getObjCDeclQualifier() == 0 && !IsParameter && |
| 4615 | Context.Idents.get("IBAction").hasMacroDefinition()) { |
| 4616 | typedef CodeCompletionString::Chunk Chunk; |
| 4617 | CodeCompletionBuilder Builder(Results.getAllocator(), CCP_CodePattern, |
| 4618 | CXAvailability_Available); |
| 4619 | Builder.AddTypedTextChunk("IBAction"); |
| 4620 | Builder.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
| 4621 | Builder.AddPlaceholderChunk("selector"); |
| 4622 | Builder.AddChunk(Chunk(CodeCompletionString::CK_Colon)); |
| 4623 | Builder.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
| 4624 | Builder.AddTextChunk("id"); |
| 4625 | Builder.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
| 4626 | Builder.AddTextChunk("sender"); |
| 4627 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 4628 | } |
| 4629 | |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 4630 | // Add various builtin type names and specifiers. |
| 4631 | AddOrdinaryNameResults(PCC_Type, S, *this, Results); |
| 4632 | Results.ExitScope(); |
| 4633 | |
| 4634 | // Add the various type names |
| 4635 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 4636 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4637 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4638 | CodeCompleter->includeGlobals()); |
| 4639 | |
| 4640 | if (CodeCompleter->includeMacros()) |
| 4641 | AddMacroResults(PP, Results); |
| 4642 | |
| 4643 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4644 | CodeCompletionContext::CCC_Type, |
| 4645 | Results.data(), Results.size()); |
| 4646 | } |
| 4647 | |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4648 | /// \brief When we have an expression with type "id", we may assume |
| 4649 | /// that it has some more-specific class type based on knowledge of |
| 4650 | /// common uses of Objective-C. This routine returns that class type, |
| 4651 | /// or NULL if no better result could be determined. |
| 4652 | static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) { |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 4653 | ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E); |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4654 | if (!Msg) |
| 4655 | return 0; |
| 4656 | |
| 4657 | Selector Sel = Msg->getSelector(); |
| 4658 | if (Sel.isNull()) |
| 4659 | return 0; |
| 4660 | |
| 4661 | IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0); |
| 4662 | if (!Id) |
| 4663 | return 0; |
| 4664 | |
| 4665 | ObjCMethodDecl *Method = Msg->getMethodDecl(); |
| 4666 | if (!Method) |
| 4667 | return 0; |
| 4668 | |
| 4669 | // Determine the class that we're sending the message to. |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 4670 | ObjCInterfaceDecl *IFace = 0; |
| 4671 | switch (Msg->getReceiverKind()) { |
| 4672 | case ObjCMessageExpr::Class: |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4673 | if (const ObjCObjectType *ObjType |
| 4674 | = Msg->getClassReceiver()->getAs<ObjCObjectType>()) |
| 4675 | IFace = ObjType->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 4676 | break; |
| 4677 | |
| 4678 | case ObjCMessageExpr::Instance: { |
| 4679 | QualType T = Msg->getInstanceReceiver()->getType(); |
| 4680 | if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) |
| 4681 | IFace = Ptr->getInterfaceDecl(); |
| 4682 | break; |
| 4683 | } |
| 4684 | |
| 4685 | case ObjCMessageExpr::SuperInstance: |
| 4686 | case ObjCMessageExpr::SuperClass: |
| 4687 | break; |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4688 | } |
| 4689 | |
| 4690 | if (!IFace) |
| 4691 | return 0; |
| 4692 | |
| 4693 | ObjCInterfaceDecl *Super = IFace->getSuperClass(); |
| 4694 | if (Method->isInstanceMethod()) |
| 4695 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 4696 | .Case("retain", IFace) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4697 | .Case("strong", IFace) |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4698 | .Case("autorelease", IFace) |
| 4699 | .Case("copy", IFace) |
| 4700 | .Case("copyWithZone", IFace) |
| 4701 | .Case("mutableCopy", IFace) |
| 4702 | .Case("mutableCopyWithZone", IFace) |
| 4703 | .Case("awakeFromCoder", IFace) |
| 4704 | .Case("replacementObjectFromCoder", IFace) |
| 4705 | .Case("class", IFace) |
| 4706 | .Case("classForCoder", IFace) |
| 4707 | .Case("superclass", Super) |
| 4708 | .Default(0); |
| 4709 | |
| 4710 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 4711 | .Case("new", IFace) |
| 4712 | .Case("alloc", IFace) |
| 4713 | .Case("allocWithZone", IFace) |
| 4714 | .Case("class", IFace) |
| 4715 | .Case("superclass", Super) |
| 4716 | .Default(0); |
| 4717 | } |
| 4718 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4719 | // Add a special completion for a message send to "super", which fills in the |
| 4720 | // most likely case of forwarding all of our arguments to the superclass |
| 4721 | // function. |
| 4722 | /// |
| 4723 | /// \param S The semantic analysis object. |
| 4724 | /// |
| 4725 | /// \param S NeedSuperKeyword Whether we need to prefix this completion with |
| 4726 | /// the "super" keyword. Otherwise, we just need to provide the arguments. |
| 4727 | /// |
| 4728 | /// \param SelIdents The identifiers in the selector that have already been |
| 4729 | /// provided as arguments for a send to "super". |
| 4730 | /// |
| 4731 | /// \param NumSelIdents The number of identifiers in \p SelIdents. |
| 4732 | /// |
| 4733 | /// \param Results The set of results to augment. |
| 4734 | /// |
| 4735 | /// \returns the Objective-C method declaration that would be invoked by |
| 4736 | /// this "super" completion. If NULL, no completion was added. |
| 4737 | static ObjCMethodDecl *AddSuperSendCompletion(Sema &S, bool NeedSuperKeyword, |
| 4738 | IdentifierInfo **SelIdents, |
| 4739 | unsigned NumSelIdents, |
| 4740 | ResultBuilder &Results) { |
| 4741 | ObjCMethodDecl *CurMethod = S.getCurMethodDecl(); |
| 4742 | if (!CurMethod) |
| 4743 | return 0; |
| 4744 | |
| 4745 | ObjCInterfaceDecl *Class = CurMethod->getClassInterface(); |
| 4746 | if (!Class) |
| 4747 | return 0; |
| 4748 | |
| 4749 | // Try to find a superclass method with the same selector. |
| 4750 | ObjCMethodDecl *SuperMethod = 0; |
Douglas Gregor | 78bcd91 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 4751 | while ((Class = Class->getSuperClass()) && !SuperMethod) { |
| 4752 | // Check in the class |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4753 | SuperMethod = Class->getMethod(CurMethod->getSelector(), |
| 4754 | CurMethod->isInstanceMethod()); |
| 4755 | |
Douglas Gregor | 78bcd91 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 4756 | // Check in categories or class extensions. |
| 4757 | if (!SuperMethod) { |
| 4758 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; |
| 4759 | Category = Category->getNextClassCategory()) |
| 4760 | if ((SuperMethod = Category->getMethod(CurMethod->getSelector(), |
| 4761 | CurMethod->isInstanceMethod()))) |
| 4762 | break; |
| 4763 | } |
| 4764 | } |
| 4765 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4766 | if (!SuperMethod) |
| 4767 | return 0; |
| 4768 | |
| 4769 | // Check whether the superclass method has the same signature. |
| 4770 | if (CurMethod->param_size() != SuperMethod->param_size() || |
| 4771 | CurMethod->isVariadic() != SuperMethod->isVariadic()) |
| 4772 | return 0; |
| 4773 | |
| 4774 | for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(), |
| 4775 | CurPEnd = CurMethod->param_end(), |
| 4776 | SuperP = SuperMethod->param_begin(); |
| 4777 | CurP != CurPEnd; ++CurP, ++SuperP) { |
| 4778 | // Make sure the parameter types are compatible. |
| 4779 | if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(), |
| 4780 | (*SuperP)->getType())) |
| 4781 | return 0; |
| 4782 | |
| 4783 | // Make sure we have a parameter name to forward! |
| 4784 | if (!(*CurP)->getIdentifier()) |
| 4785 | return 0; |
| 4786 | } |
| 4787 | |
| 4788 | // We have a superclass method. Now, form the send-to-super completion. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4789 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4790 | |
| 4791 | // Give this completion a return type. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4792 | AddResultTypeChunk(S.Context, SuperMethod, Builder); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4793 | |
| 4794 | // If we need the "super" keyword, add it (plus some spacing). |
| 4795 | if (NeedSuperKeyword) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4796 | Builder.AddTypedTextChunk("super"); |
| 4797 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4798 | } |
| 4799 | |
| 4800 | Selector Sel = CurMethod->getSelector(); |
| 4801 | if (Sel.isUnarySelector()) { |
| 4802 | if (NeedSuperKeyword) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4803 | Builder.AddTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 4804 | Sel.getNameForSlot(0))); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4805 | else |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4806 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 4807 | Sel.getNameForSlot(0))); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4808 | } else { |
| 4809 | ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(); |
| 4810 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) { |
| 4811 | if (I > NumSelIdents) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4812 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4813 | |
| 4814 | if (I < NumSelIdents) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4815 | Builder.AddInformativeChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4816 | Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 4817 | Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4818 | else if (NeedSuperKeyword || I > NumSelIdents) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4819 | Builder.AddTextChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4820 | Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 4821 | Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4822 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4823 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4824 | } else { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4825 | Builder.AddTypedTextChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4826 | Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 4827 | Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4828 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4829 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4830 | } |
| 4831 | } |
| 4832 | } |
| 4833 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4834 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), CCP_SuperCompletion, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4835 | SuperMethod->isInstanceMethod() |
| 4836 | ? CXCursor_ObjCInstanceMethodDecl |
| 4837 | : CXCursor_ObjCClassMethodDecl)); |
| 4838 | return SuperMethod; |
| 4839 | } |
| 4840 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4841 | void Sema::CodeCompleteObjCMessageReceiver(Scope *S) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4842 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4843 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4844 | CodeCompletionContext::CCC_ObjCMessageReceiver, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4845 | &ResultBuilder::IsObjCMessageReceiver); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4846 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4847 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4848 | Results.EnterNewScope(); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4849 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4850 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4851 | |
| 4852 | // If we are in an Objective-C method inside a class that has a superclass, |
| 4853 | // add "super" as an option. |
| 4854 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 4855 | if (ObjCInterfaceDecl *Iface = Method->getClassInterface()) |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4856 | if (Iface->getSuperClass()) { |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4857 | Results.AddResult(Result("super")); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4858 | |
| 4859 | AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, 0, 0, Results); |
| 4860 | } |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4861 | |
| 4862 | Results.ExitScope(); |
| 4863 | |
| 4864 | if (CodeCompleter->includeMacros()) |
| 4865 | AddMacroResults(PP, Results); |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 4866 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4867 | Results.data(), Results.size()); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4868 | |
| 4869 | } |
| 4870 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4871 | void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, |
| 4872 | IdentifierInfo **SelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4873 | unsigned NumSelIdents, |
| 4874 | bool AtArgumentExpression) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4875 | ObjCInterfaceDecl *CDecl = 0; |
| 4876 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 4877 | // Figure out which interface we're in. |
| 4878 | CDecl = CurMethod->getClassInterface(); |
| 4879 | if (!CDecl) |
| 4880 | return; |
| 4881 | |
| 4882 | // Find the superclass of this class. |
| 4883 | CDecl = CDecl->getSuperClass(); |
| 4884 | if (!CDecl) |
| 4885 | return; |
| 4886 | |
| 4887 | if (CurMethod->isInstanceMethod()) { |
| 4888 | // We are inside an instance method, which means that the message |
| 4889 | // send [super ...] is actually calling an instance method on the |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 4890 | // current object. |
| 4891 | return CodeCompleteObjCInstanceMessage(S, 0, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4892 | SelIdents, NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4893 | AtArgumentExpression, |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 4894 | CDecl); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4895 | } |
| 4896 | |
| 4897 | // Fall through to send to the superclass in CDecl. |
| 4898 | } else { |
| 4899 | // "super" may be the name of a type or variable. Figure out which |
| 4900 | // it is. |
| 4901 | IdentifierInfo *Super = &Context.Idents.get("super"); |
| 4902 | NamedDecl *ND = LookupSingleName(S, Super, SuperLoc, |
| 4903 | LookupOrdinaryName); |
| 4904 | if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) { |
| 4905 | // "super" names an interface. Use it. |
| 4906 | } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4907 | if (const ObjCObjectType *Iface |
| 4908 | = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>()) |
| 4909 | CDecl = Iface->getInterface(); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4910 | } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) { |
| 4911 | // "super" names an unresolved type; we can't be more specific. |
| 4912 | } else { |
| 4913 | // Assume that "super" names some kind of value and parse that way. |
| 4914 | CXXScopeSpec SS; |
| 4915 | UnqualifiedId id; |
| 4916 | id.setIdentifier(Super, SuperLoc); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4917 | ExprResult SuperExpr = ActOnIdExpression(S, SS, id, false, false); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4918 | return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(), |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4919 | SelIdents, NumSelIdents, |
| 4920 | AtArgumentExpression); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4921 | } |
| 4922 | |
| 4923 | // Fall through |
| 4924 | } |
| 4925 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4926 | ParsedType Receiver; |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4927 | if (CDecl) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4928 | Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4929 | return CodeCompleteObjCClassMessage(S, Receiver, SelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4930 | NumSelIdents, AtArgumentExpression, |
| 4931 | /*IsSuper=*/true); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4932 | } |
| 4933 | |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 4934 | /// \brief Given a set of code-completion results for the argument of a message |
| 4935 | /// send, determine the preferred type (if any) for that argument expression. |
| 4936 | static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results, |
| 4937 | unsigned NumSelIdents) { |
| 4938 | typedef CodeCompletionResult Result; |
| 4939 | ASTContext &Context = Results.getSema().Context; |
| 4940 | |
| 4941 | QualType PreferredType; |
| 4942 | unsigned BestPriority = CCP_Unlikely * 2; |
| 4943 | Result *ResultsData = Results.data(); |
| 4944 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 4945 | Result &R = ResultsData[I]; |
| 4946 | if (R.Kind == Result::RK_Declaration && |
| 4947 | isa<ObjCMethodDecl>(R.Declaration)) { |
| 4948 | if (R.Priority <= BestPriority) { |
| 4949 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration); |
| 4950 | if (NumSelIdents <= Method->param_size()) { |
| 4951 | QualType MyPreferredType = Method->param_begin()[NumSelIdents - 1] |
| 4952 | ->getType(); |
| 4953 | if (R.Priority < BestPriority || PreferredType.isNull()) { |
| 4954 | BestPriority = R.Priority; |
| 4955 | PreferredType = MyPreferredType; |
| 4956 | } else if (!Context.hasSameUnqualifiedType(PreferredType, |
| 4957 | MyPreferredType)) { |
| 4958 | PreferredType = QualType(); |
| 4959 | } |
| 4960 | } |
| 4961 | } |
| 4962 | } |
| 4963 | } |
| 4964 | |
| 4965 | return PreferredType; |
| 4966 | } |
| 4967 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4968 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| 4969 | ParsedType Receiver, |
| 4970 | IdentifierInfo **SelIdents, |
| 4971 | unsigned NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4972 | bool AtArgumentExpression, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4973 | bool IsSuper, |
| 4974 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4975 | typedef CodeCompletionResult Result; |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4976 | ObjCInterfaceDecl *CDecl = 0; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4977 | |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4978 | // If the given name refers to an interface type, retrieve the |
| 4979 | // corresponding declaration. |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4980 | if (Receiver) { |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4981 | QualType T = SemaRef.GetTypeFromParser(Receiver, 0); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4982 | if (!T.isNull()) |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4983 | if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>()) |
| 4984 | CDecl = Interface->getInterface(); |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4985 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4986 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4987 | // Add all of the factory methods in this Objective-C class, its protocols, |
| 4988 | // superclasses, categories, implementation, etc. |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4989 | Results.EnterNewScope(); |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4990 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4991 | // If this is a send-to-super, try to add the special "super" send |
| 4992 | // completion. |
| 4993 | if (IsSuper) { |
| 4994 | if (ObjCMethodDecl *SuperMethod |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4995 | = AddSuperSendCompletion(SemaRef, false, SelIdents, NumSelIdents, |
| 4996 | Results)) |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4997 | Results.Ignore(SuperMethod); |
| 4998 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4999 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 5000 | // If we're inside an Objective-C method definition, prefer its selector to |
| 5001 | // others. |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5002 | if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl()) |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 5003 | Results.setPreferredSelector(CurMethod->getSelector()); |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5004 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5005 | VisitedSelectorSet Selectors; |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5006 | if (CDecl) |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5007 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, NumSelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5008 | SemaRef.CurContext, Selectors, AtArgumentExpression, |
| 5009 | Results); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5010 | else { |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5011 | // We're messaging "id" as a type; provide all class/factory methods. |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5012 | |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5013 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 5014 | // pool from the AST file. |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5015 | if (SemaRef.ExternalSource) { |
| 5016 | for (uint32_t I = 0, |
| 5017 | N = SemaRef.ExternalSource->GetNumExternalSelectors(); |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 5018 | I != N; ++I) { |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5019 | Selector Sel = SemaRef.ExternalSource->GetExternalSelector(I); |
| 5020 | if (Sel.isNull() || SemaRef.MethodPool.count(Sel)) |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5021 | continue; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5022 | |
| 5023 | SemaRef.ReadMethodPool(Sel); |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5024 | } |
| 5025 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5026 | |
| 5027 | for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(), |
| 5028 | MEnd = SemaRef.MethodPool.end(); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5029 | M != MEnd; ++M) { |
| 5030 | for (ObjCMethodList *MethList = &M->second.second; |
| 5031 | MethList && MethList->Method; |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5032 | MethList = MethList->Next) { |
| 5033 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 5034 | NumSelIdents)) |
| 5035 | continue; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5036 | |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5037 | Result R(MethList->Method, 0); |
| 5038 | R.StartParameter = NumSelIdents; |
| 5039 | R.AllParametersAreInformative = false; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5040 | Results.MaybeAddResult(R, SemaRef.CurContext); |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5041 | } |
| 5042 | } |
| 5043 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5044 | |
| 5045 | Results.ExitScope(); |
| 5046 | } |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5047 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5048 | void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, |
| 5049 | IdentifierInfo **SelIdents, |
| 5050 | unsigned NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5051 | bool AtArgumentExpression, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5052 | bool IsSuper) { |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5053 | |
| 5054 | QualType T = this->GetTypeFromParser(Receiver); |
| 5055 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5056 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5057 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage, |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 5058 | T, SelIdents, NumSelIdents)); |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5059 | |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5060 | AddClassMessageCompletions(*this, S, Receiver, SelIdents, NumSelIdents, |
| 5061 | AtArgumentExpression, IsSuper, Results); |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5062 | |
| 5063 | // If we're actually at the argument expression (rather than prior to the |
| 5064 | // selector), we're actually performing code completion for an expression. |
| 5065 | // Determine whether we have a single, best method. If so, we can |
| 5066 | // code-complete the expression using the corresponding parameter type as |
| 5067 | // our preferred type, improving completion results. |
| 5068 | if (AtArgumentExpression) { |
| 5069 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5070 | NumSelIdents); |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5071 | if (PreferredType.isNull()) |
| 5072 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 5073 | else |
| 5074 | CodeCompleteExpression(S, PreferredType); |
| 5075 | return; |
| 5076 | } |
| 5077 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5078 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5079 | Results.getCompletionContext(), |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5080 | Results.data(), Results.size()); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5081 | } |
| 5082 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5083 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver, |
| 5084 | IdentifierInfo **SelIdents, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5085 | unsigned NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5086 | bool AtArgumentExpression, |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5087 | ObjCInterfaceDecl *Super) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5088 | typedef CodeCompletionResult Result; |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5089 | |
| 5090 | Expr *RecExpr = static_cast<Expr *>(Receiver); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5091 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5092 | // If necessary, apply function/array conversion to the receiver. |
| 5093 | // C99 6.7.5.3p[7,8]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5094 | if (RecExpr) { |
| 5095 | ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr); |
| 5096 | if (Conv.isInvalid()) // conversion failed. bail. |
| 5097 | return; |
| 5098 | RecExpr = Conv.take(); |
| 5099 | } |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5100 | QualType ReceiverType = RecExpr? RecExpr->getType() |
| 5101 | : Super? Context.getObjCObjectPointerType( |
| 5102 | Context.getObjCInterfaceType(Super)) |
| 5103 | : Context.getObjCIdType(); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5104 | |
Douglas Gregor | da89264 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 5105 | // If we're messaging an expression with type "id" or "Class", check |
| 5106 | // whether we know something special about the receiver that allows |
| 5107 | // us to assume a more-specific receiver type. |
| 5108 | if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) |
| 5109 | if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) { |
| 5110 | if (ReceiverType->isObjCClassType()) |
| 5111 | return CodeCompleteObjCClassMessage(S, |
| 5112 | ParsedType::make(Context.getObjCInterfaceType(IFace)), |
| 5113 | SelIdents, NumSelIdents, |
| 5114 | AtArgumentExpression, Super); |
| 5115 | |
| 5116 | ReceiverType = Context.getObjCObjectPointerType( |
| 5117 | Context.getObjCInterfaceType(IFace)); |
| 5118 | } |
| 5119 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5120 | // Build the set of methods we can see. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5121 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5122 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage, |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 5123 | ReceiverType, SelIdents, NumSelIdents)); |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5124 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5125 | Results.EnterNewScope(); |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5126 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5127 | // If this is a send-to-super, try to add the special "super" send |
| 5128 | // completion. |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5129 | if (Super) { |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5130 | if (ObjCMethodDecl *SuperMethod |
| 5131 | = AddSuperSendCompletion(*this, false, SelIdents, NumSelIdents, |
| 5132 | Results)) |
| 5133 | Results.Ignore(SuperMethod); |
| 5134 | } |
| 5135 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 5136 | // If we're inside an Objective-C method definition, prefer its selector to |
| 5137 | // others. |
| 5138 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 5139 | Results.setPreferredSelector(CurMethod->getSelector()); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5140 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5141 | // Keep track of the selectors we've already added. |
| 5142 | VisitedSelectorSet Selectors; |
| 5143 | |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5144 | // Handle messages to Class. This really isn't a message to an instance |
| 5145 | // method, so we treat it the same way we would treat a message send to a |
| 5146 | // class method. |
| 5147 | if (ReceiverType->isObjCClassType() || |
| 5148 | ReceiverType->isObjCQualifiedClassType()) { |
| 5149 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 5150 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5151 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, NumSelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5152 | CurContext, Selectors, AtArgumentExpression, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5153 | } |
| 5154 | } |
| 5155 | // Handle messages to a qualified ID ("id<foo>"). |
| 5156 | else if (const ObjCObjectPointerType *QualID |
| 5157 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 5158 | // Search protocols for instance methods. |
| 5159 | for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(), |
| 5160 | E = QualID->qual_end(); |
| 5161 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5162 | AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5163 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5164 | } |
| 5165 | // Handle messages to a pointer to interface type. |
| 5166 | else if (const ObjCObjectPointerType *IFacePtr |
| 5167 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 5168 | // Search the class, its superclasses, etc., for instance methods. |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5169 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5170 | NumSelIdents, CurContext, Selectors, AtArgumentExpression, |
| 5171 | Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5172 | |
| 5173 | // Search protocols for instance methods. |
| 5174 | for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(), |
| 5175 | E = IFacePtr->qual_end(); |
| 5176 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5177 | AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5178 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5179 | } |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5180 | // Handle messages to "id". |
| 5181 | else if (ReceiverType->isObjCIdType()) { |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5182 | // We're messaging "id", so provide all instance methods we know |
| 5183 | // about as code-completion results. |
| 5184 | |
| 5185 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 5186 | // pool from the AST file. |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5187 | if (ExternalSource) { |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 5188 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 5189 | I != N; ++I) { |
| 5190 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5191 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5192 | continue; |
| 5193 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5194 | ReadMethodPool(Sel); |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5195 | } |
| 5196 | } |
| 5197 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5198 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 5199 | MEnd = MethodPool.end(); |
| 5200 | M != MEnd; ++M) { |
| 5201 | for (ObjCMethodList *MethList = &M->second.first; |
| 5202 | MethList && MethList->Method; |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5203 | MethList = MethList->Next) { |
| 5204 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 5205 | NumSelIdents)) |
| 5206 | continue; |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5207 | |
| 5208 | if (!Selectors.insert(MethList->Method->getSelector())) |
| 5209 | continue; |
| 5210 | |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5211 | Result R(MethList->Method, 0); |
| 5212 | R.StartParameter = NumSelIdents; |
| 5213 | R.AllParametersAreInformative = false; |
| 5214 | Results.MaybeAddResult(R, CurContext); |
| 5215 | } |
| 5216 | } |
| 5217 | } |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5218 | Results.ExitScope(); |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5219 | |
| 5220 | |
| 5221 | // If we're actually at the argument expression (rather than prior to the |
| 5222 | // selector), we're actually performing code completion for an expression. |
| 5223 | // Determine whether we have a single, best method. If so, we can |
| 5224 | // code-complete the expression using the corresponding parameter type as |
| 5225 | // our preferred type, improving completion results. |
| 5226 | if (AtArgumentExpression) { |
| 5227 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
| 5228 | NumSelIdents); |
| 5229 | if (PreferredType.isNull()) |
| 5230 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 5231 | else |
| 5232 | CodeCompleteExpression(S, PreferredType); |
| 5233 | return; |
| 5234 | } |
| 5235 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5236 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5237 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5238 | Results.data(),Results.size()); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5239 | } |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5240 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 5241 | void Sema::CodeCompleteObjCForCollection(Scope *S, |
| 5242 | DeclGroupPtrTy IterationVar) { |
| 5243 | CodeCompleteExpressionData Data; |
| 5244 | Data.ObjCCollection = true; |
| 5245 | |
| 5246 | if (IterationVar.getAsOpaquePtr()) { |
| 5247 | DeclGroupRef DG = IterationVar.getAsVal<DeclGroupRef>(); |
| 5248 | for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) { |
| 5249 | if (*I) |
| 5250 | Data.IgnoreDecls.push_back(*I); |
| 5251 | } |
| 5252 | } |
| 5253 | |
| 5254 | CodeCompleteExpression(S, Data); |
| 5255 | } |
| 5256 | |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5257 | void Sema::CodeCompleteObjCSelector(Scope *S, IdentifierInfo **SelIdents, |
| 5258 | unsigned NumSelIdents) { |
| 5259 | // If we have an external source, load the entire class method |
| 5260 | // pool from the AST file. |
| 5261 | if (ExternalSource) { |
| 5262 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 5263 | I != N; ++I) { |
| 5264 | Selector Sel = ExternalSource->GetExternalSelector(I); |
| 5265 | if (Sel.isNull() || MethodPool.count(Sel)) |
| 5266 | continue; |
| 5267 | |
| 5268 | ReadMethodPool(Sel); |
| 5269 | } |
| 5270 | } |
| 5271 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5272 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5273 | CodeCompletionContext::CCC_SelectorName); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5274 | Results.EnterNewScope(); |
| 5275 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 5276 | MEnd = MethodPool.end(); |
| 5277 | M != MEnd; ++M) { |
| 5278 | |
| 5279 | Selector Sel = M->first; |
| 5280 | if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents, NumSelIdents)) |
| 5281 | continue; |
| 5282 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5283 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5284 | if (Sel.isUnarySelector()) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5285 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5286 | Sel.getNameForSlot(0))); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5287 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5288 | continue; |
| 5289 | } |
| 5290 | |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5291 | std::string Accumulator; |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5292 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) { |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5293 | if (I == NumSelIdents) { |
| 5294 | if (!Accumulator.empty()) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5295 | Builder.AddInformativeChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5296 | Accumulator)); |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5297 | Accumulator.clear(); |
| 5298 | } |
| 5299 | } |
| 5300 | |
Benjamin Kramer | a0651c5 | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 5301 | Accumulator += Sel.getNameForSlot(I); |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5302 | Accumulator += ':'; |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5303 | } |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5304 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator)); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5305 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5306 | } |
| 5307 | Results.ExitScope(); |
| 5308 | |
| 5309 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5310 | CodeCompletionContext::CCC_SelectorName, |
| 5311 | Results.data(), Results.size()); |
| 5312 | } |
| 5313 | |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5314 | /// \brief Add all of the protocol declarations that we find in the given |
| 5315 | /// (translation unit) context. |
| 5316 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5317 | bool OnlyForwardDeclarations, |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5318 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5319 | typedef CodeCompletionResult Result; |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5320 | |
| 5321 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 5322 | DEnd = Ctx->decls_end(); |
| 5323 | D != DEnd; ++D) { |
| 5324 | // Record any protocols we find. |
| 5325 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*D)) |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5326 | if (!OnlyForwardDeclarations || Proto->isForwardDecl()) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5327 | Results.AddResult(Result(Proto, 0), CurContext, 0, false); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5328 | |
| 5329 | // Record any forward-declared protocols we find. |
| 5330 | if (ObjCForwardProtocolDecl *Forward |
| 5331 | = dyn_cast<ObjCForwardProtocolDecl>(*D)) { |
| 5332 | for (ObjCForwardProtocolDecl::protocol_iterator |
| 5333 | P = Forward->protocol_begin(), |
| 5334 | PEnd = Forward->protocol_end(); |
| 5335 | P != PEnd; ++P) |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5336 | if (!OnlyForwardDeclarations || (*P)->isForwardDecl()) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5337 | Results.AddResult(Result(*P, 0), CurContext, 0, false); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5338 | } |
| 5339 | } |
| 5340 | } |
| 5341 | |
| 5342 | void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols, |
| 5343 | unsigned NumProtocols) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5344 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5345 | CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5346 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5347 | if (CodeCompleter && CodeCompleter->includeGlobals()) { |
| 5348 | Results.EnterNewScope(); |
| 5349 | |
| 5350 | // Tell the result set to ignore all of the protocols we have |
| 5351 | // already seen. |
| 5352 | // FIXME: This doesn't work when caching code-completion results. |
| 5353 | for (unsigned I = 0; I != NumProtocols; ++I) |
| 5354 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first, |
| 5355 | Protocols[I].second)) |
| 5356 | Results.Ignore(Protocol); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5357 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5358 | // Add all protocols. |
| 5359 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 5360 | Results); |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5361 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5362 | Results.ExitScope(); |
| 5363 | } |
| 5364 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5365 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5366 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 5367 | Results.data(),Results.size()); |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5368 | } |
| 5369 | |
| 5370 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5371 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5372 | CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5373 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5374 | if (CodeCompleter && CodeCompleter->includeGlobals()) { |
| 5375 | Results.EnterNewScope(); |
| 5376 | |
| 5377 | // Add all protocols. |
| 5378 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 5379 | Results); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5380 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5381 | Results.ExitScope(); |
| 5382 | } |
| 5383 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5384 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5385 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 5386 | Results.data(),Results.size()); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5387 | } |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5388 | |
| 5389 | /// \brief Add all of the Objective-C interface declarations that we find in |
| 5390 | /// the given (translation unit) context. |
| 5391 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, |
| 5392 | bool OnlyForwardDeclarations, |
| 5393 | bool OnlyUnimplemented, |
| 5394 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5395 | typedef CodeCompletionResult Result; |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5396 | |
| 5397 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 5398 | DEnd = Ctx->decls_end(); |
| 5399 | D != DEnd; ++D) { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 5400 | // Record any interfaces we find. |
| 5401 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*D)) |
| 5402 | if ((!OnlyForwardDeclarations || Class->isForwardDecl()) && |
| 5403 | (!OnlyUnimplemented || !Class->getImplementation())) |
| 5404 | Results.AddResult(Result(Class, 0), CurContext, 0, false); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5405 | |
| 5406 | // Record any forward-declared interfaces we find. |
| 5407 | if (ObjCClassDecl *Forward = dyn_cast<ObjCClassDecl>(*D)) { |
| 5408 | for (ObjCClassDecl::iterator C = Forward->begin(), CEnd = Forward->end(); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 5409 | C != CEnd; ++C) |
| 5410 | if ((!OnlyForwardDeclarations || C->getInterface()->isForwardDecl()) && |
| 5411 | (!OnlyUnimplemented || !C->getInterface()->getImplementation())) |
| 5412 | Results.AddResult(Result(C->getInterface(), 0), CurContext, |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5413 | 0, false); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5414 | } |
| 5415 | } |
| 5416 | } |
| 5417 | |
| 5418 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5419 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5420 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5421 | Results.EnterNewScope(); |
| 5422 | |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5423 | if (CodeCompleter->includeGlobals()) { |
| 5424 | // Add all classes. |
| 5425 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 5426 | false, Results); |
| 5427 | } |
| 5428 | |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5429 | Results.ExitScope(); |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5430 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5431 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5432 | CodeCompletionContext::CCC_ObjCInterfaceName, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5433 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5434 | } |
| 5435 | |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5436 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName, |
| 5437 | SourceLocation ClassNameLoc) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5438 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5439 | CodeCompletionContext::CCC_ObjCInterfaceName); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5440 | Results.EnterNewScope(); |
| 5441 | |
| 5442 | // Make sure that we ignore the class we're currently defining. |
| 5443 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5444 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5445 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5446 | Results.Ignore(CurClass); |
| 5447 | |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5448 | if (CodeCompleter->includeGlobals()) { |
| 5449 | // Add all classes. |
| 5450 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 5451 | false, Results); |
| 5452 | } |
| 5453 | |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5454 | Results.ExitScope(); |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5455 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5456 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5457 | CodeCompletionContext::CCC_ObjCInterfaceName, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5458 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5459 | } |
| 5460 | |
| 5461 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5462 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5463 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5464 | Results.EnterNewScope(); |
| 5465 | |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5466 | if (CodeCompleter->includeGlobals()) { |
| 5467 | // Add all unimplemented classes. |
| 5468 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 5469 | true, Results); |
| 5470 | } |
| 5471 | |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5472 | Results.ExitScope(); |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5473 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5474 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5475 | CodeCompletionContext::CCC_ObjCInterfaceName, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5476 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5477 | } |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5478 | |
| 5479 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5480 | IdentifierInfo *ClassName, |
| 5481 | SourceLocation ClassNameLoc) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5482 | typedef CodeCompletionResult Result; |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5483 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5484 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 5485 | CodeCompletionContext::CCC_ObjCCategoryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5486 | |
| 5487 | // Ignore any categories we find that have already been implemented by this |
| 5488 | // interface. |
| 5489 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 5490 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5491 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5492 | if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)) |
| 5493 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; |
| 5494 | Category = Category->getNextClassCategory()) |
| 5495 | CategoryNames.insert(Category->getIdentifier()); |
| 5496 | |
| 5497 | // Add all of the categories we know about. |
| 5498 | Results.EnterNewScope(); |
| 5499 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
| 5500 | for (DeclContext::decl_iterator D = TU->decls_begin(), |
| 5501 | DEnd = TU->decls_end(); |
| 5502 | D != DEnd; ++D) |
| 5503 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(*D)) |
| 5504 | if (CategoryNames.insert(Category->getIdentifier())) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5505 | Results.AddResult(Result(Category, 0), CurContext, 0, false); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5506 | Results.ExitScope(); |
| 5507 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5508 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 5509 | CodeCompletionContext::CCC_ObjCCategoryName, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5510 | Results.data(),Results.size()); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5511 | } |
| 5512 | |
| 5513 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5514 | IdentifierInfo *ClassName, |
| 5515 | SourceLocation ClassNameLoc) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5516 | typedef CodeCompletionResult Result; |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5517 | |
| 5518 | // Find the corresponding interface. If we couldn't find the interface, the |
| 5519 | // program itself is ill-formed. However, we'll try to be helpful still by |
| 5520 | // providing the list of all of the categories we know about. |
| 5521 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5522 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5523 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); |
| 5524 | if (!Class) |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5525 | return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5526 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5527 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 5528 | CodeCompletionContext::CCC_ObjCCategoryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5529 | |
| 5530 | // Add all of the categories that have have corresponding interface |
| 5531 | // declarations in this class and any of its superclasses, except for |
| 5532 | // already-implemented categories in the class itself. |
| 5533 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 5534 | Results.EnterNewScope(); |
| 5535 | bool IgnoreImplemented = true; |
| 5536 | while (Class) { |
| 5537 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; |
| 5538 | Category = Category->getNextClassCategory()) |
| 5539 | if ((!IgnoreImplemented || !Category->getImplementation()) && |
| 5540 | CategoryNames.insert(Category->getIdentifier())) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5541 | Results.AddResult(Result(Category, 0), CurContext, 0, false); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5542 | |
| 5543 | Class = Class->getSuperClass(); |
| 5544 | IgnoreImplemented = false; |
| 5545 | } |
| 5546 | Results.ExitScope(); |
| 5547 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5548 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 5549 | CodeCompletionContext::CCC_ObjCCategoryName, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5550 | Results.data(),Results.size()); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5551 | } |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5552 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5553 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5554 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5555 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5556 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5557 | |
| 5558 | // Figure out where this @synthesize lives. |
| 5559 | ObjCContainerDecl *Container |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5560 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5561 | if (!Container || |
| 5562 | (!isa<ObjCImplementationDecl>(Container) && |
| 5563 | !isa<ObjCCategoryImplDecl>(Container))) |
| 5564 | return; |
| 5565 | |
| 5566 | // Ignore any properties that have already been implemented. |
| 5567 | for (DeclContext::decl_iterator D = Container->decls_begin(), |
| 5568 | DEnd = Container->decls_end(); |
| 5569 | D != DEnd; ++D) |
| 5570 | if (ObjCPropertyImplDecl *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(*D)) |
| 5571 | Results.Ignore(PropertyImpl->getPropertyDecl()); |
| 5572 | |
| 5573 | // Add any properties that we find. |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 5574 | AddedPropertiesSet AddedProperties; |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5575 | Results.EnterNewScope(); |
| 5576 | if (ObjCImplementationDecl *ClassImpl |
| 5577 | = dyn_cast<ObjCImplementationDecl>(Container)) |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 5578 | AddObjCProperties(ClassImpl->getClassInterface(), false, |
| 5579 | /*AllowNullaryMethods=*/false, CurContext, |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 5580 | AddedProperties, Results); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5581 | else |
| 5582 | AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), |
Douglas Gregor | 4b81cde | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 5583 | false, /*AllowNullaryMethods=*/false, CurContext, |
| 5584 | AddedProperties, Results); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5585 | Results.ExitScope(); |
| 5586 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5587 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5588 | CodeCompletionContext::CCC_Other, |
| 5589 | Results.data(),Results.size()); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5590 | } |
| 5591 | |
| 5592 | void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S, |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5593 | IdentifierInfo *PropertyName) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5594 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5595 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5596 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5597 | |
| 5598 | // Figure out where this @synthesize lives. |
| 5599 | ObjCContainerDecl *Container |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5600 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5601 | if (!Container || |
| 5602 | (!isa<ObjCImplementationDecl>(Container) && |
| 5603 | !isa<ObjCCategoryImplDecl>(Container))) |
| 5604 | return; |
| 5605 | |
| 5606 | // Figure out which interface we're looking into. |
| 5607 | ObjCInterfaceDecl *Class = 0; |
| 5608 | if (ObjCImplementationDecl *ClassImpl |
| 5609 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| 5610 | Class = ClassImpl->getClassInterface(); |
| 5611 | else |
| 5612 | Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl() |
| 5613 | ->getClassInterface(); |
| 5614 | |
Douglas Gregor | e842605 | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 5615 | // Determine the type of the property we're synthesizing. |
| 5616 | QualType PropertyType = Context.getObjCIdType(); |
| 5617 | if (Class) { |
| 5618 | if (ObjCPropertyDecl *Property |
| 5619 | = Class->FindPropertyDeclaration(PropertyName)) { |
| 5620 | PropertyType |
| 5621 | = Property->getType().getNonReferenceType().getUnqualifiedType(); |
| 5622 | |
| 5623 | // Give preference to ivars |
| 5624 | Results.setPreferredType(PropertyType); |
| 5625 | } |
| 5626 | } |
| 5627 | |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5628 | // Add all of the instance variables in this class and its superclasses. |
| 5629 | Results.EnterNewScope(); |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5630 | bool SawSimilarlyNamedIvar = false; |
| 5631 | std::string NameWithPrefix; |
| 5632 | NameWithPrefix += '_'; |
Benjamin Kramer | a0651c5 | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 5633 | NameWithPrefix += PropertyName->getName(); |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5634 | std::string NameWithSuffix = PropertyName->getName().str(); |
| 5635 | NameWithSuffix += '_'; |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5636 | for(; Class; Class = Class->getSuperClass()) { |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5637 | for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar; |
| 5638 | Ivar = Ivar->getNextIvar()) { |
Douglas Gregor | e842605 | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 5639 | Results.AddResult(Result(Ivar, 0), CurContext, 0, false); |
| 5640 | |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5641 | // Determine whether we've seen an ivar with a name similar to the |
| 5642 | // property. |
Douglas Gregor | e842605 | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 5643 | if ((PropertyName == Ivar->getIdentifier() || |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5644 | NameWithPrefix == Ivar->getName() || |
Douglas Gregor | e842605 | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 5645 | NameWithSuffix == Ivar->getName())) { |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5646 | SawSimilarlyNamedIvar = true; |
Douglas Gregor | e842605 | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 5647 | |
| 5648 | // Reduce the priority of this result by one, to give it a slight |
| 5649 | // advantage over other results whose names don't match so closely. |
| 5650 | if (Results.size() && |
| 5651 | Results.data()[Results.size() - 1].Kind |
| 5652 | == CodeCompletionResult::RK_Declaration && |
| 5653 | Results.data()[Results.size() - 1].Declaration == Ivar) |
| 5654 | Results.data()[Results.size() - 1].Priority--; |
| 5655 | } |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5656 | } |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5657 | } |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5658 | |
| 5659 | if (!SawSimilarlyNamedIvar) { |
| 5660 | // Create ivar result _propName, that the user can use to synthesize |
Douglas Gregor | e842605 | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 5661 | // an ivar of the appropriate type. |
| 5662 | unsigned Priority = CCP_MemberDeclaration + 1; |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5663 | typedef CodeCompletionResult Result; |
| 5664 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
| 5665 | CodeCompletionBuilder Builder(Allocator, Priority,CXAvailability_Available); |
| 5666 | |
Douglas Gregor | e842605 | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 5667 | Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context, |
| 5668 | Allocator)); |
Douglas Gregor | aa490cb | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5669 | Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix)); |
| 5670 | Results.AddResult(Result(Builder.TakeString(), Priority, |
| 5671 | CXCursor_ObjCIvarDecl)); |
| 5672 | } |
| 5673 | |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5674 | Results.ExitScope(); |
| 5675 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5676 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5677 | CodeCompletionContext::CCC_Other, |
| 5678 | Results.data(),Results.size()); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5679 | } |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5680 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5681 | // Mapping from selectors to the methods that implement that selector, along |
| 5682 | // with the "in original class" flag. |
| 5683 | typedef llvm::DenseMap<Selector, std::pair<ObjCMethodDecl *, bool> > |
| 5684 | KnownMethodsMap; |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5685 | |
| 5686 | /// \brief Find all of the methods that reside in the given container |
| 5687 | /// (and its superclasses, protocols, etc.) that meet the given |
| 5688 | /// criteria. Insert those methods into the map of known methods, |
| 5689 | /// indexed by selector so they can be easily found. |
| 5690 | static void FindImplementableMethods(ASTContext &Context, |
| 5691 | ObjCContainerDecl *Container, |
| 5692 | bool WantInstanceMethods, |
| 5693 | QualType ReturnType, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5694 | KnownMethodsMap &KnownMethods, |
| 5695 | bool InOriginalClass = true) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5696 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 5697 | // Recurse into protocols. |
| 5698 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5699 | = IFace->getReferencedProtocols(); |
| 5700 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5701 | E = Protocols.end(); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5702 | I != E; ++I) |
| 5703 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5704 | KnownMethods, InOriginalClass); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5705 | |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5706 | // Add methods from any class extensions and categories. |
| 5707 | for (const ObjCCategoryDecl *Cat = IFace->getCategoryList(); Cat; |
| 5708 | Cat = Cat->getNextClassCategory()) |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 5709 | FindImplementableMethods(Context, const_cast<ObjCCategoryDecl*>(Cat), |
| 5710 | WantInstanceMethods, ReturnType, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5711 | KnownMethods, false); |
| 5712 | |
| 5713 | // Visit the superclass. |
| 5714 | if (IFace->getSuperClass()) |
| 5715 | FindImplementableMethods(Context, IFace->getSuperClass(), |
| 5716 | WantInstanceMethods, ReturnType, |
| 5717 | KnownMethods, false); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5718 | } |
| 5719 | |
| 5720 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 5721 | // Recurse into protocols. |
| 5722 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5723 | = Category->getReferencedProtocols(); |
| 5724 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5725 | E = Protocols.end(); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5726 | I != E; ++I) |
| 5727 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5728 | KnownMethods, InOriginalClass); |
| 5729 | |
| 5730 | // If this category is the original class, jump to the interface. |
| 5731 | if (InOriginalClass && Category->getClassInterface()) |
| 5732 | FindImplementableMethods(Context, Category->getClassInterface(), |
| 5733 | WantInstanceMethods, ReturnType, KnownMethods, |
| 5734 | false); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5735 | } |
| 5736 | |
| 5737 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 5738 | // Recurse into protocols. |
| 5739 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5740 | = Protocol->getReferencedProtocols(); |
| 5741 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 5742 | E = Protocols.end(); |
| 5743 | I != E; ++I) |
| 5744 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5745 | KnownMethods, false); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5746 | } |
| 5747 | |
| 5748 | // Add methods in this container. This operation occurs last because |
| 5749 | // we want the methods from this container to override any methods |
| 5750 | // we've previously seen with the same selector. |
| 5751 | for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), |
| 5752 | MEnd = Container->meth_end(); |
| 5753 | M != MEnd; ++M) { |
| 5754 | if ((*M)->isInstanceMethod() == WantInstanceMethods) { |
| 5755 | if (!ReturnType.isNull() && |
| 5756 | !Context.hasSameUnqualifiedType(ReturnType, (*M)->getResultType())) |
| 5757 | continue; |
| 5758 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5759 | KnownMethods[(*M)->getSelector()] = std::make_pair(*M, InOriginalClass); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5760 | } |
| 5761 | } |
| 5762 | } |
| 5763 | |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5764 | /// \brief Add the parenthesized return or parameter type chunk to a code |
| 5765 | /// completion string. |
| 5766 | static void AddObjCPassingTypeChunk(QualType Type, |
| 5767 | ASTContext &Context, |
| 5768 | CodeCompletionBuilder &Builder) { |
| 5769 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5770 | Builder.AddTextChunk(GetCompletionTypeString(Type, Context, |
| 5771 | Builder.getAllocator())); |
| 5772 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5773 | } |
| 5774 | |
| 5775 | /// \brief Determine whether the given class is or inherits from a class by |
| 5776 | /// the given name. |
| 5777 | static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5778 | StringRef Name) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5779 | if (!Class) |
| 5780 | return false; |
| 5781 | |
| 5782 | if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name) |
| 5783 | return true; |
| 5784 | |
| 5785 | return InheritsFromClassNamed(Class->getSuperClass(), Name); |
| 5786 | } |
| 5787 | |
| 5788 | /// \brief Add code completions for Objective-C Key-Value Coding (KVC) and |
| 5789 | /// Key-Value Observing (KVO). |
| 5790 | static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property, |
| 5791 | bool IsInstanceMethod, |
| 5792 | QualType ReturnType, |
| 5793 | ASTContext &Context, |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 5794 | VisitedSelectorSet &KnownSelectors, |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5795 | ResultBuilder &Results) { |
| 5796 | IdentifierInfo *PropName = Property->getIdentifier(); |
| 5797 | if (!PropName || PropName->getLength() == 0) |
| 5798 | return; |
| 5799 | |
| 5800 | |
| 5801 | // Builder that will create each code completion. |
| 5802 | typedef CodeCompletionResult Result; |
| 5803 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
| 5804 | CodeCompletionBuilder Builder(Allocator); |
| 5805 | |
| 5806 | // The selector table. |
| 5807 | SelectorTable &Selectors = Context.Selectors; |
| 5808 | |
| 5809 | // The property name, copied into the code completion allocation region |
| 5810 | // on demand. |
| 5811 | struct KeyHolder { |
| 5812 | CodeCompletionAllocator &Allocator; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5813 | StringRef Key; |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5814 | const char *CopiedKey; |
| 5815 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5816 | KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key) |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5817 | : Allocator(Allocator), Key(Key), CopiedKey(0) { } |
| 5818 | |
| 5819 | operator const char *() { |
| 5820 | if (CopiedKey) |
| 5821 | return CopiedKey; |
| 5822 | |
| 5823 | return CopiedKey = Allocator.CopyString(Key); |
| 5824 | } |
| 5825 | } Key(Allocator, PropName->getName()); |
| 5826 | |
| 5827 | // The uppercased name of the property name. |
| 5828 | std::string UpperKey = PropName->getName(); |
| 5829 | if (!UpperKey.empty()) |
| 5830 | UpperKey[0] = toupper(UpperKey[0]); |
| 5831 | |
| 5832 | bool ReturnTypeMatchesProperty = ReturnType.isNull() || |
| 5833 | Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(), |
| 5834 | Property->getType()); |
| 5835 | bool ReturnTypeMatchesVoid |
| 5836 | = ReturnType.isNull() || ReturnType->isVoidType(); |
| 5837 | |
| 5838 | // Add the normal accessor -(type)key. |
| 5839 | if (IsInstanceMethod && |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 5840 | KnownSelectors.insert(Selectors.getNullarySelector(PropName)) && |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5841 | ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) { |
| 5842 | if (ReturnType.isNull()) |
| 5843 | AddObjCPassingTypeChunk(Property->getType(), Context, Builder); |
| 5844 | |
| 5845 | Builder.AddTypedTextChunk(Key); |
| 5846 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 5847 | CXCursor_ObjCInstanceMethodDecl)); |
| 5848 | } |
| 5849 | |
| 5850 | // If we have an integral or boolean property (or the user has provided |
| 5851 | // an integral or boolean return type), add the accessor -(type)isKey. |
| 5852 | if (IsInstanceMethod && |
| 5853 | ((!ReturnType.isNull() && |
| 5854 | (ReturnType->isIntegerType() || ReturnType->isBooleanType())) || |
| 5855 | (ReturnType.isNull() && |
| 5856 | (Property->getType()->isIntegerType() || |
| 5857 | Property->getType()->isBooleanType())))) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5858 | std::string SelectorName = (Twine("is") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5859 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 5860 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5861 | if (ReturnType.isNull()) { |
| 5862 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5863 | Builder.AddTextChunk("BOOL"); |
| 5864 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5865 | } |
| 5866 | |
| 5867 | Builder.AddTypedTextChunk( |
| 5868 | Allocator.CopyString(SelectorId->getName())); |
| 5869 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 5870 | CXCursor_ObjCInstanceMethodDecl)); |
| 5871 | } |
| 5872 | } |
| 5873 | |
| 5874 | // Add the normal mutator. |
| 5875 | if (IsInstanceMethod && ReturnTypeMatchesVoid && |
| 5876 | !Property->getSetterMethodDecl()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5877 | std::string SelectorName = (Twine("set") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5878 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 5879 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5880 | if (ReturnType.isNull()) { |
| 5881 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5882 | Builder.AddTextChunk("void"); |
| 5883 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5884 | } |
| 5885 | |
| 5886 | Builder.AddTypedTextChunk( |
| 5887 | Allocator.CopyString(SelectorId->getName())); |
| 5888 | Builder.AddTypedTextChunk(":"); |
| 5889 | AddObjCPassingTypeChunk(Property->getType(), Context, Builder); |
| 5890 | Builder.AddTextChunk(Key); |
| 5891 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 5892 | CXCursor_ObjCInstanceMethodDecl)); |
| 5893 | } |
| 5894 | } |
| 5895 | |
| 5896 | // Indexed and unordered accessors |
| 5897 | unsigned IndexedGetterPriority = CCP_CodePattern; |
| 5898 | unsigned IndexedSetterPriority = CCP_CodePattern; |
| 5899 | unsigned UnorderedGetterPriority = CCP_CodePattern; |
| 5900 | unsigned UnorderedSetterPriority = CCP_CodePattern; |
| 5901 | if (const ObjCObjectPointerType *ObjCPointer |
| 5902 | = Property->getType()->getAs<ObjCObjectPointerType>()) { |
| 5903 | if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) { |
| 5904 | // If this interface type is not provably derived from a known |
| 5905 | // collection, penalize the corresponding completions. |
| 5906 | if (!InheritsFromClassNamed(IFace, "NSMutableArray")) { |
| 5907 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 5908 | if (!InheritsFromClassNamed(IFace, "NSArray")) |
| 5909 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 5910 | } |
| 5911 | |
| 5912 | if (!InheritsFromClassNamed(IFace, "NSMutableSet")) { |
| 5913 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 5914 | if (!InheritsFromClassNamed(IFace, "NSSet")) |
| 5915 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 5916 | } |
| 5917 | } |
| 5918 | } else { |
| 5919 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 5920 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 5921 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 5922 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 5923 | } |
| 5924 | |
| 5925 | // Add -(NSUInteger)countOf<key> |
| 5926 | if (IsInstanceMethod && |
| 5927 | (ReturnType.isNull() || ReturnType->isIntegerType())) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5928 | std::string SelectorName = (Twine("countOf") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5929 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 5930 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5931 | if (ReturnType.isNull()) { |
| 5932 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5933 | Builder.AddTextChunk("NSUInteger"); |
| 5934 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5935 | } |
| 5936 | |
| 5937 | Builder.AddTypedTextChunk( |
| 5938 | Allocator.CopyString(SelectorId->getName())); |
| 5939 | Results.AddResult(Result(Builder.TakeString(), |
| 5940 | std::min(IndexedGetterPriority, |
| 5941 | UnorderedGetterPriority), |
| 5942 | CXCursor_ObjCInstanceMethodDecl)); |
| 5943 | } |
| 5944 | } |
| 5945 | |
| 5946 | // Indexed getters |
| 5947 | // Add -(id)objectInKeyAtIndex:(NSUInteger)index |
| 5948 | if (IsInstanceMethod && |
| 5949 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5950 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5951 | = (Twine("objectIn") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5952 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 5953 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5954 | if (ReturnType.isNull()) { |
| 5955 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5956 | Builder.AddTextChunk("id"); |
| 5957 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5958 | } |
| 5959 | |
| 5960 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 5961 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5962 | Builder.AddTextChunk("NSUInteger"); |
| 5963 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5964 | Builder.AddTextChunk("index"); |
| 5965 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 5966 | CXCursor_ObjCInstanceMethodDecl)); |
| 5967 | } |
| 5968 | } |
| 5969 | |
| 5970 | // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes |
| 5971 | if (IsInstanceMethod && |
| 5972 | (ReturnType.isNull() || |
| 5973 | (ReturnType->isObjCObjectPointerType() && |
| 5974 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 5975 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 5976 | ->getName() == "NSArray"))) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5977 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5978 | = (Twine(Property->getName()) + "AtIndexes").str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5979 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 5980 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5981 | if (ReturnType.isNull()) { |
| 5982 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5983 | Builder.AddTextChunk("NSArray *"); |
| 5984 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5985 | } |
| 5986 | |
| 5987 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 5988 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5989 | Builder.AddTextChunk("NSIndexSet *"); |
| 5990 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5991 | Builder.AddTextChunk("indexes"); |
| 5992 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 5993 | CXCursor_ObjCInstanceMethodDecl)); |
| 5994 | } |
| 5995 | } |
| 5996 | |
| 5997 | // Add -(void)getKey:(type **)buffer range:(NSRange)inRange |
| 5998 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5999 | std::string SelectorName = (Twine("get") + UpperKey).str(); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6000 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6001 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6002 | &Context.Idents.get("range") |
| 6003 | }; |
| 6004 | |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6005 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6006 | if (ReturnType.isNull()) { |
| 6007 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6008 | Builder.AddTextChunk("void"); |
| 6009 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6010 | } |
| 6011 | |
| 6012 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6013 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6014 | Builder.AddPlaceholderChunk("object-type"); |
| 6015 | Builder.AddTextChunk(" **"); |
| 6016 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6017 | Builder.AddTextChunk("buffer"); |
| 6018 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6019 | Builder.AddTypedTextChunk("range:"); |
| 6020 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6021 | Builder.AddTextChunk("NSRange"); |
| 6022 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6023 | Builder.AddTextChunk("inRange"); |
| 6024 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 6025 | CXCursor_ObjCInstanceMethodDecl)); |
| 6026 | } |
| 6027 | } |
| 6028 | |
| 6029 | // Mutable indexed accessors |
| 6030 | |
| 6031 | // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index |
| 6032 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6033 | std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6034 | IdentifierInfo *SelectorIds[2] = { |
| 6035 | &Context.Idents.get("insertObject"), |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6036 | &Context.Idents.get(SelectorName) |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6037 | }; |
| 6038 | |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6039 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6040 | if (ReturnType.isNull()) { |
| 6041 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6042 | Builder.AddTextChunk("void"); |
| 6043 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6044 | } |
| 6045 | |
| 6046 | Builder.AddTypedTextChunk("insertObject:"); |
| 6047 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6048 | Builder.AddPlaceholderChunk("object-type"); |
| 6049 | Builder.AddTextChunk(" *"); |
| 6050 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6051 | Builder.AddTextChunk("object"); |
| 6052 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6053 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6054 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6055 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 6056 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6057 | Builder.AddTextChunk("index"); |
| 6058 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6059 | CXCursor_ObjCInstanceMethodDecl)); |
| 6060 | } |
| 6061 | } |
| 6062 | |
| 6063 | // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes |
| 6064 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6065 | std::string SelectorName = (Twine("insert") + UpperKey).str(); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6066 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6067 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6068 | &Context.Idents.get("atIndexes") |
| 6069 | }; |
| 6070 | |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6071 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6072 | if (ReturnType.isNull()) { |
| 6073 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6074 | Builder.AddTextChunk("void"); |
| 6075 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6076 | } |
| 6077 | |
| 6078 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6079 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6080 | Builder.AddTextChunk("NSArray *"); |
| 6081 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6082 | Builder.AddTextChunk("array"); |
| 6083 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6084 | Builder.AddTypedTextChunk("atIndexes:"); |
| 6085 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6086 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 6087 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6088 | Builder.AddTextChunk("indexes"); |
| 6089 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6090 | CXCursor_ObjCInstanceMethodDecl)); |
| 6091 | } |
| 6092 | } |
| 6093 | |
| 6094 | // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index |
| 6095 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6096 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6097 | = (Twine("removeObjectFrom") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6098 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6099 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6100 | if (ReturnType.isNull()) { |
| 6101 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6102 | Builder.AddTextChunk("void"); |
| 6103 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6104 | } |
| 6105 | |
| 6106 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6107 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6108 | Builder.AddTextChunk("NSUInteger"); |
| 6109 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6110 | Builder.AddTextChunk("index"); |
| 6111 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6112 | CXCursor_ObjCInstanceMethodDecl)); |
| 6113 | } |
| 6114 | } |
| 6115 | |
| 6116 | // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes |
| 6117 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6118 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6119 | = (Twine("remove") + UpperKey + "AtIndexes").str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6120 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6121 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6122 | if (ReturnType.isNull()) { |
| 6123 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6124 | Builder.AddTextChunk("void"); |
| 6125 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6126 | } |
| 6127 | |
| 6128 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6129 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6130 | Builder.AddTextChunk("NSIndexSet *"); |
| 6131 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6132 | Builder.AddTextChunk("indexes"); |
| 6133 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6134 | CXCursor_ObjCInstanceMethodDecl)); |
| 6135 | } |
| 6136 | } |
| 6137 | |
| 6138 | // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object |
| 6139 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6140 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6141 | = (Twine("replaceObjectIn") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6142 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6143 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6144 | &Context.Idents.get("withObject") |
| 6145 | }; |
| 6146 | |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6147 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6148 | if (ReturnType.isNull()) { |
| 6149 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6150 | Builder.AddTextChunk("void"); |
| 6151 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6152 | } |
| 6153 | |
| 6154 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6155 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6156 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 6157 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6158 | Builder.AddTextChunk("index"); |
| 6159 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6160 | Builder.AddTypedTextChunk("withObject:"); |
| 6161 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6162 | Builder.AddTextChunk("id"); |
| 6163 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6164 | Builder.AddTextChunk("object"); |
| 6165 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6166 | CXCursor_ObjCInstanceMethodDecl)); |
| 6167 | } |
| 6168 | } |
| 6169 | |
| 6170 | // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array |
| 6171 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6172 | std::string SelectorName1 |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6173 | = (Twine("replace") + UpperKey + "AtIndexes").str(); |
| 6174 | std::string SelectorName2 = (Twine("with") + UpperKey).str(); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6175 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6176 | &Context.Idents.get(SelectorName1), |
| 6177 | &Context.Idents.get(SelectorName2) |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6178 | }; |
| 6179 | |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6180 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6181 | if (ReturnType.isNull()) { |
| 6182 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6183 | Builder.AddTextChunk("void"); |
| 6184 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6185 | } |
| 6186 | |
| 6187 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":")); |
| 6188 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6189 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 6190 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6191 | Builder.AddTextChunk("indexes"); |
| 6192 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6193 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":")); |
| 6194 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6195 | Builder.AddTextChunk("NSArray *"); |
| 6196 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6197 | Builder.AddTextChunk("array"); |
| 6198 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 6199 | CXCursor_ObjCInstanceMethodDecl)); |
| 6200 | } |
| 6201 | } |
| 6202 | |
| 6203 | // Unordered getters |
| 6204 | // - (NSEnumerator *)enumeratorOfKey |
| 6205 | if (IsInstanceMethod && |
| 6206 | (ReturnType.isNull() || |
| 6207 | (ReturnType->isObjCObjectPointerType() && |
| 6208 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 6209 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 6210 | ->getName() == "NSEnumerator"))) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6211 | std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6212 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6213 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6214 | if (ReturnType.isNull()) { |
| 6215 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6216 | Builder.AddTextChunk("NSEnumerator *"); |
| 6217 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6218 | } |
| 6219 | |
| 6220 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 6221 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
| 6222 | CXCursor_ObjCInstanceMethodDecl)); |
| 6223 | } |
| 6224 | } |
| 6225 | |
| 6226 | // - (type *)memberOfKey:(type *)object |
| 6227 | if (IsInstanceMethod && |
| 6228 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6229 | std::string SelectorName = (Twine("memberOf") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6230 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6231 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6232 | if (ReturnType.isNull()) { |
| 6233 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6234 | Builder.AddPlaceholderChunk("object-type"); |
| 6235 | Builder.AddTextChunk(" *"); |
| 6236 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6237 | } |
| 6238 | |
| 6239 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6240 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6241 | if (ReturnType.isNull()) { |
| 6242 | Builder.AddPlaceholderChunk("object-type"); |
| 6243 | Builder.AddTextChunk(" *"); |
| 6244 | } else { |
| 6245 | Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context, |
| 6246 | Builder.getAllocator())); |
| 6247 | } |
| 6248 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6249 | Builder.AddTextChunk("object"); |
| 6250 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
| 6251 | CXCursor_ObjCInstanceMethodDecl)); |
| 6252 | } |
| 6253 | } |
| 6254 | |
| 6255 | // Mutable unordered accessors |
| 6256 | // - (void)addKeyObject:(type *)object |
| 6257 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6258 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6259 | = (Twine("add") + UpperKey + Twine("Object")).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6260 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6261 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6262 | if (ReturnType.isNull()) { |
| 6263 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6264 | Builder.AddTextChunk("void"); |
| 6265 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6266 | } |
| 6267 | |
| 6268 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6269 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6270 | Builder.AddPlaceholderChunk("object-type"); |
| 6271 | Builder.AddTextChunk(" *"); |
| 6272 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6273 | Builder.AddTextChunk("object"); |
| 6274 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6275 | CXCursor_ObjCInstanceMethodDecl)); |
| 6276 | } |
| 6277 | } |
| 6278 | |
| 6279 | // - (void)addKey:(NSSet *)objects |
| 6280 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6281 | std::string SelectorName = (Twine("add") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6282 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6283 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6284 | if (ReturnType.isNull()) { |
| 6285 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6286 | Builder.AddTextChunk("void"); |
| 6287 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6288 | } |
| 6289 | |
| 6290 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6291 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6292 | Builder.AddTextChunk("NSSet *"); |
| 6293 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6294 | Builder.AddTextChunk("objects"); |
| 6295 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6296 | CXCursor_ObjCInstanceMethodDecl)); |
| 6297 | } |
| 6298 | } |
| 6299 | |
| 6300 | // - (void)removeKeyObject:(type *)object |
| 6301 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6302 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6303 | = (Twine("remove") + UpperKey + Twine("Object")).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6304 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6305 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6306 | if (ReturnType.isNull()) { |
| 6307 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6308 | Builder.AddTextChunk("void"); |
| 6309 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6310 | } |
| 6311 | |
| 6312 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6313 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6314 | Builder.AddPlaceholderChunk("object-type"); |
| 6315 | Builder.AddTextChunk(" *"); |
| 6316 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6317 | Builder.AddTextChunk("object"); |
| 6318 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6319 | CXCursor_ObjCInstanceMethodDecl)); |
| 6320 | } |
| 6321 | } |
| 6322 | |
| 6323 | // - (void)removeKey:(NSSet *)objects |
| 6324 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6325 | std::string SelectorName = (Twine("remove") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6326 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6327 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6328 | if (ReturnType.isNull()) { |
| 6329 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6330 | Builder.AddTextChunk("void"); |
| 6331 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6332 | } |
| 6333 | |
| 6334 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6335 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6336 | Builder.AddTextChunk("NSSet *"); |
| 6337 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6338 | Builder.AddTextChunk("objects"); |
| 6339 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6340 | CXCursor_ObjCInstanceMethodDecl)); |
| 6341 | } |
| 6342 | } |
| 6343 | |
| 6344 | // - (void)intersectKey:(NSSet *)objects |
| 6345 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6346 | std::string SelectorName = (Twine("intersect") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6347 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6348 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6349 | if (ReturnType.isNull()) { |
| 6350 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6351 | Builder.AddTextChunk("void"); |
| 6352 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6353 | } |
| 6354 | |
| 6355 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6356 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6357 | Builder.AddTextChunk("NSSet *"); |
| 6358 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6359 | Builder.AddTextChunk("objects"); |
| 6360 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6361 | CXCursor_ObjCInstanceMethodDecl)); |
| 6362 | } |
| 6363 | } |
| 6364 | |
| 6365 | // Key-Value Observing |
| 6366 | // + (NSSet *)keyPathsForValuesAffectingKey |
| 6367 | if (!IsInstanceMethod && |
| 6368 | (ReturnType.isNull() || |
| 6369 | (ReturnType->isObjCObjectPointerType() && |
| 6370 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 6371 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 6372 | ->getName() == "NSSet"))) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6373 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6374 | = (Twine("keyPathsForValuesAffecting") + UpperKey).str(); |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6375 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6376 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6377 | if (ReturnType.isNull()) { |
| 6378 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6379 | Builder.AddTextChunk("NSSet *"); |
| 6380 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6381 | } |
| 6382 | |
| 6383 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 6384 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Douglas Gregor | 3f828d1 | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 6385 | CXCursor_ObjCClassMethodDecl)); |
| 6386 | } |
| 6387 | } |
| 6388 | |
| 6389 | // + (BOOL)automaticallyNotifiesObserversForKey |
| 6390 | if (!IsInstanceMethod && |
| 6391 | (ReturnType.isNull() || |
| 6392 | ReturnType->isIntegerType() || |
| 6393 | ReturnType->isBooleanType())) { |
| 6394 | std::string SelectorName |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6395 | = (Twine("automaticallyNotifiesObserversOf") + UpperKey).str(); |
Douglas Gregor | 3f828d1 | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 6396 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| 6397 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) { |
| 6398 | if (ReturnType.isNull()) { |
| 6399 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6400 | Builder.AddTextChunk("BOOL"); |
| 6401 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6402 | } |
| 6403 | |
| 6404 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 6405 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 6406 | CXCursor_ObjCClassMethodDecl)); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6407 | } |
| 6408 | } |
| 6409 | } |
| 6410 | |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6411 | void Sema::CodeCompleteObjCMethodDecl(Scope *S, |
| 6412 | bool IsInstanceMethod, |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6413 | ParsedType ReturnTy) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6414 | // Determine the return type of the method we're declaring, if |
| 6415 | // provided. |
| 6416 | QualType ReturnType = GetTypeFromParser(ReturnTy); |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6417 | Decl *IDecl = 0; |
| 6418 | if (CurContext->isObjCContainer()) { |
| 6419 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
| 6420 | IDecl = cast<Decl>(OCD); |
| 6421 | } |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6422 | // Determine where we should start searching for methods. |
| 6423 | ObjCContainerDecl *SearchDecl = 0; |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6424 | bool IsInImplementation = false; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6425 | if (Decl *D = IDecl) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6426 | if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) { |
| 6427 | SearchDecl = Impl->getClassInterface(); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6428 | IsInImplementation = true; |
| 6429 | } else if (ObjCCategoryImplDecl *CatImpl |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6430 | = dyn_cast<ObjCCategoryImplDecl>(D)) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6431 | SearchDecl = CatImpl->getCategoryDecl(); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6432 | IsInImplementation = true; |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6433 | } else |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6434 | SearchDecl = dyn_cast<ObjCContainerDecl>(D); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6435 | } |
| 6436 | |
| 6437 | if (!SearchDecl && S) { |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6438 | if (DeclContext *DC = static_cast<DeclContext *>(S->getEntity())) |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6439 | SearchDecl = dyn_cast<ObjCContainerDecl>(DC); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6440 | } |
| 6441 | |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6442 | if (!SearchDecl) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6443 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6444 | CodeCompletionContext::CCC_Other, |
| 6445 | 0, 0); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6446 | return; |
| 6447 | } |
| 6448 | |
| 6449 | // Find all of the methods that we could declare/implement here. |
| 6450 | KnownMethodsMap KnownMethods; |
| 6451 | FindImplementableMethods(Context, SearchDecl, IsInstanceMethod, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6452 | ReturnType, KnownMethods); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6453 | |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6454 | // Add declarations or definitions for each of the known methods. |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6455 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6456 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 6457 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6458 | Results.EnterNewScope(); |
| 6459 | PrintingPolicy Policy(Context.PrintingPolicy); |
| 6460 | Policy.AnonymousTagLocations = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6461 | Policy.SuppressStrongLifetime = true; |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6462 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
| 6463 | MEnd = KnownMethods.end(); |
| 6464 | M != MEnd; ++M) { |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6465 | ObjCMethodDecl *Method = M->second.first; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6466 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6467 | |
| 6468 | // If the result type was not already provided, add it to the |
| 6469 | // pattern as (type). |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6470 | if (ReturnType.isNull()) |
| 6471 | AddObjCPassingTypeChunk(Method->getResultType(), Context, Builder); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6472 | |
| 6473 | Selector Sel = Method->getSelector(); |
| 6474 | |
| 6475 | // Add the first part of the selector to the pattern. |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6476 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 6477 | Sel.getNameForSlot(0))); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6478 | |
| 6479 | // Add parameters to the pattern. |
| 6480 | unsigned I = 0; |
| 6481 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 6482 | PEnd = Method->param_end(); |
| 6483 | P != PEnd; (void)++P, ++I) { |
| 6484 | // Add the part of the selector name. |
| 6485 | if (I == 0) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6486 | Builder.AddTypedTextChunk(":"); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6487 | else if (I < Sel.getNumArgs()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6488 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6489 | Builder.AddTypedTextChunk( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 6490 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6491 | } else |
| 6492 | break; |
| 6493 | |
| 6494 | // Add the parameter type. |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6495 | AddObjCPassingTypeChunk((*P)->getOriginalType(), Context, Builder); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6496 | |
| 6497 | if (IdentifierInfo *Id = (*P)->getIdentifier()) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6498 | Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName())); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6499 | } |
| 6500 | |
| 6501 | if (Method->isVariadic()) { |
| 6502 | if (Method->param_size() > 0) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6503 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 6504 | Builder.AddTextChunk("..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 6505 | } |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6506 | |
Douglas Gregor | 447107d | 2010-05-28 00:57:46 +0000 | [diff] [blame] | 6507 | if (IsInImplementation && Results.includeCodePatterns()) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6508 | // We will be defining the method here, so add a compound statement. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6509 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6510 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 6511 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6512 | if (!Method->getResultType()->isVoidType()) { |
| 6513 | // If the result type is not void, add a return clause. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6514 | Builder.AddTextChunk("return"); |
| 6515 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6516 | Builder.AddPlaceholderChunk("expression"); |
| 6517 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6518 | } else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6519 | Builder.AddPlaceholderChunk("statements"); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6520 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6521 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 6522 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6523 | } |
| 6524 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6525 | unsigned Priority = CCP_CodePattern; |
| 6526 | if (!M->second.second) |
| 6527 | Priority += CCD_InBaseClass; |
| 6528 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6529 | Results.AddResult(Result(Builder.TakeString(), Priority, |
Douglas Gregor | 16ed9ad | 2010-08-17 16:06:07 +0000 | [diff] [blame] | 6530 | Method->isInstanceMethod() |
| 6531 | ? CXCursor_ObjCInstanceMethodDecl |
| 6532 | : CXCursor_ObjCClassMethodDecl)); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6533 | } |
| 6534 | |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6535 | // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of |
| 6536 | // the properties in this class and its categories. |
| 6537 | if (Context.getLangOptions().ObjC2) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6538 | SmallVector<ObjCContainerDecl *, 4> Containers; |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6539 | Containers.push_back(SearchDecl); |
| 6540 | |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6541 | VisitedSelectorSet KnownSelectors; |
| 6542 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
| 6543 | MEnd = KnownMethods.end(); |
| 6544 | M != MEnd; ++M) |
| 6545 | KnownSelectors.insert(M->first); |
| 6546 | |
| 6547 | |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6548 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl); |
| 6549 | if (!IFace) |
| 6550 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl)) |
| 6551 | IFace = Category->getClassInterface(); |
| 6552 | |
| 6553 | if (IFace) { |
| 6554 | for (ObjCCategoryDecl *Category = IFace->getCategoryList(); Category; |
| 6555 | Category = Category->getNextClassCategory()) |
| 6556 | Containers.push_back(Category); |
| 6557 | } |
| 6558 | |
| 6559 | for (unsigned I = 0, N = Containers.size(); I != N; ++I) { |
| 6560 | for (ObjCContainerDecl::prop_iterator P = Containers[I]->prop_begin(), |
| 6561 | PEnd = Containers[I]->prop_end(); |
| 6562 | P != PEnd; ++P) { |
| 6563 | AddObjCKeyValueCompletions(*P, IsInstanceMethod, ReturnType, Context, |
Douglas Gregor | e74c25c | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6564 | KnownSelectors, Results); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6565 | } |
| 6566 | } |
| 6567 | } |
| 6568 | |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6569 | Results.ExitScope(); |
| 6570 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6571 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6572 | CodeCompletionContext::CCC_Other, |
| 6573 | Results.data(),Results.size()); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6574 | } |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6575 | |
| 6576 | void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S, |
| 6577 | bool IsInstanceMethod, |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 6578 | bool AtParameterName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6579 | ParsedType ReturnTy, |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6580 | IdentifierInfo **SelIdents, |
| 6581 | unsigned NumSelIdents) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6582 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 6583 | // pool from the AST file. |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6584 | if (ExternalSource) { |
| 6585 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 6586 | I != N; ++I) { |
| 6587 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6588 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6589 | continue; |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6590 | |
| 6591 | ReadMethodPool(Sel); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6592 | } |
| 6593 | } |
| 6594 | |
| 6595 | // Build the set of methods we can see. |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6596 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6597 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 6598 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6599 | |
| 6600 | if (ReturnTy) |
| 6601 | Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType()); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6602 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6603 | Results.EnterNewScope(); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6604 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 6605 | MEnd = MethodPool.end(); |
| 6606 | M != MEnd; ++M) { |
| 6607 | for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first : |
| 6608 | &M->second.second; |
| 6609 | MethList && MethList->Method; |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6610 | MethList = MethList->Next) { |
| 6611 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 6612 | NumSelIdents)) |
| 6613 | continue; |
| 6614 | |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 6615 | if (AtParameterName) { |
| 6616 | // Suggest parameter names we've seen before. |
| 6617 | if (NumSelIdents && NumSelIdents <= MethList->Method->param_size()) { |
| 6618 | ParmVarDecl *Param = MethList->Method->param_begin()[NumSelIdents-1]; |
| 6619 | if (Param->getIdentifier()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6620 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6621 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6622 | Param->getIdentifier()->getName())); |
| 6623 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 6624 | } |
| 6625 | } |
| 6626 | |
| 6627 | continue; |
| 6628 | } |
| 6629 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6630 | Result R(MethList->Method, 0); |
| 6631 | R.StartParameter = NumSelIdents; |
| 6632 | R.AllParametersAreInformative = false; |
| 6633 | R.DeclaringEntity = true; |
| 6634 | Results.MaybeAddResult(R, CurContext); |
| 6635 | } |
| 6636 | } |
| 6637 | |
| 6638 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6639 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6640 | CodeCompletionContext::CCC_Other, |
| 6641 | Results.data(),Results.size()); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6642 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 6643 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6644 | void Sema::CodeCompletePreprocessorDirective(bool InConditional) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6645 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 6646 | CodeCompletionContext::CCC_PreprocessorDirective); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6647 | Results.EnterNewScope(); |
| 6648 | |
| 6649 | // #if <condition> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6650 | CodeCompletionBuilder Builder(Results.getAllocator()); |
| 6651 | Builder.AddTypedTextChunk("if"); |
| 6652 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6653 | Builder.AddPlaceholderChunk("condition"); |
| 6654 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6655 | |
| 6656 | // #ifdef <macro> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6657 | Builder.AddTypedTextChunk("ifdef"); |
| 6658 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6659 | Builder.AddPlaceholderChunk("macro"); |
| 6660 | Results.AddResult(Builder.TakeString()); |
| 6661 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6662 | // #ifndef <macro> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6663 | Builder.AddTypedTextChunk("ifndef"); |
| 6664 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6665 | Builder.AddPlaceholderChunk("macro"); |
| 6666 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6667 | |
| 6668 | if (InConditional) { |
| 6669 | // #elif <condition> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6670 | Builder.AddTypedTextChunk("elif"); |
| 6671 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6672 | Builder.AddPlaceholderChunk("condition"); |
| 6673 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6674 | |
| 6675 | // #else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6676 | Builder.AddTypedTextChunk("else"); |
| 6677 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6678 | |
| 6679 | // #endif |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6680 | Builder.AddTypedTextChunk("endif"); |
| 6681 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6682 | } |
| 6683 | |
| 6684 | // #include "header" |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6685 | Builder.AddTypedTextChunk("include"); |
| 6686 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6687 | Builder.AddTextChunk("\""); |
| 6688 | Builder.AddPlaceholderChunk("header"); |
| 6689 | Builder.AddTextChunk("\""); |
| 6690 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6691 | |
| 6692 | // #include <header> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6693 | Builder.AddTypedTextChunk("include"); |
| 6694 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6695 | Builder.AddTextChunk("<"); |
| 6696 | Builder.AddPlaceholderChunk("header"); |
| 6697 | Builder.AddTextChunk(">"); |
| 6698 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6699 | |
| 6700 | // #define <macro> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6701 | Builder.AddTypedTextChunk("define"); |
| 6702 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6703 | Builder.AddPlaceholderChunk("macro"); |
| 6704 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6705 | |
| 6706 | // #define <macro>(<args>) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6707 | Builder.AddTypedTextChunk("define"); |
| 6708 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6709 | Builder.AddPlaceholderChunk("macro"); |
| 6710 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6711 | Builder.AddPlaceholderChunk("args"); |
| 6712 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6713 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6714 | |
| 6715 | // #undef <macro> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6716 | Builder.AddTypedTextChunk("undef"); |
| 6717 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6718 | Builder.AddPlaceholderChunk("macro"); |
| 6719 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6720 | |
| 6721 | // #line <number> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6722 | Builder.AddTypedTextChunk("line"); |
| 6723 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6724 | Builder.AddPlaceholderChunk("number"); |
| 6725 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6726 | |
| 6727 | // #line <number> "filename" |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6728 | Builder.AddTypedTextChunk("line"); |
| 6729 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6730 | Builder.AddPlaceholderChunk("number"); |
| 6731 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6732 | Builder.AddTextChunk("\""); |
| 6733 | Builder.AddPlaceholderChunk("filename"); |
| 6734 | Builder.AddTextChunk("\""); |
| 6735 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6736 | |
| 6737 | // #error <message> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6738 | Builder.AddTypedTextChunk("error"); |
| 6739 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6740 | Builder.AddPlaceholderChunk("message"); |
| 6741 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6742 | |
| 6743 | // #pragma <arguments> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6744 | Builder.AddTypedTextChunk("pragma"); |
| 6745 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6746 | Builder.AddPlaceholderChunk("arguments"); |
| 6747 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6748 | |
| 6749 | if (getLangOptions().ObjC1) { |
| 6750 | // #import "header" |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6751 | Builder.AddTypedTextChunk("import"); |
| 6752 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6753 | Builder.AddTextChunk("\""); |
| 6754 | Builder.AddPlaceholderChunk("header"); |
| 6755 | Builder.AddTextChunk("\""); |
| 6756 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6757 | |
| 6758 | // #import <header> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6759 | Builder.AddTypedTextChunk("import"); |
| 6760 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6761 | Builder.AddTextChunk("<"); |
| 6762 | Builder.AddPlaceholderChunk("header"); |
| 6763 | Builder.AddTextChunk(">"); |
| 6764 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6765 | } |
| 6766 | |
| 6767 | // #include_next "header" |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6768 | Builder.AddTypedTextChunk("include_next"); |
| 6769 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6770 | Builder.AddTextChunk("\""); |
| 6771 | Builder.AddPlaceholderChunk("header"); |
| 6772 | Builder.AddTextChunk("\""); |
| 6773 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6774 | |
| 6775 | // #include_next <header> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6776 | Builder.AddTypedTextChunk("include_next"); |
| 6777 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6778 | Builder.AddTextChunk("<"); |
| 6779 | Builder.AddPlaceholderChunk("header"); |
| 6780 | Builder.AddTextChunk(">"); |
| 6781 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6782 | |
| 6783 | // #warning <message> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6784 | Builder.AddTypedTextChunk("warning"); |
| 6785 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6786 | Builder.AddPlaceholderChunk("message"); |
| 6787 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6788 | |
| 6789 | // Note: #ident and #sccs are such crazy anachronisms that we don't provide |
| 6790 | // completions for them. And __include_macros is a Clang-internal extension |
| 6791 | // that we don't want to encourage anyone to use. |
| 6792 | |
| 6793 | // FIXME: we don't support #assert or #unassert, so don't suggest them. |
| 6794 | Results.ExitScope(); |
| 6795 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6796 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 6797 | CodeCompletionContext::CCC_PreprocessorDirective, |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6798 | Results.data(), Results.size()); |
| 6799 | } |
| 6800 | |
| 6801 | void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) { |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6802 | CodeCompleteOrdinaryName(S, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6803 | S->getFnParent()? Sema::PCC_RecoveryInFunction |
| 6804 | : Sema::PCC_Namespace); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6805 | } |
| 6806 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6807 | void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6808 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 6809 | IsDefinition? CodeCompletionContext::CCC_MacroName |
| 6810 | : CodeCompletionContext::CCC_MacroNameUse); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 6811 | if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) { |
| 6812 | // Add just the names of macros, not their arguments. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6813 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 6814 | Results.EnterNewScope(); |
| 6815 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 6816 | MEnd = PP.macro_end(); |
| 6817 | M != MEnd; ++M) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6818 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6819 | M->first->getName())); |
| 6820 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 6821 | } |
| 6822 | Results.ExitScope(); |
| 6823 | } else if (IsDefinition) { |
| 6824 | // FIXME: Can we detect when the user just wrote an include guard above? |
| 6825 | } |
| 6826 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 6827 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 6828 | Results.data(), Results.size()); |
| 6829 | } |
| 6830 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6831 | void Sema::CodeCompletePreprocessorExpression() { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6832 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 6833 | CodeCompletionContext::CCC_PreprocessorExpression); |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6834 | |
| 6835 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
| 6836 | AddMacroResults(PP, Results); |
| 6837 | |
| 6838 | // defined (<macro>) |
| 6839 | Results.EnterNewScope(); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6840 | CodeCompletionBuilder Builder(Results.getAllocator()); |
| 6841 | Builder.AddTypedTextChunk("defined"); |
| 6842 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6843 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6844 | Builder.AddPlaceholderChunk("macro"); |
| 6845 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6846 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6847 | Results.ExitScope(); |
| 6848 | |
| 6849 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6850 | CodeCompletionContext::CCC_PreprocessorExpression, |
| 6851 | Results.data(), Results.size()); |
| 6852 | } |
| 6853 | |
| 6854 | void Sema::CodeCompletePreprocessorMacroArgument(Scope *S, |
| 6855 | IdentifierInfo *Macro, |
| 6856 | MacroInfo *MacroInfo, |
| 6857 | unsigned Argument) { |
| 6858 | // FIXME: In the future, we could provide "overload" results, much like we |
| 6859 | // do for function calls. |
| 6860 | |
Argyrios Kyrtzidis | 5c5f03e | 2011-08-18 19:41:28 +0000 | [diff] [blame] | 6861 | // Now just ignore this. There will be another code-completion callback |
| 6862 | // for the expanded tokens. |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6863 | } |
| 6864 | |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 6865 | void Sema::CodeCompleteNaturalLanguage() { |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 6866 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | af1c6b5 | 2010-08-25 17:10:00 +0000 | [diff] [blame] | 6867 | CodeCompletionContext::CCC_NaturalLanguage, |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 6868 | 0, 0); |
| 6869 | } |
| 6870 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6871 | void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6872 | SmallVectorImpl<CodeCompletionResult> &Results) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6873 | ResultBuilder Builder(*this, Allocator, CodeCompletionContext::CCC_Recovery); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 6874 | if (!CodeCompleter || CodeCompleter->includeGlobals()) { |
| 6875 | CodeCompletionDeclConsumer Consumer(Builder, |
| 6876 | Context.getTranslationUnitDecl()); |
| 6877 | LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName, |
| 6878 | Consumer); |
| 6879 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 6880 | |
| 6881 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
| 6882 | AddMacroResults(PP, Builder); |
| 6883 | |
| 6884 | Results.clear(); |
| 6885 | Results.insert(Results.end(), |
| 6886 | Builder.data(), Builder.data() + Builder.size()); |
| 6887 | } |