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 { |
| 64 | typedef llvm::SmallVector<DeclIndexPair, 4> DeclIndexPairVector; |
| 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) { |
| 437 | llvm::SmallVector<DeclContext *, 4> TargetParents; |
| 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: |
| 589 | case BuiltinType::UndeducedAuto: |
| 590 | return STC_Other; |
| 591 | |
| 592 | case BuiltinType::ObjCId: |
| 593 | case BuiltinType::ObjCClass: |
| 594 | case BuiltinType::ObjCSel: |
| 595 | return STC_ObjectiveC; |
| 596 | |
| 597 | default: |
| 598 | return STC_Arithmetic; |
| 599 | } |
| 600 | return STC_Other; |
| 601 | |
| 602 | case Type::Complex: |
| 603 | return STC_Arithmetic; |
| 604 | |
| 605 | case Type::Pointer: |
| 606 | return STC_Pointer; |
| 607 | |
| 608 | case Type::BlockPointer: |
| 609 | return STC_Block; |
| 610 | |
| 611 | case Type::LValueReference: |
| 612 | case Type::RValueReference: |
| 613 | return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType()); |
| 614 | |
| 615 | case Type::ConstantArray: |
| 616 | case Type::IncompleteArray: |
| 617 | case Type::VariableArray: |
| 618 | case Type::DependentSizedArray: |
| 619 | return STC_Array; |
| 620 | |
| 621 | case Type::DependentSizedExtVector: |
| 622 | case Type::Vector: |
| 623 | case Type::ExtVector: |
| 624 | return STC_Arithmetic; |
| 625 | |
| 626 | case Type::FunctionProto: |
| 627 | case Type::FunctionNoProto: |
| 628 | return STC_Function; |
| 629 | |
| 630 | case Type::Record: |
| 631 | return STC_Record; |
| 632 | |
| 633 | case Type::Enum: |
| 634 | return STC_Arithmetic; |
| 635 | |
| 636 | case Type::ObjCObject: |
| 637 | case Type::ObjCInterface: |
| 638 | case Type::ObjCObjectPointer: |
| 639 | return STC_ObjectiveC; |
| 640 | |
| 641 | default: |
| 642 | return STC_Other; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | /// \brief Get the type that a given expression will have if this declaration |
| 647 | /// is used as an expression in its "typical" code-completion form. |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 648 | QualType clang::getDeclUsageType(ASTContext &C, NamedDecl *ND) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 649 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 650 | |
| 651 | if (TypeDecl *Type = dyn_cast<TypeDecl>(ND)) |
| 652 | return C.getTypeDeclType(Type); |
| 653 | if (ObjCInterfaceDecl *Iface = dyn_cast<ObjCInterfaceDecl>(ND)) |
| 654 | return C.getObjCInterfaceType(Iface); |
| 655 | |
| 656 | QualType T; |
| 657 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 658 | T = Function->getCallResultType(); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 659 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 660 | T = Method->getSendResultType(); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 661 | else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 662 | T = FunTmpl->getTemplatedDecl()->getCallResultType(); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 663 | else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
| 664 | T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext())); |
| 665 | else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
| 666 | T = Property->getType(); |
| 667 | else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND)) |
| 668 | T = Value->getType(); |
| 669 | else |
| 670 | return QualType(); |
| 671 | |
| 672 | return T.getNonReferenceType(); |
| 673 | } |
| 674 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 675 | void ResultBuilder::AdjustResultPriorityForDecl(Result &R) { |
| 676 | // If this is an Objective-C method declaration whose selector matches our |
| 677 | // preferred selector, give it a priority boost. |
| 678 | if (!PreferredSelector.isNull()) |
| 679 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration)) |
| 680 | if (PreferredSelector == Method->getSelector()) |
| 681 | R.Priority += CCD_SelectorMatch; |
Douglas Gregor | 08f43cd | 2010-09-20 23:11:55 +0000 | [diff] [blame] | 682 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 683 | // If we have a preferred type, adjust the priority for results with exactly- |
| 684 | // matching or nearly-matching types. |
| 685 | if (!PreferredType.isNull()) { |
| 686 | QualType T = getDeclUsageType(SemaRef.Context, R.Declaration); |
| 687 | if (!T.isNull()) { |
| 688 | CanQualType TC = SemaRef.Context.getCanonicalType(T); |
| 689 | // Check for exactly-matching types (modulo qualifiers). |
| 690 | if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) |
| 691 | R.Priority /= CCF_ExactTypeMatch; |
| 692 | // Check for nearly-matching types, based on classification of each. |
| 693 | else if ((getSimplifiedTypeClass(PreferredType) |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 694 | == getSimplifiedTypeClass(TC)) && |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 695 | !(PreferredType->isEnumeralType() && TC->isEnumeralType())) |
| 696 | R.Priority /= CCF_SimilarTypeMatch; |
| 697 | } |
| 698 | } |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 701 | void ResultBuilder::MaybeAddConstructorResults(Result R) { |
| 702 | if (!SemaRef.getLangOptions().CPlusPlus || !R.Declaration || |
| 703 | !CompletionContext.wantConstructorResults()) |
| 704 | return; |
| 705 | |
| 706 | ASTContext &Context = SemaRef.Context; |
| 707 | NamedDecl *D = R.Declaration; |
| 708 | CXXRecordDecl *Record = 0; |
| 709 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) |
| 710 | Record = ClassTemplate->getTemplatedDecl(); |
| 711 | else if ((Record = dyn_cast<CXXRecordDecl>(D))) { |
| 712 | // Skip specializations and partial specializations. |
| 713 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 714 | return; |
| 715 | } else { |
| 716 | // There are no constructors here. |
| 717 | return; |
| 718 | } |
| 719 | |
| 720 | Record = Record->getDefinition(); |
| 721 | if (!Record) |
| 722 | return; |
| 723 | |
| 724 | |
| 725 | QualType RecordTy = Context.getTypeDeclType(Record); |
| 726 | DeclarationName ConstructorName |
| 727 | = Context.DeclarationNames.getCXXConstructorName( |
| 728 | Context.getCanonicalType(RecordTy)); |
| 729 | for (DeclContext::lookup_result Ctors = Record->lookup(ConstructorName); |
| 730 | Ctors.first != Ctors.second; ++Ctors.first) { |
| 731 | R.Declaration = *Ctors.first; |
| 732 | R.CursorKind = getCursorKindForDecl(R.Declaration); |
| 733 | Results.push_back(R); |
| 734 | } |
| 735 | } |
| 736 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 737 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { |
| 738 | assert(!ShadowMaps.empty() && "Must enter into a results scope"); |
| 739 | |
| 740 | if (R.Kind != Result::RK_Declaration) { |
| 741 | // For non-declaration results, just add the result. |
| 742 | Results.push_back(R); |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | // Look through using declarations. |
| 747 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
| 748 | MaybeAddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext); |
| 749 | return; |
| 750 | } |
| 751 | |
| 752 | Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
| 753 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 754 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 755 | bool AsNestedNameSpecifier = false; |
| 756 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 757 | return; |
| 758 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 759 | // C++ constructors are never found by name lookup. |
| 760 | if (isa<CXXConstructorDecl>(R.Declaration)) |
| 761 | return; |
| 762 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 763 | ShadowMap &SMap = ShadowMaps.back(); |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 764 | ShadowMapEntry::iterator I, IEnd; |
| 765 | ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName()); |
| 766 | if (NamePos != SMap.end()) { |
| 767 | I = NamePos->second.begin(); |
| 768 | IEnd = NamePos->second.end(); |
| 769 | } |
| 770 | |
| 771 | for (; I != IEnd; ++I) { |
| 772 | NamedDecl *ND = I->first; |
| 773 | unsigned Index = I->second; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 774 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 775 | // This is a redeclaration. Always pick the newer declaration. |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 776 | Results[Index].Declaration = R.Declaration; |
| 777 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 778 | // We're done. |
| 779 | return; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | // This is a new declaration in this scope. However, check whether this |
| 784 | // declaration name is hidden by a similarly-named declaration in an outer |
| 785 | // scope. |
| 786 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 787 | --SMEnd; |
| 788 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 789 | ShadowMapEntry::iterator I, IEnd; |
| 790 | ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName()); |
| 791 | if (NamePos != SM->end()) { |
| 792 | I = NamePos->second.begin(); |
| 793 | IEnd = NamePos->second.end(); |
| 794 | } |
| 795 | for (; I != IEnd; ++I) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 796 | // A tag declaration does not hide a non-tag declaration. |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 797 | if (I->first->hasTagIdentifierNamespace() && |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 798 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 799 | Decl::IDNS_ObjCProtocol))) |
| 800 | continue; |
| 801 | |
| 802 | // Protocols are in distinct namespaces from everything else. |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 803 | if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 804 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 805 | I->first->getIdentifierNamespace() != IDNS) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 806 | continue; |
| 807 | |
| 808 | // 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] | 809 | if (CheckHiddenResult(R, CurContext, I->first)) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 810 | return; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 811 | |
| 812 | break; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | // Make sure that any given declaration only shows up in the result set once. |
| 817 | if (!AllDeclsFound.insert(CanonDecl)) |
| 818 | return; |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 819 | |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 820 | // If the filter is for nested-name-specifiers, then this result starts a |
| 821 | // nested-name-specifier. |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 822 | if (AsNestedNameSpecifier) { |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 823 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 824 | R.Priority = CCP_NestedNameSpecifier; |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 825 | } else |
| 826 | AdjustResultPriorityForDecl(R); |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 827 | |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 828 | // If this result is supposed to have an informative qualifier, add one. |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 829 | if (R.QualifierIsInformative && !R.Qualifier && |
| 830 | !R.StartsNestedNameSpecifier) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 831 | DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 832 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 833 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace); |
| 834 | else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
| 835 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false, |
| 836 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
| 837 | else |
| 838 | R.QualifierIsInformative = false; |
| 839 | } |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 840 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 841 | // Insert this result into the set of results and into the current shadow |
| 842 | // map. |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 843 | SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 844 | Results.push_back(R); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 845 | |
| 846 | if (!AsNestedNameSpecifier) |
| 847 | MaybeAddConstructorResults(R); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 850 | void ResultBuilder::AddResult(Result R, DeclContext *CurContext, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 851 | NamedDecl *Hiding, bool InBaseClass = false) { |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 852 | if (R.Kind != Result::RK_Declaration) { |
| 853 | // For non-declaration results, just add the result. |
| 854 | Results.push_back(R); |
| 855 | return; |
| 856 | } |
| 857 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 858 | // Look through using declarations. |
| 859 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
| 860 | AddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext, Hiding); |
| 861 | return; |
| 862 | } |
| 863 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 864 | bool AsNestedNameSpecifier = false; |
| 865 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 866 | return; |
| 867 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 868 | // C++ constructors are never found by name lookup. |
| 869 | if (isa<CXXConstructorDecl>(R.Declaration)) |
| 870 | return; |
| 871 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 872 | if (Hiding && CheckHiddenResult(R, CurContext, Hiding)) |
| 873 | return; |
| 874 | |
| 875 | // Make sure that any given declaration only shows up in the result set once. |
| 876 | if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl())) |
| 877 | return; |
| 878 | |
| 879 | // If the filter is for nested-name-specifiers, then this result starts a |
| 880 | // nested-name-specifier. |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 881 | if (AsNestedNameSpecifier) { |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 882 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 883 | R.Priority = CCP_NestedNameSpecifier; |
| 884 | } |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 885 | else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass && |
| 886 | isa<CXXRecordDecl>(R.Declaration->getDeclContext() |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 887 | ->getRedeclContext())) |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 888 | R.QualifierIsInformative = true; |
| 889 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 890 | // If this result is supposed to have an informative qualifier, add one. |
| 891 | if (R.QualifierIsInformative && !R.Qualifier && |
| 892 | !R.StartsNestedNameSpecifier) { |
| 893 | DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 894 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 895 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace); |
| 896 | else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
| 897 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false, |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 898 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 899 | else |
| 900 | R.QualifierIsInformative = false; |
| 901 | } |
| 902 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 903 | // Adjust the priority if this result comes from a base class. |
| 904 | if (InBaseClass) |
| 905 | R.Priority += CCD_InBaseClass; |
| 906 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 907 | AdjustResultPriorityForDecl(R); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 908 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 909 | if (HasObjectTypeQualifiers) |
| 910 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(R.Declaration)) |
| 911 | if (Method->isInstance()) { |
| 912 | Qualifiers MethodQuals |
| 913 | = Qualifiers::fromCVRMask(Method->getTypeQualifiers()); |
| 914 | if (ObjectTypeQualifiers == MethodQuals) |
| 915 | R.Priority += CCD_ObjectQualifierMatch; |
| 916 | else if (ObjectTypeQualifiers - MethodQuals) { |
| 917 | // The method cannot be invoked, because doing so would drop |
| 918 | // qualifiers. |
| 919 | return; |
| 920 | } |
| 921 | } |
| 922 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 923 | // Insert this result into the set of results. |
| 924 | Results.push_back(R); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 925 | |
| 926 | if (!AsNestedNameSpecifier) |
| 927 | MaybeAddConstructorResults(R); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 930 | void ResultBuilder::AddResult(Result R) { |
| 931 | assert(R.Kind != Result::RK_Declaration && |
| 932 | "Declaration results need more context"); |
| 933 | Results.push_back(R); |
| 934 | } |
| 935 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 936 | /// \brief Enter into a new scope. |
| 937 | void ResultBuilder::EnterNewScope() { |
| 938 | ShadowMaps.push_back(ShadowMap()); |
| 939 | } |
| 940 | |
| 941 | /// \brief Exit from the current scope. |
| 942 | void ResultBuilder::ExitScope() { |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 943 | for (ShadowMap::iterator E = ShadowMaps.back().begin(), |
| 944 | EEnd = ShadowMaps.back().end(); |
| 945 | E != EEnd; |
| 946 | ++E) |
| 947 | E->second.Destroy(); |
| 948 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 949 | ShadowMaps.pop_back(); |
| 950 | } |
| 951 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 952 | /// \brief Determines whether this given declaration will be found by |
| 953 | /// ordinary name lookup. |
| 954 | bool ResultBuilder::IsOrdinaryName(NamedDecl *ND) const { |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 955 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 956 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 957 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 958 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 9b30b26 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 959 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 960 | else if (SemaRef.getLangOptions().ObjC1) { |
| 961 | if (isa<ObjCIvarDecl>(ND)) |
| 962 | return true; |
| 963 | if (isa<ObjCPropertyDecl>(ND) && |
| 964 | SemaRef.canSynthesizeProvisionalIvar(cast<ObjCPropertyDecl>(ND))) |
| 965 | return true; |
| 966 | } |
| 967 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 968 | return ND->getIdentifierNamespace() & IDNS; |
| 969 | } |
| 970 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 971 | /// \brief Determines whether this given declaration will be found by |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 972 | /// ordinary name lookup but is not a type name. |
| 973 | bool ResultBuilder::IsOrdinaryNonTypeName(NamedDecl *ND) const { |
| 974 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 975 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) |
| 976 | return false; |
| 977 | |
| 978 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 979 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 9b30b26 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 980 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 981 | else if (SemaRef.getLangOptions().ObjC1) { |
| 982 | if (isa<ObjCIvarDecl>(ND)) |
| 983 | return true; |
| 984 | if (isa<ObjCPropertyDecl>(ND) && |
| 985 | SemaRef.canSynthesizeProvisionalIvar(cast<ObjCPropertyDecl>(ND))) |
| 986 | return true; |
| 987 | } |
| 988 | |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 989 | return ND->getIdentifierNamespace() & IDNS; |
| 990 | } |
| 991 | |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 992 | bool ResultBuilder::IsIntegralConstantValue(NamedDecl *ND) const { |
| 993 | if (!IsOrdinaryNonTypeName(ND)) |
| 994 | return 0; |
| 995 | |
| 996 | if (ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl())) |
| 997 | if (VD->getType()->isIntegralOrEnumerationType()) |
| 998 | return true; |
| 999 | |
| 1000 | return false; |
| 1001 | } |
| 1002 | |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1003 | /// \brief Determines whether this given declaration will be found by |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1004 | /// ordinary name lookup. |
| 1005 | bool ResultBuilder::IsOrdinaryNonValueName(NamedDecl *ND) const { |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1006 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 1007 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1008 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 1009 | if (SemaRef.getLangOptions().CPlusPlus) |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1010 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1011 | |
| 1012 | return (ND->getIdentifierNamespace() & IDNS) && |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1013 | !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) && |
| 1014 | !isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1017 | /// \brief Determines whether the given declaration is suitable as the |
| 1018 | /// start of a C++ nested-name-specifier, e.g., a class or namespace. |
| 1019 | bool ResultBuilder::IsNestedNameSpecifier(NamedDecl *ND) const { |
| 1020 | // Allow us to find class templates, too. |
| 1021 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 1022 | ND = ClassTemplate->getTemplatedDecl(); |
| 1023 | |
| 1024 | return SemaRef.isAcceptableNestedNameSpecifier(ND); |
| 1025 | } |
| 1026 | |
| 1027 | /// \brief Determines whether the given declaration is an enumeration. |
| 1028 | bool ResultBuilder::IsEnum(NamedDecl *ND) const { |
| 1029 | return isa<EnumDecl>(ND); |
| 1030 | } |
| 1031 | |
| 1032 | /// \brief Determines whether the given declaration is a class or struct. |
| 1033 | bool ResultBuilder::IsClassOrStruct(NamedDecl *ND) const { |
| 1034 | // Allow us to find class templates, too. |
| 1035 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 1036 | ND = ClassTemplate->getTemplatedDecl(); |
| 1037 | |
| 1038 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1039 | return RD->getTagKind() == TTK_Class || |
| 1040 | RD->getTagKind() == TTK_Struct; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1041 | |
| 1042 | return false; |
| 1043 | } |
| 1044 | |
| 1045 | /// \brief Determines whether the given declaration is a union. |
| 1046 | bool ResultBuilder::IsUnion(NamedDecl *ND) const { |
| 1047 | // Allow us to find class templates, too. |
| 1048 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 1049 | ND = ClassTemplate->getTemplatedDecl(); |
| 1050 | |
| 1051 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1052 | return RD->getTagKind() == TTK_Union; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1053 | |
| 1054 | return false; |
| 1055 | } |
| 1056 | |
| 1057 | /// \brief Determines whether the given declaration is a namespace. |
| 1058 | bool ResultBuilder::IsNamespace(NamedDecl *ND) const { |
| 1059 | return isa<NamespaceDecl>(ND); |
| 1060 | } |
| 1061 | |
| 1062 | /// \brief Determines whether the given declaration is a namespace or |
| 1063 | /// namespace alias. |
| 1064 | bool ResultBuilder::IsNamespaceOrAlias(NamedDecl *ND) const { |
| 1065 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
| 1066 | } |
| 1067 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1068 | /// \brief Determines whether the given declaration is a type. |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1069 | bool ResultBuilder::IsType(NamedDecl *ND) const { |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1070 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND)) |
| 1071 | ND = Using->getTargetDecl(); |
| 1072 | |
| 1073 | return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1076 | /// \brief Determines which members of a class should be visible via |
| 1077 | /// "." or "->". Only value declarations, nested name specifiers, and |
| 1078 | /// using declarations thereof should show up. |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1079 | bool ResultBuilder::IsMember(NamedDecl *ND) const { |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1080 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND)) |
| 1081 | ND = Using->getTargetDecl(); |
| 1082 | |
Douglas Gregor | ce82196 | 2009-12-11 18:14:22 +0000 | [diff] [blame] | 1083 | return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) || |
| 1084 | isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1087 | static bool isObjCReceiverType(ASTContext &C, QualType T) { |
| 1088 | T = C.getCanonicalType(T); |
| 1089 | switch (T->getTypeClass()) { |
| 1090 | case Type::ObjCObject: |
| 1091 | case Type::ObjCInterface: |
| 1092 | case Type::ObjCObjectPointer: |
| 1093 | return true; |
| 1094 | |
| 1095 | case Type::Builtin: |
| 1096 | switch (cast<BuiltinType>(T)->getKind()) { |
| 1097 | case BuiltinType::ObjCId: |
| 1098 | case BuiltinType::ObjCClass: |
| 1099 | case BuiltinType::ObjCSel: |
| 1100 | return true; |
| 1101 | |
| 1102 | default: |
| 1103 | break; |
| 1104 | } |
| 1105 | return false; |
| 1106 | |
| 1107 | default: |
| 1108 | break; |
| 1109 | } |
| 1110 | |
| 1111 | if (!C.getLangOptions().CPlusPlus) |
| 1112 | return false; |
| 1113 | |
| 1114 | // FIXME: We could perform more analysis here to determine whether a |
| 1115 | // particular class type has any conversions to Objective-C types. For now, |
| 1116 | // just accept all class types. |
| 1117 | return T->isDependentType() || T->isRecordType(); |
| 1118 | } |
| 1119 | |
| 1120 | bool ResultBuilder::IsObjCMessageReceiver(NamedDecl *ND) const { |
| 1121 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1122 | if (T.isNull()) |
| 1123 | return false; |
| 1124 | |
| 1125 | T = SemaRef.Context.getBaseElementType(T); |
| 1126 | return isObjCReceiverType(SemaRef.Context, T); |
| 1127 | } |
| 1128 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1129 | bool ResultBuilder::IsObjCCollection(NamedDecl *ND) const { |
| 1130 | if ((SemaRef.getLangOptions().CPlusPlus && !IsOrdinaryName(ND)) || |
| 1131 | (!SemaRef.getLangOptions().CPlusPlus && !IsOrdinaryNonTypeName(ND))) |
| 1132 | return false; |
| 1133 | |
| 1134 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1135 | if (T.isNull()) |
| 1136 | return false; |
| 1137 | |
| 1138 | T = SemaRef.Context.getBaseElementType(T); |
| 1139 | return T->isObjCObjectType() || T->isObjCObjectPointerType() || |
| 1140 | T->isObjCIdType() || |
| 1141 | (SemaRef.getLangOptions().CPlusPlus && T->isRecordType()); |
| 1142 | } |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1143 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1144 | bool ResultBuilder::IsImpossibleToSatisfy(NamedDecl *ND) const { |
| 1145 | return false; |
| 1146 | } |
| 1147 | |
Douglas Gregor | 80f4f4c | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1148 | /// \rief Determines whether the given declaration is an Objective-C |
| 1149 | /// instance variable. |
| 1150 | bool ResultBuilder::IsObjCIvar(NamedDecl *ND) const { |
| 1151 | return isa<ObjCIvarDecl>(ND); |
| 1152 | } |
| 1153 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1154 | namespace { |
| 1155 | /// \brief Visible declaration consumer that adds a code-completion result |
| 1156 | /// for each visible declaration. |
| 1157 | class CodeCompletionDeclConsumer : public VisibleDeclConsumer { |
| 1158 | ResultBuilder &Results; |
| 1159 | DeclContext *CurContext; |
| 1160 | |
| 1161 | public: |
| 1162 | CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext) |
| 1163 | : Results(Results), CurContext(CurContext) { } |
| 1164 | |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 1165 | virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, bool InBaseClass) { |
| 1166 | Results.AddResult(ND, CurContext, Hiding, InBaseClass); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1167 | } |
| 1168 | }; |
| 1169 | } |
| 1170 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1171 | /// \brief Add type specifiers for the current language as keyword results. |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1172 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1173 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1174 | typedef CodeCompletionResult Result; |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1175 | Results.AddResult(Result("short", CCP_Type)); |
| 1176 | Results.AddResult(Result("long", CCP_Type)); |
| 1177 | Results.AddResult(Result("signed", CCP_Type)); |
| 1178 | Results.AddResult(Result("unsigned", CCP_Type)); |
| 1179 | Results.AddResult(Result("void", CCP_Type)); |
| 1180 | Results.AddResult(Result("char", CCP_Type)); |
| 1181 | Results.AddResult(Result("int", CCP_Type)); |
| 1182 | Results.AddResult(Result("float", CCP_Type)); |
| 1183 | Results.AddResult(Result("double", CCP_Type)); |
| 1184 | Results.AddResult(Result("enum", CCP_Type)); |
| 1185 | Results.AddResult(Result("struct", CCP_Type)); |
| 1186 | Results.AddResult(Result("union", CCP_Type)); |
| 1187 | Results.AddResult(Result("const", CCP_Type)); |
| 1188 | Results.AddResult(Result("volatile", CCP_Type)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1189 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1190 | if (LangOpts.C99) { |
| 1191 | // C99-specific |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1192 | Results.AddResult(Result("_Complex", CCP_Type)); |
| 1193 | Results.AddResult(Result("_Imaginary", CCP_Type)); |
| 1194 | Results.AddResult(Result("_Bool", CCP_Type)); |
| 1195 | Results.AddResult(Result("restrict", CCP_Type)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1198 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1199 | if (LangOpts.CPlusPlus) { |
| 1200 | // C++-specific |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 1201 | Results.AddResult(Result("bool", CCP_Type + |
| 1202 | (LangOpts.ObjC1? CCD_bool_in_ObjC : 0))); |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1203 | Results.AddResult(Result("class", CCP_Type)); |
| 1204 | Results.AddResult(Result("wchar_t", CCP_Type)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1205 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1206 | // typename qualified-id |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1207 | Builder.AddTypedTextChunk("typename"); |
| 1208 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1209 | Builder.AddPlaceholderChunk("qualifier"); |
| 1210 | Builder.AddTextChunk("::"); |
| 1211 | Builder.AddPlaceholderChunk("name"); |
| 1212 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1213 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1214 | if (LangOpts.CPlusPlus0x) { |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1215 | Results.AddResult(Result("auto", CCP_Type)); |
| 1216 | Results.AddResult(Result("char16_t", CCP_Type)); |
| 1217 | Results.AddResult(Result("char32_t", CCP_Type)); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1218 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1219 | Builder.AddTypedTextChunk("decltype"); |
| 1220 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1221 | Builder.AddPlaceholderChunk("expression"); |
| 1222 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1223 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | // GNU extensions |
| 1228 | if (LangOpts.GNUMode) { |
| 1229 | // FIXME: Enable when we actually support decimal floating point. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1230 | // Results.AddResult(Result("_Decimal32")); |
| 1231 | // Results.AddResult(Result("_Decimal64")); |
| 1232 | // Results.AddResult(Result("_Decimal128")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1233 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1234 | Builder.AddTypedTextChunk("typeof"); |
| 1235 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1236 | Builder.AddPlaceholderChunk("expression"); |
| 1237 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1238 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1239 | Builder.AddTypedTextChunk("typeof"); |
| 1240 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1241 | Builder.AddPlaceholderChunk("type"); |
| 1242 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1243 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1247 | static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1248 | const LangOptions &LangOpts, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1249 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1250 | typedef CodeCompletionResult Result; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1251 | // Note: we don't suggest either "auto" or "register", because both |
| 1252 | // are pointless as storage specifiers. Elsewhere, we suggest "auto" |
| 1253 | // in C++0x as a type specifier. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1254 | Results.AddResult(Result("extern")); |
| 1255 | Results.AddResult(Result("static")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1258 | static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1259 | const LangOptions &LangOpts, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1260 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1261 | typedef CodeCompletionResult Result; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1262 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1263 | case Sema::PCC_Class: |
| 1264 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1265 | if (LangOpts.CPlusPlus) { |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1266 | Results.AddResult(Result("explicit")); |
| 1267 | Results.AddResult(Result("friend")); |
| 1268 | Results.AddResult(Result("mutable")); |
| 1269 | Results.AddResult(Result("virtual")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1270 | } |
| 1271 | // Fall through |
| 1272 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1273 | case Sema::PCC_ObjCInterface: |
| 1274 | case Sema::PCC_ObjCImplementation: |
| 1275 | case Sema::PCC_Namespace: |
| 1276 | case Sema::PCC_Template: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1277 | if (LangOpts.CPlusPlus || LangOpts.C99) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1278 | Results.AddResult(Result("inline")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1279 | break; |
| 1280 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1281 | case Sema::PCC_ObjCInstanceVariableList: |
| 1282 | case Sema::PCC_Expression: |
| 1283 | case Sema::PCC_Statement: |
| 1284 | case Sema::PCC_ForInit: |
| 1285 | case Sema::PCC_Condition: |
| 1286 | case Sema::PCC_RecoveryInFunction: |
| 1287 | case Sema::PCC_Type: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1288 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1289 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1290 | break; |
| 1291 | } |
| 1292 | } |
| 1293 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1294 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); |
| 1295 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); |
| 1296 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1297 | ResultBuilder &Results, |
| 1298 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1299 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1300 | ResultBuilder &Results, |
| 1301 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1302 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1303 | ResultBuilder &Results, |
| 1304 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1305 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1306 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1307 | static void AddTypedefResult(ResultBuilder &Results) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1308 | CodeCompletionBuilder Builder(Results.getAllocator()); |
| 1309 | Builder.AddTypedTextChunk("typedef"); |
| 1310 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1311 | Builder.AddPlaceholderChunk("type"); |
| 1312 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1313 | Builder.AddPlaceholderChunk("name"); |
| 1314 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1317 | static bool WantTypesInContext(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1318 | const LangOptions &LangOpts) { |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1319 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1320 | case Sema::PCC_Namespace: |
| 1321 | case Sema::PCC_Class: |
| 1322 | case Sema::PCC_ObjCInstanceVariableList: |
| 1323 | case Sema::PCC_Template: |
| 1324 | case Sema::PCC_MemberTemplate: |
| 1325 | case Sema::PCC_Statement: |
| 1326 | case Sema::PCC_RecoveryInFunction: |
| 1327 | case Sema::PCC_Type: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1328 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1329 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1330 | return true; |
| 1331 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1332 | case Sema::PCC_Expression: |
| 1333 | case Sema::PCC_Condition: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1334 | return LangOpts.CPlusPlus; |
| 1335 | |
| 1336 | case Sema::PCC_ObjCInterface: |
| 1337 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1338 | return false; |
| 1339 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1340 | case Sema::PCC_ForInit: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1341 | return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99; |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | return false; |
| 1345 | } |
| 1346 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1347 | /// \brief Add language constructs that show up for "ordinary" names. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1348 | static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1349 | Scope *S, |
| 1350 | Sema &SemaRef, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1351 | ResultBuilder &Results) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1352 | CodeCompletionBuilder Builder(Results.getAllocator()); |
| 1353 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1354 | typedef CodeCompletionResult Result; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1355 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1356 | case Sema::PCC_Namespace: |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1357 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1358 | if (Results.includeCodePatterns()) { |
| 1359 | // namespace <identifier> { declarations } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1360 | Builder.AddTypedTextChunk("namespace"); |
| 1361 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1362 | Builder.AddPlaceholderChunk("identifier"); |
| 1363 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1364 | Builder.AddPlaceholderChunk("declarations"); |
| 1365 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1366 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1367 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1370 | // namespace identifier = identifier ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1371 | Builder.AddTypedTextChunk("namespace"); |
| 1372 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1373 | Builder.AddPlaceholderChunk("name"); |
| 1374 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
| 1375 | Builder.AddPlaceholderChunk("namespace"); |
| 1376 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1377 | |
| 1378 | // Using directives |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1379 | Builder.AddTypedTextChunk("using"); |
| 1380 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1381 | Builder.AddTextChunk("namespace"); |
| 1382 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1383 | Builder.AddPlaceholderChunk("identifier"); |
| 1384 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1385 | |
| 1386 | // asm(string-literal) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1387 | Builder.AddTypedTextChunk("asm"); |
| 1388 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1389 | Builder.AddPlaceholderChunk("string-literal"); |
| 1390 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1391 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1392 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1393 | if (Results.includeCodePatterns()) { |
| 1394 | // Explicit template instantiation |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1395 | Builder.AddTypedTextChunk("template"); |
| 1396 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1397 | Builder.AddPlaceholderChunk("declaration"); |
| 1398 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1399 | } |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1400 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1401 | |
| 1402 | if (SemaRef.getLangOptions().ObjC1) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1403 | AddObjCTopLevelResults(Results, true); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1404 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1405 | AddTypedefResult(Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1406 | // Fall through |
| 1407 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1408 | case Sema::PCC_Class: |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1409 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1410 | // Using declaration |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1411 | Builder.AddTypedTextChunk("using"); |
| 1412 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1413 | Builder.AddPlaceholderChunk("qualifier"); |
| 1414 | Builder.AddTextChunk("::"); |
| 1415 | Builder.AddPlaceholderChunk("name"); |
| 1416 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1417 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1418 | // using typename qualifier::name (only in a dependent context) |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1419 | if (SemaRef.CurContext->isDependentContext()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1420 | Builder.AddTypedTextChunk("using"); |
| 1421 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1422 | Builder.AddTextChunk("typename"); |
| 1423 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1424 | Builder.AddPlaceholderChunk("qualifier"); |
| 1425 | Builder.AddTextChunk("::"); |
| 1426 | Builder.AddPlaceholderChunk("name"); |
| 1427 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1430 | if (CCC == Sema::PCC_Class) { |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1431 | AddTypedefResult(Results); |
| 1432 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1433 | // public: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1434 | Builder.AddTypedTextChunk("public"); |
| 1435 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1436 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1437 | |
| 1438 | // protected: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1439 | Builder.AddTypedTextChunk("protected"); |
| 1440 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1441 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1442 | |
| 1443 | // private: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1444 | Builder.AddTypedTextChunk("private"); |
| 1445 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1446 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1447 | } |
| 1448 | } |
| 1449 | // Fall through |
| 1450 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1451 | case Sema::PCC_Template: |
| 1452 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1453 | if (SemaRef.getLangOptions().CPlusPlus && Results.includeCodePatterns()) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1454 | // template < parameters > |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1455 | Builder.AddTypedTextChunk("template"); |
| 1456 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1457 | Builder.AddPlaceholderChunk("parameters"); |
| 1458 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1459 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1462 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 1463 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1464 | break; |
| 1465 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1466 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1467 | AddObjCInterfaceResults(SemaRef.getLangOptions(), Results, true); |
| 1468 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 1469 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1470 | break; |
| 1471 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1472 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1473 | AddObjCImplementationResults(SemaRef.getLangOptions(), Results, true); |
| 1474 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 1475 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1476 | break; |
| 1477 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1478 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1479 | AddObjCVisibilityResults(SemaRef.getLangOptions(), Results, true); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1480 | break; |
| 1481 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1482 | case Sema::PCC_RecoveryInFunction: |
| 1483 | case Sema::PCC_Statement: { |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1484 | AddTypedefResult(Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1485 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1486 | if (SemaRef.getLangOptions().CPlusPlus && Results.includeCodePatterns()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1487 | Builder.AddTypedTextChunk("try"); |
| 1488 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1489 | Builder.AddPlaceholderChunk("statements"); |
| 1490 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1491 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1492 | Builder.AddTextChunk("catch"); |
| 1493 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1494 | Builder.AddPlaceholderChunk("declaration"); |
| 1495 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1496 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1497 | Builder.AddPlaceholderChunk("statements"); |
| 1498 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1499 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1500 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1501 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1502 | if (SemaRef.getLangOptions().ObjC1) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1503 | AddObjCStatementResults(Results, true); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1504 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1505 | if (Results.includeCodePatterns()) { |
| 1506 | // if (condition) { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1507 | Builder.AddTypedTextChunk("if"); |
| 1508 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1509 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1510 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1511 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1512 | Builder.AddPlaceholderChunk("expression"); |
| 1513 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1514 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1515 | Builder.AddPlaceholderChunk("statements"); |
| 1516 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1517 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1518 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1519 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1520 | // switch (condition) { } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1521 | Builder.AddTypedTextChunk("switch"); |
| 1522 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1523 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1524 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1525 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1526 | Builder.AddPlaceholderChunk("expression"); |
| 1527 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1528 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1529 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1530 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1531 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1534 | // Switch-specific statements. |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 1535 | if (!SemaRef.getCurFunction()->SwitchStack.empty()) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1536 | // case expression: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1537 | Builder.AddTypedTextChunk("case"); |
| 1538 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1539 | Builder.AddPlaceholderChunk("expression"); |
| 1540 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1541 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1542 | |
| 1543 | // default: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1544 | Builder.AddTypedTextChunk("default"); |
| 1545 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1546 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1549 | if (Results.includeCodePatterns()) { |
| 1550 | /// while (condition) { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1551 | Builder.AddTypedTextChunk("while"); |
| 1552 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1553 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1554 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1555 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1556 | Builder.AddPlaceholderChunk("expression"); |
| 1557 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1558 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1559 | Builder.AddPlaceholderChunk("statements"); |
| 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 | // do { statements } while ( expression ); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1565 | Builder.AddTypedTextChunk("do"); |
| 1566 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1567 | Builder.AddPlaceholderChunk("statements"); |
| 1568 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1569 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1570 | Builder.AddTextChunk("while"); |
| 1571 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1572 | Builder.AddPlaceholderChunk("expression"); |
| 1573 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1574 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1575 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1576 | // for ( for-init-statement ; condition ; expression ) { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1577 | Builder.AddTypedTextChunk("for"); |
| 1578 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1579 | if (SemaRef.getLangOptions().CPlusPlus || SemaRef.getLangOptions().C99) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1580 | Builder.AddPlaceholderChunk("init-statement"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1581 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1582 | Builder.AddPlaceholderChunk("init-expression"); |
| 1583 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1584 | Builder.AddPlaceholderChunk("condition"); |
| 1585 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1586 | Builder.AddPlaceholderChunk("inc-expression"); |
| 1587 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1588 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1589 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 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 | } |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1595 | |
| 1596 | if (S->getContinueParent()) { |
| 1597 | // continue ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1598 | Builder.AddTypedTextChunk("continue"); |
| 1599 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | if (S->getBreakParent()) { |
| 1603 | // break ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1604 | Builder.AddTypedTextChunk("break"); |
| 1605 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | // "return expression ;" or "return ;", depending on whether we |
| 1609 | // know the function is void or not. |
| 1610 | bool isVoid = false; |
| 1611 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) |
| 1612 | isVoid = Function->getResultType()->isVoidType(); |
| 1613 | else if (ObjCMethodDecl *Method |
| 1614 | = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) |
| 1615 | isVoid = Method->getResultType()->isVoidType(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1616 | else if (SemaRef.getCurBlock() && |
| 1617 | !SemaRef.getCurBlock()->ReturnType.isNull()) |
| 1618 | isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType(); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1619 | Builder.AddTypedTextChunk("return"); |
Douglas Gregor | 9329800 | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1620 | if (!isVoid) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1621 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1622 | Builder.AddPlaceholderChunk("expression"); |
Douglas Gregor | 9329800 | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1623 | } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1624 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1625 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1626 | // goto identifier ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1627 | Builder.AddTypedTextChunk("goto"); |
| 1628 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1629 | Builder.AddPlaceholderChunk("label"); |
| 1630 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1631 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1632 | // Using directives |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1633 | Builder.AddTypedTextChunk("using"); |
| 1634 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1635 | Builder.AddTextChunk("namespace"); |
| 1636 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1637 | Builder.AddPlaceholderChunk("identifier"); |
| 1638 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | // Fall through (for statement expressions). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1642 | case Sema::PCC_ForInit: |
| 1643 | case Sema::PCC_Condition: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1644 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1645 | // Fall through: conditions and statements can have expressions. |
| 1646 | |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1647 | case Sema::PCC_ParenthesizedExpression: |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1648 | case Sema::PCC_Expression: { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1649 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 1650 | // 'this', if we're in a non-static member function. |
| 1651 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) |
| 1652 | if (!Method->isStatic()) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1653 | Results.AddResult(Result("this")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1654 | |
| 1655 | // true, false |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1656 | Results.AddResult(Result("true")); |
| 1657 | Results.AddResult(Result("false")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1658 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1659 | // dynamic_cast < type-id > ( expression ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1660 | Builder.AddTypedTextChunk("dynamic_cast"); |
| 1661 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1662 | Builder.AddPlaceholderChunk("type"); |
| 1663 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1664 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1665 | Builder.AddPlaceholderChunk("expression"); |
| 1666 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1667 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1668 | |
| 1669 | // static_cast < type-id > ( expression ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1670 | Builder.AddTypedTextChunk("static_cast"); |
| 1671 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1672 | Builder.AddPlaceholderChunk("type"); |
| 1673 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1674 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1675 | Builder.AddPlaceholderChunk("expression"); |
| 1676 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1677 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1678 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1679 | // reinterpret_cast < type-id > ( expression ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1680 | Builder.AddTypedTextChunk("reinterpret_cast"); |
| 1681 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1682 | Builder.AddPlaceholderChunk("type"); |
| 1683 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1684 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1685 | Builder.AddPlaceholderChunk("expression"); |
| 1686 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1687 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1688 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1689 | // const_cast < type-id > ( expression ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1690 | Builder.AddTypedTextChunk("const_cast"); |
| 1691 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1692 | Builder.AddPlaceholderChunk("type"); |
| 1693 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1694 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1695 | Builder.AddPlaceholderChunk("expression"); |
| 1696 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1697 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1698 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1699 | // typeid ( expression-or-type ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1700 | Builder.AddTypedTextChunk("typeid"); |
| 1701 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1702 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 1703 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1704 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1705 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1706 | // new T ( ... ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1707 | Builder.AddTypedTextChunk("new"); |
| 1708 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1709 | Builder.AddPlaceholderChunk("type"); |
| 1710 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1711 | Builder.AddPlaceholderChunk("expressions"); |
| 1712 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1713 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1714 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1715 | // new T [ ] ( ... ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1716 | Builder.AddTypedTextChunk("new"); |
| 1717 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1718 | Builder.AddPlaceholderChunk("type"); |
| 1719 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1720 | Builder.AddPlaceholderChunk("size"); |
| 1721 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 1722 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1723 | Builder.AddPlaceholderChunk("expressions"); |
| 1724 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1725 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1726 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1727 | // delete expression |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1728 | Builder.AddTypedTextChunk("delete"); |
| 1729 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1730 | Builder.AddPlaceholderChunk("expression"); |
| 1731 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1732 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1733 | // delete [] expression |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1734 | Builder.AddTypedTextChunk("delete"); |
| 1735 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1736 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1737 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 1738 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1739 | Builder.AddPlaceholderChunk("expression"); |
| 1740 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1741 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1742 | // throw expression |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1743 | Builder.AddTypedTextChunk("throw"); |
| 1744 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1745 | Builder.AddPlaceholderChunk("expression"); |
| 1746 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1747 | |
| 1748 | // FIXME: Rethrow? |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | if (SemaRef.getLangOptions().ObjC1) { |
| 1752 | // 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] | 1753 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
| 1754 | // The interface can be NULL. |
| 1755 | if (ObjCInterfaceDecl *ID = Method->getClassInterface()) |
| 1756 | if (ID->getSuperClass()) |
| 1757 | Results.AddResult(Result("super")); |
| 1758 | } |
| 1759 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1760 | AddObjCExpressionResults(Results, true); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1763 | // sizeof expression |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1764 | Builder.AddTypedTextChunk("sizeof"); |
| 1765 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1766 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 1767 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1768 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1769 | break; |
| 1770 | } |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1771 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1772 | case Sema::PCC_Type: |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1773 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1774 | break; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1777 | if (WantTypesInContext(CCC, SemaRef.getLangOptions())) |
| 1778 | AddTypeSpecifierResults(SemaRef.getLangOptions(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1779 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1780 | if (SemaRef.getLangOptions().CPlusPlus && CCC != Sema::PCC_Type) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1781 | Results.AddResult(Result("operator")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 1784 | /// \brief Retrieve the string representation of the given type as a string |
| 1785 | /// that has the appropriate lifetime for code completion. |
| 1786 | /// |
| 1787 | /// This routine provides a fast path where we provide constant strings for |
| 1788 | /// common type names. |
| 1789 | const char *GetCompletionTypeString(QualType T, |
| 1790 | ASTContext &Context, |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 1791 | CodeCompletionAllocator &Allocator) { |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 1792 | PrintingPolicy Policy(Context.PrintingPolicy); |
| 1793 | Policy.AnonymousTagLocations = false; |
| 1794 | |
| 1795 | if (!T.getLocalQualifiers()) { |
| 1796 | // Built-in type names are constant strings. |
| 1797 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) |
| 1798 | return BT->getName(Context.getLangOptions()); |
| 1799 | |
| 1800 | // Anonymous tag types are constant strings. |
| 1801 | if (const TagType *TagT = dyn_cast<TagType>(T)) |
| 1802 | if (TagDecl *Tag = TagT->getDecl()) |
| 1803 | if (!Tag->getIdentifier() && !Tag->getTypedefForAnonDecl()) { |
| 1804 | switch (Tag->getTagKind()) { |
| 1805 | case TTK_Struct: return "struct <anonymous>"; |
| 1806 | case TTK_Class: return "class <anonymous>"; |
| 1807 | case TTK_Union: return "union <anonymous>"; |
| 1808 | case TTK_Enum: return "enum <anonymous>"; |
| 1809 | } |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | // Slow path: format the type as a string. |
| 1814 | std::string Result; |
| 1815 | T.getAsStringInternal(Result, Policy); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 1816 | return Allocator.CopyString(Result); |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1819 | /// \brief If the given declaration has an associated type, add it as a result |
| 1820 | /// type chunk. |
| 1821 | static void AddResultTypeChunk(ASTContext &Context, |
| 1822 | NamedDecl *ND, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1823 | CodeCompletionBuilder &Result) { |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1824 | if (!ND) |
| 1825 | return; |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1826 | |
| 1827 | // Skip constructors and conversion functions, which have their return types |
| 1828 | // built into their names. |
| 1829 | if (isa<CXXConstructorDecl>(ND) || isa<CXXConversionDecl>(ND)) |
| 1830 | return; |
| 1831 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1832 | // Determine the type of the declaration (if it has a type). |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1833 | QualType T; |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1834 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) |
| 1835 | T = Function->getResultType(); |
| 1836 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
| 1837 | T = Method->getResultType(); |
| 1838 | else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) |
| 1839 | T = FunTmpl->getTemplatedDecl()->getResultType(); |
| 1840 | else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
| 1841 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); |
| 1842 | else if (isa<UnresolvedUsingValueDecl>(ND)) { |
| 1843 | /* Do nothing: ignore unresolved using declarations*/ |
| 1844 | } else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND)) |
| 1845 | T = Value->getType(); |
| 1846 | else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
| 1847 | T = Property->getType(); |
| 1848 | |
| 1849 | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) |
| 1850 | return; |
| 1851 | |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 1852 | Result.AddResultTypeChunk(GetCompletionTypeString(T, Context, |
| 1853 | Result.getAllocator())); |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1856 | static void MaybeAddSentinel(ASTContext &Context, NamedDecl *FunctionOrMethod, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1857 | CodeCompletionBuilder &Result) { |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1858 | if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) |
| 1859 | if (Sentinel->getSentinel() == 0) { |
| 1860 | if (Context.getLangOptions().ObjC1 && |
| 1861 | Context.Idents.get("nil").hasMacroDefinition()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1862 | Result.AddTextChunk(", nil"); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1863 | else if (Context.Idents.get("NULL").hasMacroDefinition()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1864 | Result.AddTextChunk(", NULL"); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1865 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1866 | Result.AddTextChunk(", (void*)0"); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1870 | static std::string FormatFunctionParameter(ASTContext &Context, |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 1871 | ParmVarDecl *Param, |
| 1872 | bool SuppressName = false) { |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1873 | bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext()); |
| 1874 | if (Param->getType()->isDependentType() || |
| 1875 | !Param->getType()->isBlockPointerType()) { |
| 1876 | // The argument for a dependent or non-block parameter is a placeholder |
| 1877 | // containing that parameter's type. |
| 1878 | std::string Result; |
| 1879 | |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 1880 | if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName) |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1881 | Result = Param->getIdentifier()->getName(); |
| 1882 | |
| 1883 | Param->getType().getAsStringInternal(Result, |
| 1884 | Context.PrintingPolicy); |
| 1885 | |
| 1886 | if (ObjCMethodParam) { |
| 1887 | Result = "(" + Result; |
| 1888 | Result += ")"; |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 1889 | if (Param->getIdentifier() && !SuppressName) |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1890 | Result += Param->getIdentifier()->getName(); |
| 1891 | } |
| 1892 | return Result; |
| 1893 | } |
| 1894 | |
| 1895 | // The argument for a block pointer parameter is a block literal with |
| 1896 | // the appropriate type. |
Douglas Gregor | 830072c | 2011-02-15 22:37:09 +0000 | [diff] [blame] | 1897 | FunctionTypeLoc *Block = 0; |
| 1898 | FunctionProtoTypeLoc *BlockProto = 0; |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1899 | TypeLoc TL; |
| 1900 | if (TypeSourceInfo *TSInfo = Param->getTypeSourceInfo()) { |
| 1901 | TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 1902 | while (true) { |
| 1903 | // Look through typedefs. |
| 1904 | if (TypedefTypeLoc *TypedefTL = dyn_cast<TypedefTypeLoc>(&TL)) { |
| 1905 | if (TypeSourceInfo *InnerTSInfo |
| 1906 | = TypedefTL->getTypedefDecl()->getTypeSourceInfo()) { |
| 1907 | TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 1908 | continue; |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | // Look through qualified types |
| 1913 | if (QualifiedTypeLoc *QualifiedTL = dyn_cast<QualifiedTypeLoc>(&TL)) { |
| 1914 | TL = QualifiedTL->getUnqualifiedLoc(); |
| 1915 | continue; |
| 1916 | } |
| 1917 | |
| 1918 | // Try to get the function prototype behind the block pointer type, |
| 1919 | // then we're done. |
| 1920 | if (BlockPointerTypeLoc *BlockPtr |
| 1921 | = dyn_cast<BlockPointerTypeLoc>(&TL)) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 1922 | TL = BlockPtr->getPointeeLoc().IgnoreParens(); |
Douglas Gregor | 830072c | 2011-02-15 22:37:09 +0000 | [diff] [blame] | 1923 | Block = dyn_cast<FunctionTypeLoc>(&TL); |
| 1924 | BlockProto = dyn_cast<FunctionProtoTypeLoc>(&TL); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1925 | } |
| 1926 | break; |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | if (!Block) { |
| 1931 | // We were unable to find a FunctionProtoTypeLoc with parameter names |
| 1932 | // for the block; just use the parameter type as a placeholder. |
| 1933 | std::string Result; |
| 1934 | Param->getType().getUnqualifiedType(). |
| 1935 | getAsStringInternal(Result, Context.PrintingPolicy); |
| 1936 | |
| 1937 | if (ObjCMethodParam) { |
| 1938 | Result = "(" + Result; |
| 1939 | Result += ")"; |
| 1940 | if (Param->getIdentifier()) |
| 1941 | Result += Param->getIdentifier()->getName(); |
| 1942 | } |
| 1943 | |
| 1944 | return Result; |
| 1945 | } |
| 1946 | |
| 1947 | // We have the function prototype behind the block pointer type, as it was |
| 1948 | // written in the source. |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 1949 | std::string Result; |
| 1950 | QualType ResultType = Block->getTypePtr()->getResultType(); |
| 1951 | if (!ResultType->isVoidType()) |
| 1952 | ResultType.getAsStringInternal(Result, Context.PrintingPolicy); |
| 1953 | |
| 1954 | Result = '^' + Result; |
Douglas Gregor | 830072c | 2011-02-15 22:37:09 +0000 | [diff] [blame] | 1955 | if (!BlockProto || Block->getNumArgs() == 0) { |
| 1956 | if (BlockProto && BlockProto->getTypePtr()->isVariadic()) |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 1957 | Result += "(...)"; |
Douglas Gregor | c2760bc | 2010-10-02 23:49:58 +0000 | [diff] [blame] | 1958 | else |
| 1959 | Result += "(void)"; |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 1960 | } else { |
| 1961 | Result += "("; |
| 1962 | for (unsigned I = 0, N = Block->getNumArgs(); I != N; ++I) { |
| 1963 | if (I) |
| 1964 | Result += ", "; |
| 1965 | Result += FormatFunctionParameter(Context, Block->getArg(I)); |
| 1966 | |
Douglas Gregor | 830072c | 2011-02-15 22:37:09 +0000 | [diff] [blame] | 1967 | if (I == N - 1 && BlockProto->getTypePtr()->isVariadic()) |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 1968 | Result += ", ..."; |
| 1969 | } |
| 1970 | Result += ")"; |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 1971 | } |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 1972 | |
Douglas Gregor | c2760bc | 2010-10-02 23:49:58 +0000 | [diff] [blame] | 1973 | if (Param->getIdentifier()) |
| 1974 | Result += Param->getIdentifier()->getName(); |
| 1975 | |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1976 | return Result; |
| 1977 | } |
| 1978 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1979 | /// \brief Add function parameter chunks to the given code completion string. |
| 1980 | static void AddFunctionParameterChunks(ASTContext &Context, |
| 1981 | FunctionDecl *Function, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1982 | CodeCompletionBuilder &Result, |
| 1983 | unsigned Start = 0, |
| 1984 | bool InOptional = false) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1985 | typedef CodeCompletionString::Chunk Chunk; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1986 | bool FirstParameter = true; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1987 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1988 | for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1989 | ParmVarDecl *Param = Function->getParamDecl(P); |
| 1990 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1991 | if (Param->hasDefaultArg() && !InOptional) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1992 | // When we see an optional default argument, put that argument and |
| 1993 | // the remaining default arguments into a new, optional string. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1994 | CodeCompletionBuilder Opt(Result.getAllocator()); |
| 1995 | if (!FirstParameter) |
| 1996 | Opt.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
| 1997 | AddFunctionParameterChunks(Context, Function, Opt, P, true); |
| 1998 | Result.AddOptionalChunk(Opt.TakeString()); |
| 1999 | break; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2002 | if (FirstParameter) |
| 2003 | FirstParameter = false; |
| 2004 | else |
| 2005 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
| 2006 | |
| 2007 | InOptional = false; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2008 | |
| 2009 | // Format the placeholder string. |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2010 | std::string PlaceholderStr = FormatFunctionParameter(Context, Param); |
| 2011 | |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2012 | if (Function->isVariadic() && P == N - 1) |
| 2013 | PlaceholderStr += ", ..."; |
| 2014 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2015 | // Add the placeholder string. |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2016 | Result.AddPlaceholderChunk( |
| 2017 | Result.getAllocator().CopyString(PlaceholderStr)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2018 | } |
Douglas Gregor | b3d4525 | 2009-09-22 21:42:17 +0000 | [diff] [blame] | 2019 | |
| 2020 | if (const FunctionProtoType *Proto |
| 2021 | = Function->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2022 | if (Proto->isVariadic()) { |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2023 | if (Proto->getNumArgs() == 0) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2024 | Result.AddPlaceholderChunk("..."); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2025 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2026 | MaybeAddSentinel(Context, Function, Result); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2027 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | /// \brief Add template parameter chunks to the given code completion string. |
| 2031 | static void AddTemplateParameterChunks(ASTContext &Context, |
| 2032 | TemplateDecl *Template, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2033 | CodeCompletionBuilder &Result, |
| 2034 | unsigned MaxParameters = 0, |
| 2035 | unsigned Start = 0, |
| 2036 | bool InDefaultArg = false) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2037 | typedef CodeCompletionString::Chunk Chunk; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2038 | bool FirstParameter = true; |
| 2039 | |
| 2040 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2041 | TemplateParameterList::iterator PEnd = Params->end(); |
| 2042 | if (MaxParameters) |
| 2043 | PEnd = Params->begin() + MaxParameters; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2044 | for (TemplateParameterList::iterator P = Params->begin() + Start; |
| 2045 | P != PEnd; ++P) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2046 | bool HasDefaultArg = false; |
| 2047 | std::string PlaceholderStr; |
| 2048 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 2049 | if (TTP->wasDeclaredWithTypename()) |
| 2050 | PlaceholderStr = "typename"; |
| 2051 | else |
| 2052 | PlaceholderStr = "class"; |
| 2053 | |
| 2054 | if (TTP->getIdentifier()) { |
| 2055 | PlaceholderStr += ' '; |
| 2056 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2057 | } |
| 2058 | |
| 2059 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2060 | } else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2061 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2062 | if (NTTP->getIdentifier()) |
| 2063 | PlaceholderStr = NTTP->getIdentifier()->getName(); |
| 2064 | NTTP->getType().getAsStringInternal(PlaceholderStr, |
| 2065 | Context.PrintingPolicy); |
| 2066 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2067 | } else { |
| 2068 | assert(isa<TemplateTemplateParmDecl>(*P)); |
| 2069 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 2070 | |
| 2071 | // Since putting the template argument list into the placeholder would |
| 2072 | // be very, very long, we just use an abbreviation. |
| 2073 | PlaceholderStr = "template<...> class"; |
| 2074 | if (TTP->getIdentifier()) { |
| 2075 | PlaceholderStr += ' '; |
| 2076 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2077 | } |
| 2078 | |
| 2079 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2080 | } |
| 2081 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2082 | if (HasDefaultArg && !InDefaultArg) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2083 | // When we see an optional default argument, put that argument and |
| 2084 | // the remaining default arguments into a new, optional string. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2085 | CodeCompletionBuilder Opt(Result.getAllocator()); |
| 2086 | if (!FirstParameter) |
| 2087 | Opt.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
| 2088 | AddTemplateParameterChunks(Context, Template, Opt, MaxParameters, |
| 2089 | P - Params->begin(), true); |
| 2090 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2091 | break; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2094 | InDefaultArg = false; |
| 2095 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2096 | if (FirstParameter) |
| 2097 | FirstParameter = false; |
| 2098 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2099 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2100 | |
| 2101 | // Add the placeholder string. |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2102 | Result.AddPlaceholderChunk( |
| 2103 | Result.getAllocator().CopyString(PlaceholderStr)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2104 | } |
| 2105 | } |
| 2106 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2107 | /// \brief Add a qualifier to the given code-completion string, if the |
| 2108 | /// provided nested-name-specifier is non-NULL. |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2109 | static void |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2110 | AddQualifierToCompletionString(CodeCompletionBuilder &Result, |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2111 | NestedNameSpecifier *Qualifier, |
| 2112 | bool QualifierIsInformative, |
| 2113 | ASTContext &Context) { |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2114 | if (!Qualifier) |
| 2115 | return; |
| 2116 | |
| 2117 | std::string PrintedNNS; |
| 2118 | { |
| 2119 | llvm::raw_string_ostream OS(PrintedNNS); |
| 2120 | Qualifier->print(OS, Context.PrintingPolicy); |
| 2121 | } |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2122 | if (QualifierIsInformative) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2123 | Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2124 | else |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2125 | Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2128 | static void |
| 2129 | AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result, |
| 2130 | FunctionDecl *Function) { |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2131 | const FunctionProtoType *Proto |
| 2132 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2133 | if (!Proto || !Proto->getTypeQuals()) |
| 2134 | return; |
| 2135 | |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2136 | // FIXME: Add ref-qualifier! |
| 2137 | |
| 2138 | // Handle single qualifiers without copying |
| 2139 | if (Proto->getTypeQuals() == Qualifiers::Const) { |
| 2140 | Result.AddInformativeChunk(" const"); |
| 2141 | return; |
| 2142 | } |
| 2143 | |
| 2144 | if (Proto->getTypeQuals() == Qualifiers::Volatile) { |
| 2145 | Result.AddInformativeChunk(" volatile"); |
| 2146 | return; |
| 2147 | } |
| 2148 | |
| 2149 | if (Proto->getTypeQuals() == Qualifiers::Restrict) { |
| 2150 | Result.AddInformativeChunk(" restrict"); |
| 2151 | return; |
| 2152 | } |
| 2153 | |
| 2154 | // Handle multiple qualifiers. |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2155 | std::string QualsStr; |
| 2156 | if (Proto->getTypeQuals() & Qualifiers::Const) |
| 2157 | QualsStr += " const"; |
| 2158 | if (Proto->getTypeQuals() & Qualifiers::Volatile) |
| 2159 | QualsStr += " volatile"; |
| 2160 | if (Proto->getTypeQuals() & Qualifiers::Restrict) |
| 2161 | QualsStr += " restrict"; |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2162 | Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr)); |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2165 | /// \brief Add the name of the given declaration |
| 2166 | static void AddTypedNameChunk(ASTContext &Context, NamedDecl *ND, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2167 | CodeCompletionBuilder &Result) { |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2168 | typedef CodeCompletionString::Chunk Chunk; |
| 2169 | |
| 2170 | DeclarationName Name = ND->getDeclName(); |
| 2171 | if (!Name) |
| 2172 | return; |
| 2173 | |
| 2174 | switch (Name.getNameKind()) { |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2175 | case DeclarationName::CXXOperatorName: { |
| 2176 | const char *OperatorName = 0; |
| 2177 | switch (Name.getCXXOverloadedOperator()) { |
| 2178 | case OO_None: |
| 2179 | case OO_Conditional: |
| 2180 | case NUM_OVERLOADED_OPERATORS: |
| 2181 | OperatorName = "operator"; |
| 2182 | break; |
| 2183 | |
| 2184 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 2185 | case OO_##Name: OperatorName = "operator" Spelling; break; |
| 2186 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) |
| 2187 | #include "clang/Basic/OperatorKinds.def" |
| 2188 | |
| 2189 | case OO_New: OperatorName = "operator new"; break; |
| 2190 | case OO_Delete: OperatorName = "operator delete"; break; |
| 2191 | case OO_Array_New: OperatorName = "operator new[]"; break; |
| 2192 | case OO_Array_Delete: OperatorName = "operator delete[]"; break; |
| 2193 | case OO_Call: OperatorName = "operator()"; break; |
| 2194 | case OO_Subscript: OperatorName = "operator[]"; break; |
| 2195 | } |
| 2196 | Result.AddTypedTextChunk(OperatorName); |
| 2197 | break; |
| 2198 | } |
| 2199 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2200 | case DeclarationName::Identifier: |
| 2201 | case DeclarationName::CXXConversionFunctionName: |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2202 | case DeclarationName::CXXDestructorName: |
| 2203 | case DeclarationName::CXXLiteralOperatorName: |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2204 | Result.AddTypedTextChunk( |
| 2205 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2206 | break; |
| 2207 | |
| 2208 | case DeclarationName::CXXUsingDirective: |
| 2209 | case DeclarationName::ObjCZeroArgSelector: |
| 2210 | case DeclarationName::ObjCOneArgSelector: |
| 2211 | case DeclarationName::ObjCMultiArgSelector: |
| 2212 | break; |
| 2213 | |
| 2214 | case DeclarationName::CXXConstructorName: { |
| 2215 | CXXRecordDecl *Record = 0; |
| 2216 | QualType Ty = Name.getCXXNameType(); |
| 2217 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) |
| 2218 | Record = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 2219 | else if (const InjectedClassNameType *InjectedTy |
| 2220 | = Ty->getAs<InjectedClassNameType>()) |
| 2221 | Record = InjectedTy->getDecl(); |
| 2222 | else { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2223 | Result.AddTypedTextChunk( |
| 2224 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2225 | break; |
| 2226 | } |
| 2227 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2228 | Result.AddTypedTextChunk( |
| 2229 | Result.getAllocator().CopyString(Record->getNameAsString())); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2230 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2231 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2232 | AddTemplateParameterChunks(Context, Template, Result); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2233 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2234 | } |
| 2235 | break; |
| 2236 | } |
| 2237 | } |
| 2238 | } |
| 2239 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2240 | /// \brief If possible, create a new code completion string for the given |
| 2241 | /// result. |
| 2242 | /// |
| 2243 | /// \returns Either a new, heap-allocated code completion string describing |
| 2244 | /// how to use this result, or NULL to indicate that the string or name of the |
| 2245 | /// result is all that is needed. |
| 2246 | CodeCompletionString * |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2247 | CodeCompletionResult::CreateCodeCompletionString(Sema &S, |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2248 | CodeCompletionAllocator &Allocator) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2249 | typedef CodeCompletionString::Chunk Chunk; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2250 | CodeCompletionBuilder Result(Allocator, Priority, Availability); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2251 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2252 | if (Kind == RK_Pattern) { |
| 2253 | Pattern->Priority = Priority; |
| 2254 | Pattern->Availability = Availability; |
| 2255 | return Pattern; |
| 2256 | } |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2257 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2258 | if (Kind == RK_Keyword) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2259 | Result.AddTypedTextChunk(Keyword); |
| 2260 | return Result.TakeString(); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2261 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2262 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2263 | if (Kind == RK_Macro) { |
| 2264 | MacroInfo *MI = S.PP.getMacroInfo(Macro); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2265 | assert(MI && "Not a macro?"); |
| 2266 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2267 | Result.AddTypedTextChunk( |
| 2268 | Result.getAllocator().CopyString(Macro->getName())); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2269 | |
| 2270 | if (!MI->isFunctionLike()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2271 | return Result.TakeString(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2272 | |
| 2273 | // Format a function-like macro with placeholders for the arguments. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2274 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2275 | for (MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end(); |
| 2276 | A != AEnd; ++A) { |
| 2277 | if (A != MI->arg_begin()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2278 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2279 | |
| 2280 | if (!MI->isVariadic() || A != AEnd - 1) { |
| 2281 | // Non-variadic argument. |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2282 | Result.AddPlaceholderChunk( |
| 2283 | Result.getAllocator().CopyString((*A)->getName())); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2284 | continue; |
| 2285 | } |
| 2286 | |
| 2287 | // Variadic argument; cope with the different between GNU and C99 |
| 2288 | // variadic macros, providing a single placeholder for the rest of the |
| 2289 | // arguments. |
| 2290 | if ((*A)->isStr("__VA_ARGS__")) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2291 | Result.AddPlaceholderChunk("..."); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2292 | else { |
| 2293 | std::string Arg = (*A)->getName(); |
| 2294 | Arg += "..."; |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2295 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2296 | } |
| 2297 | } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2298 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
| 2299 | return Result.TakeString(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2302 | assert(Kind == RK_Declaration && "Missed a result kind?"); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2303 | NamedDecl *ND = Declaration; |
| 2304 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2305 | if (StartsNestedNameSpecifier) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2306 | Result.AddTypedTextChunk( |
| 2307 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2308 | Result.AddTextChunk("::"); |
| 2309 | return Result.TakeString(); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2312 | AddResultTypeChunk(S.Context, ND, Result); |
| 2313 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2314 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2315 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2316 | S.Context); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2317 | AddTypedNameChunk(S.Context, ND, Result); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2318 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2319 | AddFunctionParameterChunks(S.Context, Function, Result); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2320 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2321 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2322 | return Result.TakeString(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
| 2325 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2326 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2327 | S.Context); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2328 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2329 | AddTypedNameChunk(S.Context, Function, Result); |
| 2330 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2331 | // Figure out which template parameters are deduced (or have default |
| 2332 | // arguments). |
| 2333 | llvm::SmallVector<bool, 16> Deduced; |
| 2334 | S.MarkDeducedTemplateParameters(FunTmpl, Deduced); |
| 2335 | unsigned LastDeducibleArgument; |
| 2336 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
| 2337 | --LastDeducibleArgument) { |
| 2338 | if (!Deduced[LastDeducibleArgument - 1]) { |
| 2339 | // C++0x: Figure out if the template argument has a default. If so, |
| 2340 | // the user doesn't need to type this argument. |
| 2341 | // FIXME: We need to abstract template parameters better! |
| 2342 | bool HasDefaultArg = false; |
| 2343 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2344 | LastDeducibleArgument - 1); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2345 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 2346 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2347 | else if (NonTypeTemplateParmDecl *NTTP |
| 2348 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 2349 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2350 | else { |
| 2351 | assert(isa<TemplateTemplateParmDecl>(Param)); |
| 2352 | HasDefaultArg |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2353 | = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
| 2356 | if (!HasDefaultArg) |
| 2357 | break; |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | if (LastDeducibleArgument) { |
| 2362 | // Some of the function template arguments cannot be deduced from a |
| 2363 | // function call, so we introduce an explicit template argument list |
| 2364 | // containing all of the arguments up to the first deducible argument. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2365 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2366 | AddTemplateParameterChunks(S.Context, FunTmpl, Result, |
| 2367 | LastDeducibleArgument); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2368 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | // Add the function parameters |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2372 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2373 | AddFunctionParameterChunks(S.Context, Function, Result); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2374 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2375 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2376 | return Result.TakeString(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2380 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2381 | S.Context); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2382 | Result.AddTypedTextChunk( |
| 2383 | Result.getAllocator().CopyString(Template->getNameAsString())); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2384 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2385 | AddTemplateParameterChunks(S.Context, Template, Result); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2386 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
| 2387 | return Result.TakeString(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2390 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2391 | Selector Sel = Method->getSelector(); |
| 2392 | if (Sel.isUnarySelector()) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2393 | Result.AddTypedTextChunk(Result.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2394 | Sel.getNameForSlot(0))); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2395 | return Result.TakeString(); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2398 | std::string SelName = Sel.getNameForSlot(0).str(); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2399 | SelName += ':'; |
| 2400 | if (StartParameter == 0) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2401 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName)); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2402 | else { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2403 | Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName)); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2404 | |
| 2405 | // If there is only one parameter, and we're past it, add an empty |
| 2406 | // typed-text chunk since there is nothing to type. |
| 2407 | if (Method->param_size() == 1) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2408 | Result.AddTypedTextChunk(""); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2409 | } |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2410 | unsigned Idx = 0; |
| 2411 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 2412 | PEnd = Method->param_end(); |
| 2413 | P != PEnd; (void)++P, ++Idx) { |
| 2414 | if (Idx > 0) { |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2415 | std::string Keyword; |
| 2416 | if (Idx > StartParameter) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2417 | Result.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2418 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) |
| 2419 | Keyword += II->getName().str(); |
| 2420 | Keyword += ":"; |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2421 | if (Idx < StartParameter || AllParametersAreInformative) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2422 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword)); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2423 | else |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2424 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword)); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2425 | } |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2426 | |
| 2427 | // If we're before the starting parameter, skip the placeholder. |
| 2428 | if (Idx < StartParameter) |
| 2429 | continue; |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2430 | |
| 2431 | std::string Arg; |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2432 | |
| 2433 | if ((*P)->getType()->isBlockPointerType() && !DeclaringEntity) |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2434 | Arg = FormatFunctionParameter(S.Context, *P, true); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2435 | else { |
| 2436 | (*P)->getType().getAsStringInternal(Arg, S.Context.PrintingPolicy); |
| 2437 | Arg = "(" + Arg + ")"; |
| 2438 | if (IdentifierInfo *II = (*P)->getIdentifier()) |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2439 | if (DeclaringEntity || AllParametersAreInformative) |
| 2440 | Arg += II->getName().str(); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2443 | if (Method->isVariadic() && (P + 1) == PEnd) |
| 2444 | Arg += ", ..."; |
| 2445 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2446 | if (DeclaringEntity) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2447 | Result.AddTextChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2448 | else if (AllParametersAreInformative) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2449 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2450 | else |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2451 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
Douglas Gregor | 2a17af0 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2454 | if (Method->isVariadic()) { |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2455 | if (Method->param_size() == 0) { |
| 2456 | if (DeclaringEntity) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2457 | Result.AddTextChunk(", ..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2458 | else if (AllParametersAreInformative) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2459 | Result.AddInformativeChunk(", ..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2460 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2461 | Result.AddPlaceholderChunk(", ..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2462 | } |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2463 | |
| 2464 | MaybeAddSentinel(S.Context, Method, Result); |
Douglas Gregor | 2a17af0 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2467 | return Result.TakeString(); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2470 | if (Qualifier) |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2471 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2472 | S.Context); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2473 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2474 | Result.AddTypedTextChunk( |
| 2475 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2476 | return Result.TakeString(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2479 | CodeCompletionString * |
| 2480 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( |
| 2481 | unsigned CurrentArg, |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 2482 | Sema &S, |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2483 | CodeCompletionAllocator &Allocator) const { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2484 | typedef CodeCompletionString::Chunk Chunk; |
| 2485 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2486 | // FIXME: Set priority, availability appropriately. |
| 2487 | CodeCompletionBuilder Result(Allocator, 1, CXAvailability_Available); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2488 | FunctionDecl *FDecl = getFunction(); |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2489 | AddResultTypeChunk(S.Context, FDecl, Result); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2490 | const FunctionProtoType *Proto |
| 2491 | = dyn_cast<FunctionProtoType>(getFunctionType()); |
| 2492 | if (!FDecl && !Proto) { |
| 2493 | // Function without a prototype. Just give the return type and a |
| 2494 | // highlighted ellipsis. |
| 2495 | const FunctionType *FT = getFunctionType(); |
Douglas Gregor | a63f6de | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2496 | Result.AddTextChunk(GetCompletionTypeString(FT->getResultType(), |
| 2497 | S.Context, |
| 2498 | Result.getAllocator())); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2499 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
| 2500 | Result.AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "...")); |
| 2501 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
| 2502 | return Result.TakeString(); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | if (FDecl) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2506 | Result.AddTextChunk( |
| 2507 | Result.getAllocator().CopyString(FDecl->getNameAsString())); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2508 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2509 | Result.AddTextChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2510 | Result.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2511 | Proto->getResultType().getAsString(S.Context.PrintingPolicy))); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2512 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2513 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2514 | unsigned NumParams = FDecl? FDecl->getNumParams() : Proto->getNumArgs(); |
| 2515 | for (unsigned I = 0; I != NumParams; ++I) { |
| 2516 | if (I) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2517 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2518 | |
| 2519 | std::string ArgString; |
| 2520 | QualType ArgType; |
| 2521 | |
| 2522 | if (FDecl) { |
| 2523 | ArgString = FDecl->getParamDecl(I)->getNameAsString(); |
| 2524 | ArgType = FDecl->getParamDecl(I)->getOriginalType(); |
| 2525 | } else { |
| 2526 | ArgType = Proto->getArgType(I); |
| 2527 | } |
| 2528 | |
| 2529 | ArgType.getAsStringInternal(ArgString, S.Context.PrintingPolicy); |
| 2530 | |
| 2531 | if (I == CurrentArg) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2532 | Result.AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2533 | Result.getAllocator().CopyString(ArgString))); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2534 | else |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2535 | Result.AddTextChunk(Result.getAllocator().CopyString(ArgString)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2536 | } |
| 2537 | |
| 2538 | if (Proto && Proto->isVariadic()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2539 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2540 | if (CurrentArg < NumParams) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2541 | Result.AddTextChunk("..."); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2542 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2543 | Result.AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "...")); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2544 | } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2545 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2546 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2547 | return Result.TakeString(); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2550 | unsigned clang::getMacroUsagePriority(llvm::StringRef MacroName, |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2551 | const LangOptions &LangOpts, |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2552 | bool PreferredTypeIsPointer) { |
| 2553 | unsigned Priority = CCP_Macro; |
| 2554 | |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2555 | // Treat the "nil", "Nil" and "NULL" macros as null pointer constants. |
| 2556 | if (MacroName.equals("nil") || MacroName.equals("NULL") || |
| 2557 | MacroName.equals("Nil")) { |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2558 | Priority = CCP_Constant; |
| 2559 | if (PreferredTypeIsPointer) |
| 2560 | Priority = Priority / CCF_SimilarTypeMatch; |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2561 | } |
| 2562 | // Treat "YES", "NO", "true", and "false" as constants. |
| 2563 | else if (MacroName.equals("YES") || MacroName.equals("NO") || |
| 2564 | MacroName.equals("true") || MacroName.equals("false")) |
| 2565 | Priority = CCP_Constant; |
| 2566 | // Treat "bool" as a type. |
| 2567 | else if (MacroName.equals("bool")) |
| 2568 | Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0); |
| 2569 | |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2570 | |
| 2571 | return Priority; |
| 2572 | } |
| 2573 | |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 2574 | CXCursorKind clang::getCursorKindForDecl(Decl *D) { |
| 2575 | if (!D) |
| 2576 | return CXCursor_UnexposedDecl; |
| 2577 | |
| 2578 | switch (D->getKind()) { |
| 2579 | case Decl::Enum: return CXCursor_EnumDecl; |
| 2580 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 2581 | case Decl::Field: return CXCursor_FieldDecl; |
| 2582 | case Decl::Function: |
| 2583 | return CXCursor_FunctionDecl; |
| 2584 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 2585 | case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryImplDecl; |
| 2586 | case Decl::ObjCClass: |
| 2587 | // FIXME |
| 2588 | return CXCursor_UnexposedDecl; |
| 2589 | case Decl::ObjCForwardProtocol: |
| 2590 | // FIXME |
| 2591 | return CXCursor_UnexposedDecl; |
| 2592 | case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl; |
| 2593 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
| 2594 | case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl; |
| 2595 | case Decl::ObjCMethod: |
| 2596 | return cast<ObjCMethodDecl>(D)->isInstanceMethod() |
| 2597 | ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl; |
| 2598 | case Decl::CXXMethod: return CXCursor_CXXMethod; |
| 2599 | case Decl::CXXConstructor: return CXCursor_Constructor; |
| 2600 | case Decl::CXXDestructor: return CXCursor_Destructor; |
| 2601 | case Decl::CXXConversion: return CXCursor_ConversionFunction; |
| 2602 | case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl; |
| 2603 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
| 2604 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 2605 | case Decl::Typedef: return CXCursor_TypedefDecl; |
| 2606 | case Decl::Var: return CXCursor_VarDecl; |
| 2607 | case Decl::Namespace: return CXCursor_Namespace; |
| 2608 | case Decl::NamespaceAlias: return CXCursor_NamespaceAlias; |
| 2609 | case Decl::TemplateTypeParm: return CXCursor_TemplateTypeParameter; |
| 2610 | case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter; |
| 2611 | case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter; |
| 2612 | case Decl::FunctionTemplate: return CXCursor_FunctionTemplate; |
| 2613 | case Decl::ClassTemplate: return CXCursor_ClassTemplate; |
| 2614 | case Decl::ClassTemplatePartialSpecialization: |
| 2615 | return CXCursor_ClassTemplatePartialSpecialization; |
| 2616 | case Decl::UsingDirective: return CXCursor_UsingDirective; |
| 2617 | |
| 2618 | case Decl::Using: |
| 2619 | case Decl::UnresolvedUsingValue: |
| 2620 | case Decl::UnresolvedUsingTypename: |
| 2621 | return CXCursor_UsingDeclaration; |
| 2622 | |
| 2623 | default: |
| 2624 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 2625 | switch (TD->getTagKind()) { |
| 2626 | case TTK_Struct: return CXCursor_StructDecl; |
| 2627 | case TTK_Class: return CXCursor_ClassDecl; |
| 2628 | case TTK_Union: return CXCursor_UnionDecl; |
| 2629 | case TTK_Enum: return CXCursor_EnumDecl; |
| 2630 | } |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | return CXCursor_UnexposedDecl; |
| 2635 | } |
| 2636 | |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2637 | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, |
| 2638 | bool TargetTypeIsPointer = false) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2639 | typedef CodeCompletionResult Result; |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2640 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2641 | Results.EnterNewScope(); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2642 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2643 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 2644 | MEnd = PP.macro_end(); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2645 | M != MEnd; ++M) { |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2646 | Results.AddResult(Result(M->first, |
| 2647 | getMacroUsagePriority(M->first->getName(), |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2648 | PP.getLangOptions(), |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2649 | TargetTypeIsPointer))); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2650 | } |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2651 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2652 | Results.ExitScope(); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2653 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2656 | static void AddPrettyFunctionResults(const LangOptions &LangOpts, |
| 2657 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2658 | typedef CodeCompletionResult Result; |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2659 | |
| 2660 | Results.EnterNewScope(); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2661 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2662 | Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant)); |
| 2663 | Results.AddResult(Result("__FUNCTION__", CCP_Constant)); |
| 2664 | if (LangOpts.C99 || LangOpts.CPlusPlus0x) |
| 2665 | Results.AddResult(Result("__func__", CCP_Constant)); |
| 2666 | Results.ExitScope(); |
| 2667 | } |
| 2668 | |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2669 | static void HandleCodeCompleteResults(Sema *S, |
| 2670 | CodeCompleteConsumer *CodeCompleter, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2671 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2672 | CodeCompletionResult *Results, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2673 | unsigned NumResults) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2674 | if (CodeCompleter) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2675 | CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2678 | static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S, |
| 2679 | Sema::ParserCompletionContext PCC) { |
| 2680 | switch (PCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2681 | case Sema::PCC_Namespace: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2682 | return CodeCompletionContext::CCC_TopLevel; |
| 2683 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2684 | case Sema::PCC_Class: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2685 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 2686 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2687 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2688 | return CodeCompletionContext::CCC_ObjCInterface; |
| 2689 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2690 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2691 | return CodeCompletionContext::CCC_ObjCImplementation; |
| 2692 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2693 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2694 | return CodeCompletionContext::CCC_ObjCIvarList; |
| 2695 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2696 | case Sema::PCC_Template: |
| 2697 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2698 | if (S.CurContext->isFileContext()) |
| 2699 | return CodeCompletionContext::CCC_TopLevel; |
| 2700 | else if (S.CurContext->isRecord()) |
| 2701 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 2702 | else |
| 2703 | return CodeCompletionContext::CCC_Other; |
| 2704 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2705 | case Sema::PCC_RecoveryInFunction: |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2706 | return CodeCompletionContext::CCC_Recovery; |
Douglas Gregor | a5450a0 | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 2707 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2708 | case Sema::PCC_ForInit: |
Douglas Gregor | a5450a0 | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 2709 | if (S.getLangOptions().CPlusPlus || S.getLangOptions().C99 || |
| 2710 | S.getLangOptions().ObjC1) |
| 2711 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
| 2712 | else |
| 2713 | return CodeCompletionContext::CCC_Expression; |
| 2714 | |
| 2715 | case Sema::PCC_Expression: |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2716 | case Sema::PCC_Condition: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2717 | return CodeCompletionContext::CCC_Expression; |
| 2718 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2719 | case Sema::PCC_Statement: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2720 | return CodeCompletionContext::CCC_Statement; |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2721 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2722 | case Sema::PCC_Type: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2723 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2724 | |
| 2725 | case Sema::PCC_ParenthesizedExpression: |
| 2726 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2727 | |
| 2728 | case Sema::PCC_LocalDeclarationSpecifiers: |
| 2729 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | return CodeCompletionContext::CCC_Other; |
| 2733 | } |
| 2734 | |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2735 | /// \brief If we're in a C++ virtual member function, add completion results |
| 2736 | /// that invoke the functions we override, since it's common to invoke the |
| 2737 | /// overridden function as well as adding new functionality. |
| 2738 | /// |
| 2739 | /// \param S The semantic analysis object for which we are generating results. |
| 2740 | /// |
| 2741 | /// \param InContext This context in which the nested-name-specifier preceding |
| 2742 | /// the code-completion point |
| 2743 | static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, |
| 2744 | ResultBuilder &Results) { |
| 2745 | // Look through blocks. |
| 2746 | DeclContext *CurContext = S.CurContext; |
| 2747 | while (isa<BlockDecl>(CurContext)) |
| 2748 | CurContext = CurContext->getParent(); |
| 2749 | |
| 2750 | |
| 2751 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext); |
| 2752 | if (!Method || !Method->isVirtual()) |
| 2753 | return; |
| 2754 | |
| 2755 | // We need to have names for all of the parameters, if we're going to |
| 2756 | // generate a forwarding call. |
| 2757 | for (CXXMethodDecl::param_iterator P = Method->param_begin(), |
| 2758 | PEnd = Method->param_end(); |
| 2759 | P != PEnd; |
| 2760 | ++P) { |
| 2761 | if (!(*P)->getDeclName()) |
| 2762 | return; |
| 2763 | } |
| 2764 | |
| 2765 | for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(), |
| 2766 | MEnd = Method->end_overridden_methods(); |
| 2767 | M != MEnd; ++M) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2768 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2769 | CXXMethodDecl *Overridden = const_cast<CXXMethodDecl *>(*M); |
| 2770 | if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl()) |
| 2771 | continue; |
| 2772 | |
| 2773 | // If we need a nested-name-specifier, add one now. |
| 2774 | if (!InContext) { |
| 2775 | NestedNameSpecifier *NNS |
| 2776 | = getRequiredQualification(S.Context, CurContext, |
| 2777 | Overridden->getDeclContext()); |
| 2778 | if (NNS) { |
| 2779 | std::string Str; |
| 2780 | llvm::raw_string_ostream OS(Str); |
| 2781 | NNS->print(OS, S.Context.PrintingPolicy); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2782 | Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str())); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2783 | } |
| 2784 | } else if (!InContext->Equals(Overridden->getDeclContext())) |
| 2785 | continue; |
| 2786 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2787 | Builder.AddTypedTextChunk(Results.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2788 | Overridden->getNameAsString())); |
| 2789 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2790 | bool FirstParam = true; |
| 2791 | for (CXXMethodDecl::param_iterator P = Method->param_begin(), |
| 2792 | PEnd = Method->param_end(); |
| 2793 | P != PEnd; ++P) { |
| 2794 | if (FirstParam) |
| 2795 | FirstParam = false; |
| 2796 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2797 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2798 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2799 | Builder.AddPlaceholderChunk(Results.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2800 | (*P)->getIdentifier()->getName())); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2801 | } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2802 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2803 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2804 | CCP_SuperCompletion, |
| 2805 | CXCursor_CXXMethod)); |
| 2806 | Results.Ignore(Overridden); |
| 2807 | } |
| 2808 | } |
| 2809 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2810 | void Sema::CodeCompleteOrdinaryName(Scope *S, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2811 | ParserCompletionContext CompletionContext) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2812 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2813 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2814 | mapCodeCompletionContext(*this, CompletionContext)); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2815 | Results.EnterNewScope(); |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 2816 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2817 | // Determine how to filter results, e.g., so that the names of |
| 2818 | // values (functions, enumerators, function templates, etc.) are |
| 2819 | // only allowed where we can have an expression. |
| 2820 | switch (CompletionContext) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2821 | case PCC_Namespace: |
| 2822 | case PCC_Class: |
| 2823 | case PCC_ObjCInterface: |
| 2824 | case PCC_ObjCImplementation: |
| 2825 | case PCC_ObjCInstanceVariableList: |
| 2826 | case PCC_Template: |
| 2827 | case PCC_MemberTemplate: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2828 | case PCC_Type: |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2829 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2830 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 2831 | break; |
| 2832 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2833 | case PCC_Statement: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2834 | case PCC_ParenthesizedExpression: |
Douglas Gregor | eb0d014 | 2010-08-24 23:58:17 +0000 | [diff] [blame] | 2835 | case PCC_Expression: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2836 | case PCC_ForInit: |
| 2837 | case PCC_Condition: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 2838 | if (WantTypesInContext(CompletionContext, getLangOptions())) |
| 2839 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 2840 | else |
| 2841 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2842 | |
| 2843 | if (getLangOptions().CPlusPlus) |
| 2844 | MaybeAddOverrideCalls(*this, /*InContext=*/0, Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2845 | break; |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 2846 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2847 | case PCC_RecoveryInFunction: |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 2848 | // Unfiltered |
| 2849 | break; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2850 | } |
| 2851 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 2852 | // If we are in a C++ non-static member function, check the qualifiers on |
| 2853 | // the member function to filter/prioritize the results list. |
| 2854 | if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext)) |
| 2855 | if (CurMethod->isInstance()) |
| 2856 | Results.setObjectTypeQualifiers( |
| 2857 | Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers())); |
| 2858 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 2859 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2860 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 2861 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 2a7925c | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 2862 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2863 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); |
Douglas Gregor | 2a7925c | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 2864 | Results.ExitScope(); |
| 2865 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2866 | switch (CompletionContext) { |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2867 | case PCC_ParenthesizedExpression: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2868 | case PCC_Expression: |
| 2869 | case PCC_Statement: |
| 2870 | case PCC_RecoveryInFunction: |
| 2871 | if (S->getFnParent()) |
| 2872 | AddPrettyFunctionResults(PP.getLangOptions(), Results); |
| 2873 | break; |
| 2874 | |
| 2875 | case PCC_Namespace: |
| 2876 | case PCC_Class: |
| 2877 | case PCC_ObjCInterface: |
| 2878 | case PCC_ObjCImplementation: |
| 2879 | case PCC_ObjCInstanceVariableList: |
| 2880 | case PCC_Template: |
| 2881 | case PCC_MemberTemplate: |
| 2882 | case PCC_ForInit: |
| 2883 | case PCC_Condition: |
| 2884 | case PCC_Type: |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2885 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2886 | break; |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2889 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2890 | AddMacroResults(PP, Results); |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2891 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 2892 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2893 | Results.data(),Results.size()); |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2896 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| 2897 | ParsedType Receiver, |
| 2898 | IdentifierInfo **SelIdents, |
| 2899 | unsigned NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2900 | bool AtArgumentExpression, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2901 | bool IsSuper, |
| 2902 | ResultBuilder &Results); |
| 2903 | |
| 2904 | void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, |
| 2905 | bool AllowNonIdentifiers, |
| 2906 | bool AllowNestedNameSpecifiers) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2907 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2908 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2909 | AllowNestedNameSpecifiers |
| 2910 | ? CodeCompletionContext::CCC_PotentiallyQualifiedName |
| 2911 | : CodeCompletionContext::CCC_Name); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2912 | Results.EnterNewScope(); |
| 2913 | |
| 2914 | // Type qualifiers can come after names. |
| 2915 | Results.AddResult(Result("const")); |
| 2916 | Results.AddResult(Result("volatile")); |
| 2917 | if (getLangOptions().C99) |
| 2918 | Results.AddResult(Result("restrict")); |
| 2919 | |
| 2920 | if (getLangOptions().CPlusPlus) { |
| 2921 | if (AllowNonIdentifiers) { |
| 2922 | Results.AddResult(Result("operator")); |
| 2923 | } |
| 2924 | |
| 2925 | // Add nested-name-specifiers. |
| 2926 | if (AllowNestedNameSpecifiers) { |
| 2927 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2928 | Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2929 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 2930 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
| 2931 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2932 | Results.setFilter(0); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2933 | } |
| 2934 | } |
| 2935 | Results.ExitScope(); |
| 2936 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2937 | // If we're in a context where we might have an expression (rather than a |
| 2938 | // declaration), and what we've seen so far is an Objective-C type that could |
| 2939 | // be a receiver of a class message, this may be a class message send with |
| 2940 | // the initial opening bracket '[' missing. Add appropriate completions. |
| 2941 | if (AllowNonIdentifiers && !AllowNestedNameSpecifiers && |
| 2942 | DS.getTypeSpecType() == DeclSpec::TST_typename && |
| 2943 | DS.getStorageClassSpecAsWritten() == DeclSpec::SCS_unspecified && |
| 2944 | !DS.isThreadSpecified() && !DS.isExternInLinkageSpec() && |
| 2945 | DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified && |
| 2946 | DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 2947 | DS.getTypeQualifiers() == 0 && |
| 2948 | S && |
| 2949 | (S->getFlags() & Scope::DeclScope) != 0 && |
| 2950 | (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope | |
| 2951 | Scope::FunctionPrototypeScope | |
| 2952 | Scope::AtCatchScope)) == 0) { |
| 2953 | ParsedType T = DS.getRepAsType(); |
| 2954 | if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType()) |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2955 | AddClassMessageCompletions(*this, S, T, 0, 0, false, false, Results); |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2956 | } |
| 2957 | |
Douglas Gregor | 4497dd4 | 2010-08-24 04:59:56 +0000 | [diff] [blame] | 2958 | // Note that we intentionally suppress macro results here, since we do not |
| 2959 | // encourage using macros to produce the names of entities. |
| 2960 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2961 | HandleCodeCompleteResults(this, CodeCompleter, |
| 2962 | Results.getCompletionContext(), |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2963 | Results.data(), Results.size()); |
| 2964 | } |
| 2965 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2966 | struct Sema::CodeCompleteExpressionData { |
| 2967 | CodeCompleteExpressionData(QualType PreferredType = QualType()) |
| 2968 | : PreferredType(PreferredType), IntegralConstantExpression(false), |
| 2969 | ObjCCollection(false) { } |
| 2970 | |
| 2971 | QualType PreferredType; |
| 2972 | bool IntegralConstantExpression; |
| 2973 | bool ObjCCollection; |
| 2974 | llvm::SmallVector<Decl *, 4> IgnoreDecls; |
| 2975 | }; |
| 2976 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2977 | /// \brief Perform code-completion in an expression context when we know what |
| 2978 | /// type we're looking for. |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 2979 | /// |
| 2980 | /// \param IntegralConstantExpression Only permit integral constant |
| 2981 | /// expressions. |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2982 | void Sema::CodeCompleteExpression(Scope *S, |
| 2983 | const CodeCompleteExpressionData &Data) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2984 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2985 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 2986 | CodeCompletionContext::CCC_Expression); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2987 | if (Data.ObjCCollection) |
| 2988 | Results.setFilter(&ResultBuilder::IsObjCCollection); |
| 2989 | else if (Data.IntegralConstantExpression) |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 2990 | Results.setFilter(&ResultBuilder::IsIntegralConstantValue); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2991 | else if (WantTypesInContext(PCC_Expression, getLangOptions())) |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2992 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 2993 | else |
| 2994 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2995 | |
| 2996 | if (!Data.PreferredType.isNull()) |
| 2997 | Results.setPreferredType(Data.PreferredType.getNonReferenceType()); |
| 2998 | |
| 2999 | // Ignore any declarations that we were told that we don't care about. |
| 3000 | for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I) |
| 3001 | Results.Ignore(Data.IgnoreDecls[I]); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3002 | |
| 3003 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3004 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3005 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3006 | |
| 3007 | Results.EnterNewScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3008 | AddOrdinaryNameResults(PCC_Expression, S, *this, Results); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3009 | Results.ExitScope(); |
| 3010 | |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3011 | bool PreferredTypeIsPointer = false; |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3012 | if (!Data.PreferredType.isNull()) |
| 3013 | PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType() |
| 3014 | || Data.PreferredType->isMemberPointerType() |
| 3015 | || Data.PreferredType->isBlockPointerType(); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3016 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3017 | if (S->getFnParent() && |
| 3018 | !Data.ObjCCollection && |
| 3019 | !Data.IntegralConstantExpression) |
| 3020 | AddPrettyFunctionResults(PP.getLangOptions(), Results); |
| 3021 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3022 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3023 | AddMacroResults(PP, Results, PreferredTypeIsPointer); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3024 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3025 | CodeCompletionContext(CodeCompletionContext::CCC_Expression, |
| 3026 | Data.PreferredType), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3027 | Results.data(),Results.size()); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
Douglas Gregor | ac5fd84 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 3030 | void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) { |
| 3031 | if (E.isInvalid()) |
| 3032 | CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction); |
| 3033 | else if (getLangOptions().ObjC1) |
| 3034 | CodeCompleteObjCInstanceMessage(S, E.take(), 0, 0, false); |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 3035 | } |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3036 | |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3037 | /// \brief The set of properties that have already been added, referenced by |
| 3038 | /// property name. |
| 3039 | typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet; |
| 3040 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3041 | static void AddObjCProperties(ObjCContainerDecl *Container, |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3042 | bool AllowCategories, |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3043 | DeclContext *CurContext, |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3044 | AddedPropertiesSet &AddedProperties, |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3045 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3046 | typedef CodeCompletionResult Result; |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3047 | |
| 3048 | // Add properties in this container. |
| 3049 | for (ObjCContainerDecl::prop_iterator P = Container->prop_begin(), |
| 3050 | PEnd = Container->prop_end(); |
| 3051 | P != PEnd; |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3052 | ++P) { |
| 3053 | if (AddedProperties.insert(P->getIdentifier())) |
| 3054 | Results.MaybeAddResult(Result(*P, 0), CurContext); |
| 3055 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3056 | |
| 3057 | // Add properties in referenced protocols. |
| 3058 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 3059 | for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(), |
| 3060 | PEnd = Protocol->protocol_end(); |
| 3061 | P != PEnd; ++P) |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3062 | AddObjCProperties(*P, AllowCategories, CurContext, AddedProperties, |
| 3063 | Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3064 | } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){ |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3065 | if (AllowCategories) { |
| 3066 | // Look through categories. |
| 3067 | for (ObjCCategoryDecl *Category = IFace->getCategoryList(); |
| 3068 | Category; Category = Category->getNextClassCategory()) |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3069 | AddObjCProperties(Category, AllowCategories, CurContext, |
| 3070 | AddedProperties, Results); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3071 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3072 | |
| 3073 | // Look through protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 3074 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 3075 | I = IFace->all_referenced_protocol_begin(), |
| 3076 | E = IFace->all_referenced_protocol_end(); I != E; ++I) |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3077 | AddObjCProperties(*I, AllowCategories, CurContext, AddedProperties, |
| 3078 | Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3079 | |
| 3080 | // Look in the superclass. |
| 3081 | if (IFace->getSuperClass()) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3082 | AddObjCProperties(IFace->getSuperClass(), AllowCategories, CurContext, |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3083 | AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3084 | } else if (const ObjCCategoryDecl *Category |
| 3085 | = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 3086 | // Look through protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 3087 | for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(), |
| 3088 | PEnd = Category->protocol_end(); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3089 | P != PEnd; ++P) |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3090 | AddObjCProperties(*P, AllowCategories, CurContext, AddedProperties, |
| 3091 | Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3092 | } |
| 3093 | } |
| 3094 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3095 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, ExprTy *BaseE, |
| 3096 | SourceLocation OpLoc, |
| 3097 | bool IsArrow) { |
| 3098 | if (!BaseE || !CodeCompleter) |
| 3099 | return; |
| 3100 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3101 | typedef CodeCompletionResult Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3102 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3103 | Expr *Base = static_cast<Expr *>(BaseE); |
| 3104 | QualType BaseType = Base->getType(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3105 | |
| 3106 | if (IsArrow) { |
| 3107 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 3108 | BaseType = Ptr->getPointeeType(); |
| 3109 | else if (BaseType->isObjCObjectPointerType()) |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3110 | /*Do nothing*/ ; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3111 | else |
| 3112 | return; |
| 3113 | } |
| 3114 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3115 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3116 | CodeCompletionContext(CodeCompletionContext::CCC_MemberAccess, |
| 3117 | BaseType), |
| 3118 | &ResultBuilder::IsMember); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3119 | Results.EnterNewScope(); |
| 3120 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3121 | // Indicate that we are performing a member access, and the cv-qualifiers |
| 3122 | // for the base object type. |
| 3123 | Results.setObjectTypeQualifiers(BaseType.getQualifiers()); |
| 3124 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3125 | // Access to a C/C++ class, struct, or union. |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 3126 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3127 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3128 | LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer, |
| 3129 | CodeCompleter->includeGlobals()); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3130 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3131 | if (getLangOptions().CPlusPlus) { |
| 3132 | if (!Results.empty()) { |
| 3133 | // The "template" keyword can follow "->" or "." in the grammar. |
| 3134 | // However, we only want to suggest the template keyword if something |
| 3135 | // is dependent. |
| 3136 | bool IsDependent = BaseType->isDependentType(); |
| 3137 | if (!IsDependent) { |
| 3138 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) |
| 3139 | if (DeclContext *Ctx = (DeclContext *)DepScope->getEntity()) { |
| 3140 | IsDependent = Ctx->isDependentContext(); |
| 3141 | break; |
| 3142 | } |
| 3143 | } |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3144 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3145 | if (IsDependent) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3146 | Results.AddResult(Result("template")); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3147 | } |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3148 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3149 | } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) { |
| 3150 | // Objective-C property reference. |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3151 | AddedPropertiesSet AddedProperties; |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3152 | |
| 3153 | // Add property results based on our interface. |
| 3154 | const ObjCObjectPointerType *ObjCPtr |
| 3155 | = BaseType->getAsObjCInterfacePointerType(); |
| 3156 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!"); |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3157 | AddObjCProperties(ObjCPtr->getInterfaceDecl(), true, CurContext, |
| 3158 | AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3159 | |
| 3160 | // Add properties from the protocols in a qualified interface. |
| 3161 | for (ObjCObjectPointerType::qual_iterator I = ObjCPtr->qual_begin(), |
| 3162 | E = ObjCPtr->qual_end(); |
| 3163 | I != E; ++I) |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3164 | AddObjCProperties(*I, true, CurContext, AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3165 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3166 | (!IsArrow && BaseType->isObjCObjectType())) { |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3167 | // Objective-C instance variable access. |
| 3168 | ObjCInterfaceDecl *Class = 0; |
| 3169 | if (const ObjCObjectPointerType *ObjCPtr |
| 3170 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 3171 | Class = ObjCPtr->getInterfaceDecl(); |
| 3172 | else |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3173 | Class = BaseType->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3174 | |
| 3175 | // Add all ivars from this class and its superclasses. |
Douglas Gregor | 80f4f4c | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 3176 | if (Class) { |
| 3177 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3178 | Results.setFilter(&ResultBuilder::IsObjCIvar); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3179 | LookupVisibleDecls(Class, LookupMemberName, Consumer, |
| 3180 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3181 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3182 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3183 | |
| 3184 | // FIXME: How do we cope with isa? |
| 3185 | |
| 3186 | Results.ExitScope(); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3187 | |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3188 | // Hand off the results found for code completion. |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3189 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3190 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3191 | Results.data(),Results.size()); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3194 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { |
| 3195 | if (!CodeCompleter) |
| 3196 | return; |
| 3197 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3198 | typedef CodeCompletionResult Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3199 | ResultBuilder::LookupFilter Filter = 0; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3200 | enum CodeCompletionContext::Kind ContextKind |
| 3201 | = CodeCompletionContext::CCC_Other; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3202 | switch ((DeclSpec::TST)TagSpec) { |
| 3203 | case DeclSpec::TST_enum: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3204 | Filter = &ResultBuilder::IsEnum; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3205 | ContextKind = CodeCompletionContext::CCC_EnumTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3206 | break; |
| 3207 | |
| 3208 | case DeclSpec::TST_union: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3209 | Filter = &ResultBuilder::IsUnion; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3210 | ContextKind = CodeCompletionContext::CCC_UnionTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3211 | break; |
| 3212 | |
| 3213 | case DeclSpec::TST_struct: |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3214 | case DeclSpec::TST_class: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3215 | Filter = &ResultBuilder::IsClassOrStruct; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3216 | ContextKind = CodeCompletionContext::CCC_ClassOrStructTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3217 | break; |
| 3218 | |
| 3219 | default: |
| 3220 | assert(false && "Unknown type specifier kind in CodeCompleteTag"); |
| 3221 | return; |
| 3222 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3223 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3224 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), ContextKind); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3225 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 3226 | |
| 3227 | // First pass: look for tags. |
| 3228 | Results.setFilter(Filter); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3229 | LookupVisibleDecls(S, LookupTagName, Consumer, |
| 3230 | CodeCompleter->includeGlobals()); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 3231 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3232 | if (CodeCompleter->includeGlobals()) { |
| 3233 | // Second pass: look for nested name specifiers. |
| 3234 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
| 3235 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer); |
| 3236 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3237 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3238 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3239 | Results.data(),Results.size()); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3242 | void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3243 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3244 | CodeCompletionContext::CCC_TypeQualifiers); |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3245 | Results.EnterNewScope(); |
| 3246 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const)) |
| 3247 | Results.AddResult("const"); |
| 3248 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile)) |
| 3249 | Results.AddResult("volatile"); |
| 3250 | if (getLangOptions().C99 && |
| 3251 | !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict)) |
| 3252 | Results.AddResult("restrict"); |
| 3253 | Results.ExitScope(); |
| 3254 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3255 | Results.getCompletionContext(), |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3256 | Results.data(), Results.size()); |
| 3257 | } |
| 3258 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3259 | void Sema::CodeCompleteCase(Scope *S) { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3260 | if (getCurFunction()->SwitchStack.empty() || !CodeCompleter) |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3261 | return; |
| 3262 | |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3263 | SwitchStmt *Switch = getCurFunction()->SwitchStack.back(); |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3264 | if (!Switch->getCond()->getType()->isEnumeralType()) { |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3265 | CodeCompleteExpressionData Data(Switch->getCond()->getType()); |
| 3266 | Data.IntegralConstantExpression = true; |
| 3267 | CodeCompleteExpression(S, Data); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3268 | return; |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3269 | } |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3270 | |
| 3271 | // Code-complete the cases of a switch statement over an enumeration type |
| 3272 | // by providing the list of |
| 3273 | EnumDecl *Enum = Switch->getCond()->getType()->getAs<EnumType>()->getDecl(); |
| 3274 | |
| 3275 | // Determine which enumerators we have already seen in the switch statement. |
| 3276 | // FIXME: Ideally, we would also be able to look *past* the code-completion |
| 3277 | // token, in case we are code-completing in the middle of the switch and not |
| 3278 | // at the end. However, we aren't able to do so at the moment. |
| 3279 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3280 | NestedNameSpecifier *Qualifier = 0; |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3281 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; |
| 3282 | SC = SC->getNextSwitchCase()) { |
| 3283 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); |
| 3284 | if (!Case) |
| 3285 | continue; |
| 3286 | |
| 3287 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); |
| 3288 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal)) |
| 3289 | if (EnumConstantDecl *Enumerator |
| 3290 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 3291 | // We look into the AST of the case statement to determine which |
| 3292 | // enumerator was named. Alternatively, we could compute the value of |
| 3293 | // the integral constant expression, then compare it against the |
| 3294 | // values of each enumerator. However, value-based approach would not |
| 3295 | // work as well with C++ templates where enumerators declared within a |
| 3296 | // template are type- and value-dependent. |
| 3297 | EnumeratorsSeen.insert(Enumerator); |
| 3298 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3299 | // If this is a qualified-id, keep track of the nested-name-specifier |
| 3300 | // 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] | 3301 | // |
| 3302 | // switch (TagD.getKind()) { |
| 3303 | // case TagDecl::TK_enum: |
| 3304 | // break; |
| 3305 | // case XXX |
| 3306 | // |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3307 | // At the XXX, our completions are TagDecl::TK_union, |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3308 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, |
| 3309 | // TK_struct, and TK_class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3310 | Qualifier = DRE->getQualifier(); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3311 | } |
| 3312 | } |
| 3313 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3314 | if (getLangOptions().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { |
| 3315 | // If there are no prior enumerators in C++, check whether we have to |
| 3316 | // qualify the names of the enumerators that we suggest, because they |
| 3317 | // may not be visible in this scope. |
| 3318 | Qualifier = getRequiredQualification(Context, CurContext, |
| 3319 | Enum->getDeclContext()); |
| 3320 | |
| 3321 | // FIXME: Scoped enums need to start with "EnumDecl" as the context! |
| 3322 | } |
| 3323 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3324 | // Add any enumerators that have not yet been mentioned. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3325 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3326 | CodeCompletionContext::CCC_Expression); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3327 | Results.EnterNewScope(); |
| 3328 | for (EnumDecl::enumerator_iterator E = Enum->enumerator_begin(), |
| 3329 | EEnd = Enum->enumerator_end(); |
| 3330 | E != EEnd; ++E) { |
| 3331 | if (EnumeratorsSeen.count(*E)) |
| 3332 | continue; |
| 3333 | |
Douglas Gregor | 5c722c70 | 2011-02-18 23:30:37 +0000 | [diff] [blame^] | 3334 | CodeCompletionResult R(*E, Qualifier); |
| 3335 | R.Priority = CCP_EnumInCase; |
| 3336 | Results.AddResult(R, CurContext, 0, false); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3337 | } |
| 3338 | Results.ExitScope(); |
Douglas Gregor | 2f880e4 | 2010-04-06 20:02:15 +0000 | [diff] [blame] | 3339 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3340 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3341 | AddMacroResults(PP, Results); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3342 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 5c722c70 | 2011-02-18 23:30:37 +0000 | [diff] [blame^] | 3343 | CodeCompletionContext::CCC_OtherWithMacros, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3344 | Results.data(),Results.size()); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3347 | namespace { |
| 3348 | struct IsBetterOverloadCandidate { |
| 3349 | Sema &S; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3350 | SourceLocation Loc; |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3351 | |
| 3352 | public: |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3353 | explicit IsBetterOverloadCandidate(Sema &S, SourceLocation Loc) |
| 3354 | : S(S), Loc(Loc) { } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3355 | |
| 3356 | bool |
| 3357 | operator()(const OverloadCandidate &X, const OverloadCandidate &Y) const { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3358 | return isBetterOverloadCandidate(S, X, Y, Loc); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3359 | } |
| 3360 | }; |
| 3361 | } |
| 3362 | |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3363 | static bool anyNullArguments(Expr **Args, unsigned NumArgs) { |
| 3364 | if (NumArgs && !Args) |
| 3365 | return true; |
| 3366 | |
| 3367 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3368 | if (!Args[I]) |
| 3369 | return true; |
| 3370 | |
| 3371 | return false; |
| 3372 | } |
| 3373 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3374 | void Sema::CodeCompleteCall(Scope *S, ExprTy *FnIn, |
| 3375 | ExprTy **ArgsIn, unsigned NumArgs) { |
| 3376 | if (!CodeCompleter) |
| 3377 | return; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3378 | |
| 3379 | // When we're code-completing for a call, we fall back to ordinary |
| 3380 | // name code-completion whenever we can't produce specific |
| 3381 | // results. We may want to revisit this strategy in the future, |
| 3382 | // e.g., by merging the two kinds of results. |
| 3383 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3384 | Expr *Fn = (Expr *)FnIn; |
| 3385 | Expr **Args = (Expr **)ArgsIn; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3386 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3387 | // Ignore type-dependent call expressions entirely. |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3388 | if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args, NumArgs) || |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3389 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3390 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3391 | return; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3392 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3393 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3394 | // Build an overload candidate set based on the functions we find. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3395 | SourceLocation Loc = Fn->getExprLoc(); |
| 3396 | OverloadCandidateSet CandidateSet(Loc); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3397 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3398 | // FIXME: What if we're calling something that isn't a function declaration? |
| 3399 | // FIXME: What if we're calling a pseudo-destructor? |
| 3400 | // FIXME: What if we're calling a member function? |
| 3401 | |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3402 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; |
| 3403 | llvm::SmallVector<ResultCandidate, 8> Results; |
| 3404 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3405 | Expr *NakedFn = Fn->IgnoreParenCasts(); |
| 3406 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) |
| 3407 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet, |
| 3408 | /*PartialOverloading=*/ true); |
| 3409 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(NakedFn)) { |
| 3410 | FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3411 | if (FDecl) { |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3412 | if (!getLangOptions().CPlusPlus || |
| 3413 | !FDecl->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3414 | Results.push_back(ResultCandidate(FDecl)); |
| 3415 | else |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3416 | // FIXME: access? |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3417 | AddOverloadCandidate(FDecl, DeclAccessPair::make(FDecl, AS_none), |
| 3418 | Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 3419 | false, /*PartialOverloading*/true); |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3420 | } |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3421 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3422 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3423 | QualType ParamType; |
| 3424 | |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3425 | if (!CandidateSet.empty()) { |
| 3426 | // Sort the overload candidate set by placing the best overloads first. |
| 3427 | std::stable_sort(CandidateSet.begin(), CandidateSet.end(), |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3428 | IsBetterOverloadCandidate(*this, Loc)); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3429 | |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3430 | // Add the remaining viable overload candidates as code-completion reslults. |
| 3431 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3432 | CandEnd = CandidateSet.end(); |
| 3433 | Cand != CandEnd; ++Cand) { |
| 3434 | if (Cand->Viable) |
| 3435 | Results.push_back(ResultCandidate(Cand->Function)); |
| 3436 | } |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3437 | |
| 3438 | // From the viable candidates, try to determine the type of this parameter. |
| 3439 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 3440 | if (const FunctionType *FType = Results[I].getFunctionType()) |
| 3441 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FType)) |
| 3442 | if (NumArgs < Proto->getNumArgs()) { |
| 3443 | if (ParamType.isNull()) |
| 3444 | ParamType = Proto->getArgType(NumArgs); |
| 3445 | else if (!Context.hasSameUnqualifiedType( |
| 3446 | ParamType.getNonReferenceType(), |
| 3447 | Proto->getArgType(NumArgs).getNonReferenceType())) { |
| 3448 | ParamType = QualType(); |
| 3449 | break; |
| 3450 | } |
| 3451 | } |
| 3452 | } |
| 3453 | } else { |
| 3454 | // Try to determine the parameter type from the type of the expression |
| 3455 | // being called. |
| 3456 | QualType FunctionType = Fn->getType(); |
| 3457 | if (const PointerType *Ptr = FunctionType->getAs<PointerType>()) |
| 3458 | FunctionType = Ptr->getPointeeType(); |
| 3459 | else if (const BlockPointerType *BlockPtr |
| 3460 | = FunctionType->getAs<BlockPointerType>()) |
| 3461 | FunctionType = BlockPtr->getPointeeType(); |
| 3462 | else if (const MemberPointerType *MemPtr |
| 3463 | = FunctionType->getAs<MemberPointerType>()) |
| 3464 | FunctionType = MemPtr->getPointeeType(); |
| 3465 | |
| 3466 | if (const FunctionProtoType *Proto |
| 3467 | = FunctionType->getAs<FunctionProtoType>()) { |
| 3468 | if (NumArgs < Proto->getNumArgs()) |
| 3469 | ParamType = Proto->getArgType(NumArgs); |
| 3470 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3471 | } |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3472 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3473 | if (ParamType.isNull()) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3474 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3475 | else |
| 3476 | CodeCompleteExpression(S, ParamType); |
| 3477 | |
Douglas Gregor | 2e4c7a5 | 2010-04-06 20:19:47 +0000 | [diff] [blame] | 3478 | if (!Results.empty()) |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3479 | CodeCompleter->ProcessOverloadCandidates(*this, NumArgs, Results.data(), |
| 3480 | Results.size()); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3483 | void Sema::CodeCompleteInitializer(Scope *S, Decl *D) { |
| 3484 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3485 | if (!VD) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3486 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3487 | return; |
| 3488 | } |
| 3489 | |
| 3490 | CodeCompleteExpression(S, VD->getType()); |
| 3491 | } |
| 3492 | |
| 3493 | void Sema::CodeCompleteReturn(Scope *S) { |
| 3494 | QualType ResultType; |
| 3495 | if (isa<BlockDecl>(CurContext)) { |
| 3496 | if (BlockScopeInfo *BSI = getCurBlock()) |
| 3497 | ResultType = BSI->ReturnType; |
| 3498 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext)) |
| 3499 | ResultType = Function->getResultType(); |
| 3500 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext)) |
| 3501 | ResultType = Method->getResultType(); |
| 3502 | |
| 3503 | if (ResultType.isNull()) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3504 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3505 | else |
| 3506 | CodeCompleteExpression(S, ResultType); |
| 3507 | } |
| 3508 | |
| 3509 | void Sema::CodeCompleteAssignmentRHS(Scope *S, ExprTy *LHS) { |
| 3510 | if (LHS) |
| 3511 | CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType()); |
| 3512 | else |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3513 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3514 | } |
| 3515 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3516 | void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3517 | bool EnteringContext) { |
| 3518 | if (!SS.getScopeRep() || !CodeCompleter) |
| 3519 | return; |
| 3520 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3521 | DeclContext *Ctx = computeDeclContext(SS, EnteringContext); |
| 3522 | if (!Ctx) |
| 3523 | return; |
Douglas Gregor | d1cd31a | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 3524 | |
| 3525 | // Try to instantiate any non-dependent declaration contexts before |
| 3526 | // we look in them. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3527 | if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx)) |
Douglas Gregor | d1cd31a | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 3528 | return; |
| 3529 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3530 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3531 | CodeCompletionContext::CCC_Name); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3532 | Results.EnterNewScope(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3533 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3534 | // The "template" keyword can follow "::" in the grammar, but only |
| 3535 | // put it into the grammar if the nested-name-specifier is dependent. |
| 3536 | NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 3537 | if (!Results.empty() && NNS->isDependent()) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3538 | Results.AddResult("template"); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3539 | |
| 3540 | // Add calls to overridden virtual functions, if there are any. |
| 3541 | // |
| 3542 | // FIXME: This isn't wonderful, because we don't know whether we're actually |
| 3543 | // in a context that permits expressions. This is a general issue with |
| 3544 | // qualified-id completions. |
| 3545 | if (!EnteringContext) |
| 3546 | MaybeAddOverrideCalls(*this, Ctx, Results); |
| 3547 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3548 | |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3549 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3550 | LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer); |
| 3551 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3552 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3553 | CodeCompletionContext::CCC_Name, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3554 | Results.data(),Results.size()); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3555 | } |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3556 | |
| 3557 | void Sema::CodeCompleteUsing(Scope *S) { |
| 3558 | if (!CodeCompleter) |
| 3559 | return; |
| 3560 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3561 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3562 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
| 3563 | &ResultBuilder::IsNestedNameSpecifier); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3564 | Results.EnterNewScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3565 | |
| 3566 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 3567 | if (!S->isClassScope()) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3568 | Results.AddResult(CodeCompletionResult("namespace")); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3569 | |
| 3570 | // After "using", we can see anything that would start a |
| 3571 | // nested-name-specifier. |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3572 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3573 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3574 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3575 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3576 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3577 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3578 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3579 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
| 3582 | void Sema::CodeCompleteUsingDirective(Scope *S) { |
| 3583 | if (!CodeCompleter) |
| 3584 | return; |
| 3585 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3586 | // After "using namespace", we expect to see a namespace name or namespace |
| 3587 | // alias. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3588 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3589 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3590 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3591 | Results.EnterNewScope(); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3592 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3593 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3594 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3595 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3596 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3597 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3598 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { |
| 3602 | if (!CodeCompleter) |
| 3603 | return; |
| 3604 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3605 | DeclContext *Ctx = (DeclContext *)S->getEntity(); |
| 3606 | if (!S->getParent()) |
| 3607 | Ctx = Context.getTranslationUnitDecl(); |
| 3608 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3609 | bool SuppressedGlobalResults |
| 3610 | = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx); |
| 3611 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3612 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3613 | SuppressedGlobalResults |
| 3614 | ? CodeCompletionContext::CCC_Namespace |
| 3615 | : CodeCompletionContext::CCC_Other, |
| 3616 | &ResultBuilder::IsNamespace); |
| 3617 | |
| 3618 | if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3619 | // We only want to see those namespaces that have already been defined |
| 3620 | // within this scope, because its likely that the user is creating an |
| 3621 | // extended namespace declaration. Keep track of the most recent |
| 3622 | // definition of each namespace. |
| 3623 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
| 3624 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
| 3625 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); |
| 3626 | NS != NSEnd; ++NS) |
| 3627 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
| 3628 | |
| 3629 | // Add the most recent definition (or extended definition) of each |
| 3630 | // namespace to the list of results. |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3631 | Results.EnterNewScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3632 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
| 3633 | NS = OrigToLatest.begin(), NSEnd = OrigToLatest.end(); |
| 3634 | NS != NSEnd; ++NS) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3635 | Results.AddResult(CodeCompletionResult(NS->second, 0), |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 3636 | CurContext, 0, false); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3637 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3640 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3641 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3642 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3643 | } |
| 3644 | |
| 3645 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { |
| 3646 | if (!CodeCompleter) |
| 3647 | return; |
| 3648 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3649 | // After "namespace", we expect to see a namespace or alias. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3650 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3651 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3652 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3653 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3654 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3655 | CodeCompleter->includeGlobals()); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3656 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3657 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3658 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3659 | } |
| 3660 | |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3661 | void Sema::CodeCompleteOperatorName(Scope *S) { |
| 3662 | if (!CodeCompleter) |
| 3663 | return; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3664 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3665 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3666 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3667 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3668 | &ResultBuilder::IsType); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3669 | Results.EnterNewScope(); |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3670 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3671 | // Add the names of overloadable operators. |
| 3672 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 3673 | if (std::strcmp(Spelling, "?")) \ |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3674 | Results.AddResult(Result(Spelling)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3675 | #include "clang/Basic/OperatorKinds.def" |
| 3676 | |
| 3677 | // Add any type names visible from the current scope |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 3678 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3679 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3680 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3681 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3682 | |
| 3683 | // Add any type specifiers |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3684 | AddTypeSpecifierResults(getLangOptions(), Results); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3685 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3686 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3687 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3688 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3689 | Results.data(),Results.size()); |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3690 | } |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3691 | |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3692 | void Sema::CodeCompleteConstructorInitializer(Decl *ConstructorD, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3693 | CXXCtorInitializer** Initializers, |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3694 | unsigned NumInitializers) { |
| 3695 | CXXConstructorDecl *Constructor |
| 3696 | = static_cast<CXXConstructorDecl *>(ConstructorD); |
| 3697 | if (!Constructor) |
| 3698 | return; |
| 3699 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3700 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3701 | CodeCompletionContext::CCC_PotentiallyQualifiedName); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3702 | Results.EnterNewScope(); |
| 3703 | |
| 3704 | // Fill in any already-initialized fields or base classes. |
| 3705 | llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields; |
| 3706 | llvm::SmallPtrSet<CanQualType, 4> InitializedBases; |
| 3707 | for (unsigned I = 0; I != NumInitializers; ++I) { |
| 3708 | if (Initializers[I]->isBaseInitializer()) |
| 3709 | InitializedBases.insert( |
| 3710 | Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0))); |
| 3711 | else |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3712 | InitializedFields.insert(cast<FieldDecl>( |
| 3713 | Initializers[I]->getAnyMember())); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3714 | } |
| 3715 | |
| 3716 | // Add completions for base classes. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3717 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3718 | bool SawLastInitializer = (NumInitializers == 0); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3719 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 3720 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3721 | BaseEnd = ClassDecl->bases_end(); |
| 3722 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3723 | if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) { |
| 3724 | SawLastInitializer |
| 3725 | = NumInitializers > 0 && |
| 3726 | Initializers[NumInitializers - 1]->isBaseInitializer() && |
| 3727 | Context.hasSameUnqualifiedType(Base->getType(), |
| 3728 | QualType(Initializers[NumInitializers - 1]->getBaseClass(), 0)); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3729 | continue; |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3730 | } |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3731 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3732 | Builder.AddTypedTextChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3733 | Results.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3734 | Base->getType().getAsString(Context.PrintingPolicy))); |
| 3735 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3736 | Builder.AddPlaceholderChunk("args"); |
| 3737 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3738 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3739 | SawLastInitializer? CCP_NextInitializer |
| 3740 | : CCP_MemberDeclaration)); |
| 3741 | SawLastInitializer = false; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3742 | } |
| 3743 | |
| 3744 | // Add completions for virtual base classes. |
| 3745 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 3746 | BaseEnd = ClassDecl->vbases_end(); |
| 3747 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3748 | if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) { |
| 3749 | SawLastInitializer |
| 3750 | = NumInitializers > 0 && |
| 3751 | Initializers[NumInitializers - 1]->isBaseInitializer() && |
| 3752 | Context.hasSameUnqualifiedType(Base->getType(), |
| 3753 | QualType(Initializers[NumInitializers - 1]->getBaseClass(), 0)); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3754 | continue; |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3755 | } |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3756 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3757 | Builder.AddTypedTextChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3758 | Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3759 | Base->getType().getAsString(Context.PrintingPolicy))); |
| 3760 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3761 | Builder.AddPlaceholderChunk("args"); |
| 3762 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3763 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3764 | SawLastInitializer? CCP_NextInitializer |
| 3765 | : CCP_MemberDeclaration)); |
| 3766 | SawLastInitializer = false; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3767 | } |
| 3768 | |
| 3769 | // Add completions for members. |
| 3770 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 3771 | FieldEnd = ClassDecl->field_end(); |
| 3772 | Field != FieldEnd; ++Field) { |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3773 | if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl()))) { |
| 3774 | SawLastInitializer |
| 3775 | = NumInitializers > 0 && |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3776 | Initializers[NumInitializers - 1]->isAnyMemberInitializer() && |
| 3777 | Initializers[NumInitializers - 1]->getAnyMember() == *Field; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3778 | continue; |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3779 | } |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3780 | |
| 3781 | if (!Field->getDeclName()) |
| 3782 | continue; |
| 3783 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3784 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3785 | Field->getIdentifier()->getName())); |
| 3786 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3787 | Builder.AddPlaceholderChunk("args"); |
| 3788 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3789 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3790 | SawLastInitializer? CCP_NextInitializer |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3791 | : CCP_MemberDeclaration, |
| 3792 | CXCursor_MemberRef)); |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3793 | SawLastInitializer = false; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3794 | } |
| 3795 | Results.ExitScope(); |
| 3796 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3797 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3798 | Results.data(), Results.size()); |
| 3799 | } |
| 3800 | |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3801 | // Macro that expands to @Keyword or Keyword, depending on whether NeedAt is |
| 3802 | // true or false. |
| 3803 | #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) NeedAt? "@" #Keyword : #Keyword |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3804 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3805 | ResultBuilder &Results, |
| 3806 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3807 | typedef CodeCompletionResult Result; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3808 | // Since we have an implementation, we can end it. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3809 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3810 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3811 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3812 | if (LangOpts.ObjC2) { |
| 3813 | // @dynamic |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3814 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,dynamic)); |
| 3815 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3816 | Builder.AddPlaceholderChunk("property"); |
| 3817 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3818 | |
| 3819 | // @synthesize |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3820 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synthesize)); |
| 3821 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3822 | Builder.AddPlaceholderChunk("property"); |
| 3823 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3824 | } |
| 3825 | } |
| 3826 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3827 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3828 | ResultBuilder &Results, |
| 3829 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3830 | typedef CodeCompletionResult Result; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3831 | |
| 3832 | // Since we have an interface or protocol, we can end it. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3833 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3834 | |
| 3835 | if (LangOpts.ObjC2) { |
| 3836 | // @property |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3837 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,property))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3838 | |
| 3839 | // @required |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3840 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,required))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3841 | |
| 3842 | // @optional |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3843 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,optional))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3844 | } |
| 3845 | } |
| 3846 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3847 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3848 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3849 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3850 | |
| 3851 | // @class name ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3852 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,class)); |
| 3853 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3854 | Builder.AddPlaceholderChunk("name"); |
| 3855 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3856 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3857 | if (Results.includeCodePatterns()) { |
| 3858 | // @interface name |
| 3859 | // FIXME: Could introduce the whole pattern, including superclasses and |
| 3860 | // such. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3861 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,interface)); |
| 3862 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3863 | Builder.AddPlaceholderChunk("class"); |
| 3864 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3865 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3866 | // @protocol name |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3867 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol)); |
| 3868 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3869 | Builder.AddPlaceholderChunk("protocol"); |
| 3870 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3871 | |
| 3872 | // @implementation name |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3873 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,implementation)); |
| 3874 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3875 | Builder.AddPlaceholderChunk("class"); |
| 3876 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3877 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3878 | |
| 3879 | // @compatibility_alias name |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3880 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,compatibility_alias)); |
| 3881 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3882 | Builder.AddPlaceholderChunk("alias"); |
| 3883 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3884 | Builder.AddPlaceholderChunk("class"); |
| 3885 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3886 | } |
| 3887 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3888 | void Sema::CodeCompleteObjCAtDirective(Scope *S, Decl *ObjCImpDecl, |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 3889 | bool InInterface) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3890 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3891 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3892 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 3893 | Results.EnterNewScope(); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3894 | if (ObjCImpDecl) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3895 | AddObjCImplementationResults(getLangOptions(), Results, false); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3896 | else if (InInterface) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3897 | AddObjCInterfaceResults(getLangOptions(), Results, false); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3898 | else |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3899 | AddObjCTopLevelResults(Results, false); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 3900 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3901 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3902 | CodeCompletionContext::CCC_Other, |
| 3903 | Results.data(),Results.size()); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 3904 | } |
| 3905 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3906 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3907 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3908 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3909 | |
| 3910 | // @encode ( type-name ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3911 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,encode)); |
| 3912 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3913 | Builder.AddPlaceholderChunk("type-name"); |
| 3914 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3915 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3916 | |
| 3917 | // @protocol ( protocol-name ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3918 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol)); |
| 3919 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3920 | Builder.AddPlaceholderChunk("protocol-name"); |
| 3921 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3922 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3923 | |
| 3924 | // @selector ( selector ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3925 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,selector)); |
| 3926 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3927 | Builder.AddPlaceholderChunk("selector"); |
| 3928 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3929 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3932 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3933 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3934 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3935 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3936 | if (Results.includeCodePatterns()) { |
| 3937 | // @try { statements } @catch ( declaration ) { statements } @finally |
| 3938 | // { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3939 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,try)); |
| 3940 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3941 | Builder.AddPlaceholderChunk("statements"); |
| 3942 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 3943 | Builder.AddTextChunk("@catch"); |
| 3944 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3945 | Builder.AddPlaceholderChunk("parameter"); |
| 3946 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3947 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3948 | Builder.AddPlaceholderChunk("statements"); |
| 3949 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 3950 | Builder.AddTextChunk("@finally"); |
| 3951 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3952 | Builder.AddPlaceholderChunk("statements"); |
| 3953 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 3954 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3955 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3956 | |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3957 | // @throw |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3958 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,throw)); |
| 3959 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3960 | Builder.AddPlaceholderChunk("expression"); |
| 3961 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3962 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3963 | if (Results.includeCodePatterns()) { |
| 3964 | // @synchronized ( expression ) { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3965 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synchronized)); |
| 3966 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3967 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3968 | Builder.AddPlaceholderChunk("expression"); |
| 3969 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3970 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3971 | Builder.AddPlaceholderChunk("statements"); |
| 3972 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 3973 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3974 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3975 | } |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3976 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3977 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3978 | ResultBuilder &Results, |
| 3979 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3980 | typedef CodeCompletionResult Result; |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3981 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,private))); |
| 3982 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,protected))); |
| 3983 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,public))); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3984 | if (LangOpts.ObjC2) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3985 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,package))); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3986 | } |
| 3987 | |
| 3988 | void Sema::CodeCompleteObjCAtVisibility(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3989 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3990 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3991 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3992 | AddObjCVisibilityResults(getLangOptions(), Results, false); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3993 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3994 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3995 | CodeCompletionContext::CCC_Other, |
| 3996 | Results.data(),Results.size()); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3997 | } |
| 3998 | |
| 3999 | void Sema::CodeCompleteObjCAtStatement(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4000 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4001 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4002 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4003 | AddObjCStatementResults(Results, false); |
| 4004 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4005 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4006 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4007 | CodeCompletionContext::CCC_Other, |
| 4008 | Results.data(),Results.size()); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4009 | } |
| 4010 | |
| 4011 | void Sema::CodeCompleteObjCAtExpression(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4012 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4013 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4014 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4015 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4016 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4017 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4018 | CodeCompletionContext::CCC_Other, |
| 4019 | Results.data(),Results.size()); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4020 | } |
| 4021 | |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4022 | /// \brief Determine whether the addition of the given flag to an Objective-C |
| 4023 | /// property's attributes will cause a conflict. |
| 4024 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { |
| 4025 | // Check if we've already added this flag. |
| 4026 | if (Attributes & NewFlag) |
| 4027 | return true; |
| 4028 | |
| 4029 | Attributes |= NewFlag; |
| 4030 | |
| 4031 | // Check for collisions with "readonly". |
| 4032 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 4033 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | |
| 4034 | ObjCDeclSpec::DQ_PR_assign | |
| 4035 | ObjCDeclSpec::DQ_PR_copy | |
| 4036 | ObjCDeclSpec::DQ_PR_retain))) |
| 4037 | return true; |
| 4038 | |
| 4039 | // Check for more than one of { assign, copy, retain }. |
| 4040 | unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign | |
| 4041 | ObjCDeclSpec::DQ_PR_copy | |
| 4042 | ObjCDeclSpec::DQ_PR_retain); |
| 4043 | if (AssignCopyRetMask && |
| 4044 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && |
| 4045 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && |
| 4046 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain) |
| 4047 | return true; |
| 4048 | |
| 4049 | return false; |
| 4050 | } |
| 4051 | |
Douglas Gregor | a93b108 | 2009-11-18 23:08:07 +0000 | [diff] [blame] | 4052 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4053 | if (!CodeCompleter) |
| 4054 | return; |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4055 | |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4056 | unsigned Attributes = ODS.getPropertyAttributes(); |
| 4057 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4058 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4059 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4060 | CodeCompletionContext::CCC_Other); |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4061 | Results.EnterNewScope(); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4062 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4063 | Results.AddResult(CodeCompletionResult("readonly")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4064 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4065 | Results.AddResult(CodeCompletionResult("assign")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4066 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4067 | Results.AddResult(CodeCompletionResult("readwrite")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4068 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4069 | Results.AddResult(CodeCompletionResult("retain")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4070 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4071 | Results.AddResult(CodeCompletionResult("copy")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4072 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4073 | Results.AddResult(CodeCompletionResult("nonatomic")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4074 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4075 | CodeCompletionBuilder Setter(Results.getAllocator()); |
| 4076 | Setter.AddTypedTextChunk("setter"); |
| 4077 | Setter.AddTextChunk(" = "); |
| 4078 | Setter.AddPlaceholderChunk("method"); |
| 4079 | Results.AddResult(CodeCompletionResult(Setter.TakeString())); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 4080 | } |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4081 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4082 | CodeCompletionBuilder Getter(Results.getAllocator()); |
| 4083 | Getter.AddTypedTextChunk("getter"); |
| 4084 | Getter.AddTextChunk(" = "); |
| 4085 | Getter.AddPlaceholderChunk("method"); |
| 4086 | Results.AddResult(CodeCompletionResult(Getter.TakeString())); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 4087 | } |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4088 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4089 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4090 | CodeCompletionContext::CCC_Other, |
| 4091 | Results.data(),Results.size()); |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4092 | } |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4093 | |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4094 | /// \brief Descripts the kind of Objective-C method that we want to find |
| 4095 | /// via code completion. |
| 4096 | enum ObjCMethodKind { |
| 4097 | MK_Any, //< Any kind of method, provided it means other specified criteria. |
| 4098 | MK_ZeroArgSelector, //< Zero-argument (unary) selector. |
| 4099 | MK_OneArgSelector //< One-argument selector. |
| 4100 | }; |
| 4101 | |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4102 | static bool isAcceptableObjCSelector(Selector Sel, |
| 4103 | ObjCMethodKind WantKind, |
| 4104 | IdentifierInfo **SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4105 | unsigned NumSelIdents, |
| 4106 | bool AllowSameLength = true) { |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4107 | if (NumSelIdents > Sel.getNumArgs()) |
| 4108 | return false; |
| 4109 | |
| 4110 | switch (WantKind) { |
| 4111 | case MK_Any: break; |
| 4112 | case MK_ZeroArgSelector: return Sel.isUnarySelector(); |
| 4113 | case MK_OneArgSelector: return Sel.getNumArgs() == 1; |
| 4114 | } |
| 4115 | |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4116 | if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs()) |
| 4117 | return false; |
| 4118 | |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4119 | for (unsigned I = 0; I != NumSelIdents; ++I) |
| 4120 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) |
| 4121 | return false; |
| 4122 | |
| 4123 | return true; |
| 4124 | } |
| 4125 | |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4126 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, |
| 4127 | ObjCMethodKind WantKind, |
| 4128 | IdentifierInfo **SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4129 | unsigned NumSelIdents, |
| 4130 | bool AllowSameLength = true) { |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4131 | return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4132 | NumSelIdents, AllowSameLength); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4133 | } |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4134 | |
| 4135 | namespace { |
| 4136 | /// \brief A set of selectors, which is used to avoid introducing multiple |
| 4137 | /// completions with the same selector into the result set. |
| 4138 | typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet; |
| 4139 | } |
| 4140 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4141 | /// \brief Add all of the Objective-C methods in the given Objective-C |
| 4142 | /// container to the set of results. |
| 4143 | /// |
| 4144 | /// The container will be a class, protocol, category, or implementation of |
| 4145 | /// any of the above. This mether will recurse to include methods from |
| 4146 | /// the superclasses of classes along with their categories, protocols, and |
| 4147 | /// implementations. |
| 4148 | /// |
| 4149 | /// \param Container the container in which we'll look to find methods. |
| 4150 | /// |
| 4151 | /// \param WantInstance whether to add instance methods (only); if false, this |
| 4152 | /// routine will add factory methods (only). |
| 4153 | /// |
| 4154 | /// \param CurContext the context in which we're performing the lookup that |
| 4155 | /// finds methods. |
| 4156 | /// |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4157 | /// \param AllowSameLength Whether we allow a method to be added to the list |
| 4158 | /// when it has the same number of parameters as we have selector identifiers. |
| 4159 | /// |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4160 | /// \param Results the structure into which we'll add results. |
| 4161 | static void AddObjCMethods(ObjCContainerDecl *Container, |
| 4162 | bool WantInstanceMethods, |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4163 | ObjCMethodKind WantKind, |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4164 | IdentifierInfo **SelIdents, |
| 4165 | unsigned NumSelIdents, |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4166 | DeclContext *CurContext, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4167 | VisitedSelectorSet &Selectors, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4168 | bool AllowSameLength, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4169 | ResultBuilder &Results, |
| 4170 | bool InOriginalClass = true) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4171 | typedef CodeCompletionResult Result; |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4172 | for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), |
| 4173 | MEnd = Container->meth_end(); |
| 4174 | M != MEnd; ++M) { |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4175 | if ((*M)->isInstanceMethod() == WantInstanceMethods) { |
| 4176 | // Check whether the selector identifiers we've been given are a |
| 4177 | // subset of the identifiers for this particular method. |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4178 | if (!isAcceptableObjCMethod(*M, WantKind, SelIdents, NumSelIdents, |
| 4179 | AllowSameLength)) |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4180 | continue; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4181 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4182 | if (!Selectors.insert((*M)->getSelector())) |
| 4183 | continue; |
| 4184 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4185 | Result R = Result(*M, 0); |
| 4186 | R.StartParameter = NumSelIdents; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4187 | R.AllParametersAreInformative = (WantKind != MK_Any); |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4188 | if (!InOriginalClass) |
| 4189 | R.Priority += CCD_InBaseClass; |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4190 | Results.MaybeAddResult(R, CurContext); |
| 4191 | } |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
Douglas Gregor | e396c7b | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 4194 | // Visit the protocols of protocols. |
| 4195 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 4196 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 4197 | = Protocol->getReferencedProtocols(); |
| 4198 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 4199 | E = Protocols.end(); |
| 4200 | I != E; ++I) |
| 4201 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4202 | CurContext, Selectors, AllowSameLength, Results, false); |
Douglas Gregor | e396c7b | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4205 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); |
| 4206 | if (!IFace) |
| 4207 | return; |
| 4208 | |
| 4209 | // Add methods in protocols. |
| 4210 | const ObjCList<ObjCProtocolDecl> &Protocols= IFace->getReferencedProtocols(); |
| 4211 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 4212 | E = Protocols.end(); |
| 4213 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4214 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4215 | CurContext, Selectors, AllowSameLength, Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4216 | |
| 4217 | // Add methods in categories. |
| 4218 | for (ObjCCategoryDecl *CatDecl = IFace->getCategoryList(); CatDecl; |
| 4219 | CatDecl = CatDecl->getNextClassCategory()) { |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4220 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4221 | NumSelIdents, CurContext, Selectors, AllowSameLength, |
| 4222 | Results, InOriginalClass); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4223 | |
| 4224 | // Add a categories protocol methods. |
| 4225 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 4226 | = CatDecl->getReferencedProtocols(); |
| 4227 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 4228 | E = Protocols.end(); |
| 4229 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4230 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4231 | NumSelIdents, CurContext, Selectors, AllowSameLength, |
| 4232 | Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4233 | |
| 4234 | // Add methods in category implementations. |
| 4235 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4236 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4237 | NumSelIdents, CurContext, Selectors, AllowSameLength, |
| 4238 | Results, InOriginalClass); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
| 4241 | // Add methods in superclass. |
| 4242 | if (IFace->getSuperClass()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4243 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4244 | SelIdents, NumSelIdents, CurContext, Selectors, |
| 4245 | AllowSameLength, Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4246 | |
| 4247 | // Add methods in our implementation, if any. |
| 4248 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4249 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4250 | NumSelIdents, CurContext, Selectors, AllowSameLength, |
| 4251 | Results, InOriginalClass); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | |
Douglas Gregor | bdb2d50 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 4255 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S, Decl *ClassDecl) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4256 | typedef CodeCompletionResult Result; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4257 | |
| 4258 | // Try to find the interface where getters might live. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4259 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(ClassDecl); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4260 | if (!Class) { |
| 4261 | if (ObjCCategoryDecl *Category |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4262 | = dyn_cast_or_null<ObjCCategoryDecl>(ClassDecl)) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4263 | Class = Category->getClassInterface(); |
| 4264 | |
| 4265 | if (!Class) |
| 4266 | return; |
| 4267 | } |
| 4268 | |
| 4269 | // Find all of the potential getters. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4270 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4271 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4272 | Results.EnterNewScope(); |
| 4273 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4274 | VisitedSelectorSet Selectors; |
| 4275 | AddObjCMethods(Class, true, MK_ZeroArgSelector, 0, 0, CurContext, Selectors, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4276 | /*AllowSameLength=*/true, Results); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4277 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4278 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4279 | CodeCompletionContext::CCC_Other, |
| 4280 | Results.data(),Results.size()); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4281 | } |
| 4282 | |
Douglas Gregor | bdb2d50 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 4283 | void Sema::CodeCompleteObjCPropertySetter(Scope *S, Decl *ObjCImplDecl) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4284 | typedef CodeCompletionResult Result; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4285 | |
| 4286 | // Try to find the interface where setters might live. |
| 4287 | ObjCInterfaceDecl *Class |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4288 | = dyn_cast_or_null<ObjCInterfaceDecl>(ObjCImplDecl); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4289 | if (!Class) { |
| 4290 | if (ObjCCategoryDecl *Category |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4291 | = dyn_cast_or_null<ObjCCategoryDecl>(ObjCImplDecl)) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4292 | Class = Category->getClassInterface(); |
| 4293 | |
| 4294 | if (!Class) |
| 4295 | return; |
| 4296 | } |
| 4297 | |
| 4298 | // Find all of the potential getters. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4299 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4300 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4301 | Results.EnterNewScope(); |
| 4302 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4303 | VisitedSelectorSet Selectors; |
| 4304 | AddObjCMethods(Class, true, MK_OneArgSelector, 0, 0, CurContext, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4305 | Selectors, /*AllowSameLength=*/true, Results); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4306 | |
| 4307 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4308 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4309 | CodeCompletionContext::CCC_Other, |
| 4310 | Results.data(),Results.size()); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
Douglas Gregor | afc4578 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 4313 | void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS, |
| 4314 | bool IsParameter) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4315 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4316 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4317 | CodeCompletionContext::CCC_Type); |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 4318 | Results.EnterNewScope(); |
| 4319 | |
| 4320 | // Add context-sensitive, Objective-C parameter-passing keywords. |
| 4321 | bool AddedInOut = false; |
| 4322 | if ((DS.getObjCDeclQualifier() & |
| 4323 | (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 4324 | Results.AddResult("in"); |
| 4325 | Results.AddResult("inout"); |
| 4326 | AddedInOut = true; |
| 4327 | } |
| 4328 | if ((DS.getObjCDeclQualifier() & |
| 4329 | (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 4330 | Results.AddResult("out"); |
| 4331 | if (!AddedInOut) |
| 4332 | Results.AddResult("inout"); |
| 4333 | } |
| 4334 | if ((DS.getObjCDeclQualifier() & |
| 4335 | (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref | |
| 4336 | ObjCDeclSpec::DQ_Oneway)) == 0) { |
| 4337 | Results.AddResult("bycopy"); |
| 4338 | Results.AddResult("byref"); |
| 4339 | Results.AddResult("oneway"); |
| 4340 | } |
| 4341 | |
Douglas Gregor | afc4578 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 4342 | // If we're completing the return type of an Objective-C method and the |
| 4343 | // identifier IBAction refers to a macro, provide a completion item for |
| 4344 | // an action, e.g., |
| 4345 | // IBAction)<#selector#>:(id)sender |
| 4346 | if (DS.getObjCDeclQualifier() == 0 && !IsParameter && |
| 4347 | Context.Idents.get("IBAction").hasMacroDefinition()) { |
| 4348 | typedef CodeCompletionString::Chunk Chunk; |
| 4349 | CodeCompletionBuilder Builder(Results.getAllocator(), CCP_CodePattern, |
| 4350 | CXAvailability_Available); |
| 4351 | Builder.AddTypedTextChunk("IBAction"); |
| 4352 | Builder.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
| 4353 | Builder.AddPlaceholderChunk("selector"); |
| 4354 | Builder.AddChunk(Chunk(CodeCompletionString::CK_Colon)); |
| 4355 | Builder.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
| 4356 | Builder.AddTextChunk("id"); |
| 4357 | Builder.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
| 4358 | Builder.AddTextChunk("sender"); |
| 4359 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 4360 | } |
| 4361 | |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 4362 | // Add various builtin type names and specifiers. |
| 4363 | AddOrdinaryNameResults(PCC_Type, S, *this, Results); |
| 4364 | Results.ExitScope(); |
| 4365 | |
| 4366 | // Add the various type names |
| 4367 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 4368 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4369 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4370 | CodeCompleter->includeGlobals()); |
| 4371 | |
| 4372 | if (CodeCompleter->includeMacros()) |
| 4373 | AddMacroResults(PP, Results); |
| 4374 | |
| 4375 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4376 | CodeCompletionContext::CCC_Type, |
| 4377 | Results.data(), Results.size()); |
| 4378 | } |
| 4379 | |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4380 | /// \brief When we have an expression with type "id", we may assume |
| 4381 | /// that it has some more-specific class type based on knowledge of |
| 4382 | /// common uses of Objective-C. This routine returns that class type, |
| 4383 | /// or NULL if no better result could be determined. |
| 4384 | static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) { |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 4385 | ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E); |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4386 | if (!Msg) |
| 4387 | return 0; |
| 4388 | |
| 4389 | Selector Sel = Msg->getSelector(); |
| 4390 | if (Sel.isNull()) |
| 4391 | return 0; |
| 4392 | |
| 4393 | IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0); |
| 4394 | if (!Id) |
| 4395 | return 0; |
| 4396 | |
| 4397 | ObjCMethodDecl *Method = Msg->getMethodDecl(); |
| 4398 | if (!Method) |
| 4399 | return 0; |
| 4400 | |
| 4401 | // Determine the class that we're sending the message to. |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 4402 | ObjCInterfaceDecl *IFace = 0; |
| 4403 | switch (Msg->getReceiverKind()) { |
| 4404 | case ObjCMessageExpr::Class: |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4405 | if (const ObjCObjectType *ObjType |
| 4406 | = Msg->getClassReceiver()->getAs<ObjCObjectType>()) |
| 4407 | IFace = ObjType->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 4408 | break; |
| 4409 | |
| 4410 | case ObjCMessageExpr::Instance: { |
| 4411 | QualType T = Msg->getInstanceReceiver()->getType(); |
| 4412 | if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) |
| 4413 | IFace = Ptr->getInterfaceDecl(); |
| 4414 | break; |
| 4415 | } |
| 4416 | |
| 4417 | case ObjCMessageExpr::SuperInstance: |
| 4418 | case ObjCMessageExpr::SuperClass: |
| 4419 | break; |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4420 | } |
| 4421 | |
| 4422 | if (!IFace) |
| 4423 | return 0; |
| 4424 | |
| 4425 | ObjCInterfaceDecl *Super = IFace->getSuperClass(); |
| 4426 | if (Method->isInstanceMethod()) |
| 4427 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 4428 | .Case("retain", IFace) |
| 4429 | .Case("autorelease", IFace) |
| 4430 | .Case("copy", IFace) |
| 4431 | .Case("copyWithZone", IFace) |
| 4432 | .Case("mutableCopy", IFace) |
| 4433 | .Case("mutableCopyWithZone", IFace) |
| 4434 | .Case("awakeFromCoder", IFace) |
| 4435 | .Case("replacementObjectFromCoder", IFace) |
| 4436 | .Case("class", IFace) |
| 4437 | .Case("classForCoder", IFace) |
| 4438 | .Case("superclass", Super) |
| 4439 | .Default(0); |
| 4440 | |
| 4441 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 4442 | .Case("new", IFace) |
| 4443 | .Case("alloc", IFace) |
| 4444 | .Case("allocWithZone", IFace) |
| 4445 | .Case("class", IFace) |
| 4446 | .Case("superclass", Super) |
| 4447 | .Default(0); |
| 4448 | } |
| 4449 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4450 | // Add a special completion for a message send to "super", which fills in the |
| 4451 | // most likely case of forwarding all of our arguments to the superclass |
| 4452 | // function. |
| 4453 | /// |
| 4454 | /// \param S The semantic analysis object. |
| 4455 | /// |
| 4456 | /// \param S NeedSuperKeyword Whether we need to prefix this completion with |
| 4457 | /// the "super" keyword. Otherwise, we just need to provide the arguments. |
| 4458 | /// |
| 4459 | /// \param SelIdents The identifiers in the selector that have already been |
| 4460 | /// provided as arguments for a send to "super". |
| 4461 | /// |
| 4462 | /// \param NumSelIdents The number of identifiers in \p SelIdents. |
| 4463 | /// |
| 4464 | /// \param Results The set of results to augment. |
| 4465 | /// |
| 4466 | /// \returns the Objective-C method declaration that would be invoked by |
| 4467 | /// this "super" completion. If NULL, no completion was added. |
| 4468 | static ObjCMethodDecl *AddSuperSendCompletion(Sema &S, bool NeedSuperKeyword, |
| 4469 | IdentifierInfo **SelIdents, |
| 4470 | unsigned NumSelIdents, |
| 4471 | ResultBuilder &Results) { |
| 4472 | ObjCMethodDecl *CurMethod = S.getCurMethodDecl(); |
| 4473 | if (!CurMethod) |
| 4474 | return 0; |
| 4475 | |
| 4476 | ObjCInterfaceDecl *Class = CurMethod->getClassInterface(); |
| 4477 | if (!Class) |
| 4478 | return 0; |
| 4479 | |
| 4480 | // Try to find a superclass method with the same selector. |
| 4481 | ObjCMethodDecl *SuperMethod = 0; |
Douglas Gregor | 78bcd91 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 4482 | while ((Class = Class->getSuperClass()) && !SuperMethod) { |
| 4483 | // Check in the class |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4484 | SuperMethod = Class->getMethod(CurMethod->getSelector(), |
| 4485 | CurMethod->isInstanceMethod()); |
| 4486 | |
Douglas Gregor | 78bcd91 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 4487 | // Check in categories or class extensions. |
| 4488 | if (!SuperMethod) { |
| 4489 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; |
| 4490 | Category = Category->getNextClassCategory()) |
| 4491 | if ((SuperMethod = Category->getMethod(CurMethod->getSelector(), |
| 4492 | CurMethod->isInstanceMethod()))) |
| 4493 | break; |
| 4494 | } |
| 4495 | } |
| 4496 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4497 | if (!SuperMethod) |
| 4498 | return 0; |
| 4499 | |
| 4500 | // Check whether the superclass method has the same signature. |
| 4501 | if (CurMethod->param_size() != SuperMethod->param_size() || |
| 4502 | CurMethod->isVariadic() != SuperMethod->isVariadic()) |
| 4503 | return 0; |
| 4504 | |
| 4505 | for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(), |
| 4506 | CurPEnd = CurMethod->param_end(), |
| 4507 | SuperP = SuperMethod->param_begin(); |
| 4508 | CurP != CurPEnd; ++CurP, ++SuperP) { |
| 4509 | // Make sure the parameter types are compatible. |
| 4510 | if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(), |
| 4511 | (*SuperP)->getType())) |
| 4512 | return 0; |
| 4513 | |
| 4514 | // Make sure we have a parameter name to forward! |
| 4515 | if (!(*CurP)->getIdentifier()) |
| 4516 | return 0; |
| 4517 | } |
| 4518 | |
| 4519 | // We have a superclass method. Now, form the send-to-super completion. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4520 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4521 | |
| 4522 | // Give this completion a return type. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4523 | AddResultTypeChunk(S.Context, SuperMethod, Builder); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4524 | |
| 4525 | // If we need the "super" keyword, add it (plus some spacing). |
| 4526 | if (NeedSuperKeyword) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4527 | Builder.AddTypedTextChunk("super"); |
| 4528 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4529 | } |
| 4530 | |
| 4531 | Selector Sel = CurMethod->getSelector(); |
| 4532 | if (Sel.isUnarySelector()) { |
| 4533 | if (NeedSuperKeyword) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4534 | Builder.AddTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 4535 | Sel.getNameForSlot(0))); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4536 | else |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4537 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 4538 | Sel.getNameForSlot(0))); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4539 | } else { |
| 4540 | ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(); |
| 4541 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) { |
| 4542 | if (I > NumSelIdents) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4543 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4544 | |
| 4545 | if (I < NumSelIdents) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4546 | Builder.AddInformativeChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4547 | Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 4548 | Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4549 | else if (NeedSuperKeyword || I > NumSelIdents) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4550 | Builder.AddTextChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4551 | Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 4552 | Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4553 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4554 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4555 | } else { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4556 | Builder.AddTypedTextChunk( |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4557 | Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 4558 | Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4559 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4560 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4561 | } |
| 4562 | } |
| 4563 | } |
| 4564 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4565 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), CCP_SuperCompletion, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4566 | SuperMethod->isInstanceMethod() |
| 4567 | ? CXCursor_ObjCInstanceMethodDecl |
| 4568 | : CXCursor_ObjCClassMethodDecl)); |
| 4569 | return SuperMethod; |
| 4570 | } |
| 4571 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4572 | void Sema::CodeCompleteObjCMessageReceiver(Scope *S) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4573 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4574 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4575 | CodeCompletionContext::CCC_ObjCMessageReceiver, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4576 | &ResultBuilder::IsObjCMessageReceiver); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4577 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4578 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4579 | Results.EnterNewScope(); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4580 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4581 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4582 | |
| 4583 | // If we are in an Objective-C method inside a class that has a superclass, |
| 4584 | // add "super" as an option. |
| 4585 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 4586 | if (ObjCInterfaceDecl *Iface = Method->getClassInterface()) |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4587 | if (Iface->getSuperClass()) { |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4588 | Results.AddResult(Result("super")); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4589 | |
| 4590 | AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, 0, 0, Results); |
| 4591 | } |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4592 | |
| 4593 | Results.ExitScope(); |
| 4594 | |
| 4595 | if (CodeCompleter->includeMacros()) |
| 4596 | AddMacroResults(PP, Results); |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 4597 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4598 | Results.data(), Results.size()); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4599 | |
| 4600 | } |
| 4601 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4602 | void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, |
| 4603 | IdentifierInfo **SelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4604 | unsigned NumSelIdents, |
| 4605 | bool AtArgumentExpression) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4606 | ObjCInterfaceDecl *CDecl = 0; |
| 4607 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 4608 | // Figure out which interface we're in. |
| 4609 | CDecl = CurMethod->getClassInterface(); |
| 4610 | if (!CDecl) |
| 4611 | return; |
| 4612 | |
| 4613 | // Find the superclass of this class. |
| 4614 | CDecl = CDecl->getSuperClass(); |
| 4615 | if (!CDecl) |
| 4616 | return; |
| 4617 | |
| 4618 | if (CurMethod->isInstanceMethod()) { |
| 4619 | // We are inside an instance method, which means that the message |
| 4620 | // send [super ...] is actually calling an instance method on the |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 4621 | // current object. |
| 4622 | return CodeCompleteObjCInstanceMessage(S, 0, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4623 | SelIdents, NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4624 | AtArgumentExpression, |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 4625 | CDecl); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4626 | } |
| 4627 | |
| 4628 | // Fall through to send to the superclass in CDecl. |
| 4629 | } else { |
| 4630 | // "super" may be the name of a type or variable. Figure out which |
| 4631 | // it is. |
| 4632 | IdentifierInfo *Super = &Context.Idents.get("super"); |
| 4633 | NamedDecl *ND = LookupSingleName(S, Super, SuperLoc, |
| 4634 | LookupOrdinaryName); |
| 4635 | if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) { |
| 4636 | // "super" names an interface. Use it. |
| 4637 | } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4638 | if (const ObjCObjectType *Iface |
| 4639 | = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>()) |
| 4640 | CDecl = Iface->getInterface(); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4641 | } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) { |
| 4642 | // "super" names an unresolved type; we can't be more specific. |
| 4643 | } else { |
| 4644 | // Assume that "super" names some kind of value and parse that way. |
| 4645 | CXXScopeSpec SS; |
| 4646 | UnqualifiedId id; |
| 4647 | id.setIdentifier(Super, SuperLoc); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4648 | ExprResult SuperExpr = ActOnIdExpression(S, SS, id, false, false); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4649 | return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(), |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4650 | SelIdents, NumSelIdents, |
| 4651 | AtArgumentExpression); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
| 4654 | // Fall through |
| 4655 | } |
| 4656 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4657 | ParsedType Receiver; |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4658 | if (CDecl) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4659 | Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4660 | return CodeCompleteObjCClassMessage(S, Receiver, SelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4661 | NumSelIdents, AtArgumentExpression, |
| 4662 | /*IsSuper=*/true); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4663 | } |
| 4664 | |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 4665 | /// \brief Given a set of code-completion results for the argument of a message |
| 4666 | /// send, determine the preferred type (if any) for that argument expression. |
| 4667 | static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results, |
| 4668 | unsigned NumSelIdents) { |
| 4669 | typedef CodeCompletionResult Result; |
| 4670 | ASTContext &Context = Results.getSema().Context; |
| 4671 | |
| 4672 | QualType PreferredType; |
| 4673 | unsigned BestPriority = CCP_Unlikely * 2; |
| 4674 | Result *ResultsData = Results.data(); |
| 4675 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 4676 | Result &R = ResultsData[I]; |
| 4677 | if (R.Kind == Result::RK_Declaration && |
| 4678 | isa<ObjCMethodDecl>(R.Declaration)) { |
| 4679 | if (R.Priority <= BestPriority) { |
| 4680 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration); |
| 4681 | if (NumSelIdents <= Method->param_size()) { |
| 4682 | QualType MyPreferredType = Method->param_begin()[NumSelIdents - 1] |
| 4683 | ->getType(); |
| 4684 | if (R.Priority < BestPriority || PreferredType.isNull()) { |
| 4685 | BestPriority = R.Priority; |
| 4686 | PreferredType = MyPreferredType; |
| 4687 | } else if (!Context.hasSameUnqualifiedType(PreferredType, |
| 4688 | MyPreferredType)) { |
| 4689 | PreferredType = QualType(); |
| 4690 | } |
| 4691 | } |
| 4692 | } |
| 4693 | } |
| 4694 | } |
| 4695 | |
| 4696 | return PreferredType; |
| 4697 | } |
| 4698 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4699 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| 4700 | ParsedType Receiver, |
| 4701 | IdentifierInfo **SelIdents, |
| 4702 | unsigned NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4703 | bool AtArgumentExpression, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4704 | bool IsSuper, |
| 4705 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4706 | typedef CodeCompletionResult Result; |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4707 | ObjCInterfaceDecl *CDecl = 0; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4708 | |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4709 | // If the given name refers to an interface type, retrieve the |
| 4710 | // corresponding declaration. |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4711 | if (Receiver) { |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4712 | QualType T = SemaRef.GetTypeFromParser(Receiver, 0); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4713 | if (!T.isNull()) |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4714 | if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>()) |
| 4715 | CDecl = Interface->getInterface(); |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4716 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4717 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4718 | // Add all of the factory methods in this Objective-C class, its protocols, |
| 4719 | // superclasses, categories, implementation, etc. |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4720 | Results.EnterNewScope(); |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4721 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4722 | // If this is a send-to-super, try to add the special "super" send |
| 4723 | // completion. |
| 4724 | if (IsSuper) { |
| 4725 | if (ObjCMethodDecl *SuperMethod |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4726 | = AddSuperSendCompletion(SemaRef, false, SelIdents, NumSelIdents, |
| 4727 | Results)) |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4728 | Results.Ignore(SuperMethod); |
| 4729 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4730 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 4731 | // If we're inside an Objective-C method definition, prefer its selector to |
| 4732 | // others. |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4733 | if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl()) |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 4734 | Results.setPreferredSelector(CurMethod->getSelector()); |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4735 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4736 | VisitedSelectorSet Selectors; |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4737 | if (CDecl) |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4738 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, NumSelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4739 | SemaRef.CurContext, Selectors, AtArgumentExpression, |
| 4740 | Results); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4741 | else { |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4742 | // We're messaging "id" as a type; provide all class/factory methods. |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4743 | |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4744 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 4745 | // pool from the AST file. |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4746 | if (SemaRef.ExternalSource) { |
| 4747 | for (uint32_t I = 0, |
| 4748 | N = SemaRef.ExternalSource->GetNumExternalSelectors(); |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 4749 | I != N; ++I) { |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4750 | Selector Sel = SemaRef.ExternalSource->GetExternalSelector(I); |
| 4751 | if (Sel.isNull() || SemaRef.MethodPool.count(Sel)) |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4752 | continue; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4753 | |
| 4754 | SemaRef.ReadMethodPool(Sel); |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4755 | } |
| 4756 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4757 | |
| 4758 | for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(), |
| 4759 | MEnd = SemaRef.MethodPool.end(); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4760 | M != MEnd; ++M) { |
| 4761 | for (ObjCMethodList *MethList = &M->second.second; |
| 4762 | MethList && MethList->Method; |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4763 | MethList = MethList->Next) { |
| 4764 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 4765 | NumSelIdents)) |
| 4766 | continue; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4767 | |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4768 | Result R(MethList->Method, 0); |
| 4769 | R.StartParameter = NumSelIdents; |
| 4770 | R.AllParametersAreInformative = false; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4771 | Results.MaybeAddResult(R, SemaRef.CurContext); |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4772 | } |
| 4773 | } |
| 4774 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4775 | |
| 4776 | Results.ExitScope(); |
| 4777 | } |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4778 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4779 | void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, |
| 4780 | IdentifierInfo **SelIdents, |
| 4781 | unsigned NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4782 | bool AtArgumentExpression, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4783 | bool IsSuper) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4784 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4785 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4786 | AddClassMessageCompletions(*this, S, Receiver, SelIdents, NumSelIdents, |
| 4787 | AtArgumentExpression, IsSuper, Results); |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 4788 | |
| 4789 | // If we're actually at the argument expression (rather than prior to the |
| 4790 | // selector), we're actually performing code completion for an expression. |
| 4791 | // Determine whether we have a single, best method. If so, we can |
| 4792 | // code-complete the expression using the corresponding parameter type as |
| 4793 | // our preferred type, improving completion results. |
| 4794 | if (AtArgumentExpression) { |
| 4795 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
| 4796 | NumSelIdents); |
| 4797 | if (PreferredType.isNull()) |
| 4798 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 4799 | else |
| 4800 | CodeCompleteExpression(S, PreferredType); |
| 4801 | return; |
| 4802 | } |
| 4803 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4804 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4805 | CodeCompletionContext::CCC_Other, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4806 | Results.data(), Results.size()); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4809 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver, |
| 4810 | IdentifierInfo **SelIdents, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4811 | unsigned NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4812 | bool AtArgumentExpression, |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 4813 | ObjCInterfaceDecl *Super) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4814 | typedef CodeCompletionResult Result; |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4815 | |
| 4816 | Expr *RecExpr = static_cast<Expr *>(Receiver); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4817 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4818 | // If necessary, apply function/array conversion to the receiver. |
| 4819 | // C99 6.7.5.3p[7,8]. |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 4820 | if (RecExpr) |
| 4821 | DefaultFunctionArrayLvalueConversion(RecExpr); |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 4822 | QualType ReceiverType = RecExpr? RecExpr->getType() |
| 4823 | : Super? Context.getObjCObjectPointerType( |
| 4824 | Context.getObjCInterfaceType(Super)) |
| 4825 | : Context.getObjCIdType(); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4826 | |
Douglas Gregor | da89264 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 4827 | // If we're messaging an expression with type "id" or "Class", check |
| 4828 | // whether we know something special about the receiver that allows |
| 4829 | // us to assume a more-specific receiver type. |
| 4830 | if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) |
| 4831 | if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) { |
| 4832 | if (ReceiverType->isObjCClassType()) |
| 4833 | return CodeCompleteObjCClassMessage(S, |
| 4834 | ParsedType::make(Context.getObjCInterfaceType(IFace)), |
| 4835 | SelIdents, NumSelIdents, |
| 4836 | AtArgumentExpression, Super); |
| 4837 | |
| 4838 | ReceiverType = Context.getObjCObjectPointerType( |
| 4839 | Context.getObjCInterfaceType(IFace)); |
| 4840 | } |
| 4841 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4842 | // Build the set of methods we can see. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4843 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4844 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4845 | Results.EnterNewScope(); |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4846 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4847 | // If this is a send-to-super, try to add the special "super" send |
| 4848 | // completion. |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 4849 | if (Super) { |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4850 | if (ObjCMethodDecl *SuperMethod |
| 4851 | = AddSuperSendCompletion(*this, false, SelIdents, NumSelIdents, |
| 4852 | Results)) |
| 4853 | Results.Ignore(SuperMethod); |
| 4854 | } |
| 4855 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 4856 | // If we're inside an Objective-C method definition, prefer its selector to |
| 4857 | // others. |
| 4858 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 4859 | Results.setPreferredSelector(CurMethod->getSelector()); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4860 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4861 | // Keep track of the selectors we've already added. |
| 4862 | VisitedSelectorSet Selectors; |
| 4863 | |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4864 | // Handle messages to Class. This really isn't a message to an instance |
| 4865 | // method, so we treat it the same way we would treat a message send to a |
| 4866 | // class method. |
| 4867 | if (ReceiverType->isObjCClassType() || |
| 4868 | ReceiverType->isObjCQualifiedClassType()) { |
| 4869 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 4870 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4871 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, NumSelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4872 | CurContext, Selectors, AtArgumentExpression, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4873 | } |
| 4874 | } |
| 4875 | // Handle messages to a qualified ID ("id<foo>"). |
| 4876 | else if (const ObjCObjectPointerType *QualID |
| 4877 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 4878 | // Search protocols for instance methods. |
| 4879 | for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(), |
| 4880 | E = QualID->qual_end(); |
| 4881 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4882 | AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4883 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4884 | } |
| 4885 | // Handle messages to a pointer to interface type. |
| 4886 | else if (const ObjCObjectPointerType *IFacePtr |
| 4887 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 4888 | // Search the class, its superclasses, etc., for instance methods. |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4889 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4890 | NumSelIdents, CurContext, Selectors, AtArgumentExpression, |
| 4891 | Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4892 | |
| 4893 | // Search protocols for instance methods. |
| 4894 | for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(), |
| 4895 | E = IFacePtr->qual_end(); |
| 4896 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4897 | AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4898 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4899 | } |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4900 | // Handle messages to "id". |
| 4901 | else if (ReceiverType->isObjCIdType()) { |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4902 | // We're messaging "id", so provide all instance methods we know |
| 4903 | // about as code-completion results. |
| 4904 | |
| 4905 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 4906 | // pool from the AST file. |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4907 | if (ExternalSource) { |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 4908 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 4909 | I != N; ++I) { |
| 4910 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4911 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4912 | continue; |
| 4913 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4914 | ReadMethodPool(Sel); |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4915 | } |
| 4916 | } |
| 4917 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4918 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 4919 | MEnd = MethodPool.end(); |
| 4920 | M != MEnd; ++M) { |
| 4921 | for (ObjCMethodList *MethList = &M->second.first; |
| 4922 | MethList && MethList->Method; |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4923 | MethList = MethList->Next) { |
| 4924 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 4925 | NumSelIdents)) |
| 4926 | continue; |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4927 | |
| 4928 | if (!Selectors.insert(MethList->Method->getSelector())) |
| 4929 | continue; |
| 4930 | |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4931 | Result R(MethList->Method, 0); |
| 4932 | R.StartParameter = NumSelIdents; |
| 4933 | R.AllParametersAreInformative = false; |
| 4934 | Results.MaybeAddResult(R, CurContext); |
| 4935 | } |
| 4936 | } |
| 4937 | } |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4938 | Results.ExitScope(); |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 4939 | |
| 4940 | |
| 4941 | // If we're actually at the argument expression (rather than prior to the |
| 4942 | // selector), we're actually performing code completion for an expression. |
| 4943 | // Determine whether we have a single, best method. If so, we can |
| 4944 | // code-complete the expression using the corresponding parameter type as |
| 4945 | // our preferred type, improving completion results. |
| 4946 | if (AtArgumentExpression) { |
| 4947 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
| 4948 | NumSelIdents); |
| 4949 | if (PreferredType.isNull()) |
| 4950 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 4951 | else |
| 4952 | CodeCompleteExpression(S, PreferredType); |
| 4953 | return; |
| 4954 | } |
| 4955 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4956 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4957 | CodeCompletionContext::CCC_Other, |
| 4958 | Results.data(),Results.size()); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4959 | } |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4960 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4961 | void Sema::CodeCompleteObjCForCollection(Scope *S, |
| 4962 | DeclGroupPtrTy IterationVar) { |
| 4963 | CodeCompleteExpressionData Data; |
| 4964 | Data.ObjCCollection = true; |
| 4965 | |
| 4966 | if (IterationVar.getAsOpaquePtr()) { |
| 4967 | DeclGroupRef DG = IterationVar.getAsVal<DeclGroupRef>(); |
| 4968 | for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) { |
| 4969 | if (*I) |
| 4970 | Data.IgnoreDecls.push_back(*I); |
| 4971 | } |
| 4972 | } |
| 4973 | |
| 4974 | CodeCompleteExpression(S, Data); |
| 4975 | } |
| 4976 | |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4977 | void Sema::CodeCompleteObjCSelector(Scope *S, IdentifierInfo **SelIdents, |
| 4978 | unsigned NumSelIdents) { |
| 4979 | // If we have an external source, load the entire class method |
| 4980 | // pool from the AST file. |
| 4981 | if (ExternalSource) { |
| 4982 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 4983 | I != N; ++I) { |
| 4984 | Selector Sel = ExternalSource->GetExternalSelector(I); |
| 4985 | if (Sel.isNull() || MethodPool.count(Sel)) |
| 4986 | continue; |
| 4987 | |
| 4988 | ReadMethodPool(Sel); |
| 4989 | } |
| 4990 | } |
| 4991 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4992 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4993 | CodeCompletionContext::CCC_SelectorName); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4994 | Results.EnterNewScope(); |
| 4995 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 4996 | MEnd = MethodPool.end(); |
| 4997 | M != MEnd; ++M) { |
| 4998 | |
| 4999 | Selector Sel = M->first; |
| 5000 | if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents, NumSelIdents)) |
| 5001 | continue; |
| 5002 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5003 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5004 | if (Sel.isUnarySelector()) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5005 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5006 | Sel.getNameForSlot(0))); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5007 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5008 | continue; |
| 5009 | } |
| 5010 | |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5011 | std::string Accumulator; |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5012 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) { |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5013 | if (I == NumSelIdents) { |
| 5014 | if (!Accumulator.empty()) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5015 | Builder.AddInformativeChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5016 | Accumulator)); |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5017 | Accumulator.clear(); |
| 5018 | } |
| 5019 | } |
| 5020 | |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5021 | Accumulator += Sel.getNameForSlot(I).str(); |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5022 | Accumulator += ':'; |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5023 | } |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5024 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator)); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5025 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5026 | } |
| 5027 | Results.ExitScope(); |
| 5028 | |
| 5029 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5030 | CodeCompletionContext::CCC_SelectorName, |
| 5031 | Results.data(), Results.size()); |
| 5032 | } |
| 5033 | |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5034 | /// \brief Add all of the protocol declarations that we find in the given |
| 5035 | /// (translation unit) context. |
| 5036 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5037 | bool OnlyForwardDeclarations, |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5038 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5039 | typedef CodeCompletionResult Result; |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5040 | |
| 5041 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 5042 | DEnd = Ctx->decls_end(); |
| 5043 | D != DEnd; ++D) { |
| 5044 | // Record any protocols we find. |
| 5045 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*D)) |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5046 | if (!OnlyForwardDeclarations || Proto->isForwardDecl()) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5047 | Results.AddResult(Result(Proto, 0), CurContext, 0, false); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5048 | |
| 5049 | // Record any forward-declared protocols we find. |
| 5050 | if (ObjCForwardProtocolDecl *Forward |
| 5051 | = dyn_cast<ObjCForwardProtocolDecl>(*D)) { |
| 5052 | for (ObjCForwardProtocolDecl::protocol_iterator |
| 5053 | P = Forward->protocol_begin(), |
| 5054 | PEnd = Forward->protocol_end(); |
| 5055 | P != PEnd; ++P) |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5056 | if (!OnlyForwardDeclarations || (*P)->isForwardDecl()) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5057 | Results.AddResult(Result(*P, 0), CurContext, 0, false); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5058 | } |
| 5059 | } |
| 5060 | } |
| 5061 | |
| 5062 | void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols, |
| 5063 | unsigned NumProtocols) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5064 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5065 | CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5066 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5067 | if (CodeCompleter && CodeCompleter->includeGlobals()) { |
| 5068 | Results.EnterNewScope(); |
| 5069 | |
| 5070 | // Tell the result set to ignore all of the protocols we have |
| 5071 | // already seen. |
| 5072 | // FIXME: This doesn't work when caching code-completion results. |
| 5073 | for (unsigned I = 0; I != NumProtocols; ++I) |
| 5074 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first, |
| 5075 | Protocols[I].second)) |
| 5076 | Results.Ignore(Protocol); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5077 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5078 | // Add all protocols. |
| 5079 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 5080 | Results); |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5081 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5082 | Results.ExitScope(); |
| 5083 | } |
| 5084 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5085 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5086 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 5087 | Results.data(),Results.size()); |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5088 | } |
| 5089 | |
| 5090 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5091 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5092 | CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5093 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5094 | if (CodeCompleter && CodeCompleter->includeGlobals()) { |
| 5095 | Results.EnterNewScope(); |
| 5096 | |
| 5097 | // Add all protocols. |
| 5098 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 5099 | Results); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5100 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5101 | Results.ExitScope(); |
| 5102 | } |
| 5103 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5104 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5105 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 5106 | Results.data(),Results.size()); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5107 | } |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5108 | |
| 5109 | /// \brief Add all of the Objective-C interface declarations that we find in |
| 5110 | /// the given (translation unit) context. |
| 5111 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, |
| 5112 | bool OnlyForwardDeclarations, |
| 5113 | bool OnlyUnimplemented, |
| 5114 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5115 | typedef CodeCompletionResult Result; |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5116 | |
| 5117 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 5118 | DEnd = Ctx->decls_end(); |
| 5119 | D != DEnd; ++D) { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 5120 | // Record any interfaces we find. |
| 5121 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*D)) |
| 5122 | if ((!OnlyForwardDeclarations || Class->isForwardDecl()) && |
| 5123 | (!OnlyUnimplemented || !Class->getImplementation())) |
| 5124 | Results.AddResult(Result(Class, 0), CurContext, 0, false); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5125 | |
| 5126 | // Record any forward-declared interfaces we find. |
| 5127 | if (ObjCClassDecl *Forward = dyn_cast<ObjCClassDecl>(*D)) { |
| 5128 | for (ObjCClassDecl::iterator C = Forward->begin(), CEnd = Forward->end(); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 5129 | C != CEnd; ++C) |
| 5130 | if ((!OnlyForwardDeclarations || C->getInterface()->isForwardDecl()) && |
| 5131 | (!OnlyUnimplemented || !C->getInterface()->getImplementation())) |
| 5132 | Results.AddResult(Result(C->getInterface(), 0), CurContext, |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5133 | 0, false); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5134 | } |
| 5135 | } |
| 5136 | } |
| 5137 | |
| 5138 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5139 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5140 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5141 | Results.EnterNewScope(); |
| 5142 | |
| 5143 | // Add all classes. |
| 5144 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 5145 | false, Results); |
| 5146 | |
| 5147 | Results.ExitScope(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5148 | // FIXME: Add a special context for this, use cached global completion |
| 5149 | // results. |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5150 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5151 | CodeCompletionContext::CCC_Other, |
| 5152 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5155 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName, |
| 5156 | SourceLocation ClassNameLoc) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5157 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5158 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5159 | Results.EnterNewScope(); |
| 5160 | |
| 5161 | // Make sure that we ignore the class we're currently defining. |
| 5162 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5163 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5164 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5165 | Results.Ignore(CurClass); |
| 5166 | |
| 5167 | // Add all classes. |
| 5168 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 5169 | false, Results); |
| 5170 | |
| 5171 | Results.ExitScope(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5172 | // FIXME: Add a special context for this, use cached global completion |
| 5173 | // results. |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5174 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5175 | CodeCompletionContext::CCC_Other, |
| 5176 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5177 | } |
| 5178 | |
| 5179 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5180 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5181 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5182 | Results.EnterNewScope(); |
| 5183 | |
| 5184 | // Add all unimplemented classes. |
| 5185 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 5186 | true, Results); |
| 5187 | |
| 5188 | Results.ExitScope(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5189 | // FIXME: Add a special context for this, use cached global completion |
| 5190 | // results. |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5191 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5192 | CodeCompletionContext::CCC_Other, |
| 5193 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5194 | } |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5195 | |
| 5196 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5197 | IdentifierInfo *ClassName, |
| 5198 | SourceLocation ClassNameLoc) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5199 | typedef CodeCompletionResult Result; |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5200 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5201 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5202 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5203 | |
| 5204 | // Ignore any categories we find that have already been implemented by this |
| 5205 | // interface. |
| 5206 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 5207 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5208 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5209 | if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)) |
| 5210 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; |
| 5211 | Category = Category->getNextClassCategory()) |
| 5212 | CategoryNames.insert(Category->getIdentifier()); |
| 5213 | |
| 5214 | // Add all of the categories we know about. |
| 5215 | Results.EnterNewScope(); |
| 5216 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
| 5217 | for (DeclContext::decl_iterator D = TU->decls_begin(), |
| 5218 | DEnd = TU->decls_end(); |
| 5219 | D != DEnd; ++D) |
| 5220 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(*D)) |
| 5221 | if (CategoryNames.insert(Category->getIdentifier())) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5222 | Results.AddResult(Result(Category, 0), CurContext, 0, false); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5223 | Results.ExitScope(); |
| 5224 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5225 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5226 | CodeCompletionContext::CCC_Other, |
| 5227 | Results.data(),Results.size()); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5228 | } |
| 5229 | |
| 5230 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5231 | IdentifierInfo *ClassName, |
| 5232 | SourceLocation ClassNameLoc) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5233 | typedef CodeCompletionResult Result; |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5234 | |
| 5235 | // Find the corresponding interface. If we couldn't find the interface, the |
| 5236 | // program itself is ill-formed. However, we'll try to be helpful still by |
| 5237 | // providing the list of all of the categories we know about. |
| 5238 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5239 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5240 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); |
| 5241 | if (!Class) |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5242 | return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5243 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5244 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5245 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5246 | |
| 5247 | // Add all of the categories that have have corresponding interface |
| 5248 | // declarations in this class and any of its superclasses, except for |
| 5249 | // already-implemented categories in the class itself. |
| 5250 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 5251 | Results.EnterNewScope(); |
| 5252 | bool IgnoreImplemented = true; |
| 5253 | while (Class) { |
| 5254 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; |
| 5255 | Category = Category->getNextClassCategory()) |
| 5256 | if ((!IgnoreImplemented || !Category->getImplementation()) && |
| 5257 | CategoryNames.insert(Category->getIdentifier())) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5258 | Results.AddResult(Result(Category, 0), CurContext, 0, false); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5259 | |
| 5260 | Class = Class->getSuperClass(); |
| 5261 | IgnoreImplemented = false; |
| 5262 | } |
| 5263 | Results.ExitScope(); |
| 5264 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5265 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5266 | CodeCompletionContext::CCC_Other, |
| 5267 | Results.data(),Results.size()); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5268 | } |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5269 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5270 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S, Decl *ObjCImpDecl) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5271 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5272 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5273 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5274 | |
| 5275 | // Figure out where this @synthesize lives. |
| 5276 | ObjCContainerDecl *Container |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5277 | = dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5278 | if (!Container || |
| 5279 | (!isa<ObjCImplementationDecl>(Container) && |
| 5280 | !isa<ObjCCategoryImplDecl>(Container))) |
| 5281 | return; |
| 5282 | |
| 5283 | // Ignore any properties that have already been implemented. |
| 5284 | for (DeclContext::decl_iterator D = Container->decls_begin(), |
| 5285 | DEnd = Container->decls_end(); |
| 5286 | D != DEnd; ++D) |
| 5287 | if (ObjCPropertyImplDecl *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(*D)) |
| 5288 | Results.Ignore(PropertyImpl->getPropertyDecl()); |
| 5289 | |
| 5290 | // Add any properties that we find. |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 5291 | AddedPropertiesSet AddedProperties; |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5292 | Results.EnterNewScope(); |
| 5293 | if (ObjCImplementationDecl *ClassImpl |
| 5294 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| 5295 | AddObjCProperties(ClassImpl->getClassInterface(), false, CurContext, |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 5296 | AddedProperties, Results); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5297 | else |
| 5298 | AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 5299 | false, CurContext, AddedProperties, Results); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5300 | Results.ExitScope(); |
| 5301 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5302 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5303 | CodeCompletionContext::CCC_Other, |
| 5304 | Results.data(),Results.size()); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5305 | } |
| 5306 | |
| 5307 | void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S, |
| 5308 | IdentifierInfo *PropertyName, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5309 | Decl *ObjCImpDecl) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5310 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5311 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5312 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5313 | |
| 5314 | // Figure out where this @synthesize lives. |
| 5315 | ObjCContainerDecl *Container |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5316 | = dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5317 | if (!Container || |
| 5318 | (!isa<ObjCImplementationDecl>(Container) && |
| 5319 | !isa<ObjCCategoryImplDecl>(Container))) |
| 5320 | return; |
| 5321 | |
| 5322 | // Figure out which interface we're looking into. |
| 5323 | ObjCInterfaceDecl *Class = 0; |
| 5324 | if (ObjCImplementationDecl *ClassImpl |
| 5325 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| 5326 | Class = ClassImpl->getClassInterface(); |
| 5327 | else |
| 5328 | Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl() |
| 5329 | ->getClassInterface(); |
| 5330 | |
| 5331 | // Add all of the instance variables in this class and its superclasses. |
| 5332 | Results.EnterNewScope(); |
| 5333 | for(; Class; Class = Class->getSuperClass()) { |
| 5334 | // FIXME: We could screen the type of each ivar for compatibility with |
| 5335 | // the property, but is that being too paternal? |
| 5336 | for (ObjCInterfaceDecl::ivar_iterator IVar = Class->ivar_begin(), |
| 5337 | IVarEnd = Class->ivar_end(); |
| 5338 | IVar != IVarEnd; ++IVar) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5339 | Results.AddResult(Result(*IVar, 0), CurContext, 0, false); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5340 | } |
| 5341 | Results.ExitScope(); |
| 5342 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5343 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5344 | CodeCompletionContext::CCC_Other, |
| 5345 | Results.data(),Results.size()); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5346 | } |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5347 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5348 | // Mapping from selectors to the methods that implement that selector, along |
| 5349 | // with the "in original class" flag. |
| 5350 | typedef llvm::DenseMap<Selector, std::pair<ObjCMethodDecl *, bool> > |
| 5351 | KnownMethodsMap; |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5352 | |
| 5353 | /// \brief Find all of the methods that reside in the given container |
| 5354 | /// (and its superclasses, protocols, etc.) that meet the given |
| 5355 | /// criteria. Insert those methods into the map of known methods, |
| 5356 | /// indexed by selector so they can be easily found. |
| 5357 | static void FindImplementableMethods(ASTContext &Context, |
| 5358 | ObjCContainerDecl *Container, |
| 5359 | bool WantInstanceMethods, |
| 5360 | QualType ReturnType, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5361 | KnownMethodsMap &KnownMethods, |
| 5362 | bool InOriginalClass = true) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5363 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 5364 | // Recurse into protocols. |
| 5365 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5366 | = IFace->getReferencedProtocols(); |
| 5367 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5368 | E = Protocols.end(); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5369 | I != E; ++I) |
| 5370 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5371 | KnownMethods, InOriginalClass); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5372 | |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5373 | // Add methods from any class extensions and categories. |
| 5374 | for (const ObjCCategoryDecl *Cat = IFace->getCategoryList(); Cat; |
| 5375 | Cat = Cat->getNextClassCategory()) |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 5376 | FindImplementableMethods(Context, const_cast<ObjCCategoryDecl*>(Cat), |
| 5377 | WantInstanceMethods, ReturnType, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5378 | KnownMethods, false); |
| 5379 | |
| 5380 | // Visit the superclass. |
| 5381 | if (IFace->getSuperClass()) |
| 5382 | FindImplementableMethods(Context, IFace->getSuperClass(), |
| 5383 | WantInstanceMethods, ReturnType, |
| 5384 | KnownMethods, false); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5385 | } |
| 5386 | |
| 5387 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 5388 | // Recurse into protocols. |
| 5389 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5390 | = Category->getReferencedProtocols(); |
| 5391 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5392 | E = Protocols.end(); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5393 | I != E; ++I) |
| 5394 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5395 | KnownMethods, InOriginalClass); |
| 5396 | |
| 5397 | // If this category is the original class, jump to the interface. |
| 5398 | if (InOriginalClass && Category->getClassInterface()) |
| 5399 | FindImplementableMethods(Context, Category->getClassInterface(), |
| 5400 | WantInstanceMethods, ReturnType, KnownMethods, |
| 5401 | false); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5402 | } |
| 5403 | |
| 5404 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 5405 | // Recurse into protocols. |
| 5406 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5407 | = Protocol->getReferencedProtocols(); |
| 5408 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 5409 | E = Protocols.end(); |
| 5410 | I != E; ++I) |
| 5411 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5412 | KnownMethods, false); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5413 | } |
| 5414 | |
| 5415 | // Add methods in this container. This operation occurs last because |
| 5416 | // we want the methods from this container to override any methods |
| 5417 | // we've previously seen with the same selector. |
| 5418 | for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), |
| 5419 | MEnd = Container->meth_end(); |
| 5420 | M != MEnd; ++M) { |
| 5421 | if ((*M)->isInstanceMethod() == WantInstanceMethods) { |
| 5422 | if (!ReturnType.isNull() && |
| 5423 | !Context.hasSameUnqualifiedType(ReturnType, (*M)->getResultType())) |
| 5424 | continue; |
| 5425 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5426 | KnownMethods[(*M)->getSelector()] = std::make_pair(*M, InOriginalClass); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5427 | } |
| 5428 | } |
| 5429 | } |
| 5430 | |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5431 | /// \brief Add the parenthesized return or parameter type chunk to a code |
| 5432 | /// completion string. |
| 5433 | static void AddObjCPassingTypeChunk(QualType Type, |
| 5434 | ASTContext &Context, |
| 5435 | CodeCompletionBuilder &Builder) { |
| 5436 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5437 | Builder.AddTextChunk(GetCompletionTypeString(Type, Context, |
| 5438 | Builder.getAllocator())); |
| 5439 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5440 | } |
| 5441 | |
| 5442 | /// \brief Determine whether the given class is or inherits from a class by |
| 5443 | /// the given name. |
| 5444 | static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class, |
| 5445 | llvm::StringRef Name) { |
| 5446 | if (!Class) |
| 5447 | return false; |
| 5448 | |
| 5449 | if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name) |
| 5450 | return true; |
| 5451 | |
| 5452 | return InheritsFromClassNamed(Class->getSuperClass(), Name); |
| 5453 | } |
| 5454 | |
| 5455 | /// \brief Add code completions for Objective-C Key-Value Coding (KVC) and |
| 5456 | /// Key-Value Observing (KVO). |
| 5457 | static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property, |
| 5458 | bool IsInstanceMethod, |
| 5459 | QualType ReturnType, |
| 5460 | ASTContext &Context, |
| 5461 | const KnownMethodsMap &KnownMethods, |
| 5462 | ResultBuilder &Results) { |
| 5463 | IdentifierInfo *PropName = Property->getIdentifier(); |
| 5464 | if (!PropName || PropName->getLength() == 0) |
| 5465 | return; |
| 5466 | |
| 5467 | |
| 5468 | // Builder that will create each code completion. |
| 5469 | typedef CodeCompletionResult Result; |
| 5470 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
| 5471 | CodeCompletionBuilder Builder(Allocator); |
| 5472 | |
| 5473 | // The selector table. |
| 5474 | SelectorTable &Selectors = Context.Selectors; |
| 5475 | |
| 5476 | // The property name, copied into the code completion allocation region |
| 5477 | // on demand. |
| 5478 | struct KeyHolder { |
| 5479 | CodeCompletionAllocator &Allocator; |
| 5480 | llvm::StringRef Key; |
| 5481 | const char *CopiedKey; |
| 5482 | |
| 5483 | KeyHolder(CodeCompletionAllocator &Allocator, llvm::StringRef Key) |
| 5484 | : Allocator(Allocator), Key(Key), CopiedKey(0) { } |
| 5485 | |
| 5486 | operator const char *() { |
| 5487 | if (CopiedKey) |
| 5488 | return CopiedKey; |
| 5489 | |
| 5490 | return CopiedKey = Allocator.CopyString(Key); |
| 5491 | } |
| 5492 | } Key(Allocator, PropName->getName()); |
| 5493 | |
| 5494 | // The uppercased name of the property name. |
| 5495 | std::string UpperKey = PropName->getName(); |
| 5496 | if (!UpperKey.empty()) |
| 5497 | UpperKey[0] = toupper(UpperKey[0]); |
| 5498 | |
| 5499 | bool ReturnTypeMatchesProperty = ReturnType.isNull() || |
| 5500 | Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(), |
| 5501 | Property->getType()); |
| 5502 | bool ReturnTypeMatchesVoid |
| 5503 | = ReturnType.isNull() || ReturnType->isVoidType(); |
| 5504 | |
| 5505 | // Add the normal accessor -(type)key. |
| 5506 | if (IsInstanceMethod && |
| 5507 | !KnownMethods.count(Selectors.getNullarySelector(PropName)) && |
| 5508 | ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) { |
| 5509 | if (ReturnType.isNull()) |
| 5510 | AddObjCPassingTypeChunk(Property->getType(), Context, Builder); |
| 5511 | |
| 5512 | Builder.AddTypedTextChunk(Key); |
| 5513 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 5514 | CXCursor_ObjCInstanceMethodDecl)); |
| 5515 | } |
| 5516 | |
| 5517 | // If we have an integral or boolean property (or the user has provided |
| 5518 | // an integral or boolean return type), add the accessor -(type)isKey. |
| 5519 | if (IsInstanceMethod && |
| 5520 | ((!ReturnType.isNull() && |
| 5521 | (ReturnType->isIntegerType() || ReturnType->isBooleanType())) || |
| 5522 | (ReturnType.isNull() && |
| 5523 | (Property->getType()->isIntegerType() || |
| 5524 | Property->getType()->isBooleanType())))) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5525 | std::string SelectorName = (llvm::Twine("is") + UpperKey).str(); |
| 5526 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5527 | if (!KnownMethods.count(Selectors.getNullarySelector(SelectorId))) { |
| 5528 | if (ReturnType.isNull()) { |
| 5529 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5530 | Builder.AddTextChunk("BOOL"); |
| 5531 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5532 | } |
| 5533 | |
| 5534 | Builder.AddTypedTextChunk( |
| 5535 | Allocator.CopyString(SelectorId->getName())); |
| 5536 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 5537 | CXCursor_ObjCInstanceMethodDecl)); |
| 5538 | } |
| 5539 | } |
| 5540 | |
| 5541 | // Add the normal mutator. |
| 5542 | if (IsInstanceMethod && ReturnTypeMatchesVoid && |
| 5543 | !Property->getSetterMethodDecl()) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5544 | std::string SelectorName = (llvm::Twine("set") + UpperKey).str(); |
| 5545 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5546 | if (!KnownMethods.count(Selectors.getUnarySelector(SelectorId))) { |
| 5547 | if (ReturnType.isNull()) { |
| 5548 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5549 | Builder.AddTextChunk("void"); |
| 5550 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5551 | } |
| 5552 | |
| 5553 | Builder.AddTypedTextChunk( |
| 5554 | Allocator.CopyString(SelectorId->getName())); |
| 5555 | Builder.AddTypedTextChunk(":"); |
| 5556 | AddObjCPassingTypeChunk(Property->getType(), Context, Builder); |
| 5557 | Builder.AddTextChunk(Key); |
| 5558 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 5559 | CXCursor_ObjCInstanceMethodDecl)); |
| 5560 | } |
| 5561 | } |
| 5562 | |
| 5563 | // Indexed and unordered accessors |
| 5564 | unsigned IndexedGetterPriority = CCP_CodePattern; |
| 5565 | unsigned IndexedSetterPriority = CCP_CodePattern; |
| 5566 | unsigned UnorderedGetterPriority = CCP_CodePattern; |
| 5567 | unsigned UnorderedSetterPriority = CCP_CodePattern; |
| 5568 | if (const ObjCObjectPointerType *ObjCPointer |
| 5569 | = Property->getType()->getAs<ObjCObjectPointerType>()) { |
| 5570 | if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) { |
| 5571 | // If this interface type is not provably derived from a known |
| 5572 | // collection, penalize the corresponding completions. |
| 5573 | if (!InheritsFromClassNamed(IFace, "NSMutableArray")) { |
| 5574 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 5575 | if (!InheritsFromClassNamed(IFace, "NSArray")) |
| 5576 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 5577 | } |
| 5578 | |
| 5579 | if (!InheritsFromClassNamed(IFace, "NSMutableSet")) { |
| 5580 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 5581 | if (!InheritsFromClassNamed(IFace, "NSSet")) |
| 5582 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 5583 | } |
| 5584 | } |
| 5585 | } else { |
| 5586 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 5587 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 5588 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 5589 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 5590 | } |
| 5591 | |
| 5592 | // Add -(NSUInteger)countOf<key> |
| 5593 | if (IsInstanceMethod && |
| 5594 | (ReturnType.isNull() || ReturnType->isIntegerType())) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5595 | std::string SelectorName = (llvm::Twine("countOf") + UpperKey).str(); |
| 5596 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5597 | if (!KnownMethods.count(Selectors.getNullarySelector(SelectorId))) { |
| 5598 | if (ReturnType.isNull()) { |
| 5599 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5600 | Builder.AddTextChunk("NSUInteger"); |
| 5601 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5602 | } |
| 5603 | |
| 5604 | Builder.AddTypedTextChunk( |
| 5605 | Allocator.CopyString(SelectorId->getName())); |
| 5606 | Results.AddResult(Result(Builder.TakeString(), |
| 5607 | std::min(IndexedGetterPriority, |
| 5608 | UnorderedGetterPriority), |
| 5609 | CXCursor_ObjCInstanceMethodDecl)); |
| 5610 | } |
| 5611 | } |
| 5612 | |
| 5613 | // Indexed getters |
| 5614 | // Add -(id)objectInKeyAtIndex:(NSUInteger)index |
| 5615 | if (IsInstanceMethod && |
| 5616 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5617 | std::string SelectorName |
| 5618 | = (llvm::Twine("objectIn") + UpperKey + "AtIndex").str(); |
| 5619 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5620 | if (!KnownMethods.count(Selectors.getUnarySelector(SelectorId))) { |
| 5621 | if (ReturnType.isNull()) { |
| 5622 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5623 | Builder.AddTextChunk("id"); |
| 5624 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5625 | } |
| 5626 | |
| 5627 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 5628 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5629 | Builder.AddTextChunk("NSUInteger"); |
| 5630 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5631 | Builder.AddTextChunk("index"); |
| 5632 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 5633 | CXCursor_ObjCInstanceMethodDecl)); |
| 5634 | } |
| 5635 | } |
| 5636 | |
| 5637 | // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes |
| 5638 | if (IsInstanceMethod && |
| 5639 | (ReturnType.isNull() || |
| 5640 | (ReturnType->isObjCObjectPointerType() && |
| 5641 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 5642 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 5643 | ->getName() == "NSArray"))) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5644 | std::string SelectorName |
| 5645 | = (llvm::Twine(Property->getName()) + "AtIndexes").str(); |
| 5646 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5647 | if (!KnownMethods.count(Selectors.getUnarySelector(SelectorId))) { |
| 5648 | if (ReturnType.isNull()) { |
| 5649 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5650 | Builder.AddTextChunk("NSArray *"); |
| 5651 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5652 | } |
| 5653 | |
| 5654 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 5655 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5656 | Builder.AddTextChunk("NSIndexSet *"); |
| 5657 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5658 | Builder.AddTextChunk("indexes"); |
| 5659 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 5660 | CXCursor_ObjCInstanceMethodDecl)); |
| 5661 | } |
| 5662 | } |
| 5663 | |
| 5664 | // Add -(void)getKey:(type **)buffer range:(NSRange)inRange |
| 5665 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5666 | std::string SelectorName = (llvm::Twine("get") + UpperKey).str(); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5667 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5668 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5669 | &Context.Idents.get("range") |
| 5670 | }; |
| 5671 | |
| 5672 | if (!KnownMethods.count(Selectors.getSelector(2, SelectorIds))) { |
| 5673 | if (ReturnType.isNull()) { |
| 5674 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5675 | Builder.AddTextChunk("void"); |
| 5676 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5677 | } |
| 5678 | |
| 5679 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 5680 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5681 | Builder.AddPlaceholderChunk("object-type"); |
| 5682 | Builder.AddTextChunk(" **"); |
| 5683 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5684 | Builder.AddTextChunk("buffer"); |
| 5685 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5686 | Builder.AddTypedTextChunk("range:"); |
| 5687 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5688 | Builder.AddTextChunk("NSRange"); |
| 5689 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5690 | Builder.AddTextChunk("inRange"); |
| 5691 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| 5692 | CXCursor_ObjCInstanceMethodDecl)); |
| 5693 | } |
| 5694 | } |
| 5695 | |
| 5696 | // Mutable indexed accessors |
| 5697 | |
| 5698 | // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index |
| 5699 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5700 | std::string SelectorName = (llvm::Twine("in") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5701 | IdentifierInfo *SelectorIds[2] = { |
| 5702 | &Context.Idents.get("insertObject"), |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5703 | &Context.Idents.get(SelectorName) |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5704 | }; |
| 5705 | |
| 5706 | if (!KnownMethods.count(Selectors.getSelector(2, SelectorIds))) { |
| 5707 | if (ReturnType.isNull()) { |
| 5708 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5709 | Builder.AddTextChunk("void"); |
| 5710 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5711 | } |
| 5712 | |
| 5713 | Builder.AddTypedTextChunk("insertObject:"); |
| 5714 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5715 | Builder.AddPlaceholderChunk("object-type"); |
| 5716 | Builder.AddTextChunk(" *"); |
| 5717 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5718 | Builder.AddTextChunk("object"); |
| 5719 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5720 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 5721 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5722 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 5723 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5724 | Builder.AddTextChunk("index"); |
| 5725 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 5726 | CXCursor_ObjCInstanceMethodDecl)); |
| 5727 | } |
| 5728 | } |
| 5729 | |
| 5730 | // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes |
| 5731 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5732 | std::string SelectorName = (llvm::Twine("insert") + UpperKey).str(); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5733 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5734 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5735 | &Context.Idents.get("atIndexes") |
| 5736 | }; |
| 5737 | |
| 5738 | if (!KnownMethods.count(Selectors.getSelector(2, SelectorIds))) { |
| 5739 | if (ReturnType.isNull()) { |
| 5740 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5741 | Builder.AddTextChunk("void"); |
| 5742 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5743 | } |
| 5744 | |
| 5745 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 5746 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5747 | Builder.AddTextChunk("NSArray *"); |
| 5748 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5749 | Builder.AddTextChunk("array"); |
| 5750 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5751 | Builder.AddTypedTextChunk("atIndexes:"); |
| 5752 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5753 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 5754 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5755 | Builder.AddTextChunk("indexes"); |
| 5756 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 5757 | CXCursor_ObjCInstanceMethodDecl)); |
| 5758 | } |
| 5759 | } |
| 5760 | |
| 5761 | // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index |
| 5762 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5763 | std::string SelectorName |
| 5764 | = (llvm::Twine("removeObjectFrom") + UpperKey + "AtIndex").str(); |
| 5765 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5766 | if (!KnownMethods.count(Selectors.getUnarySelector(SelectorId))) { |
| 5767 | if (ReturnType.isNull()) { |
| 5768 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5769 | Builder.AddTextChunk("void"); |
| 5770 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5771 | } |
| 5772 | |
| 5773 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 5774 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5775 | Builder.AddTextChunk("NSUInteger"); |
| 5776 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5777 | Builder.AddTextChunk("index"); |
| 5778 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 5779 | CXCursor_ObjCInstanceMethodDecl)); |
| 5780 | } |
| 5781 | } |
| 5782 | |
| 5783 | // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes |
| 5784 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5785 | std::string SelectorName |
| 5786 | = (llvm::Twine("remove") + UpperKey + "AtIndexes").str(); |
| 5787 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5788 | if (!KnownMethods.count(Selectors.getUnarySelector(SelectorId))) { |
| 5789 | if (ReturnType.isNull()) { |
| 5790 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5791 | Builder.AddTextChunk("void"); |
| 5792 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5793 | } |
| 5794 | |
| 5795 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 5796 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5797 | Builder.AddTextChunk("NSIndexSet *"); |
| 5798 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5799 | Builder.AddTextChunk("indexes"); |
| 5800 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 5801 | CXCursor_ObjCInstanceMethodDecl)); |
| 5802 | } |
| 5803 | } |
| 5804 | |
| 5805 | // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object |
| 5806 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5807 | std::string SelectorName |
| 5808 | = (llvm::Twine("replaceObjectIn") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5809 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5810 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5811 | &Context.Idents.get("withObject") |
| 5812 | }; |
| 5813 | |
| 5814 | if (!KnownMethods.count(Selectors.getSelector(2, SelectorIds))) { |
| 5815 | if (ReturnType.isNull()) { |
| 5816 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5817 | Builder.AddTextChunk("void"); |
| 5818 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5819 | } |
| 5820 | |
| 5821 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 5822 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5823 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 5824 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5825 | Builder.AddTextChunk("index"); |
| 5826 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5827 | Builder.AddTypedTextChunk("withObject:"); |
| 5828 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5829 | Builder.AddTextChunk("id"); |
| 5830 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5831 | Builder.AddTextChunk("object"); |
| 5832 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 5833 | CXCursor_ObjCInstanceMethodDecl)); |
| 5834 | } |
| 5835 | } |
| 5836 | |
| 5837 | // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array |
| 5838 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5839 | std::string SelectorName1 |
| 5840 | = (llvm::Twine("replace") + UpperKey + "AtIndexes").str(); |
| 5841 | std::string SelectorName2 = (llvm::Twine("with") + UpperKey).str(); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5842 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5843 | &Context.Idents.get(SelectorName1), |
| 5844 | &Context.Idents.get(SelectorName2) |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5845 | }; |
| 5846 | |
| 5847 | if (!KnownMethods.count(Selectors.getSelector(2, SelectorIds))) { |
| 5848 | if (ReturnType.isNull()) { |
| 5849 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5850 | Builder.AddTextChunk("void"); |
| 5851 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5852 | } |
| 5853 | |
| 5854 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":")); |
| 5855 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5856 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 5857 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5858 | Builder.AddTextChunk("indexes"); |
| 5859 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5860 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":")); |
| 5861 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5862 | Builder.AddTextChunk("NSArray *"); |
| 5863 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5864 | Builder.AddTextChunk("array"); |
| 5865 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| 5866 | CXCursor_ObjCInstanceMethodDecl)); |
| 5867 | } |
| 5868 | } |
| 5869 | |
| 5870 | // Unordered getters |
| 5871 | // - (NSEnumerator *)enumeratorOfKey |
| 5872 | if (IsInstanceMethod && |
| 5873 | (ReturnType.isNull() || |
| 5874 | (ReturnType->isObjCObjectPointerType() && |
| 5875 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 5876 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 5877 | ->getName() == "NSEnumerator"))) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5878 | std::string SelectorName = (llvm::Twine("enumeratorOf") + UpperKey).str(); |
| 5879 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5880 | if (!KnownMethods.count(Selectors.getNullarySelector(SelectorId))) { |
| 5881 | if (ReturnType.isNull()) { |
| 5882 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5883 | Builder.AddTextChunk("NSEnumerator *"); |
| 5884 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5885 | } |
| 5886 | |
| 5887 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 5888 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
| 5889 | CXCursor_ObjCInstanceMethodDecl)); |
| 5890 | } |
| 5891 | } |
| 5892 | |
| 5893 | // - (type *)memberOfKey:(type *)object |
| 5894 | if (IsInstanceMethod && |
| 5895 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5896 | std::string SelectorName = (llvm::Twine("memberOf") + UpperKey).str(); |
| 5897 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5898 | if (!KnownMethods.count(Selectors.getUnarySelector(SelectorId))) { |
| 5899 | if (ReturnType.isNull()) { |
| 5900 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5901 | Builder.AddPlaceholderChunk("object-type"); |
| 5902 | Builder.AddTextChunk(" *"); |
| 5903 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5904 | } |
| 5905 | |
| 5906 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 5907 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5908 | if (ReturnType.isNull()) { |
| 5909 | Builder.AddPlaceholderChunk("object-type"); |
| 5910 | Builder.AddTextChunk(" *"); |
| 5911 | } else { |
| 5912 | Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context, |
| 5913 | Builder.getAllocator())); |
| 5914 | } |
| 5915 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5916 | Builder.AddTextChunk("object"); |
| 5917 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
| 5918 | CXCursor_ObjCInstanceMethodDecl)); |
| 5919 | } |
| 5920 | } |
| 5921 | |
| 5922 | // Mutable unordered accessors |
| 5923 | // - (void)addKeyObject:(type *)object |
| 5924 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5925 | std::string SelectorName |
| 5926 | = (llvm::Twine("add") + UpperKey + llvm::Twine("Object")).str(); |
| 5927 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5928 | if (!KnownMethods.count(Selectors.getUnarySelector(SelectorId))) { |
| 5929 | if (ReturnType.isNull()) { |
| 5930 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5931 | Builder.AddTextChunk("void"); |
| 5932 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5933 | } |
| 5934 | |
| 5935 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 5936 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5937 | Builder.AddPlaceholderChunk("object-type"); |
| 5938 | Builder.AddTextChunk(" *"); |
| 5939 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5940 | Builder.AddTextChunk("object"); |
| 5941 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 5942 | CXCursor_ObjCInstanceMethodDecl)); |
| 5943 | } |
| 5944 | } |
| 5945 | |
| 5946 | // - (void)addKey:(NSSet *)objects |
| 5947 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5948 | std::string SelectorName = (llvm::Twine("add") + UpperKey).str(); |
| 5949 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5950 | if (!KnownMethods.count(Selectors.getUnarySelector(SelectorId))) { |
| 5951 | if (ReturnType.isNull()) { |
| 5952 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5953 | Builder.AddTextChunk("void"); |
| 5954 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5955 | } |
| 5956 | |
| 5957 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 5958 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5959 | Builder.AddTextChunk("NSSet *"); |
| 5960 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5961 | Builder.AddTextChunk("objects"); |
| 5962 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 5963 | CXCursor_ObjCInstanceMethodDecl)); |
| 5964 | } |
| 5965 | } |
| 5966 | |
| 5967 | // - (void)removeKeyObject:(type *)object |
| 5968 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5969 | std::string SelectorName |
| 5970 | = (llvm::Twine("remove") + UpperKey + llvm::Twine("Object")).str(); |
| 5971 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5972 | if (!KnownMethods.count(Selectors.getUnarySelector(SelectorId))) { |
| 5973 | if (ReturnType.isNull()) { |
| 5974 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5975 | Builder.AddTextChunk("void"); |
| 5976 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5977 | } |
| 5978 | |
| 5979 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 5980 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5981 | Builder.AddPlaceholderChunk("object-type"); |
| 5982 | Builder.AddTextChunk(" *"); |
| 5983 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5984 | Builder.AddTextChunk("object"); |
| 5985 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 5986 | CXCursor_ObjCInstanceMethodDecl)); |
| 5987 | } |
| 5988 | } |
| 5989 | |
| 5990 | // - (void)removeKey:(NSSet *)objects |
| 5991 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5992 | std::string SelectorName = (llvm::Twine("remove") + UpperKey).str(); |
| 5993 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5994 | if (!KnownMethods.count(Selectors.getUnarySelector(SelectorId))) { |
| 5995 | if (ReturnType.isNull()) { |
| 5996 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5997 | Builder.AddTextChunk("void"); |
| 5998 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5999 | } |
| 6000 | |
| 6001 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6002 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6003 | Builder.AddTextChunk("NSSet *"); |
| 6004 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6005 | Builder.AddTextChunk("objects"); |
| 6006 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6007 | CXCursor_ObjCInstanceMethodDecl)); |
| 6008 | } |
| 6009 | } |
| 6010 | |
| 6011 | // - (void)intersectKey:(NSSet *)objects |
| 6012 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6013 | std::string SelectorName = (llvm::Twine("intersect") + UpperKey).str(); |
| 6014 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6015 | if (!KnownMethods.count(Selectors.getUnarySelector(SelectorId))) { |
| 6016 | if (ReturnType.isNull()) { |
| 6017 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6018 | Builder.AddTextChunk("void"); |
| 6019 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6020 | } |
| 6021 | |
| 6022 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 6023 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6024 | Builder.AddTextChunk("NSSet *"); |
| 6025 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6026 | Builder.AddTextChunk("objects"); |
| 6027 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| 6028 | CXCursor_ObjCInstanceMethodDecl)); |
| 6029 | } |
| 6030 | } |
| 6031 | |
| 6032 | // Key-Value Observing |
| 6033 | // + (NSSet *)keyPathsForValuesAffectingKey |
| 6034 | if (!IsInstanceMethod && |
| 6035 | (ReturnType.isNull() || |
| 6036 | (ReturnType->isObjCObjectPointerType() && |
| 6037 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 6038 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 6039 | ->getName() == "NSSet"))) { |
Douglas Gregor | 6204159 | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6040 | std::string SelectorName |
| 6041 | = (llvm::Twine("keyPathsForValuesAffecting") + UpperKey).str(); |
| 6042 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6043 | if (!KnownMethods.count(Selectors.getNullarySelector(SelectorId))) { |
| 6044 | if (ReturnType.isNull()) { |
| 6045 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6046 | Builder.AddTextChunk("NSSet *"); |
| 6047 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6048 | } |
| 6049 | |
| 6050 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| 6051 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| 6052 | CXCursor_ObjCInstanceMethodDecl)); |
| 6053 | } |
| 6054 | } |
| 6055 | } |
| 6056 | |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6057 | void Sema::CodeCompleteObjCMethodDecl(Scope *S, |
| 6058 | bool IsInstanceMethod, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6059 | ParsedType ReturnTy, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6060 | Decl *IDecl) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6061 | // Determine the return type of the method we're declaring, if |
| 6062 | // provided. |
| 6063 | QualType ReturnType = GetTypeFromParser(ReturnTy); |
| 6064 | |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6065 | // Determine where we should start searching for methods. |
| 6066 | ObjCContainerDecl *SearchDecl = 0; |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6067 | bool IsInImplementation = false; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6068 | if (Decl *D = IDecl) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6069 | if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) { |
| 6070 | SearchDecl = Impl->getClassInterface(); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6071 | IsInImplementation = true; |
| 6072 | } else if (ObjCCategoryImplDecl *CatImpl |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6073 | = dyn_cast<ObjCCategoryImplDecl>(D)) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6074 | SearchDecl = CatImpl->getCategoryDecl(); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6075 | IsInImplementation = true; |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6076 | } else |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6077 | SearchDecl = dyn_cast<ObjCContainerDecl>(D); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6078 | } |
| 6079 | |
| 6080 | if (!SearchDecl && S) { |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6081 | if (DeclContext *DC = static_cast<DeclContext *>(S->getEntity())) |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6082 | SearchDecl = dyn_cast<ObjCContainerDecl>(DC); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6083 | } |
| 6084 | |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6085 | if (!SearchDecl) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6086 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6087 | CodeCompletionContext::CCC_Other, |
| 6088 | 0, 0); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6089 | return; |
| 6090 | } |
| 6091 | |
| 6092 | // Find all of the methods that we could declare/implement here. |
| 6093 | KnownMethodsMap KnownMethods; |
| 6094 | FindImplementableMethods(Context, SearchDecl, IsInstanceMethod, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6095 | ReturnType, KnownMethods); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6096 | |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6097 | // Add declarations or definitions for each of the known methods. |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6098 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6099 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 6100 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6101 | Results.EnterNewScope(); |
| 6102 | PrintingPolicy Policy(Context.PrintingPolicy); |
| 6103 | Policy.AnonymousTagLocations = false; |
| 6104 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
| 6105 | MEnd = KnownMethods.end(); |
| 6106 | M != MEnd; ++M) { |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6107 | ObjCMethodDecl *Method = M->second.first; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6108 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6109 | |
| 6110 | // If the result type was not already provided, add it to the |
| 6111 | // pattern as (type). |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6112 | if (ReturnType.isNull()) |
| 6113 | AddObjCPassingTypeChunk(Method->getResultType(), Context, Builder); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6114 | |
| 6115 | Selector Sel = Method->getSelector(); |
| 6116 | |
| 6117 | // Add the first part of the selector to the pattern. |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6118 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 6119 | Sel.getNameForSlot(0))); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6120 | |
| 6121 | // Add parameters to the pattern. |
| 6122 | unsigned I = 0; |
| 6123 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 6124 | PEnd = Method->param_end(); |
| 6125 | P != PEnd; (void)++P, ++I) { |
| 6126 | // Add the part of the selector name. |
| 6127 | if (I == 0) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6128 | Builder.AddTypedTextChunk(":"); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6129 | else if (I < Sel.getNumArgs()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6130 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6131 | Builder.AddTypedTextChunk( |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 6132 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6133 | } else |
| 6134 | break; |
| 6135 | |
| 6136 | // Add the parameter type. |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6137 | AddObjCPassingTypeChunk((*P)->getOriginalType(), Context, Builder); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6138 | |
| 6139 | if (IdentifierInfo *Id = (*P)->getIdentifier()) |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6140 | Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName())); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6141 | } |
| 6142 | |
| 6143 | if (Method->isVariadic()) { |
| 6144 | if (Method->param_size() > 0) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6145 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 6146 | Builder.AddTextChunk("..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 6147 | } |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6148 | |
Douglas Gregor | 447107d | 2010-05-28 00:57:46 +0000 | [diff] [blame] | 6149 | if (IsInImplementation && Results.includeCodePatterns()) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6150 | // We will be defining the method here, so add a compound statement. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6151 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6152 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 6153 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6154 | if (!Method->getResultType()->isVoidType()) { |
| 6155 | // If the result type is not void, add a return clause. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6156 | Builder.AddTextChunk("return"); |
| 6157 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6158 | Builder.AddPlaceholderChunk("expression"); |
| 6159 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6160 | } else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6161 | Builder.AddPlaceholderChunk("statements"); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6162 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6163 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 6164 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6165 | } |
| 6166 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6167 | unsigned Priority = CCP_CodePattern; |
| 6168 | if (!M->second.second) |
| 6169 | Priority += CCD_InBaseClass; |
| 6170 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6171 | Results.AddResult(Result(Builder.TakeString(), Priority, |
Douglas Gregor | 16ed9ad | 2010-08-17 16:06:07 +0000 | [diff] [blame] | 6172 | Method->isInstanceMethod() |
| 6173 | ? CXCursor_ObjCInstanceMethodDecl |
| 6174 | : CXCursor_ObjCClassMethodDecl)); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6175 | } |
| 6176 | |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6177 | // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of |
| 6178 | // the properties in this class and its categories. |
| 6179 | if (Context.getLangOptions().ObjC2) { |
| 6180 | llvm::SmallVector<ObjCContainerDecl *, 4> Containers; |
| 6181 | Containers.push_back(SearchDecl); |
| 6182 | |
| 6183 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl); |
| 6184 | if (!IFace) |
| 6185 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl)) |
| 6186 | IFace = Category->getClassInterface(); |
| 6187 | |
| 6188 | if (IFace) { |
| 6189 | for (ObjCCategoryDecl *Category = IFace->getCategoryList(); Category; |
| 6190 | Category = Category->getNextClassCategory()) |
| 6191 | Containers.push_back(Category); |
| 6192 | } |
| 6193 | |
| 6194 | for (unsigned I = 0, N = Containers.size(); I != N; ++I) { |
| 6195 | for (ObjCContainerDecl::prop_iterator P = Containers[I]->prop_begin(), |
| 6196 | PEnd = Containers[I]->prop_end(); |
| 6197 | P != PEnd; ++P) { |
| 6198 | AddObjCKeyValueCompletions(*P, IsInstanceMethod, ReturnType, Context, |
| 6199 | KnownMethods, Results); |
| 6200 | } |
| 6201 | } |
| 6202 | } |
| 6203 | |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6204 | Results.ExitScope(); |
| 6205 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6206 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6207 | CodeCompletionContext::CCC_Other, |
| 6208 | Results.data(),Results.size()); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6209 | } |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6210 | |
| 6211 | void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S, |
| 6212 | bool IsInstanceMethod, |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 6213 | bool AtParameterName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6214 | ParsedType ReturnTy, |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6215 | IdentifierInfo **SelIdents, |
| 6216 | unsigned NumSelIdents) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6217 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 6218 | // pool from the AST file. |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6219 | if (ExternalSource) { |
| 6220 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 6221 | I != N; ++I) { |
| 6222 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6223 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6224 | continue; |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6225 | |
| 6226 | ReadMethodPool(Sel); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6227 | } |
| 6228 | } |
| 6229 | |
| 6230 | // Build the set of methods we can see. |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6231 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6232 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 6233 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6234 | |
| 6235 | if (ReturnTy) |
| 6236 | Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType()); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6237 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6238 | Results.EnterNewScope(); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6239 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 6240 | MEnd = MethodPool.end(); |
| 6241 | M != MEnd; ++M) { |
| 6242 | for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first : |
| 6243 | &M->second.second; |
| 6244 | MethList && MethList->Method; |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6245 | MethList = MethList->Next) { |
| 6246 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 6247 | NumSelIdents)) |
| 6248 | continue; |
| 6249 | |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 6250 | if (AtParameterName) { |
| 6251 | // Suggest parameter names we've seen before. |
| 6252 | if (NumSelIdents && NumSelIdents <= MethList->Method->param_size()) { |
| 6253 | ParmVarDecl *Param = MethList->Method->param_begin()[NumSelIdents-1]; |
| 6254 | if (Param->getIdentifier()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6255 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6256 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6257 | Param->getIdentifier()->getName())); |
| 6258 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 6259 | } |
| 6260 | } |
| 6261 | |
| 6262 | continue; |
| 6263 | } |
| 6264 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6265 | Result R(MethList->Method, 0); |
| 6266 | R.StartParameter = NumSelIdents; |
| 6267 | R.AllParametersAreInformative = false; |
| 6268 | R.DeclaringEntity = true; |
| 6269 | Results.MaybeAddResult(R, CurContext); |
| 6270 | } |
| 6271 | } |
| 6272 | |
| 6273 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6274 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6275 | CodeCompletionContext::CCC_Other, |
| 6276 | Results.data(),Results.size()); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6277 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 6278 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6279 | void Sema::CodeCompletePreprocessorDirective(bool InConditional) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6280 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 6281 | CodeCompletionContext::CCC_PreprocessorDirective); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6282 | Results.EnterNewScope(); |
| 6283 | |
| 6284 | // #if <condition> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6285 | CodeCompletionBuilder Builder(Results.getAllocator()); |
| 6286 | Builder.AddTypedTextChunk("if"); |
| 6287 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6288 | Builder.AddPlaceholderChunk("condition"); |
| 6289 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6290 | |
| 6291 | // #ifdef <macro> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6292 | Builder.AddTypedTextChunk("ifdef"); |
| 6293 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6294 | Builder.AddPlaceholderChunk("macro"); |
| 6295 | Results.AddResult(Builder.TakeString()); |
| 6296 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6297 | // #ifndef <macro> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6298 | Builder.AddTypedTextChunk("ifndef"); |
| 6299 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6300 | Builder.AddPlaceholderChunk("macro"); |
| 6301 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6302 | |
| 6303 | if (InConditional) { |
| 6304 | // #elif <condition> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6305 | Builder.AddTypedTextChunk("elif"); |
| 6306 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6307 | Builder.AddPlaceholderChunk("condition"); |
| 6308 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6309 | |
| 6310 | // #else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6311 | Builder.AddTypedTextChunk("else"); |
| 6312 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6313 | |
| 6314 | // #endif |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6315 | Builder.AddTypedTextChunk("endif"); |
| 6316 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6317 | } |
| 6318 | |
| 6319 | // #include "header" |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6320 | Builder.AddTypedTextChunk("include"); |
| 6321 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6322 | Builder.AddTextChunk("\""); |
| 6323 | Builder.AddPlaceholderChunk("header"); |
| 6324 | Builder.AddTextChunk("\""); |
| 6325 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6326 | |
| 6327 | // #include <header> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6328 | Builder.AddTypedTextChunk("include"); |
| 6329 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6330 | Builder.AddTextChunk("<"); |
| 6331 | Builder.AddPlaceholderChunk("header"); |
| 6332 | Builder.AddTextChunk(">"); |
| 6333 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6334 | |
| 6335 | // #define <macro> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6336 | Builder.AddTypedTextChunk("define"); |
| 6337 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6338 | Builder.AddPlaceholderChunk("macro"); |
| 6339 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6340 | |
| 6341 | // #define <macro>(<args>) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6342 | Builder.AddTypedTextChunk("define"); |
| 6343 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6344 | Builder.AddPlaceholderChunk("macro"); |
| 6345 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6346 | Builder.AddPlaceholderChunk("args"); |
| 6347 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6348 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6349 | |
| 6350 | // #undef <macro> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6351 | Builder.AddTypedTextChunk("undef"); |
| 6352 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6353 | Builder.AddPlaceholderChunk("macro"); |
| 6354 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6355 | |
| 6356 | // #line <number> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6357 | Builder.AddTypedTextChunk("line"); |
| 6358 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6359 | Builder.AddPlaceholderChunk("number"); |
| 6360 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6361 | |
| 6362 | // #line <number> "filename" |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6363 | Builder.AddTypedTextChunk("line"); |
| 6364 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6365 | Builder.AddPlaceholderChunk("number"); |
| 6366 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6367 | Builder.AddTextChunk("\""); |
| 6368 | Builder.AddPlaceholderChunk("filename"); |
| 6369 | Builder.AddTextChunk("\""); |
| 6370 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6371 | |
| 6372 | // #error <message> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6373 | Builder.AddTypedTextChunk("error"); |
| 6374 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6375 | Builder.AddPlaceholderChunk("message"); |
| 6376 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6377 | |
| 6378 | // #pragma <arguments> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6379 | Builder.AddTypedTextChunk("pragma"); |
| 6380 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6381 | Builder.AddPlaceholderChunk("arguments"); |
| 6382 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6383 | |
| 6384 | if (getLangOptions().ObjC1) { |
| 6385 | // #import "header" |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6386 | Builder.AddTypedTextChunk("import"); |
| 6387 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6388 | Builder.AddTextChunk("\""); |
| 6389 | Builder.AddPlaceholderChunk("header"); |
| 6390 | Builder.AddTextChunk("\""); |
| 6391 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6392 | |
| 6393 | // #import <header> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6394 | Builder.AddTypedTextChunk("import"); |
| 6395 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6396 | Builder.AddTextChunk("<"); |
| 6397 | Builder.AddPlaceholderChunk("header"); |
| 6398 | Builder.AddTextChunk(">"); |
| 6399 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6400 | } |
| 6401 | |
| 6402 | // #include_next "header" |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6403 | Builder.AddTypedTextChunk("include_next"); |
| 6404 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6405 | Builder.AddTextChunk("\""); |
| 6406 | Builder.AddPlaceholderChunk("header"); |
| 6407 | Builder.AddTextChunk("\""); |
| 6408 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6409 | |
| 6410 | // #include_next <header> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6411 | Builder.AddTypedTextChunk("include_next"); |
| 6412 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6413 | Builder.AddTextChunk("<"); |
| 6414 | Builder.AddPlaceholderChunk("header"); |
| 6415 | Builder.AddTextChunk(">"); |
| 6416 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6417 | |
| 6418 | // #warning <message> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6419 | Builder.AddTypedTextChunk("warning"); |
| 6420 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6421 | Builder.AddPlaceholderChunk("message"); |
| 6422 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6423 | |
| 6424 | // Note: #ident and #sccs are such crazy anachronisms that we don't provide |
| 6425 | // completions for them. And __include_macros is a Clang-internal extension |
| 6426 | // that we don't want to encourage anyone to use. |
| 6427 | |
| 6428 | // FIXME: we don't support #assert or #unassert, so don't suggest them. |
| 6429 | Results.ExitScope(); |
| 6430 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6431 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 6432 | CodeCompletionContext::CCC_PreprocessorDirective, |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6433 | Results.data(), Results.size()); |
| 6434 | } |
| 6435 | |
| 6436 | void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) { |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6437 | CodeCompleteOrdinaryName(S, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6438 | S->getFnParent()? Sema::PCC_RecoveryInFunction |
| 6439 | : Sema::PCC_Namespace); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6440 | } |
| 6441 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6442 | void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6443 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 6444 | IsDefinition? CodeCompletionContext::CCC_MacroName |
| 6445 | : CodeCompletionContext::CCC_MacroNameUse); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 6446 | if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) { |
| 6447 | // Add just the names of macros, not their arguments. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6448 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 6449 | Results.EnterNewScope(); |
| 6450 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 6451 | MEnd = PP.macro_end(); |
| 6452 | M != MEnd; ++M) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6453 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6454 | M->first->getName())); |
| 6455 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 6456 | } |
| 6457 | Results.ExitScope(); |
| 6458 | } else if (IsDefinition) { |
| 6459 | // FIXME: Can we detect when the user just wrote an include guard above? |
| 6460 | } |
| 6461 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 6462 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 6463 | Results.data(), Results.size()); |
| 6464 | } |
| 6465 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6466 | void Sema::CodeCompletePreprocessorExpression() { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6467 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 6468 | CodeCompletionContext::CCC_PreprocessorExpression); |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6469 | |
| 6470 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
| 6471 | AddMacroResults(PP, Results); |
| 6472 | |
| 6473 | // defined (<macro>) |
| 6474 | Results.EnterNewScope(); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6475 | CodeCompletionBuilder Builder(Results.getAllocator()); |
| 6476 | Builder.AddTypedTextChunk("defined"); |
| 6477 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 6478 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6479 | Builder.AddPlaceholderChunk("macro"); |
| 6480 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6481 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6482 | Results.ExitScope(); |
| 6483 | |
| 6484 | HandleCodeCompleteResults(this, CodeCompleter, |
| 6485 | CodeCompletionContext::CCC_PreprocessorExpression, |
| 6486 | Results.data(), Results.size()); |
| 6487 | } |
| 6488 | |
| 6489 | void Sema::CodeCompletePreprocessorMacroArgument(Scope *S, |
| 6490 | IdentifierInfo *Macro, |
| 6491 | MacroInfo *MacroInfo, |
| 6492 | unsigned Argument) { |
| 6493 | // FIXME: In the future, we could provide "overload" results, much like we |
| 6494 | // do for function calls. |
| 6495 | |
| 6496 | CodeCompleteOrdinaryName(S, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6497 | S->getFnParent()? Sema::PCC_RecoveryInFunction |
| 6498 | : Sema::PCC_Namespace); |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6499 | } |
| 6500 | |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 6501 | void Sema::CodeCompleteNaturalLanguage() { |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 6502 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | af1c6b5 | 2010-08-25 17:10:00 +0000 | [diff] [blame] | 6503 | CodeCompletionContext::CCC_NaturalLanguage, |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 6504 | 0, 0); |
| 6505 | } |
| 6506 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6507 | void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6508 | llvm::SmallVectorImpl<CodeCompletionResult> &Results) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6509 | ResultBuilder Builder(*this, Allocator, CodeCompletionContext::CCC_Recovery); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 6510 | if (!CodeCompleter || CodeCompleter->includeGlobals()) { |
| 6511 | CodeCompletionDeclConsumer Consumer(Builder, |
| 6512 | Context.getTranslationUnitDecl()); |
| 6513 | LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName, |
| 6514 | Consumer); |
| 6515 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 6516 | |
| 6517 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
| 6518 | AddMacroResults(PP, Builder); |
| 6519 | |
| 6520 | Results.clear(); |
| 6521 | Results.insert(Results.end(), |
| 6522 | Builder.data(), Builder.data() + Builder.size()); |
| 6523 | } |