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. |
| 122 | llvm::BumpPtrAllocator &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 | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 165 | explicit ResultBuilder(Sema &SemaRef, llvm::BumpPtrAllocator &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. |
| 250 | llvm::BumpPtrAllocator &getAllocator() const { return Allocator; } |
| 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 | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1289 | break; |
| 1290 | } |
| 1291 | } |
| 1292 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1293 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); |
| 1294 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); |
| 1295 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1296 | ResultBuilder &Results, |
| 1297 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1298 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1299 | ResultBuilder &Results, |
| 1300 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1301 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1302 | ResultBuilder &Results, |
| 1303 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1304 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1305 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1306 | static void AddTypedefResult(ResultBuilder &Results) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1307 | CodeCompletionBuilder Builder(Results.getAllocator()); |
| 1308 | Builder.AddTypedTextChunk("typedef"); |
| 1309 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1310 | Builder.AddPlaceholderChunk("type"); |
| 1311 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1312 | Builder.AddPlaceholderChunk("name"); |
| 1313 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1316 | static bool WantTypesInContext(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1317 | const LangOptions &LangOpts) { |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1318 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1319 | case Sema::PCC_Namespace: |
| 1320 | case Sema::PCC_Class: |
| 1321 | case Sema::PCC_ObjCInstanceVariableList: |
| 1322 | case Sema::PCC_Template: |
| 1323 | case Sema::PCC_MemberTemplate: |
| 1324 | case Sema::PCC_Statement: |
| 1325 | case Sema::PCC_RecoveryInFunction: |
| 1326 | case Sema::PCC_Type: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1327 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1328 | return true; |
| 1329 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1330 | case Sema::PCC_Expression: |
| 1331 | case Sema::PCC_Condition: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1332 | return LangOpts.CPlusPlus; |
| 1333 | |
| 1334 | case Sema::PCC_ObjCInterface: |
| 1335 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1336 | return false; |
| 1337 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1338 | case Sema::PCC_ForInit: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1339 | return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99; |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | return false; |
| 1343 | } |
| 1344 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1345 | /// \brief Add language constructs that show up for "ordinary" names. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1346 | static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1347 | Scope *S, |
| 1348 | Sema &SemaRef, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1349 | ResultBuilder &Results) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1350 | CodeCompletionBuilder Builder(Results.getAllocator()); |
| 1351 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1352 | typedef CodeCompletionResult Result; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1353 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1354 | case Sema::PCC_Namespace: |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1355 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1356 | if (Results.includeCodePatterns()) { |
| 1357 | // namespace <identifier> { declarations } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1358 | Builder.AddTypedTextChunk("namespace"); |
| 1359 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1360 | Builder.AddPlaceholderChunk("identifier"); |
| 1361 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1362 | Builder.AddPlaceholderChunk("declarations"); |
| 1363 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1364 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1365 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1368 | // namespace identifier = identifier ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1369 | Builder.AddTypedTextChunk("namespace"); |
| 1370 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1371 | Builder.AddPlaceholderChunk("name"); |
| 1372 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
| 1373 | Builder.AddPlaceholderChunk("namespace"); |
| 1374 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1375 | |
| 1376 | // Using directives |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1377 | Builder.AddTypedTextChunk("using"); |
| 1378 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1379 | Builder.AddTextChunk("namespace"); |
| 1380 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1381 | Builder.AddPlaceholderChunk("identifier"); |
| 1382 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1383 | |
| 1384 | // asm(string-literal) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1385 | Builder.AddTypedTextChunk("asm"); |
| 1386 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1387 | Builder.AddPlaceholderChunk("string-literal"); |
| 1388 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1389 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1390 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1391 | if (Results.includeCodePatterns()) { |
| 1392 | // Explicit template instantiation |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1393 | Builder.AddTypedTextChunk("template"); |
| 1394 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1395 | Builder.AddPlaceholderChunk("declaration"); |
| 1396 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1397 | } |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1398 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1399 | |
| 1400 | if (SemaRef.getLangOptions().ObjC1) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1401 | AddObjCTopLevelResults(Results, true); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1402 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1403 | AddTypedefResult(Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1404 | // Fall through |
| 1405 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1406 | case Sema::PCC_Class: |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1407 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1408 | // Using declaration |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1409 | Builder.AddTypedTextChunk("using"); |
| 1410 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1411 | Builder.AddPlaceholderChunk("qualifier"); |
| 1412 | Builder.AddTextChunk("::"); |
| 1413 | Builder.AddPlaceholderChunk("name"); |
| 1414 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1415 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1416 | // using typename qualifier::name (only in a dependent context) |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1417 | if (SemaRef.CurContext->isDependentContext()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1418 | Builder.AddTypedTextChunk("using"); |
| 1419 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1420 | Builder.AddTextChunk("typename"); |
| 1421 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1422 | Builder.AddPlaceholderChunk("qualifier"); |
| 1423 | Builder.AddTextChunk("::"); |
| 1424 | Builder.AddPlaceholderChunk("name"); |
| 1425 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1428 | if (CCC == Sema::PCC_Class) { |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1429 | AddTypedefResult(Results); |
| 1430 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1431 | // public: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1432 | Builder.AddTypedTextChunk("public"); |
| 1433 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1434 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1435 | |
| 1436 | // protected: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1437 | Builder.AddTypedTextChunk("protected"); |
| 1438 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1439 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1440 | |
| 1441 | // private: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1442 | Builder.AddTypedTextChunk("private"); |
| 1443 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1444 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1445 | } |
| 1446 | } |
| 1447 | // Fall through |
| 1448 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1449 | case Sema::PCC_Template: |
| 1450 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1451 | if (SemaRef.getLangOptions().CPlusPlus && Results.includeCodePatterns()) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1452 | // template < parameters > |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1453 | Builder.AddTypedTextChunk("template"); |
| 1454 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1455 | Builder.AddPlaceholderChunk("parameters"); |
| 1456 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1457 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1460 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 1461 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1462 | break; |
| 1463 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1464 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1465 | AddObjCInterfaceResults(SemaRef.getLangOptions(), Results, true); |
| 1466 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 1467 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1468 | break; |
| 1469 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1470 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1471 | AddObjCImplementationResults(SemaRef.getLangOptions(), Results, true); |
| 1472 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 1473 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1474 | break; |
| 1475 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1476 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1477 | AddObjCVisibilityResults(SemaRef.getLangOptions(), Results, true); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1478 | break; |
| 1479 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1480 | case Sema::PCC_RecoveryInFunction: |
| 1481 | case Sema::PCC_Statement: { |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1482 | AddTypedefResult(Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1483 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1484 | if (SemaRef.getLangOptions().CPlusPlus && Results.includeCodePatterns()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1485 | Builder.AddTypedTextChunk("try"); |
| 1486 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1487 | Builder.AddPlaceholderChunk("statements"); |
| 1488 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1489 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1490 | Builder.AddTextChunk("catch"); |
| 1491 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1492 | Builder.AddPlaceholderChunk("declaration"); |
| 1493 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1494 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1495 | Builder.AddPlaceholderChunk("statements"); |
| 1496 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1497 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1498 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1499 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1500 | if (SemaRef.getLangOptions().ObjC1) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1501 | AddObjCStatementResults(Results, true); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1502 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1503 | if (Results.includeCodePatterns()) { |
| 1504 | // if (condition) { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1505 | Builder.AddTypedTextChunk("if"); |
| 1506 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1507 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1508 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1509 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1510 | Builder.AddPlaceholderChunk("expression"); |
| 1511 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1512 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1513 | Builder.AddPlaceholderChunk("statements"); |
| 1514 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1515 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1516 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1517 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1518 | // switch (condition) { } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1519 | Builder.AddTypedTextChunk("switch"); |
| 1520 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1521 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1522 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1523 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1524 | Builder.AddPlaceholderChunk("expression"); |
| 1525 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1526 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1527 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1528 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1529 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1532 | // Switch-specific statements. |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 1533 | if (!SemaRef.getCurFunction()->SwitchStack.empty()) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1534 | // case expression: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1535 | Builder.AddTypedTextChunk("case"); |
| 1536 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1537 | Builder.AddPlaceholderChunk("expression"); |
| 1538 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1539 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1540 | |
| 1541 | // default: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1542 | Builder.AddTypedTextChunk("default"); |
| 1543 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1544 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1547 | if (Results.includeCodePatterns()) { |
| 1548 | /// while (condition) { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1549 | Builder.AddTypedTextChunk("while"); |
| 1550 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1551 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1552 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1553 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1554 | Builder.AddPlaceholderChunk("expression"); |
| 1555 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1556 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1557 | Builder.AddPlaceholderChunk("statements"); |
| 1558 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1559 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1560 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1561 | |
| 1562 | // do { statements } while ( expression ); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1563 | Builder.AddTypedTextChunk("do"); |
| 1564 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1565 | Builder.AddPlaceholderChunk("statements"); |
| 1566 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1567 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1568 | Builder.AddTextChunk("while"); |
| 1569 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1570 | Builder.AddPlaceholderChunk("expression"); |
| 1571 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1572 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1573 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1574 | // for ( for-init-statement ; condition ; expression ) { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1575 | Builder.AddTypedTextChunk("for"); |
| 1576 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1577 | if (SemaRef.getLangOptions().CPlusPlus || SemaRef.getLangOptions().C99) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1578 | Builder.AddPlaceholderChunk("init-statement"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1579 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1580 | Builder.AddPlaceholderChunk("init-expression"); |
| 1581 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1582 | Builder.AddPlaceholderChunk("condition"); |
| 1583 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1584 | Builder.AddPlaceholderChunk("inc-expression"); |
| 1585 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1586 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1587 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1588 | Builder.AddPlaceholderChunk("statements"); |
| 1589 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1590 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1591 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1592 | } |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1593 | |
| 1594 | if (S->getContinueParent()) { |
| 1595 | // continue ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1596 | Builder.AddTypedTextChunk("continue"); |
| 1597 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | if (S->getBreakParent()) { |
| 1601 | // break ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1602 | Builder.AddTypedTextChunk("break"); |
| 1603 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | // "return expression ;" or "return ;", depending on whether we |
| 1607 | // know the function is void or not. |
| 1608 | bool isVoid = false; |
| 1609 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) |
| 1610 | isVoid = Function->getResultType()->isVoidType(); |
| 1611 | else if (ObjCMethodDecl *Method |
| 1612 | = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) |
| 1613 | isVoid = Method->getResultType()->isVoidType(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1614 | else if (SemaRef.getCurBlock() && |
| 1615 | !SemaRef.getCurBlock()->ReturnType.isNull()) |
| 1616 | isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType(); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1617 | Builder.AddTypedTextChunk("return"); |
Douglas Gregor | 9329800 | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1618 | if (!isVoid) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1619 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1620 | Builder.AddPlaceholderChunk("expression"); |
Douglas Gregor | 9329800 | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1621 | } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1622 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1623 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1624 | // goto identifier ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1625 | Builder.AddTypedTextChunk("goto"); |
| 1626 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1627 | Builder.AddPlaceholderChunk("label"); |
| 1628 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1629 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1630 | // Using directives |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1631 | Builder.AddTypedTextChunk("using"); |
| 1632 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1633 | Builder.AddTextChunk("namespace"); |
| 1634 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1635 | Builder.AddPlaceholderChunk("identifier"); |
| 1636 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | // Fall through (for statement expressions). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1640 | case Sema::PCC_ForInit: |
| 1641 | case Sema::PCC_Condition: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1642 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1643 | // Fall through: conditions and statements can have expressions. |
| 1644 | |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1645 | case Sema::PCC_ParenthesizedExpression: |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1646 | case Sema::PCC_Expression: { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1647 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 1648 | // 'this', if we're in a non-static member function. |
| 1649 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) |
| 1650 | if (!Method->isStatic()) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1651 | Results.AddResult(Result("this")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1652 | |
| 1653 | // true, false |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1654 | Results.AddResult(Result("true")); |
| 1655 | Results.AddResult(Result("false")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1656 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1657 | // dynamic_cast < type-id > ( expression ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1658 | Builder.AddTypedTextChunk("dynamic_cast"); |
| 1659 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1660 | Builder.AddPlaceholderChunk("type"); |
| 1661 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1662 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1663 | Builder.AddPlaceholderChunk("expression"); |
| 1664 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1665 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1666 | |
| 1667 | // static_cast < type-id > ( expression ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1668 | Builder.AddTypedTextChunk("static_cast"); |
| 1669 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1670 | Builder.AddPlaceholderChunk("type"); |
| 1671 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1672 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1673 | Builder.AddPlaceholderChunk("expression"); |
| 1674 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1675 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1676 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1677 | // reinterpret_cast < type-id > ( expression ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1678 | Builder.AddTypedTextChunk("reinterpret_cast"); |
| 1679 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1680 | Builder.AddPlaceholderChunk("type"); |
| 1681 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1682 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1683 | Builder.AddPlaceholderChunk("expression"); |
| 1684 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1685 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1686 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1687 | // const_cast < type-id > ( expression ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1688 | Builder.AddTypedTextChunk("const_cast"); |
| 1689 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1690 | Builder.AddPlaceholderChunk("type"); |
| 1691 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1692 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1693 | Builder.AddPlaceholderChunk("expression"); |
| 1694 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1695 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1696 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1697 | // typeid ( expression-or-type ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1698 | Builder.AddTypedTextChunk("typeid"); |
| 1699 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1700 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 1701 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1702 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1703 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1704 | // new T ( ... ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1705 | Builder.AddTypedTextChunk("new"); |
| 1706 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1707 | Builder.AddPlaceholderChunk("type"); |
| 1708 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1709 | Builder.AddPlaceholderChunk("expressions"); |
| 1710 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1711 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1712 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1713 | // new T [ ] ( ... ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1714 | Builder.AddTypedTextChunk("new"); |
| 1715 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1716 | Builder.AddPlaceholderChunk("type"); |
| 1717 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1718 | Builder.AddPlaceholderChunk("size"); |
| 1719 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 1720 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1721 | Builder.AddPlaceholderChunk("expressions"); |
| 1722 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1723 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1724 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1725 | // delete expression |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1726 | Builder.AddTypedTextChunk("delete"); |
| 1727 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1728 | Builder.AddPlaceholderChunk("expression"); |
| 1729 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1730 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1731 | // delete [] expression |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1732 | Builder.AddTypedTextChunk("delete"); |
| 1733 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1734 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1735 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 1736 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1737 | Builder.AddPlaceholderChunk("expression"); |
| 1738 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1739 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1740 | // throw expression |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1741 | Builder.AddTypedTextChunk("throw"); |
| 1742 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1743 | Builder.AddPlaceholderChunk("expression"); |
| 1744 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1745 | |
| 1746 | // FIXME: Rethrow? |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | if (SemaRef.getLangOptions().ObjC1) { |
| 1750 | // 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] | 1751 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
| 1752 | // The interface can be NULL. |
| 1753 | if (ObjCInterfaceDecl *ID = Method->getClassInterface()) |
| 1754 | if (ID->getSuperClass()) |
| 1755 | Results.AddResult(Result("super")); |
| 1756 | } |
| 1757 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1758 | AddObjCExpressionResults(Results, true); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1761 | // sizeof expression |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1762 | Builder.AddTypedTextChunk("sizeof"); |
| 1763 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1764 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 1765 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1766 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1767 | break; |
| 1768 | } |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1769 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1770 | case Sema::PCC_Type: |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1771 | break; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1774 | if (WantTypesInContext(CCC, SemaRef.getLangOptions())) |
| 1775 | AddTypeSpecifierResults(SemaRef.getLangOptions(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1776 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1777 | if (SemaRef.getLangOptions().CPlusPlus && CCC != Sema::PCC_Type) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1778 | Results.AddResult(Result("operator")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1781 | /// \brief Copy the given string into the allocator. |
| 1782 | static const char* |
| 1783 | CopyString(llvm::BumpPtrAllocator &Allocator, llvm::StringRef Text) { |
| 1784 | char *Mem = (char *)Allocator.Allocate(Text.size() + 1, 1); |
| 1785 | std::copy(Text.begin(), Text.end(), Mem); |
| 1786 | Mem[Text.size()] = 0; |
| 1787 | return Mem; |
| 1788 | } |
| 1789 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1790 | /// \brief If the given declaration has an associated type, add it as a result |
| 1791 | /// type chunk. |
| 1792 | static void AddResultTypeChunk(ASTContext &Context, |
| 1793 | NamedDecl *ND, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1794 | CodeCompletionBuilder &Result) { |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1795 | if (!ND) |
| 1796 | return; |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1797 | |
| 1798 | // Skip constructors and conversion functions, which have their return types |
| 1799 | // built into their names. |
| 1800 | if (isa<CXXConstructorDecl>(ND) || isa<CXXConversionDecl>(ND)) |
| 1801 | return; |
| 1802 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1803 | // Determine the type of the declaration (if it has a type). |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1804 | QualType T; |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1805 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) |
| 1806 | T = Function->getResultType(); |
| 1807 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
| 1808 | T = Method->getResultType(); |
| 1809 | else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) |
| 1810 | T = FunTmpl->getTemplatedDecl()->getResultType(); |
| 1811 | else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
| 1812 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); |
| 1813 | else if (isa<UnresolvedUsingValueDecl>(ND)) { |
| 1814 | /* Do nothing: ignore unresolved using declarations*/ |
| 1815 | } else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND)) |
| 1816 | T = Value->getType(); |
| 1817 | else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
| 1818 | T = Property->getType(); |
| 1819 | |
| 1820 | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) |
| 1821 | return; |
| 1822 | |
Douglas Gregor | 84139d6 | 2010-04-05 21:25:31 +0000 | [diff] [blame] | 1823 | PrintingPolicy Policy(Context.PrintingPolicy); |
| 1824 | Policy.AnonymousTagLocations = false; |
| 1825 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1826 | // FIXME: Fast-path common strings. |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1827 | std::string TypeStr; |
Douglas Gregor | 84139d6 | 2010-04-05 21:25:31 +0000 | [diff] [blame] | 1828 | T.getAsStringInternal(TypeStr, Policy); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1829 | Result.AddResultTypeChunk(CopyString(Result.getAllocator(), TypeStr)); |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1832 | static void MaybeAddSentinel(ASTContext &Context, NamedDecl *FunctionOrMethod, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1833 | CodeCompletionBuilder &Result) { |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1834 | if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) |
| 1835 | if (Sentinel->getSentinel() == 0) { |
| 1836 | if (Context.getLangOptions().ObjC1 && |
| 1837 | Context.Idents.get("nil").hasMacroDefinition()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1838 | Result.AddTextChunk(", nil"); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1839 | else if (Context.Idents.get("NULL").hasMacroDefinition()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1840 | Result.AddTextChunk(", NULL"); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1841 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1842 | Result.AddTextChunk(", (void*)0"); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1843 | } |
| 1844 | } |
| 1845 | |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1846 | static std::string FormatFunctionParameter(ASTContext &Context, |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 1847 | ParmVarDecl *Param, |
| 1848 | bool SuppressName = false) { |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1849 | bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext()); |
| 1850 | if (Param->getType()->isDependentType() || |
| 1851 | !Param->getType()->isBlockPointerType()) { |
| 1852 | // The argument for a dependent or non-block parameter is a placeholder |
| 1853 | // containing that parameter's type. |
| 1854 | std::string Result; |
| 1855 | |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 1856 | if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName) |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1857 | Result = Param->getIdentifier()->getName(); |
| 1858 | |
| 1859 | Param->getType().getAsStringInternal(Result, |
| 1860 | Context.PrintingPolicy); |
| 1861 | |
| 1862 | if (ObjCMethodParam) { |
| 1863 | Result = "(" + Result; |
| 1864 | Result += ")"; |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 1865 | if (Param->getIdentifier() && !SuppressName) |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1866 | Result += Param->getIdentifier()->getName(); |
| 1867 | } |
| 1868 | return Result; |
| 1869 | } |
| 1870 | |
| 1871 | // The argument for a block pointer parameter is a block literal with |
| 1872 | // the appropriate type. |
| 1873 | FunctionProtoTypeLoc *Block = 0; |
| 1874 | TypeLoc TL; |
| 1875 | if (TypeSourceInfo *TSInfo = Param->getTypeSourceInfo()) { |
| 1876 | TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 1877 | while (true) { |
| 1878 | // Look through typedefs. |
| 1879 | if (TypedefTypeLoc *TypedefTL = dyn_cast<TypedefTypeLoc>(&TL)) { |
| 1880 | if (TypeSourceInfo *InnerTSInfo |
| 1881 | = TypedefTL->getTypedefDecl()->getTypeSourceInfo()) { |
| 1882 | TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 1883 | continue; |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | // Look through qualified types |
| 1888 | if (QualifiedTypeLoc *QualifiedTL = dyn_cast<QualifiedTypeLoc>(&TL)) { |
| 1889 | TL = QualifiedTL->getUnqualifiedLoc(); |
| 1890 | continue; |
| 1891 | } |
| 1892 | |
| 1893 | // Try to get the function prototype behind the block pointer type, |
| 1894 | // then we're done. |
| 1895 | if (BlockPointerTypeLoc *BlockPtr |
| 1896 | = dyn_cast<BlockPointerTypeLoc>(&TL)) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 1897 | TL = BlockPtr->getPointeeLoc().IgnoreParens(); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1898 | Block = dyn_cast<FunctionProtoTypeLoc>(&TL); |
| 1899 | } |
| 1900 | break; |
| 1901 | } |
| 1902 | } |
| 1903 | |
| 1904 | if (!Block) { |
| 1905 | // We were unable to find a FunctionProtoTypeLoc with parameter names |
| 1906 | // for the block; just use the parameter type as a placeholder. |
| 1907 | std::string Result; |
| 1908 | Param->getType().getUnqualifiedType(). |
| 1909 | getAsStringInternal(Result, Context.PrintingPolicy); |
| 1910 | |
| 1911 | if (ObjCMethodParam) { |
| 1912 | Result = "(" + Result; |
| 1913 | Result += ")"; |
| 1914 | if (Param->getIdentifier()) |
| 1915 | Result += Param->getIdentifier()->getName(); |
| 1916 | } |
| 1917 | |
| 1918 | return Result; |
| 1919 | } |
| 1920 | |
| 1921 | // We have the function prototype behind the block pointer type, as it was |
| 1922 | // written in the source. |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 1923 | std::string Result; |
| 1924 | QualType ResultType = Block->getTypePtr()->getResultType(); |
| 1925 | if (!ResultType->isVoidType()) |
| 1926 | ResultType.getAsStringInternal(Result, Context.PrintingPolicy); |
| 1927 | |
| 1928 | Result = '^' + Result; |
| 1929 | if (Block->getNumArgs() == 0) { |
| 1930 | if (Block->getTypePtr()->isVariadic()) |
| 1931 | Result += "(...)"; |
Douglas Gregor | c2760bc | 2010-10-02 23:49:58 +0000 | [diff] [blame] | 1932 | else |
| 1933 | Result += "(void)"; |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 1934 | } else { |
| 1935 | Result += "("; |
| 1936 | for (unsigned I = 0, N = Block->getNumArgs(); I != N; ++I) { |
| 1937 | if (I) |
| 1938 | Result += ", "; |
| 1939 | Result += FormatFunctionParameter(Context, Block->getArg(I)); |
| 1940 | |
| 1941 | if (I == N - 1 && Block->getTypePtr()->isVariadic()) |
| 1942 | Result += ", ..."; |
| 1943 | } |
| 1944 | Result += ")"; |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 1945 | } |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 1946 | |
Douglas Gregor | c2760bc | 2010-10-02 23:49:58 +0000 | [diff] [blame] | 1947 | if (Param->getIdentifier()) |
| 1948 | Result += Param->getIdentifier()->getName(); |
| 1949 | |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1950 | return Result; |
| 1951 | } |
| 1952 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1953 | /// \brief Add function parameter chunks to the given code completion string. |
| 1954 | static void AddFunctionParameterChunks(ASTContext &Context, |
| 1955 | FunctionDecl *Function, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1956 | CodeCompletionBuilder &Result, |
| 1957 | unsigned Start = 0, |
| 1958 | bool InOptional = false) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1959 | typedef CodeCompletionString::Chunk Chunk; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1960 | bool FirstParameter = true; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1961 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1962 | for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1963 | ParmVarDecl *Param = Function->getParamDecl(P); |
| 1964 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1965 | if (Param->hasDefaultArg() && !InOptional) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1966 | // When we see an optional default argument, put that argument and |
| 1967 | // the remaining default arguments into a new, optional string. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1968 | CodeCompletionBuilder Opt(Result.getAllocator()); |
| 1969 | if (!FirstParameter) |
| 1970 | Opt.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
| 1971 | AddFunctionParameterChunks(Context, Function, Opt, P, true); |
| 1972 | Result.AddOptionalChunk(Opt.TakeString()); |
| 1973 | break; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1976 | if (FirstParameter) |
| 1977 | FirstParameter = false; |
| 1978 | else |
| 1979 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
| 1980 | |
| 1981 | InOptional = false; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1982 | |
| 1983 | // Format the placeholder string. |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1984 | std::string PlaceholderStr = FormatFunctionParameter(Context, Param); |
| 1985 | |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 1986 | if (Function->isVariadic() && P == N - 1) |
| 1987 | PlaceholderStr += ", ..."; |
| 1988 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1989 | // Add the placeholder string. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1990 | Result.AddPlaceholderChunk(CopyString(Result.getAllocator(), |
| 1991 | PlaceholderStr)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1992 | } |
Douglas Gregor | b3d4525 | 2009-09-22 21:42:17 +0000 | [diff] [blame] | 1993 | |
| 1994 | if (const FunctionProtoType *Proto |
| 1995 | = Function->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1996 | if (Proto->isVariadic()) { |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 1997 | if (Proto->getNumArgs() == 0) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 1998 | Result.AddPlaceholderChunk("..."); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1999 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2000 | MaybeAddSentinel(Context, Function, Result); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2001 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | /// \brief Add template parameter chunks to the given code completion string. |
| 2005 | static void AddTemplateParameterChunks(ASTContext &Context, |
| 2006 | TemplateDecl *Template, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2007 | CodeCompletionBuilder &Result, |
| 2008 | unsigned MaxParameters = 0, |
| 2009 | unsigned Start = 0, |
| 2010 | bool InDefaultArg = false) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2011 | typedef CodeCompletionString::Chunk Chunk; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2012 | bool FirstParameter = true; |
| 2013 | |
| 2014 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2015 | TemplateParameterList::iterator PEnd = Params->end(); |
| 2016 | if (MaxParameters) |
| 2017 | PEnd = Params->begin() + MaxParameters; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2018 | for (TemplateParameterList::iterator P = Params->begin() + Start; |
| 2019 | P != PEnd; ++P) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2020 | bool HasDefaultArg = false; |
| 2021 | std::string PlaceholderStr; |
| 2022 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 2023 | if (TTP->wasDeclaredWithTypename()) |
| 2024 | PlaceholderStr = "typename"; |
| 2025 | else |
| 2026 | PlaceholderStr = "class"; |
| 2027 | |
| 2028 | if (TTP->getIdentifier()) { |
| 2029 | PlaceholderStr += ' '; |
| 2030 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2031 | } |
| 2032 | |
| 2033 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2034 | } else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2035 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2036 | if (NTTP->getIdentifier()) |
| 2037 | PlaceholderStr = NTTP->getIdentifier()->getName(); |
| 2038 | NTTP->getType().getAsStringInternal(PlaceholderStr, |
| 2039 | Context.PrintingPolicy); |
| 2040 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2041 | } else { |
| 2042 | assert(isa<TemplateTemplateParmDecl>(*P)); |
| 2043 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 2044 | |
| 2045 | // Since putting the template argument list into the placeholder would |
| 2046 | // be very, very long, we just use an abbreviation. |
| 2047 | PlaceholderStr = "template<...> class"; |
| 2048 | if (TTP->getIdentifier()) { |
| 2049 | PlaceholderStr += ' '; |
| 2050 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2051 | } |
| 2052 | |
| 2053 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2054 | } |
| 2055 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2056 | if (HasDefaultArg && !InDefaultArg) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2057 | // When we see an optional default argument, put that argument and |
| 2058 | // the remaining default arguments into a new, optional string. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2059 | CodeCompletionBuilder Opt(Result.getAllocator()); |
| 2060 | if (!FirstParameter) |
| 2061 | Opt.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
| 2062 | AddTemplateParameterChunks(Context, Template, Opt, MaxParameters, |
| 2063 | P - Params->begin(), true); |
| 2064 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2065 | break; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2068 | InDefaultArg = false; |
| 2069 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2070 | if (FirstParameter) |
| 2071 | FirstParameter = false; |
| 2072 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2073 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2074 | |
| 2075 | // Add the placeholder string. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2076 | Result.AddPlaceholderChunk(CopyString(Result.getAllocator(), |
| 2077 | PlaceholderStr)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2078 | } |
| 2079 | } |
| 2080 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2081 | /// \brief Add a qualifier to the given code-completion string, if the |
| 2082 | /// provided nested-name-specifier is non-NULL. |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2083 | static void |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2084 | AddQualifierToCompletionString(CodeCompletionBuilder &Result, |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2085 | NestedNameSpecifier *Qualifier, |
| 2086 | bool QualifierIsInformative, |
| 2087 | ASTContext &Context) { |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2088 | if (!Qualifier) |
| 2089 | return; |
| 2090 | |
| 2091 | std::string PrintedNNS; |
| 2092 | { |
| 2093 | llvm::raw_string_ostream OS(PrintedNNS); |
| 2094 | Qualifier->print(OS, Context.PrintingPolicy); |
| 2095 | } |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2096 | if (QualifierIsInformative) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2097 | Result.AddInformativeChunk(CopyString(Result.getAllocator(), PrintedNNS)); |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2098 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2099 | Result.AddTextChunk(CopyString(Result.getAllocator(), PrintedNNS)); |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2102 | static void |
| 2103 | AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result, |
| 2104 | FunctionDecl *Function) { |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2105 | const FunctionProtoType *Proto |
| 2106 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2107 | if (!Proto || !Proto->getTypeQuals()) |
| 2108 | return; |
| 2109 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2110 | // FIXME: Fast-path single-qualifier strings. |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2111 | std::string QualsStr; |
| 2112 | if (Proto->getTypeQuals() & Qualifiers::Const) |
| 2113 | QualsStr += " const"; |
| 2114 | if (Proto->getTypeQuals() & Qualifiers::Volatile) |
| 2115 | QualsStr += " volatile"; |
| 2116 | if (Proto->getTypeQuals() & Qualifiers::Restrict) |
| 2117 | QualsStr += " restrict"; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2118 | Result.AddInformativeChunk(CopyString(Result.getAllocator(), QualsStr)); |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2121 | /// \brief Add the name of the given declaration |
| 2122 | static void AddTypedNameChunk(ASTContext &Context, NamedDecl *ND, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2123 | CodeCompletionBuilder &Result) { |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2124 | typedef CodeCompletionString::Chunk Chunk; |
| 2125 | |
| 2126 | DeclarationName Name = ND->getDeclName(); |
| 2127 | if (!Name) |
| 2128 | return; |
| 2129 | |
| 2130 | switch (Name.getNameKind()) { |
| 2131 | case DeclarationName::Identifier: |
| 2132 | case DeclarationName::CXXConversionFunctionName: |
| 2133 | case DeclarationName::CXXOperatorName: |
| 2134 | case DeclarationName::CXXDestructorName: |
| 2135 | case DeclarationName::CXXLiteralOperatorName: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2136 | // FIXME: Fast-path operator names? |
| 2137 | Result.AddTypedTextChunk(CopyString(Result.getAllocator(), |
| 2138 | ND->getNameAsString())); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2139 | break; |
| 2140 | |
| 2141 | case DeclarationName::CXXUsingDirective: |
| 2142 | case DeclarationName::ObjCZeroArgSelector: |
| 2143 | case DeclarationName::ObjCOneArgSelector: |
| 2144 | case DeclarationName::ObjCMultiArgSelector: |
| 2145 | break; |
| 2146 | |
| 2147 | case DeclarationName::CXXConstructorName: { |
| 2148 | CXXRecordDecl *Record = 0; |
| 2149 | QualType Ty = Name.getCXXNameType(); |
| 2150 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) |
| 2151 | Record = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 2152 | else if (const InjectedClassNameType *InjectedTy |
| 2153 | = Ty->getAs<InjectedClassNameType>()) |
| 2154 | Record = InjectedTy->getDecl(); |
| 2155 | else { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2156 | Result.AddTypedTextChunk(CopyString(Result.getAllocator(), |
| 2157 | ND->getNameAsString())); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2158 | break; |
| 2159 | } |
| 2160 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2161 | Result.AddTypedTextChunk(CopyString(Result.getAllocator(), |
| 2162 | Record->getNameAsString())); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2163 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2164 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2165 | AddTemplateParameterChunks(Context, Template, Result); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2166 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2167 | } |
| 2168 | break; |
| 2169 | } |
| 2170 | } |
| 2171 | } |
| 2172 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2173 | /// \brief If possible, create a new code completion string for the given |
| 2174 | /// result. |
| 2175 | /// |
| 2176 | /// \returns Either a new, heap-allocated code completion string describing |
| 2177 | /// how to use this result, or NULL to indicate that the string or name of the |
| 2178 | /// result is all that is needed. |
| 2179 | CodeCompletionString * |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2180 | CodeCompletionResult::CreateCodeCompletionString(Sema &S, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2181 | llvm::BumpPtrAllocator &Allocator) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2182 | typedef CodeCompletionString::Chunk Chunk; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2183 | CodeCompletionBuilder Result(Allocator, Priority, Availability); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2184 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2185 | if (Kind == RK_Pattern) { |
| 2186 | Pattern->Priority = Priority; |
| 2187 | Pattern->Availability = Availability; |
| 2188 | return Pattern; |
| 2189 | } |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2190 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2191 | if (Kind == RK_Keyword) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2192 | Result.AddTypedTextChunk(Keyword); |
| 2193 | return Result.TakeString(); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2194 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2195 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2196 | if (Kind == RK_Macro) { |
| 2197 | MacroInfo *MI = S.PP.getMacroInfo(Macro); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2198 | assert(MI && "Not a macro?"); |
| 2199 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2200 | Result.AddTypedTextChunk(CopyString(Result.getAllocator(), |
| 2201 | Macro->getName())); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2202 | |
| 2203 | if (!MI->isFunctionLike()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2204 | return Result.TakeString(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2205 | |
| 2206 | // Format a function-like macro with placeholders for the arguments. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2207 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2208 | for (MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end(); |
| 2209 | A != AEnd; ++A) { |
| 2210 | if (A != MI->arg_begin()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2211 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2212 | |
| 2213 | if (!MI->isVariadic() || A != AEnd - 1) { |
| 2214 | // Non-variadic argument. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2215 | Result.AddPlaceholderChunk(CopyString(Result.getAllocator(), |
| 2216 | (*A)->getName())); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2217 | continue; |
| 2218 | } |
| 2219 | |
| 2220 | // Variadic argument; cope with the different between GNU and C99 |
| 2221 | // variadic macros, providing a single placeholder for the rest of the |
| 2222 | // arguments. |
| 2223 | if ((*A)->isStr("__VA_ARGS__")) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2224 | Result.AddPlaceholderChunk("..."); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2225 | else { |
| 2226 | std::string Arg = (*A)->getName(); |
| 2227 | Arg += "..."; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2228 | Result.AddPlaceholderChunk(CopyString(Result.getAllocator(), Arg)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2229 | } |
| 2230 | } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2231 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
| 2232 | return Result.TakeString(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2235 | assert(Kind == RK_Declaration && "Missed a result kind?"); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2236 | NamedDecl *ND = Declaration; |
| 2237 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2238 | if (StartsNestedNameSpecifier) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2239 | Result.AddTypedTextChunk(CopyString(Result.getAllocator(), |
| 2240 | ND->getNameAsString())); |
| 2241 | Result.AddTextChunk("::"); |
| 2242 | return Result.TakeString(); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2245 | AddResultTypeChunk(S.Context, ND, Result); |
| 2246 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2247 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2248 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2249 | S.Context); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2250 | AddTypedNameChunk(S.Context, ND, Result); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2251 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2252 | AddFunctionParameterChunks(S.Context, Function, Result); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2253 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2254 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2255 | return Result.TakeString(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2259 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2260 | S.Context); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2261 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2262 | AddTypedNameChunk(S.Context, Function, Result); |
| 2263 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2264 | // Figure out which template parameters are deduced (or have default |
| 2265 | // arguments). |
| 2266 | llvm::SmallVector<bool, 16> Deduced; |
| 2267 | S.MarkDeducedTemplateParameters(FunTmpl, Deduced); |
| 2268 | unsigned LastDeducibleArgument; |
| 2269 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
| 2270 | --LastDeducibleArgument) { |
| 2271 | if (!Deduced[LastDeducibleArgument - 1]) { |
| 2272 | // C++0x: Figure out if the template argument has a default. If so, |
| 2273 | // the user doesn't need to type this argument. |
| 2274 | // FIXME: We need to abstract template parameters better! |
| 2275 | bool HasDefaultArg = false; |
| 2276 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2277 | LastDeducibleArgument - 1); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2278 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 2279 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2280 | else if (NonTypeTemplateParmDecl *NTTP |
| 2281 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 2282 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2283 | else { |
| 2284 | assert(isa<TemplateTemplateParmDecl>(Param)); |
| 2285 | HasDefaultArg |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2286 | = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | if (!HasDefaultArg) |
| 2290 | break; |
| 2291 | } |
| 2292 | } |
| 2293 | |
| 2294 | if (LastDeducibleArgument) { |
| 2295 | // Some of the function template arguments cannot be deduced from a |
| 2296 | // function call, so we introduce an explicit template argument list |
| 2297 | // containing all of the arguments up to the first deducible argument. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2298 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2299 | AddTemplateParameterChunks(S.Context, FunTmpl, Result, |
| 2300 | LastDeducibleArgument); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2301 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | // Add the function parameters |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2305 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2306 | AddFunctionParameterChunks(S.Context, Function, Result); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2307 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2308 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2309 | return Result.TakeString(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
| 2312 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2313 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2314 | S.Context); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2315 | Result.AddTypedTextChunk(CopyString(Result.getAllocator(), |
| 2316 | Template->getNameAsString())); |
| 2317 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2318 | AddTemplateParameterChunks(S.Context, Template, Result); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2319 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
| 2320 | return Result.TakeString(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2323 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2324 | Selector Sel = Method->getSelector(); |
| 2325 | if (Sel.isUnarySelector()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2326 | Result.AddTypedTextChunk(CopyString(Result.getAllocator(), |
| 2327 | Sel.getIdentifierInfoForSlot(0)->getName())); |
| 2328 | return Result.TakeString(); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2331 | std::string SelName = Sel.getIdentifierInfoForSlot(0)->getName().str(); |
| 2332 | SelName += ':'; |
| 2333 | if (StartParameter == 0) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2334 | Result.AddTypedTextChunk(CopyString(Result.getAllocator(), SelName)); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2335 | else { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2336 | Result.AddInformativeChunk(CopyString(Result.getAllocator(), SelName)); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2337 | |
| 2338 | // If there is only one parameter, and we're past it, add an empty |
| 2339 | // typed-text chunk since there is nothing to type. |
| 2340 | if (Method->param_size() == 1) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2341 | Result.AddTypedTextChunk(""); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2342 | } |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2343 | unsigned Idx = 0; |
| 2344 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 2345 | PEnd = Method->param_end(); |
| 2346 | P != PEnd; (void)++P, ++Idx) { |
| 2347 | if (Idx > 0) { |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2348 | std::string Keyword; |
| 2349 | if (Idx > StartParameter) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2350 | Result.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2351 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) |
| 2352 | Keyword += II->getName().str(); |
| 2353 | Keyword += ":"; |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2354 | if (Idx < StartParameter || AllParametersAreInformative) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2355 | Result.AddInformativeChunk(CopyString(Result.getAllocator(), |
| 2356 | Keyword)); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2357 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2358 | Result.AddTypedTextChunk(CopyString(Result.getAllocator(), Keyword)); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2359 | } |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2360 | |
| 2361 | // If we're before the starting parameter, skip the placeholder. |
| 2362 | if (Idx < StartParameter) |
| 2363 | continue; |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2364 | |
| 2365 | std::string Arg; |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2366 | |
| 2367 | if ((*P)->getType()->isBlockPointerType() && !DeclaringEntity) |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2368 | Arg = FormatFunctionParameter(S.Context, *P, true); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2369 | else { |
| 2370 | (*P)->getType().getAsStringInternal(Arg, S.Context.PrintingPolicy); |
| 2371 | Arg = "(" + Arg + ")"; |
| 2372 | if (IdentifierInfo *II = (*P)->getIdentifier()) |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2373 | if (DeclaringEntity || AllParametersAreInformative) |
| 2374 | Arg += II->getName().str(); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2377 | if (Method->isVariadic() && (P + 1) == PEnd) |
| 2378 | Arg += ", ..."; |
| 2379 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2380 | if (DeclaringEntity) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2381 | Result.AddTextChunk(CopyString(Result.getAllocator(), Arg)); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2382 | else if (AllParametersAreInformative) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2383 | Result.AddInformativeChunk(CopyString(Result.getAllocator(), Arg)); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2384 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2385 | Result.AddPlaceholderChunk(CopyString(Result.getAllocator(), Arg)); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
Douglas Gregor | 2a17af0 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2388 | if (Method->isVariadic()) { |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2389 | if (Method->param_size() == 0) { |
| 2390 | if (DeclaringEntity) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2391 | Result.AddTextChunk(", ..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2392 | else if (AllParametersAreInformative) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2393 | Result.AddInformativeChunk(", ..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2394 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2395 | Result.AddPlaceholderChunk(", ..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2396 | } |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2397 | |
| 2398 | MaybeAddSentinel(S.Context, Method, Result); |
Douglas Gregor | 2a17af0 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2401 | return Result.TakeString(); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2404 | if (Qualifier) |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2405 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2406 | S.Context); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2407 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2408 | Result.AddTypedTextChunk(CopyString(Result.getAllocator(), |
| 2409 | ND->getNameAsString())); |
| 2410 | return Result.TakeString(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2413 | CodeCompletionString * |
| 2414 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( |
| 2415 | unsigned CurrentArg, |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 2416 | Sema &S, |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2417 | llvm::BumpPtrAllocator &Allocator) const { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2418 | typedef CodeCompletionString::Chunk Chunk; |
| 2419 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2420 | // FIXME: Set priority, availability appropriately. |
| 2421 | CodeCompletionBuilder Result(Allocator, 1, CXAvailability_Available); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2422 | FunctionDecl *FDecl = getFunction(); |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2423 | AddResultTypeChunk(S.Context, FDecl, Result); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2424 | const FunctionProtoType *Proto |
| 2425 | = dyn_cast<FunctionProtoType>(getFunctionType()); |
| 2426 | if (!FDecl && !Proto) { |
| 2427 | // Function without a prototype. Just give the return type and a |
| 2428 | // highlighted ellipsis. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2429 | // FIXME: Fast-path common types? |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2430 | const FunctionType *FT = getFunctionType(); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2431 | Result.AddTextChunk(CopyString(Result.getAllocator(), |
| 2432 | FT->getResultType().getAsString(S.Context.PrintingPolicy))); |
| 2433 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
| 2434 | Result.AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "...")); |
| 2435 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
| 2436 | return Result.TakeString(); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | if (FDecl) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2440 | Result.AddTextChunk(CopyString(Result.getAllocator(), |
| 2441 | FDecl->getNameAsString())); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2442 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2443 | Result.AddTextChunk( |
| 2444 | CopyString(Result.getAllocator(), |
| 2445 | Proto->getResultType().getAsString(S.Context.PrintingPolicy))); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2446 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2447 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2448 | unsigned NumParams = FDecl? FDecl->getNumParams() : Proto->getNumArgs(); |
| 2449 | for (unsigned I = 0; I != NumParams; ++I) { |
| 2450 | if (I) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2451 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2452 | |
| 2453 | std::string ArgString; |
| 2454 | QualType ArgType; |
| 2455 | |
| 2456 | if (FDecl) { |
| 2457 | ArgString = FDecl->getParamDecl(I)->getNameAsString(); |
| 2458 | ArgType = FDecl->getParamDecl(I)->getOriginalType(); |
| 2459 | } else { |
| 2460 | ArgType = Proto->getArgType(I); |
| 2461 | } |
| 2462 | |
| 2463 | ArgType.getAsStringInternal(ArgString, S.Context.PrintingPolicy); |
| 2464 | |
| 2465 | if (I == CurrentArg) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2466 | Result.AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, |
| 2467 | CopyString(Result.getAllocator(), ArgString))); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2468 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2469 | Result.AddTextChunk(CopyString(Result.getAllocator(), ArgString)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | if (Proto && Proto->isVariadic()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2473 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2474 | if (CurrentArg < NumParams) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2475 | Result.AddTextChunk("..."); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2476 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2477 | Result.AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "...")); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2478 | } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2479 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2480 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2481 | return Result.TakeString(); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2484 | unsigned clang::getMacroUsagePriority(llvm::StringRef MacroName, |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2485 | const LangOptions &LangOpts, |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2486 | bool PreferredTypeIsPointer) { |
| 2487 | unsigned Priority = CCP_Macro; |
| 2488 | |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2489 | // Treat the "nil", "Nil" and "NULL" macros as null pointer constants. |
| 2490 | if (MacroName.equals("nil") || MacroName.equals("NULL") || |
| 2491 | MacroName.equals("Nil")) { |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2492 | Priority = CCP_Constant; |
| 2493 | if (PreferredTypeIsPointer) |
| 2494 | Priority = Priority / CCF_SimilarTypeMatch; |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2495 | } |
| 2496 | // Treat "YES", "NO", "true", and "false" as constants. |
| 2497 | else if (MacroName.equals("YES") || MacroName.equals("NO") || |
| 2498 | MacroName.equals("true") || MacroName.equals("false")) |
| 2499 | Priority = CCP_Constant; |
| 2500 | // Treat "bool" as a type. |
| 2501 | else if (MacroName.equals("bool")) |
| 2502 | Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0); |
| 2503 | |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2504 | |
| 2505 | return Priority; |
| 2506 | } |
| 2507 | |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 2508 | CXCursorKind clang::getCursorKindForDecl(Decl *D) { |
| 2509 | if (!D) |
| 2510 | return CXCursor_UnexposedDecl; |
| 2511 | |
| 2512 | switch (D->getKind()) { |
| 2513 | case Decl::Enum: return CXCursor_EnumDecl; |
| 2514 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 2515 | case Decl::Field: return CXCursor_FieldDecl; |
| 2516 | case Decl::Function: |
| 2517 | return CXCursor_FunctionDecl; |
| 2518 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 2519 | case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryImplDecl; |
| 2520 | case Decl::ObjCClass: |
| 2521 | // FIXME |
| 2522 | return CXCursor_UnexposedDecl; |
| 2523 | case Decl::ObjCForwardProtocol: |
| 2524 | // FIXME |
| 2525 | return CXCursor_UnexposedDecl; |
| 2526 | case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl; |
| 2527 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
| 2528 | case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl; |
| 2529 | case Decl::ObjCMethod: |
| 2530 | return cast<ObjCMethodDecl>(D)->isInstanceMethod() |
| 2531 | ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl; |
| 2532 | case Decl::CXXMethod: return CXCursor_CXXMethod; |
| 2533 | case Decl::CXXConstructor: return CXCursor_Constructor; |
| 2534 | case Decl::CXXDestructor: return CXCursor_Destructor; |
| 2535 | case Decl::CXXConversion: return CXCursor_ConversionFunction; |
| 2536 | case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl; |
| 2537 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
| 2538 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 2539 | case Decl::Typedef: return CXCursor_TypedefDecl; |
| 2540 | case Decl::Var: return CXCursor_VarDecl; |
| 2541 | case Decl::Namespace: return CXCursor_Namespace; |
| 2542 | case Decl::NamespaceAlias: return CXCursor_NamespaceAlias; |
| 2543 | case Decl::TemplateTypeParm: return CXCursor_TemplateTypeParameter; |
| 2544 | case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter; |
| 2545 | case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter; |
| 2546 | case Decl::FunctionTemplate: return CXCursor_FunctionTemplate; |
| 2547 | case Decl::ClassTemplate: return CXCursor_ClassTemplate; |
| 2548 | case Decl::ClassTemplatePartialSpecialization: |
| 2549 | return CXCursor_ClassTemplatePartialSpecialization; |
| 2550 | case Decl::UsingDirective: return CXCursor_UsingDirective; |
| 2551 | |
| 2552 | case Decl::Using: |
| 2553 | case Decl::UnresolvedUsingValue: |
| 2554 | case Decl::UnresolvedUsingTypename: |
| 2555 | return CXCursor_UsingDeclaration; |
| 2556 | |
| 2557 | default: |
| 2558 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 2559 | switch (TD->getTagKind()) { |
| 2560 | case TTK_Struct: return CXCursor_StructDecl; |
| 2561 | case TTK_Class: return CXCursor_ClassDecl; |
| 2562 | case TTK_Union: return CXCursor_UnionDecl; |
| 2563 | case TTK_Enum: return CXCursor_EnumDecl; |
| 2564 | } |
| 2565 | } |
| 2566 | } |
| 2567 | |
| 2568 | return CXCursor_UnexposedDecl; |
| 2569 | } |
| 2570 | |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2571 | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, |
| 2572 | bool TargetTypeIsPointer = false) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2573 | typedef CodeCompletionResult Result; |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2574 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2575 | Results.EnterNewScope(); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2576 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2577 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 2578 | MEnd = PP.macro_end(); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2579 | M != MEnd; ++M) { |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2580 | Results.AddResult(Result(M->first, |
| 2581 | getMacroUsagePriority(M->first->getName(), |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2582 | PP.getLangOptions(), |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2583 | TargetTypeIsPointer))); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2584 | } |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2585 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2586 | Results.ExitScope(); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2587 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2590 | static void AddPrettyFunctionResults(const LangOptions &LangOpts, |
| 2591 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2592 | typedef CodeCompletionResult Result; |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2593 | |
| 2594 | Results.EnterNewScope(); |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2595 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2596 | Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant)); |
| 2597 | Results.AddResult(Result("__FUNCTION__", CCP_Constant)); |
| 2598 | if (LangOpts.C99 || LangOpts.CPlusPlus0x) |
| 2599 | Results.AddResult(Result("__func__", CCP_Constant)); |
| 2600 | Results.ExitScope(); |
| 2601 | } |
| 2602 | |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2603 | static void HandleCodeCompleteResults(Sema *S, |
| 2604 | CodeCompleteConsumer *CodeCompleter, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2605 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2606 | CodeCompletionResult *Results, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2607 | unsigned NumResults) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2608 | if (CodeCompleter) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2609 | CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2612 | static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S, |
| 2613 | Sema::ParserCompletionContext PCC) { |
| 2614 | switch (PCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2615 | case Sema::PCC_Namespace: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2616 | return CodeCompletionContext::CCC_TopLevel; |
| 2617 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2618 | case Sema::PCC_Class: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2619 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 2620 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2621 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2622 | return CodeCompletionContext::CCC_ObjCInterface; |
| 2623 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2624 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2625 | return CodeCompletionContext::CCC_ObjCImplementation; |
| 2626 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2627 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2628 | return CodeCompletionContext::CCC_ObjCIvarList; |
| 2629 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2630 | case Sema::PCC_Template: |
| 2631 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2632 | if (S.CurContext->isFileContext()) |
| 2633 | return CodeCompletionContext::CCC_TopLevel; |
| 2634 | else if (S.CurContext->isRecord()) |
| 2635 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 2636 | else |
| 2637 | return CodeCompletionContext::CCC_Other; |
| 2638 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2639 | case Sema::PCC_RecoveryInFunction: |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2640 | return CodeCompletionContext::CCC_Recovery; |
Douglas Gregor | a5450a0 | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 2641 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2642 | case Sema::PCC_ForInit: |
Douglas Gregor | a5450a0 | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 2643 | if (S.getLangOptions().CPlusPlus || S.getLangOptions().C99 || |
| 2644 | S.getLangOptions().ObjC1) |
| 2645 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
| 2646 | else |
| 2647 | return CodeCompletionContext::CCC_Expression; |
| 2648 | |
| 2649 | case Sema::PCC_Expression: |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2650 | case Sema::PCC_Condition: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2651 | return CodeCompletionContext::CCC_Expression; |
| 2652 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2653 | case Sema::PCC_Statement: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2654 | return CodeCompletionContext::CCC_Statement; |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2655 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2656 | case Sema::PCC_Type: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2657 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2658 | |
| 2659 | case Sema::PCC_ParenthesizedExpression: |
| 2660 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | return CodeCompletionContext::CCC_Other; |
| 2664 | } |
| 2665 | |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2666 | /// \brief If we're in a C++ virtual member function, add completion results |
| 2667 | /// that invoke the functions we override, since it's common to invoke the |
| 2668 | /// overridden function as well as adding new functionality. |
| 2669 | /// |
| 2670 | /// \param S The semantic analysis object for which we are generating results. |
| 2671 | /// |
| 2672 | /// \param InContext This context in which the nested-name-specifier preceding |
| 2673 | /// the code-completion point |
| 2674 | static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, |
| 2675 | ResultBuilder &Results) { |
| 2676 | // Look through blocks. |
| 2677 | DeclContext *CurContext = S.CurContext; |
| 2678 | while (isa<BlockDecl>(CurContext)) |
| 2679 | CurContext = CurContext->getParent(); |
| 2680 | |
| 2681 | |
| 2682 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext); |
| 2683 | if (!Method || !Method->isVirtual()) |
| 2684 | return; |
| 2685 | |
| 2686 | // We need to have names for all of the parameters, if we're going to |
| 2687 | // generate a forwarding call. |
| 2688 | for (CXXMethodDecl::param_iterator P = Method->param_begin(), |
| 2689 | PEnd = Method->param_end(); |
| 2690 | P != PEnd; |
| 2691 | ++P) { |
| 2692 | if (!(*P)->getDeclName()) |
| 2693 | return; |
| 2694 | } |
| 2695 | |
| 2696 | for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(), |
| 2697 | MEnd = Method->end_overridden_methods(); |
| 2698 | M != MEnd; ++M) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2699 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2700 | CXXMethodDecl *Overridden = const_cast<CXXMethodDecl *>(*M); |
| 2701 | if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl()) |
| 2702 | continue; |
| 2703 | |
| 2704 | // If we need a nested-name-specifier, add one now. |
| 2705 | if (!InContext) { |
| 2706 | NestedNameSpecifier *NNS |
| 2707 | = getRequiredQualification(S.Context, CurContext, |
| 2708 | Overridden->getDeclContext()); |
| 2709 | if (NNS) { |
| 2710 | std::string Str; |
| 2711 | llvm::raw_string_ostream OS(Str); |
| 2712 | NNS->print(OS, S.Context.PrintingPolicy); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2713 | Builder.AddTextChunk(CopyString(Results.getAllocator(), OS.str())); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2714 | } |
| 2715 | } else if (!InContext->Equals(Overridden->getDeclContext())) |
| 2716 | continue; |
| 2717 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2718 | Builder.AddTypedTextChunk(CopyString(Results.getAllocator(), |
| 2719 | Overridden->getNameAsString())); |
| 2720 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2721 | bool FirstParam = true; |
| 2722 | for (CXXMethodDecl::param_iterator P = Method->param_begin(), |
| 2723 | PEnd = Method->param_end(); |
| 2724 | P != PEnd; ++P) { |
| 2725 | if (FirstParam) |
| 2726 | FirstParam = false; |
| 2727 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2728 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2729 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2730 | Builder.AddPlaceholderChunk(CopyString(Results.getAllocator(), |
| 2731 | (*P)->getIdentifier()->getName())); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2732 | } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2733 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2734 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2735 | CCP_SuperCompletion, |
| 2736 | CXCursor_CXXMethod)); |
| 2737 | Results.Ignore(Overridden); |
| 2738 | } |
| 2739 | } |
| 2740 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2741 | void Sema::CodeCompleteOrdinaryName(Scope *S, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2742 | ParserCompletionContext CompletionContext) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2743 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2744 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2745 | mapCodeCompletionContext(*this, CompletionContext)); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2746 | Results.EnterNewScope(); |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 2747 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2748 | // Determine how to filter results, e.g., so that the names of |
| 2749 | // values (functions, enumerators, function templates, etc.) are |
| 2750 | // only allowed where we can have an expression. |
| 2751 | switch (CompletionContext) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2752 | case PCC_Namespace: |
| 2753 | case PCC_Class: |
| 2754 | case PCC_ObjCInterface: |
| 2755 | case PCC_ObjCImplementation: |
| 2756 | case PCC_ObjCInstanceVariableList: |
| 2757 | case PCC_Template: |
| 2758 | case PCC_MemberTemplate: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2759 | case PCC_Type: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2760 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 2761 | break; |
| 2762 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2763 | case PCC_Statement: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2764 | case PCC_ParenthesizedExpression: |
Douglas Gregor | eb0d014 | 2010-08-24 23:58:17 +0000 | [diff] [blame] | 2765 | case PCC_Expression: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2766 | case PCC_ForInit: |
| 2767 | case PCC_Condition: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 2768 | if (WantTypesInContext(CompletionContext, getLangOptions())) |
| 2769 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 2770 | else |
| 2771 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2772 | |
| 2773 | if (getLangOptions().CPlusPlus) |
| 2774 | MaybeAddOverrideCalls(*this, /*InContext=*/0, Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2775 | break; |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 2776 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2777 | case PCC_RecoveryInFunction: |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 2778 | // Unfiltered |
| 2779 | break; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 2782 | // If we are in a C++ non-static member function, check the qualifiers on |
| 2783 | // the member function to filter/prioritize the results list. |
| 2784 | if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext)) |
| 2785 | if (CurMethod->isInstance()) |
| 2786 | Results.setObjectTypeQualifiers( |
| 2787 | Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers())); |
| 2788 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 2789 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2790 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 2791 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 2a7925c | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 2792 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2793 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); |
Douglas Gregor | 2a7925c | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 2794 | Results.ExitScope(); |
| 2795 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2796 | switch (CompletionContext) { |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2797 | case PCC_ParenthesizedExpression: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2798 | case PCC_Expression: |
| 2799 | case PCC_Statement: |
| 2800 | case PCC_RecoveryInFunction: |
| 2801 | if (S->getFnParent()) |
| 2802 | AddPrettyFunctionResults(PP.getLangOptions(), Results); |
| 2803 | break; |
| 2804 | |
| 2805 | case PCC_Namespace: |
| 2806 | case PCC_Class: |
| 2807 | case PCC_ObjCInterface: |
| 2808 | case PCC_ObjCImplementation: |
| 2809 | case PCC_ObjCInstanceVariableList: |
| 2810 | case PCC_Template: |
| 2811 | case PCC_MemberTemplate: |
| 2812 | case PCC_ForInit: |
| 2813 | case PCC_Condition: |
| 2814 | case PCC_Type: |
| 2815 | break; |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2818 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2819 | AddMacroResults(PP, Results); |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2820 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 2821 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2822 | Results.data(),Results.size()); |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2825 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| 2826 | ParsedType Receiver, |
| 2827 | IdentifierInfo **SelIdents, |
| 2828 | unsigned NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2829 | bool AtArgumentExpression, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2830 | bool IsSuper, |
| 2831 | ResultBuilder &Results); |
| 2832 | |
| 2833 | void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, |
| 2834 | bool AllowNonIdentifiers, |
| 2835 | bool AllowNestedNameSpecifiers) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2836 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2837 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2838 | AllowNestedNameSpecifiers |
| 2839 | ? CodeCompletionContext::CCC_PotentiallyQualifiedName |
| 2840 | : CodeCompletionContext::CCC_Name); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2841 | Results.EnterNewScope(); |
| 2842 | |
| 2843 | // Type qualifiers can come after names. |
| 2844 | Results.AddResult(Result("const")); |
| 2845 | Results.AddResult(Result("volatile")); |
| 2846 | if (getLangOptions().C99) |
| 2847 | Results.AddResult(Result("restrict")); |
| 2848 | |
| 2849 | if (getLangOptions().CPlusPlus) { |
| 2850 | if (AllowNonIdentifiers) { |
| 2851 | Results.AddResult(Result("operator")); |
| 2852 | } |
| 2853 | |
| 2854 | // Add nested-name-specifiers. |
| 2855 | if (AllowNestedNameSpecifiers) { |
| 2856 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2857 | Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2858 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 2859 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
| 2860 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2861 | Results.setFilter(0); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2862 | } |
| 2863 | } |
| 2864 | Results.ExitScope(); |
| 2865 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2866 | // If we're in a context where we might have an expression (rather than a |
| 2867 | // declaration), and what we've seen so far is an Objective-C type that could |
| 2868 | // be a receiver of a class message, this may be a class message send with |
| 2869 | // the initial opening bracket '[' missing. Add appropriate completions. |
| 2870 | if (AllowNonIdentifiers && !AllowNestedNameSpecifiers && |
| 2871 | DS.getTypeSpecType() == DeclSpec::TST_typename && |
| 2872 | DS.getStorageClassSpecAsWritten() == DeclSpec::SCS_unspecified && |
| 2873 | !DS.isThreadSpecified() && !DS.isExternInLinkageSpec() && |
| 2874 | DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified && |
| 2875 | DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 2876 | DS.getTypeQualifiers() == 0 && |
| 2877 | S && |
| 2878 | (S->getFlags() & Scope::DeclScope) != 0 && |
| 2879 | (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope | |
| 2880 | Scope::FunctionPrototypeScope | |
| 2881 | Scope::AtCatchScope)) == 0) { |
| 2882 | ParsedType T = DS.getRepAsType(); |
| 2883 | if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType()) |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2884 | AddClassMessageCompletions(*this, S, T, 0, 0, false, false, Results); |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Douglas Gregor | 4497dd4 | 2010-08-24 04:59:56 +0000 | [diff] [blame] | 2887 | // Note that we intentionally suppress macro results here, since we do not |
| 2888 | // encourage using macros to produce the names of entities. |
| 2889 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2890 | HandleCodeCompleteResults(this, CodeCompleter, |
| 2891 | Results.getCompletionContext(), |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2892 | Results.data(), Results.size()); |
| 2893 | } |
| 2894 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2895 | struct Sema::CodeCompleteExpressionData { |
| 2896 | CodeCompleteExpressionData(QualType PreferredType = QualType()) |
| 2897 | : PreferredType(PreferredType), IntegralConstantExpression(false), |
| 2898 | ObjCCollection(false) { } |
| 2899 | |
| 2900 | QualType PreferredType; |
| 2901 | bool IntegralConstantExpression; |
| 2902 | bool ObjCCollection; |
| 2903 | llvm::SmallVector<Decl *, 4> IgnoreDecls; |
| 2904 | }; |
| 2905 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2906 | /// \brief Perform code-completion in an expression context when we know what |
| 2907 | /// type we're looking for. |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 2908 | /// |
| 2909 | /// \param IntegralConstantExpression Only permit integral constant |
| 2910 | /// expressions. |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2911 | void Sema::CodeCompleteExpression(Scope *S, |
| 2912 | const CodeCompleteExpressionData &Data) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2913 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 2914 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 2915 | CodeCompletionContext::CCC_Expression); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2916 | if (Data.ObjCCollection) |
| 2917 | Results.setFilter(&ResultBuilder::IsObjCCollection); |
| 2918 | else if (Data.IntegralConstantExpression) |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 2919 | Results.setFilter(&ResultBuilder::IsIntegralConstantValue); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2920 | else if (WantTypesInContext(PCC_Expression, getLangOptions())) |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2921 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 2922 | else |
| 2923 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2924 | |
| 2925 | if (!Data.PreferredType.isNull()) |
| 2926 | Results.setPreferredType(Data.PreferredType.getNonReferenceType()); |
| 2927 | |
| 2928 | // Ignore any declarations that we were told that we don't care about. |
| 2929 | for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I) |
| 2930 | Results.Ignore(Data.IgnoreDecls[I]); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2931 | |
| 2932 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2933 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 2934 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2935 | |
| 2936 | Results.EnterNewScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2937 | AddOrdinaryNameResults(PCC_Expression, S, *this, Results); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2938 | Results.ExitScope(); |
| 2939 | |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2940 | bool PreferredTypeIsPointer = false; |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2941 | if (!Data.PreferredType.isNull()) |
| 2942 | PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType() |
| 2943 | || Data.PreferredType->isMemberPointerType() |
| 2944 | || Data.PreferredType->isBlockPointerType(); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2945 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2946 | if (S->getFnParent() && |
| 2947 | !Data.ObjCCollection && |
| 2948 | !Data.IntegralConstantExpression) |
| 2949 | AddPrettyFunctionResults(PP.getLangOptions(), Results); |
| 2950 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2951 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2952 | AddMacroResults(PP, Results, PreferredTypeIsPointer); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2953 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2954 | CodeCompletionContext(CodeCompletionContext::CCC_Expression, |
| 2955 | Data.PreferredType), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2956 | Results.data(),Results.size()); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
Douglas Gregor | ac5fd84 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 2959 | void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) { |
| 2960 | if (E.isInvalid()) |
| 2961 | CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction); |
| 2962 | else if (getLangOptions().ObjC1) |
| 2963 | CodeCompleteObjCInstanceMessage(S, E.take(), 0, 0, false); |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 2964 | } |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2965 | |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 2966 | /// \brief The set of properties that have already been added, referenced by |
| 2967 | /// property name. |
| 2968 | typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet; |
| 2969 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2970 | static void AddObjCProperties(ObjCContainerDecl *Container, |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2971 | bool AllowCategories, |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2972 | DeclContext *CurContext, |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 2973 | AddedPropertiesSet &AddedProperties, |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2974 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2975 | typedef CodeCompletionResult Result; |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2976 | |
| 2977 | // Add properties in this container. |
| 2978 | for (ObjCContainerDecl::prop_iterator P = Container->prop_begin(), |
| 2979 | PEnd = Container->prop_end(); |
| 2980 | P != PEnd; |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 2981 | ++P) { |
| 2982 | if (AddedProperties.insert(P->getIdentifier())) |
| 2983 | Results.MaybeAddResult(Result(*P, 0), CurContext); |
| 2984 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2985 | |
| 2986 | // Add properties in referenced protocols. |
| 2987 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 2988 | for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(), |
| 2989 | PEnd = Protocol->protocol_end(); |
| 2990 | P != PEnd; ++P) |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 2991 | AddObjCProperties(*P, AllowCategories, CurContext, AddedProperties, |
| 2992 | Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2993 | } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){ |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2994 | if (AllowCategories) { |
| 2995 | // Look through categories. |
| 2996 | for (ObjCCategoryDecl *Category = IFace->getCategoryList(); |
| 2997 | Category; Category = Category->getNextClassCategory()) |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 2998 | AddObjCProperties(Category, AllowCategories, CurContext, |
| 2999 | AddedProperties, Results); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3000 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3001 | |
| 3002 | // Look through protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 3003 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 3004 | I = IFace->all_referenced_protocol_begin(), |
| 3005 | E = IFace->all_referenced_protocol_end(); I != E; ++I) |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3006 | AddObjCProperties(*I, AllowCategories, CurContext, AddedProperties, |
| 3007 | Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3008 | |
| 3009 | // Look in the superclass. |
| 3010 | if (IFace->getSuperClass()) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3011 | AddObjCProperties(IFace->getSuperClass(), AllowCategories, CurContext, |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3012 | AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3013 | } else if (const ObjCCategoryDecl *Category |
| 3014 | = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 3015 | // Look through protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 3016 | for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(), |
| 3017 | PEnd = Category->protocol_end(); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3018 | P != PEnd; ++P) |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3019 | AddObjCProperties(*P, AllowCategories, CurContext, AddedProperties, |
| 3020 | Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3021 | } |
| 3022 | } |
| 3023 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3024 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, ExprTy *BaseE, |
| 3025 | SourceLocation OpLoc, |
| 3026 | bool IsArrow) { |
| 3027 | if (!BaseE || !CodeCompleter) |
| 3028 | return; |
| 3029 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3030 | typedef CodeCompletionResult Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3031 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3032 | Expr *Base = static_cast<Expr *>(BaseE); |
| 3033 | QualType BaseType = Base->getType(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3034 | |
| 3035 | if (IsArrow) { |
| 3036 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 3037 | BaseType = Ptr->getPointeeType(); |
| 3038 | else if (BaseType->isObjCObjectPointerType()) |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3039 | /*Do nothing*/ ; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3040 | else |
| 3041 | return; |
| 3042 | } |
| 3043 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3044 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3045 | CodeCompletionContext(CodeCompletionContext::CCC_MemberAccess, |
| 3046 | BaseType), |
| 3047 | &ResultBuilder::IsMember); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3048 | Results.EnterNewScope(); |
| 3049 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3050 | // Indicate that we are performing a member access, and the cv-qualifiers |
| 3051 | // for the base object type. |
| 3052 | Results.setObjectTypeQualifiers(BaseType.getQualifiers()); |
| 3053 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3054 | // Access to a C/C++ class, struct, or union. |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 3055 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3056 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3057 | LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer, |
| 3058 | CodeCompleter->includeGlobals()); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3059 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3060 | if (getLangOptions().CPlusPlus) { |
| 3061 | if (!Results.empty()) { |
| 3062 | // The "template" keyword can follow "->" or "." in the grammar. |
| 3063 | // However, we only want to suggest the template keyword if something |
| 3064 | // is dependent. |
| 3065 | bool IsDependent = BaseType->isDependentType(); |
| 3066 | if (!IsDependent) { |
| 3067 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) |
| 3068 | if (DeclContext *Ctx = (DeclContext *)DepScope->getEntity()) { |
| 3069 | IsDependent = Ctx->isDependentContext(); |
| 3070 | break; |
| 3071 | } |
| 3072 | } |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3073 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3074 | if (IsDependent) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3075 | Results.AddResult(Result("template")); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3076 | } |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3077 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3078 | } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) { |
| 3079 | // Objective-C property reference. |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3080 | AddedPropertiesSet AddedProperties; |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3081 | |
| 3082 | // Add property results based on our interface. |
| 3083 | const ObjCObjectPointerType *ObjCPtr |
| 3084 | = BaseType->getAsObjCInterfacePointerType(); |
| 3085 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!"); |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3086 | AddObjCProperties(ObjCPtr->getInterfaceDecl(), true, CurContext, |
| 3087 | AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3088 | |
| 3089 | // Add properties from the protocols in a qualified interface. |
| 3090 | for (ObjCObjectPointerType::qual_iterator I = ObjCPtr->qual_begin(), |
| 3091 | E = ObjCPtr->qual_end(); |
| 3092 | I != E; ++I) |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3093 | AddObjCProperties(*I, true, CurContext, AddedProperties, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3094 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3095 | (!IsArrow && BaseType->isObjCObjectType())) { |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3096 | // Objective-C instance variable access. |
| 3097 | ObjCInterfaceDecl *Class = 0; |
| 3098 | if (const ObjCObjectPointerType *ObjCPtr |
| 3099 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 3100 | Class = ObjCPtr->getInterfaceDecl(); |
| 3101 | else |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3102 | Class = BaseType->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3103 | |
| 3104 | // Add all ivars from this class and its superclasses. |
Douglas Gregor | 80f4f4c | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 3105 | if (Class) { |
| 3106 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3107 | Results.setFilter(&ResultBuilder::IsObjCIvar); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3108 | LookupVisibleDecls(Class, LookupMemberName, Consumer, |
| 3109 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3110 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3111 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3112 | |
| 3113 | // FIXME: How do we cope with isa? |
| 3114 | |
| 3115 | Results.ExitScope(); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3116 | |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3117 | // Hand off the results found for code completion. |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3118 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3119 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3120 | Results.data(),Results.size()); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3121 | } |
| 3122 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3123 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { |
| 3124 | if (!CodeCompleter) |
| 3125 | return; |
| 3126 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3127 | typedef CodeCompletionResult Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3128 | ResultBuilder::LookupFilter Filter = 0; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3129 | enum CodeCompletionContext::Kind ContextKind |
| 3130 | = CodeCompletionContext::CCC_Other; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3131 | switch ((DeclSpec::TST)TagSpec) { |
| 3132 | case DeclSpec::TST_enum: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3133 | Filter = &ResultBuilder::IsEnum; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3134 | ContextKind = CodeCompletionContext::CCC_EnumTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3135 | break; |
| 3136 | |
| 3137 | case DeclSpec::TST_union: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3138 | Filter = &ResultBuilder::IsUnion; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3139 | ContextKind = CodeCompletionContext::CCC_UnionTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3140 | break; |
| 3141 | |
| 3142 | case DeclSpec::TST_struct: |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3143 | case DeclSpec::TST_class: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3144 | Filter = &ResultBuilder::IsClassOrStruct; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3145 | ContextKind = CodeCompletionContext::CCC_ClassOrStructTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3146 | break; |
| 3147 | |
| 3148 | default: |
| 3149 | assert(false && "Unknown type specifier kind in CodeCompleteTag"); |
| 3150 | return; |
| 3151 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3152 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3153 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), ContextKind); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3154 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 3155 | |
| 3156 | // First pass: look for tags. |
| 3157 | Results.setFilter(Filter); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3158 | LookupVisibleDecls(S, LookupTagName, Consumer, |
| 3159 | CodeCompleter->includeGlobals()); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 3160 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3161 | if (CodeCompleter->includeGlobals()) { |
| 3162 | // Second pass: look for nested name specifiers. |
| 3163 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
| 3164 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer); |
| 3165 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3166 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3167 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3168 | Results.data(),Results.size()); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3169 | } |
| 3170 | |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3171 | void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3172 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3173 | CodeCompletionContext::CCC_TypeQualifiers); |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3174 | Results.EnterNewScope(); |
| 3175 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const)) |
| 3176 | Results.AddResult("const"); |
| 3177 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile)) |
| 3178 | Results.AddResult("volatile"); |
| 3179 | if (getLangOptions().C99 && |
| 3180 | !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict)) |
| 3181 | Results.AddResult("restrict"); |
| 3182 | Results.ExitScope(); |
| 3183 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3184 | Results.getCompletionContext(), |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3185 | Results.data(), Results.size()); |
| 3186 | } |
| 3187 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3188 | void Sema::CodeCompleteCase(Scope *S) { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3189 | if (getCurFunction()->SwitchStack.empty() || !CodeCompleter) |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3190 | return; |
| 3191 | |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3192 | SwitchStmt *Switch = getCurFunction()->SwitchStack.back(); |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3193 | if (!Switch->getCond()->getType()->isEnumeralType()) { |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3194 | CodeCompleteExpressionData Data(Switch->getCond()->getType()); |
| 3195 | Data.IntegralConstantExpression = true; |
| 3196 | CodeCompleteExpression(S, Data); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3197 | return; |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3198 | } |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3199 | |
| 3200 | // Code-complete the cases of a switch statement over an enumeration type |
| 3201 | // by providing the list of |
| 3202 | EnumDecl *Enum = Switch->getCond()->getType()->getAs<EnumType>()->getDecl(); |
| 3203 | |
| 3204 | // Determine which enumerators we have already seen in the switch statement. |
| 3205 | // FIXME: Ideally, we would also be able to look *past* the code-completion |
| 3206 | // token, in case we are code-completing in the middle of the switch and not |
| 3207 | // at the end. However, we aren't able to do so at the moment. |
| 3208 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3209 | NestedNameSpecifier *Qualifier = 0; |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3210 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; |
| 3211 | SC = SC->getNextSwitchCase()) { |
| 3212 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); |
| 3213 | if (!Case) |
| 3214 | continue; |
| 3215 | |
| 3216 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); |
| 3217 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal)) |
| 3218 | if (EnumConstantDecl *Enumerator |
| 3219 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 3220 | // We look into the AST of the case statement to determine which |
| 3221 | // enumerator was named. Alternatively, we could compute the value of |
| 3222 | // the integral constant expression, then compare it against the |
| 3223 | // values of each enumerator. However, value-based approach would not |
| 3224 | // work as well with C++ templates where enumerators declared within a |
| 3225 | // template are type- and value-dependent. |
| 3226 | EnumeratorsSeen.insert(Enumerator); |
| 3227 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3228 | // If this is a qualified-id, keep track of the nested-name-specifier |
| 3229 | // 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] | 3230 | // |
| 3231 | // switch (TagD.getKind()) { |
| 3232 | // case TagDecl::TK_enum: |
| 3233 | // break; |
| 3234 | // case XXX |
| 3235 | // |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3236 | // At the XXX, our completions are TagDecl::TK_union, |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3237 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, |
| 3238 | // TK_struct, and TK_class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3239 | Qualifier = DRE->getQualifier(); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3240 | } |
| 3241 | } |
| 3242 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3243 | if (getLangOptions().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { |
| 3244 | // If there are no prior enumerators in C++, check whether we have to |
| 3245 | // qualify the names of the enumerators that we suggest, because they |
| 3246 | // may not be visible in this scope. |
| 3247 | Qualifier = getRequiredQualification(Context, CurContext, |
| 3248 | Enum->getDeclContext()); |
| 3249 | |
| 3250 | // FIXME: Scoped enums need to start with "EnumDecl" as the context! |
| 3251 | } |
| 3252 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3253 | // Add any enumerators that have not yet been mentioned. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3254 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3255 | CodeCompletionContext::CCC_Expression); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3256 | Results.EnterNewScope(); |
| 3257 | for (EnumDecl::enumerator_iterator E = Enum->enumerator_begin(), |
| 3258 | EEnd = Enum->enumerator_end(); |
| 3259 | E != EEnd; ++E) { |
| 3260 | if (EnumeratorsSeen.count(*E)) |
| 3261 | continue; |
| 3262 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3263 | Results.AddResult(CodeCompletionResult(*E, Qualifier), |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 3264 | CurContext, 0, false); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3265 | } |
| 3266 | Results.ExitScope(); |
Douglas Gregor | 2f880e4 | 2010-04-06 20:02:15 +0000 | [diff] [blame] | 3267 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3268 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3269 | AddMacroResults(PP, Results); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3270 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3271 | CodeCompletionContext::CCC_Expression, |
| 3272 | Results.data(),Results.size()); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3275 | namespace { |
| 3276 | struct IsBetterOverloadCandidate { |
| 3277 | Sema &S; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3278 | SourceLocation Loc; |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3279 | |
| 3280 | public: |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3281 | explicit IsBetterOverloadCandidate(Sema &S, SourceLocation Loc) |
| 3282 | : S(S), Loc(Loc) { } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3283 | |
| 3284 | bool |
| 3285 | operator()(const OverloadCandidate &X, const OverloadCandidate &Y) const { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3286 | return isBetterOverloadCandidate(S, X, Y, Loc); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3287 | } |
| 3288 | }; |
| 3289 | } |
| 3290 | |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3291 | static bool anyNullArguments(Expr **Args, unsigned NumArgs) { |
| 3292 | if (NumArgs && !Args) |
| 3293 | return true; |
| 3294 | |
| 3295 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3296 | if (!Args[I]) |
| 3297 | return true; |
| 3298 | |
| 3299 | return false; |
| 3300 | } |
| 3301 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3302 | void Sema::CodeCompleteCall(Scope *S, ExprTy *FnIn, |
| 3303 | ExprTy **ArgsIn, unsigned NumArgs) { |
| 3304 | if (!CodeCompleter) |
| 3305 | return; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3306 | |
| 3307 | // When we're code-completing for a call, we fall back to ordinary |
| 3308 | // name code-completion whenever we can't produce specific |
| 3309 | // results. We may want to revisit this strategy in the future, |
| 3310 | // e.g., by merging the two kinds of results. |
| 3311 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3312 | Expr *Fn = (Expr *)FnIn; |
| 3313 | Expr **Args = (Expr **)ArgsIn; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3314 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3315 | // Ignore type-dependent call expressions entirely. |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3316 | if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args, NumArgs) || |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3317 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3318 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3319 | return; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3320 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3321 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3322 | // Build an overload candidate set based on the functions we find. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3323 | SourceLocation Loc = Fn->getExprLoc(); |
| 3324 | OverloadCandidateSet CandidateSet(Loc); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3325 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3326 | // FIXME: What if we're calling something that isn't a function declaration? |
| 3327 | // FIXME: What if we're calling a pseudo-destructor? |
| 3328 | // FIXME: What if we're calling a member function? |
| 3329 | |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3330 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; |
| 3331 | llvm::SmallVector<ResultCandidate, 8> Results; |
| 3332 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3333 | Expr *NakedFn = Fn->IgnoreParenCasts(); |
| 3334 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) |
| 3335 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet, |
| 3336 | /*PartialOverloading=*/ true); |
| 3337 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(NakedFn)) { |
| 3338 | FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3339 | if (FDecl) { |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3340 | if (!getLangOptions().CPlusPlus || |
| 3341 | !FDecl->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3342 | Results.push_back(ResultCandidate(FDecl)); |
| 3343 | else |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3344 | // FIXME: access? |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3345 | AddOverloadCandidate(FDecl, DeclAccessPair::make(FDecl, AS_none), |
| 3346 | Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 3347 | false, /*PartialOverloading*/true); |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3348 | } |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3349 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3350 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3351 | QualType ParamType; |
| 3352 | |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3353 | if (!CandidateSet.empty()) { |
| 3354 | // Sort the overload candidate set by placing the best overloads first. |
| 3355 | std::stable_sort(CandidateSet.begin(), CandidateSet.end(), |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3356 | IsBetterOverloadCandidate(*this, Loc)); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3357 | |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3358 | // Add the remaining viable overload candidates as code-completion reslults. |
| 3359 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3360 | CandEnd = CandidateSet.end(); |
| 3361 | Cand != CandEnd; ++Cand) { |
| 3362 | if (Cand->Viable) |
| 3363 | Results.push_back(ResultCandidate(Cand->Function)); |
| 3364 | } |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3365 | |
| 3366 | // From the viable candidates, try to determine the type of this parameter. |
| 3367 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 3368 | if (const FunctionType *FType = Results[I].getFunctionType()) |
| 3369 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FType)) |
| 3370 | if (NumArgs < Proto->getNumArgs()) { |
| 3371 | if (ParamType.isNull()) |
| 3372 | ParamType = Proto->getArgType(NumArgs); |
| 3373 | else if (!Context.hasSameUnqualifiedType( |
| 3374 | ParamType.getNonReferenceType(), |
| 3375 | Proto->getArgType(NumArgs).getNonReferenceType())) { |
| 3376 | ParamType = QualType(); |
| 3377 | break; |
| 3378 | } |
| 3379 | } |
| 3380 | } |
| 3381 | } else { |
| 3382 | // Try to determine the parameter type from the type of the expression |
| 3383 | // being called. |
| 3384 | QualType FunctionType = Fn->getType(); |
| 3385 | if (const PointerType *Ptr = FunctionType->getAs<PointerType>()) |
| 3386 | FunctionType = Ptr->getPointeeType(); |
| 3387 | else if (const BlockPointerType *BlockPtr |
| 3388 | = FunctionType->getAs<BlockPointerType>()) |
| 3389 | FunctionType = BlockPtr->getPointeeType(); |
| 3390 | else if (const MemberPointerType *MemPtr |
| 3391 | = FunctionType->getAs<MemberPointerType>()) |
| 3392 | FunctionType = MemPtr->getPointeeType(); |
| 3393 | |
| 3394 | if (const FunctionProtoType *Proto |
| 3395 | = FunctionType->getAs<FunctionProtoType>()) { |
| 3396 | if (NumArgs < Proto->getNumArgs()) |
| 3397 | ParamType = Proto->getArgType(NumArgs); |
| 3398 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3399 | } |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3400 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3401 | if (ParamType.isNull()) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3402 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3403 | else |
| 3404 | CodeCompleteExpression(S, ParamType); |
| 3405 | |
Douglas Gregor | 2e4c7a5 | 2010-04-06 20:19:47 +0000 | [diff] [blame] | 3406 | if (!Results.empty()) |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3407 | CodeCompleter->ProcessOverloadCandidates(*this, NumArgs, Results.data(), |
| 3408 | Results.size()); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3411 | void Sema::CodeCompleteInitializer(Scope *S, Decl *D) { |
| 3412 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3413 | if (!VD) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3414 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3415 | return; |
| 3416 | } |
| 3417 | |
| 3418 | CodeCompleteExpression(S, VD->getType()); |
| 3419 | } |
| 3420 | |
| 3421 | void Sema::CodeCompleteReturn(Scope *S) { |
| 3422 | QualType ResultType; |
| 3423 | if (isa<BlockDecl>(CurContext)) { |
| 3424 | if (BlockScopeInfo *BSI = getCurBlock()) |
| 3425 | ResultType = BSI->ReturnType; |
| 3426 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext)) |
| 3427 | ResultType = Function->getResultType(); |
| 3428 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext)) |
| 3429 | ResultType = Method->getResultType(); |
| 3430 | |
| 3431 | if (ResultType.isNull()) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3432 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3433 | else |
| 3434 | CodeCompleteExpression(S, ResultType); |
| 3435 | } |
| 3436 | |
| 3437 | void Sema::CodeCompleteAssignmentRHS(Scope *S, ExprTy *LHS) { |
| 3438 | if (LHS) |
| 3439 | CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType()); |
| 3440 | else |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3441 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3442 | } |
| 3443 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3444 | void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3445 | bool EnteringContext) { |
| 3446 | if (!SS.getScopeRep() || !CodeCompleter) |
| 3447 | return; |
| 3448 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3449 | DeclContext *Ctx = computeDeclContext(SS, EnteringContext); |
| 3450 | if (!Ctx) |
| 3451 | return; |
Douglas Gregor | d1cd31a | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 3452 | |
| 3453 | // Try to instantiate any non-dependent declaration contexts before |
| 3454 | // we look in them. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3455 | if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx)) |
Douglas Gregor | d1cd31a | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 3456 | return; |
| 3457 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3458 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3459 | CodeCompletionContext::CCC_Name); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3460 | Results.EnterNewScope(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3461 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3462 | // The "template" keyword can follow "::" in the grammar, but only |
| 3463 | // put it into the grammar if the nested-name-specifier is dependent. |
| 3464 | NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 3465 | if (!Results.empty() && NNS->isDependent()) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3466 | Results.AddResult("template"); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3467 | |
| 3468 | // Add calls to overridden virtual functions, if there are any. |
| 3469 | // |
| 3470 | // FIXME: This isn't wonderful, because we don't know whether we're actually |
| 3471 | // in a context that permits expressions. This is a general issue with |
| 3472 | // qualified-id completions. |
| 3473 | if (!EnteringContext) |
| 3474 | MaybeAddOverrideCalls(*this, Ctx, Results); |
| 3475 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3476 | |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3477 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3478 | LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer); |
| 3479 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3480 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3481 | CodeCompletionContext::CCC_Name, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3482 | Results.data(),Results.size()); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3483 | } |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3484 | |
| 3485 | void Sema::CodeCompleteUsing(Scope *S) { |
| 3486 | if (!CodeCompleter) |
| 3487 | return; |
| 3488 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3489 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3490 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
| 3491 | &ResultBuilder::IsNestedNameSpecifier); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3492 | Results.EnterNewScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3493 | |
| 3494 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 3495 | if (!S->isClassScope()) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3496 | Results.AddResult(CodeCompletionResult("namespace")); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3497 | |
| 3498 | // After "using", we can see anything that would start a |
| 3499 | // nested-name-specifier. |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3500 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3501 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3502 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3503 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3504 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3505 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3506 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3507 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | void Sema::CodeCompleteUsingDirective(Scope *S) { |
| 3511 | if (!CodeCompleter) |
| 3512 | return; |
| 3513 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3514 | // After "using namespace", we expect to see a namespace name or namespace |
| 3515 | // alias. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3516 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3517 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3518 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3519 | Results.EnterNewScope(); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3520 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3521 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3522 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3523 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3524 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3525 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3526 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
| 3529 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { |
| 3530 | if (!CodeCompleter) |
| 3531 | return; |
| 3532 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3533 | DeclContext *Ctx = (DeclContext *)S->getEntity(); |
| 3534 | if (!S->getParent()) |
| 3535 | Ctx = Context.getTranslationUnitDecl(); |
| 3536 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3537 | bool SuppressedGlobalResults |
| 3538 | = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx); |
| 3539 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3540 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3541 | SuppressedGlobalResults |
| 3542 | ? CodeCompletionContext::CCC_Namespace |
| 3543 | : CodeCompletionContext::CCC_Other, |
| 3544 | &ResultBuilder::IsNamespace); |
| 3545 | |
| 3546 | if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3547 | // We only want to see those namespaces that have already been defined |
| 3548 | // within this scope, because its likely that the user is creating an |
| 3549 | // extended namespace declaration. Keep track of the most recent |
| 3550 | // definition of each namespace. |
| 3551 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
| 3552 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
| 3553 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); |
| 3554 | NS != NSEnd; ++NS) |
| 3555 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
| 3556 | |
| 3557 | // Add the most recent definition (or extended definition) of each |
| 3558 | // namespace to the list of results. |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3559 | Results.EnterNewScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3560 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
| 3561 | NS = OrigToLatest.begin(), NSEnd = OrigToLatest.end(); |
| 3562 | NS != NSEnd; ++NS) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3563 | Results.AddResult(CodeCompletionResult(NS->second, 0), |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 3564 | CurContext, 0, false); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3565 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3566 | } |
| 3567 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3568 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3569 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3570 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { |
| 3574 | if (!CodeCompleter) |
| 3575 | return; |
| 3576 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3577 | // After "namespace", we expect to see a namespace or alias. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3578 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3579 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3580 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3581 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3582 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3583 | CodeCompleter->includeGlobals()); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3584 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3585 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3586 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3587 | } |
| 3588 | |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3589 | void Sema::CodeCompleteOperatorName(Scope *S) { |
| 3590 | if (!CodeCompleter) |
| 3591 | return; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3592 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3593 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3594 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3595 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3596 | &ResultBuilder::IsType); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3597 | Results.EnterNewScope(); |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3598 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3599 | // Add the names of overloadable operators. |
| 3600 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 3601 | if (std::strcmp(Spelling, "?")) \ |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3602 | Results.AddResult(Result(Spelling)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3603 | #include "clang/Basic/OperatorKinds.def" |
| 3604 | |
| 3605 | // Add any type names visible from the current scope |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 3606 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3607 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3608 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3609 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3610 | |
| 3611 | // Add any type specifiers |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3612 | AddTypeSpecifierResults(getLangOptions(), Results); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3613 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3614 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3615 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3616 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3617 | Results.data(),Results.size()); |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3618 | } |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3619 | |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3620 | void Sema::CodeCompleteConstructorInitializer(Decl *ConstructorD, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3621 | CXXCtorInitializer** Initializers, |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3622 | unsigned NumInitializers) { |
| 3623 | CXXConstructorDecl *Constructor |
| 3624 | = static_cast<CXXConstructorDecl *>(ConstructorD); |
| 3625 | if (!Constructor) |
| 3626 | return; |
| 3627 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3628 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3629 | CodeCompletionContext::CCC_PotentiallyQualifiedName); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3630 | Results.EnterNewScope(); |
| 3631 | |
| 3632 | // Fill in any already-initialized fields or base classes. |
| 3633 | llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields; |
| 3634 | llvm::SmallPtrSet<CanQualType, 4> InitializedBases; |
| 3635 | for (unsigned I = 0; I != NumInitializers; ++I) { |
| 3636 | if (Initializers[I]->isBaseInitializer()) |
| 3637 | InitializedBases.insert( |
| 3638 | Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0))); |
| 3639 | else |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3640 | InitializedFields.insert(cast<FieldDecl>( |
| 3641 | Initializers[I]->getAnyMember())); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
| 3644 | // Add completions for base classes. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3645 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3646 | bool SawLastInitializer = (NumInitializers == 0); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3647 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 3648 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3649 | BaseEnd = ClassDecl->bases_end(); |
| 3650 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3651 | if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) { |
| 3652 | SawLastInitializer |
| 3653 | = NumInitializers > 0 && |
| 3654 | Initializers[NumInitializers - 1]->isBaseInitializer() && |
| 3655 | Context.hasSameUnqualifiedType(Base->getType(), |
| 3656 | QualType(Initializers[NumInitializers - 1]->getBaseClass(), 0)); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3657 | continue; |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3658 | } |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3659 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3660 | Builder.AddTypedTextChunk( |
| 3661 | CopyString(Results.getAllocator(), |
| 3662 | Base->getType().getAsString(Context.PrintingPolicy))); |
| 3663 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3664 | Builder.AddPlaceholderChunk("args"); |
| 3665 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3666 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3667 | SawLastInitializer? CCP_NextInitializer |
| 3668 | : CCP_MemberDeclaration)); |
| 3669 | SawLastInitializer = false; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | // Add completions for virtual base classes. |
| 3673 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 3674 | BaseEnd = ClassDecl->vbases_end(); |
| 3675 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3676 | if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) { |
| 3677 | SawLastInitializer |
| 3678 | = NumInitializers > 0 && |
| 3679 | Initializers[NumInitializers - 1]->isBaseInitializer() && |
| 3680 | Context.hasSameUnqualifiedType(Base->getType(), |
| 3681 | QualType(Initializers[NumInitializers - 1]->getBaseClass(), 0)); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3682 | continue; |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3683 | } |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3684 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3685 | Builder.AddTypedTextChunk( |
| 3686 | CopyString(Builder.getAllocator(), |
| 3687 | Base->getType().getAsString(Context.PrintingPolicy))); |
| 3688 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3689 | Builder.AddPlaceholderChunk("args"); |
| 3690 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3691 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3692 | SawLastInitializer? CCP_NextInitializer |
| 3693 | : CCP_MemberDeclaration)); |
| 3694 | SawLastInitializer = false; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | // Add completions for members. |
| 3698 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 3699 | FieldEnd = ClassDecl->field_end(); |
| 3700 | Field != FieldEnd; ++Field) { |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3701 | if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl()))) { |
| 3702 | SawLastInitializer |
| 3703 | = NumInitializers > 0 && |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3704 | Initializers[NumInitializers - 1]->isAnyMemberInitializer() && |
| 3705 | Initializers[NumInitializers - 1]->getAnyMember() == *Field; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3706 | continue; |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3707 | } |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3708 | |
| 3709 | if (!Field->getDeclName()) |
| 3710 | continue; |
| 3711 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3712 | Builder.AddTypedTextChunk(CopyString(Builder.getAllocator(), |
| 3713 | Field->getIdentifier()->getName())); |
| 3714 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3715 | Builder.AddPlaceholderChunk("args"); |
| 3716 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3717 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3718 | SawLastInitializer? CCP_NextInitializer |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3719 | : CCP_MemberDeclaration, |
| 3720 | CXCursor_MemberRef)); |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3721 | SawLastInitializer = false; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3722 | } |
| 3723 | Results.ExitScope(); |
| 3724 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3725 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3726 | Results.data(), Results.size()); |
| 3727 | } |
| 3728 | |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3729 | // Macro that expands to @Keyword or Keyword, depending on whether NeedAt is |
| 3730 | // true or false. |
| 3731 | #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) NeedAt? "@" #Keyword : #Keyword |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3732 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3733 | ResultBuilder &Results, |
| 3734 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3735 | typedef CodeCompletionResult Result; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3736 | // Since we have an implementation, we can end it. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3737 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3738 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3739 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3740 | if (LangOpts.ObjC2) { |
| 3741 | // @dynamic |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3742 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,dynamic)); |
| 3743 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3744 | Builder.AddPlaceholderChunk("property"); |
| 3745 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3746 | |
| 3747 | // @synthesize |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3748 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synthesize)); |
| 3749 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3750 | Builder.AddPlaceholderChunk("property"); |
| 3751 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3752 | } |
| 3753 | } |
| 3754 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3755 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3756 | ResultBuilder &Results, |
| 3757 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3758 | typedef CodeCompletionResult Result; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3759 | |
| 3760 | // Since we have an interface or protocol, we can end it. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3761 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3762 | |
| 3763 | if (LangOpts.ObjC2) { |
| 3764 | // @property |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3765 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,property))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3766 | |
| 3767 | // @required |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3768 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,required))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3769 | |
| 3770 | // @optional |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3771 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,optional))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3772 | } |
| 3773 | } |
| 3774 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3775 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3776 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3777 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3778 | |
| 3779 | // @class name ; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3780 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,class)); |
| 3781 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3782 | Builder.AddPlaceholderChunk("name"); |
| 3783 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3784 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3785 | if (Results.includeCodePatterns()) { |
| 3786 | // @interface name |
| 3787 | // FIXME: Could introduce the whole pattern, including superclasses and |
| 3788 | // such. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3789 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,interface)); |
| 3790 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3791 | Builder.AddPlaceholderChunk("class"); |
| 3792 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3793 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3794 | // @protocol name |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3795 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol)); |
| 3796 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3797 | Builder.AddPlaceholderChunk("protocol"); |
| 3798 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3799 | |
| 3800 | // @implementation name |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3801 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,implementation)); |
| 3802 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3803 | Builder.AddPlaceholderChunk("class"); |
| 3804 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3805 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3806 | |
| 3807 | // @compatibility_alias name |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3808 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,compatibility_alias)); |
| 3809 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3810 | Builder.AddPlaceholderChunk("alias"); |
| 3811 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3812 | Builder.AddPlaceholderChunk("class"); |
| 3813 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3814 | } |
| 3815 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3816 | void Sema::CodeCompleteObjCAtDirective(Scope *S, Decl *ObjCImpDecl, |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 3817 | bool InInterface) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3818 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3819 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3820 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 3821 | Results.EnterNewScope(); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3822 | if (ObjCImpDecl) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3823 | AddObjCImplementationResults(getLangOptions(), Results, false); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3824 | else if (InInterface) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3825 | AddObjCInterfaceResults(getLangOptions(), Results, false); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3826 | else |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3827 | AddObjCTopLevelResults(Results, false); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 3828 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3829 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3830 | CodeCompletionContext::CCC_Other, |
| 3831 | Results.data(),Results.size()); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3834 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3835 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3836 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3837 | |
| 3838 | // @encode ( type-name ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3839 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,encode)); |
| 3840 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3841 | Builder.AddPlaceholderChunk("type-name"); |
| 3842 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3843 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3844 | |
| 3845 | // @protocol ( protocol-name ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3846 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol)); |
| 3847 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3848 | Builder.AddPlaceholderChunk("protocol-name"); |
| 3849 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3850 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3851 | |
| 3852 | // @selector ( selector ) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3853 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,selector)); |
| 3854 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3855 | Builder.AddPlaceholderChunk("selector"); |
| 3856 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3857 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3858 | } |
| 3859 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3860 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3861 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3862 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3863 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3864 | if (Results.includeCodePatterns()) { |
| 3865 | // @try { statements } @catch ( declaration ) { statements } @finally |
| 3866 | // { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3867 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,try)); |
| 3868 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3869 | Builder.AddPlaceholderChunk("statements"); |
| 3870 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 3871 | Builder.AddTextChunk("@catch"); |
| 3872 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3873 | Builder.AddPlaceholderChunk("parameter"); |
| 3874 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3875 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3876 | Builder.AddPlaceholderChunk("statements"); |
| 3877 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 3878 | Builder.AddTextChunk("@finally"); |
| 3879 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3880 | Builder.AddPlaceholderChunk("statements"); |
| 3881 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 3882 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3883 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3884 | |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3885 | // @throw |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3886 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,throw)); |
| 3887 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3888 | Builder.AddPlaceholderChunk("expression"); |
| 3889 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3890 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3891 | if (Results.includeCodePatterns()) { |
| 3892 | // @synchronized ( expression ) { statements } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3893 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synchronized)); |
| 3894 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3895 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3896 | Builder.AddPlaceholderChunk("expression"); |
| 3897 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3898 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3899 | Builder.AddPlaceholderChunk("statements"); |
| 3900 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 3901 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3902 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3903 | } |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3904 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3905 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3906 | ResultBuilder &Results, |
| 3907 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3908 | typedef CodeCompletionResult Result; |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3909 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,private))); |
| 3910 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,protected))); |
| 3911 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,public))); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3912 | if (LangOpts.ObjC2) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3913 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,package))); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
| 3916 | void Sema::CodeCompleteObjCAtVisibility(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3917 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3918 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3919 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3920 | AddObjCVisibilityResults(getLangOptions(), Results, false); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3921 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3922 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3923 | CodeCompletionContext::CCC_Other, |
| 3924 | Results.data(),Results.size()); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | void Sema::CodeCompleteObjCAtStatement(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3928 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3929 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3930 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3931 | AddObjCStatementResults(Results, false); |
| 3932 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3933 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3934 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3935 | CodeCompletionContext::CCC_Other, |
| 3936 | Results.data(),Results.size()); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3937 | } |
| 3938 | |
| 3939 | void Sema::CodeCompleteObjCAtExpression(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3940 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3941 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3942 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3943 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3944 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3945 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3946 | CodeCompletionContext::CCC_Other, |
| 3947 | Results.data(),Results.size()); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3950 | /// \brief Determine whether the addition of the given flag to an Objective-C |
| 3951 | /// property's attributes will cause a conflict. |
| 3952 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { |
| 3953 | // Check if we've already added this flag. |
| 3954 | if (Attributes & NewFlag) |
| 3955 | return true; |
| 3956 | |
| 3957 | Attributes |= NewFlag; |
| 3958 | |
| 3959 | // Check for collisions with "readonly". |
| 3960 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 3961 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | |
| 3962 | ObjCDeclSpec::DQ_PR_assign | |
| 3963 | ObjCDeclSpec::DQ_PR_copy | |
| 3964 | ObjCDeclSpec::DQ_PR_retain))) |
| 3965 | return true; |
| 3966 | |
| 3967 | // Check for more than one of { assign, copy, retain }. |
| 3968 | unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign | |
| 3969 | ObjCDeclSpec::DQ_PR_copy | |
| 3970 | ObjCDeclSpec::DQ_PR_retain); |
| 3971 | if (AssignCopyRetMask && |
| 3972 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && |
| 3973 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && |
| 3974 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain) |
| 3975 | return true; |
| 3976 | |
| 3977 | return false; |
| 3978 | } |
| 3979 | |
Douglas Gregor | a93b108 | 2009-11-18 23:08:07 +0000 | [diff] [blame] | 3980 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 3981 | if (!CodeCompleter) |
| 3982 | return; |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3983 | |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 3984 | unsigned Attributes = ODS.getPropertyAttributes(); |
| 3985 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3986 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 3987 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 3988 | CodeCompletionContext::CCC_Other); |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 3989 | Results.EnterNewScope(); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3990 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3991 | Results.AddResult(CodeCompletionResult("readonly")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3992 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3993 | Results.AddResult(CodeCompletionResult("assign")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3994 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3995 | Results.AddResult(CodeCompletionResult("readwrite")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3996 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3997 | Results.AddResult(CodeCompletionResult("retain")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3998 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3999 | Results.AddResult(CodeCompletionResult("copy")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4000 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4001 | Results.AddResult(CodeCompletionResult("nonatomic")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4002 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4003 | CodeCompletionBuilder Setter(Results.getAllocator()); |
| 4004 | Setter.AddTypedTextChunk("setter"); |
| 4005 | Setter.AddTextChunk(" = "); |
| 4006 | Setter.AddPlaceholderChunk("method"); |
| 4007 | Results.AddResult(CodeCompletionResult(Setter.TakeString())); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 4008 | } |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4009 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4010 | CodeCompletionBuilder Getter(Results.getAllocator()); |
| 4011 | Getter.AddTypedTextChunk("getter"); |
| 4012 | Getter.AddTextChunk(" = "); |
| 4013 | Getter.AddPlaceholderChunk("method"); |
| 4014 | Results.AddResult(CodeCompletionResult(Getter.TakeString())); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 4015 | } |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +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()); |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4020 | } |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4021 | |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4022 | /// \brief Descripts the kind of Objective-C method that we want to find |
| 4023 | /// via code completion. |
| 4024 | enum ObjCMethodKind { |
| 4025 | MK_Any, //< Any kind of method, provided it means other specified criteria. |
| 4026 | MK_ZeroArgSelector, //< Zero-argument (unary) selector. |
| 4027 | MK_OneArgSelector //< One-argument selector. |
| 4028 | }; |
| 4029 | |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4030 | static bool isAcceptableObjCSelector(Selector Sel, |
| 4031 | ObjCMethodKind WantKind, |
| 4032 | IdentifierInfo **SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4033 | unsigned NumSelIdents, |
| 4034 | bool AllowSameLength = true) { |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4035 | if (NumSelIdents > Sel.getNumArgs()) |
| 4036 | return false; |
| 4037 | |
| 4038 | switch (WantKind) { |
| 4039 | case MK_Any: break; |
| 4040 | case MK_ZeroArgSelector: return Sel.isUnarySelector(); |
| 4041 | case MK_OneArgSelector: return Sel.getNumArgs() == 1; |
| 4042 | } |
| 4043 | |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4044 | if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs()) |
| 4045 | return false; |
| 4046 | |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4047 | for (unsigned I = 0; I != NumSelIdents; ++I) |
| 4048 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) |
| 4049 | return false; |
| 4050 | |
| 4051 | return true; |
| 4052 | } |
| 4053 | |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4054 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, |
| 4055 | ObjCMethodKind WantKind, |
| 4056 | IdentifierInfo **SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4057 | unsigned NumSelIdents, |
| 4058 | bool AllowSameLength = true) { |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4059 | return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4060 | NumSelIdents, AllowSameLength); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4061 | } |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4062 | |
| 4063 | namespace { |
| 4064 | /// \brief A set of selectors, which is used to avoid introducing multiple |
| 4065 | /// completions with the same selector into the result set. |
| 4066 | typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet; |
| 4067 | } |
| 4068 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4069 | /// \brief Add all of the Objective-C methods in the given Objective-C |
| 4070 | /// container to the set of results. |
| 4071 | /// |
| 4072 | /// The container will be a class, protocol, category, or implementation of |
| 4073 | /// any of the above. This mether will recurse to include methods from |
| 4074 | /// the superclasses of classes along with their categories, protocols, and |
| 4075 | /// implementations. |
| 4076 | /// |
| 4077 | /// \param Container the container in which we'll look to find methods. |
| 4078 | /// |
| 4079 | /// \param WantInstance whether to add instance methods (only); if false, this |
| 4080 | /// routine will add factory methods (only). |
| 4081 | /// |
| 4082 | /// \param CurContext the context in which we're performing the lookup that |
| 4083 | /// finds methods. |
| 4084 | /// |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4085 | /// \param AllowSameLength Whether we allow a method to be added to the list |
| 4086 | /// when it has the same number of parameters as we have selector identifiers. |
| 4087 | /// |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4088 | /// \param Results the structure into which we'll add results. |
| 4089 | static void AddObjCMethods(ObjCContainerDecl *Container, |
| 4090 | bool WantInstanceMethods, |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4091 | ObjCMethodKind WantKind, |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4092 | IdentifierInfo **SelIdents, |
| 4093 | unsigned NumSelIdents, |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4094 | DeclContext *CurContext, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4095 | VisitedSelectorSet &Selectors, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4096 | bool AllowSameLength, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4097 | ResultBuilder &Results, |
| 4098 | bool InOriginalClass = true) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4099 | typedef CodeCompletionResult Result; |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4100 | for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), |
| 4101 | MEnd = Container->meth_end(); |
| 4102 | M != MEnd; ++M) { |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4103 | if ((*M)->isInstanceMethod() == WantInstanceMethods) { |
| 4104 | // Check whether the selector identifiers we've been given are a |
| 4105 | // subset of the identifiers for this particular method. |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4106 | if (!isAcceptableObjCMethod(*M, WantKind, SelIdents, NumSelIdents, |
| 4107 | AllowSameLength)) |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4108 | continue; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4109 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4110 | if (!Selectors.insert((*M)->getSelector())) |
| 4111 | continue; |
| 4112 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4113 | Result R = Result(*M, 0); |
| 4114 | R.StartParameter = NumSelIdents; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4115 | R.AllParametersAreInformative = (WantKind != MK_Any); |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4116 | if (!InOriginalClass) |
| 4117 | R.Priority += CCD_InBaseClass; |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4118 | Results.MaybeAddResult(R, CurContext); |
| 4119 | } |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
Douglas Gregor | e396c7b | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 4122 | // Visit the protocols of protocols. |
| 4123 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 4124 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 4125 | = Protocol->getReferencedProtocols(); |
| 4126 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 4127 | E = Protocols.end(); |
| 4128 | I != E; ++I) |
| 4129 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4130 | CurContext, Selectors, AllowSameLength, Results, false); |
Douglas Gregor | e396c7b | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4133 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); |
| 4134 | if (!IFace) |
| 4135 | return; |
| 4136 | |
| 4137 | // Add methods in protocols. |
| 4138 | const ObjCList<ObjCProtocolDecl> &Protocols= IFace->getReferencedProtocols(); |
| 4139 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 4140 | E = Protocols.end(); |
| 4141 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4142 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4143 | CurContext, Selectors, AllowSameLength, Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4144 | |
| 4145 | // Add methods in categories. |
| 4146 | for (ObjCCategoryDecl *CatDecl = IFace->getCategoryList(); CatDecl; |
| 4147 | CatDecl = CatDecl->getNextClassCategory()) { |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4148 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4149 | NumSelIdents, CurContext, Selectors, AllowSameLength, |
| 4150 | Results, InOriginalClass); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4151 | |
| 4152 | // Add a categories protocol methods. |
| 4153 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 4154 | = CatDecl->getReferencedProtocols(); |
| 4155 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 4156 | E = Protocols.end(); |
| 4157 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4158 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4159 | NumSelIdents, CurContext, Selectors, AllowSameLength, |
| 4160 | Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4161 | |
| 4162 | // Add methods in category implementations. |
| 4163 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4164 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4165 | NumSelIdents, CurContext, Selectors, AllowSameLength, |
| 4166 | Results, InOriginalClass); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | // Add methods in superclass. |
| 4170 | if (IFace->getSuperClass()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4171 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4172 | SelIdents, NumSelIdents, CurContext, Selectors, |
| 4173 | AllowSameLength, Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4174 | |
| 4175 | // Add methods in our implementation, if any. |
| 4176 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4177 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4178 | NumSelIdents, CurContext, Selectors, AllowSameLength, |
| 4179 | Results, InOriginalClass); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4180 | } |
| 4181 | |
| 4182 | |
Douglas Gregor | bdb2d50 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 4183 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S, Decl *ClassDecl) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4184 | typedef CodeCompletionResult Result; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4185 | |
| 4186 | // Try to find the interface where getters might live. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4187 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(ClassDecl); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4188 | if (!Class) { |
| 4189 | if (ObjCCategoryDecl *Category |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4190 | = dyn_cast_or_null<ObjCCategoryDecl>(ClassDecl)) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4191 | Class = Category->getClassInterface(); |
| 4192 | |
| 4193 | if (!Class) |
| 4194 | return; |
| 4195 | } |
| 4196 | |
| 4197 | // Find all of the potential getters. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4198 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4199 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4200 | Results.EnterNewScope(); |
| 4201 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4202 | VisitedSelectorSet Selectors; |
| 4203 | AddObjCMethods(Class, true, MK_ZeroArgSelector, 0, 0, CurContext, Selectors, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4204 | /*AllowSameLength=*/true, Results); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4205 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4206 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4207 | CodeCompletionContext::CCC_Other, |
| 4208 | Results.data(),Results.size()); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
Douglas Gregor | bdb2d50 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 4211 | void Sema::CodeCompleteObjCPropertySetter(Scope *S, Decl *ObjCImplDecl) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4212 | typedef CodeCompletionResult Result; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4213 | |
| 4214 | // Try to find the interface where setters might live. |
| 4215 | ObjCInterfaceDecl *Class |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4216 | = dyn_cast_or_null<ObjCInterfaceDecl>(ObjCImplDecl); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4217 | if (!Class) { |
| 4218 | if (ObjCCategoryDecl *Category |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4219 | = dyn_cast_or_null<ObjCCategoryDecl>(ObjCImplDecl)) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4220 | Class = Category->getClassInterface(); |
| 4221 | |
| 4222 | if (!Class) |
| 4223 | return; |
| 4224 | } |
| 4225 | |
| 4226 | // Find all of the potential getters. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4227 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4228 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4229 | Results.EnterNewScope(); |
| 4230 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4231 | VisitedSelectorSet Selectors; |
| 4232 | AddObjCMethods(Class, true, MK_OneArgSelector, 0, 0, CurContext, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4233 | Selectors, /*AllowSameLength=*/true, Results); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4234 | |
| 4235 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4236 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4237 | CodeCompletionContext::CCC_Other, |
| 4238 | Results.data(),Results.size()); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 4241 | void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4242 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4243 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4244 | CodeCompletionContext::CCC_Type); |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 4245 | Results.EnterNewScope(); |
| 4246 | |
| 4247 | // Add context-sensitive, Objective-C parameter-passing keywords. |
| 4248 | bool AddedInOut = false; |
| 4249 | if ((DS.getObjCDeclQualifier() & |
| 4250 | (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 4251 | Results.AddResult("in"); |
| 4252 | Results.AddResult("inout"); |
| 4253 | AddedInOut = true; |
| 4254 | } |
| 4255 | if ((DS.getObjCDeclQualifier() & |
| 4256 | (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 4257 | Results.AddResult("out"); |
| 4258 | if (!AddedInOut) |
| 4259 | Results.AddResult("inout"); |
| 4260 | } |
| 4261 | if ((DS.getObjCDeclQualifier() & |
| 4262 | (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref | |
| 4263 | ObjCDeclSpec::DQ_Oneway)) == 0) { |
| 4264 | Results.AddResult("bycopy"); |
| 4265 | Results.AddResult("byref"); |
| 4266 | Results.AddResult("oneway"); |
| 4267 | } |
| 4268 | |
| 4269 | // Add various builtin type names and specifiers. |
| 4270 | AddOrdinaryNameResults(PCC_Type, S, *this, Results); |
| 4271 | Results.ExitScope(); |
| 4272 | |
| 4273 | // Add the various type names |
| 4274 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 4275 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4276 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4277 | CodeCompleter->includeGlobals()); |
| 4278 | |
| 4279 | if (CodeCompleter->includeMacros()) |
| 4280 | AddMacroResults(PP, Results); |
| 4281 | |
| 4282 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4283 | CodeCompletionContext::CCC_Type, |
| 4284 | Results.data(), Results.size()); |
| 4285 | } |
| 4286 | |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4287 | /// \brief When we have an expression with type "id", we may assume |
| 4288 | /// that it has some more-specific class type based on knowledge of |
| 4289 | /// common uses of Objective-C. This routine returns that class type, |
| 4290 | /// or NULL if no better result could be determined. |
| 4291 | static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) { |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 4292 | ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E); |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4293 | if (!Msg) |
| 4294 | return 0; |
| 4295 | |
| 4296 | Selector Sel = Msg->getSelector(); |
| 4297 | if (Sel.isNull()) |
| 4298 | return 0; |
| 4299 | |
| 4300 | IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0); |
| 4301 | if (!Id) |
| 4302 | return 0; |
| 4303 | |
| 4304 | ObjCMethodDecl *Method = Msg->getMethodDecl(); |
| 4305 | if (!Method) |
| 4306 | return 0; |
| 4307 | |
| 4308 | // Determine the class that we're sending the message to. |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 4309 | ObjCInterfaceDecl *IFace = 0; |
| 4310 | switch (Msg->getReceiverKind()) { |
| 4311 | case ObjCMessageExpr::Class: |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4312 | if (const ObjCObjectType *ObjType |
| 4313 | = Msg->getClassReceiver()->getAs<ObjCObjectType>()) |
| 4314 | IFace = ObjType->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 4315 | break; |
| 4316 | |
| 4317 | case ObjCMessageExpr::Instance: { |
| 4318 | QualType T = Msg->getInstanceReceiver()->getType(); |
| 4319 | if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) |
| 4320 | IFace = Ptr->getInterfaceDecl(); |
| 4321 | break; |
| 4322 | } |
| 4323 | |
| 4324 | case ObjCMessageExpr::SuperInstance: |
| 4325 | case ObjCMessageExpr::SuperClass: |
| 4326 | break; |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
| 4329 | if (!IFace) |
| 4330 | return 0; |
| 4331 | |
| 4332 | ObjCInterfaceDecl *Super = IFace->getSuperClass(); |
| 4333 | if (Method->isInstanceMethod()) |
| 4334 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 4335 | .Case("retain", IFace) |
| 4336 | .Case("autorelease", IFace) |
| 4337 | .Case("copy", IFace) |
| 4338 | .Case("copyWithZone", IFace) |
| 4339 | .Case("mutableCopy", IFace) |
| 4340 | .Case("mutableCopyWithZone", IFace) |
| 4341 | .Case("awakeFromCoder", IFace) |
| 4342 | .Case("replacementObjectFromCoder", IFace) |
| 4343 | .Case("class", IFace) |
| 4344 | .Case("classForCoder", IFace) |
| 4345 | .Case("superclass", Super) |
| 4346 | .Default(0); |
| 4347 | |
| 4348 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 4349 | .Case("new", IFace) |
| 4350 | .Case("alloc", IFace) |
| 4351 | .Case("allocWithZone", IFace) |
| 4352 | .Case("class", IFace) |
| 4353 | .Case("superclass", Super) |
| 4354 | .Default(0); |
| 4355 | } |
| 4356 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4357 | // Add a special completion for a message send to "super", which fills in the |
| 4358 | // most likely case of forwarding all of our arguments to the superclass |
| 4359 | // function. |
| 4360 | /// |
| 4361 | /// \param S The semantic analysis object. |
| 4362 | /// |
| 4363 | /// \param S NeedSuperKeyword Whether we need to prefix this completion with |
| 4364 | /// the "super" keyword. Otherwise, we just need to provide the arguments. |
| 4365 | /// |
| 4366 | /// \param SelIdents The identifiers in the selector that have already been |
| 4367 | /// provided as arguments for a send to "super". |
| 4368 | /// |
| 4369 | /// \param NumSelIdents The number of identifiers in \p SelIdents. |
| 4370 | /// |
| 4371 | /// \param Results The set of results to augment. |
| 4372 | /// |
| 4373 | /// \returns the Objective-C method declaration that would be invoked by |
| 4374 | /// this "super" completion. If NULL, no completion was added. |
| 4375 | static ObjCMethodDecl *AddSuperSendCompletion(Sema &S, bool NeedSuperKeyword, |
| 4376 | IdentifierInfo **SelIdents, |
| 4377 | unsigned NumSelIdents, |
| 4378 | ResultBuilder &Results) { |
| 4379 | ObjCMethodDecl *CurMethod = S.getCurMethodDecl(); |
| 4380 | if (!CurMethod) |
| 4381 | return 0; |
| 4382 | |
| 4383 | ObjCInterfaceDecl *Class = CurMethod->getClassInterface(); |
| 4384 | if (!Class) |
| 4385 | return 0; |
| 4386 | |
| 4387 | // Try to find a superclass method with the same selector. |
| 4388 | ObjCMethodDecl *SuperMethod = 0; |
| 4389 | while ((Class = Class->getSuperClass()) && !SuperMethod) |
| 4390 | SuperMethod = Class->getMethod(CurMethod->getSelector(), |
| 4391 | CurMethod->isInstanceMethod()); |
| 4392 | |
| 4393 | if (!SuperMethod) |
| 4394 | return 0; |
| 4395 | |
| 4396 | // Check whether the superclass method has the same signature. |
| 4397 | if (CurMethod->param_size() != SuperMethod->param_size() || |
| 4398 | CurMethod->isVariadic() != SuperMethod->isVariadic()) |
| 4399 | return 0; |
| 4400 | |
| 4401 | for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(), |
| 4402 | CurPEnd = CurMethod->param_end(), |
| 4403 | SuperP = SuperMethod->param_begin(); |
| 4404 | CurP != CurPEnd; ++CurP, ++SuperP) { |
| 4405 | // Make sure the parameter types are compatible. |
| 4406 | if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(), |
| 4407 | (*SuperP)->getType())) |
| 4408 | return 0; |
| 4409 | |
| 4410 | // Make sure we have a parameter name to forward! |
| 4411 | if (!(*CurP)->getIdentifier()) |
| 4412 | return 0; |
| 4413 | } |
| 4414 | |
| 4415 | // We have a superclass method. Now, form the send-to-super completion. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4416 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4417 | |
| 4418 | // Give this completion a return type. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4419 | AddResultTypeChunk(S.Context, SuperMethod, Builder); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4420 | |
| 4421 | // If we need the "super" keyword, add it (plus some spacing). |
| 4422 | if (NeedSuperKeyword) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4423 | Builder.AddTypedTextChunk("super"); |
| 4424 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | Selector Sel = CurMethod->getSelector(); |
| 4428 | if (Sel.isUnarySelector()) { |
| 4429 | if (NeedSuperKeyword) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4430 | Builder.AddTextChunk(CopyString(Builder.getAllocator(), |
| 4431 | Sel.getIdentifierInfoForSlot(0)->getName())); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4432 | else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4433 | Builder.AddTypedTextChunk(CopyString(Builder.getAllocator(), |
| 4434 | Sel.getIdentifierInfoForSlot(0)->getName())); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4435 | } else { |
| 4436 | ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(); |
| 4437 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) { |
| 4438 | if (I > NumSelIdents) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4439 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4440 | |
| 4441 | if (I < NumSelIdents) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4442 | Builder.AddInformativeChunk( |
| 4443 | CopyString(Builder.getAllocator(), |
| 4444 | Sel.getIdentifierInfoForSlot(I)->getName().str() + ":")); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4445 | else if (NeedSuperKeyword || I > NumSelIdents) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4446 | Builder.AddTextChunk( |
| 4447 | CopyString(Builder.getAllocator(), |
| 4448 | Sel.getIdentifierInfoForSlot(I)->getName().str() + ":")); |
| 4449 | Builder.AddPlaceholderChunk(CopyString(Builder.getAllocator(), |
| 4450 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4451 | } else { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4452 | Builder.AddTypedTextChunk( |
| 4453 | CopyString(Builder.getAllocator(), |
| 4454 | Sel.getIdentifierInfoForSlot(I)->getName().str() + ":")); |
| 4455 | Builder.AddPlaceholderChunk(CopyString(Builder.getAllocator(), |
| 4456 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4457 | } |
| 4458 | } |
| 4459 | } |
| 4460 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4461 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), CCP_SuperCompletion, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4462 | SuperMethod->isInstanceMethod() |
| 4463 | ? CXCursor_ObjCInstanceMethodDecl |
| 4464 | : CXCursor_ObjCClassMethodDecl)); |
| 4465 | return SuperMethod; |
| 4466 | } |
| 4467 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4468 | void Sema::CodeCompleteObjCMessageReceiver(Scope *S) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4469 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4470 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4471 | CodeCompletionContext::CCC_ObjCMessageReceiver, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4472 | &ResultBuilder::IsObjCMessageReceiver); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4473 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4474 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4475 | Results.EnterNewScope(); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4476 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4477 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4478 | |
| 4479 | // If we are in an Objective-C method inside a class that has a superclass, |
| 4480 | // add "super" as an option. |
| 4481 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 4482 | if (ObjCInterfaceDecl *Iface = Method->getClassInterface()) |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4483 | if (Iface->getSuperClass()) { |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4484 | Results.AddResult(Result("super")); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4485 | |
| 4486 | AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, 0, 0, Results); |
| 4487 | } |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4488 | |
| 4489 | Results.ExitScope(); |
| 4490 | |
| 4491 | if (CodeCompleter->includeMacros()) |
| 4492 | AddMacroResults(PP, Results); |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 4493 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4494 | Results.data(), Results.size()); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4495 | |
| 4496 | } |
| 4497 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4498 | void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, |
| 4499 | IdentifierInfo **SelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4500 | unsigned NumSelIdents, |
| 4501 | bool AtArgumentExpression) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4502 | ObjCInterfaceDecl *CDecl = 0; |
| 4503 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 4504 | // Figure out which interface we're in. |
| 4505 | CDecl = CurMethod->getClassInterface(); |
| 4506 | if (!CDecl) |
| 4507 | return; |
| 4508 | |
| 4509 | // Find the superclass of this class. |
| 4510 | CDecl = CDecl->getSuperClass(); |
| 4511 | if (!CDecl) |
| 4512 | return; |
| 4513 | |
| 4514 | if (CurMethod->isInstanceMethod()) { |
| 4515 | // We are inside an instance method, which means that the message |
| 4516 | // send [super ...] is actually calling an instance method on the |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 4517 | // current object. |
| 4518 | return CodeCompleteObjCInstanceMessage(S, 0, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4519 | SelIdents, NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4520 | AtArgumentExpression, |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 4521 | CDecl); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4522 | } |
| 4523 | |
| 4524 | // Fall through to send to the superclass in CDecl. |
| 4525 | } else { |
| 4526 | // "super" may be the name of a type or variable. Figure out which |
| 4527 | // it is. |
| 4528 | IdentifierInfo *Super = &Context.Idents.get("super"); |
| 4529 | NamedDecl *ND = LookupSingleName(S, Super, SuperLoc, |
| 4530 | LookupOrdinaryName); |
| 4531 | if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) { |
| 4532 | // "super" names an interface. Use it. |
| 4533 | } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4534 | if (const ObjCObjectType *Iface |
| 4535 | = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>()) |
| 4536 | CDecl = Iface->getInterface(); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4537 | } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) { |
| 4538 | // "super" names an unresolved type; we can't be more specific. |
| 4539 | } else { |
| 4540 | // Assume that "super" names some kind of value and parse that way. |
| 4541 | CXXScopeSpec SS; |
| 4542 | UnqualifiedId id; |
| 4543 | id.setIdentifier(Super, SuperLoc); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4544 | ExprResult SuperExpr = ActOnIdExpression(S, SS, id, false, false); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4545 | return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(), |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4546 | SelIdents, NumSelIdents, |
| 4547 | AtArgumentExpression); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
| 4550 | // Fall through |
| 4551 | } |
| 4552 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4553 | ParsedType Receiver; |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4554 | if (CDecl) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4555 | Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4556 | return CodeCompleteObjCClassMessage(S, Receiver, SelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4557 | NumSelIdents, AtArgumentExpression, |
| 4558 | /*IsSuper=*/true); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4559 | } |
| 4560 | |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 4561 | /// \brief Given a set of code-completion results for the argument of a message |
| 4562 | /// send, determine the preferred type (if any) for that argument expression. |
| 4563 | static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results, |
| 4564 | unsigned NumSelIdents) { |
| 4565 | typedef CodeCompletionResult Result; |
| 4566 | ASTContext &Context = Results.getSema().Context; |
| 4567 | |
| 4568 | QualType PreferredType; |
| 4569 | unsigned BestPriority = CCP_Unlikely * 2; |
| 4570 | Result *ResultsData = Results.data(); |
| 4571 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 4572 | Result &R = ResultsData[I]; |
| 4573 | if (R.Kind == Result::RK_Declaration && |
| 4574 | isa<ObjCMethodDecl>(R.Declaration)) { |
| 4575 | if (R.Priority <= BestPriority) { |
| 4576 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration); |
| 4577 | if (NumSelIdents <= Method->param_size()) { |
| 4578 | QualType MyPreferredType = Method->param_begin()[NumSelIdents - 1] |
| 4579 | ->getType(); |
| 4580 | if (R.Priority < BestPriority || PreferredType.isNull()) { |
| 4581 | BestPriority = R.Priority; |
| 4582 | PreferredType = MyPreferredType; |
| 4583 | } else if (!Context.hasSameUnqualifiedType(PreferredType, |
| 4584 | MyPreferredType)) { |
| 4585 | PreferredType = QualType(); |
| 4586 | } |
| 4587 | } |
| 4588 | } |
| 4589 | } |
| 4590 | } |
| 4591 | |
| 4592 | return PreferredType; |
| 4593 | } |
| 4594 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4595 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| 4596 | ParsedType Receiver, |
| 4597 | IdentifierInfo **SelIdents, |
| 4598 | unsigned NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4599 | bool AtArgumentExpression, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4600 | bool IsSuper, |
| 4601 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4602 | typedef CodeCompletionResult Result; |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4603 | ObjCInterfaceDecl *CDecl = 0; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4604 | |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4605 | // If the given name refers to an interface type, retrieve the |
| 4606 | // corresponding declaration. |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4607 | if (Receiver) { |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4608 | QualType T = SemaRef.GetTypeFromParser(Receiver, 0); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4609 | if (!T.isNull()) |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4610 | if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>()) |
| 4611 | CDecl = Interface->getInterface(); |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4612 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4613 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4614 | // Add all of the factory methods in this Objective-C class, its protocols, |
| 4615 | // superclasses, categories, implementation, etc. |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4616 | Results.EnterNewScope(); |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4617 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4618 | // If this is a send-to-super, try to add the special "super" send |
| 4619 | // completion. |
| 4620 | if (IsSuper) { |
| 4621 | if (ObjCMethodDecl *SuperMethod |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4622 | = AddSuperSendCompletion(SemaRef, false, SelIdents, NumSelIdents, |
| 4623 | Results)) |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4624 | Results.Ignore(SuperMethod); |
| 4625 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4626 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 4627 | // If we're inside an Objective-C method definition, prefer its selector to |
| 4628 | // others. |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4629 | if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl()) |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 4630 | Results.setPreferredSelector(CurMethod->getSelector()); |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4631 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4632 | VisitedSelectorSet Selectors; |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4633 | if (CDecl) |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4634 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, NumSelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4635 | SemaRef.CurContext, Selectors, AtArgumentExpression, |
| 4636 | Results); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4637 | else { |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4638 | // We're messaging "id" as a type; provide all class/factory methods. |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4639 | |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4640 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 4641 | // pool from the AST file. |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4642 | if (SemaRef.ExternalSource) { |
| 4643 | for (uint32_t I = 0, |
| 4644 | N = SemaRef.ExternalSource->GetNumExternalSelectors(); |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 4645 | I != N; ++I) { |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4646 | Selector Sel = SemaRef.ExternalSource->GetExternalSelector(I); |
| 4647 | if (Sel.isNull() || SemaRef.MethodPool.count(Sel)) |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4648 | continue; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4649 | |
| 4650 | SemaRef.ReadMethodPool(Sel); |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4651 | } |
| 4652 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4653 | |
| 4654 | for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(), |
| 4655 | MEnd = SemaRef.MethodPool.end(); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4656 | M != MEnd; ++M) { |
| 4657 | for (ObjCMethodList *MethList = &M->second.second; |
| 4658 | MethList && MethList->Method; |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4659 | MethList = MethList->Next) { |
| 4660 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 4661 | NumSelIdents)) |
| 4662 | continue; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4663 | |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4664 | Result R(MethList->Method, 0); |
| 4665 | R.StartParameter = NumSelIdents; |
| 4666 | R.AllParametersAreInformative = false; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4667 | Results.MaybeAddResult(R, SemaRef.CurContext); |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4668 | } |
| 4669 | } |
| 4670 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4671 | |
| 4672 | Results.ExitScope(); |
| 4673 | } |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4674 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4675 | void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, |
| 4676 | IdentifierInfo **SelIdents, |
| 4677 | unsigned NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4678 | bool AtArgumentExpression, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4679 | bool IsSuper) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4680 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4681 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4682 | AddClassMessageCompletions(*this, S, Receiver, SelIdents, NumSelIdents, |
| 4683 | AtArgumentExpression, IsSuper, Results); |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 4684 | |
| 4685 | // If we're actually at the argument expression (rather than prior to the |
| 4686 | // selector), we're actually performing code completion for an expression. |
| 4687 | // Determine whether we have a single, best method. If so, we can |
| 4688 | // code-complete the expression using the corresponding parameter type as |
| 4689 | // our preferred type, improving completion results. |
| 4690 | if (AtArgumentExpression) { |
| 4691 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
| 4692 | NumSelIdents); |
| 4693 | if (PreferredType.isNull()) |
| 4694 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 4695 | else |
| 4696 | CodeCompleteExpression(S, PreferredType); |
| 4697 | return; |
| 4698 | } |
| 4699 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4700 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4701 | CodeCompletionContext::CCC_Other, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4702 | Results.data(), Results.size()); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4705 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver, |
| 4706 | IdentifierInfo **SelIdents, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4707 | unsigned NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4708 | bool AtArgumentExpression, |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 4709 | ObjCInterfaceDecl *Super) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4710 | typedef CodeCompletionResult Result; |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4711 | |
| 4712 | Expr *RecExpr = static_cast<Expr *>(Receiver); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4713 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4714 | // If necessary, apply function/array conversion to the receiver. |
| 4715 | // C99 6.7.5.3p[7,8]. |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 4716 | if (RecExpr) |
| 4717 | DefaultFunctionArrayLvalueConversion(RecExpr); |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 4718 | QualType ReceiverType = RecExpr? RecExpr->getType() |
| 4719 | : Super? Context.getObjCObjectPointerType( |
| 4720 | Context.getObjCInterfaceType(Super)) |
| 4721 | : Context.getObjCIdType(); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4722 | |
Douglas Gregor | da89264 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 4723 | // If we're messaging an expression with type "id" or "Class", check |
| 4724 | // whether we know something special about the receiver that allows |
| 4725 | // us to assume a more-specific receiver type. |
| 4726 | if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) |
| 4727 | if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) { |
| 4728 | if (ReceiverType->isObjCClassType()) |
| 4729 | return CodeCompleteObjCClassMessage(S, |
| 4730 | ParsedType::make(Context.getObjCInterfaceType(IFace)), |
| 4731 | SelIdents, NumSelIdents, |
| 4732 | AtArgumentExpression, Super); |
| 4733 | |
| 4734 | ReceiverType = Context.getObjCObjectPointerType( |
| 4735 | Context.getObjCInterfaceType(IFace)); |
| 4736 | } |
| 4737 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4738 | // Build the set of methods we can see. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4739 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4740 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4741 | Results.EnterNewScope(); |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4742 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4743 | // If this is a send-to-super, try to add the special "super" send |
| 4744 | // completion. |
Douglas Gregor | 6b0656a | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 4745 | if (Super) { |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4746 | if (ObjCMethodDecl *SuperMethod |
| 4747 | = AddSuperSendCompletion(*this, false, SelIdents, NumSelIdents, |
| 4748 | Results)) |
| 4749 | Results.Ignore(SuperMethod); |
| 4750 | } |
| 4751 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 4752 | // If we're inside an Objective-C method definition, prefer its selector to |
| 4753 | // others. |
| 4754 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 4755 | Results.setPreferredSelector(CurMethod->getSelector()); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4756 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4757 | // Keep track of the selectors we've already added. |
| 4758 | VisitedSelectorSet Selectors; |
| 4759 | |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4760 | // Handle messages to Class. This really isn't a message to an instance |
| 4761 | // method, so we treat it the same way we would treat a message send to a |
| 4762 | // class method. |
| 4763 | if (ReceiverType->isObjCClassType() || |
| 4764 | ReceiverType->isObjCQualifiedClassType()) { |
| 4765 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 4766 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4767 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, NumSelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4768 | CurContext, Selectors, AtArgumentExpression, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4769 | } |
| 4770 | } |
| 4771 | // Handle messages to a qualified ID ("id<foo>"). |
| 4772 | else if (const ObjCObjectPointerType *QualID |
| 4773 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 4774 | // Search protocols for instance methods. |
| 4775 | for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(), |
| 4776 | E = QualID->qual_end(); |
| 4777 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4778 | AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4779 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4780 | } |
| 4781 | // Handle messages to a pointer to interface type. |
| 4782 | else if (const ObjCObjectPointerType *IFacePtr |
| 4783 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 4784 | // Search the class, its superclasses, etc., for instance methods. |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4785 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4786 | NumSelIdents, CurContext, Selectors, AtArgumentExpression, |
| 4787 | Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4788 | |
| 4789 | // Search protocols for instance methods. |
| 4790 | for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(), |
| 4791 | E = IFacePtr->qual_end(); |
| 4792 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4793 | AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext, |
Douglas Gregor | cf54426 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4794 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4795 | } |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4796 | // Handle messages to "id". |
| 4797 | else if (ReceiverType->isObjCIdType()) { |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4798 | // We're messaging "id", so provide all instance methods we know |
| 4799 | // about as code-completion results. |
| 4800 | |
| 4801 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 4802 | // pool from the AST file. |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4803 | if (ExternalSource) { |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 4804 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 4805 | I != N; ++I) { |
| 4806 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4807 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4808 | continue; |
| 4809 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4810 | ReadMethodPool(Sel); |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4811 | } |
| 4812 | } |
| 4813 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4814 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 4815 | MEnd = MethodPool.end(); |
| 4816 | M != MEnd; ++M) { |
| 4817 | for (ObjCMethodList *MethList = &M->second.first; |
| 4818 | MethList && MethList->Method; |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4819 | MethList = MethList->Next) { |
| 4820 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 4821 | NumSelIdents)) |
| 4822 | continue; |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4823 | |
| 4824 | if (!Selectors.insert(MethList->Method->getSelector())) |
| 4825 | continue; |
| 4826 | |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4827 | Result R(MethList->Method, 0); |
| 4828 | R.StartParameter = NumSelIdents; |
| 4829 | R.AllParametersAreInformative = false; |
| 4830 | Results.MaybeAddResult(R, CurContext); |
| 4831 | } |
| 4832 | } |
| 4833 | } |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4834 | Results.ExitScope(); |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 4835 | |
| 4836 | |
| 4837 | // If we're actually at the argument expression (rather than prior to the |
| 4838 | // selector), we're actually performing code completion for an expression. |
| 4839 | // Determine whether we have a single, best method. If so, we can |
| 4840 | // code-complete the expression using the corresponding parameter type as |
| 4841 | // our preferred type, improving completion results. |
| 4842 | if (AtArgumentExpression) { |
| 4843 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
| 4844 | NumSelIdents); |
| 4845 | if (PreferredType.isNull()) |
| 4846 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 4847 | else |
| 4848 | CodeCompleteExpression(S, PreferredType); |
| 4849 | return; |
| 4850 | } |
| 4851 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4852 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4853 | CodeCompletionContext::CCC_Other, |
| 4854 | Results.data(),Results.size()); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4855 | } |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4856 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4857 | void Sema::CodeCompleteObjCForCollection(Scope *S, |
| 4858 | DeclGroupPtrTy IterationVar) { |
| 4859 | CodeCompleteExpressionData Data; |
| 4860 | Data.ObjCCollection = true; |
| 4861 | |
| 4862 | if (IterationVar.getAsOpaquePtr()) { |
| 4863 | DeclGroupRef DG = IterationVar.getAsVal<DeclGroupRef>(); |
| 4864 | for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) { |
| 4865 | if (*I) |
| 4866 | Data.IgnoreDecls.push_back(*I); |
| 4867 | } |
| 4868 | } |
| 4869 | |
| 4870 | CodeCompleteExpression(S, Data); |
| 4871 | } |
| 4872 | |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4873 | void Sema::CodeCompleteObjCSelector(Scope *S, IdentifierInfo **SelIdents, |
| 4874 | unsigned NumSelIdents) { |
| 4875 | // If we have an external source, load the entire class method |
| 4876 | // pool from the AST file. |
| 4877 | if (ExternalSource) { |
| 4878 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 4879 | I != N; ++I) { |
| 4880 | Selector Sel = ExternalSource->GetExternalSelector(I); |
| 4881 | if (Sel.isNull() || MethodPool.count(Sel)) |
| 4882 | continue; |
| 4883 | |
| 4884 | ReadMethodPool(Sel); |
| 4885 | } |
| 4886 | } |
| 4887 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4888 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4889 | CodeCompletionContext::CCC_SelectorName); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4890 | Results.EnterNewScope(); |
| 4891 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 4892 | MEnd = MethodPool.end(); |
| 4893 | M != MEnd; ++M) { |
| 4894 | |
| 4895 | Selector Sel = M->first; |
| 4896 | if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents, NumSelIdents)) |
| 4897 | continue; |
| 4898 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4899 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4900 | if (Sel.isUnarySelector()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4901 | Builder.AddTypedTextChunk(CopyString(Builder.getAllocator(), |
| 4902 | Sel.getIdentifierInfoForSlot(0)->getName())); |
| 4903 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4904 | continue; |
| 4905 | } |
| 4906 | |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 4907 | std::string Accumulator; |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4908 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) { |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 4909 | if (I == NumSelIdents) { |
| 4910 | if (!Accumulator.empty()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4911 | Builder.AddInformativeChunk(CopyString(Builder.getAllocator(), |
| 4912 | Accumulator)); |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 4913 | Accumulator.clear(); |
| 4914 | } |
| 4915 | } |
| 4916 | |
| 4917 | Accumulator += Sel.getIdentifierInfoForSlot(I)->getName().str(); |
| 4918 | Accumulator += ':'; |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4919 | } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4920 | Builder.AddTypedTextChunk(CopyString(Builder.getAllocator(), Accumulator)); |
| 4921 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4922 | } |
| 4923 | Results.ExitScope(); |
| 4924 | |
| 4925 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4926 | CodeCompletionContext::CCC_SelectorName, |
| 4927 | Results.data(), Results.size()); |
| 4928 | } |
| 4929 | |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4930 | /// \brief Add all of the protocol declarations that we find in the given |
| 4931 | /// (translation unit) context. |
| 4932 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4933 | bool OnlyForwardDeclarations, |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4934 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4935 | typedef CodeCompletionResult Result; |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4936 | |
| 4937 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 4938 | DEnd = Ctx->decls_end(); |
| 4939 | D != DEnd; ++D) { |
| 4940 | // Record any protocols we find. |
| 4941 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*D)) |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4942 | if (!OnlyForwardDeclarations || Proto->isForwardDecl()) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 4943 | Results.AddResult(Result(Proto, 0), CurContext, 0, false); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4944 | |
| 4945 | // Record any forward-declared protocols we find. |
| 4946 | if (ObjCForwardProtocolDecl *Forward |
| 4947 | = dyn_cast<ObjCForwardProtocolDecl>(*D)) { |
| 4948 | for (ObjCForwardProtocolDecl::protocol_iterator |
| 4949 | P = Forward->protocol_begin(), |
| 4950 | PEnd = Forward->protocol_end(); |
| 4951 | P != PEnd; ++P) |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4952 | if (!OnlyForwardDeclarations || (*P)->isForwardDecl()) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 4953 | Results.AddResult(Result(*P, 0), CurContext, 0, false); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4954 | } |
| 4955 | } |
| 4956 | } |
| 4957 | |
| 4958 | void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols, |
| 4959 | unsigned NumProtocols) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4960 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4961 | CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4962 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 4963 | if (CodeCompleter && CodeCompleter->includeGlobals()) { |
| 4964 | Results.EnterNewScope(); |
| 4965 | |
| 4966 | // Tell the result set to ignore all of the protocols we have |
| 4967 | // already seen. |
| 4968 | // FIXME: This doesn't work when caching code-completion results. |
| 4969 | for (unsigned I = 0; I != NumProtocols; ++I) |
| 4970 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first, |
| 4971 | Protocols[I].second)) |
| 4972 | Results.Ignore(Protocol); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4973 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 4974 | // Add all protocols. |
| 4975 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 4976 | Results); |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4977 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 4978 | Results.ExitScope(); |
| 4979 | } |
| 4980 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4981 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4982 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 4983 | Results.data(),Results.size()); |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4984 | } |
| 4985 | |
| 4986 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 4987 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4988 | CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4989 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 4990 | if (CodeCompleter && CodeCompleter->includeGlobals()) { |
| 4991 | Results.EnterNewScope(); |
| 4992 | |
| 4993 | // Add all protocols. |
| 4994 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 4995 | Results); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4996 | |
Douglas Gregor | 70c2335 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 4997 | Results.ExitScope(); |
| 4998 | } |
| 4999 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5000 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5001 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 5002 | Results.data(),Results.size()); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5003 | } |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5004 | |
| 5005 | /// \brief Add all of the Objective-C interface declarations that we find in |
| 5006 | /// the given (translation unit) context. |
| 5007 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, |
| 5008 | bool OnlyForwardDeclarations, |
| 5009 | bool OnlyUnimplemented, |
| 5010 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5011 | typedef CodeCompletionResult Result; |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5012 | |
| 5013 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 5014 | DEnd = Ctx->decls_end(); |
| 5015 | D != DEnd; ++D) { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 5016 | // Record any interfaces we find. |
| 5017 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*D)) |
| 5018 | if ((!OnlyForwardDeclarations || Class->isForwardDecl()) && |
| 5019 | (!OnlyUnimplemented || !Class->getImplementation())) |
| 5020 | Results.AddResult(Result(Class, 0), CurContext, 0, false); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5021 | |
| 5022 | // Record any forward-declared interfaces we find. |
| 5023 | if (ObjCClassDecl *Forward = dyn_cast<ObjCClassDecl>(*D)) { |
| 5024 | for (ObjCClassDecl::iterator C = Forward->begin(), CEnd = Forward->end(); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 5025 | C != CEnd; ++C) |
| 5026 | if ((!OnlyForwardDeclarations || C->getInterface()->isForwardDecl()) && |
| 5027 | (!OnlyUnimplemented || !C->getInterface()->getImplementation())) |
| 5028 | Results.AddResult(Result(C->getInterface(), 0), CurContext, |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5029 | 0, false); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5030 | } |
| 5031 | } |
| 5032 | } |
| 5033 | |
| 5034 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5035 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5036 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5037 | Results.EnterNewScope(); |
| 5038 | |
| 5039 | // Add all classes. |
| 5040 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 5041 | false, Results); |
| 5042 | |
| 5043 | Results.ExitScope(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5044 | // FIXME: Add a special context for this, use cached global completion |
| 5045 | // results. |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5046 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5047 | CodeCompletionContext::CCC_Other, |
| 5048 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5049 | } |
| 5050 | |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5051 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName, |
| 5052 | SourceLocation ClassNameLoc) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5053 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5054 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5055 | Results.EnterNewScope(); |
| 5056 | |
| 5057 | // Make sure that we ignore the class we're currently defining. |
| 5058 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5059 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5060 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5061 | Results.Ignore(CurClass); |
| 5062 | |
| 5063 | // Add all classes. |
| 5064 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 5065 | false, Results); |
| 5066 | |
| 5067 | Results.ExitScope(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5068 | // FIXME: Add a special context for this, use cached global completion |
| 5069 | // results. |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5070 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5071 | CodeCompletionContext::CCC_Other, |
| 5072 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5073 | } |
| 5074 | |
| 5075 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5076 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5077 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5078 | Results.EnterNewScope(); |
| 5079 | |
| 5080 | // Add all unimplemented classes. |
| 5081 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 5082 | true, Results); |
| 5083 | |
| 5084 | Results.ExitScope(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5085 | // FIXME: Add a special context for this, use cached global completion |
| 5086 | // results. |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5087 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5088 | CodeCompletionContext::CCC_Other, |
| 5089 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5090 | } |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5091 | |
| 5092 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5093 | IdentifierInfo *ClassName, |
| 5094 | SourceLocation ClassNameLoc) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5095 | typedef CodeCompletionResult Result; |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5096 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5097 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5098 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5099 | |
| 5100 | // Ignore any categories we find that have already been implemented by this |
| 5101 | // interface. |
| 5102 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 5103 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5104 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5105 | if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)) |
| 5106 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; |
| 5107 | Category = Category->getNextClassCategory()) |
| 5108 | CategoryNames.insert(Category->getIdentifier()); |
| 5109 | |
| 5110 | // Add all of the categories we know about. |
| 5111 | Results.EnterNewScope(); |
| 5112 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
| 5113 | for (DeclContext::decl_iterator D = TU->decls_begin(), |
| 5114 | DEnd = TU->decls_end(); |
| 5115 | D != DEnd; ++D) |
| 5116 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(*D)) |
| 5117 | if (CategoryNames.insert(Category->getIdentifier())) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5118 | Results.AddResult(Result(Category, 0), CurContext, 0, false); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5119 | Results.ExitScope(); |
| 5120 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5121 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5122 | CodeCompletionContext::CCC_Other, |
| 5123 | Results.data(),Results.size()); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5124 | } |
| 5125 | |
| 5126 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5127 | IdentifierInfo *ClassName, |
| 5128 | SourceLocation ClassNameLoc) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5129 | typedef CodeCompletionResult Result; |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5130 | |
| 5131 | // Find the corresponding interface. If we couldn't find the interface, the |
| 5132 | // program itself is ill-formed. However, we'll try to be helpful still by |
| 5133 | // providing the list of all of the categories we know about. |
| 5134 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5135 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5136 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); |
| 5137 | if (!Class) |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5138 | return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5139 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5140 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5141 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5142 | |
| 5143 | // Add all of the categories that have have corresponding interface |
| 5144 | // declarations in this class and any of its superclasses, except for |
| 5145 | // already-implemented categories in the class itself. |
| 5146 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 5147 | Results.EnterNewScope(); |
| 5148 | bool IgnoreImplemented = true; |
| 5149 | while (Class) { |
| 5150 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; |
| 5151 | Category = Category->getNextClassCategory()) |
| 5152 | if ((!IgnoreImplemented || !Category->getImplementation()) && |
| 5153 | CategoryNames.insert(Category->getIdentifier())) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5154 | Results.AddResult(Result(Category, 0), CurContext, 0, false); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5155 | |
| 5156 | Class = Class->getSuperClass(); |
| 5157 | IgnoreImplemented = false; |
| 5158 | } |
| 5159 | Results.ExitScope(); |
| 5160 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5161 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5162 | CodeCompletionContext::CCC_Other, |
| 5163 | Results.data(),Results.size()); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5164 | } |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5165 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5166 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S, Decl *ObjCImpDecl) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5167 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5168 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5169 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5170 | |
| 5171 | // Figure out where this @synthesize lives. |
| 5172 | ObjCContainerDecl *Container |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5173 | = dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5174 | if (!Container || |
| 5175 | (!isa<ObjCImplementationDecl>(Container) && |
| 5176 | !isa<ObjCCategoryImplDecl>(Container))) |
| 5177 | return; |
| 5178 | |
| 5179 | // Ignore any properties that have already been implemented. |
| 5180 | for (DeclContext::decl_iterator D = Container->decls_begin(), |
| 5181 | DEnd = Container->decls_end(); |
| 5182 | D != DEnd; ++D) |
| 5183 | if (ObjCPropertyImplDecl *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(*D)) |
| 5184 | Results.Ignore(PropertyImpl->getPropertyDecl()); |
| 5185 | |
| 5186 | // Add any properties that we find. |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 5187 | AddedPropertiesSet AddedProperties; |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5188 | Results.EnterNewScope(); |
| 5189 | if (ObjCImplementationDecl *ClassImpl |
| 5190 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| 5191 | AddObjCProperties(ClassImpl->getClassInterface(), false, CurContext, |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 5192 | AddedProperties, Results); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5193 | else |
| 5194 | AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), |
Douglas Gregor | 7344921 | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 5195 | false, CurContext, AddedProperties, Results); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5196 | Results.ExitScope(); |
| 5197 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5198 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5199 | CodeCompletionContext::CCC_Other, |
| 5200 | Results.data(),Results.size()); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5201 | } |
| 5202 | |
| 5203 | void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S, |
| 5204 | IdentifierInfo *PropertyName, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5205 | Decl *ObjCImpDecl) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5206 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5207 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5208 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5209 | |
| 5210 | // Figure out where this @synthesize lives. |
| 5211 | ObjCContainerDecl *Container |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5212 | = dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5213 | if (!Container || |
| 5214 | (!isa<ObjCImplementationDecl>(Container) && |
| 5215 | !isa<ObjCCategoryImplDecl>(Container))) |
| 5216 | return; |
| 5217 | |
| 5218 | // Figure out which interface we're looking into. |
| 5219 | ObjCInterfaceDecl *Class = 0; |
| 5220 | if (ObjCImplementationDecl *ClassImpl |
| 5221 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| 5222 | Class = ClassImpl->getClassInterface(); |
| 5223 | else |
| 5224 | Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl() |
| 5225 | ->getClassInterface(); |
| 5226 | |
| 5227 | // Add all of the instance variables in this class and its superclasses. |
| 5228 | Results.EnterNewScope(); |
| 5229 | for(; Class; Class = Class->getSuperClass()) { |
| 5230 | // FIXME: We could screen the type of each ivar for compatibility with |
| 5231 | // the property, but is that being too paternal? |
| 5232 | for (ObjCInterfaceDecl::ivar_iterator IVar = Class->ivar_begin(), |
| 5233 | IVarEnd = Class->ivar_end(); |
| 5234 | IVar != IVarEnd; ++IVar) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5235 | Results.AddResult(Result(*IVar, 0), CurContext, 0, false); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5236 | } |
| 5237 | Results.ExitScope(); |
| 5238 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5239 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5240 | CodeCompletionContext::CCC_Other, |
| 5241 | Results.data(),Results.size()); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5242 | } |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5243 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5244 | // Mapping from selectors to the methods that implement that selector, along |
| 5245 | // with the "in original class" flag. |
| 5246 | typedef llvm::DenseMap<Selector, std::pair<ObjCMethodDecl *, bool> > |
| 5247 | KnownMethodsMap; |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5248 | |
| 5249 | /// \brief Find all of the methods that reside in the given container |
| 5250 | /// (and its superclasses, protocols, etc.) that meet the given |
| 5251 | /// criteria. Insert those methods into the map of known methods, |
| 5252 | /// indexed by selector so they can be easily found. |
| 5253 | static void FindImplementableMethods(ASTContext &Context, |
| 5254 | ObjCContainerDecl *Container, |
| 5255 | bool WantInstanceMethods, |
| 5256 | QualType ReturnType, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5257 | KnownMethodsMap &KnownMethods, |
| 5258 | bool InOriginalClass = true) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5259 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 5260 | // Recurse into protocols. |
| 5261 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5262 | = IFace->getReferencedProtocols(); |
| 5263 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5264 | E = Protocols.end(); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5265 | I != E; ++I) |
| 5266 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5267 | KnownMethods, InOriginalClass); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5268 | |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5269 | // Add methods from any class extensions and categories. |
| 5270 | for (const ObjCCategoryDecl *Cat = IFace->getCategoryList(); Cat; |
| 5271 | Cat = Cat->getNextClassCategory()) |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 5272 | FindImplementableMethods(Context, const_cast<ObjCCategoryDecl*>(Cat), |
| 5273 | WantInstanceMethods, ReturnType, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5274 | KnownMethods, false); |
| 5275 | |
| 5276 | // Visit the superclass. |
| 5277 | if (IFace->getSuperClass()) |
| 5278 | FindImplementableMethods(Context, IFace->getSuperClass(), |
| 5279 | WantInstanceMethods, ReturnType, |
| 5280 | KnownMethods, false); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5281 | } |
| 5282 | |
| 5283 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 5284 | // Recurse into protocols. |
| 5285 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5286 | = Category->getReferencedProtocols(); |
| 5287 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5288 | E = Protocols.end(); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5289 | I != E; ++I) |
| 5290 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5291 | KnownMethods, InOriginalClass); |
| 5292 | |
| 5293 | // If this category is the original class, jump to the interface. |
| 5294 | if (InOriginalClass && Category->getClassInterface()) |
| 5295 | FindImplementableMethods(Context, Category->getClassInterface(), |
| 5296 | WantInstanceMethods, ReturnType, KnownMethods, |
| 5297 | false); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5298 | } |
| 5299 | |
| 5300 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 5301 | // Recurse into protocols. |
| 5302 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5303 | = Protocol->getReferencedProtocols(); |
| 5304 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 5305 | E = Protocols.end(); |
| 5306 | I != E; ++I) |
| 5307 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5308 | KnownMethods, false); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5309 | } |
| 5310 | |
| 5311 | // Add methods in this container. This operation occurs last because |
| 5312 | // we want the methods from this container to override any methods |
| 5313 | // we've previously seen with the same selector. |
| 5314 | for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), |
| 5315 | MEnd = Container->meth_end(); |
| 5316 | M != MEnd; ++M) { |
| 5317 | if ((*M)->isInstanceMethod() == WantInstanceMethods) { |
| 5318 | if (!ReturnType.isNull() && |
| 5319 | !Context.hasSameUnqualifiedType(ReturnType, (*M)->getResultType())) |
| 5320 | continue; |
| 5321 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5322 | KnownMethods[(*M)->getSelector()] = std::make_pair(*M, InOriginalClass); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5323 | } |
| 5324 | } |
| 5325 | } |
| 5326 | |
| 5327 | void Sema::CodeCompleteObjCMethodDecl(Scope *S, |
| 5328 | bool IsInstanceMethod, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5329 | ParsedType ReturnTy, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5330 | Decl *IDecl) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5331 | // Determine the return type of the method we're declaring, if |
| 5332 | // provided. |
| 5333 | QualType ReturnType = GetTypeFromParser(ReturnTy); |
| 5334 | |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5335 | // Determine where we should start searching for methods. |
| 5336 | ObjCContainerDecl *SearchDecl = 0; |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5337 | bool IsInImplementation = false; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5338 | if (Decl *D = IDecl) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5339 | if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) { |
| 5340 | SearchDecl = Impl->getClassInterface(); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5341 | IsInImplementation = true; |
| 5342 | } else if (ObjCCategoryImplDecl *CatImpl |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5343 | = dyn_cast<ObjCCategoryImplDecl>(D)) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5344 | SearchDecl = CatImpl->getCategoryDecl(); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5345 | IsInImplementation = true; |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5346 | } else |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5347 | SearchDecl = dyn_cast<ObjCContainerDecl>(D); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5348 | } |
| 5349 | |
| 5350 | if (!SearchDecl && S) { |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5351 | if (DeclContext *DC = static_cast<DeclContext *>(S->getEntity())) |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5352 | SearchDecl = dyn_cast<ObjCContainerDecl>(DC); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5353 | } |
| 5354 | |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5355 | if (!SearchDecl) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5356 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5357 | CodeCompletionContext::CCC_Other, |
| 5358 | 0, 0); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5359 | return; |
| 5360 | } |
| 5361 | |
| 5362 | // Find all of the methods that we could declare/implement here. |
| 5363 | KnownMethodsMap KnownMethods; |
| 5364 | FindImplementableMethods(Context, SearchDecl, IsInstanceMethod, |
Douglas Gregor | ea76618 | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5365 | ReturnType, KnownMethods); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5366 | |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5367 | // Add declarations or definitions for each of the known methods. |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5368 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5369 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5370 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5371 | Results.EnterNewScope(); |
| 5372 | PrintingPolicy Policy(Context.PrintingPolicy); |
| 5373 | Policy.AnonymousTagLocations = false; |
| 5374 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
| 5375 | MEnd = KnownMethods.end(); |
| 5376 | M != MEnd; ++M) { |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5377 | ObjCMethodDecl *Method = M->second.first; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5378 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5379 | |
| 5380 | // If the result type was not already provided, add it to the |
| 5381 | // pattern as (type). |
| 5382 | if (ReturnType.isNull()) { |
| 5383 | std::string TypeStr; |
| 5384 | Method->getResultType().getAsStringInternal(TypeStr, Policy); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5385 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5386 | // FIXME: Fast-path common type names |
| 5387 | Builder.AddTextChunk(CopyString(Builder.getAllocator(), TypeStr)); |
| 5388 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5389 | } |
| 5390 | |
| 5391 | Selector Sel = Method->getSelector(); |
| 5392 | |
| 5393 | // Add the first part of the selector to the pattern. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5394 | Builder.AddTypedTextChunk(CopyString(Builder.getAllocator(), |
| 5395 | Sel.getIdentifierInfoForSlot(0)->getName())); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5396 | |
| 5397 | // Add parameters to the pattern. |
| 5398 | unsigned I = 0; |
| 5399 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 5400 | PEnd = Method->param_end(); |
| 5401 | P != PEnd; (void)++P, ++I) { |
| 5402 | // Add the part of the selector name. |
| 5403 | if (I == 0) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5404 | Builder.AddTypedTextChunk(":"); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5405 | else if (I < Sel.getNumArgs()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5406 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5407 | Builder.AddTypedTextChunk( |
| 5408 | CopyString(Builder.getAllocator(), |
| 5409 | (Sel.getIdentifierInfoForSlot(I)->getName() |
| 5410 | + ":").str())); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5411 | } else |
| 5412 | break; |
| 5413 | |
| 5414 | // Add the parameter type. |
| 5415 | std::string TypeStr; |
| 5416 | (*P)->getOriginalType().getAsStringInternal(TypeStr, Policy); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5417 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5418 | // FIXME: Fast-path common type names |
| 5419 | Builder.AddTextChunk(CopyString(Builder.getAllocator(), |
| 5420 | TypeStr)); |
| 5421 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5422 | |
| 5423 | if (IdentifierInfo *Id = (*P)->getIdentifier()) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5424 | Builder.AddTextChunk(CopyString(Builder.getAllocator(), Id->getName())); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5425 | } |
| 5426 | |
| 5427 | if (Method->isVariadic()) { |
| 5428 | if (Method->param_size() > 0) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5429 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 5430 | Builder.AddTextChunk("..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 5431 | } |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5432 | |
Douglas Gregor | 447107d | 2010-05-28 00:57:46 +0000 | [diff] [blame] | 5433 | if (IsInImplementation && Results.includeCodePatterns()) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5434 | // We will be defining the method here, so add a compound statement. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5435 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5436 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5437 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5438 | if (!Method->getResultType()->isVoidType()) { |
| 5439 | // If the result type is not void, add a return clause. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5440 | Builder.AddTextChunk("return"); |
| 5441 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5442 | Builder.AddPlaceholderChunk("expression"); |
| 5443 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5444 | } else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5445 | Builder.AddPlaceholderChunk("statements"); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5446 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5447 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 5448 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5449 | } |
| 5450 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5451 | unsigned Priority = CCP_CodePattern; |
| 5452 | if (!M->second.second) |
| 5453 | Priority += CCD_InBaseClass; |
| 5454 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5455 | Results.AddResult(Result(Builder.TakeString(), Priority, |
Douglas Gregor | 16ed9ad | 2010-08-17 16:06:07 +0000 | [diff] [blame] | 5456 | Method->isInstanceMethod() |
| 5457 | ? CXCursor_ObjCInstanceMethodDecl |
| 5458 | : CXCursor_ObjCClassMethodDecl)); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5459 | } |
| 5460 | |
| 5461 | Results.ExitScope(); |
| 5462 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5463 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5464 | CodeCompletionContext::CCC_Other, |
| 5465 | Results.data(),Results.size()); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5466 | } |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5467 | |
| 5468 | void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S, |
| 5469 | bool IsInstanceMethod, |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 5470 | bool AtParameterName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5471 | ParsedType ReturnTy, |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5472 | IdentifierInfo **SelIdents, |
| 5473 | unsigned NumSelIdents) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5474 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 5475 | // pool from the AST file. |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5476 | if (ExternalSource) { |
| 5477 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 5478 | I != N; ++I) { |
| 5479 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5480 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5481 | continue; |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5482 | |
| 5483 | ReadMethodPool(Sel); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5484 | } |
| 5485 | } |
| 5486 | |
| 5487 | // Build the set of methods we can see. |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5488 | typedef CodeCompletionResult Result; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5489 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 5490 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5491 | |
| 5492 | if (ReturnTy) |
| 5493 | Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType()); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5494 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5495 | Results.EnterNewScope(); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5496 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 5497 | MEnd = MethodPool.end(); |
| 5498 | M != MEnd; ++M) { |
| 5499 | for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first : |
| 5500 | &M->second.second; |
| 5501 | MethList && MethList->Method; |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5502 | MethList = MethList->Next) { |
| 5503 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 5504 | NumSelIdents)) |
| 5505 | continue; |
| 5506 | |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 5507 | if (AtParameterName) { |
| 5508 | // Suggest parameter names we've seen before. |
| 5509 | if (NumSelIdents && NumSelIdents <= MethList->Method->param_size()) { |
| 5510 | ParmVarDecl *Param = MethList->Method->param_begin()[NumSelIdents-1]; |
| 5511 | if (Param->getIdentifier()) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5512 | CodeCompletionBuilder Builder(Results.getAllocator()); |
| 5513 | Builder.AddTypedTextChunk(CopyString(Builder.getAllocator(), |
| 5514 | Param->getIdentifier()->getName())); |
| 5515 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 5516 | } |
| 5517 | } |
| 5518 | |
| 5519 | continue; |
| 5520 | } |
| 5521 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5522 | Result R(MethList->Method, 0); |
| 5523 | R.StartParameter = NumSelIdents; |
| 5524 | R.AllParametersAreInformative = false; |
| 5525 | R.DeclaringEntity = true; |
| 5526 | Results.MaybeAddResult(R, CurContext); |
| 5527 | } |
| 5528 | } |
| 5529 | |
| 5530 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5531 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5532 | CodeCompletionContext::CCC_Other, |
| 5533 | Results.data(),Results.size()); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5534 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 5535 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5536 | void Sema::CodeCompletePreprocessorDirective(bool InConditional) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5537 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5538 | CodeCompletionContext::CCC_PreprocessorDirective); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5539 | Results.EnterNewScope(); |
| 5540 | |
| 5541 | // #if <condition> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5542 | CodeCompletionBuilder Builder(Results.getAllocator()); |
| 5543 | Builder.AddTypedTextChunk("if"); |
| 5544 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5545 | Builder.AddPlaceholderChunk("condition"); |
| 5546 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5547 | |
| 5548 | // #ifdef <macro> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5549 | Builder.AddTypedTextChunk("ifdef"); |
| 5550 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5551 | Builder.AddPlaceholderChunk("macro"); |
| 5552 | Results.AddResult(Builder.TakeString()); |
| 5553 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5554 | // #ifndef <macro> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5555 | Builder.AddTypedTextChunk("ifndef"); |
| 5556 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5557 | Builder.AddPlaceholderChunk("macro"); |
| 5558 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5559 | |
| 5560 | if (InConditional) { |
| 5561 | // #elif <condition> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5562 | Builder.AddTypedTextChunk("elif"); |
| 5563 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5564 | Builder.AddPlaceholderChunk("condition"); |
| 5565 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5566 | |
| 5567 | // #else |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5568 | Builder.AddTypedTextChunk("else"); |
| 5569 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5570 | |
| 5571 | // #endif |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5572 | Builder.AddTypedTextChunk("endif"); |
| 5573 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5574 | } |
| 5575 | |
| 5576 | // #include "header" |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5577 | Builder.AddTypedTextChunk("include"); |
| 5578 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5579 | Builder.AddTextChunk("\""); |
| 5580 | Builder.AddPlaceholderChunk("header"); |
| 5581 | Builder.AddTextChunk("\""); |
| 5582 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5583 | |
| 5584 | // #include <header> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5585 | Builder.AddTypedTextChunk("include"); |
| 5586 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5587 | Builder.AddTextChunk("<"); |
| 5588 | Builder.AddPlaceholderChunk("header"); |
| 5589 | Builder.AddTextChunk(">"); |
| 5590 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5591 | |
| 5592 | // #define <macro> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5593 | Builder.AddTypedTextChunk("define"); |
| 5594 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5595 | Builder.AddPlaceholderChunk("macro"); |
| 5596 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5597 | |
| 5598 | // #define <macro>(<args>) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5599 | Builder.AddTypedTextChunk("define"); |
| 5600 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5601 | Builder.AddPlaceholderChunk("macro"); |
| 5602 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5603 | Builder.AddPlaceholderChunk("args"); |
| 5604 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5605 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5606 | |
| 5607 | // #undef <macro> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5608 | Builder.AddTypedTextChunk("undef"); |
| 5609 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5610 | Builder.AddPlaceholderChunk("macro"); |
| 5611 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5612 | |
| 5613 | // #line <number> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5614 | Builder.AddTypedTextChunk("line"); |
| 5615 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5616 | Builder.AddPlaceholderChunk("number"); |
| 5617 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5618 | |
| 5619 | // #line <number> "filename" |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5620 | Builder.AddTypedTextChunk("line"); |
| 5621 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5622 | Builder.AddPlaceholderChunk("number"); |
| 5623 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5624 | Builder.AddTextChunk("\""); |
| 5625 | Builder.AddPlaceholderChunk("filename"); |
| 5626 | Builder.AddTextChunk("\""); |
| 5627 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5628 | |
| 5629 | // #error <message> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5630 | Builder.AddTypedTextChunk("error"); |
| 5631 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5632 | Builder.AddPlaceholderChunk("message"); |
| 5633 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5634 | |
| 5635 | // #pragma <arguments> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5636 | Builder.AddTypedTextChunk("pragma"); |
| 5637 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5638 | Builder.AddPlaceholderChunk("arguments"); |
| 5639 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5640 | |
| 5641 | if (getLangOptions().ObjC1) { |
| 5642 | // #import "header" |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5643 | Builder.AddTypedTextChunk("import"); |
| 5644 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5645 | Builder.AddTextChunk("\""); |
| 5646 | Builder.AddPlaceholderChunk("header"); |
| 5647 | Builder.AddTextChunk("\""); |
| 5648 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5649 | |
| 5650 | // #import <header> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5651 | Builder.AddTypedTextChunk("import"); |
| 5652 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5653 | Builder.AddTextChunk("<"); |
| 5654 | Builder.AddPlaceholderChunk("header"); |
| 5655 | Builder.AddTextChunk(">"); |
| 5656 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5657 | } |
| 5658 | |
| 5659 | // #include_next "header" |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5660 | Builder.AddTypedTextChunk("include_next"); |
| 5661 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5662 | Builder.AddTextChunk("\""); |
| 5663 | Builder.AddPlaceholderChunk("header"); |
| 5664 | Builder.AddTextChunk("\""); |
| 5665 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5666 | |
| 5667 | // #include_next <header> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5668 | Builder.AddTypedTextChunk("include_next"); |
| 5669 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5670 | Builder.AddTextChunk("<"); |
| 5671 | Builder.AddPlaceholderChunk("header"); |
| 5672 | Builder.AddTextChunk(">"); |
| 5673 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5674 | |
| 5675 | // #warning <message> |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5676 | Builder.AddTypedTextChunk("warning"); |
| 5677 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5678 | Builder.AddPlaceholderChunk("message"); |
| 5679 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5680 | |
| 5681 | // Note: #ident and #sccs are such crazy anachronisms that we don't provide |
| 5682 | // completions for them. And __include_macros is a Clang-internal extension |
| 5683 | // that we don't want to encourage anyone to use. |
| 5684 | |
| 5685 | // FIXME: we don't support #assert or #unassert, so don't suggest them. |
| 5686 | Results.ExitScope(); |
| 5687 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5688 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 5689 | CodeCompletionContext::CCC_PreprocessorDirective, |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5690 | Results.data(), Results.size()); |
| 5691 | } |
| 5692 | |
| 5693 | void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) { |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5694 | CodeCompleteOrdinaryName(S, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5695 | S->getFnParent()? Sema::PCC_RecoveryInFunction |
| 5696 | : Sema::PCC_Namespace); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5697 | } |
| 5698 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5699 | void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5700 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5701 | IsDefinition? CodeCompletionContext::CCC_MacroName |
| 5702 | : CodeCompletionContext::CCC_MacroNameUse); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 5703 | if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) { |
| 5704 | // Add just the names of macros, not their arguments. |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5705 | CodeCompletionBuilder Builder(Results.getAllocator()); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 5706 | Results.EnterNewScope(); |
| 5707 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 5708 | MEnd = PP.macro_end(); |
| 5709 | M != MEnd; ++M) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5710 | Builder.AddTypedTextChunk(CopyString(Builder.getAllocator(), |
| 5711 | M->first->getName())); |
| 5712 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 5713 | } |
| 5714 | Results.ExitScope(); |
| 5715 | } else if (IsDefinition) { |
| 5716 | // FIXME: Can we detect when the user just wrote an include guard above? |
| 5717 | } |
| 5718 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5719 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 5720 | Results.data(), Results.size()); |
| 5721 | } |
| 5722 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5723 | void Sema::CodeCompletePreprocessorExpression() { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5724 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5725 | CodeCompletionContext::CCC_PreprocessorExpression); |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5726 | |
| 5727 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
| 5728 | AddMacroResults(PP, Results); |
| 5729 | |
| 5730 | // defined (<macro>) |
| 5731 | Results.EnterNewScope(); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5732 | CodeCompletionBuilder Builder(Results.getAllocator()); |
| 5733 | Builder.AddTypedTextChunk("defined"); |
| 5734 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5735 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5736 | Builder.AddPlaceholderChunk("macro"); |
| 5737 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5738 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5739 | Results.ExitScope(); |
| 5740 | |
| 5741 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5742 | CodeCompletionContext::CCC_PreprocessorExpression, |
| 5743 | Results.data(), Results.size()); |
| 5744 | } |
| 5745 | |
| 5746 | void Sema::CodeCompletePreprocessorMacroArgument(Scope *S, |
| 5747 | IdentifierInfo *Macro, |
| 5748 | MacroInfo *MacroInfo, |
| 5749 | unsigned Argument) { |
| 5750 | // FIXME: In the future, we could provide "overload" results, much like we |
| 5751 | // do for function calls. |
| 5752 | |
| 5753 | CodeCompleteOrdinaryName(S, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5754 | S->getFnParent()? Sema::PCC_RecoveryInFunction |
| 5755 | : Sema::PCC_Namespace); |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5756 | } |
| 5757 | |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 5758 | void Sema::CodeCompleteNaturalLanguage() { |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 5759 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | af1c6b5 | 2010-08-25 17:10:00 +0000 | [diff] [blame] | 5760 | CodeCompletionContext::CCC_NaturalLanguage, |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 5761 | 0, 0); |
| 5762 | } |
| 5763 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5764 | void Sema::GatherGlobalCodeCompletions(llvm::BumpPtrAllocator &Allocator, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5765 | llvm::SmallVectorImpl<CodeCompletionResult> &Results) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame^] | 5766 | ResultBuilder Builder(*this, Allocator, CodeCompletionContext::CCC_Recovery); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5767 | if (!CodeCompleter || CodeCompleter->includeGlobals()) { |
| 5768 | CodeCompletionDeclConsumer Consumer(Builder, |
| 5769 | Context.getTranslationUnitDecl()); |
| 5770 | LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName, |
| 5771 | Consumer); |
| 5772 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 5773 | |
| 5774 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
| 5775 | AddMacroResults(PP, Builder); |
| 5776 | |
| 5777 | Results.clear(); |
| 5778 | Results.insert(Results.end(), |
| 5779 | Builder.data(), Builder.data() + Builder.size()); |
| 5780 | } |