| Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1 | //===---------------- SemaCodeComplete.cpp - Code Completion ----*- C++ -*-===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
|  | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
|  | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | //  This file defines the code-completion semantic actions. | 
|  | 11 | // | 
|  | 12 | //===----------------------------------------------------------------------===// | 
| John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 13 | #include "clang/Sema/SemaInternal.h" | 
| Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Lookup.h" | 
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Overload.h" | 
| Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 16 | #include "clang/Sema/CodeCompleteConsumer.h" | 
| Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ExternalSemaSource.h" | 
| John McCall | cc14d1f | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Scope.h" | 
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ScopeInfo.h" | 
| John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" | 
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" | 
| Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" | 
| Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 23 | #include "clang/Lex/MacroInfo.h" | 
|  | 24 | #include "clang/Lex/Preprocessor.h" | 
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DenseSet.h" | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" | 
| Douglas Gregor | e6688e6 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" | 
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringSwitch.h" | 
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Twine.h" | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 30 | #include <list> | 
|  | 31 | #include <map> | 
|  | 32 | #include <vector> | 
| Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 33 |  | 
|  | 34 | using namespace clang; | 
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 35 | using namespace sema; | 
| Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 36 |  | 
| Douglas Gregor | 3545ff4 | 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 | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 47 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | 3545ff4 | 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 | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 58 | typedef std::pair<NamedDecl *, unsigned> DeclIndexPair; | 
|  | 59 |  | 
|  | 60 | /// \brief An entry in the shadow map, which is optimized to store | 
|  | 61 | /// a single (declaration, index) mapping (the common case) but | 
|  | 62 | /// can also store a list of (declaration, index) mappings. | 
|  | 63 | class ShadowMapEntry { | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 64 | typedef SmallVector<DeclIndexPair, 4> DeclIndexPairVector; | 
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 65 |  | 
|  | 66 | /// \brief Contains either the solitary NamedDecl * or a vector | 
|  | 67 | /// of (declaration, index) pairs. | 
|  | 68 | llvm::PointerUnion<NamedDecl *, DeclIndexPairVector*> DeclOrVector; | 
|  | 69 |  | 
|  | 70 | /// \brief When the entry contains a single declaration, this is | 
|  | 71 | /// the index associated with that entry. | 
|  | 72 | unsigned SingleDeclIndex; | 
|  | 73 |  | 
|  | 74 | public: | 
|  | 75 | ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { } | 
|  | 76 |  | 
|  | 77 | void Add(NamedDecl *ND, unsigned Index) { | 
|  | 78 | if (DeclOrVector.isNull()) { | 
|  | 79 | // 0 - > 1 elements: just set the single element information. | 
|  | 80 | DeclOrVector = ND; | 
|  | 81 | SingleDeclIndex = Index; | 
|  | 82 | return; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | if (NamedDecl *PrevND = DeclOrVector.dyn_cast<NamedDecl *>()) { | 
|  | 86 | // 1 -> 2 elements: create the vector of results and push in the | 
|  | 87 | // existing declaration. | 
|  | 88 | DeclIndexPairVector *Vec = new DeclIndexPairVector; | 
|  | 89 | Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex)); | 
|  | 90 | DeclOrVector = Vec; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | // Add the new element to the end of the vector. | 
|  | 94 | DeclOrVector.get<DeclIndexPairVector*>()->push_back( | 
|  | 95 | DeclIndexPair(ND, Index)); | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | void Destroy() { | 
|  | 99 | if (DeclIndexPairVector *Vec | 
|  | 100 | = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) { | 
|  | 101 | delete Vec; | 
|  | 102 | DeclOrVector = ((NamedDecl *)0); | 
|  | 103 | } | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | // Iteration. | 
|  | 107 | class iterator; | 
|  | 108 | iterator begin() const; | 
|  | 109 | iterator end() const; | 
|  | 110 | }; | 
|  | 111 |  | 
| Douglas Gregor | 3545ff4 | 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 | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 115 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; | 
| Douglas Gregor | 3545ff4 | 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 | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 120 |  | 
|  | 121 | /// \brief The allocator used to allocate new code-completion strings. | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 122 | CodeCompletionAllocator &Allocator; | 
| Douglas Gregor | 3545ff4 | 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 | 6ae4c52 | 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 | 7aa6b22 | 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 | 3545ff4 | 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 | 9be0ed4 | 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 | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 150 | /// \brief The selector that we prefer. | 
|  | 151 | Selector PreferredSelector; | 
|  | 152 |  | 
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 153 | /// \brief The completion context in which we are gathering results. | 
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 154 | CodeCompletionContext CompletionContext; | 
|  | 155 |  | 
| Douglas Gregor | 05fcf84 | 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 | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 160 | void AdjustResultPriorityForDecl(Result &R); | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 161 |  | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 162 | void MaybeAddConstructorResults(Result R); | 
|  | 163 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 164 | public: | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 165 | explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator, | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 166 | const CodeCompletionContext &CompletionContext, | 
|  | 167 | LookupFilter Filter = 0) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 168 | : SemaRef(SemaRef), Allocator(Allocator), Filter(Filter), | 
|  | 169 | AllowNestedNameSpecifiers(false), HasObjectTypeQualifiers(false), | 
| Douglas Gregor | 05fcf84 | 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 | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 191 |  | 
| Douglas Gregor | f64acca | 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 | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 196 | SemaRef.CodeCompleter->includeCodePatterns(); | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
| Douglas Gregor | 3545ff4 | 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 | 3545ff4 | 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 | 7aa6b22 | 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 | 9be0ed4 | 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 | c2cb2e2 | 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 | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 233 |  | 
| Douglas Gregor | 50832e0 | 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 | 6ae4c52 | 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 | 7466127 | 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 | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 249 | /// \brief Retrieve the allocator used to allocate code completion strings. | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 250 | CodeCompletionAllocator &getAllocator() const { return Allocator; } | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 251 |  | 
| Douglas Gregor | 7c20861 | 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 | 6ae4c52 | 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 | e0717ab | 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 | 3545ff4 | 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 | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 273 | /// | 
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 274 | /// \param CurContext the result to add (if it is unique). | 
| Douglas Gregor | 2af2f67 | 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 | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 278 |  | 
| Douglas Gregor | c580c52 | 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 | 09bbc65 | 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 | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 292 |  | 
| Douglas Gregor | 78a2101 | 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 | 3545ff4 | 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 | baf6961 | 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 | 3545ff4 | 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 | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 311 | bool IsOrdinaryName(NamedDecl *ND) const; | 
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 312 | bool IsOrdinaryNonTypeName(NamedDecl *ND) const; | 
| Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 313 | bool IsIntegralConstantValue(NamedDecl *ND) const; | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 314 | bool IsOrdinaryNonValueName(NamedDecl *ND) const; | 
| Douglas Gregor | 3545ff4 | 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 | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 322 | bool IsMember(NamedDecl *ND) const; | 
| Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 323 | bool IsObjCIvar(NamedDecl *ND) const; | 
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 324 | bool IsObjCMessageReceiver(NamedDecl *ND) const; | 
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 325 | bool IsObjCCollection(NamedDecl *ND) const; | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 326 | bool IsImpossibleToSatisfy(NamedDecl *ND) const; | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 327 | //@} | 
|  | 328 | }; | 
|  | 329 | } | 
|  | 330 |  | 
| Douglas Gregor | 05e7ca3 | 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 | 9795b39 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 373 | /*iterator operator++(int) { | 
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 374 | iterator tmp(*this); | 
|  | 375 | ++(*this); | 
|  | 376 | return tmp; | 
| Chris Lattner | 9795b39 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 377 | }*/ | 
| Douglas Gregor | 05e7ca3 | 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 | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 383 | return *DeclOrIterator.get<const DeclIndexPair*>(); | 
| Douglas Gregor | 05e7ca3 | 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 | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 391 | return X.DeclOrIterator.getOpaqueValue() | 
|  | 392 | == Y.DeclOrIterator.getOpaqueValue() && | 
| Douglas Gregor | 05e7ca3 | 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 | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 397 | return !(X == Y); | 
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 398 | } | 
|  | 399 | }; | 
|  | 400 |  | 
| Douglas Gregor | 05e7ca3 | 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 | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 420 | /// \brief Compute the qualification required to get from the current context | 
|  | 421 | /// (\p CurContext) to the target context (\p TargetContext). | 
|  | 422 | /// | 
|  | 423 | /// \param Context the AST context in which the qualification will be used. | 
|  | 424 | /// | 
|  | 425 | /// \param CurContext the context where an entity is being named, which is | 
|  | 426 | /// typically based on the current scope. | 
|  | 427 | /// | 
|  | 428 | /// \param TargetContext the context in which the named entity actually | 
|  | 429 | /// resides. | 
|  | 430 | /// | 
|  | 431 | /// \returns a nested name specifier that refers into the target context, or | 
|  | 432 | /// NULL if no qualification is needed. | 
|  | 433 | static NestedNameSpecifier * | 
|  | 434 | getRequiredQualification(ASTContext &Context, | 
|  | 435 | DeclContext *CurContext, | 
|  | 436 | DeclContext *TargetContext) { | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 437 | SmallVector<DeclContext *, 4> TargetParents; | 
| Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 438 |  | 
|  | 439 | for (DeclContext *CommonAncestor = TargetContext; | 
|  | 440 | CommonAncestor && !CommonAncestor->Encloses(CurContext); | 
|  | 441 | CommonAncestor = CommonAncestor->getLookupParent()) { | 
|  | 442 | if (CommonAncestor->isTransparentContext() || | 
|  | 443 | CommonAncestor->isFunctionOrMethod()) | 
|  | 444 | continue; | 
|  | 445 |  | 
|  | 446 | TargetParents.push_back(CommonAncestor); | 
|  | 447 | } | 
|  | 448 |  | 
|  | 449 | NestedNameSpecifier *Result = 0; | 
|  | 450 | while (!TargetParents.empty()) { | 
|  | 451 | DeclContext *Parent = TargetParents.back(); | 
|  | 452 | TargetParents.pop_back(); | 
|  | 453 |  | 
| Douglas Gregor | 68762e7 | 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 | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 458 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); | 
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 459 | } | 
| Douglas Gregor | 2af2f67 | 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 | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 464 | } | 
| Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 465 | return Result; | 
|  | 466 | } | 
|  | 467 |  | 
| Douglas Gregor | 6ae4c52 | 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 | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 472 | ND = ND->getUnderlyingDecl(); | 
|  | 473 | unsigned IDNS = ND->getIdentifierNamespace(); | 
| Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 474 |  | 
|  | 475 | // Skip unnamed entities. | 
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 476 | if (!ND->getDeclName()) | 
|  | 477 | return false; | 
| Douglas Gregor | 3545ff4 | 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 | bbbbe4e | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 481 | if (IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend)) | 
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 482 | return false; | 
|  | 483 |  | 
| Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 484 | // Class template (partial) specializations are never added as results. | 
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 485 | if (isa<ClassTemplateSpecializationDecl>(ND) || | 
|  | 486 | isa<ClassTemplatePartialSpecializationDecl>(ND)) | 
|  | 487 | return false; | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 488 |  | 
| Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 489 | // Using declarations themselves are never added as results. | 
| Douglas Gregor | 7c20861 | 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 | 3545ff4 | 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 | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 497 | return false; | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 498 |  | 
| Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 499 | // Filter out names reserved for the implementation (C99 7.1.3, | 
| Douglas Gregor | 9f1570d | 2010-07-14 17:44:04 +0000 | [diff] [blame] | 500 | // C++ [lib.global.names]) if they come from a system header. | 
| Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 501 | // | 
|  | 502 | // FIXME: Add predicate for this. | 
| Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 503 | if (Id->getLength() >= 2) { | 
| Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 504 | const char *Name = Id->getNameStart(); | 
| Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 505 | if (Name[0] == '_' && | 
| Douglas Gregor | 9f1570d | 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 | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 510 | return false; | 
| Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 511 | } | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 512 | } | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 513 |  | 
| Douglas Gregor | 2927c0c | 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 | 59cab55 | 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 | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 525 | Filter != &ResultBuilder::IsNamespaceOrAlias && | 
|  | 526 | Filter != 0)) | 
| Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 527 | AsNestedNameSpecifier = true; | 
|  | 528 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 529 | // Filter out any unwanted results. | 
| Douglas Gregor | 6ae4c52 | 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 | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 541 | return false; | 
| Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 542 | } | 
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 543 | // ... then it must be interesting! | 
|  | 544 | return true; | 
|  | 545 | } | 
|  | 546 |  | 
| Douglas Gregor | e0717ab | 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 | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 555 | DeclContext *HiddenCtx = R.Declaration->getDeclContext()->getRedeclContext(); | 
| Douglas Gregor | e0717ab | 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 | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 561 | if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext()) | 
| Douglas Gregor | e0717ab | 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 | 95887f9 | 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 | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 577 | SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) { | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 578 | switch (T->getTypeClass()) { | 
|  | 579 | case Type::Builtin: | 
|  | 580 | switch (cast<BuiltinType>(T)->getKind()) { | 
|  | 581 | case BuiltinType::Void: | 
|  | 582 | return STC_Void; | 
|  | 583 |  | 
|  | 584 | case BuiltinType::NullPtr: | 
|  | 585 | return STC_Pointer; | 
|  | 586 |  | 
|  | 587 | case BuiltinType::Overload: | 
|  | 588 | case BuiltinType::Dependent: | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 589 | return STC_Other; | 
|  | 590 |  | 
|  | 591 | case BuiltinType::ObjCId: | 
|  | 592 | case BuiltinType::ObjCClass: | 
|  | 593 | case BuiltinType::ObjCSel: | 
|  | 594 | return STC_ObjectiveC; | 
|  | 595 |  | 
|  | 596 | default: | 
|  | 597 | return STC_Arithmetic; | 
|  | 598 | } | 
|  | 599 | return STC_Other; | 
|  | 600 |  | 
|  | 601 | case Type::Complex: | 
|  | 602 | return STC_Arithmetic; | 
|  | 603 |  | 
|  | 604 | case Type::Pointer: | 
|  | 605 | return STC_Pointer; | 
|  | 606 |  | 
|  | 607 | case Type::BlockPointer: | 
|  | 608 | return STC_Block; | 
|  | 609 |  | 
|  | 610 | case Type::LValueReference: | 
|  | 611 | case Type::RValueReference: | 
|  | 612 | return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType()); | 
|  | 613 |  | 
|  | 614 | case Type::ConstantArray: | 
|  | 615 | case Type::IncompleteArray: | 
|  | 616 | case Type::VariableArray: | 
|  | 617 | case Type::DependentSizedArray: | 
|  | 618 | return STC_Array; | 
|  | 619 |  | 
|  | 620 | case Type::DependentSizedExtVector: | 
|  | 621 | case Type::Vector: | 
|  | 622 | case Type::ExtVector: | 
|  | 623 | return STC_Arithmetic; | 
|  | 624 |  | 
|  | 625 | case Type::FunctionProto: | 
|  | 626 | case Type::FunctionNoProto: | 
|  | 627 | return STC_Function; | 
|  | 628 |  | 
|  | 629 | case Type::Record: | 
|  | 630 | return STC_Record; | 
|  | 631 |  | 
|  | 632 | case Type::Enum: | 
|  | 633 | return STC_Arithmetic; | 
|  | 634 |  | 
|  | 635 | case Type::ObjCObject: | 
|  | 636 | case Type::ObjCInterface: | 
|  | 637 | case Type::ObjCObjectPointer: | 
|  | 638 | return STC_ObjectiveC; | 
|  | 639 |  | 
|  | 640 | default: | 
|  | 641 | return STC_Other; | 
|  | 642 | } | 
|  | 643 | } | 
|  | 644 |  | 
|  | 645 | /// \brief Get the type that a given expression will have if this declaration | 
|  | 646 | /// is used as an expression in its "typical" code-completion form. | 
| Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 647 | QualType clang::getDeclUsageType(ASTContext &C, NamedDecl *ND) { | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 648 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); | 
|  | 649 |  | 
|  | 650 | if (TypeDecl *Type = dyn_cast<TypeDecl>(ND)) | 
|  | 651 | return C.getTypeDeclType(Type); | 
|  | 652 | if (ObjCInterfaceDecl *Iface = dyn_cast<ObjCInterfaceDecl>(ND)) | 
|  | 653 | return C.getObjCInterfaceType(Iface); | 
|  | 654 |  | 
|  | 655 | QualType T; | 
|  | 656 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) | 
| Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 657 | T = Function->getCallResultType(); | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 658 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) | 
| Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 659 | T = Method->getSendResultType(); | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 660 | else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) | 
| Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 661 | T = FunTmpl->getTemplatedDecl()->getCallResultType(); | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 662 | else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) | 
|  | 663 | T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext())); | 
|  | 664 | else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) | 
|  | 665 | T = Property->getType(); | 
|  | 666 | else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND)) | 
|  | 667 | T = Value->getType(); | 
|  | 668 | else | 
|  | 669 | return QualType(); | 
| Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 670 |  | 
|  | 671 | // Dig through references, function pointers, and block pointers to | 
|  | 672 | // get down to the likely type of an expression when the entity is | 
|  | 673 | // used. | 
|  | 674 | do { | 
|  | 675 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) { | 
|  | 676 | T = Ref->getPointeeType(); | 
|  | 677 | continue; | 
|  | 678 | } | 
|  | 679 |  | 
|  | 680 | if (const PointerType *Pointer = T->getAs<PointerType>()) { | 
|  | 681 | if (Pointer->getPointeeType()->isFunctionType()) { | 
|  | 682 | T = Pointer->getPointeeType(); | 
|  | 683 | continue; | 
|  | 684 | } | 
|  | 685 |  | 
|  | 686 | break; | 
|  | 687 | } | 
|  | 688 |  | 
|  | 689 | if (const BlockPointerType *Block = T->getAs<BlockPointerType>()) { | 
|  | 690 | T = Block->getPointeeType(); | 
|  | 691 | continue; | 
|  | 692 | } | 
|  | 693 |  | 
|  | 694 | if (const FunctionType *Function = T->getAs<FunctionType>()) { | 
|  | 695 | T = Function->getResultType(); | 
|  | 696 | continue; | 
|  | 697 | } | 
|  | 698 |  | 
|  | 699 | break; | 
|  | 700 | } while (true); | 
|  | 701 |  | 
|  | 702 | return T; | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 703 | } | 
|  | 704 |  | 
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 705 | void ResultBuilder::AdjustResultPriorityForDecl(Result &R) { | 
|  | 706 | // If this is an Objective-C method declaration whose selector matches our | 
|  | 707 | // preferred selector, give it a priority boost. | 
|  | 708 | if (!PreferredSelector.isNull()) | 
|  | 709 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration)) | 
|  | 710 | if (PreferredSelector == Method->getSelector()) | 
|  | 711 | R.Priority += CCD_SelectorMatch; | 
| Douglas Gregor | 5fb901d | 2010-09-20 23:11:55 +0000 | [diff] [blame] | 712 |  | 
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 713 | // If we have a preferred type, adjust the priority for results with exactly- | 
|  | 714 | // matching or nearly-matching types. | 
|  | 715 | if (!PreferredType.isNull()) { | 
|  | 716 | QualType T = getDeclUsageType(SemaRef.Context, R.Declaration); | 
|  | 717 | if (!T.isNull()) { | 
|  | 718 | CanQualType TC = SemaRef.Context.getCanonicalType(T); | 
|  | 719 | // Check for exactly-matching types (modulo qualifiers). | 
|  | 720 | if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) | 
|  | 721 | R.Priority /= CCF_ExactTypeMatch; | 
|  | 722 | // Check for nearly-matching types, based on classification of each. | 
|  | 723 | else if ((getSimplifiedTypeClass(PreferredType) | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 724 | == getSimplifiedTypeClass(TC)) && | 
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 725 | !(PreferredType->isEnumeralType() && TC->isEnumeralType())) | 
|  | 726 | R.Priority /= CCF_SimilarTypeMatch; | 
|  | 727 | } | 
|  | 728 | } | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 729 | } | 
|  | 730 |  | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 731 | void ResultBuilder::MaybeAddConstructorResults(Result R) { | 
|  | 732 | if (!SemaRef.getLangOptions().CPlusPlus || !R.Declaration || | 
|  | 733 | !CompletionContext.wantConstructorResults()) | 
|  | 734 | return; | 
|  | 735 |  | 
|  | 736 | ASTContext &Context = SemaRef.Context; | 
|  | 737 | NamedDecl *D = R.Declaration; | 
|  | 738 | CXXRecordDecl *Record = 0; | 
|  | 739 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) | 
|  | 740 | Record = ClassTemplate->getTemplatedDecl(); | 
|  | 741 | else if ((Record = dyn_cast<CXXRecordDecl>(D))) { | 
|  | 742 | // Skip specializations and partial specializations. | 
|  | 743 | if (isa<ClassTemplateSpecializationDecl>(Record)) | 
|  | 744 | return; | 
|  | 745 | } else { | 
|  | 746 | // There are no constructors here. | 
|  | 747 | return; | 
|  | 748 | } | 
|  | 749 |  | 
|  | 750 | Record = Record->getDefinition(); | 
|  | 751 | if (!Record) | 
|  | 752 | return; | 
|  | 753 |  | 
|  | 754 |  | 
|  | 755 | QualType RecordTy = Context.getTypeDeclType(Record); | 
|  | 756 | DeclarationName ConstructorName | 
|  | 757 | = Context.DeclarationNames.getCXXConstructorName( | 
|  | 758 | Context.getCanonicalType(RecordTy)); | 
|  | 759 | for (DeclContext::lookup_result Ctors = Record->lookup(ConstructorName); | 
|  | 760 | Ctors.first != Ctors.second; ++Ctors.first) { | 
|  | 761 | R.Declaration = *Ctors.first; | 
|  | 762 | R.CursorKind = getCursorKindForDecl(R.Declaration); | 
|  | 763 | Results.push_back(R); | 
|  | 764 | } | 
|  | 765 | } | 
|  | 766 |  | 
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 767 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { | 
|  | 768 | assert(!ShadowMaps.empty() && "Must enter into a results scope"); | 
|  | 769 |  | 
|  | 770 | if (R.Kind != Result::RK_Declaration) { | 
|  | 771 | // For non-declaration results, just add the result. | 
|  | 772 | Results.push_back(R); | 
|  | 773 | return; | 
|  | 774 | } | 
|  | 775 |  | 
|  | 776 | // Look through using declarations. | 
|  | 777 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { | 
|  | 778 | MaybeAddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext); | 
|  | 779 | return; | 
|  | 780 | } | 
|  | 781 |  | 
|  | 782 | Decl *CanonDecl = R.Declaration->getCanonicalDecl(); | 
|  | 783 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); | 
|  | 784 |  | 
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 785 | bool AsNestedNameSpecifier = false; | 
|  | 786 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) | 
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 787 | return; | 
|  | 788 |  | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 789 | // C++ constructors are never found by name lookup. | 
|  | 790 | if (isa<CXXConstructorDecl>(R.Declaration)) | 
|  | 791 | return; | 
|  | 792 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 793 | ShadowMap &SMap = ShadowMaps.back(); | 
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 794 | ShadowMapEntry::iterator I, IEnd; | 
|  | 795 | ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName()); | 
|  | 796 | if (NamePos != SMap.end()) { | 
|  | 797 | I = NamePos->second.begin(); | 
|  | 798 | IEnd = NamePos->second.end(); | 
|  | 799 | } | 
|  | 800 |  | 
|  | 801 | for (; I != IEnd; ++I) { | 
|  | 802 | NamedDecl *ND = I->first; | 
|  | 803 | unsigned Index = I->second; | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 804 | if (ND->getCanonicalDecl() == CanonDecl) { | 
|  | 805 | // This is a redeclaration. Always pick the newer declaration. | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 806 | Results[Index].Declaration = R.Declaration; | 
|  | 807 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 808 | // We're done. | 
|  | 809 | return; | 
|  | 810 | } | 
|  | 811 | } | 
|  | 812 |  | 
|  | 813 | // This is a new declaration in this scope. However, check whether this | 
|  | 814 | // declaration name is hidden by a similarly-named declaration in an outer | 
|  | 815 | // scope. | 
|  | 816 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); | 
|  | 817 | --SMEnd; | 
|  | 818 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { | 
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 819 | ShadowMapEntry::iterator I, IEnd; | 
|  | 820 | ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName()); | 
|  | 821 | if (NamePos != SM->end()) { | 
|  | 822 | I = NamePos->second.begin(); | 
|  | 823 | IEnd = NamePos->second.end(); | 
|  | 824 | } | 
|  | 825 | for (; I != IEnd; ++I) { | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 826 | // A tag declaration does not hide a non-tag declaration. | 
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 827 | if (I->first->hasTagIdentifierNamespace() && | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 828 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | | 
|  | 829 | Decl::IDNS_ObjCProtocol))) | 
|  | 830 | continue; | 
|  | 831 |  | 
|  | 832 | // Protocols are in distinct namespaces from everything else. | 
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 833 | if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 834 | || (IDNS & Decl::IDNS_ObjCProtocol)) && | 
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 835 | I->first->getIdentifierNamespace() != IDNS) | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 836 | continue; | 
|  | 837 |  | 
|  | 838 | // The newly-added result is hidden by an entry in the shadow map. | 
| Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 839 | if (CheckHiddenResult(R, CurContext, I->first)) | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 840 | return; | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 841 |  | 
|  | 842 | break; | 
|  | 843 | } | 
|  | 844 | } | 
|  | 845 |  | 
|  | 846 | // Make sure that any given declaration only shows up in the result set once. | 
|  | 847 | if (!AllDeclsFound.insert(CanonDecl)) | 
|  | 848 | return; | 
| Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 849 |  | 
| Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 850 | // If the filter is for nested-name-specifiers, then this result starts a | 
|  | 851 | // nested-name-specifier. | 
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 852 | if (AsNestedNameSpecifier) { | 
| Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 853 | R.StartsNestedNameSpecifier = true; | 
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 854 | R.Priority = CCP_NestedNameSpecifier; | 
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 855 | } else | 
|  | 856 | AdjustResultPriorityForDecl(R); | 
| Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 857 |  | 
| Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 858 | // If this result is supposed to have an informative qualifier, add one. | 
| Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 859 | if (R.QualifierIsInformative && !R.Qualifier && | 
|  | 860 | !R.StartsNestedNameSpecifier) { | 
| Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 861 | DeclContext *Ctx = R.Declaration->getDeclContext(); | 
|  | 862 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) | 
|  | 863 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace); | 
|  | 864 | else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) | 
|  | 865 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false, | 
|  | 866 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); | 
|  | 867 | else | 
|  | 868 | R.QualifierIsInformative = false; | 
|  | 869 | } | 
| Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 870 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 871 | // Insert this result into the set of results and into the current shadow | 
|  | 872 | // map. | 
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 873 | SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size()); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 874 | Results.push_back(R); | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 875 |  | 
|  | 876 | if (!AsNestedNameSpecifier) | 
|  | 877 | MaybeAddConstructorResults(R); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 878 | } | 
|  | 879 |  | 
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 880 | void ResultBuilder::AddResult(Result R, DeclContext *CurContext, | 
| Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 881 | NamedDecl *Hiding, bool InBaseClass = false) { | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 882 | if (R.Kind != Result::RK_Declaration) { | 
|  | 883 | // For non-declaration results, just add the result. | 
|  | 884 | Results.push_back(R); | 
|  | 885 | return; | 
|  | 886 | } | 
|  | 887 |  | 
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 888 | // Look through using declarations. | 
|  | 889 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { | 
|  | 890 | AddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext, Hiding); | 
|  | 891 | return; | 
|  | 892 | } | 
|  | 893 |  | 
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 894 | bool AsNestedNameSpecifier = false; | 
|  | 895 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) | 
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 896 | return; | 
|  | 897 |  | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 898 | // C++ constructors are never found by name lookup. | 
|  | 899 | if (isa<CXXConstructorDecl>(R.Declaration)) | 
|  | 900 | return; | 
|  | 901 |  | 
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 902 | if (Hiding && CheckHiddenResult(R, CurContext, Hiding)) | 
|  | 903 | return; | 
|  | 904 |  | 
|  | 905 | // Make sure that any given declaration only shows up in the result set once. | 
|  | 906 | if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl())) | 
|  | 907 | return; | 
|  | 908 |  | 
|  | 909 | // If the filter is for nested-name-specifiers, then this result starts a | 
|  | 910 | // nested-name-specifier. | 
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 911 | if (AsNestedNameSpecifier) { | 
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 912 | R.StartsNestedNameSpecifier = true; | 
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 913 | R.Priority = CCP_NestedNameSpecifier; | 
|  | 914 | } | 
| Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 915 | else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass && | 
|  | 916 | isa<CXXRecordDecl>(R.Declaration->getDeclContext() | 
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 917 | ->getRedeclContext())) | 
| Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 918 | R.QualifierIsInformative = true; | 
|  | 919 |  | 
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 920 | // If this result is supposed to have an informative qualifier, add one. | 
|  | 921 | if (R.QualifierIsInformative && !R.Qualifier && | 
|  | 922 | !R.StartsNestedNameSpecifier) { | 
|  | 923 | DeclContext *Ctx = R.Declaration->getDeclContext(); | 
|  | 924 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) | 
|  | 925 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace); | 
|  | 926 | else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) | 
|  | 927 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false, | 
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 928 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); | 
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 929 | else | 
|  | 930 | R.QualifierIsInformative = false; | 
|  | 931 | } | 
|  | 932 |  | 
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 933 | // Adjust the priority if this result comes from a base class. | 
|  | 934 | if (InBaseClass) | 
|  | 935 | R.Priority += CCD_InBaseClass; | 
|  | 936 |  | 
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 937 | AdjustResultPriorityForDecl(R); | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 938 |  | 
| Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 939 | if (HasObjectTypeQualifiers) | 
|  | 940 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(R.Declaration)) | 
|  | 941 | if (Method->isInstance()) { | 
|  | 942 | Qualifiers MethodQuals | 
|  | 943 | = Qualifiers::fromCVRMask(Method->getTypeQualifiers()); | 
|  | 944 | if (ObjectTypeQualifiers == MethodQuals) | 
|  | 945 | R.Priority += CCD_ObjectQualifierMatch; | 
|  | 946 | else if (ObjectTypeQualifiers - MethodQuals) { | 
|  | 947 | // The method cannot be invoked, because doing so would drop | 
|  | 948 | // qualifiers. | 
|  | 949 | return; | 
|  | 950 | } | 
|  | 951 | } | 
|  | 952 |  | 
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 953 | // Insert this result into the set of results. | 
|  | 954 | Results.push_back(R); | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 955 |  | 
|  | 956 | if (!AsNestedNameSpecifier) | 
|  | 957 | MaybeAddConstructorResults(R); | 
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 958 | } | 
|  | 959 |  | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 960 | void ResultBuilder::AddResult(Result R) { | 
|  | 961 | assert(R.Kind != Result::RK_Declaration && | 
|  | 962 | "Declaration results need more context"); | 
|  | 963 | Results.push_back(R); | 
|  | 964 | } | 
|  | 965 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 966 | /// \brief Enter into a new scope. | 
|  | 967 | void ResultBuilder::EnterNewScope() { | 
|  | 968 | ShadowMaps.push_back(ShadowMap()); | 
|  | 969 | } | 
|  | 970 |  | 
|  | 971 | /// \brief Exit from the current scope. | 
|  | 972 | void ResultBuilder::ExitScope() { | 
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 973 | for (ShadowMap::iterator E = ShadowMaps.back().begin(), | 
|  | 974 | EEnd = ShadowMaps.back().end(); | 
|  | 975 | E != EEnd; | 
|  | 976 | ++E) | 
|  | 977 | E->second.Destroy(); | 
|  | 978 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 979 | ShadowMaps.pop_back(); | 
|  | 980 | } | 
|  | 981 |  | 
| Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 982 | /// \brief Determines whether this given declaration will be found by | 
|  | 983 | /// ordinary name lookup. | 
|  | 984 | bool ResultBuilder::IsOrdinaryName(NamedDecl *ND) const { | 
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 985 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); | 
|  | 986 |  | 
| Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 987 | unsigned IDNS = Decl::IDNS_Ordinary; | 
|  | 988 | if (SemaRef.getLangOptions().CPlusPlus) | 
| Douglas Gregor | 9858ed5 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 989 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; | 
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 990 | else if (SemaRef.getLangOptions().ObjC1) { | 
|  | 991 | if (isa<ObjCIvarDecl>(ND)) | 
|  | 992 | return true; | 
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 993 | } | 
|  | 994 |  | 
| Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 995 | return ND->getIdentifierNamespace() & IDNS; | 
|  | 996 | } | 
|  | 997 |  | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 998 | /// \brief Determines whether this given declaration will be found by | 
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 999 | /// ordinary name lookup but is not a type name. | 
|  | 1000 | bool ResultBuilder::IsOrdinaryNonTypeName(NamedDecl *ND) const { | 
|  | 1001 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); | 
|  | 1002 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) | 
|  | 1003 | return false; | 
|  | 1004 |  | 
|  | 1005 | unsigned IDNS = Decl::IDNS_Ordinary; | 
|  | 1006 | if (SemaRef.getLangOptions().CPlusPlus) | 
| Douglas Gregor | 9858ed5 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1007 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; | 
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1008 | else if (SemaRef.getLangOptions().ObjC1) { | 
|  | 1009 | if (isa<ObjCIvarDecl>(ND)) | 
|  | 1010 | return true; | 
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1011 | } | 
|  | 1012 |  | 
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1013 | return ND->getIdentifierNamespace() & IDNS; | 
|  | 1014 | } | 
|  | 1015 |  | 
| Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1016 | bool ResultBuilder::IsIntegralConstantValue(NamedDecl *ND) const { | 
|  | 1017 | if (!IsOrdinaryNonTypeName(ND)) | 
|  | 1018 | return 0; | 
|  | 1019 |  | 
|  | 1020 | if (ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl())) | 
|  | 1021 | if (VD->getType()->isIntegralOrEnumerationType()) | 
|  | 1022 | return true; | 
|  | 1023 |  | 
|  | 1024 | return false; | 
|  | 1025 | } | 
|  | 1026 |  | 
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1027 | /// \brief Determines whether this given declaration will be found by | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1028 | /// ordinary name lookup. | 
|  | 1029 | bool ResultBuilder::IsOrdinaryNonValueName(NamedDecl *ND) const { | 
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1030 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); | 
|  | 1031 |  | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1032 | unsigned IDNS = Decl::IDNS_Ordinary; | 
|  | 1033 | if (SemaRef.getLangOptions().CPlusPlus) | 
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1034 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace; | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1035 |  | 
|  | 1036 | return (ND->getIdentifierNamespace() & IDNS) && | 
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1037 | !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) && | 
|  | 1038 | !isa<ObjCPropertyDecl>(ND); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1039 | } | 
|  | 1040 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1041 | /// \brief Determines whether the given declaration is suitable as the | 
|  | 1042 | /// start of a C++ nested-name-specifier, e.g., a class or namespace. | 
|  | 1043 | bool ResultBuilder::IsNestedNameSpecifier(NamedDecl *ND) const { | 
|  | 1044 | // Allow us to find class templates, too. | 
|  | 1045 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) | 
|  | 1046 | ND = ClassTemplate->getTemplatedDecl(); | 
|  | 1047 |  | 
|  | 1048 | return SemaRef.isAcceptableNestedNameSpecifier(ND); | 
|  | 1049 | } | 
|  | 1050 |  | 
|  | 1051 | /// \brief Determines whether the given declaration is an enumeration. | 
|  | 1052 | bool ResultBuilder::IsEnum(NamedDecl *ND) const { | 
|  | 1053 | return isa<EnumDecl>(ND); | 
|  | 1054 | } | 
|  | 1055 |  | 
|  | 1056 | /// \brief Determines whether the given declaration is a class or struct. | 
|  | 1057 | bool ResultBuilder::IsClassOrStruct(NamedDecl *ND) const { | 
|  | 1058 | // Allow us to find class templates, too. | 
|  | 1059 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) | 
|  | 1060 | ND = ClassTemplate->getTemplatedDecl(); | 
|  | 1061 |  | 
|  | 1062 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) | 
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1063 | return RD->getTagKind() == TTK_Class || | 
|  | 1064 | RD->getTagKind() == TTK_Struct; | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1065 |  | 
|  | 1066 | return false; | 
|  | 1067 | } | 
|  | 1068 |  | 
|  | 1069 | /// \brief Determines whether the given declaration is a union. | 
|  | 1070 | bool ResultBuilder::IsUnion(NamedDecl *ND) const { | 
|  | 1071 | // Allow us to find class templates, too. | 
|  | 1072 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) | 
|  | 1073 | ND = ClassTemplate->getTemplatedDecl(); | 
|  | 1074 |  | 
|  | 1075 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) | 
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1076 | return RD->getTagKind() == TTK_Union; | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1077 |  | 
|  | 1078 | return false; | 
|  | 1079 | } | 
|  | 1080 |  | 
|  | 1081 | /// \brief Determines whether the given declaration is a namespace. | 
|  | 1082 | bool ResultBuilder::IsNamespace(NamedDecl *ND) const { | 
|  | 1083 | return isa<NamespaceDecl>(ND); | 
|  | 1084 | } | 
|  | 1085 |  | 
|  | 1086 | /// \brief Determines whether the given declaration is a namespace or | 
|  | 1087 | /// namespace alias. | 
|  | 1088 | bool ResultBuilder::IsNamespaceOrAlias(NamedDecl *ND) const { | 
|  | 1089 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); | 
|  | 1090 | } | 
|  | 1091 |  | 
| Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1092 | /// \brief Determines whether the given declaration is a type. | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1093 | bool ResultBuilder::IsType(NamedDecl *ND) const { | 
| Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1094 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND)) | 
|  | 1095 | ND = Using->getTargetDecl(); | 
|  | 1096 |  | 
|  | 1097 | return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1098 | } | 
|  | 1099 |  | 
| Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1100 | /// \brief Determines which members of a class should be visible via | 
|  | 1101 | /// "." or "->".  Only value declarations, nested name specifiers, and | 
|  | 1102 | /// using declarations thereof should show up. | 
| Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1103 | bool ResultBuilder::IsMember(NamedDecl *ND) const { | 
| Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1104 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND)) | 
|  | 1105 | ND = Using->getTargetDecl(); | 
|  | 1106 |  | 
| Douglas Gregor | 7078839 | 2009-12-11 18:14:22 +0000 | [diff] [blame] | 1107 | return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) || | 
|  | 1108 | isa<ObjCPropertyDecl>(ND); | 
| Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1109 | } | 
|  | 1110 |  | 
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1111 | static bool isObjCReceiverType(ASTContext &C, QualType T) { | 
|  | 1112 | T = C.getCanonicalType(T); | 
|  | 1113 | switch (T->getTypeClass()) { | 
|  | 1114 | case Type::ObjCObject: | 
|  | 1115 | case Type::ObjCInterface: | 
|  | 1116 | case Type::ObjCObjectPointer: | 
|  | 1117 | return true; | 
|  | 1118 |  | 
|  | 1119 | case Type::Builtin: | 
|  | 1120 | switch (cast<BuiltinType>(T)->getKind()) { | 
|  | 1121 | case BuiltinType::ObjCId: | 
|  | 1122 | case BuiltinType::ObjCClass: | 
|  | 1123 | case BuiltinType::ObjCSel: | 
|  | 1124 | return true; | 
|  | 1125 |  | 
|  | 1126 | default: | 
|  | 1127 | break; | 
|  | 1128 | } | 
|  | 1129 | return false; | 
|  | 1130 |  | 
|  | 1131 | default: | 
|  | 1132 | break; | 
|  | 1133 | } | 
|  | 1134 |  | 
|  | 1135 | if (!C.getLangOptions().CPlusPlus) | 
|  | 1136 | return false; | 
|  | 1137 |  | 
|  | 1138 | // FIXME: We could perform more analysis here to determine whether a | 
|  | 1139 | // particular class type has any conversions to Objective-C types. For now, | 
|  | 1140 | // just accept all class types. | 
|  | 1141 | return T->isDependentType() || T->isRecordType(); | 
|  | 1142 | } | 
|  | 1143 |  | 
|  | 1144 | bool ResultBuilder::IsObjCMessageReceiver(NamedDecl *ND) const { | 
|  | 1145 | QualType T = getDeclUsageType(SemaRef.Context, ND); | 
|  | 1146 | if (T.isNull()) | 
|  | 1147 | return false; | 
|  | 1148 |  | 
|  | 1149 | T = SemaRef.Context.getBaseElementType(T); | 
|  | 1150 | return isObjCReceiverType(SemaRef.Context, T); | 
|  | 1151 | } | 
|  | 1152 |  | 
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1153 | bool ResultBuilder::IsObjCCollection(NamedDecl *ND) const { | 
|  | 1154 | if ((SemaRef.getLangOptions().CPlusPlus && !IsOrdinaryName(ND)) || | 
|  | 1155 | (!SemaRef.getLangOptions().CPlusPlus && !IsOrdinaryNonTypeName(ND))) | 
|  | 1156 | return false; | 
|  | 1157 |  | 
|  | 1158 | QualType T = getDeclUsageType(SemaRef.Context, ND); | 
|  | 1159 | if (T.isNull()) | 
|  | 1160 | return false; | 
|  | 1161 |  | 
|  | 1162 | T = SemaRef.Context.getBaseElementType(T); | 
|  | 1163 | return T->isObjCObjectType() || T->isObjCObjectPointerType() || | 
|  | 1164 | T->isObjCIdType() || | 
|  | 1165 | (SemaRef.getLangOptions().CPlusPlus && T->isRecordType()); | 
|  | 1166 | } | 
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1167 |  | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1168 | bool ResultBuilder::IsImpossibleToSatisfy(NamedDecl *ND) const { | 
|  | 1169 | return false; | 
|  | 1170 | } | 
|  | 1171 |  | 
| Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1172 | /// \rief Determines whether the given declaration is an Objective-C | 
|  | 1173 | /// instance variable. | 
|  | 1174 | bool ResultBuilder::IsObjCIvar(NamedDecl *ND) const { | 
|  | 1175 | return isa<ObjCIvarDecl>(ND); | 
|  | 1176 | } | 
|  | 1177 |  | 
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1178 | namespace { | 
|  | 1179 | /// \brief Visible declaration consumer that adds a code-completion result | 
|  | 1180 | /// for each visible declaration. | 
|  | 1181 | class CodeCompletionDeclConsumer : public VisibleDeclConsumer { | 
|  | 1182 | ResultBuilder &Results; | 
|  | 1183 | DeclContext *CurContext; | 
|  | 1184 |  | 
|  | 1185 | public: | 
|  | 1186 | CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext) | 
|  | 1187 | : Results(Results), CurContext(CurContext) { } | 
|  | 1188 |  | 
| Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 1189 | virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, bool InBaseClass) { | 
|  | 1190 | Results.AddResult(ND, CurContext, Hiding, InBaseClass); | 
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1191 | } | 
|  | 1192 | }; | 
|  | 1193 | } | 
|  | 1194 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1195 | /// \brief Add type specifiers for the current language as keyword results. | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1196 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1197 | ResultBuilder &Results) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1198 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1199 | Results.AddResult(Result("short", CCP_Type)); | 
|  | 1200 | Results.AddResult(Result("long", CCP_Type)); | 
|  | 1201 | Results.AddResult(Result("signed", CCP_Type)); | 
|  | 1202 | Results.AddResult(Result("unsigned", CCP_Type)); | 
|  | 1203 | Results.AddResult(Result("void", CCP_Type)); | 
|  | 1204 | Results.AddResult(Result("char", CCP_Type)); | 
|  | 1205 | Results.AddResult(Result("int", CCP_Type)); | 
|  | 1206 | Results.AddResult(Result("float", CCP_Type)); | 
|  | 1207 | Results.AddResult(Result("double", CCP_Type)); | 
|  | 1208 | Results.AddResult(Result("enum", CCP_Type)); | 
|  | 1209 | Results.AddResult(Result("struct", CCP_Type)); | 
|  | 1210 | Results.AddResult(Result("union", CCP_Type)); | 
|  | 1211 | Results.AddResult(Result("const", CCP_Type)); | 
|  | 1212 | Results.AddResult(Result("volatile", CCP_Type)); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1213 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1214 | if (LangOpts.C99) { | 
|  | 1215 | // C99-specific | 
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1216 | Results.AddResult(Result("_Complex", CCP_Type)); | 
|  | 1217 | Results.AddResult(Result("_Imaginary", CCP_Type)); | 
|  | 1218 | Results.AddResult(Result("_Bool", CCP_Type)); | 
|  | 1219 | Results.AddResult(Result("restrict", CCP_Type)); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1220 | } | 
|  | 1221 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1222 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1223 | if (LangOpts.CPlusPlus) { | 
|  | 1224 | // C++-specific | 
| Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 1225 | Results.AddResult(Result("bool", CCP_Type + | 
|  | 1226 | (LangOpts.ObjC1? CCD_bool_in_ObjC : 0))); | 
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1227 | Results.AddResult(Result("class", CCP_Type)); | 
|  | 1228 | Results.AddResult(Result("wchar_t", CCP_Type)); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1229 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1230 | // typename qualified-id | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1231 | Builder.AddTypedTextChunk("typename"); | 
|  | 1232 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1233 | Builder.AddPlaceholderChunk("qualifier"); | 
|  | 1234 | Builder.AddTextChunk("::"); | 
|  | 1235 | Builder.AddPlaceholderChunk("name"); | 
|  | 1236 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1237 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1238 | if (LangOpts.CPlusPlus0x) { | 
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1239 | Results.AddResult(Result("auto", CCP_Type)); | 
|  | 1240 | Results.AddResult(Result("char16_t", CCP_Type)); | 
|  | 1241 | Results.AddResult(Result("char32_t", CCP_Type)); | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1242 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1243 | Builder.AddTypedTextChunk("decltype"); | 
|  | 1244 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 1245 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1246 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1247 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1248 | } | 
|  | 1249 | } | 
|  | 1250 |  | 
|  | 1251 | // GNU extensions | 
|  | 1252 | if (LangOpts.GNUMode) { | 
|  | 1253 | // FIXME: Enable when we actually support decimal floating point. | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1254 | //    Results.AddResult(Result("_Decimal32")); | 
|  | 1255 | //    Results.AddResult(Result("_Decimal64")); | 
|  | 1256 | //    Results.AddResult(Result("_Decimal128")); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1257 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1258 | Builder.AddTypedTextChunk("typeof"); | 
|  | 1259 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1260 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1261 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1262 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1263 | Builder.AddTypedTextChunk("typeof"); | 
|  | 1264 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 1265 | Builder.AddPlaceholderChunk("type"); | 
|  | 1266 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1267 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1268 | } | 
|  | 1269 | } | 
|  | 1270 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1271 | static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC, | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1272 | const LangOptions &LangOpts, | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1273 | ResultBuilder &Results) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1274 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1275 | // Note: we don't suggest either "auto" or "register", because both | 
|  | 1276 | // are pointless as storage specifiers. Elsewhere, we suggest "auto" | 
|  | 1277 | // in C++0x as a type specifier. | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1278 | Results.AddResult(Result("extern")); | 
|  | 1279 | Results.AddResult(Result("static")); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1280 | } | 
|  | 1281 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1282 | static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC, | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1283 | const LangOptions &LangOpts, | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1284 | ResultBuilder &Results) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1285 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1286 | switch (CCC) { | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1287 | case Sema::PCC_Class: | 
|  | 1288 | case Sema::PCC_MemberTemplate: | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1289 | if (LangOpts.CPlusPlus) { | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1290 | Results.AddResult(Result("explicit")); | 
|  | 1291 | Results.AddResult(Result("friend")); | 
|  | 1292 | Results.AddResult(Result("mutable")); | 
|  | 1293 | Results.AddResult(Result("virtual")); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1294 | } | 
|  | 1295 | // Fall through | 
|  | 1296 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1297 | case Sema::PCC_ObjCInterface: | 
|  | 1298 | case Sema::PCC_ObjCImplementation: | 
|  | 1299 | case Sema::PCC_Namespace: | 
|  | 1300 | case Sema::PCC_Template: | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1301 | if (LangOpts.CPlusPlus || LangOpts.C99) | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1302 | Results.AddResult(Result("inline")); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1303 | break; | 
|  | 1304 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1305 | case Sema::PCC_ObjCInstanceVariableList: | 
|  | 1306 | case Sema::PCC_Expression: | 
|  | 1307 | case Sema::PCC_Statement: | 
|  | 1308 | case Sema::PCC_ForInit: | 
|  | 1309 | case Sema::PCC_Condition: | 
|  | 1310 | case Sema::PCC_RecoveryInFunction: | 
|  | 1311 | case Sema::PCC_Type: | 
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1312 | case Sema::PCC_ParenthesizedExpression: | 
| Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1313 | case Sema::PCC_LocalDeclarationSpecifiers: | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1314 | break; | 
|  | 1315 | } | 
|  | 1316 | } | 
|  | 1317 |  | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1318 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); | 
|  | 1319 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); | 
|  | 1320 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, | 
| Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1321 | ResultBuilder &Results, | 
|  | 1322 | bool NeedAt); | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1323 | static void AddObjCImplementationResults(const LangOptions &LangOpts, | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1324 | ResultBuilder &Results, | 
|  | 1325 | bool NeedAt); | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1326 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1327 | ResultBuilder &Results, | 
|  | 1328 | bool NeedAt); | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1329 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1330 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1331 | static void AddTypedefResult(ResultBuilder &Results) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1332 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
|  | 1333 | Builder.AddTypedTextChunk("typedef"); | 
|  | 1334 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1335 | Builder.AddPlaceholderChunk("type"); | 
|  | 1336 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1337 | Builder.AddPlaceholderChunk("name"); | 
|  | 1338 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1339 | } | 
|  | 1340 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1341 | static bool WantTypesInContext(Sema::ParserCompletionContext CCC, | 
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1342 | const LangOptions &LangOpts) { | 
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1343 | switch (CCC) { | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1344 | case Sema::PCC_Namespace: | 
|  | 1345 | case Sema::PCC_Class: | 
|  | 1346 | case Sema::PCC_ObjCInstanceVariableList: | 
|  | 1347 | case Sema::PCC_Template: | 
|  | 1348 | case Sema::PCC_MemberTemplate: | 
|  | 1349 | case Sema::PCC_Statement: | 
|  | 1350 | case Sema::PCC_RecoveryInFunction: | 
|  | 1351 | case Sema::PCC_Type: | 
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1352 | case Sema::PCC_ParenthesizedExpression: | 
| Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1353 | case Sema::PCC_LocalDeclarationSpecifiers: | 
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1354 | return true; | 
|  | 1355 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1356 | case Sema::PCC_Expression: | 
|  | 1357 | case Sema::PCC_Condition: | 
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1358 | return LangOpts.CPlusPlus; | 
|  | 1359 |  | 
|  | 1360 | case Sema::PCC_ObjCInterface: | 
|  | 1361 | case Sema::PCC_ObjCImplementation: | 
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1362 | return false; | 
|  | 1363 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1364 | case Sema::PCC_ForInit: | 
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1365 | return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99; | 
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1366 | } | 
|  | 1367 |  | 
|  | 1368 | return false; | 
|  | 1369 | } | 
|  | 1370 |  | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1371 | /// \brief Add language constructs that show up for "ordinary" names. | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1372 | static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1373 | Scope *S, | 
|  | 1374 | Sema &SemaRef, | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1375 | ResultBuilder &Results) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1376 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
|  | 1377 |  | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1378 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1379 | switch (CCC) { | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1380 | case Sema::PCC_Namespace: | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1381 | if (SemaRef.getLangOptions().CPlusPlus) { | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1382 | if (Results.includeCodePatterns()) { | 
|  | 1383 | // namespace <identifier> { declarations } | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1384 | Builder.AddTypedTextChunk("namespace"); | 
|  | 1385 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1386 | Builder.AddPlaceholderChunk("identifier"); | 
|  | 1387 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | 
|  | 1388 | Builder.AddPlaceholderChunk("declarations"); | 
|  | 1389 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | 
|  | 1390 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | 
|  | 1391 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1392 | } | 
|  | 1393 |  | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1394 | // namespace identifier = identifier ; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1395 | Builder.AddTypedTextChunk("namespace"); | 
|  | 1396 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1397 | Builder.AddPlaceholderChunk("name"); | 
|  | 1398 | Builder.AddChunk(CodeCompletionString::CK_Equal); | 
|  | 1399 | Builder.AddPlaceholderChunk("namespace"); | 
|  | 1400 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1401 |  | 
|  | 1402 | // Using directives | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1403 | Builder.AddTypedTextChunk("using"); | 
|  | 1404 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1405 | Builder.AddTextChunk("namespace"); | 
|  | 1406 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1407 | Builder.AddPlaceholderChunk("identifier"); | 
|  | 1408 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1409 |  | 
|  | 1410 | // asm(string-literal) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1411 | Builder.AddTypedTextChunk("asm"); | 
|  | 1412 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 1413 | Builder.AddPlaceholderChunk("string-literal"); | 
|  | 1414 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1415 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1416 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1417 | if (Results.includeCodePatterns()) { | 
|  | 1418 | // Explicit template instantiation | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1419 | Builder.AddTypedTextChunk("template"); | 
|  | 1420 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1421 | Builder.AddPlaceholderChunk("declaration"); | 
|  | 1422 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1423 | } | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1424 | } | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1425 |  | 
|  | 1426 | if (SemaRef.getLangOptions().ObjC1) | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1427 | AddObjCTopLevelResults(Results, true); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1428 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1429 | AddTypedefResult(Results); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1430 | // Fall through | 
|  | 1431 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1432 | case Sema::PCC_Class: | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1433 | if (SemaRef.getLangOptions().CPlusPlus) { | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1434 | // Using declaration | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1435 | Builder.AddTypedTextChunk("using"); | 
|  | 1436 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1437 | Builder.AddPlaceholderChunk("qualifier"); | 
|  | 1438 | Builder.AddTextChunk("::"); | 
|  | 1439 | Builder.AddPlaceholderChunk("name"); | 
|  | 1440 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1441 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1442 | // using typename qualifier::name (only in a dependent context) | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1443 | if (SemaRef.CurContext->isDependentContext()) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1444 | Builder.AddTypedTextChunk("using"); | 
|  | 1445 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1446 | Builder.AddTextChunk("typename"); | 
|  | 1447 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1448 | Builder.AddPlaceholderChunk("qualifier"); | 
|  | 1449 | Builder.AddTextChunk("::"); | 
|  | 1450 | Builder.AddPlaceholderChunk("name"); | 
|  | 1451 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1452 | } | 
|  | 1453 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1454 | if (CCC == Sema::PCC_Class) { | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1455 | AddTypedefResult(Results); | 
|  | 1456 |  | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1457 | // public: | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1458 | Builder.AddTypedTextChunk("public"); | 
|  | 1459 | Builder.AddChunk(CodeCompletionString::CK_Colon); | 
|  | 1460 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1461 |  | 
|  | 1462 | // protected: | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1463 | Builder.AddTypedTextChunk("protected"); | 
|  | 1464 | Builder.AddChunk(CodeCompletionString::CK_Colon); | 
|  | 1465 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1466 |  | 
|  | 1467 | // private: | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1468 | Builder.AddTypedTextChunk("private"); | 
|  | 1469 | Builder.AddChunk(CodeCompletionString::CK_Colon); | 
|  | 1470 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1471 | } | 
|  | 1472 | } | 
|  | 1473 | // Fall through | 
|  | 1474 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1475 | case Sema::PCC_Template: | 
|  | 1476 | case Sema::PCC_MemberTemplate: | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1477 | if (SemaRef.getLangOptions().CPlusPlus && Results.includeCodePatterns()) { | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1478 | // template < parameters > | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1479 | Builder.AddTypedTextChunk("template"); | 
|  | 1480 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); | 
|  | 1481 | Builder.AddPlaceholderChunk("parameters"); | 
|  | 1482 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); | 
|  | 1483 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1484 | } | 
|  | 1485 |  | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1486 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); | 
|  | 1487 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1488 | break; | 
|  | 1489 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1490 | case Sema::PCC_ObjCInterface: | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1491 | AddObjCInterfaceResults(SemaRef.getLangOptions(), Results, true); | 
|  | 1492 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); | 
|  | 1493 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1494 | break; | 
|  | 1495 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1496 | case Sema::PCC_ObjCImplementation: | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1497 | AddObjCImplementationResults(SemaRef.getLangOptions(), Results, true); | 
|  | 1498 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); | 
|  | 1499 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1500 | break; | 
|  | 1501 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1502 | case Sema::PCC_ObjCInstanceVariableList: | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1503 | AddObjCVisibilityResults(SemaRef.getLangOptions(), Results, true); | 
| Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1504 | break; | 
|  | 1505 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1506 | case Sema::PCC_RecoveryInFunction: | 
|  | 1507 | case Sema::PCC_Statement: { | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1508 | AddTypedefResult(Results); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1509 |  | 
| Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1510 | if (SemaRef.getLangOptions().CPlusPlus && Results.includeCodePatterns() && | 
|  | 1511 | SemaRef.getLangOptions().CXXExceptions) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1512 | Builder.AddTypedTextChunk("try"); | 
|  | 1513 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | 
|  | 1514 | Builder.AddPlaceholderChunk("statements"); | 
|  | 1515 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | 
|  | 1516 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | 
|  | 1517 | Builder.AddTextChunk("catch"); | 
|  | 1518 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 1519 | Builder.AddPlaceholderChunk("declaration"); | 
|  | 1520 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1521 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | 
|  | 1522 | Builder.AddPlaceholderChunk("statements"); | 
|  | 1523 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | 
|  | 1524 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | 
|  | 1525 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1526 | } | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1527 | if (SemaRef.getLangOptions().ObjC1) | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1528 | AddObjCStatementResults(Results, true); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1529 |  | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1530 | if (Results.includeCodePatterns()) { | 
|  | 1531 | // if (condition) { statements } | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1532 | Builder.AddTypedTextChunk("if"); | 
|  | 1533 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1534 | if (SemaRef.getLangOptions().CPlusPlus) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1535 | Builder.AddPlaceholderChunk("condition"); | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1536 | else | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1537 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1538 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1539 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | 
|  | 1540 | Builder.AddPlaceholderChunk("statements"); | 
|  | 1541 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | 
|  | 1542 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | 
|  | 1543 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1544 |  | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1545 | // switch (condition) { } | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1546 | Builder.AddTypedTextChunk("switch"); | 
|  | 1547 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1548 | if (SemaRef.getLangOptions().CPlusPlus) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1549 | Builder.AddPlaceholderChunk("condition"); | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1550 | else | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1551 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1552 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1553 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | 
|  | 1554 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | 
|  | 1555 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | 
|  | 1556 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1557 | } | 
|  | 1558 |  | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1559 | // Switch-specific statements. | 
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 1560 | if (!SemaRef.getCurFunction()->SwitchStack.empty()) { | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1561 | // case expression: | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1562 | Builder.AddTypedTextChunk("case"); | 
|  | 1563 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1564 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1565 | Builder.AddChunk(CodeCompletionString::CK_Colon); | 
|  | 1566 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1567 |  | 
|  | 1568 | // default: | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1569 | Builder.AddTypedTextChunk("default"); | 
|  | 1570 | Builder.AddChunk(CodeCompletionString::CK_Colon); | 
|  | 1571 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1572 | } | 
|  | 1573 |  | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1574 | if (Results.includeCodePatterns()) { | 
|  | 1575 | /// while (condition) { statements } | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1576 | Builder.AddTypedTextChunk("while"); | 
|  | 1577 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1578 | if (SemaRef.getLangOptions().CPlusPlus) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1579 | Builder.AddPlaceholderChunk("condition"); | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1580 | else | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1581 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1582 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1583 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | 
|  | 1584 | Builder.AddPlaceholderChunk("statements"); | 
|  | 1585 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | 
|  | 1586 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | 
|  | 1587 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1588 |  | 
|  | 1589 | // do { statements } while ( expression ); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1590 | Builder.AddTypedTextChunk("do"); | 
|  | 1591 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | 
|  | 1592 | Builder.AddPlaceholderChunk("statements"); | 
|  | 1593 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | 
|  | 1594 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | 
|  | 1595 | Builder.AddTextChunk("while"); | 
|  | 1596 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 1597 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1598 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1599 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1600 |  | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1601 | // for ( for-init-statement ; condition ; expression ) { statements } | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1602 | Builder.AddTypedTextChunk("for"); | 
|  | 1603 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1604 | if (SemaRef.getLangOptions().CPlusPlus || SemaRef.getLangOptions().C99) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1605 | Builder.AddPlaceholderChunk("init-statement"); | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1606 | else | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1607 | Builder.AddPlaceholderChunk("init-expression"); | 
|  | 1608 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); | 
|  | 1609 | Builder.AddPlaceholderChunk("condition"); | 
|  | 1610 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); | 
|  | 1611 | Builder.AddPlaceholderChunk("inc-expression"); | 
|  | 1612 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1613 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | 
|  | 1614 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | 
|  | 1615 | Builder.AddPlaceholderChunk("statements"); | 
|  | 1616 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | 
|  | 1617 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | 
|  | 1618 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1619 | } | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1620 |  | 
|  | 1621 | if (S->getContinueParent()) { | 
|  | 1622 | // continue ; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1623 | Builder.AddTypedTextChunk("continue"); | 
|  | 1624 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1625 | } | 
|  | 1626 |  | 
|  | 1627 | if (S->getBreakParent()) { | 
|  | 1628 | // break ; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1629 | Builder.AddTypedTextChunk("break"); | 
|  | 1630 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1631 | } | 
|  | 1632 |  | 
|  | 1633 | // "return expression ;" or "return ;", depending on whether we | 
|  | 1634 | // know the function is void or not. | 
|  | 1635 | bool isVoid = false; | 
|  | 1636 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) | 
|  | 1637 | isVoid = Function->getResultType()->isVoidType(); | 
|  | 1638 | else if (ObjCMethodDecl *Method | 
|  | 1639 | = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) | 
|  | 1640 | isVoid = Method->getResultType()->isVoidType(); | 
| Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1641 | else if (SemaRef.getCurBlock() && | 
|  | 1642 | !SemaRef.getCurBlock()->ReturnType.isNull()) | 
|  | 1643 | isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType(); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1644 | Builder.AddTypedTextChunk("return"); | 
| Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1645 | if (!isVoid) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1646 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1647 | Builder.AddPlaceholderChunk("expression"); | 
| Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1648 | } | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1649 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1650 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1651 | // goto identifier ; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1652 | Builder.AddTypedTextChunk("goto"); | 
|  | 1653 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1654 | Builder.AddPlaceholderChunk("label"); | 
|  | 1655 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1656 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1657 | // Using directives | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1658 | Builder.AddTypedTextChunk("using"); | 
|  | 1659 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1660 | Builder.AddTextChunk("namespace"); | 
|  | 1661 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1662 | Builder.AddPlaceholderChunk("identifier"); | 
|  | 1663 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1664 | } | 
|  | 1665 |  | 
|  | 1666 | // Fall through (for statement expressions). | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1667 | case Sema::PCC_ForInit: | 
|  | 1668 | case Sema::PCC_Condition: | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1669 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1670 | // Fall through: conditions and statements can have expressions. | 
|  | 1671 |  | 
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1672 | case Sema::PCC_ParenthesizedExpression: | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1673 | if (SemaRef.getLangOptions().ObjCAutoRefCount && | 
|  | 1674 | CCC == Sema::PCC_ParenthesizedExpression) { | 
|  | 1675 | // (__bridge <type>)<expression> | 
|  | 1676 | Builder.AddTypedTextChunk("__bridge"); | 
|  | 1677 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1678 | Builder.AddPlaceholderChunk("type"); | 
|  | 1679 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1680 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1681 | Results.AddResult(Result(Builder.TakeString())); | 
|  | 1682 |  | 
|  | 1683 | // (__bridge_transfer <Objective-C type>)<expression> | 
|  | 1684 | Builder.AddTypedTextChunk("__bridge_transfer"); | 
|  | 1685 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1686 | Builder.AddPlaceholderChunk("Objective-C type"); | 
|  | 1687 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1688 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1689 | Results.AddResult(Result(Builder.TakeString())); | 
|  | 1690 |  | 
|  | 1691 | // (__bridge_retained <CF type>)<expression> | 
|  | 1692 | Builder.AddTypedTextChunk("__bridge_retained"); | 
|  | 1693 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1694 | Builder.AddPlaceholderChunk("CF type"); | 
|  | 1695 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1696 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1697 | Results.AddResult(Result(Builder.TakeString())); | 
|  | 1698 | } | 
|  | 1699 | // Fall through | 
|  | 1700 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1701 | case Sema::PCC_Expression: { | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1702 | if (SemaRef.getLangOptions().CPlusPlus) { | 
|  | 1703 | // 'this', if we're in a non-static member function. | 
|  | 1704 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) | 
|  | 1705 | if (!Method->isStatic()) | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1706 | Results.AddResult(Result("this")); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1707 |  | 
|  | 1708 | // true, false | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1709 | Results.AddResult(Result("true")); | 
|  | 1710 | Results.AddResult(Result("false")); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1711 |  | 
| Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1712 | if (SemaRef.getLangOptions().RTTI) { | 
|  | 1713 | // dynamic_cast < type-id > ( expression ) | 
|  | 1714 | Builder.AddTypedTextChunk("dynamic_cast"); | 
|  | 1715 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); | 
|  | 1716 | Builder.AddPlaceholderChunk("type"); | 
|  | 1717 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); | 
|  | 1718 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 1719 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1720 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1721 | Results.AddResult(Result(Builder.TakeString())); | 
|  | 1722 | } | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1723 |  | 
|  | 1724 | // static_cast < type-id > ( expression ) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1725 | Builder.AddTypedTextChunk("static_cast"); | 
|  | 1726 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); | 
|  | 1727 | Builder.AddPlaceholderChunk("type"); | 
|  | 1728 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); | 
|  | 1729 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 1730 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1731 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1732 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1733 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1734 | // reinterpret_cast < type-id > ( expression ) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1735 | Builder.AddTypedTextChunk("reinterpret_cast"); | 
|  | 1736 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); | 
|  | 1737 | Builder.AddPlaceholderChunk("type"); | 
|  | 1738 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); | 
|  | 1739 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 1740 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1741 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1742 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1743 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1744 | // const_cast < type-id > ( expression ) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1745 | Builder.AddTypedTextChunk("const_cast"); | 
|  | 1746 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); | 
|  | 1747 | Builder.AddPlaceholderChunk("type"); | 
|  | 1748 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); | 
|  | 1749 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 1750 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1751 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1752 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1753 |  | 
| Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1754 | if (SemaRef.getLangOptions().RTTI) { | 
|  | 1755 | // typeid ( expression-or-type ) | 
|  | 1756 | Builder.AddTypedTextChunk("typeid"); | 
|  | 1757 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 1758 | Builder.AddPlaceholderChunk("expression-or-type"); | 
|  | 1759 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1760 | Results.AddResult(Result(Builder.TakeString())); | 
|  | 1761 | } | 
|  | 1762 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1763 | // new T ( ... ) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1764 | Builder.AddTypedTextChunk("new"); | 
|  | 1765 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1766 | Builder.AddPlaceholderChunk("type"); | 
|  | 1767 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 1768 | Builder.AddPlaceholderChunk("expressions"); | 
|  | 1769 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1770 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1771 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1772 | // new T [ ] ( ... ) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1773 | Builder.AddTypedTextChunk("new"); | 
|  | 1774 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1775 | Builder.AddPlaceholderChunk("type"); | 
|  | 1776 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); | 
|  | 1777 | Builder.AddPlaceholderChunk("size"); | 
|  | 1778 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); | 
|  | 1779 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 1780 | Builder.AddPlaceholderChunk("expressions"); | 
|  | 1781 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1782 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1783 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1784 | // delete expression | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1785 | Builder.AddTypedTextChunk("delete"); | 
|  | 1786 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1787 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1788 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1789 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1790 | // delete [] expression | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1791 | Builder.AddTypedTextChunk("delete"); | 
|  | 1792 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1793 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); | 
|  | 1794 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); | 
|  | 1795 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1796 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1797 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1798 |  | 
| Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1799 | if (SemaRef.getLangOptions().CXXExceptions) { | 
|  | 1800 | // throw expression | 
|  | 1801 | Builder.AddTypedTextChunk("throw"); | 
|  | 1802 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 1803 | Builder.AddPlaceholderChunk("expression"); | 
|  | 1804 | Results.AddResult(Result(Builder.TakeString())); | 
|  | 1805 | } | 
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1806 |  | 
|  | 1807 | // FIXME: Rethrow? | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1808 | } | 
|  | 1809 |  | 
|  | 1810 | if (SemaRef.getLangOptions().ObjC1) { | 
|  | 1811 | // Add "super", if we're in an Objective-C class with a superclass. | 
| Ted Kremenek | 305a0a7 | 2010-05-31 21:43:10 +0000 | [diff] [blame] | 1812 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { | 
|  | 1813 | // The interface can be NULL. | 
|  | 1814 | if (ObjCInterfaceDecl *ID = Method->getClassInterface()) | 
|  | 1815 | if (ID->getSuperClass()) | 
|  | 1816 | Results.AddResult(Result("super")); | 
|  | 1817 | } | 
|  | 1818 |  | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1819 | AddObjCExpressionResults(Results, true); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1820 | } | 
|  | 1821 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1822 | // sizeof expression | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1823 | Builder.AddTypedTextChunk("sizeof"); | 
|  | 1824 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 1825 | Builder.AddPlaceholderChunk("expression-or-type"); | 
|  | 1826 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 1827 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1828 | break; | 
|  | 1829 | } | 
| Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1830 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1831 | case Sema::PCC_Type: | 
| Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1832 | case Sema::PCC_LocalDeclarationSpecifiers: | 
| Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1833 | break; | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1834 | } | 
|  | 1835 |  | 
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1836 | if (WantTypesInContext(CCC, SemaRef.getLangOptions())) | 
|  | 1837 | AddTypeSpecifierResults(SemaRef.getLangOptions(), Results); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1838 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1839 | if (SemaRef.getLangOptions().CPlusPlus && CCC != Sema::PCC_Type) | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1840 | Results.AddResult(Result("operator")); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1841 | } | 
|  | 1842 |  | 
| Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1843 | /// \brief Retrieve a printing policy suitable for code completion. | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 1844 | static PrintingPolicy getCompletionPrintingPolicy(Sema &S) { | 
|  | 1845 | PrintingPolicy Policy = S.getPrintingPolicy(); | 
| Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1846 | Policy.AnonymousTagLocations = false; | 
|  | 1847 | Policy.SuppressStrongLifetime = true; | 
|  | 1848 | return Policy; | 
|  | 1849 | } | 
|  | 1850 |  | 
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 1851 | /// \brief Retrieve the string representation of the given type as a string | 
|  | 1852 | /// that has the appropriate lifetime for code completion. | 
|  | 1853 | /// | 
|  | 1854 | /// This routine provides a fast path where we provide constant strings for | 
|  | 1855 | /// common type names. | 
| Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1856 | static const char *GetCompletionTypeString(QualType T, | 
|  | 1857 | ASTContext &Context, | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 1858 | const PrintingPolicy &Policy, | 
| Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1859 | CodeCompletionAllocator &Allocator) { | 
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 1860 | if (!T.getLocalQualifiers()) { | 
|  | 1861 | // Built-in type names are constant strings. | 
|  | 1862 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) | 
| Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 1863 | return BT->getName(Policy); | 
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 1864 |  | 
|  | 1865 | // Anonymous tag types are constant strings. | 
|  | 1866 | if (const TagType *TagT = dyn_cast<TagType>(T)) | 
|  | 1867 | if (TagDecl *Tag = TagT->getDecl()) | 
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1868 | if (!Tag->getIdentifier() && !Tag->getTypedefNameForAnonDecl()) { | 
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 1869 | switch (Tag->getTagKind()) { | 
|  | 1870 | case TTK_Struct: return "struct <anonymous>"; | 
|  | 1871 | case TTK_Class:  return "class <anonymous>"; | 
|  | 1872 | case TTK_Union:  return "union <anonymous>"; | 
|  | 1873 | case TTK_Enum:   return "enum <anonymous>"; | 
|  | 1874 | } | 
|  | 1875 | } | 
|  | 1876 | } | 
|  | 1877 |  | 
|  | 1878 | // Slow path: format the type as a string. | 
|  | 1879 | std::string Result; | 
|  | 1880 | T.getAsStringInternal(Result, Policy); | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 1881 | return Allocator.CopyString(Result); | 
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 1882 | } | 
|  | 1883 |  | 
| Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1884 | /// \brief If the given declaration has an associated type, add it as a result | 
|  | 1885 | /// type chunk. | 
|  | 1886 | static void AddResultTypeChunk(ASTContext &Context, | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 1887 | const PrintingPolicy &Policy, | 
| Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1888 | NamedDecl *ND, | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1889 | CodeCompletionBuilder &Result) { | 
| Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1890 | if (!ND) | 
|  | 1891 | return; | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1892 |  | 
|  | 1893 | // Skip constructors and conversion functions, which have their return types | 
|  | 1894 | // built into their names. | 
|  | 1895 | if (isa<CXXConstructorDecl>(ND) || isa<CXXConversionDecl>(ND)) | 
|  | 1896 | return; | 
|  | 1897 |  | 
| Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1898 | // Determine the type of the declaration (if it has a type). | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1899 | QualType T; | 
| Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1900 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) | 
|  | 1901 | T = Function->getResultType(); | 
|  | 1902 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) | 
|  | 1903 | T = Method->getResultType(); | 
|  | 1904 | else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) | 
|  | 1905 | T = FunTmpl->getTemplatedDecl()->getResultType(); | 
|  | 1906 | else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) | 
|  | 1907 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); | 
|  | 1908 | else if (isa<UnresolvedUsingValueDecl>(ND)) { | 
|  | 1909 | /* Do nothing: ignore unresolved using declarations*/ | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1910 | } else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND)) { | 
| Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1911 | T = Value->getType(); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1912 | } else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) | 
| Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1913 | T = Property->getType(); | 
|  | 1914 |  | 
|  | 1915 | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) | 
|  | 1916 | return; | 
|  | 1917 |  | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 1918 | Result.AddResultTypeChunk(GetCompletionTypeString(T, Context, Policy, | 
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 1919 | Result.getAllocator())); | 
| Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1920 | } | 
|  | 1921 |  | 
| Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1922 | static void MaybeAddSentinel(ASTContext &Context, NamedDecl *FunctionOrMethod, | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1923 | CodeCompletionBuilder &Result) { | 
| Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1924 | if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) | 
|  | 1925 | if (Sentinel->getSentinel() == 0) { | 
|  | 1926 | if (Context.getLangOptions().ObjC1 && | 
|  | 1927 | Context.Idents.get("nil").hasMacroDefinition()) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1928 | Result.AddTextChunk(", nil"); | 
| Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1929 | else if (Context.Idents.get("NULL").hasMacroDefinition()) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1930 | Result.AddTextChunk(", NULL"); | 
| Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1931 | else | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1932 | Result.AddTextChunk(", (void*)0"); | 
| Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1933 | } | 
|  | 1934 | } | 
|  | 1935 |  | 
| Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 1936 | static void appendWithSpace(std::string &Result, StringRef Text) { | 
|  | 1937 | if (!Result.empty()) | 
|  | 1938 | Result += ' '; | 
|  | 1939 | Result += Text.str(); | 
|  | 1940 | } | 
|  | 1941 | static std::string formatObjCParamQualifiers(unsigned ObjCQuals) { | 
|  | 1942 | std::string Result; | 
|  | 1943 | if (ObjCQuals & Decl::OBJC_TQ_In) | 
|  | 1944 | appendWithSpace(Result, "in"); | 
|  | 1945 | else if (ObjCQuals & Decl::OBJC_TQ_Inout) | 
|  | 1946 | appendWithSpace(Result, "inout"); | 
|  | 1947 | else if (ObjCQuals & Decl::OBJC_TQ_Out) | 
|  | 1948 | appendWithSpace(Result, "out"); | 
|  | 1949 | if (ObjCQuals & Decl::OBJC_TQ_Bycopy) | 
|  | 1950 | appendWithSpace(Result, "bycopy"); | 
|  | 1951 | else if (ObjCQuals & Decl::OBJC_TQ_Byref) | 
|  | 1952 | appendWithSpace(Result, "byref"); | 
|  | 1953 | if (ObjCQuals & Decl::OBJC_TQ_Oneway) | 
|  | 1954 | appendWithSpace(Result, "oneway"); | 
|  | 1955 | return Result; | 
|  | 1956 | } | 
|  | 1957 |  | 
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1958 | static std::string FormatFunctionParameter(ASTContext &Context, | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 1959 | const PrintingPolicy &Policy, | 
| Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 1960 | ParmVarDecl *Param, | 
|  | 1961 | bool SuppressName = false) { | 
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1962 | bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext()); | 
|  | 1963 | if (Param->getType()->isDependentType() || | 
|  | 1964 | !Param->getType()->isBlockPointerType()) { | 
|  | 1965 | // The argument for a dependent or non-block parameter is a placeholder | 
|  | 1966 | // containing that parameter's type. | 
|  | 1967 | std::string Result; | 
|  | 1968 |  | 
| Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 1969 | if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName) | 
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1970 | Result = Param->getIdentifier()->getName(); | 
|  | 1971 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1972 | Param->getType().getAsStringInternal(Result, Policy); | 
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1973 |  | 
|  | 1974 | if (ObjCMethodParam) { | 
| Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 1975 | Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier()) | 
|  | 1976 | + Result + ")"; | 
| Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 1977 | if (Param->getIdentifier() && !SuppressName) | 
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1978 | Result += Param->getIdentifier()->getName(); | 
|  | 1979 | } | 
|  | 1980 | return Result; | 
|  | 1981 | } | 
|  | 1982 |  | 
|  | 1983 | // The argument for a block pointer parameter is a block literal with | 
|  | 1984 | // the appropriate type. | 
| Douglas Gregor | 24bbc46 | 2011-02-15 22:37:09 +0000 | [diff] [blame] | 1985 | FunctionTypeLoc *Block = 0; | 
|  | 1986 | FunctionProtoTypeLoc *BlockProto = 0; | 
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1987 | TypeLoc TL; | 
|  | 1988 | if (TypeSourceInfo *TSInfo = Param->getTypeSourceInfo()) { | 
|  | 1989 | TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); | 
|  | 1990 | while (true) { | 
|  | 1991 | // Look through typedefs. | 
|  | 1992 | if (TypedefTypeLoc *TypedefTL = dyn_cast<TypedefTypeLoc>(&TL)) { | 
|  | 1993 | if (TypeSourceInfo *InnerTSInfo | 
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1994 | = TypedefTL->getTypedefNameDecl()->getTypeSourceInfo()) { | 
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1995 | TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); | 
|  | 1996 | continue; | 
|  | 1997 | } | 
|  | 1998 | } | 
|  | 1999 |  | 
|  | 2000 | // Look through qualified types | 
|  | 2001 | if (QualifiedTypeLoc *QualifiedTL = dyn_cast<QualifiedTypeLoc>(&TL)) { | 
|  | 2002 | TL = QualifiedTL->getUnqualifiedLoc(); | 
|  | 2003 | continue; | 
|  | 2004 | } | 
|  | 2005 |  | 
|  | 2006 | // Try to get the function prototype behind the block pointer type, | 
|  | 2007 | // then we're done. | 
|  | 2008 | if (BlockPointerTypeLoc *BlockPtr | 
|  | 2009 | = dyn_cast<BlockPointerTypeLoc>(&TL)) { | 
| Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 2010 | TL = BlockPtr->getPointeeLoc().IgnoreParens(); | 
| Douglas Gregor | 24bbc46 | 2011-02-15 22:37:09 +0000 | [diff] [blame] | 2011 | Block = dyn_cast<FunctionTypeLoc>(&TL); | 
|  | 2012 | BlockProto = dyn_cast<FunctionProtoTypeLoc>(&TL); | 
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2013 | } | 
|  | 2014 | break; | 
|  | 2015 | } | 
|  | 2016 | } | 
|  | 2017 |  | 
|  | 2018 | if (!Block) { | 
|  | 2019 | // We were unable to find a FunctionProtoTypeLoc with parameter names | 
|  | 2020 | // for the block; just use the parameter type as a placeholder. | 
|  | 2021 | std::string Result; | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2022 | Param->getType().getUnqualifiedType().getAsStringInternal(Result, Policy); | 
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2023 |  | 
|  | 2024 | if (ObjCMethodParam) { | 
| Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2025 | Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier()) | 
|  | 2026 | + Result + ")"; | 
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2027 | if (Param->getIdentifier()) | 
|  | 2028 | Result += Param->getIdentifier()->getName(); | 
|  | 2029 | } | 
|  | 2030 |  | 
|  | 2031 | return Result; | 
|  | 2032 | } | 
|  | 2033 |  | 
|  | 2034 | // We have the function prototype behind the block pointer type, as it was | 
|  | 2035 | // written in the source. | 
| Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2036 | std::string Result; | 
|  | 2037 | QualType ResultType = Block->getTypePtr()->getResultType(); | 
|  | 2038 | if (!ResultType->isVoidType()) | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2039 | ResultType.getAsStringInternal(Result, Policy); | 
| Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2040 |  | 
|  | 2041 | Result = '^' + Result; | 
| Douglas Gregor | 24bbc46 | 2011-02-15 22:37:09 +0000 | [diff] [blame] | 2042 | if (!BlockProto || Block->getNumArgs() == 0) { | 
|  | 2043 | if (BlockProto && BlockProto->getTypePtr()->isVariadic()) | 
| Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2044 | Result += "(...)"; | 
| Douglas Gregor | af25cfa | 2010-10-02 23:49:58 +0000 | [diff] [blame] | 2045 | else | 
|  | 2046 | Result += "(void)"; | 
| Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2047 | } else { | 
|  | 2048 | Result += "("; | 
|  | 2049 | for (unsigned I = 0, N = Block->getNumArgs(); I != N; ++I) { | 
|  | 2050 | if (I) | 
|  | 2051 | Result += ", "; | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2052 | Result += FormatFunctionParameter(Context, Policy, Block->getArg(I)); | 
| Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2053 |  | 
| Douglas Gregor | 24bbc46 | 2011-02-15 22:37:09 +0000 | [diff] [blame] | 2054 | if (I == N - 1 && BlockProto->getTypePtr()->isVariadic()) | 
| Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2055 | Result += ", ..."; | 
|  | 2056 | } | 
|  | 2057 | Result += ")"; | 
| Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2058 | } | 
| Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2059 |  | 
| Douglas Gregor | af25cfa | 2010-10-02 23:49:58 +0000 | [diff] [blame] | 2060 | if (Param->getIdentifier()) | 
|  | 2061 | Result += Param->getIdentifier()->getName(); | 
|  | 2062 |  | 
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2063 | return Result; | 
|  | 2064 | } | 
|  | 2065 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2066 | /// \brief Add function parameter chunks to the given code completion string. | 
|  | 2067 | static void AddFunctionParameterChunks(ASTContext &Context, | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2068 | const PrintingPolicy &Policy, | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2069 | FunctionDecl *Function, | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2070 | CodeCompletionBuilder &Result, | 
|  | 2071 | unsigned Start = 0, | 
|  | 2072 | bool InOptional = false) { | 
| Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2073 | typedef CodeCompletionString::Chunk Chunk; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2074 | bool FirstParameter = true; | 
| Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2075 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2076 | for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) { | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2077 | ParmVarDecl *Param = Function->getParamDecl(P); | 
|  | 2078 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2079 | if (Param->hasDefaultArg() && !InOptional) { | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2080 | // When we see an optional default argument, put that argument and | 
|  | 2081 | // the remaining default arguments into a new, optional string. | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2082 | CodeCompletionBuilder Opt(Result.getAllocator()); | 
|  | 2083 | if (!FirstParameter) | 
|  | 2084 | Opt.AddChunk(Chunk(CodeCompletionString::CK_Comma)); | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2085 | AddFunctionParameterChunks(Context, Policy, Function, Opt, P, true); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2086 | Result.AddOptionalChunk(Opt.TakeString()); | 
|  | 2087 | break; | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2088 | } | 
|  | 2089 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2090 | if (FirstParameter) | 
|  | 2091 | FirstParameter = false; | 
|  | 2092 | else | 
|  | 2093 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); | 
|  | 2094 |  | 
|  | 2095 | InOptional = false; | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2096 |  | 
|  | 2097 | // Format the placeholder string. | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2098 | std::string PlaceholderStr = FormatFunctionParameter(Context, Policy, | 
|  | 2099 | Param); | 
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2100 |  | 
| Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2101 | if (Function->isVariadic() && P == N - 1) | 
|  | 2102 | PlaceholderStr += ", ..."; | 
|  | 2103 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2104 | // Add the placeholder string. | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2105 | Result.AddPlaceholderChunk( | 
|  | 2106 | Result.getAllocator().CopyString(PlaceholderStr)); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2107 | } | 
| Douglas Gregor | ba44903 | 2009-09-22 21:42:17 +0000 | [diff] [blame] | 2108 |  | 
|  | 2109 | if (const FunctionProtoType *Proto | 
|  | 2110 | = Function->getType()->getAs<FunctionProtoType>()) | 
| Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2111 | if (Proto->isVariadic()) { | 
| Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2112 | if (Proto->getNumArgs() == 0) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2113 | Result.AddPlaceholderChunk("..."); | 
| Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2114 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2115 | MaybeAddSentinel(Context, Function, Result); | 
| Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2116 | } | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2117 | } | 
|  | 2118 |  | 
|  | 2119 | /// \brief Add template parameter chunks to the given code completion string. | 
|  | 2120 | static void AddTemplateParameterChunks(ASTContext &Context, | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2121 | const PrintingPolicy &Policy, | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2122 | TemplateDecl *Template, | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2123 | CodeCompletionBuilder &Result, | 
|  | 2124 | unsigned MaxParameters = 0, | 
|  | 2125 | unsigned Start = 0, | 
|  | 2126 | bool InDefaultArg = false) { | 
| Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2127 | typedef CodeCompletionString::Chunk Chunk; | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2128 | bool FirstParameter = true; | 
|  | 2129 |  | 
|  | 2130 | TemplateParameterList *Params = Template->getTemplateParameters(); | 
|  | 2131 | TemplateParameterList::iterator PEnd = Params->end(); | 
|  | 2132 | if (MaxParameters) | 
|  | 2133 | PEnd = Params->begin() + MaxParameters; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2134 | for (TemplateParameterList::iterator P = Params->begin() + Start; | 
|  | 2135 | P != PEnd; ++P) { | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2136 | bool HasDefaultArg = false; | 
|  | 2137 | std::string PlaceholderStr; | 
|  | 2138 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { | 
|  | 2139 | if (TTP->wasDeclaredWithTypename()) | 
|  | 2140 | PlaceholderStr = "typename"; | 
|  | 2141 | else | 
|  | 2142 | PlaceholderStr = "class"; | 
|  | 2143 |  | 
|  | 2144 | if (TTP->getIdentifier()) { | 
|  | 2145 | PlaceholderStr += ' '; | 
|  | 2146 | PlaceholderStr += TTP->getIdentifier()->getName(); | 
|  | 2147 | } | 
|  | 2148 |  | 
|  | 2149 | HasDefaultArg = TTP->hasDefaultArgument(); | 
|  | 2150 | } else if (NonTypeTemplateParmDecl *NTTP | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2151 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2152 | if (NTTP->getIdentifier()) | 
|  | 2153 | PlaceholderStr = NTTP->getIdentifier()->getName(); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2154 | NTTP->getType().getAsStringInternal(PlaceholderStr, Policy); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2155 | HasDefaultArg = NTTP->hasDefaultArgument(); | 
|  | 2156 | } else { | 
|  | 2157 | assert(isa<TemplateTemplateParmDecl>(*P)); | 
|  | 2158 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); | 
|  | 2159 |  | 
|  | 2160 | // Since putting the template argument list into the placeholder would | 
|  | 2161 | // be very, very long, we just use an abbreviation. | 
|  | 2162 | PlaceholderStr = "template<...> class"; | 
|  | 2163 | if (TTP->getIdentifier()) { | 
|  | 2164 | PlaceholderStr += ' '; | 
|  | 2165 | PlaceholderStr += TTP->getIdentifier()->getName(); | 
|  | 2166 | } | 
|  | 2167 |  | 
|  | 2168 | HasDefaultArg = TTP->hasDefaultArgument(); | 
|  | 2169 | } | 
|  | 2170 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2171 | if (HasDefaultArg && !InDefaultArg) { | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2172 | // When we see an optional default argument, put that argument and | 
|  | 2173 | // the remaining default arguments into a new, optional string. | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2174 | CodeCompletionBuilder Opt(Result.getAllocator()); | 
|  | 2175 | if (!FirstParameter) | 
|  | 2176 | Opt.AddChunk(Chunk(CodeCompletionString::CK_Comma)); | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2177 | AddTemplateParameterChunks(Context, Policy, Template, Opt, MaxParameters, | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2178 | P - Params->begin(), true); | 
|  | 2179 | Result.AddOptionalChunk(Opt.TakeString()); | 
|  | 2180 | break; | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2181 | } | 
|  | 2182 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2183 | InDefaultArg = false; | 
|  | 2184 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2185 | if (FirstParameter) | 
|  | 2186 | FirstParameter = false; | 
|  | 2187 | else | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2188 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2189 |  | 
|  | 2190 | // Add the placeholder string. | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2191 | Result.AddPlaceholderChunk( | 
|  | 2192 | Result.getAllocator().CopyString(PlaceholderStr)); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2193 | } | 
|  | 2194 | } | 
|  | 2195 |  | 
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2196 | /// \brief Add a qualifier to the given code-completion string, if the | 
|  | 2197 | /// provided nested-name-specifier is non-NULL. | 
| Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2198 | static void | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2199 | AddQualifierToCompletionString(CodeCompletionBuilder &Result, | 
| Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2200 | NestedNameSpecifier *Qualifier, | 
|  | 2201 | bool QualifierIsInformative, | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2202 | ASTContext &Context, | 
|  | 2203 | const PrintingPolicy &Policy) { | 
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2204 | if (!Qualifier) | 
|  | 2205 | return; | 
|  | 2206 |  | 
|  | 2207 | std::string PrintedNNS; | 
|  | 2208 | { | 
|  | 2209 | llvm::raw_string_ostream OS(PrintedNNS); | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2210 | Qualifier->print(OS, Policy); | 
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2211 | } | 
| Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2212 | if (QualifierIsInformative) | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2213 | Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS)); | 
| Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2214 | else | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2215 | Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS)); | 
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2216 | } | 
|  | 2217 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2218 | static void | 
|  | 2219 | AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result, | 
|  | 2220 | FunctionDecl *Function) { | 
| Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2221 | const FunctionProtoType *Proto | 
|  | 2222 | = Function->getType()->getAs<FunctionProtoType>(); | 
|  | 2223 | if (!Proto || !Proto->getTypeQuals()) | 
|  | 2224 | return; | 
|  | 2225 |  | 
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2226 | // FIXME: Add ref-qualifier! | 
|  | 2227 |  | 
|  | 2228 | // Handle single qualifiers without copying | 
|  | 2229 | if (Proto->getTypeQuals() == Qualifiers::Const) { | 
|  | 2230 | Result.AddInformativeChunk(" const"); | 
|  | 2231 | return; | 
|  | 2232 | } | 
|  | 2233 |  | 
|  | 2234 | if (Proto->getTypeQuals() == Qualifiers::Volatile) { | 
|  | 2235 | Result.AddInformativeChunk(" volatile"); | 
|  | 2236 | return; | 
|  | 2237 | } | 
|  | 2238 |  | 
|  | 2239 | if (Proto->getTypeQuals() == Qualifiers::Restrict) { | 
|  | 2240 | Result.AddInformativeChunk(" restrict"); | 
|  | 2241 | return; | 
|  | 2242 | } | 
|  | 2243 |  | 
|  | 2244 | // Handle multiple qualifiers. | 
| Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2245 | std::string QualsStr; | 
|  | 2246 | if (Proto->getTypeQuals() & Qualifiers::Const) | 
|  | 2247 | QualsStr += " const"; | 
|  | 2248 | if (Proto->getTypeQuals() & Qualifiers::Volatile) | 
|  | 2249 | QualsStr += " volatile"; | 
|  | 2250 | if (Proto->getTypeQuals() & Qualifiers::Restrict) | 
|  | 2251 | QualsStr += " restrict"; | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2252 | Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr)); | 
| Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2253 | } | 
|  | 2254 |  | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2255 | /// \brief Add the name of the given declaration | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2256 | static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy, | 
|  | 2257 | NamedDecl *ND, CodeCompletionBuilder &Result) { | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2258 | typedef CodeCompletionString::Chunk Chunk; | 
|  | 2259 |  | 
|  | 2260 | DeclarationName Name = ND->getDeclName(); | 
|  | 2261 | if (!Name) | 
|  | 2262 | return; | 
|  | 2263 |  | 
|  | 2264 | switch (Name.getNameKind()) { | 
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2265 | case DeclarationName::CXXOperatorName: { | 
|  | 2266 | const char *OperatorName = 0; | 
|  | 2267 | switch (Name.getCXXOverloadedOperator()) { | 
|  | 2268 | case OO_None: | 
|  | 2269 | case OO_Conditional: | 
|  | 2270 | case NUM_OVERLOADED_OPERATORS: | 
|  | 2271 | OperatorName = "operator"; | 
|  | 2272 | break; | 
|  | 2273 |  | 
|  | 2274 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ | 
|  | 2275 | case OO_##Name: OperatorName = "operator" Spelling; break; | 
|  | 2276 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) | 
|  | 2277 | #include "clang/Basic/OperatorKinds.def" | 
|  | 2278 |  | 
|  | 2279 | case OO_New:          OperatorName = "operator new"; break; | 
|  | 2280 | case OO_Delete:       OperatorName = "operator delete"; break; | 
|  | 2281 | case OO_Array_New:    OperatorName = "operator new[]"; break; | 
|  | 2282 | case OO_Array_Delete: OperatorName = "operator delete[]"; break; | 
|  | 2283 | case OO_Call:         OperatorName = "operator()"; break; | 
|  | 2284 | case OO_Subscript:    OperatorName = "operator[]"; break; | 
|  | 2285 | } | 
|  | 2286 | Result.AddTypedTextChunk(OperatorName); | 
|  | 2287 | break; | 
|  | 2288 | } | 
|  | 2289 |  | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2290 | case DeclarationName::Identifier: | 
|  | 2291 | case DeclarationName::CXXConversionFunctionName: | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2292 | case DeclarationName::CXXDestructorName: | 
|  | 2293 | case DeclarationName::CXXLiteralOperatorName: | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2294 | Result.AddTypedTextChunk( | 
|  | 2295 | Result.getAllocator().CopyString(ND->getNameAsString())); | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2296 | break; | 
|  | 2297 |  | 
|  | 2298 | case DeclarationName::CXXUsingDirective: | 
|  | 2299 | case DeclarationName::ObjCZeroArgSelector: | 
|  | 2300 | case DeclarationName::ObjCOneArgSelector: | 
|  | 2301 | case DeclarationName::ObjCMultiArgSelector: | 
|  | 2302 | break; | 
|  | 2303 |  | 
|  | 2304 | case DeclarationName::CXXConstructorName: { | 
|  | 2305 | CXXRecordDecl *Record = 0; | 
|  | 2306 | QualType Ty = Name.getCXXNameType(); | 
|  | 2307 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) | 
|  | 2308 | Record = cast<CXXRecordDecl>(RecordTy->getDecl()); | 
|  | 2309 | else if (const InjectedClassNameType *InjectedTy | 
|  | 2310 | = Ty->getAs<InjectedClassNameType>()) | 
|  | 2311 | Record = InjectedTy->getDecl(); | 
|  | 2312 | else { | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2313 | Result.AddTypedTextChunk( | 
|  | 2314 | Result.getAllocator().CopyString(ND->getNameAsString())); | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2315 | break; | 
|  | 2316 | } | 
|  | 2317 |  | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2318 | Result.AddTypedTextChunk( | 
|  | 2319 | Result.getAllocator().CopyString(Record->getNameAsString())); | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2320 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2321 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2322 | AddTemplateParameterChunks(Context, Policy, Template, Result); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2323 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2324 | } | 
|  | 2325 | break; | 
|  | 2326 | } | 
|  | 2327 | } | 
|  | 2328 | } | 
|  | 2329 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2330 | /// \brief If possible, create a new code completion string for the given | 
|  | 2331 | /// result. | 
|  | 2332 | /// | 
|  | 2333 | /// \returns Either a new, heap-allocated code completion string describing | 
|  | 2334 | /// how to use this result, or NULL to indicate that the string or name of the | 
|  | 2335 | /// result is all that is needed. | 
|  | 2336 | CodeCompletionString * | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2337 | CodeCompletionResult::CreateCodeCompletionString(Sema &S, | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2338 | CodeCompletionAllocator &Allocator) { | 
| Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2339 | typedef CodeCompletionString::Chunk Chunk; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2340 | CodeCompletionBuilder Result(Allocator, Priority, Availability); | 
| Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2341 |  | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2342 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2343 | if (Kind == RK_Pattern) { | 
|  | 2344 | Pattern->Priority = Priority; | 
|  | 2345 | Pattern->Availability = Availability; | 
|  | 2346 | return Pattern; | 
|  | 2347 | } | 
| Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2348 |  | 
| Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2349 | if (Kind == RK_Keyword) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2350 | Result.AddTypedTextChunk(Keyword); | 
|  | 2351 | return Result.TakeString(); | 
| Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2352 | } | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2353 |  | 
| Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2354 | if (Kind == RK_Macro) { | 
|  | 2355 | MacroInfo *MI = S.PP.getMacroInfo(Macro); | 
| Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2356 | assert(MI && "Not a macro?"); | 
|  | 2357 |  | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2358 | Result.AddTypedTextChunk( | 
|  | 2359 | Result.getAllocator().CopyString(Macro->getName())); | 
| Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2360 |  | 
|  | 2361 | if (!MI->isFunctionLike()) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2362 | return Result.TakeString(); | 
| Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2363 |  | 
|  | 2364 | // Format a function-like macro with placeholders for the arguments. | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2365 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); | 
| Douglas Gregor | 0c50531 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2366 | bool CombineVariadicArgument = false; | 
|  | 2367 | MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end(); | 
|  | 2368 | if (MI->isVariadic() && AEnd - A > 1) { | 
|  | 2369 | AEnd -= 2; | 
|  | 2370 | CombineVariadicArgument = true; | 
|  | 2371 | } | 
|  | 2372 | for (MacroInfo::arg_iterator A = MI->arg_begin(); A != AEnd; ++A) { | 
| Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2373 | if (A != MI->arg_begin()) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2374 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); | 
| Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2375 |  | 
| Douglas Gregor | 0c50531 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2376 | if (!MI->isVariadic() || A + 1 != AEnd) { | 
| Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2377 | // Non-variadic argument. | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2378 | Result.AddPlaceholderChunk( | 
|  | 2379 | Result.getAllocator().CopyString((*A)->getName())); | 
| Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2380 | continue; | 
|  | 2381 | } | 
|  | 2382 |  | 
| Douglas Gregor | 0c50531 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2383 | // Variadic argument; cope with the difference between GNU and C99 | 
| Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2384 | // variadic macros, providing a single placeholder for the rest of the | 
|  | 2385 | // arguments. | 
|  | 2386 | if ((*A)->isStr("__VA_ARGS__")) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2387 | Result.AddPlaceholderChunk("..."); | 
| Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2388 | else { | 
|  | 2389 | std::string Arg = (*A)->getName(); | 
|  | 2390 | Arg += "..."; | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2391 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); | 
| Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2392 | } | 
|  | 2393 | } | 
| Douglas Gregor | 0c50531 | 2011-07-30 08:17:44 +0000 | [diff] [blame] | 2394 |  | 
|  | 2395 | if (CombineVariadicArgument) { | 
|  | 2396 | // Handle the next-to-last argument, combining it with the variadic | 
|  | 2397 | // argument. | 
|  | 2398 | std::string LastArg = (*A)->getName(); | 
|  | 2399 | ++A; | 
|  | 2400 | if ((*A)->isStr("__VA_ARGS__")) | 
|  | 2401 | LastArg += ", ..."; | 
|  | 2402 | else | 
|  | 2403 | LastArg += ", " + (*A)->getName().str() + "..."; | 
|  | 2404 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(LastArg)); | 
|  | 2405 | } | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2406 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); | 
|  | 2407 | return Result.TakeString(); | 
| Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2408 | } | 
|  | 2409 |  | 
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2410 | assert(Kind == RK_Declaration && "Missed a result kind?"); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2411 | NamedDecl *ND = Declaration; | 
|  | 2412 |  | 
| Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2413 | if (StartsNestedNameSpecifier) { | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2414 | Result.AddTypedTextChunk( | 
|  | 2415 | Result.getAllocator().CopyString(ND->getNameAsString())); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2416 | Result.AddTextChunk("::"); | 
|  | 2417 | return Result.TakeString(); | 
| Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2418 | } | 
|  | 2419 |  | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2420 | AddResultTypeChunk(S.Context, Policy, ND, Result); | 
| Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2421 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2422 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { | 
| Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2423 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2424 | S.Context, Policy); | 
|  | 2425 | AddTypedNameChunk(S.Context, Policy, ND, Result); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2426 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2427 | AddFunctionParameterChunks(S.Context, Policy, Function, Result); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2428 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); | 
| Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2429 | AddFunctionTypeQualsToCompletionString(Result, Function); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2430 | return Result.TakeString(); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2431 | } | 
|  | 2432 |  | 
|  | 2433 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) { | 
| Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2434 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2435 | S.Context, Policy); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2436 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2437 | AddTypedNameChunk(S.Context, Policy, Function, Result); | 
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2438 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2439 | // Figure out which template parameters are deduced (or have default | 
|  | 2440 | // arguments). | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2441 | SmallVector<bool, 16> Deduced; | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2442 | S.MarkDeducedTemplateParameters(FunTmpl, Deduced); | 
|  | 2443 | unsigned LastDeducibleArgument; | 
|  | 2444 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; | 
|  | 2445 | --LastDeducibleArgument) { | 
|  | 2446 | if (!Deduced[LastDeducibleArgument - 1]) { | 
|  | 2447 | // C++0x: Figure out if the template argument has a default. If so, | 
|  | 2448 | // the user doesn't need to type this argument. | 
|  | 2449 | // FIXME: We need to abstract template parameters better! | 
|  | 2450 | bool HasDefaultArg = false; | 
|  | 2451 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2452 | LastDeducibleArgument - 1); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2453 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) | 
|  | 2454 | HasDefaultArg = TTP->hasDefaultArgument(); | 
|  | 2455 | else if (NonTypeTemplateParmDecl *NTTP | 
|  | 2456 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) | 
|  | 2457 | HasDefaultArg = NTTP->hasDefaultArgument(); | 
|  | 2458 | else { | 
|  | 2459 | assert(isa<TemplateTemplateParmDecl>(Param)); | 
|  | 2460 | HasDefaultArg | 
| Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2461 | = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2462 | } | 
|  | 2463 |  | 
|  | 2464 | if (!HasDefaultArg) | 
|  | 2465 | break; | 
|  | 2466 | } | 
|  | 2467 | } | 
|  | 2468 |  | 
|  | 2469 | if (LastDeducibleArgument) { | 
|  | 2470 | // Some of the function template arguments cannot be deduced from a | 
|  | 2471 | // function call, so we introduce an explicit template argument list | 
|  | 2472 | // containing all of the arguments up to the first deducible argument. | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2473 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2474 | AddTemplateParameterChunks(S.Context, Policy, FunTmpl, Result, | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2475 | LastDeducibleArgument); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2476 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2477 | } | 
|  | 2478 |  | 
|  | 2479 | // Add the function parameters | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2480 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2481 | AddFunctionParameterChunks(S.Context, Policy, Function, Result); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2482 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); | 
| Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2483 | AddFunctionTypeQualsToCompletionString(Result, Function); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2484 | return Result.TakeString(); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2485 | } | 
|  | 2486 |  | 
|  | 2487 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) { | 
| Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2488 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2489 | S.Context, Policy); | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2490 | Result.AddTypedTextChunk( | 
|  | 2491 | Result.getAllocator().CopyString(Template->getNameAsString())); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2492 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2493 | AddTemplateParameterChunks(S.Context, Policy, Template, Result); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2494 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); | 
|  | 2495 | return Result.TakeString(); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2496 | } | 
|  | 2497 |  | 
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2498 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { | 
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2499 | Selector Sel = Method->getSelector(); | 
|  | 2500 | if (Sel.isUnarySelector()) { | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2501 | Result.AddTypedTextChunk(Result.getAllocator().CopyString( | 
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2502 | Sel.getNameForSlot(0))); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2503 | return Result.TakeString(); | 
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2504 | } | 
|  | 2505 |  | 
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2506 | std::string SelName = Sel.getNameForSlot(0).str(); | 
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2507 | SelName += ':'; | 
|  | 2508 | if (StartParameter == 0) | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2509 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName)); | 
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2510 | else { | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2511 | Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName)); | 
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2512 |  | 
|  | 2513 | // If there is only one parameter, and we're past it, add an empty | 
|  | 2514 | // typed-text chunk since there is nothing to type. | 
|  | 2515 | if (Method->param_size() == 1) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2516 | Result.AddTypedTextChunk(""); | 
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2517 | } | 
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2518 | unsigned Idx = 0; | 
|  | 2519 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), | 
|  | 2520 | PEnd = Method->param_end(); | 
|  | 2521 | P != PEnd; (void)++P, ++Idx) { | 
|  | 2522 | if (Idx > 0) { | 
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2523 | std::string Keyword; | 
|  | 2524 | if (Idx > StartParameter) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2525 | Result.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2526 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) | 
| Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 2527 | Keyword += II->getName(); | 
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2528 | Keyword += ":"; | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2529 | if (Idx < StartParameter || AllParametersAreInformative) | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2530 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword)); | 
| Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2531 | else | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2532 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword)); | 
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2533 | } | 
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2534 |  | 
|  | 2535 | // If we're before the starting parameter, skip the placeholder. | 
|  | 2536 | if (Idx < StartParameter) | 
|  | 2537 | continue; | 
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2538 |  | 
|  | 2539 | std::string Arg; | 
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2540 |  | 
|  | 2541 | if ((*P)->getType()->isBlockPointerType() && !DeclaringEntity) | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2542 | Arg = FormatFunctionParameter(S.Context, Policy, *P, true); | 
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2543 | else { | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2544 | (*P)->getType().getAsStringInternal(Arg, Policy); | 
| Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2545 | Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier()) | 
|  | 2546 | + Arg + ")"; | 
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2547 | if (IdentifierInfo *II = (*P)->getIdentifier()) | 
| Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2548 | if (DeclaringEntity || AllParametersAreInformative) | 
| Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 2549 | Arg += II->getName(); | 
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2550 | } | 
|  | 2551 |  | 
| Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2552 | if (Method->isVariadic() && (P + 1) == PEnd) | 
|  | 2553 | Arg += ", ..."; | 
|  | 2554 |  | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2555 | if (DeclaringEntity) | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2556 | Result.AddTextChunk(Result.getAllocator().CopyString(Arg)); | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2557 | else if (AllParametersAreInformative) | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2558 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg)); | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2559 | else | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2560 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); | 
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2561 | } | 
|  | 2562 |  | 
| Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2563 | if (Method->isVariadic()) { | 
| Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2564 | if (Method->param_size() == 0) { | 
|  | 2565 | if (DeclaringEntity) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2566 | Result.AddTextChunk(", ..."); | 
| Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2567 | else if (AllParametersAreInformative) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2568 | Result.AddInformativeChunk(", ..."); | 
| Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2569 | else | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2570 | Result.AddPlaceholderChunk(", ..."); | 
| Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2571 | } | 
| Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2572 |  | 
|  | 2573 | MaybeAddSentinel(S.Context, Method, Result); | 
| Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2574 | } | 
|  | 2575 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2576 | return Result.TakeString(); | 
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2577 | } | 
|  | 2578 |  | 
| Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2579 | if (Qualifier) | 
| Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2580 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2581 | S.Context, Policy); | 
| Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2582 |  | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2583 | Result.AddTypedTextChunk( | 
|  | 2584 | Result.getAllocator().CopyString(ND->getNameAsString())); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2585 | return Result.TakeString(); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2586 | } | 
|  | 2587 |  | 
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2588 | CodeCompletionString * | 
|  | 2589 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( | 
|  | 2590 | unsigned CurrentArg, | 
| Douglas Gregor | 36e3b5c | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 2591 | Sema &S, | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2592 | CodeCompletionAllocator &Allocator) const { | 
| Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2593 | typedef CodeCompletionString::Chunk Chunk; | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2594 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2595 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2596 | // FIXME: Set priority, availability appropriately. | 
|  | 2597 | CodeCompletionBuilder Result(Allocator, 1, CXAvailability_Available); | 
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2598 | FunctionDecl *FDecl = getFunction(); | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2599 | AddResultTypeChunk(S.Context, Policy, FDecl, Result); | 
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2600 | const FunctionProtoType *Proto | 
|  | 2601 | = dyn_cast<FunctionProtoType>(getFunctionType()); | 
|  | 2602 | if (!FDecl && !Proto) { | 
|  | 2603 | // Function without a prototype. Just give the return type and a | 
|  | 2604 | // highlighted ellipsis. | 
|  | 2605 | const FunctionType *FT = getFunctionType(); | 
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2606 | Result.AddTextChunk(GetCompletionTypeString(FT->getResultType(), | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2607 | S.Context, Policy, | 
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2608 | Result.getAllocator())); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2609 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); | 
|  | 2610 | Result.AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "...")); | 
|  | 2611 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); | 
|  | 2612 | return Result.TakeString(); | 
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2613 | } | 
|  | 2614 |  | 
|  | 2615 | if (FDecl) | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2616 | Result.AddTextChunk( | 
|  | 2617 | Result.getAllocator().CopyString(FDecl->getNameAsString())); | 
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2618 | else | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2619 | Result.AddTextChunk( | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2620 | Result.getAllocator().CopyString( | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2621 | Proto->getResultType().getAsString(Policy))); | 
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2622 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2623 | Result.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); | 
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2624 | unsigned NumParams = FDecl? FDecl->getNumParams() : Proto->getNumArgs(); | 
|  | 2625 | for (unsigned I = 0; I != NumParams; ++I) { | 
|  | 2626 | if (I) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2627 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); | 
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2628 |  | 
|  | 2629 | std::string ArgString; | 
|  | 2630 | QualType ArgType; | 
|  | 2631 |  | 
|  | 2632 | if (FDecl) { | 
|  | 2633 | ArgString = FDecl->getParamDecl(I)->getNameAsString(); | 
|  | 2634 | ArgType = FDecl->getParamDecl(I)->getOriginalType(); | 
|  | 2635 | } else { | 
|  | 2636 | ArgType = Proto->getArgType(I); | 
|  | 2637 | } | 
|  | 2638 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2639 | ArgType.getAsStringInternal(ArgString, Policy); | 
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2640 |  | 
|  | 2641 | if (I == CurrentArg) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2642 | Result.AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2643 | Result.getAllocator().CopyString(ArgString))); | 
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2644 | else | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2645 | Result.AddTextChunk(Result.getAllocator().CopyString(ArgString)); | 
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2646 | } | 
|  | 2647 |  | 
|  | 2648 | if (Proto && Proto->isVariadic()) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2649 | Result.AddChunk(Chunk(CodeCompletionString::CK_Comma)); | 
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2650 | if (CurrentArg < NumParams) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2651 | Result.AddTextChunk("..."); | 
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2652 | else | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2653 | Result.AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "...")); | 
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2654 | } | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2655 | Result.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); | 
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2656 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2657 | return Result.TakeString(); | 
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2658 | } | 
|  | 2659 |  | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2660 | unsigned clang::getMacroUsagePriority(StringRef MacroName, | 
| Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2661 | const LangOptions &LangOpts, | 
| Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2662 | bool PreferredTypeIsPointer) { | 
|  | 2663 | unsigned Priority = CCP_Macro; | 
|  | 2664 |  | 
| Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2665 | // Treat the "nil", "Nil" and "NULL" macros as null pointer constants. | 
|  | 2666 | if (MacroName.equals("nil") || MacroName.equals("NULL") || | 
|  | 2667 | MacroName.equals("Nil")) { | 
| Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2668 | Priority = CCP_Constant; | 
|  | 2669 | if (PreferredTypeIsPointer) | 
|  | 2670 | Priority = Priority / CCF_SimilarTypeMatch; | 
| Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2671 | } | 
|  | 2672 | // Treat "YES", "NO", "true", and "false" as constants. | 
|  | 2673 | else if (MacroName.equals("YES") || MacroName.equals("NO") || | 
|  | 2674 | MacroName.equals("true") || MacroName.equals("false")) | 
|  | 2675 | Priority = CCP_Constant; | 
|  | 2676 | // Treat "bool" as a type. | 
|  | 2677 | else if (MacroName.equals("bool")) | 
|  | 2678 | Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0); | 
|  | 2679 |  | 
| Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2680 |  | 
|  | 2681 | return Priority; | 
|  | 2682 | } | 
|  | 2683 |  | 
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 2684 | CXCursorKind clang::getCursorKindForDecl(Decl *D) { | 
|  | 2685 | if (!D) | 
|  | 2686 | return CXCursor_UnexposedDecl; | 
|  | 2687 |  | 
|  | 2688 | switch (D->getKind()) { | 
|  | 2689 | case Decl::Enum:               return CXCursor_EnumDecl; | 
|  | 2690 | case Decl::EnumConstant:       return CXCursor_EnumConstantDecl; | 
|  | 2691 | case Decl::Field:              return CXCursor_FieldDecl; | 
|  | 2692 | case Decl::Function: | 
|  | 2693 | return CXCursor_FunctionDecl; | 
|  | 2694 | case Decl::ObjCCategory:       return CXCursor_ObjCCategoryDecl; | 
|  | 2695 | case Decl::ObjCCategoryImpl:   return CXCursor_ObjCCategoryImplDecl; | 
|  | 2696 | case Decl::ObjCClass: | 
|  | 2697 | // FIXME | 
|  | 2698 | return CXCursor_UnexposedDecl; | 
|  | 2699 | case Decl::ObjCForwardProtocol: | 
|  | 2700 | // FIXME | 
|  | 2701 | return CXCursor_UnexposedDecl; | 
|  | 2702 | case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl; | 
|  | 2703 | case Decl::ObjCInterface:      return CXCursor_ObjCInterfaceDecl; | 
|  | 2704 | case Decl::ObjCIvar:           return CXCursor_ObjCIvarDecl; | 
|  | 2705 | case Decl::ObjCMethod: | 
|  | 2706 | return cast<ObjCMethodDecl>(D)->isInstanceMethod() | 
|  | 2707 | ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl; | 
|  | 2708 | case Decl::CXXMethod:          return CXCursor_CXXMethod; | 
|  | 2709 | case Decl::CXXConstructor:     return CXCursor_Constructor; | 
|  | 2710 | case Decl::CXXDestructor:      return CXCursor_Destructor; | 
|  | 2711 | case Decl::CXXConversion:      return CXCursor_ConversionFunction; | 
|  | 2712 | case Decl::ObjCProperty:       return CXCursor_ObjCPropertyDecl; | 
|  | 2713 | case Decl::ObjCProtocol:       return CXCursor_ObjCProtocolDecl; | 
|  | 2714 | case Decl::ParmVar:            return CXCursor_ParmDecl; | 
|  | 2715 | case Decl::Typedef:            return CXCursor_TypedefDecl; | 
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2716 | case Decl::TypeAlias:          return CXCursor_TypeAliasDecl; | 
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 2717 | case Decl::Var:                return CXCursor_VarDecl; | 
|  | 2718 | case Decl::Namespace:          return CXCursor_Namespace; | 
|  | 2719 | case Decl::NamespaceAlias:     return CXCursor_NamespaceAlias; | 
|  | 2720 | case Decl::TemplateTypeParm:   return CXCursor_TemplateTypeParameter; | 
|  | 2721 | case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter; | 
|  | 2722 | case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter; | 
|  | 2723 | case Decl::FunctionTemplate:   return CXCursor_FunctionTemplate; | 
|  | 2724 | case Decl::ClassTemplate:      return CXCursor_ClassTemplate; | 
|  | 2725 | case Decl::ClassTemplatePartialSpecialization: | 
|  | 2726 | return CXCursor_ClassTemplatePartialSpecialization; | 
|  | 2727 | case Decl::UsingDirective:     return CXCursor_UsingDirective; | 
|  | 2728 |  | 
|  | 2729 | case Decl::Using: | 
|  | 2730 | case Decl::UnresolvedUsingValue: | 
|  | 2731 | case Decl::UnresolvedUsingTypename: | 
|  | 2732 | return CXCursor_UsingDeclaration; | 
|  | 2733 |  | 
| Douglas Gregor | 4cd6596 | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 2734 | case Decl::ObjCPropertyImpl: | 
|  | 2735 | switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) { | 
|  | 2736 | case ObjCPropertyImplDecl::Dynamic: | 
|  | 2737 | return CXCursor_ObjCDynamicDecl; | 
|  | 2738 |  | 
|  | 2739 | case ObjCPropertyImplDecl::Synthesize: | 
|  | 2740 | return CXCursor_ObjCSynthesizeDecl; | 
|  | 2741 | } | 
|  | 2742 | break; | 
|  | 2743 |  | 
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 2744 | default: | 
|  | 2745 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) { | 
|  | 2746 | switch (TD->getTagKind()) { | 
|  | 2747 | case TTK_Struct: return CXCursor_StructDecl; | 
|  | 2748 | case TTK_Class:  return CXCursor_ClassDecl; | 
|  | 2749 | case TTK_Union:  return CXCursor_UnionDecl; | 
|  | 2750 | case TTK_Enum:   return CXCursor_EnumDecl; | 
|  | 2751 | } | 
|  | 2752 | } | 
|  | 2753 | } | 
|  | 2754 |  | 
|  | 2755 | return CXCursor_UnexposedDecl; | 
|  | 2756 | } | 
|  | 2757 |  | 
| Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2758 | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, | 
|  | 2759 | bool TargetTypeIsPointer = false) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2760 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2761 |  | 
| Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2762 | Results.EnterNewScope(); | 
| Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2763 |  | 
| Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2764 | for (Preprocessor::macro_iterator M = PP.macro_begin(), | 
|  | 2765 | MEnd = PP.macro_end(); | 
| Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2766 | M != MEnd; ++M) { | 
| Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2767 | Results.AddResult(Result(M->first, | 
|  | 2768 | getMacroUsagePriority(M->first->getName(), | 
| Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2769 | PP.getLangOptions(), | 
| Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2770 | TargetTypeIsPointer))); | 
| Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2771 | } | 
| Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2772 |  | 
| Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2773 | Results.ExitScope(); | 
| Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2774 |  | 
| Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2775 | } | 
|  | 2776 |  | 
| Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2777 | static void AddPrettyFunctionResults(const LangOptions &LangOpts, | 
|  | 2778 | ResultBuilder &Results) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2779 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2780 |  | 
|  | 2781 | Results.EnterNewScope(); | 
| Douglas Gregor | 8e3e874 | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 2782 |  | 
| Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2783 | Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant)); | 
|  | 2784 | Results.AddResult(Result("__FUNCTION__", CCP_Constant)); | 
|  | 2785 | if (LangOpts.C99 || LangOpts.CPlusPlus0x) | 
|  | 2786 | Results.AddResult(Result("__func__", CCP_Constant)); | 
|  | 2787 | Results.ExitScope(); | 
|  | 2788 | } | 
|  | 2789 |  | 
| Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2790 | static void HandleCodeCompleteResults(Sema *S, | 
|  | 2791 | CodeCompleteConsumer *CodeCompleter, | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2792 | CodeCompletionContext Context, | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2793 | CodeCompletionResult *Results, | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2794 | unsigned NumResults) { | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2795 | if (CodeCompleter) | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2796 | CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2797 | } | 
|  | 2798 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2799 | static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S, | 
|  | 2800 | Sema::ParserCompletionContext PCC) { | 
|  | 2801 | switch (PCC) { | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2802 | case Sema::PCC_Namespace: | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2803 | return CodeCompletionContext::CCC_TopLevel; | 
|  | 2804 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2805 | case Sema::PCC_Class: | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2806 | return CodeCompletionContext::CCC_ClassStructUnion; | 
|  | 2807 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2808 | case Sema::PCC_ObjCInterface: | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2809 | return CodeCompletionContext::CCC_ObjCInterface; | 
|  | 2810 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2811 | case Sema::PCC_ObjCImplementation: | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2812 | return CodeCompletionContext::CCC_ObjCImplementation; | 
|  | 2813 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2814 | case Sema::PCC_ObjCInstanceVariableList: | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2815 | return CodeCompletionContext::CCC_ObjCIvarList; | 
|  | 2816 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2817 | case Sema::PCC_Template: | 
|  | 2818 | case Sema::PCC_MemberTemplate: | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2819 | if (S.CurContext->isFileContext()) | 
|  | 2820 | return CodeCompletionContext::CCC_TopLevel; | 
|  | 2821 | else if (S.CurContext->isRecord()) | 
|  | 2822 | return CodeCompletionContext::CCC_ClassStructUnion; | 
|  | 2823 | else | 
|  | 2824 | return CodeCompletionContext::CCC_Other; | 
|  | 2825 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2826 | case Sema::PCC_RecoveryInFunction: | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2827 | return CodeCompletionContext::CCC_Recovery; | 
| Douglas Gregor | c769d6e | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 2828 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2829 | case Sema::PCC_ForInit: | 
| Douglas Gregor | c769d6e | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 2830 | if (S.getLangOptions().CPlusPlus || S.getLangOptions().C99 || | 
|  | 2831 | S.getLangOptions().ObjC1) | 
|  | 2832 | return CodeCompletionContext::CCC_ParenthesizedExpression; | 
|  | 2833 | else | 
|  | 2834 | return CodeCompletionContext::CCC_Expression; | 
|  | 2835 |  | 
|  | 2836 | case Sema::PCC_Expression: | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2837 | case Sema::PCC_Condition: | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2838 | return CodeCompletionContext::CCC_Expression; | 
|  | 2839 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2840 | case Sema::PCC_Statement: | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2841 | return CodeCompletionContext::CCC_Statement; | 
| Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2842 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2843 | case Sema::PCC_Type: | 
| Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2844 | return CodeCompletionContext::CCC_Type; | 
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2845 |  | 
|  | 2846 | case Sema::PCC_ParenthesizedExpression: | 
|  | 2847 | return CodeCompletionContext::CCC_ParenthesizedExpression; | 
| Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2848 |  | 
|  | 2849 | case Sema::PCC_LocalDeclarationSpecifiers: | 
|  | 2850 | return CodeCompletionContext::CCC_Type; | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2851 | } | 
|  | 2852 |  | 
|  | 2853 | return CodeCompletionContext::CCC_Other; | 
|  | 2854 | } | 
|  | 2855 |  | 
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2856 | /// \brief If we're in a C++ virtual member function, add completion results | 
|  | 2857 | /// that invoke the functions we override, since it's common to invoke the | 
|  | 2858 | /// overridden function as well as adding new functionality. | 
|  | 2859 | /// | 
|  | 2860 | /// \param S The semantic analysis object for which we are generating results. | 
|  | 2861 | /// | 
|  | 2862 | /// \param InContext This context in which the nested-name-specifier preceding | 
|  | 2863 | /// the code-completion point | 
|  | 2864 | static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, | 
|  | 2865 | ResultBuilder &Results) { | 
|  | 2866 | // Look through blocks. | 
|  | 2867 | DeclContext *CurContext = S.CurContext; | 
|  | 2868 | while (isa<BlockDecl>(CurContext)) | 
|  | 2869 | CurContext = CurContext->getParent(); | 
|  | 2870 |  | 
|  | 2871 |  | 
|  | 2872 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext); | 
|  | 2873 | if (!Method || !Method->isVirtual()) | 
|  | 2874 | return; | 
|  | 2875 |  | 
|  | 2876 | // We need to have names for all of the parameters, if we're going to | 
|  | 2877 | // generate a forwarding call. | 
|  | 2878 | for (CXXMethodDecl::param_iterator P = Method->param_begin(), | 
|  | 2879 | PEnd = Method->param_end(); | 
|  | 2880 | P != PEnd; | 
|  | 2881 | ++P) { | 
|  | 2882 | if (!(*P)->getDeclName()) | 
|  | 2883 | return; | 
|  | 2884 | } | 
|  | 2885 |  | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2886 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); | 
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2887 | for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(), | 
|  | 2888 | MEnd = Method->end_overridden_methods(); | 
|  | 2889 | M != MEnd; ++M) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2890 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2891 | CXXMethodDecl *Overridden = const_cast<CXXMethodDecl *>(*M); | 
|  | 2892 | if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl()) | 
|  | 2893 | continue; | 
|  | 2894 |  | 
|  | 2895 | // If we need a nested-name-specifier, add one now. | 
|  | 2896 | if (!InContext) { | 
|  | 2897 | NestedNameSpecifier *NNS | 
|  | 2898 | = getRequiredQualification(S.Context, CurContext, | 
|  | 2899 | Overridden->getDeclContext()); | 
|  | 2900 | if (NNS) { | 
|  | 2901 | std::string Str; | 
|  | 2902 | llvm::raw_string_ostream OS(Str); | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2903 | NNS->print(OS, Policy); | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2904 | Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str())); | 
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2905 | } | 
|  | 2906 | } else if (!InContext->Equals(Overridden->getDeclContext())) | 
|  | 2907 | continue; | 
|  | 2908 |  | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2909 | Builder.AddTypedTextChunk(Results.getAllocator().CopyString( | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2910 | Overridden->getNameAsString())); | 
|  | 2911 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2912 | bool FirstParam = true; | 
|  | 2913 | for (CXXMethodDecl::param_iterator P = Method->param_begin(), | 
|  | 2914 | PEnd = Method->param_end(); | 
|  | 2915 | P != PEnd; ++P) { | 
|  | 2916 | if (FirstParam) | 
|  | 2917 | FirstParam = false; | 
|  | 2918 | else | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2919 | Builder.AddChunk(CodeCompletionString::CK_Comma); | 
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2920 |  | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2921 | Builder.AddPlaceholderChunk(Results.getAllocator().CopyString( | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2922 | (*P)->getIdentifier()->getName())); | 
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2923 | } | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2924 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 2925 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), | 
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2926 | CCP_SuperCompletion, | 
|  | 2927 | CXCursor_CXXMethod)); | 
|  | 2928 | Results.Ignore(Overridden); | 
|  | 2929 | } | 
|  | 2930 | } | 
|  | 2931 |  | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2932 | void Sema::CodeCompleteOrdinaryName(Scope *S, | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2933 | ParserCompletionContext CompletionContext) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2934 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2935 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2936 | mapCodeCompletionContext(*this, CompletionContext)); | 
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2937 | Results.EnterNewScope(); | 
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 2938 |  | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2939 | // Determine how to filter results, e.g., so that the names of | 
|  | 2940 | // values (functions, enumerators, function templates, etc.) are | 
|  | 2941 | // only allowed where we can have an expression. | 
|  | 2942 | switch (CompletionContext) { | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2943 | case PCC_Namespace: | 
|  | 2944 | case PCC_Class: | 
|  | 2945 | case PCC_ObjCInterface: | 
|  | 2946 | case PCC_ObjCImplementation: | 
|  | 2947 | case PCC_ObjCInstanceVariableList: | 
|  | 2948 | case PCC_Template: | 
|  | 2949 | case PCC_MemberTemplate: | 
| Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2950 | case PCC_Type: | 
| Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2951 | case PCC_LocalDeclarationSpecifiers: | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2952 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); | 
|  | 2953 | break; | 
|  | 2954 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2955 | case PCC_Statement: | 
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2956 | case PCC_ParenthesizedExpression: | 
| Douglas Gregor | 4d755e8 | 2010-08-24 23:58:17 +0000 | [diff] [blame] | 2957 | case PCC_Expression: | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2958 | case PCC_ForInit: | 
|  | 2959 | case PCC_Condition: | 
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 2960 | if (WantTypesInContext(CompletionContext, getLangOptions())) | 
|  | 2961 | Results.setFilter(&ResultBuilder::IsOrdinaryName); | 
|  | 2962 | else | 
|  | 2963 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); | 
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2964 |  | 
|  | 2965 | if (getLangOptions().CPlusPlus) | 
|  | 2966 | MaybeAddOverrideCalls(*this, /*InContext=*/0, Results); | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2967 | break; | 
| Douglas Gregor | 6da3db4 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 2968 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2969 | case PCC_RecoveryInFunction: | 
| Douglas Gregor | 6da3db4 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 2970 | // Unfiltered | 
|  | 2971 | break; | 
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2972 | } | 
|  | 2973 |  | 
| Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 2974 | // If we are in a C++ non-static member function, check the qualifiers on | 
|  | 2975 | // the member function to filter/prioritize the results list. | 
|  | 2976 | if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext)) | 
|  | 2977 | if (CurMethod->isInstance()) | 
|  | 2978 | Results.setObjectTypeQualifiers( | 
|  | 2979 | Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers())); | 
|  | 2980 |  | 
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 2981 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | 
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2982 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | 
|  | 2983 | CodeCompleter->includeGlobals()); | 
| Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 2984 |  | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2985 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); | 
| Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 2986 | Results.ExitScope(); | 
|  | 2987 |  | 
| Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2988 | switch (CompletionContext) { | 
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2989 | case PCC_ParenthesizedExpression: | 
| Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2990 | case PCC_Expression: | 
|  | 2991 | case PCC_Statement: | 
|  | 2992 | case PCC_RecoveryInFunction: | 
|  | 2993 | if (S->getFnParent()) | 
|  | 2994 | AddPrettyFunctionResults(PP.getLangOptions(), Results); | 
|  | 2995 | break; | 
|  | 2996 |  | 
|  | 2997 | case PCC_Namespace: | 
|  | 2998 | case PCC_Class: | 
|  | 2999 | case PCC_ObjCInterface: | 
|  | 3000 | case PCC_ObjCImplementation: | 
|  | 3001 | case PCC_ObjCInstanceVariableList: | 
|  | 3002 | case PCC_Template: | 
|  | 3003 | case PCC_MemberTemplate: | 
|  | 3004 | case PCC_ForInit: | 
|  | 3005 | case PCC_Condition: | 
|  | 3006 | case PCC_Type: | 
| Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3007 | case PCC_LocalDeclarationSpecifiers: | 
| Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3008 | break; | 
| Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3009 | } | 
|  | 3010 |  | 
| Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3011 | if (CodeCompleter->includeMacros()) | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3012 | AddMacroResults(PP, Results); | 
| Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3013 |  | 
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 3014 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3015 | Results.data(),Results.size()); | 
| Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 3016 | } | 
|  | 3017 |  | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3018 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, | 
|  | 3019 | ParsedType Receiver, | 
|  | 3020 | IdentifierInfo **SelIdents, | 
|  | 3021 | unsigned NumSelIdents, | 
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 3022 | bool AtArgumentExpression, | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3023 | bool IsSuper, | 
|  | 3024 | ResultBuilder &Results); | 
|  | 3025 |  | 
|  | 3026 | void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, | 
|  | 3027 | bool AllowNonIdentifiers, | 
|  | 3028 | bool AllowNestedNameSpecifiers) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3029 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3030 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3031 | AllowNestedNameSpecifiers | 
|  | 3032 | ? CodeCompletionContext::CCC_PotentiallyQualifiedName | 
|  | 3033 | : CodeCompletionContext::CCC_Name); | 
| Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3034 | Results.EnterNewScope(); | 
|  | 3035 |  | 
|  | 3036 | // Type qualifiers can come after names. | 
|  | 3037 | Results.AddResult(Result("const")); | 
|  | 3038 | Results.AddResult(Result("volatile")); | 
|  | 3039 | if (getLangOptions().C99) | 
|  | 3040 | Results.AddResult(Result("restrict")); | 
|  | 3041 |  | 
|  | 3042 | if (getLangOptions().CPlusPlus) { | 
|  | 3043 | if (AllowNonIdentifiers) { | 
|  | 3044 | Results.AddResult(Result("operator")); | 
|  | 3045 | } | 
|  | 3046 |  | 
|  | 3047 | // Add nested-name-specifiers. | 
|  | 3048 | if (AllowNestedNameSpecifiers) { | 
|  | 3049 | Results.allowNestedNameSpecifiers(); | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3050 | Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy); | 
| Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3051 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | 
|  | 3052 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, | 
|  | 3053 | CodeCompleter->includeGlobals()); | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3054 | Results.setFilter(0); | 
| Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3055 | } | 
|  | 3056 | } | 
|  | 3057 | Results.ExitScope(); | 
|  | 3058 |  | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3059 | // If we're in a context where we might have an expression (rather than a | 
|  | 3060 | // declaration), and what we've seen so far is an Objective-C type that could | 
|  | 3061 | // be a receiver of a class message, this may be a class message send with | 
|  | 3062 | // the initial opening bracket '[' missing. Add appropriate completions. | 
|  | 3063 | if (AllowNonIdentifiers && !AllowNestedNameSpecifiers && | 
|  | 3064 | DS.getTypeSpecType() == DeclSpec::TST_typename && | 
|  | 3065 | DS.getStorageClassSpecAsWritten() == DeclSpec::SCS_unspecified && | 
|  | 3066 | !DS.isThreadSpecified() && !DS.isExternInLinkageSpec() && | 
|  | 3067 | DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified && | 
|  | 3068 | DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && | 
|  | 3069 | DS.getTypeQualifiers() == 0 && | 
|  | 3070 | S && | 
|  | 3071 | (S->getFlags() & Scope::DeclScope) != 0 && | 
|  | 3072 | (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope | | 
|  | 3073 | Scope::FunctionPrototypeScope | | 
|  | 3074 | Scope::AtCatchScope)) == 0) { | 
|  | 3075 | ParsedType T = DS.getRepAsType(); | 
|  | 3076 | if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType()) | 
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 3077 | AddClassMessageCompletions(*this, S, T, 0, 0, false, false, Results); | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3078 | } | 
|  | 3079 |  | 
| Douglas Gregor | 56ccce0 | 2010-08-24 04:59:56 +0000 | [diff] [blame] | 3080 | // Note that we intentionally suppress macro results here, since we do not | 
|  | 3081 | // encourage using macros to produce the names of entities. | 
|  | 3082 |  | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3083 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 3084 | Results.getCompletionContext(), | 
| Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3085 | Results.data(), Results.size()); | 
|  | 3086 | } | 
|  | 3087 |  | 
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3088 | struct Sema::CodeCompleteExpressionData { | 
|  | 3089 | CodeCompleteExpressionData(QualType PreferredType = QualType()) | 
|  | 3090 | : PreferredType(PreferredType), IntegralConstantExpression(false), | 
|  | 3091 | ObjCCollection(false) { } | 
|  | 3092 |  | 
|  | 3093 | QualType PreferredType; | 
|  | 3094 | bool IntegralConstantExpression; | 
|  | 3095 | bool ObjCCollection; | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3096 | SmallVector<Decl *, 4> IgnoreDecls; | 
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3097 | }; | 
|  | 3098 |  | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3099 | /// \brief Perform code-completion in an expression context when we know what | 
|  | 3100 | /// type we're looking for. | 
| Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3101 | /// | 
|  | 3102 | /// \param IntegralConstantExpression Only permit integral constant | 
|  | 3103 | /// expressions. | 
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3104 | void Sema::CodeCompleteExpression(Scope *S, | 
|  | 3105 | const CodeCompleteExpressionData &Data) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3106 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3107 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 3108 | CodeCompletionContext::CCC_Expression); | 
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3109 | if (Data.ObjCCollection) | 
|  | 3110 | Results.setFilter(&ResultBuilder::IsObjCCollection); | 
|  | 3111 | else if (Data.IntegralConstantExpression) | 
| Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3112 | Results.setFilter(&ResultBuilder::IsIntegralConstantValue); | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3113 | else if (WantTypesInContext(PCC_Expression, getLangOptions())) | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3114 | Results.setFilter(&ResultBuilder::IsOrdinaryName); | 
|  | 3115 | else | 
|  | 3116 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); | 
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3117 |  | 
|  | 3118 | if (!Data.PreferredType.isNull()) | 
|  | 3119 | Results.setPreferredType(Data.PreferredType.getNonReferenceType()); | 
|  | 3120 |  | 
|  | 3121 | // Ignore any declarations that we were told that we don't care about. | 
|  | 3122 | for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I) | 
|  | 3123 | Results.Ignore(Data.IgnoreDecls[I]); | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3124 |  | 
|  | 3125 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | 
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3126 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | 
|  | 3127 | CodeCompleter->includeGlobals()); | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3128 |  | 
|  | 3129 | Results.EnterNewScope(); | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3130 | AddOrdinaryNameResults(PCC_Expression, S, *this, Results); | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3131 | Results.ExitScope(); | 
|  | 3132 |  | 
| Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3133 | bool PreferredTypeIsPointer = false; | 
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3134 | if (!Data.PreferredType.isNull()) | 
|  | 3135 | PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType() | 
|  | 3136 | || Data.PreferredType->isMemberPointerType() | 
|  | 3137 | || Data.PreferredType->isBlockPointerType(); | 
| Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3138 |  | 
| Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3139 | if (S->getFnParent() && | 
|  | 3140 | !Data.ObjCCollection && | 
|  | 3141 | !Data.IntegralConstantExpression) | 
|  | 3142 | AddPrettyFunctionResults(PP.getLangOptions(), Results); | 
|  | 3143 |  | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3144 | if (CodeCompleter->includeMacros()) | 
| Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3145 | AddMacroResults(PP, Results, PreferredTypeIsPointer); | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3146 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3147 | CodeCompletionContext(CodeCompletionContext::CCC_Expression, | 
|  | 3148 | Data.PreferredType), | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3149 | Results.data(),Results.size()); | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3150 | } | 
|  | 3151 |  | 
| Douglas Gregor | eda7e54 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 3152 | void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) { | 
|  | 3153 | if (E.isInvalid()) | 
|  | 3154 | CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction); | 
|  | 3155 | else if (getLangOptions().ObjC1) | 
|  | 3156 | CodeCompleteObjCInstanceMessage(S, E.take(), 0, 0, false); | 
| Douglas Gregor | ed0b69d | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 3157 | } | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3158 |  | 
| Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3159 | /// \brief The set of properties that have already been added, referenced by | 
|  | 3160 | /// property name. | 
|  | 3161 | typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet; | 
|  | 3162 |  | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3163 | static void AddObjCProperties(ObjCContainerDecl *Container, | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3164 | bool AllowCategories, | 
| Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3165 | bool AllowNullaryMethods, | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3166 | DeclContext *CurContext, | 
| Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3167 | AddedPropertiesSet &AddedProperties, | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3168 | ResultBuilder &Results) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3169 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3170 |  | 
|  | 3171 | // Add properties in this container. | 
|  | 3172 | for (ObjCContainerDecl::prop_iterator P = Container->prop_begin(), | 
|  | 3173 | PEnd = Container->prop_end(); | 
|  | 3174 | P != PEnd; | 
| Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3175 | ++P) { | 
|  | 3176 | if (AddedProperties.insert(P->getIdentifier())) | 
|  | 3177 | Results.MaybeAddResult(Result(*P, 0), CurContext); | 
|  | 3178 | } | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3179 |  | 
| Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3180 | // Add nullary methods | 
|  | 3181 | if (AllowNullaryMethods) { | 
|  | 3182 | ASTContext &Context = Container->getASTContext(); | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3183 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); | 
| Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3184 | for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), | 
|  | 3185 | MEnd = Container->meth_end(); | 
|  | 3186 | M != MEnd; ++M) { | 
|  | 3187 | if (M->getSelector().isUnarySelector()) | 
|  | 3188 | if (IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0)) | 
|  | 3189 | if (AddedProperties.insert(Name)) { | 
|  | 3190 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3191 | AddResultTypeChunk(Context, Policy, *M, Builder); | 
| Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3192 | Builder.AddTypedTextChunk( | 
|  | 3193 | Results.getAllocator().CopyString(Name->getName())); | 
|  | 3194 |  | 
|  | 3195 | CXAvailabilityKind Availability = CXAvailability_Available; | 
|  | 3196 | switch (M->getAvailability()) { | 
|  | 3197 | case AR_Available: | 
|  | 3198 | case AR_NotYetIntroduced: | 
|  | 3199 | Availability = CXAvailability_Available; | 
|  | 3200 | break; | 
|  | 3201 |  | 
|  | 3202 | case AR_Deprecated: | 
|  | 3203 | Availability = CXAvailability_Deprecated; | 
|  | 3204 | break; | 
|  | 3205 |  | 
|  | 3206 | case AR_Unavailable: | 
|  | 3207 | Availability = CXAvailability_NotAvailable; | 
|  | 3208 | break; | 
|  | 3209 | } | 
|  | 3210 |  | 
|  | 3211 | Results.MaybeAddResult(Result(Builder.TakeString(), | 
|  | 3212 | CCP_MemberDeclaration + CCD_MethodAsProperty, | 
|  | 3213 | M->isInstanceMethod() | 
|  | 3214 | ? CXCursor_ObjCInstanceMethodDecl | 
|  | 3215 | : CXCursor_ObjCClassMethodDecl, | 
|  | 3216 | Availability), | 
|  | 3217 | CurContext); | 
|  | 3218 | } | 
|  | 3219 | } | 
|  | 3220 | } | 
|  | 3221 |  | 
|  | 3222 |  | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3223 | // Add properties in referenced protocols. | 
|  | 3224 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { | 
|  | 3225 | for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(), | 
|  | 3226 | PEnd = Protocol->protocol_end(); | 
|  | 3227 | P != PEnd; ++P) | 
| Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3228 | AddObjCProperties(*P, AllowCategories, AllowNullaryMethods, CurContext, | 
|  | 3229 | AddedProperties, Results); | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3230 | } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){ | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3231 | if (AllowCategories) { | 
|  | 3232 | // Look through categories. | 
|  | 3233 | for (ObjCCategoryDecl *Category = IFace->getCategoryList(); | 
|  | 3234 | Category; Category = Category->getNextClassCategory()) | 
| Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3235 | AddObjCProperties(Category, AllowCategories, AllowNullaryMethods, | 
|  | 3236 | CurContext, AddedProperties, Results); | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3237 | } | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3238 |  | 
|  | 3239 | // Look through protocols. | 
| Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 3240 | for (ObjCInterfaceDecl::all_protocol_iterator | 
|  | 3241 | I = IFace->all_referenced_protocol_begin(), | 
|  | 3242 | E = IFace->all_referenced_protocol_end(); I != E; ++I) | 
| Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3243 | AddObjCProperties(*I, AllowCategories, AllowNullaryMethods, CurContext, | 
|  | 3244 | AddedProperties, Results); | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3245 |  | 
|  | 3246 | // Look in the superclass. | 
|  | 3247 | if (IFace->getSuperClass()) | 
| Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3248 | AddObjCProperties(IFace->getSuperClass(), AllowCategories, | 
|  | 3249 | AllowNullaryMethods, CurContext, | 
| Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3250 | AddedProperties, Results); | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3251 | } else if (const ObjCCategoryDecl *Category | 
|  | 3252 | = dyn_cast<ObjCCategoryDecl>(Container)) { | 
|  | 3253 | // Look through protocols. | 
| Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 3254 | for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(), | 
|  | 3255 | PEnd = Category->protocol_end(); | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3256 | P != PEnd; ++P) | 
| Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3257 | AddObjCProperties(*P, AllowCategories, AllowNullaryMethods, CurContext, | 
|  | 3258 | AddedProperties, Results); | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3259 | } | 
|  | 3260 | } | 
|  | 3261 |  | 
| Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 3262 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, Expr *BaseE, | 
| Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3263 | SourceLocation OpLoc, | 
|  | 3264 | bool IsArrow) { | 
|  | 3265 | if (!BaseE || !CodeCompleter) | 
|  | 3266 | return; | 
|  | 3267 |  | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3268 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3269 |  | 
| Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3270 | Expr *Base = static_cast<Expr *>(BaseE); | 
|  | 3271 | QualType BaseType = Base->getType(); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3272 |  | 
|  | 3273 | if (IsArrow) { | 
|  | 3274 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) | 
|  | 3275 | BaseType = Ptr->getPointeeType(); | 
|  | 3276 | else if (BaseType->isObjCObjectPointerType()) | 
| Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3277 | /*Do nothing*/ ; | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3278 | else | 
|  | 3279 | return; | 
|  | 3280 | } | 
|  | 3281 |  | 
| Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3282 | enum CodeCompletionContext::Kind contextKind; | 
|  | 3283 |  | 
|  | 3284 | if (IsArrow) { | 
|  | 3285 | contextKind = CodeCompletionContext::CCC_ArrowMemberAccess; | 
|  | 3286 | } | 
|  | 3287 | else { | 
|  | 3288 | if (BaseType->isObjCObjectPointerType() || | 
|  | 3289 | BaseType->isObjCObjectOrInterfaceType()) { | 
|  | 3290 | contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess; | 
|  | 3291 | } | 
|  | 3292 | else { | 
|  | 3293 | contextKind = CodeCompletionContext::CCC_DotMemberAccess; | 
|  | 3294 | } | 
|  | 3295 | } | 
|  | 3296 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3297 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
| Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3298 | CodeCompletionContext(contextKind, | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3299 | BaseType), | 
|  | 3300 | &ResultBuilder::IsMember); | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3301 | Results.EnterNewScope(); | 
|  | 3302 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { | 
| Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3303 | // Indicate that we are performing a member access, and the cv-qualifiers | 
|  | 3304 | // for the base object type. | 
|  | 3305 | Results.setObjectTypeQualifiers(BaseType.getQualifiers()); | 
|  | 3306 |  | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3307 | // Access to a C/C++ class, struct, or union. | 
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 3308 | Results.allowNestedNameSpecifiers(); | 
| Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 3309 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | 
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3310 | LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer, | 
|  | 3311 | CodeCompleter->includeGlobals()); | 
| Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3312 |  | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3313 | if (getLangOptions().CPlusPlus) { | 
|  | 3314 | if (!Results.empty()) { | 
|  | 3315 | // The "template" keyword can follow "->" or "." in the grammar. | 
|  | 3316 | // However, we only want to suggest the template keyword if something | 
|  | 3317 | // is dependent. | 
|  | 3318 | bool IsDependent = BaseType->isDependentType(); | 
|  | 3319 | if (!IsDependent) { | 
|  | 3320 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) | 
|  | 3321 | if (DeclContext *Ctx = (DeclContext *)DepScope->getEntity()) { | 
|  | 3322 | IsDependent = Ctx->isDependentContext(); | 
|  | 3323 | break; | 
|  | 3324 | } | 
|  | 3325 | } | 
| Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3326 |  | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3327 | if (IsDependent) | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3328 | Results.AddResult(Result("template")); | 
| Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3329 | } | 
| Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3330 | } | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3331 | } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) { | 
|  | 3332 | // Objective-C property reference. | 
| Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3333 | AddedPropertiesSet AddedProperties; | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3334 |  | 
|  | 3335 | // Add property results based on our interface. | 
|  | 3336 | const ObjCObjectPointerType *ObjCPtr | 
|  | 3337 | = BaseType->getAsObjCInterfacePointerType(); | 
|  | 3338 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!"); | 
| Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3339 | AddObjCProperties(ObjCPtr->getInterfaceDecl(), true, | 
|  | 3340 | /*AllowNullaryMethods=*/true, CurContext, | 
| Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3341 | AddedProperties, Results); | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3342 |  | 
|  | 3343 | // Add properties from the protocols in a qualified interface. | 
|  | 3344 | for (ObjCObjectPointerType::qual_iterator I = ObjCPtr->qual_begin(), | 
|  | 3345 | E = ObjCPtr->qual_end(); | 
|  | 3346 | I != E; ++I) | 
| Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3347 | AddObjCProperties(*I, true, /*AllowNullaryMethods=*/true, CurContext, | 
|  | 3348 | AddedProperties, Results); | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3349 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || | 
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3350 | (!IsArrow && BaseType->isObjCObjectType())) { | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3351 | // Objective-C instance variable access. | 
|  | 3352 | ObjCInterfaceDecl *Class = 0; | 
|  | 3353 | if (const ObjCObjectPointerType *ObjCPtr | 
|  | 3354 | = BaseType->getAs<ObjCObjectPointerType>()) | 
|  | 3355 | Class = ObjCPtr->getInterfaceDecl(); | 
|  | 3356 | else | 
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3357 | Class = BaseType->getAs<ObjCObjectType>()->getInterface(); | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3358 |  | 
|  | 3359 | // Add all ivars from this class and its superclasses. | 
| Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 3360 | if (Class) { | 
|  | 3361 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | 
|  | 3362 | Results.setFilter(&ResultBuilder::IsObjCIvar); | 
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3363 | LookupVisibleDecls(Class, LookupMemberName, Consumer, | 
|  | 3364 | CodeCompleter->includeGlobals()); | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3365 | } | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3366 | } | 
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3367 |  | 
|  | 3368 | // FIXME: How do we cope with isa? | 
|  | 3369 |  | 
|  | 3370 | Results.ExitScope(); | 
| Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3371 |  | 
| Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3372 | // Hand off the results found for code completion. | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3373 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3374 | Results.getCompletionContext(), | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3375 | Results.data(),Results.size()); | 
| Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3376 | } | 
|  | 3377 |  | 
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3378 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { | 
|  | 3379 | if (!CodeCompleter) | 
|  | 3380 | return; | 
|  | 3381 |  | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3382 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3383 | ResultBuilder::LookupFilter Filter = 0; | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3384 | enum CodeCompletionContext::Kind ContextKind | 
|  | 3385 | = CodeCompletionContext::CCC_Other; | 
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3386 | switch ((DeclSpec::TST)TagSpec) { | 
|  | 3387 | case DeclSpec::TST_enum: | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3388 | Filter = &ResultBuilder::IsEnum; | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3389 | ContextKind = CodeCompletionContext::CCC_EnumTag; | 
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3390 | break; | 
|  | 3391 |  | 
|  | 3392 | case DeclSpec::TST_union: | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3393 | Filter = &ResultBuilder::IsUnion; | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3394 | ContextKind = CodeCompletionContext::CCC_UnionTag; | 
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3395 | break; | 
|  | 3396 |  | 
|  | 3397 | case DeclSpec::TST_struct: | 
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3398 | case DeclSpec::TST_class: | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3399 | Filter = &ResultBuilder::IsClassOrStruct; | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3400 | ContextKind = CodeCompletionContext::CCC_ClassOrStructTag; | 
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3401 | break; | 
|  | 3402 |  | 
|  | 3403 | default: | 
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3404 | llvm_unreachable("Unknown type specifier kind in CodeCompleteTag"); | 
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3405 | } | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3406 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3407 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), ContextKind); | 
| Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3408 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | 
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 3409 |  | 
|  | 3410 | // First pass: look for tags. | 
|  | 3411 | Results.setFilter(Filter); | 
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3412 | LookupVisibleDecls(S, LookupTagName, Consumer, | 
|  | 3413 | CodeCompleter->includeGlobals()); | 
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 3414 |  | 
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3415 | if (CodeCompleter->includeGlobals()) { | 
|  | 3416 | // Second pass: look for nested name specifiers. | 
|  | 3417 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); | 
|  | 3418 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer); | 
|  | 3419 | } | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3420 |  | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3421 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3422 | Results.data(),Results.size()); | 
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3423 | } | 
|  | 3424 |  | 
| Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3425 | void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3426 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 3427 | CodeCompletionContext::CCC_TypeQualifiers); | 
| Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3428 | Results.EnterNewScope(); | 
|  | 3429 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const)) | 
|  | 3430 | Results.AddResult("const"); | 
|  | 3431 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile)) | 
|  | 3432 | Results.AddResult("volatile"); | 
|  | 3433 | if (getLangOptions().C99 && | 
|  | 3434 | !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict)) | 
|  | 3435 | Results.AddResult("restrict"); | 
|  | 3436 | Results.ExitScope(); | 
|  | 3437 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3438 | Results.getCompletionContext(), | 
| Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3439 | Results.data(), Results.size()); | 
|  | 3440 | } | 
|  | 3441 |  | 
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3442 | void Sema::CodeCompleteCase(Scope *S) { | 
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3443 | if (getCurFunction()->SwitchStack.empty() || !CodeCompleter) | 
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3444 | return; | 
| John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 3445 |  | 
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3446 | SwitchStmt *Switch = getCurFunction()->SwitchStack.back(); | 
| John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 3447 | QualType type = Switch->getCond()->IgnoreImplicit()->getType(); | 
|  | 3448 | if (!type->isEnumeralType()) { | 
|  | 3449 | CodeCompleteExpressionData Data(type); | 
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3450 | Data.IntegralConstantExpression = true; | 
|  | 3451 | CodeCompleteExpression(S, Data); | 
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3452 | return; | 
| Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3453 | } | 
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3454 |  | 
|  | 3455 | // Code-complete the cases of a switch statement over an enumeration type | 
|  | 3456 | // by providing the list of | 
| John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 3457 | EnumDecl *Enum = type->castAs<EnumType>()->getDecl(); | 
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3458 |  | 
|  | 3459 | // Determine which enumerators we have already seen in the switch statement. | 
|  | 3460 | // FIXME: Ideally, we would also be able to look *past* the code-completion | 
|  | 3461 | // token, in case we are code-completing in the middle of the switch and not | 
|  | 3462 | // at the end. However, we aren't able to do so at the moment. | 
|  | 3463 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; | 
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3464 | NestedNameSpecifier *Qualifier = 0; | 
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3465 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; | 
|  | 3466 | SC = SC->getNextSwitchCase()) { | 
|  | 3467 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); | 
|  | 3468 | if (!Case) | 
|  | 3469 | continue; | 
|  | 3470 |  | 
|  | 3471 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); | 
|  | 3472 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal)) | 
|  | 3473 | if (EnumConstantDecl *Enumerator | 
|  | 3474 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { | 
|  | 3475 | // We look into the AST of the case statement to determine which | 
|  | 3476 | // enumerator was named. Alternatively, we could compute the value of | 
|  | 3477 | // the integral constant expression, then compare it against the | 
|  | 3478 | // values of each enumerator. However, value-based approach would not | 
|  | 3479 | // work as well with C++ templates where enumerators declared within a | 
|  | 3480 | // template are type- and value-dependent. | 
|  | 3481 | EnumeratorsSeen.insert(Enumerator); | 
|  | 3482 |  | 
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3483 | // If this is a qualified-id, keep track of the nested-name-specifier | 
|  | 3484 | // so that we can reproduce it as part of code completion, e.g., | 
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3485 | // | 
|  | 3486 | //   switch (TagD.getKind()) { | 
|  | 3487 | //     case TagDecl::TK_enum: | 
|  | 3488 | //       break; | 
|  | 3489 | //     case XXX | 
|  | 3490 | // | 
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3491 | // At the XXX, our completions are TagDecl::TK_union, | 
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3492 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, | 
|  | 3493 | // TK_struct, and TK_class. | 
| Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3494 | Qualifier = DRE->getQualifier(); | 
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3495 | } | 
|  | 3496 | } | 
|  | 3497 |  | 
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3498 | if (getLangOptions().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { | 
|  | 3499 | // If there are no prior enumerators in C++, check whether we have to | 
|  | 3500 | // qualify the names of the enumerators that we suggest, because they | 
|  | 3501 | // may not be visible in this scope. | 
|  | 3502 | Qualifier = getRequiredQualification(Context, CurContext, | 
|  | 3503 | Enum->getDeclContext()); | 
|  | 3504 |  | 
|  | 3505 | // FIXME: Scoped enums need to start with "EnumDecl" as the context! | 
|  | 3506 | } | 
|  | 3507 |  | 
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3508 | // Add any enumerators that have not yet been mentioned. | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3509 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 3510 | CodeCompletionContext::CCC_Expression); | 
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3511 | Results.EnterNewScope(); | 
|  | 3512 | for (EnumDecl::enumerator_iterator E = Enum->enumerator_begin(), | 
|  | 3513 | EEnd = Enum->enumerator_end(); | 
|  | 3514 | E != EEnd; ++E) { | 
|  | 3515 | if (EnumeratorsSeen.count(*E)) | 
|  | 3516 | continue; | 
|  | 3517 |  | 
| Douglas Gregor | 3a69eaf | 2011-02-18 23:30:37 +0000 | [diff] [blame] | 3518 | CodeCompletionResult R(*E, Qualifier); | 
|  | 3519 | R.Priority = CCP_EnumInCase; | 
|  | 3520 | Results.AddResult(R, CurContext, 0, false); | 
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3521 | } | 
|  | 3522 | Results.ExitScope(); | 
| Douglas Gregor | 28556092 | 2010-04-06 20:02:15 +0000 | [diff] [blame] | 3523 |  | 
| Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3524 | //We need to make sure we're setting the right context, | 
|  | 3525 | //so only say we include macros if the code completer says we do | 
|  | 3526 | enum CodeCompletionContext::Kind kind = CodeCompletionContext::CCC_Other; | 
|  | 3527 | if (CodeCompleter->includeMacros()) { | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3528 | AddMacroResults(PP, Results); | 
| Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3529 | kind = CodeCompletionContext::CCC_OtherWithMacros; | 
|  | 3530 | } | 
|  | 3531 |  | 
|  | 3532 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3533 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 3534 | kind, | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3535 | Results.data(),Results.size()); | 
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3536 | } | 
|  | 3537 |  | 
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3538 | namespace { | 
|  | 3539 | struct IsBetterOverloadCandidate { | 
|  | 3540 | Sema &S; | 
| John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3541 | SourceLocation Loc; | 
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3542 |  | 
|  | 3543 | public: | 
| John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3544 | explicit IsBetterOverloadCandidate(Sema &S, SourceLocation Loc) | 
|  | 3545 | : S(S), Loc(Loc) { } | 
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3546 |  | 
|  | 3547 | bool | 
|  | 3548 | operator()(const OverloadCandidate &X, const OverloadCandidate &Y) const { | 
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3549 | return isBetterOverloadCandidate(S, X, Y, Loc); | 
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3550 | } | 
|  | 3551 | }; | 
|  | 3552 | } | 
|  | 3553 |  | 
| Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3554 | static bool anyNullArguments(Expr **Args, unsigned NumArgs) { | 
|  | 3555 | if (NumArgs && !Args) | 
|  | 3556 | return true; | 
|  | 3557 |  | 
|  | 3558 | for (unsigned I = 0; I != NumArgs; ++I) | 
|  | 3559 | if (!Args[I]) | 
|  | 3560 | return true; | 
|  | 3561 |  | 
|  | 3562 | return false; | 
|  | 3563 | } | 
|  | 3564 |  | 
| Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 3565 | void Sema::CodeCompleteCall(Scope *S, Expr *FnIn, | 
|  | 3566 | Expr **ArgsIn, unsigned NumArgs) { | 
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3567 | if (!CodeCompleter) | 
|  | 3568 | return; | 
| Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3569 |  | 
|  | 3570 | // When we're code-completing for a call, we fall back to ordinary | 
|  | 3571 | // name code-completion whenever we can't produce specific | 
|  | 3572 | // results. We may want to revisit this strategy in the future, | 
|  | 3573 | // e.g., by merging the two kinds of results. | 
|  | 3574 |  | 
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3575 | Expr *Fn = (Expr *)FnIn; | 
|  | 3576 | Expr **Args = (Expr **)ArgsIn; | 
| Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3577 |  | 
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3578 | // Ignore type-dependent call expressions entirely. | 
| Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3579 | if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args, NumArgs) || | 
| Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3580 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3581 | CodeCompleteOrdinaryName(S, PCC_Expression); | 
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3582 | return; | 
| Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3583 | } | 
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3584 |  | 
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3585 | // Build an overload candidate set based on the functions we find. | 
| John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3586 | SourceLocation Loc = Fn->getExprLoc(); | 
|  | 3587 | OverloadCandidateSet CandidateSet(Loc); | 
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3588 |  | 
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3589 | // FIXME: What if we're calling something that isn't a function declaration? | 
|  | 3590 | // FIXME: What if we're calling a pseudo-destructor? | 
|  | 3591 | // FIXME: What if we're calling a member function? | 
|  | 3592 |  | 
| Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3593 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3594 | SmallVector<ResultCandidate, 8> Results; | 
| Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3595 |  | 
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3596 | Expr *NakedFn = Fn->IgnoreParenCasts(); | 
|  | 3597 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) | 
|  | 3598 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet, | 
|  | 3599 | /*PartialOverloading=*/ true); | 
|  | 3600 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(NakedFn)) { | 
|  | 3601 | FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); | 
| Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3602 | if (FDecl) { | 
| Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3603 | if (!getLangOptions().CPlusPlus || | 
|  | 3604 | !FDecl->getType()->getAs<FunctionProtoType>()) | 
| Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3605 | Results.push_back(ResultCandidate(FDecl)); | 
|  | 3606 | else | 
| John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3607 | // FIXME: access? | 
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3608 | AddOverloadCandidate(FDecl, DeclAccessPair::make(FDecl, AS_none), | 
|  | 3609 | Args, NumArgs, CandidateSet, | 
| Douglas Gregor | b05275a | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 3610 | false, /*PartialOverloading*/true); | 
| Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3611 | } | 
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3612 | } | 
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3613 |  | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3614 | QualType ParamType; | 
|  | 3615 |  | 
| Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3616 | if (!CandidateSet.empty()) { | 
|  | 3617 | // Sort the overload candidate set by placing the best overloads first. | 
|  | 3618 | std::stable_sort(CandidateSet.begin(), CandidateSet.end(), | 
| John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3619 | IsBetterOverloadCandidate(*this, Loc)); | 
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3620 |  | 
| Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3621 | // Add the remaining viable overload candidates as code-completion reslults. | 
|  | 3622 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), | 
|  | 3623 | CandEnd = CandidateSet.end(); | 
|  | 3624 | Cand != CandEnd; ++Cand) { | 
|  | 3625 | if (Cand->Viable) | 
|  | 3626 | Results.push_back(ResultCandidate(Cand->Function)); | 
|  | 3627 | } | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3628 |  | 
|  | 3629 | // From the viable candidates, try to determine the type of this parameter. | 
|  | 3630 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { | 
|  | 3631 | if (const FunctionType *FType = Results[I].getFunctionType()) | 
|  | 3632 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FType)) | 
|  | 3633 | if (NumArgs < Proto->getNumArgs()) { | 
|  | 3634 | if (ParamType.isNull()) | 
|  | 3635 | ParamType = Proto->getArgType(NumArgs); | 
|  | 3636 | else if (!Context.hasSameUnqualifiedType( | 
|  | 3637 | ParamType.getNonReferenceType(), | 
|  | 3638 | Proto->getArgType(NumArgs).getNonReferenceType())) { | 
|  | 3639 | ParamType = QualType(); | 
|  | 3640 | break; | 
|  | 3641 | } | 
|  | 3642 | } | 
|  | 3643 | } | 
|  | 3644 | } else { | 
|  | 3645 | // Try to determine the parameter type from the type of the expression | 
|  | 3646 | // being called. | 
|  | 3647 | QualType FunctionType = Fn->getType(); | 
|  | 3648 | if (const PointerType *Ptr = FunctionType->getAs<PointerType>()) | 
|  | 3649 | FunctionType = Ptr->getPointeeType(); | 
|  | 3650 | else if (const BlockPointerType *BlockPtr | 
|  | 3651 | = FunctionType->getAs<BlockPointerType>()) | 
|  | 3652 | FunctionType = BlockPtr->getPointeeType(); | 
|  | 3653 | else if (const MemberPointerType *MemPtr | 
|  | 3654 | = FunctionType->getAs<MemberPointerType>()) | 
|  | 3655 | FunctionType = MemPtr->getPointeeType(); | 
|  | 3656 |  | 
|  | 3657 | if (const FunctionProtoType *Proto | 
|  | 3658 | = FunctionType->getAs<FunctionProtoType>()) { | 
|  | 3659 | if (NumArgs < Proto->getNumArgs()) | 
|  | 3660 | ParamType = Proto->getArgType(NumArgs); | 
|  | 3661 | } | 
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3662 | } | 
| Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3663 |  | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3664 | if (ParamType.isNull()) | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3665 | CodeCompleteOrdinaryName(S, PCC_Expression); | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3666 | else | 
|  | 3667 | CodeCompleteExpression(S, ParamType); | 
|  | 3668 |  | 
| Douglas Gregor | c01890e | 2010-04-06 20:19:47 +0000 | [diff] [blame] | 3669 | if (!Results.empty()) | 
| Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3670 | CodeCompleter->ProcessOverloadCandidates(*this, NumArgs, Results.data(), | 
|  | 3671 | Results.size()); | 
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3672 | } | 
|  | 3673 |  | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3674 | void Sema::CodeCompleteInitializer(Scope *S, Decl *D) { | 
|  | 3675 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3676 | if (!VD) { | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3677 | CodeCompleteOrdinaryName(S, PCC_Expression); | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3678 | return; | 
|  | 3679 | } | 
|  | 3680 |  | 
|  | 3681 | CodeCompleteExpression(S, VD->getType()); | 
|  | 3682 | } | 
|  | 3683 |  | 
|  | 3684 | void Sema::CodeCompleteReturn(Scope *S) { | 
|  | 3685 | QualType ResultType; | 
|  | 3686 | if (isa<BlockDecl>(CurContext)) { | 
|  | 3687 | if (BlockScopeInfo *BSI = getCurBlock()) | 
|  | 3688 | ResultType = BSI->ReturnType; | 
|  | 3689 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext)) | 
|  | 3690 | ResultType = Function->getResultType(); | 
|  | 3691 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext)) | 
|  | 3692 | ResultType = Method->getResultType(); | 
|  | 3693 |  | 
|  | 3694 | if (ResultType.isNull()) | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3695 | CodeCompleteOrdinaryName(S, PCC_Expression); | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3696 | else | 
|  | 3697 | CodeCompleteExpression(S, ResultType); | 
|  | 3698 | } | 
|  | 3699 |  | 
| Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 3700 | void Sema::CodeCompleteAfterIf(Scope *S) { | 
|  | 3701 | typedef CodeCompletionResult Result; | 
|  | 3702 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 3703 | mapCodeCompletionContext(*this, PCC_Statement)); | 
|  | 3704 | Results.setFilter(&ResultBuilder::IsOrdinaryName); | 
|  | 3705 | Results.EnterNewScope(); | 
|  | 3706 |  | 
|  | 3707 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | 
|  | 3708 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | 
|  | 3709 | CodeCompleter->includeGlobals()); | 
|  | 3710 |  | 
|  | 3711 | AddOrdinaryNameResults(PCC_Statement, S, *this, Results); | 
|  | 3712 |  | 
|  | 3713 | // "else" block | 
|  | 3714 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
|  | 3715 | Builder.AddTypedTextChunk("else"); | 
|  | 3716 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 3717 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | 
|  | 3718 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | 
|  | 3719 | Builder.AddPlaceholderChunk("statements"); | 
|  | 3720 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | 
|  | 3721 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | 
|  | 3722 | Results.AddResult(Builder.TakeString()); | 
|  | 3723 |  | 
|  | 3724 | // "else if" block | 
|  | 3725 | Builder.AddTypedTextChunk("else"); | 
|  | 3726 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 3727 | Builder.AddTextChunk("if"); | 
|  | 3728 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 3729 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 3730 | if (getLangOptions().CPlusPlus) | 
|  | 3731 | Builder.AddPlaceholderChunk("condition"); | 
|  | 3732 | else | 
|  | 3733 | Builder.AddPlaceholderChunk("expression"); | 
|  | 3734 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 3735 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 3736 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | 
|  | 3737 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | 
|  | 3738 | Builder.AddPlaceholderChunk("statements"); | 
|  | 3739 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | 
|  | 3740 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | 
|  | 3741 | Results.AddResult(Builder.TakeString()); | 
|  | 3742 |  | 
|  | 3743 | Results.ExitScope(); | 
|  | 3744 |  | 
|  | 3745 | if (S->getFnParent()) | 
|  | 3746 | AddPrettyFunctionResults(PP.getLangOptions(), Results); | 
|  | 3747 |  | 
|  | 3748 | if (CodeCompleter->includeMacros()) | 
|  | 3749 | AddMacroResults(PP, Results); | 
|  | 3750 |  | 
|  | 3751 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), | 
|  | 3752 | Results.data(),Results.size()); | 
|  | 3753 | } | 
|  | 3754 |  | 
| Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 3755 | void Sema::CodeCompleteAssignmentRHS(Scope *S, Expr *LHS) { | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3756 | if (LHS) | 
|  | 3757 | CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType()); | 
|  | 3758 | else | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3759 | CodeCompleteOrdinaryName(S, PCC_Expression); | 
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3760 | } | 
|  | 3761 |  | 
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3762 | void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, | 
| Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3763 | bool EnteringContext) { | 
|  | 3764 | if (!SS.getScopeRep() || !CodeCompleter) | 
|  | 3765 | return; | 
|  | 3766 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3767 | DeclContext *Ctx = computeDeclContext(SS, EnteringContext); | 
|  | 3768 | if (!Ctx) | 
|  | 3769 | return; | 
| Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 3770 |  | 
|  | 3771 | // Try to instantiate any non-dependent declaration contexts before | 
|  | 3772 | // we look in them. | 
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3773 | if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx)) | 
| Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 3774 | return; | 
|  | 3775 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3776 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 3777 | CodeCompletionContext::CCC_Name); | 
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3778 | Results.EnterNewScope(); | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3779 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3780 | // The "template" keyword can follow "::" in the grammar, but only | 
|  | 3781 | // put it into the grammar if the nested-name-specifier is dependent. | 
|  | 3782 | NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); | 
|  | 3783 | if (!Results.empty() && NNS->isDependent()) | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3784 | Results.AddResult("template"); | 
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3785 |  | 
|  | 3786 | // Add calls to overridden virtual functions, if there are any. | 
|  | 3787 | // | 
|  | 3788 | // FIXME: This isn't wonderful, because we don't know whether we're actually | 
|  | 3789 | // in a context that permits expressions. This is a general issue with | 
|  | 3790 | // qualified-id completions. | 
|  | 3791 | if (!EnteringContext) | 
|  | 3792 | MaybeAddOverrideCalls(*this, Ctx, Results); | 
|  | 3793 | Results.ExitScope(); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3794 |  | 
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3795 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | 
|  | 3796 | LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer); | 
|  | 3797 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3798 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | c1679ec | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 3799 | Results.getCompletionContext(), | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3800 | Results.data(),Results.size()); | 
| Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3801 | } | 
| Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3802 |  | 
|  | 3803 | void Sema::CodeCompleteUsing(Scope *S) { | 
|  | 3804 | if (!CodeCompleter) | 
|  | 3805 | return; | 
|  | 3806 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3807 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3808 | CodeCompletionContext::CCC_PotentiallyQualifiedName, | 
|  | 3809 | &ResultBuilder::IsNestedNameSpecifier); | 
| Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3810 | Results.EnterNewScope(); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3811 |  | 
|  | 3812 | // If we aren't in class scope, we could see the "namespace" keyword. | 
|  | 3813 | if (!S->isClassScope()) | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3814 | Results.AddResult(CodeCompletionResult("namespace")); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3815 |  | 
|  | 3816 | // After "using", we can see anything that would start a | 
|  | 3817 | // nested-name-specifier. | 
| Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3818 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | 
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3819 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | 
|  | 3820 | CodeCompleter->includeGlobals()); | 
| Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3821 | Results.ExitScope(); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3822 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3823 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3824 | CodeCompletionContext::CCC_PotentiallyQualifiedName, | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3825 | Results.data(),Results.size()); | 
| Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3826 | } | 
|  | 3827 |  | 
|  | 3828 | void Sema::CodeCompleteUsingDirective(Scope *S) { | 
|  | 3829 | if (!CodeCompleter) | 
|  | 3830 | return; | 
|  | 3831 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3832 | // After "using namespace", we expect to see a namespace name or namespace | 
|  | 3833 | // alias. | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3834 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 3835 | CodeCompletionContext::CCC_Namespace, | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3836 | &ResultBuilder::IsNamespaceOrAlias); | 
| Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3837 | Results.EnterNewScope(); | 
| Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3838 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | 
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3839 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | 
|  | 3840 | CodeCompleter->includeGlobals()); | 
| Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3841 | Results.ExitScope(); | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3842 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3843 | CodeCompletionContext::CCC_Namespace, | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3844 | Results.data(),Results.size()); | 
| Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3845 | } | 
|  | 3846 |  | 
|  | 3847 | void Sema::CodeCompleteNamespaceDecl(Scope *S)  { | 
|  | 3848 | if (!CodeCompleter) | 
|  | 3849 | return; | 
|  | 3850 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3851 | DeclContext *Ctx = (DeclContext *)S->getEntity(); | 
|  | 3852 | if (!S->getParent()) | 
|  | 3853 | Ctx = Context.getTranslationUnitDecl(); | 
|  | 3854 |  | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3855 | bool SuppressedGlobalResults | 
|  | 3856 | = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx); | 
|  | 3857 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3858 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3859 | SuppressedGlobalResults | 
|  | 3860 | ? CodeCompletionContext::CCC_Namespace | 
|  | 3861 | : CodeCompletionContext::CCC_Other, | 
|  | 3862 | &ResultBuilder::IsNamespace); | 
|  | 3863 |  | 
|  | 3864 | if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) { | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3865 | // We only want to see those namespaces that have already been defined | 
|  | 3866 | // within this scope, because its likely that the user is creating an | 
|  | 3867 | // extended namespace declaration. Keep track of the most recent | 
|  | 3868 | // definition of each namespace. | 
|  | 3869 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; | 
|  | 3870 | for (DeclContext::specific_decl_iterator<NamespaceDecl> | 
|  | 3871 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); | 
|  | 3872 | NS != NSEnd; ++NS) | 
|  | 3873 | OrigToLatest[NS->getOriginalNamespace()] = *NS; | 
|  | 3874 |  | 
|  | 3875 | // Add the most recent definition (or extended definition) of each | 
|  | 3876 | // namespace to the list of results. | 
| Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3877 | Results.EnterNewScope(); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3878 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator | 
|  | 3879 | NS = OrigToLatest.begin(), NSEnd = OrigToLatest.end(); | 
|  | 3880 | NS != NSEnd; ++NS) | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3881 | Results.AddResult(CodeCompletionResult(NS->second, 0), | 
| Douglas Gregor | fc59ce1 | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 3882 | CurContext, 0, false); | 
| Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3883 | Results.ExitScope(); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3884 | } | 
|  | 3885 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3886 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3887 | Results.getCompletionContext(), | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3888 | Results.data(),Results.size()); | 
| Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3889 | } | 
|  | 3890 |  | 
|  | 3891 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S)  { | 
|  | 3892 | if (!CodeCompleter) | 
|  | 3893 | return; | 
|  | 3894 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3895 | // After "namespace", we expect to see a namespace or alias. | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3896 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 3897 | CodeCompletionContext::CCC_Namespace, | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3898 | &ResultBuilder::IsNamespaceOrAlias); | 
| Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3899 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | 
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3900 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | 
|  | 3901 | CodeCompleter->includeGlobals()); | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3902 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3903 | Results.getCompletionContext(), | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3904 | Results.data(),Results.size()); | 
| Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3905 | } | 
|  | 3906 |  | 
| Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3907 | void Sema::CodeCompleteOperatorName(Scope *S) { | 
|  | 3908 | if (!CodeCompleter) | 
|  | 3909 | return; | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3910 |  | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3911 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3912 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 3913 | CodeCompletionContext::CCC_Type, | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3914 | &ResultBuilder::IsType); | 
| Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3915 | Results.EnterNewScope(); | 
| Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3916 |  | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3917 | // Add the names of overloadable operators. | 
|  | 3918 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly)      \ | 
|  | 3919 | if (std::strcmp(Spelling, "?"))                                                  \ | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3920 | Results.AddResult(Result(Spelling)); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3921 | #include "clang/Basic/OperatorKinds.def" | 
|  | 3922 |  | 
|  | 3923 | // Add any type names visible from the current scope | 
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 3924 | Results.allowNestedNameSpecifiers(); | 
| Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3925 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | 
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3926 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | 
|  | 3927 | CodeCompleter->includeGlobals()); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3928 |  | 
|  | 3929 | // Add any type specifiers | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3930 | AddTypeSpecifierResults(getLangOptions(), Results); | 
| Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3931 | Results.ExitScope(); | 
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3932 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3933 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3934 | CodeCompletionContext::CCC_Type, | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3935 | Results.data(),Results.size()); | 
| Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3936 | } | 
| Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3937 |  | 
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3938 | void Sema::CodeCompleteConstructorInitializer(Decl *ConstructorD, | 
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3939 | CXXCtorInitializer** Initializers, | 
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3940 | unsigned NumInitializers) { | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3941 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); | 
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3942 | CXXConstructorDecl *Constructor | 
|  | 3943 | = static_cast<CXXConstructorDecl *>(ConstructorD); | 
|  | 3944 | if (!Constructor) | 
|  | 3945 | return; | 
|  | 3946 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3947 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3948 | CodeCompletionContext::CCC_PotentiallyQualifiedName); | 
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3949 | Results.EnterNewScope(); | 
|  | 3950 |  | 
|  | 3951 | // Fill in any already-initialized fields or base classes. | 
|  | 3952 | llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields; | 
|  | 3953 | llvm::SmallPtrSet<CanQualType, 4> InitializedBases; | 
|  | 3954 | for (unsigned I = 0; I != NumInitializers; ++I) { | 
|  | 3955 | if (Initializers[I]->isBaseInitializer()) | 
|  | 3956 | InitializedBases.insert( | 
|  | 3957 | Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0))); | 
|  | 3958 | else | 
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3959 | InitializedFields.insert(cast<FieldDecl>( | 
|  | 3960 | Initializers[I]->getAnyMember())); | 
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3961 | } | 
|  | 3962 |  | 
|  | 3963 | // Add completions for base classes. | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3964 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3965 | bool SawLastInitializer = (NumInitializers == 0); | 
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3966 | CXXRecordDecl *ClassDecl = Constructor->getParent(); | 
|  | 3967 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), | 
|  | 3968 | BaseEnd = ClassDecl->bases_end(); | 
|  | 3969 | Base != BaseEnd; ++Base) { | 
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3970 | if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) { | 
|  | 3971 | SawLastInitializer | 
|  | 3972 | = NumInitializers > 0 && | 
|  | 3973 | Initializers[NumInitializers - 1]->isBaseInitializer() && | 
|  | 3974 | Context.hasSameUnqualifiedType(Base->getType(), | 
|  | 3975 | QualType(Initializers[NumInitializers - 1]->getBaseClass(), 0)); | 
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3976 | continue; | 
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3977 | } | 
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3978 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3979 | Builder.AddTypedTextChunk( | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3980 | Results.getAllocator().CopyString( | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3981 | Base->getType().getAsString(Policy))); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3982 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 3983 | Builder.AddPlaceholderChunk("args"); | 
|  | 3984 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 3985 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), | 
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3986 | SawLastInitializer? CCP_NextInitializer | 
|  | 3987 | : CCP_MemberDeclaration)); | 
|  | 3988 | SawLastInitializer = false; | 
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3989 | } | 
|  | 3990 |  | 
|  | 3991 | // Add completions for virtual base classes. | 
|  | 3992 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), | 
|  | 3993 | BaseEnd = ClassDecl->vbases_end(); | 
|  | 3994 | Base != BaseEnd; ++Base) { | 
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3995 | if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) { | 
|  | 3996 | SawLastInitializer | 
|  | 3997 | = NumInitializers > 0 && | 
|  | 3998 | Initializers[NumInitializers - 1]->isBaseInitializer() && | 
|  | 3999 | Context.hasSameUnqualifiedType(Base->getType(), | 
|  | 4000 | QualType(Initializers[NumInitializers - 1]->getBaseClass(), 0)); | 
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4001 | continue; | 
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4002 | } | 
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4003 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4004 | Builder.AddTypedTextChunk( | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4005 | Builder.getAllocator().CopyString( | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4006 | Base->getType().getAsString(Policy))); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4007 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 4008 | Builder.AddPlaceholderChunk("args"); | 
|  | 4009 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 4010 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), | 
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4011 | SawLastInitializer? CCP_NextInitializer | 
|  | 4012 | : CCP_MemberDeclaration)); | 
|  | 4013 | SawLastInitializer = false; | 
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4014 | } | 
|  | 4015 |  | 
|  | 4016 | // Add completions for members. | 
|  | 4017 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), | 
|  | 4018 | FieldEnd = ClassDecl->field_end(); | 
|  | 4019 | Field != FieldEnd; ++Field) { | 
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4020 | if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl()))) { | 
|  | 4021 | SawLastInitializer | 
|  | 4022 | = NumInitializers > 0 && | 
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4023 | Initializers[NumInitializers - 1]->isAnyMemberInitializer() && | 
|  | 4024 | Initializers[NumInitializers - 1]->getAnyMember() == *Field; | 
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4025 | continue; | 
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4026 | } | 
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4027 |  | 
|  | 4028 | if (!Field->getDeclName()) | 
|  | 4029 | continue; | 
|  | 4030 |  | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4031 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4032 | Field->getIdentifier()->getName())); | 
|  | 4033 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 4034 | Builder.AddPlaceholderChunk("args"); | 
|  | 4035 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 4036 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), | 
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4037 | SawLastInitializer? CCP_NextInitializer | 
| Douglas Gregor | f3af311 | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 4038 | : CCP_MemberDeclaration, | 
|  | 4039 | CXCursor_MemberRef)); | 
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4040 | SawLastInitializer = false; | 
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4041 | } | 
|  | 4042 | Results.ExitScope(); | 
|  | 4043 |  | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4044 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), | 
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4045 | Results.data(), Results.size()); | 
|  | 4046 | } | 
|  | 4047 |  | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4048 | // Macro that expands to @Keyword or Keyword, depending on whether NeedAt is | 
|  | 4049 | // true or false. | 
|  | 4050 | #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) NeedAt? "@" #Keyword : #Keyword | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4051 | static void AddObjCImplementationResults(const LangOptions &LangOpts, | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4052 | ResultBuilder &Results, | 
|  | 4053 | bool NeedAt) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4054 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4055 | // Since we have an implementation, we can end it. | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4056 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end))); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4057 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4058 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4059 | if (LangOpts.ObjC2) { | 
|  | 4060 | // @dynamic | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4061 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,dynamic)); | 
|  | 4062 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 4063 | Builder.AddPlaceholderChunk("property"); | 
|  | 4064 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4065 |  | 
|  | 4066 | // @synthesize | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4067 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synthesize)); | 
|  | 4068 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 4069 | Builder.AddPlaceholderChunk("property"); | 
|  | 4070 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4071 | } | 
|  | 4072 | } | 
|  | 4073 |  | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4074 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4075 | ResultBuilder &Results, | 
|  | 4076 | bool NeedAt) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4077 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4078 |  | 
|  | 4079 | // Since we have an interface or protocol, we can end it. | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4080 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end))); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4081 |  | 
|  | 4082 | if (LangOpts.ObjC2) { | 
|  | 4083 | // @property | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4084 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,property))); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4085 |  | 
|  | 4086 | // @required | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4087 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,required))); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4088 |  | 
|  | 4089 | // @optional | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4090 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,optional))); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4091 | } | 
|  | 4092 | } | 
|  | 4093 |  | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4094 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4095 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4096 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4097 |  | 
|  | 4098 | // @class name ; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4099 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,class)); | 
|  | 4100 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 4101 | Builder.AddPlaceholderChunk("name"); | 
|  | 4102 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4103 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4104 | if (Results.includeCodePatterns()) { | 
|  | 4105 | // @interface name | 
|  | 4106 | // FIXME: Could introduce the whole pattern, including superclasses and | 
|  | 4107 | // such. | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4108 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,interface)); | 
|  | 4109 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 4110 | Builder.AddPlaceholderChunk("class"); | 
|  | 4111 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4112 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4113 | // @protocol name | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4114 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol)); | 
|  | 4115 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 4116 | Builder.AddPlaceholderChunk("protocol"); | 
|  | 4117 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4118 |  | 
|  | 4119 | // @implementation name | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4120 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,implementation)); | 
|  | 4121 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 4122 | Builder.AddPlaceholderChunk("class"); | 
|  | 4123 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4124 | } | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4125 |  | 
|  | 4126 | // @compatibility_alias name | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4127 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,compatibility_alias)); | 
|  | 4128 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 4129 | Builder.AddPlaceholderChunk("alias"); | 
|  | 4130 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 4131 | Builder.AddPlaceholderChunk("class"); | 
|  | 4132 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4133 | } | 
|  | 4134 |  | 
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4135 | void Sema::CodeCompleteObjCAtDirective(Scope *S) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4136 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4137 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 4138 | CodeCompletionContext::CCC_Other); | 
| Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4139 | Results.EnterNewScope(); | 
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4140 | if (isa<ObjCImplDecl>(CurContext)) | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4141 | AddObjCImplementationResults(getLangOptions(), Results, false); | 
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4142 | else if (CurContext->isObjCContainer()) | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4143 | AddObjCInterfaceResults(getLangOptions(), Results, false); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4144 | else | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4145 | AddObjCTopLevelResults(Results, false); | 
| Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4146 | Results.ExitScope(); | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4147 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 4148 | CodeCompletionContext::CCC_Other, | 
|  | 4149 | Results.data(),Results.size()); | 
| Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 4150 | } | 
|  | 4151 |  | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4152 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4153 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4154 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4155 |  | 
|  | 4156 | // @encode ( type-name ) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4157 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,encode)); | 
|  | 4158 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 4159 | Builder.AddPlaceholderChunk("type-name"); | 
|  | 4160 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 4161 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4162 |  | 
|  | 4163 | // @protocol ( protocol-name ) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4164 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol)); | 
|  | 4165 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 4166 | Builder.AddPlaceholderChunk("protocol-name"); | 
|  | 4167 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 4168 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4169 |  | 
|  | 4170 | // @selector ( selector ) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4171 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,selector)); | 
|  | 4172 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 4173 | Builder.AddPlaceholderChunk("selector"); | 
|  | 4174 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 4175 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4176 | } | 
|  | 4177 |  | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4178 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4179 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4180 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4181 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4182 | if (Results.includeCodePatterns()) { | 
|  | 4183 | // @try { statements } @catch ( declaration ) { statements } @finally | 
|  | 4184 | //   { statements } | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4185 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,try)); | 
|  | 4186 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | 
|  | 4187 | Builder.AddPlaceholderChunk("statements"); | 
|  | 4188 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | 
|  | 4189 | Builder.AddTextChunk("@catch"); | 
|  | 4190 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 4191 | Builder.AddPlaceholderChunk("parameter"); | 
|  | 4192 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 4193 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | 
|  | 4194 | Builder.AddPlaceholderChunk("statements"); | 
|  | 4195 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | 
|  | 4196 | Builder.AddTextChunk("@finally"); | 
|  | 4197 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | 
|  | 4198 | Builder.AddPlaceholderChunk("statements"); | 
|  | 4199 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | 
|  | 4200 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4201 | } | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4202 |  | 
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4203 | // @throw | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4204 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,throw)); | 
|  | 4205 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 4206 | Builder.AddPlaceholderChunk("expression"); | 
|  | 4207 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4208 |  | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4209 | if (Results.includeCodePatterns()) { | 
|  | 4210 | // @synchronized ( expression ) { statements } | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4211 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synchronized)); | 
|  | 4212 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 4213 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 4214 | Builder.AddPlaceholderChunk("expression"); | 
|  | 4215 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 4216 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | 
|  | 4217 | Builder.AddPlaceholderChunk("statements"); | 
|  | 4218 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | 
|  | 4219 | Results.AddResult(Result(Builder.TakeString())); | 
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 4220 | } | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4221 | } | 
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4222 |  | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4223 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, | 
| Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4224 | ResultBuilder &Results, | 
|  | 4225 | bool NeedAt) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4226 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4227 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,private))); | 
|  | 4228 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,protected))); | 
|  | 4229 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,public))); | 
| Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4230 | if (LangOpts.ObjC2) | 
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4231 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,package))); | 
| Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4232 | } | 
|  | 4233 |  | 
|  | 4234 | void Sema::CodeCompleteObjCAtVisibility(Scope *S) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4235 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 4236 | CodeCompletionContext::CCC_Other); | 
| Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4237 | Results.EnterNewScope(); | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4238 | AddObjCVisibilityResults(getLangOptions(), Results, false); | 
| Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4239 | Results.ExitScope(); | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4240 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 4241 | CodeCompletionContext::CCC_Other, | 
|  | 4242 | Results.data(),Results.size()); | 
| Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 4243 | } | 
|  | 4244 |  | 
|  | 4245 | void Sema::CodeCompleteObjCAtStatement(Scope *S) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4246 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 4247 | CodeCompletionContext::CCC_Other); | 
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 4248 | Results.EnterNewScope(); | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4249 | AddObjCStatementResults(Results, false); | 
|  | 4250 | AddObjCExpressionResults(Results, false); | 
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4251 | Results.ExitScope(); | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4252 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 4253 | CodeCompletionContext::CCC_Other, | 
|  | 4254 | Results.data(),Results.size()); | 
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4255 | } | 
|  | 4256 |  | 
|  | 4257 | void Sema::CodeCompleteObjCAtExpression(Scope *S) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4258 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 4259 | CodeCompletionContext::CCC_Other); | 
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4260 | Results.EnterNewScope(); | 
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 4261 | AddObjCExpressionResults(Results, false); | 
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4262 | Results.ExitScope(); | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4263 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 4264 | CodeCompletionContext::CCC_Other, | 
|  | 4265 | Results.data(),Results.size()); | 
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 4266 | } | 
|  | 4267 |  | 
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4268 | /// \brief Determine whether the addition of the given flag to an Objective-C | 
|  | 4269 | /// property's attributes will cause a conflict. | 
|  | 4270 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { | 
|  | 4271 | // Check if we've already added this flag. | 
|  | 4272 | if (Attributes & NewFlag) | 
|  | 4273 | return true; | 
|  | 4274 |  | 
|  | 4275 | Attributes |= NewFlag; | 
|  | 4276 |  | 
|  | 4277 | // Check for collisions with "readonly". | 
|  | 4278 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && | 
|  | 4279 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | | 
|  | 4280 | ObjCDeclSpec::DQ_PR_assign | | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4281 | ObjCDeclSpec::DQ_PR_unsafe_unretained | | 
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4282 | ObjCDeclSpec::DQ_PR_copy | | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4283 | ObjCDeclSpec::DQ_PR_retain | | 
|  | 4284 | ObjCDeclSpec::DQ_PR_strong))) | 
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4285 | return true; | 
|  | 4286 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4287 | // Check for more than one of { assign, copy, retain, strong }. | 
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4288 | unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign | | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4289 | ObjCDeclSpec::DQ_PR_unsafe_unretained | | 
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4290 | ObjCDeclSpec::DQ_PR_copy | | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4291 | ObjCDeclSpec::DQ_PR_retain| | 
|  | 4292 | ObjCDeclSpec::DQ_PR_strong); | 
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4293 | if (AssignCopyRetMask && | 
|  | 4294 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4295 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained && | 
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4296 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4297 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain && | 
|  | 4298 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong) | 
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4299 | return true; | 
|  | 4300 |  | 
|  | 4301 | return false; | 
|  | 4302 | } | 
|  | 4303 |  | 
| Douglas Gregor | 36029f4 | 2009-11-18 23:08:07 +0000 | [diff] [blame] | 4304 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { | 
| Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4305 | if (!CodeCompleter) | 
|  | 4306 | return; | 
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4307 |  | 
| Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4308 | unsigned Attributes = ODS.getPropertyAttributes(); | 
|  | 4309 |  | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4310 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4311 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 4312 | CodeCompletionContext::CCC_Other); | 
| Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4313 | Results.EnterNewScope(); | 
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4314 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4315 | Results.AddResult(CodeCompletionResult("readonly")); | 
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4316 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4317 | Results.AddResult(CodeCompletionResult("assign")); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4318 | if (!ObjCPropertyFlagConflicts(Attributes, | 
|  | 4319 | ObjCDeclSpec::DQ_PR_unsafe_unretained)) | 
|  | 4320 | Results.AddResult(CodeCompletionResult("unsafe_unretained")); | 
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4321 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4322 | Results.AddResult(CodeCompletionResult("readwrite")); | 
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4323 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4324 | Results.AddResult(CodeCompletionResult("retain")); | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4325 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong)) | 
|  | 4326 | Results.AddResult(CodeCompletionResult("strong")); | 
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4327 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4328 | Results.AddResult(CodeCompletionResult("copy")); | 
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4329 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4330 | Results.AddResult(CodeCompletionResult("nonatomic")); | 
| Fariborz Jahanian | 1c2d29e | 2011-06-11 17:14:27 +0000 | [diff] [blame] | 4331 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic)) | 
|  | 4332 | Results.AddResult(CodeCompletionResult("atomic")); | 
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4333 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4334 | CodeCompletionBuilder Setter(Results.getAllocator()); | 
|  | 4335 | Setter.AddTypedTextChunk("setter"); | 
|  | 4336 | Setter.AddTextChunk(" = "); | 
|  | 4337 | Setter.AddPlaceholderChunk("method"); | 
|  | 4338 | Results.AddResult(CodeCompletionResult(Setter.TakeString())); | 
| Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 4339 | } | 
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 4340 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4341 | CodeCompletionBuilder Getter(Results.getAllocator()); | 
|  | 4342 | Getter.AddTypedTextChunk("getter"); | 
|  | 4343 | Getter.AddTextChunk(" = "); | 
|  | 4344 | Getter.AddPlaceholderChunk("method"); | 
|  | 4345 | Results.AddResult(CodeCompletionResult(Getter.TakeString())); | 
| Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 4346 | } | 
| Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4347 | Results.ExitScope(); | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4348 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 4349 | CodeCompletionContext::CCC_Other, | 
|  | 4350 | Results.data(),Results.size()); | 
| Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 4351 | } | 
| Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4352 |  | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4353 | /// \brief Descripts the kind of Objective-C method that we want to find | 
|  | 4354 | /// via code completion. | 
|  | 4355 | enum ObjCMethodKind { | 
|  | 4356 | MK_Any, //< Any kind of method, provided it means other specified criteria. | 
|  | 4357 | MK_ZeroArgSelector, //< Zero-argument (unary) selector. | 
|  | 4358 | MK_OneArgSelector //< One-argument selector. | 
|  | 4359 | }; | 
|  | 4360 |  | 
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4361 | static bool isAcceptableObjCSelector(Selector Sel, | 
|  | 4362 | ObjCMethodKind WantKind, | 
|  | 4363 | IdentifierInfo **SelIdents, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4364 | unsigned NumSelIdents, | 
|  | 4365 | bool AllowSameLength = true) { | 
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4366 | if (NumSelIdents > Sel.getNumArgs()) | 
|  | 4367 | return false; | 
|  | 4368 |  | 
|  | 4369 | switch (WantKind) { | 
|  | 4370 | case MK_Any:             break; | 
|  | 4371 | case MK_ZeroArgSelector: return Sel.isUnarySelector(); | 
|  | 4372 | case MK_OneArgSelector:  return Sel.getNumArgs() == 1; | 
|  | 4373 | } | 
|  | 4374 |  | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4375 | if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs()) | 
|  | 4376 | return false; | 
|  | 4377 |  | 
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4378 | for (unsigned I = 0; I != NumSelIdents; ++I) | 
|  | 4379 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) | 
|  | 4380 | return false; | 
|  | 4381 |  | 
|  | 4382 | return true; | 
|  | 4383 | } | 
|  | 4384 |  | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4385 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, | 
|  | 4386 | ObjCMethodKind WantKind, | 
|  | 4387 | IdentifierInfo **SelIdents, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4388 | unsigned NumSelIdents, | 
|  | 4389 | bool AllowSameLength = true) { | 
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4390 | return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4391 | NumSelIdents, AllowSameLength); | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4392 | } | 
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4393 |  | 
|  | 4394 | namespace { | 
|  | 4395 | /// \brief A set of selectors, which is used to avoid introducing multiple | 
|  | 4396 | /// completions with the same selector into the result set. | 
|  | 4397 | typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet; | 
|  | 4398 | } | 
|  | 4399 |  | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4400 | /// \brief Add all of the Objective-C methods in the given Objective-C | 
|  | 4401 | /// container to the set of results. | 
|  | 4402 | /// | 
|  | 4403 | /// The container will be a class, protocol, category, or implementation of | 
|  | 4404 | /// any of the above. This mether will recurse to include methods from | 
|  | 4405 | /// the superclasses of classes along with their categories, protocols, and | 
|  | 4406 | /// implementations. | 
|  | 4407 | /// | 
|  | 4408 | /// \param Container the container in which we'll look to find methods. | 
|  | 4409 | /// | 
|  | 4410 | /// \param WantInstance whether to add instance methods (only); if false, this | 
|  | 4411 | /// routine will add factory methods (only). | 
|  | 4412 | /// | 
|  | 4413 | /// \param CurContext the context in which we're performing the lookup that | 
|  | 4414 | /// finds methods. | 
|  | 4415 | /// | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4416 | /// \param AllowSameLength Whether we allow a method to be added to the list | 
|  | 4417 | /// when it has the same number of parameters as we have selector identifiers. | 
|  | 4418 | /// | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4419 | /// \param Results the structure into which we'll add results. | 
|  | 4420 | static void AddObjCMethods(ObjCContainerDecl *Container, | 
|  | 4421 | bool WantInstanceMethods, | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4422 | ObjCMethodKind WantKind, | 
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4423 | IdentifierInfo **SelIdents, | 
|  | 4424 | unsigned NumSelIdents, | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4425 | DeclContext *CurContext, | 
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4426 | VisitedSelectorSet &Selectors, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4427 | bool AllowSameLength, | 
| Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4428 | ResultBuilder &Results, | 
|  | 4429 | bool InOriginalClass = true) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4430 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4431 | for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), | 
|  | 4432 | MEnd = Container->meth_end(); | 
|  | 4433 | M != MEnd; ++M) { | 
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4434 | if ((*M)->isInstanceMethod() == WantInstanceMethods) { | 
|  | 4435 | // Check whether the selector identifiers we've been given are a | 
|  | 4436 | // subset of the identifiers for this particular method. | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4437 | if (!isAcceptableObjCMethod(*M, WantKind, SelIdents, NumSelIdents, | 
|  | 4438 | AllowSameLength)) | 
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4439 | continue; | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4440 |  | 
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4441 | if (!Selectors.insert((*M)->getSelector())) | 
|  | 4442 | continue; | 
|  | 4443 |  | 
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4444 | Result R = Result(*M, 0); | 
|  | 4445 | R.StartParameter = NumSelIdents; | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4446 | R.AllParametersAreInformative = (WantKind != MK_Any); | 
| Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4447 | if (!InOriginalClass) | 
|  | 4448 | R.Priority += CCD_InBaseClass; | 
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4449 | Results.MaybeAddResult(R, CurContext); | 
|  | 4450 | } | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4451 | } | 
|  | 4452 |  | 
| Douglas Gregor | f37c949 | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 4453 | // Visit the protocols of protocols. | 
|  | 4454 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { | 
|  | 4455 | const ObjCList<ObjCProtocolDecl> &Protocols | 
|  | 4456 | = Protocol->getReferencedProtocols(); | 
|  | 4457 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), | 
|  | 4458 | E = Protocols.end(); | 
|  | 4459 | I != E; ++I) | 
|  | 4460 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4461 | CurContext, Selectors, AllowSameLength, Results, false); | 
| Douglas Gregor | f37c949 | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 4462 | } | 
|  | 4463 |  | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4464 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); | 
|  | 4465 | if (!IFace) | 
|  | 4466 | return; | 
|  | 4467 |  | 
|  | 4468 | // Add methods in protocols. | 
|  | 4469 | const ObjCList<ObjCProtocolDecl> &Protocols= IFace->getReferencedProtocols(); | 
|  | 4470 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), | 
|  | 4471 | E = Protocols.end(); | 
|  | 4472 | I != E; ++I) | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4473 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4474 | CurContext, Selectors, AllowSameLength, Results, false); | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4475 |  | 
|  | 4476 | // Add methods in categories. | 
|  | 4477 | for (ObjCCategoryDecl *CatDecl = IFace->getCategoryList(); CatDecl; | 
|  | 4478 | CatDecl = CatDecl->getNextClassCategory()) { | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4479 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4480 | NumSelIdents, CurContext, Selectors, AllowSameLength, | 
|  | 4481 | Results, InOriginalClass); | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4482 |  | 
|  | 4483 | // Add a categories protocol methods. | 
|  | 4484 | const ObjCList<ObjCProtocolDecl> &Protocols | 
|  | 4485 | = CatDecl->getReferencedProtocols(); | 
|  | 4486 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), | 
|  | 4487 | E = Protocols.end(); | 
|  | 4488 | I != E; ++I) | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4489 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4490 | NumSelIdents, CurContext, Selectors, AllowSameLength, | 
|  | 4491 | Results, false); | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4492 |  | 
|  | 4493 | // Add methods in category implementations. | 
|  | 4494 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4495 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4496 | NumSelIdents, CurContext, Selectors, AllowSameLength, | 
|  | 4497 | Results, InOriginalClass); | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4498 | } | 
|  | 4499 |  | 
|  | 4500 | // Add methods in superclass. | 
|  | 4501 | if (IFace->getSuperClass()) | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4502 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4503 | SelIdents, NumSelIdents, CurContext, Selectors, | 
|  | 4504 | AllowSameLength, Results, false); | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4505 |  | 
|  | 4506 | // Add methods in our implementation, if any. | 
|  | 4507 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4508 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4509 | NumSelIdents, CurContext, Selectors, AllowSameLength, | 
|  | 4510 | Results, InOriginalClass); | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4511 | } | 
|  | 4512 |  | 
|  | 4513 |  | 
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4514 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4515 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4516 |  | 
|  | 4517 | // Try to find the interface where getters might live. | 
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4518 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4519 | if (!Class) { | 
|  | 4520 | if (ObjCCategoryDecl *Category | 
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4521 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4522 | Class = Category->getClassInterface(); | 
|  | 4523 |  | 
|  | 4524 | if (!Class) | 
|  | 4525 | return; | 
|  | 4526 | } | 
|  | 4527 |  | 
|  | 4528 | // Find all of the potential getters. | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4529 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 4530 | CodeCompletionContext::CCC_Other); | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4531 | Results.EnterNewScope(); | 
|  | 4532 |  | 
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4533 | VisitedSelectorSet Selectors; | 
|  | 4534 | AddObjCMethods(Class, true, MK_ZeroArgSelector, 0, 0, CurContext, Selectors, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4535 | /*AllowSameLength=*/true, Results); | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4536 | Results.ExitScope(); | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4537 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 4538 | CodeCompletionContext::CCC_Other, | 
|  | 4539 | Results.data(),Results.size()); | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4540 | } | 
|  | 4541 |  | 
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4542 | void Sema::CodeCompleteObjCPropertySetter(Scope *S) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4543 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4544 |  | 
|  | 4545 | // Try to find the interface where setters might live. | 
|  | 4546 | ObjCInterfaceDecl *Class | 
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4547 | = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4548 | if (!Class) { | 
|  | 4549 | if (ObjCCategoryDecl *Category | 
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4550 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4551 | Class = Category->getClassInterface(); | 
|  | 4552 |  | 
|  | 4553 | if (!Class) | 
|  | 4554 | return; | 
|  | 4555 | } | 
|  | 4556 |  | 
|  | 4557 | // Find all of the potential getters. | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4558 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 4559 | CodeCompletionContext::CCC_Other); | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4560 | Results.EnterNewScope(); | 
|  | 4561 |  | 
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4562 | VisitedSelectorSet Selectors; | 
|  | 4563 | AddObjCMethods(Class, true, MK_OneArgSelector, 0, 0, CurContext, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 4564 | Selectors, /*AllowSameLength=*/true, Results); | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4565 |  | 
|  | 4566 | Results.ExitScope(); | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4567 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 4568 | CodeCompletionContext::CCC_Other, | 
|  | 4569 | Results.data(),Results.size()); | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4570 | } | 
|  | 4571 |  | 
| Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 4572 | void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS, | 
|  | 4573 | bool IsParameter) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4574 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4575 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 4576 | CodeCompletionContext::CCC_Type); | 
| Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 4577 | Results.EnterNewScope(); | 
|  | 4578 |  | 
|  | 4579 | // Add context-sensitive, Objective-C parameter-passing keywords. | 
|  | 4580 | bool AddedInOut = false; | 
|  | 4581 | if ((DS.getObjCDeclQualifier() & | 
|  | 4582 | (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) { | 
|  | 4583 | Results.AddResult("in"); | 
|  | 4584 | Results.AddResult("inout"); | 
|  | 4585 | AddedInOut = true; | 
|  | 4586 | } | 
|  | 4587 | if ((DS.getObjCDeclQualifier() & | 
|  | 4588 | (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) { | 
|  | 4589 | Results.AddResult("out"); | 
|  | 4590 | if (!AddedInOut) | 
|  | 4591 | Results.AddResult("inout"); | 
|  | 4592 | } | 
|  | 4593 | if ((DS.getObjCDeclQualifier() & | 
|  | 4594 | (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref | | 
|  | 4595 | ObjCDeclSpec::DQ_Oneway)) == 0) { | 
|  | 4596 | Results.AddResult("bycopy"); | 
|  | 4597 | Results.AddResult("byref"); | 
|  | 4598 | Results.AddResult("oneway"); | 
|  | 4599 | } | 
|  | 4600 |  | 
| Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 4601 | // If we're completing the return type of an Objective-C method and the | 
|  | 4602 | // identifier IBAction refers to a macro, provide a completion item for | 
|  | 4603 | // an action, e.g., | 
|  | 4604 | //   IBAction)<#selector#>:(id)sender | 
|  | 4605 | if (DS.getObjCDeclQualifier() == 0 && !IsParameter && | 
|  | 4606 | Context.Idents.get("IBAction").hasMacroDefinition()) { | 
|  | 4607 | typedef CodeCompletionString::Chunk Chunk; | 
|  | 4608 | CodeCompletionBuilder Builder(Results.getAllocator(), CCP_CodePattern, | 
|  | 4609 | CXAvailability_Available); | 
|  | 4610 | Builder.AddTypedTextChunk("IBAction"); | 
|  | 4611 | Builder.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); | 
|  | 4612 | Builder.AddPlaceholderChunk("selector"); | 
|  | 4613 | Builder.AddChunk(Chunk(CodeCompletionString::CK_Colon)); | 
|  | 4614 | Builder.AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); | 
|  | 4615 | Builder.AddTextChunk("id"); | 
|  | 4616 | Builder.AddChunk(Chunk(CodeCompletionString::CK_RightParen)); | 
|  | 4617 | Builder.AddTextChunk("sender"); | 
|  | 4618 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); | 
|  | 4619 | } | 
|  | 4620 |  | 
| Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 4621 | // Add various builtin type names and specifiers. | 
|  | 4622 | AddOrdinaryNameResults(PCC_Type, S, *this, Results); | 
|  | 4623 | Results.ExitScope(); | 
|  | 4624 |  | 
|  | 4625 | // Add the various type names | 
|  | 4626 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); | 
|  | 4627 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | 
|  | 4628 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | 
|  | 4629 | CodeCompleter->includeGlobals()); | 
|  | 4630 |  | 
|  | 4631 | if (CodeCompleter->includeMacros()) | 
|  | 4632 | AddMacroResults(PP, Results); | 
|  | 4633 |  | 
|  | 4634 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 4635 | CodeCompletionContext::CCC_Type, | 
|  | 4636 | Results.data(), Results.size()); | 
|  | 4637 | } | 
|  | 4638 |  | 
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4639 | /// \brief When we have an expression with type "id", we may assume | 
|  | 4640 | /// that it has some more-specific class type based on knowledge of | 
|  | 4641 | /// common uses of Objective-C. This routine returns that class type, | 
|  | 4642 | /// or NULL if no better result could be determined. | 
|  | 4643 | static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) { | 
| Douglas Gregor | ed0b69d | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 4644 | ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E); | 
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4645 | if (!Msg) | 
|  | 4646 | return 0; | 
|  | 4647 |  | 
|  | 4648 | Selector Sel = Msg->getSelector(); | 
|  | 4649 | if (Sel.isNull()) | 
|  | 4650 | return 0; | 
|  | 4651 |  | 
|  | 4652 | IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0); | 
|  | 4653 | if (!Id) | 
|  | 4654 | return 0; | 
|  | 4655 |  | 
|  | 4656 | ObjCMethodDecl *Method = Msg->getMethodDecl(); | 
|  | 4657 | if (!Method) | 
|  | 4658 | return 0; | 
|  | 4659 |  | 
|  | 4660 | // Determine the class that we're sending the message to. | 
| Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 4661 | ObjCInterfaceDecl *IFace = 0; | 
|  | 4662 | switch (Msg->getReceiverKind()) { | 
|  | 4663 | case ObjCMessageExpr::Class: | 
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4664 | if (const ObjCObjectType *ObjType | 
|  | 4665 | = Msg->getClassReceiver()->getAs<ObjCObjectType>()) | 
|  | 4666 | IFace = ObjType->getInterface(); | 
| Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 4667 | break; | 
|  | 4668 |  | 
|  | 4669 | case ObjCMessageExpr::Instance: { | 
|  | 4670 | QualType T = Msg->getInstanceReceiver()->getType(); | 
|  | 4671 | if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) | 
|  | 4672 | IFace = Ptr->getInterfaceDecl(); | 
|  | 4673 | break; | 
|  | 4674 | } | 
|  | 4675 |  | 
|  | 4676 | case ObjCMessageExpr::SuperInstance: | 
|  | 4677 | case ObjCMessageExpr::SuperClass: | 
|  | 4678 | break; | 
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4679 | } | 
|  | 4680 |  | 
|  | 4681 | if (!IFace) | 
|  | 4682 | return 0; | 
|  | 4683 |  | 
|  | 4684 | ObjCInterfaceDecl *Super = IFace->getSuperClass(); | 
|  | 4685 | if (Method->isInstanceMethod()) | 
|  | 4686 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) | 
|  | 4687 | .Case("retain", IFace) | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4688 | .Case("strong", IFace) | 
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4689 | .Case("autorelease", IFace) | 
|  | 4690 | .Case("copy", IFace) | 
|  | 4691 | .Case("copyWithZone", IFace) | 
|  | 4692 | .Case("mutableCopy", IFace) | 
|  | 4693 | .Case("mutableCopyWithZone", IFace) | 
|  | 4694 | .Case("awakeFromCoder", IFace) | 
|  | 4695 | .Case("replacementObjectFromCoder", IFace) | 
|  | 4696 | .Case("class", IFace) | 
|  | 4697 | .Case("classForCoder", IFace) | 
|  | 4698 | .Case("superclass", Super) | 
|  | 4699 | .Default(0); | 
|  | 4700 |  | 
|  | 4701 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) | 
|  | 4702 | .Case("new", IFace) | 
|  | 4703 | .Case("alloc", IFace) | 
|  | 4704 | .Case("allocWithZone", IFace) | 
|  | 4705 | .Case("class", IFace) | 
|  | 4706 | .Case("superclass", Super) | 
|  | 4707 | .Default(0); | 
|  | 4708 | } | 
|  | 4709 |  | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4710 | // Add a special completion for a message send to "super", which fills in the | 
|  | 4711 | // most likely case of forwarding all of our arguments to the superclass | 
|  | 4712 | // function. | 
|  | 4713 | /// | 
|  | 4714 | /// \param S The semantic analysis object. | 
|  | 4715 | /// | 
|  | 4716 | /// \param S NeedSuperKeyword Whether we need to prefix this completion with | 
|  | 4717 | /// the "super" keyword. Otherwise, we just need to provide the arguments. | 
|  | 4718 | /// | 
|  | 4719 | /// \param SelIdents The identifiers in the selector that have already been | 
|  | 4720 | /// provided as arguments for a send to "super". | 
|  | 4721 | /// | 
|  | 4722 | /// \param NumSelIdents The number of identifiers in \p SelIdents. | 
|  | 4723 | /// | 
|  | 4724 | /// \param Results The set of results to augment. | 
|  | 4725 | /// | 
|  | 4726 | /// \returns the Objective-C method declaration that would be invoked by | 
|  | 4727 | /// this "super" completion. If NULL, no completion was added. | 
|  | 4728 | static ObjCMethodDecl *AddSuperSendCompletion(Sema &S, bool NeedSuperKeyword, | 
|  | 4729 | IdentifierInfo **SelIdents, | 
|  | 4730 | unsigned NumSelIdents, | 
|  | 4731 | ResultBuilder &Results) { | 
|  | 4732 | ObjCMethodDecl *CurMethod = S.getCurMethodDecl(); | 
|  | 4733 | if (!CurMethod) | 
|  | 4734 | return 0; | 
|  | 4735 |  | 
|  | 4736 | ObjCInterfaceDecl *Class = CurMethod->getClassInterface(); | 
|  | 4737 | if (!Class) | 
|  | 4738 | return 0; | 
|  | 4739 |  | 
|  | 4740 | // Try to find a superclass method with the same selector. | 
|  | 4741 | ObjCMethodDecl *SuperMethod = 0; | 
| Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 4742 | while ((Class = Class->getSuperClass()) && !SuperMethod) { | 
|  | 4743 | // Check in the class | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4744 | SuperMethod = Class->getMethod(CurMethod->getSelector(), | 
|  | 4745 | CurMethod->isInstanceMethod()); | 
|  | 4746 |  | 
| Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 4747 | // Check in categories or class extensions. | 
|  | 4748 | if (!SuperMethod) { | 
|  | 4749 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; | 
|  | 4750 | Category = Category->getNextClassCategory()) | 
|  | 4751 | if ((SuperMethod = Category->getMethod(CurMethod->getSelector(), | 
|  | 4752 | CurMethod->isInstanceMethod()))) | 
|  | 4753 | break; | 
|  | 4754 | } | 
|  | 4755 | } | 
|  | 4756 |  | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4757 | if (!SuperMethod) | 
|  | 4758 | return 0; | 
|  | 4759 |  | 
|  | 4760 | // Check whether the superclass method has the same signature. | 
|  | 4761 | if (CurMethod->param_size() != SuperMethod->param_size() || | 
|  | 4762 | CurMethod->isVariadic() != SuperMethod->isVariadic()) | 
|  | 4763 | return 0; | 
|  | 4764 |  | 
|  | 4765 | for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(), | 
|  | 4766 | CurPEnd = CurMethod->param_end(), | 
|  | 4767 | SuperP = SuperMethod->param_begin(); | 
|  | 4768 | CurP != CurPEnd; ++CurP, ++SuperP) { | 
|  | 4769 | // Make sure the parameter types are compatible. | 
|  | 4770 | if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(), | 
|  | 4771 | (*SuperP)->getType())) | 
|  | 4772 | return 0; | 
|  | 4773 |  | 
|  | 4774 | // Make sure we have a parameter name to forward! | 
|  | 4775 | if (!(*CurP)->getIdentifier()) | 
|  | 4776 | return 0; | 
|  | 4777 | } | 
|  | 4778 |  | 
|  | 4779 | // We have a superclass method. Now, form the send-to-super completion. | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4780 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4781 |  | 
|  | 4782 | // Give this completion a return type. | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 4783 | AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod, | 
|  | 4784 | Builder); | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4785 |  | 
|  | 4786 | // If we need the "super" keyword, add it (plus some spacing). | 
|  | 4787 | if (NeedSuperKeyword) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4788 | Builder.AddTypedTextChunk("super"); | 
|  | 4789 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4790 | } | 
|  | 4791 |  | 
|  | 4792 | Selector Sel = CurMethod->getSelector(); | 
|  | 4793 | if (Sel.isUnarySelector()) { | 
|  | 4794 | if (NeedSuperKeyword) | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4795 | Builder.AddTextChunk(Builder.getAllocator().CopyString( | 
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 4796 | Sel.getNameForSlot(0))); | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4797 | else | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4798 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( | 
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 4799 | Sel.getNameForSlot(0))); | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4800 | } else { | 
|  | 4801 | ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(); | 
|  | 4802 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) { | 
|  | 4803 | if (I > NumSelIdents) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4804 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4805 |  | 
|  | 4806 | if (I < NumSelIdents) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4807 | Builder.AddInformativeChunk( | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4808 | Builder.getAllocator().CopyString( | 
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 4809 | Sel.getNameForSlot(I) + ":")); | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4810 | else if (NeedSuperKeyword || I > NumSelIdents) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4811 | Builder.AddTextChunk( | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4812 | Builder.getAllocator().CopyString( | 
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 4813 | Sel.getNameForSlot(I) + ":")); | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4814 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4815 | (*CurP)->getIdentifier()->getName())); | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4816 | } else { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4817 | Builder.AddTypedTextChunk( | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4818 | Builder.getAllocator().CopyString( | 
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 4819 | Sel.getNameForSlot(I) + ":")); | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4820 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4821 | (*CurP)->getIdentifier()->getName())); | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4822 | } | 
|  | 4823 | } | 
|  | 4824 | } | 
|  | 4825 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4826 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), CCP_SuperCompletion, | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4827 | SuperMethod->isInstanceMethod() | 
|  | 4828 | ? CXCursor_ObjCInstanceMethodDecl | 
|  | 4829 | : CXCursor_ObjCClassMethodDecl)); | 
|  | 4830 | return SuperMethod; | 
|  | 4831 | } | 
|  | 4832 |  | 
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4833 | void Sema::CodeCompleteObjCMessageReceiver(Scope *S) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4834 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4835 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 4836 | CodeCompletionContext::CCC_ObjCMessageReceiver, | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4837 | &ResultBuilder::IsObjCMessageReceiver); | 
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4838 |  | 
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4839 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | 
|  | 4840 | Results.EnterNewScope(); | 
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4841 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | 
|  | 4842 | CodeCompleter->includeGlobals()); | 
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4843 |  | 
|  | 4844 | // If we are in an Objective-C method inside a class that has a superclass, | 
|  | 4845 | // add "super" as an option. | 
|  | 4846 | if (ObjCMethodDecl *Method = getCurMethodDecl()) | 
|  | 4847 | if (ObjCInterfaceDecl *Iface = Method->getClassInterface()) | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4848 | if (Iface->getSuperClass()) { | 
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4849 | Results.AddResult(Result("super")); | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4850 |  | 
|  | 4851 | AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, 0, 0, Results); | 
|  | 4852 | } | 
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4853 |  | 
|  | 4854 | Results.ExitScope(); | 
|  | 4855 |  | 
|  | 4856 | if (CodeCompleter->includeMacros()) | 
|  | 4857 | AddMacroResults(PP, Results); | 
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 4858 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4859 | Results.data(), Results.size()); | 
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4860 |  | 
|  | 4861 | } | 
|  | 4862 |  | 
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4863 | void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, | 
|  | 4864 | IdentifierInfo **SelIdents, | 
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4865 | unsigned NumSelIdents, | 
|  | 4866 | bool AtArgumentExpression) { | 
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4867 | ObjCInterfaceDecl *CDecl = 0; | 
|  | 4868 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { | 
|  | 4869 | // Figure out which interface we're in. | 
|  | 4870 | CDecl = CurMethod->getClassInterface(); | 
|  | 4871 | if (!CDecl) | 
|  | 4872 | return; | 
|  | 4873 |  | 
|  | 4874 | // Find the superclass of this class. | 
|  | 4875 | CDecl = CDecl->getSuperClass(); | 
|  | 4876 | if (!CDecl) | 
|  | 4877 | return; | 
|  | 4878 |  | 
|  | 4879 | if (CurMethod->isInstanceMethod()) { | 
|  | 4880 | // We are inside an instance method, which means that the message | 
|  | 4881 | // send [super ...] is actually calling an instance method on the | 
| Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 4882 | // current object. | 
|  | 4883 | return CodeCompleteObjCInstanceMessage(S, 0, | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4884 | SelIdents, NumSelIdents, | 
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4885 | AtArgumentExpression, | 
| Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 4886 | CDecl); | 
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4887 | } | 
|  | 4888 |  | 
|  | 4889 | // Fall through to send to the superclass in CDecl. | 
|  | 4890 | } else { | 
|  | 4891 | // "super" may be the name of a type or variable. Figure out which | 
|  | 4892 | // it is. | 
|  | 4893 | IdentifierInfo *Super = &Context.Idents.get("super"); | 
|  | 4894 | NamedDecl *ND = LookupSingleName(S, Super, SuperLoc, | 
|  | 4895 | LookupOrdinaryName); | 
|  | 4896 | if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) { | 
|  | 4897 | // "super" names an interface. Use it. | 
|  | 4898 | } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) { | 
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4899 | if (const ObjCObjectType *Iface | 
|  | 4900 | = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>()) | 
|  | 4901 | CDecl = Iface->getInterface(); | 
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4902 | } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) { | 
|  | 4903 | // "super" names an unresolved type; we can't be more specific. | 
|  | 4904 | } else { | 
|  | 4905 | // Assume that "super" names some kind of value and parse that way. | 
|  | 4906 | CXXScopeSpec SS; | 
|  | 4907 | UnqualifiedId id; | 
|  | 4908 | id.setIdentifier(Super, SuperLoc); | 
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4909 | ExprResult SuperExpr = ActOnIdExpression(S, SS, id, false, false); | 
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4910 | return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(), | 
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4911 | SelIdents, NumSelIdents, | 
|  | 4912 | AtArgumentExpression); | 
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4913 | } | 
|  | 4914 |  | 
|  | 4915 | // Fall through | 
|  | 4916 | } | 
|  | 4917 |  | 
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4918 | ParsedType Receiver; | 
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4919 | if (CDecl) | 
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4920 | Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl)); | 
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4921 | return CodeCompleteObjCClassMessage(S, Receiver, SelIdents, | 
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4922 | NumSelIdents, AtArgumentExpression, | 
|  | 4923 | /*IsSuper=*/true); | 
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4924 | } | 
|  | 4925 |  | 
| Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 4926 | /// \brief Given a set of code-completion results for the argument of a message | 
|  | 4927 | /// send, determine the preferred type (if any) for that argument expression. | 
|  | 4928 | static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results, | 
|  | 4929 | unsigned NumSelIdents) { | 
|  | 4930 | typedef CodeCompletionResult Result; | 
|  | 4931 | ASTContext &Context = Results.getSema().Context; | 
|  | 4932 |  | 
|  | 4933 | QualType PreferredType; | 
|  | 4934 | unsigned BestPriority = CCP_Unlikely * 2; | 
|  | 4935 | Result *ResultsData = Results.data(); | 
|  | 4936 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { | 
|  | 4937 | Result &R = ResultsData[I]; | 
|  | 4938 | if (R.Kind == Result::RK_Declaration && | 
|  | 4939 | isa<ObjCMethodDecl>(R.Declaration)) { | 
|  | 4940 | if (R.Priority <= BestPriority) { | 
|  | 4941 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration); | 
|  | 4942 | if (NumSelIdents <= Method->param_size()) { | 
|  | 4943 | QualType MyPreferredType = Method->param_begin()[NumSelIdents - 1] | 
|  | 4944 | ->getType(); | 
|  | 4945 | if (R.Priority < BestPriority || PreferredType.isNull()) { | 
|  | 4946 | BestPriority = R.Priority; | 
|  | 4947 | PreferredType = MyPreferredType; | 
|  | 4948 | } else if (!Context.hasSameUnqualifiedType(PreferredType, | 
|  | 4949 | MyPreferredType)) { | 
|  | 4950 | PreferredType = QualType(); | 
|  | 4951 | } | 
|  | 4952 | } | 
|  | 4953 | } | 
|  | 4954 | } | 
|  | 4955 | } | 
|  | 4956 |  | 
|  | 4957 | return PreferredType; | 
|  | 4958 | } | 
|  | 4959 |  | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4960 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, | 
|  | 4961 | ParsedType Receiver, | 
|  | 4962 | IdentifierInfo **SelIdents, | 
|  | 4963 | unsigned NumSelIdents, | 
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4964 | bool AtArgumentExpression, | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4965 | bool IsSuper, | 
|  | 4966 | ResultBuilder &Results) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4967 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4968 | ObjCInterfaceDecl *CDecl = 0; | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4969 |  | 
| Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4970 | // If the given name refers to an interface type, retrieve the | 
|  | 4971 | // corresponding declaration. | 
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4972 | if (Receiver) { | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4973 | QualType T = SemaRef.GetTypeFromParser(Receiver, 0); | 
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4974 | if (!T.isNull()) | 
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4975 | if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>()) | 
|  | 4976 | CDecl = Interface->getInterface(); | 
| Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4977 | } | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4978 |  | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4979 | // Add all of the factory methods in this Objective-C class, its protocols, | 
|  | 4980 | // superclasses, categories, implementation, etc. | 
| Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4981 | Results.EnterNewScope(); | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4982 |  | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4983 | // If this is a send-to-super, try to add the special "super" send | 
|  | 4984 | // completion. | 
|  | 4985 | if (IsSuper) { | 
|  | 4986 | if (ObjCMethodDecl *SuperMethod | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4987 | = AddSuperSendCompletion(SemaRef, false, SelIdents, NumSelIdents, | 
|  | 4988 | Results)) | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4989 | Results.Ignore(SuperMethod); | 
|  | 4990 | } | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4991 |  | 
| Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 4992 | // If we're inside an Objective-C method definition, prefer its selector to | 
|  | 4993 | // others. | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4994 | if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl()) | 
| Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 4995 | Results.setPreferredSelector(CurMethod->getSelector()); | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4996 |  | 
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4997 | VisitedSelectorSet Selectors; | 
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4998 | if (CDecl) | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4999 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, NumSelIdents, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5000 | SemaRef.CurContext, Selectors, AtArgumentExpression, | 
|  | 5001 | Results); | 
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5002 | else { | 
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5003 | // We're messaging "id" as a type; provide all class/factory methods. | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5004 |  | 
| Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5005 | // If we have an external source, load the entire class method | 
| Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 5006 | // pool from the AST file. | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5007 | if (SemaRef.ExternalSource) { | 
|  | 5008 | for (uint32_t I = 0, | 
|  | 5009 | N = SemaRef.ExternalSource->GetNumExternalSelectors(); | 
| John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 5010 | I != N; ++I) { | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5011 | Selector Sel = SemaRef.ExternalSource->GetExternalSelector(I); | 
|  | 5012 | if (Sel.isNull() || SemaRef.MethodPool.count(Sel)) | 
| Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5013 | continue; | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5014 |  | 
|  | 5015 | SemaRef.ReadMethodPool(Sel); | 
| Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5016 | } | 
|  | 5017 | } | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5018 |  | 
|  | 5019 | for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(), | 
|  | 5020 | MEnd = SemaRef.MethodPool.end(); | 
| Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5021 | M != MEnd; ++M) { | 
|  | 5022 | for (ObjCMethodList *MethList = &M->second.second; | 
|  | 5023 | MethList && MethList->Method; | 
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5024 | MethList = MethList->Next) { | 
|  | 5025 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, | 
|  | 5026 | NumSelIdents)) | 
|  | 5027 | continue; | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5028 |  | 
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5029 | Result R(MethList->Method, 0); | 
|  | 5030 | R.StartParameter = NumSelIdents; | 
|  | 5031 | R.AllParametersAreInformative = false; | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5032 | Results.MaybeAddResult(R, SemaRef.CurContext); | 
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5033 | } | 
|  | 5034 | } | 
|  | 5035 | } | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5036 |  | 
|  | 5037 | Results.ExitScope(); | 
|  | 5038 | } | 
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5039 |  | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5040 | void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, | 
|  | 5041 | IdentifierInfo **SelIdents, | 
|  | 5042 | unsigned NumSelIdents, | 
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5043 | bool AtArgumentExpression, | 
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 5044 | bool IsSuper) { | 
| Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5045 |  | 
|  | 5046 | QualType T = this->GetTypeFromParser(Receiver); | 
|  | 5047 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5048 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
| Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5049 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage, | 
| Douglas Gregor | ea77740 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 5050 | T, SelIdents, NumSelIdents)); | 
| Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5051 |  | 
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5052 | AddClassMessageCompletions(*this, S, Receiver, SelIdents, NumSelIdents, | 
|  | 5053 | AtArgumentExpression, IsSuper, Results); | 
| Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5054 |  | 
|  | 5055 | // If we're actually at the argument expression (rather than prior to the | 
|  | 5056 | // selector), we're actually performing code completion for an expression. | 
|  | 5057 | // Determine whether we have a single, best method. If so, we can | 
|  | 5058 | // code-complete the expression using the corresponding parameter type as | 
|  | 5059 | // our preferred type, improving completion results. | 
|  | 5060 | if (AtArgumentExpression) { | 
|  | 5061 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, | 
| Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5062 | NumSelIdents); | 
| Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5063 | if (PreferredType.isNull()) | 
|  | 5064 | CodeCompleteOrdinaryName(S, PCC_Expression); | 
|  | 5065 | else | 
|  | 5066 | CodeCompleteExpression(S, PreferredType); | 
|  | 5067 | return; | 
|  | 5068 | } | 
|  | 5069 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5070 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5071 | Results.getCompletionContext(), | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5072 | Results.data(), Results.size()); | 
| Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5073 | } | 
|  | 5074 |  | 
| Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 5075 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver, | 
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5076 | IdentifierInfo **SelIdents, | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5077 | unsigned NumSelIdents, | 
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5078 | bool AtArgumentExpression, | 
| Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5079 | ObjCInterfaceDecl *Super) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5080 | typedef CodeCompletionResult Result; | 
| Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5081 |  | 
|  | 5082 | Expr *RecExpr = static_cast<Expr *>(Receiver); | 
| Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5083 |  | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5084 | // If necessary, apply function/array conversion to the receiver. | 
|  | 5085 | // C99 6.7.5.3p[7,8]. | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5086 | if (RecExpr) { | 
|  | 5087 | ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr); | 
|  | 5088 | if (Conv.isInvalid()) // conversion failed. bail. | 
|  | 5089 | return; | 
|  | 5090 | RecExpr = Conv.take(); | 
|  | 5091 | } | 
| Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5092 | QualType ReceiverType = RecExpr? RecExpr->getType() | 
|  | 5093 | : Super? Context.getObjCObjectPointerType( | 
|  | 5094 | Context.getObjCInterfaceType(Super)) | 
|  | 5095 | : Context.getObjCIdType(); | 
| Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5096 |  | 
| Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 5097 | // If we're messaging an expression with type "id" or "Class", check | 
|  | 5098 | // whether we know something special about the receiver that allows | 
|  | 5099 | // us to assume a more-specific receiver type. | 
|  | 5100 | if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) | 
|  | 5101 | if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) { | 
|  | 5102 | if (ReceiverType->isObjCClassType()) | 
|  | 5103 | return CodeCompleteObjCClassMessage(S, | 
|  | 5104 | ParsedType::make(Context.getObjCInterfaceType(IFace)), | 
|  | 5105 | SelIdents, NumSelIdents, | 
|  | 5106 | AtArgumentExpression, Super); | 
|  | 5107 |  | 
|  | 5108 | ReceiverType = Context.getObjCObjectPointerType( | 
|  | 5109 | Context.getObjCInterfaceType(IFace)); | 
|  | 5110 | } | 
|  | 5111 |  | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5112 | // Build the set of methods we can see. | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5113 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
| Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5114 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage, | 
| Douglas Gregor | ea77740 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 5115 | ReceiverType, SelIdents, NumSelIdents)); | 
| Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5116 |  | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5117 | Results.EnterNewScope(); | 
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5118 |  | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5119 | // If this is a send-to-super, try to add the special "super" send | 
|  | 5120 | // completion. | 
| Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5121 | if (Super) { | 
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5122 | if (ObjCMethodDecl *SuperMethod | 
|  | 5123 | = AddSuperSendCompletion(*this, false, SelIdents, NumSelIdents, | 
|  | 5124 | Results)) | 
|  | 5125 | Results.Ignore(SuperMethod); | 
|  | 5126 | } | 
|  | 5127 |  | 
| Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 5128 | // If we're inside an Objective-C method definition, prefer its selector to | 
|  | 5129 | // others. | 
|  | 5130 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) | 
|  | 5131 | Results.setPreferredSelector(CurMethod->getSelector()); | 
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5132 |  | 
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5133 | // Keep track of the selectors we've already added. | 
|  | 5134 | VisitedSelectorSet Selectors; | 
|  | 5135 |  | 
| Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5136 | // Handle messages to Class. This really isn't a message to an instance | 
|  | 5137 | // method, so we treat it the same way we would treat a message send to a | 
|  | 5138 | // class method. | 
|  | 5139 | if (ReceiverType->isObjCClassType() || | 
|  | 5140 | ReceiverType->isObjCQualifiedClassType()) { | 
|  | 5141 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { | 
|  | 5142 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5143 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, NumSelIdents, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5144 | CurContext, Selectors, AtArgumentExpression, Results); | 
| Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5145 | } | 
|  | 5146 | } | 
|  | 5147 | // Handle messages to a qualified ID ("id<foo>"). | 
|  | 5148 | else if (const ObjCObjectPointerType *QualID | 
|  | 5149 | = ReceiverType->getAsObjCQualifiedIdType()) { | 
|  | 5150 | // Search protocols for instance methods. | 
|  | 5151 | for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(), | 
|  | 5152 | E = QualID->qual_end(); | 
|  | 5153 | I != E; ++I) | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5154 | AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5155 | Selectors, AtArgumentExpression, Results); | 
| Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5156 | } | 
|  | 5157 | // Handle messages to a pointer to interface type. | 
|  | 5158 | else if (const ObjCObjectPointerType *IFacePtr | 
|  | 5159 | = ReceiverType->getAsObjCInterfacePointerType()) { | 
|  | 5160 | // Search the class, its superclasses, etc., for instance methods. | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5161 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5162 | NumSelIdents, CurContext, Selectors, AtArgumentExpression, | 
|  | 5163 | Results); | 
| Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5164 |  | 
|  | 5165 | // Search protocols for instance methods. | 
|  | 5166 | for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(), | 
|  | 5167 | E = IFacePtr->qual_end(); | 
|  | 5168 | I != E; ++I) | 
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5169 | AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext, | 
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5170 | Selectors, AtArgumentExpression, Results); | 
| Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 5171 | } | 
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5172 | // Handle messages to "id". | 
|  | 5173 | else if (ReceiverType->isObjCIdType()) { | 
| Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5174 | // We're messaging "id", so provide all instance methods we know | 
|  | 5175 | // about as code-completion results. | 
|  | 5176 |  | 
|  | 5177 | // If we have an external source, load the entire class method | 
| Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 5178 | // pool from the AST file. | 
| Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5179 | if (ExternalSource) { | 
| John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 5180 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); | 
|  | 5181 | I != N; ++I) { | 
|  | 5182 | Selector Sel = ExternalSource->GetExternalSelector(I); | 
| Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5183 | if (Sel.isNull() || MethodPool.count(Sel)) | 
| Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5184 | continue; | 
|  | 5185 |  | 
| Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5186 | ReadMethodPool(Sel); | 
| Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 5187 | } | 
|  | 5188 | } | 
|  | 5189 |  | 
| Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5190 | for (GlobalMethodPool::iterator M = MethodPool.begin(), | 
|  | 5191 | MEnd = MethodPool.end(); | 
|  | 5192 | M != MEnd; ++M) { | 
|  | 5193 | for (ObjCMethodList *MethList = &M->second.first; | 
|  | 5194 | MethList && MethList->Method; | 
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5195 | MethList = MethList->Next) { | 
|  | 5196 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, | 
|  | 5197 | NumSelIdents)) | 
|  | 5198 | continue; | 
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5199 |  | 
|  | 5200 | if (!Selectors.insert(MethList->Method->getSelector())) | 
|  | 5201 | continue; | 
|  | 5202 |  | 
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 5203 | Result R(MethList->Method, 0); | 
|  | 5204 | R.StartParameter = NumSelIdents; | 
|  | 5205 | R.AllParametersAreInformative = false; | 
|  | 5206 | Results.MaybeAddResult(R, CurContext); | 
|  | 5207 | } | 
|  | 5208 | } | 
|  | 5209 | } | 
| Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5210 | Results.ExitScope(); | 
| Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 5211 |  | 
|  | 5212 |  | 
|  | 5213 | // If we're actually at the argument expression (rather than prior to the | 
|  | 5214 | // selector), we're actually performing code completion for an expression. | 
|  | 5215 | // Determine whether we have a single, best method. If so, we can | 
|  | 5216 | // code-complete the expression using the corresponding parameter type as | 
|  | 5217 | // our preferred type, improving completion results. | 
|  | 5218 | if (AtArgumentExpression) { | 
|  | 5219 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, | 
|  | 5220 | NumSelIdents); | 
|  | 5221 | if (PreferredType.isNull()) | 
|  | 5222 | CodeCompleteOrdinaryName(S, PCC_Expression); | 
|  | 5223 | else | 
|  | 5224 | CodeCompleteExpression(S, PreferredType); | 
|  | 5225 | return; | 
|  | 5226 | } | 
|  | 5227 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5228 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 5229 | Results.getCompletionContext(), | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5230 | Results.data(),Results.size()); | 
| Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5231 | } | 
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5232 |  | 
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 5233 | void Sema::CodeCompleteObjCForCollection(Scope *S, | 
|  | 5234 | DeclGroupPtrTy IterationVar) { | 
|  | 5235 | CodeCompleteExpressionData Data; | 
|  | 5236 | Data.ObjCCollection = true; | 
|  | 5237 |  | 
|  | 5238 | if (IterationVar.getAsOpaquePtr()) { | 
|  | 5239 | DeclGroupRef DG = IterationVar.getAsVal<DeclGroupRef>(); | 
|  | 5240 | for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) { | 
|  | 5241 | if (*I) | 
|  | 5242 | Data.IgnoreDecls.push_back(*I); | 
|  | 5243 | } | 
|  | 5244 | } | 
|  | 5245 |  | 
|  | 5246 | CodeCompleteExpression(S, Data); | 
|  | 5247 | } | 
|  | 5248 |  | 
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5249 | void Sema::CodeCompleteObjCSelector(Scope *S, IdentifierInfo **SelIdents, | 
|  | 5250 | unsigned NumSelIdents) { | 
|  | 5251 | // If we have an external source, load the entire class method | 
|  | 5252 | // pool from the AST file. | 
|  | 5253 | if (ExternalSource) { | 
|  | 5254 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); | 
|  | 5255 | I != N; ++I) { | 
|  | 5256 | Selector Sel = ExternalSource->GetExternalSelector(I); | 
|  | 5257 | if (Sel.isNull() || MethodPool.count(Sel)) | 
|  | 5258 | continue; | 
|  | 5259 |  | 
|  | 5260 | ReadMethodPool(Sel); | 
|  | 5261 | } | 
|  | 5262 | } | 
|  | 5263 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5264 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 5265 | CodeCompletionContext::CCC_SelectorName); | 
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5266 | Results.EnterNewScope(); | 
|  | 5267 | for (GlobalMethodPool::iterator M = MethodPool.begin(), | 
|  | 5268 | MEnd = MethodPool.end(); | 
|  | 5269 | M != MEnd; ++M) { | 
|  | 5270 |  | 
|  | 5271 | Selector Sel = M->first; | 
|  | 5272 | if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents, NumSelIdents)) | 
|  | 5273 | continue; | 
|  | 5274 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5275 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5276 | if (Sel.isUnarySelector()) { | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5277 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( | 
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5278 | Sel.getNameForSlot(0))); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5279 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5280 | continue; | 
|  | 5281 | } | 
|  | 5282 |  | 
| Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5283 | std::string Accumulator; | 
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5284 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) { | 
| Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5285 | if (I == NumSelIdents) { | 
|  | 5286 | if (!Accumulator.empty()) { | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5287 | Builder.AddInformativeChunk(Builder.getAllocator().CopyString( | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5288 | Accumulator)); | 
| Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5289 | Accumulator.clear(); | 
|  | 5290 | } | 
|  | 5291 | } | 
|  | 5292 |  | 
| Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 5293 | Accumulator += Sel.getNameForSlot(I); | 
| Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 5294 | Accumulator += ':'; | 
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5295 | } | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5296 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator)); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5297 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5298 | } | 
|  | 5299 | Results.ExitScope(); | 
|  | 5300 |  | 
|  | 5301 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 5302 | CodeCompletionContext::CCC_SelectorName, | 
|  | 5303 | Results.data(), Results.size()); | 
|  | 5304 | } | 
|  | 5305 |  | 
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5306 | /// \brief Add all of the protocol declarations that we find in the given | 
|  | 5307 | /// (translation unit) context. | 
|  | 5308 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, | 
| Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5309 | bool OnlyForwardDeclarations, | 
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5310 | ResultBuilder &Results) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5311 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5312 |  | 
|  | 5313 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), | 
|  | 5314 | DEnd = Ctx->decls_end(); | 
|  | 5315 | D != DEnd; ++D) { | 
|  | 5316 | // Record any protocols we find. | 
|  | 5317 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*D)) | 
| Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5318 | if (!OnlyForwardDeclarations || Proto->isForwardDecl()) | 
| Douglas Gregor | fc59ce1 | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5319 | Results.AddResult(Result(Proto, 0), CurContext, 0, false); | 
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5320 |  | 
|  | 5321 | // Record any forward-declared protocols we find. | 
|  | 5322 | if (ObjCForwardProtocolDecl *Forward | 
|  | 5323 | = dyn_cast<ObjCForwardProtocolDecl>(*D)) { | 
|  | 5324 | for (ObjCForwardProtocolDecl::protocol_iterator | 
|  | 5325 | P = Forward->protocol_begin(), | 
|  | 5326 | PEnd = Forward->protocol_end(); | 
|  | 5327 | P != PEnd; ++P) | 
| Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5328 | if (!OnlyForwardDeclarations || (*P)->isForwardDecl()) | 
| Douglas Gregor | fc59ce1 | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5329 | Results.AddResult(Result(*P, 0), CurContext, 0, false); | 
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5330 | } | 
|  | 5331 | } | 
|  | 5332 | } | 
|  | 5333 |  | 
|  | 5334 | void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols, | 
|  | 5335 | unsigned NumProtocols) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5336 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 5337 | CodeCompletionContext::CCC_ObjCProtocolName); | 
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5338 |  | 
| Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5339 | if (CodeCompleter && CodeCompleter->includeGlobals()) { | 
|  | 5340 | Results.EnterNewScope(); | 
|  | 5341 |  | 
|  | 5342 | // Tell the result set to ignore all of the protocols we have | 
|  | 5343 | // already seen. | 
|  | 5344 | // FIXME: This doesn't work when caching code-completion results. | 
|  | 5345 | for (unsigned I = 0; I != NumProtocols; ++I) | 
|  | 5346 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first, | 
|  | 5347 | Protocols[I].second)) | 
|  | 5348 | Results.Ignore(Protocol); | 
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5349 |  | 
| Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5350 | // Add all protocols. | 
|  | 5351 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, | 
|  | 5352 | Results); | 
| Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5353 |  | 
| Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5354 | Results.ExitScope(); | 
|  | 5355 | } | 
|  | 5356 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5357 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 5358 | CodeCompletionContext::CCC_ObjCProtocolName, | 
|  | 5359 | Results.data(),Results.size()); | 
| Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5360 | } | 
|  | 5361 |  | 
|  | 5362 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5363 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 5364 | CodeCompletionContext::CCC_ObjCProtocolName); | 
| Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 5365 |  | 
| Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5366 | if (CodeCompleter && CodeCompleter->includeGlobals()) { | 
|  | 5367 | Results.EnterNewScope(); | 
|  | 5368 |  | 
|  | 5369 | // Add all protocols. | 
|  | 5370 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, | 
|  | 5371 | Results); | 
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5372 |  | 
| Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 5373 | Results.ExitScope(); | 
|  | 5374 | } | 
|  | 5375 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5376 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 5377 | CodeCompletionContext::CCC_ObjCProtocolName, | 
|  | 5378 | Results.data(),Results.size()); | 
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 5379 | } | 
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5380 |  | 
|  | 5381 | /// \brief Add all of the Objective-C interface declarations that we find in | 
|  | 5382 | /// the given (translation unit) context. | 
|  | 5383 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, | 
|  | 5384 | bool OnlyForwardDeclarations, | 
|  | 5385 | bool OnlyUnimplemented, | 
|  | 5386 | ResultBuilder &Results) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5387 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5388 |  | 
|  | 5389 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), | 
|  | 5390 | DEnd = Ctx->decls_end(); | 
|  | 5391 | D != DEnd; ++D) { | 
| Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 5392 | // Record any interfaces we find. | 
|  | 5393 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*D)) | 
|  | 5394 | if ((!OnlyForwardDeclarations || Class->isForwardDecl()) && | 
|  | 5395 | (!OnlyUnimplemented || !Class->getImplementation())) | 
|  | 5396 | Results.AddResult(Result(Class, 0), CurContext, 0, false); | 
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5397 |  | 
|  | 5398 | // Record any forward-declared interfaces we find. | 
|  | 5399 | if (ObjCClassDecl *Forward = dyn_cast<ObjCClassDecl>(*D)) { | 
| Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 5400 | ObjCInterfaceDecl *IDecl = Forward->getForwardInterfaceDecl(); | 
|  | 5401 | if ((!OnlyForwardDeclarations || IDecl->isForwardDecl()) && | 
|  | 5402 | (!OnlyUnimplemented || !IDecl->getImplementation())) | 
|  | 5403 | Results.AddResult(Result(IDecl, 0), CurContext, | 
|  | 5404 | 0, false); | 
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5405 | } | 
|  | 5406 | } | 
|  | 5407 | } | 
|  | 5408 |  | 
|  | 5409 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5410 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 5411 | CodeCompletionContext::CCC_Other); | 
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5412 | Results.EnterNewScope(); | 
|  | 5413 |  | 
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5414 | if (CodeCompleter->includeGlobals()) { | 
|  | 5415 | // Add all classes. | 
|  | 5416 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, | 
|  | 5417 | false, Results); | 
|  | 5418 | } | 
|  | 5419 |  | 
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5420 | Results.ExitScope(); | 
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5421 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5422 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5423 | CodeCompletionContext::CCC_ObjCInterfaceName, | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5424 | Results.data(),Results.size()); | 
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5425 | } | 
|  | 5426 |  | 
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5427 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName, | 
|  | 5428 | SourceLocation ClassNameLoc) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5429 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5430 | CodeCompletionContext::CCC_ObjCInterfaceName); | 
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5431 | Results.EnterNewScope(); | 
|  | 5432 |  | 
|  | 5433 | // Make sure that we ignore the class we're currently defining. | 
|  | 5434 | NamedDecl *CurClass | 
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5435 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); | 
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5436 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) | 
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5437 | Results.Ignore(CurClass); | 
|  | 5438 |  | 
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5439 | if (CodeCompleter->includeGlobals()) { | 
|  | 5440 | // Add all classes. | 
|  | 5441 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, | 
|  | 5442 | false, Results); | 
|  | 5443 | } | 
|  | 5444 |  | 
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5445 | Results.ExitScope(); | 
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5446 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5447 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5448 | CodeCompletionContext::CCC_ObjCInterfaceName, | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5449 | Results.data(),Results.size()); | 
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5450 | } | 
|  | 5451 |  | 
|  | 5452 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5453 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 5454 | CodeCompletionContext::CCC_Other); | 
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5455 | Results.EnterNewScope(); | 
|  | 5456 |  | 
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5457 | if (CodeCompleter->includeGlobals()) { | 
|  | 5458 | // Add all unimplemented classes. | 
|  | 5459 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, | 
|  | 5460 | true, Results); | 
|  | 5461 | } | 
|  | 5462 |  | 
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5463 | Results.ExitScope(); | 
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5464 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5465 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 5466 | CodeCompletionContext::CCC_ObjCInterfaceName, | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5467 | Results.data(),Results.size()); | 
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 5468 | } | 
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5469 |  | 
|  | 5470 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, | 
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5471 | IdentifierInfo *ClassName, | 
|  | 5472 | SourceLocation ClassNameLoc) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5473 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5474 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5475 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
| Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 5476 | CodeCompletionContext::CCC_ObjCCategoryName); | 
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5477 |  | 
|  | 5478 | // Ignore any categories we find that have already been implemented by this | 
|  | 5479 | // interface. | 
|  | 5480 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; | 
|  | 5481 | NamedDecl *CurClass | 
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5482 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); | 
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5483 | if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)) | 
|  | 5484 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; | 
|  | 5485 | Category = Category->getNextClassCategory()) | 
|  | 5486 | CategoryNames.insert(Category->getIdentifier()); | 
|  | 5487 |  | 
|  | 5488 | // Add all of the categories we know about. | 
|  | 5489 | Results.EnterNewScope(); | 
|  | 5490 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); | 
|  | 5491 | for (DeclContext::decl_iterator D = TU->decls_begin(), | 
|  | 5492 | DEnd = TU->decls_end(); | 
|  | 5493 | D != DEnd; ++D) | 
|  | 5494 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(*D)) | 
|  | 5495 | if (CategoryNames.insert(Category->getIdentifier())) | 
| Douglas Gregor | fc59ce1 | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5496 | Results.AddResult(Result(Category, 0), CurContext, 0, false); | 
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5497 | Results.ExitScope(); | 
|  | 5498 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5499 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 5500 | CodeCompletionContext::CCC_ObjCCategoryName, | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5501 | Results.data(),Results.size()); | 
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5502 | } | 
|  | 5503 |  | 
|  | 5504 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, | 
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5505 | IdentifierInfo *ClassName, | 
|  | 5506 | SourceLocation ClassNameLoc) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5507 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5508 |  | 
|  | 5509 | // Find the corresponding interface. If we couldn't find the interface, the | 
|  | 5510 | // program itself is ill-formed. However, we'll try to be helpful still by | 
|  | 5511 | // providing the list of all of the categories we know about. | 
|  | 5512 | NamedDecl *CurClass | 
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5513 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); | 
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5514 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); | 
|  | 5515 | if (!Class) | 
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5516 | return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc); | 
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5517 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5518 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
| Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 5519 | CodeCompletionContext::CCC_ObjCCategoryName); | 
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5520 |  | 
|  | 5521 | // Add all of the categories that have have corresponding interface | 
|  | 5522 | // declarations in this class and any of its superclasses, except for | 
|  | 5523 | // already-implemented categories in the class itself. | 
|  | 5524 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; | 
|  | 5525 | Results.EnterNewScope(); | 
|  | 5526 | bool IgnoreImplemented = true; | 
|  | 5527 | while (Class) { | 
|  | 5528 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; | 
|  | 5529 | Category = Category->getNextClassCategory()) | 
|  | 5530 | if ((!IgnoreImplemented || !Category->getImplementation()) && | 
|  | 5531 | CategoryNames.insert(Category->getIdentifier())) | 
| Douglas Gregor | fc59ce1 | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5532 | Results.AddResult(Result(Category, 0), CurContext, 0, false); | 
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5533 |  | 
|  | 5534 | Class = Class->getSuperClass(); | 
|  | 5535 | IgnoreImplemented = false; | 
|  | 5536 | } | 
|  | 5537 | Results.ExitScope(); | 
|  | 5538 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5539 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 5540 | CodeCompletionContext::CCC_ObjCCategoryName, | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5541 | Results.data(),Results.size()); | 
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5542 | } | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5543 |  | 
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5544 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5545 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5546 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 5547 | CodeCompletionContext::CCC_Other); | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5548 |  | 
|  | 5549 | // Figure out where this @synthesize lives. | 
|  | 5550 | ObjCContainerDecl *Container | 
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5551 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5552 | if (!Container || | 
|  | 5553 | (!isa<ObjCImplementationDecl>(Container) && | 
|  | 5554 | !isa<ObjCCategoryImplDecl>(Container))) | 
|  | 5555 | return; | 
|  | 5556 |  | 
|  | 5557 | // Ignore any properties that have already been implemented. | 
|  | 5558 | for (DeclContext::decl_iterator D = Container->decls_begin(), | 
|  | 5559 | DEnd = Container->decls_end(); | 
|  | 5560 | D != DEnd; ++D) | 
|  | 5561 | if (ObjCPropertyImplDecl *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(*D)) | 
|  | 5562 | Results.Ignore(PropertyImpl->getPropertyDecl()); | 
|  | 5563 |  | 
|  | 5564 | // Add any properties that we find. | 
| Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 5565 | AddedPropertiesSet AddedProperties; | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5566 | Results.EnterNewScope(); | 
|  | 5567 | if (ObjCImplementationDecl *ClassImpl | 
|  | 5568 | = dyn_cast<ObjCImplementationDecl>(Container)) | 
| Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 5569 | AddObjCProperties(ClassImpl->getClassInterface(), false, | 
|  | 5570 | /*AllowNullaryMethods=*/false, CurContext, | 
| Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 5571 | AddedProperties, Results); | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5572 | else | 
|  | 5573 | AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), | 
| Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 5574 | false, /*AllowNullaryMethods=*/false, CurContext, | 
|  | 5575 | AddedProperties, Results); | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5576 | Results.ExitScope(); | 
|  | 5577 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5578 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 5579 | CodeCompletionContext::CCC_Other, | 
|  | 5580 | Results.data(),Results.size()); | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5581 | } | 
|  | 5582 |  | 
|  | 5583 | void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S, | 
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5584 | IdentifierInfo *PropertyName) { | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5585 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5586 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 5587 | CodeCompletionContext::CCC_Other); | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5588 |  | 
|  | 5589 | // Figure out where this @synthesize lives. | 
|  | 5590 | ObjCContainerDecl *Container | 
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5591 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5592 | if (!Container || | 
|  | 5593 | (!isa<ObjCImplementationDecl>(Container) && | 
|  | 5594 | !isa<ObjCCategoryImplDecl>(Container))) | 
|  | 5595 | return; | 
|  | 5596 |  | 
|  | 5597 | // Figure out which interface we're looking into. | 
|  | 5598 | ObjCInterfaceDecl *Class = 0; | 
|  | 5599 | if (ObjCImplementationDecl *ClassImpl | 
|  | 5600 | = dyn_cast<ObjCImplementationDecl>(Container)) | 
|  | 5601 | Class = ClassImpl->getClassInterface(); | 
|  | 5602 | else | 
|  | 5603 | Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl() | 
|  | 5604 | ->getClassInterface(); | 
|  | 5605 |  | 
| Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 5606 | // Determine the type of the property we're synthesizing. | 
|  | 5607 | QualType PropertyType = Context.getObjCIdType(); | 
|  | 5608 | if (Class) { | 
|  | 5609 | if (ObjCPropertyDecl *Property | 
|  | 5610 | = Class->FindPropertyDeclaration(PropertyName)) { | 
|  | 5611 | PropertyType | 
|  | 5612 | = Property->getType().getNonReferenceType().getUnqualifiedType(); | 
|  | 5613 |  | 
|  | 5614 | // Give preference to ivars | 
|  | 5615 | Results.setPreferredType(PropertyType); | 
|  | 5616 | } | 
|  | 5617 | } | 
|  | 5618 |  | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5619 | // Add all of the instance variables in this class and its superclasses. | 
|  | 5620 | Results.EnterNewScope(); | 
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5621 | bool SawSimilarlyNamedIvar = false; | 
|  | 5622 | std::string NameWithPrefix; | 
|  | 5623 | NameWithPrefix += '_'; | 
| Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 5624 | NameWithPrefix += PropertyName->getName(); | 
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5625 | std::string NameWithSuffix = PropertyName->getName().str(); | 
|  | 5626 | NameWithSuffix += '_'; | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5627 | for(; Class; Class = Class->getSuperClass()) { | 
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5628 | for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar; | 
|  | 5629 | Ivar = Ivar->getNextIvar()) { | 
| Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 5630 | Results.AddResult(Result(Ivar, 0), CurContext, 0, false); | 
|  | 5631 |  | 
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5632 | // Determine whether we've seen an ivar with a name similar to the | 
|  | 5633 | // property. | 
| Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 5634 | if ((PropertyName == Ivar->getIdentifier() || | 
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5635 | NameWithPrefix == Ivar->getName() || | 
| Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 5636 | NameWithSuffix == Ivar->getName())) { | 
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5637 | SawSimilarlyNamedIvar = true; | 
| Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 5638 |  | 
|  | 5639 | // Reduce the priority of this result by one, to give it a slight | 
|  | 5640 | // advantage over other results whose names don't match so closely. | 
|  | 5641 | if (Results.size() && | 
|  | 5642 | Results.data()[Results.size() - 1].Kind | 
|  | 5643 | == CodeCompletionResult::RK_Declaration && | 
|  | 5644 | Results.data()[Results.size() - 1].Declaration == Ivar) | 
|  | 5645 | Results.data()[Results.size() - 1].Priority--; | 
|  | 5646 | } | 
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5647 | } | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5648 | } | 
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5649 |  | 
|  | 5650 | if (!SawSimilarlyNamedIvar) { | 
|  | 5651 | // Create ivar result _propName, that the user can use to synthesize | 
| Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 5652 | // an ivar of the appropriate type. | 
|  | 5653 | unsigned Priority = CCP_MemberDeclaration + 1; | 
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5654 | typedef CodeCompletionResult Result; | 
|  | 5655 | CodeCompletionAllocator &Allocator = Results.getAllocator(); | 
|  | 5656 | CodeCompletionBuilder Builder(Allocator, Priority,CXAvailability_Available); | 
|  | 5657 |  | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 5658 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); | 
| Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 5659 | Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context, | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 5660 | Policy, Allocator)); | 
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 5661 | Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix)); | 
|  | 5662 | Results.AddResult(Result(Builder.TakeString(), Priority, | 
|  | 5663 | CXCursor_ObjCIvarDecl)); | 
|  | 5664 | } | 
|  | 5665 |  | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5666 | Results.ExitScope(); | 
|  | 5667 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5668 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 5669 | CodeCompletionContext::CCC_Other, | 
|  | 5670 | Results.data(),Results.size()); | 
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5671 | } | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5672 |  | 
| Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5673 | // Mapping from selectors to the methods that implement that selector, along | 
|  | 5674 | // with the "in original class" flag. | 
|  | 5675 | typedef llvm::DenseMap<Selector, std::pair<ObjCMethodDecl *, bool> > | 
|  | 5676 | KnownMethodsMap; | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5677 |  | 
|  | 5678 | /// \brief Find all of the methods that reside in the given container | 
|  | 5679 | /// (and its superclasses, protocols, etc.) that meet the given | 
|  | 5680 | /// criteria. Insert those methods into the map of known methods, | 
|  | 5681 | /// indexed by selector so they can be easily found. | 
|  | 5682 | static void FindImplementableMethods(ASTContext &Context, | 
|  | 5683 | ObjCContainerDecl *Container, | 
|  | 5684 | bool WantInstanceMethods, | 
|  | 5685 | QualType ReturnType, | 
| Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5686 | KnownMethodsMap &KnownMethods, | 
|  | 5687 | bool InOriginalClass = true) { | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5688 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) { | 
|  | 5689 | // Recurse into protocols. | 
|  | 5690 | const ObjCList<ObjCProtocolDecl> &Protocols | 
|  | 5691 | = IFace->getReferencedProtocols(); | 
|  | 5692 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), | 
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5693 | E = Protocols.end(); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5694 | I != E; ++I) | 
|  | 5695 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, | 
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5696 | KnownMethods, InOriginalClass); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5697 |  | 
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5698 | // Add methods from any class extensions and categories. | 
|  | 5699 | for (const ObjCCategoryDecl *Cat = IFace->getCategoryList(); Cat; | 
|  | 5700 | Cat = Cat->getNextClassCategory()) | 
| Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 5701 | FindImplementableMethods(Context, const_cast<ObjCCategoryDecl*>(Cat), | 
|  | 5702 | WantInstanceMethods, ReturnType, | 
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5703 | KnownMethods, false); | 
|  | 5704 |  | 
|  | 5705 | // Visit the superclass. | 
|  | 5706 | if (IFace->getSuperClass()) | 
|  | 5707 | FindImplementableMethods(Context, IFace->getSuperClass(), | 
|  | 5708 | WantInstanceMethods, ReturnType, | 
|  | 5709 | KnownMethods, false); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5710 | } | 
|  | 5711 |  | 
|  | 5712 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { | 
|  | 5713 | // Recurse into protocols. | 
|  | 5714 | const ObjCList<ObjCProtocolDecl> &Protocols | 
|  | 5715 | = Category->getReferencedProtocols(); | 
|  | 5716 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), | 
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5717 | E = Protocols.end(); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5718 | I != E; ++I) | 
|  | 5719 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, | 
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5720 | KnownMethods, InOriginalClass); | 
|  | 5721 |  | 
|  | 5722 | // If this category is the original class, jump to the interface. | 
|  | 5723 | if (InOriginalClass && Category->getClassInterface()) | 
|  | 5724 | FindImplementableMethods(Context, Category->getClassInterface(), | 
|  | 5725 | WantInstanceMethods, ReturnType, KnownMethods, | 
|  | 5726 | false); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5727 | } | 
|  | 5728 |  | 
|  | 5729 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { | 
|  | 5730 | // Recurse into protocols. | 
|  | 5731 | const ObjCList<ObjCProtocolDecl> &Protocols | 
|  | 5732 | = Protocol->getReferencedProtocols(); | 
|  | 5733 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), | 
|  | 5734 | E = Protocols.end(); | 
|  | 5735 | I != E; ++I) | 
|  | 5736 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, | 
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 5737 | KnownMethods, false); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5738 | } | 
|  | 5739 |  | 
|  | 5740 | // Add methods in this container. This operation occurs last because | 
|  | 5741 | // we want the methods from this container to override any methods | 
|  | 5742 | // we've previously seen with the same selector. | 
|  | 5743 | for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), | 
|  | 5744 | MEnd = Container->meth_end(); | 
|  | 5745 | M != MEnd; ++M) { | 
|  | 5746 | if ((*M)->isInstanceMethod() == WantInstanceMethods) { | 
|  | 5747 | if (!ReturnType.isNull() && | 
|  | 5748 | !Context.hasSameUnqualifiedType(ReturnType, (*M)->getResultType())) | 
|  | 5749 | continue; | 
|  | 5750 |  | 
| Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5751 | KnownMethods[(*M)->getSelector()] = std::make_pair(*M, InOriginalClass); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5752 | } | 
|  | 5753 | } | 
|  | 5754 | } | 
|  | 5755 |  | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5756 | /// \brief Add the parenthesized return or parameter type chunk to a code | 
|  | 5757 | /// completion string. | 
|  | 5758 | static void AddObjCPassingTypeChunk(QualType Type, | 
|  | 5759 | ASTContext &Context, | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 5760 | const PrintingPolicy &Policy, | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5761 | CodeCompletionBuilder &Builder) { | 
|  | 5762 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 5763 | Builder.AddTextChunk(GetCompletionTypeString(Type, Context, Policy, | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5764 | Builder.getAllocator())); | 
|  | 5765 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 5766 | } | 
|  | 5767 |  | 
|  | 5768 | /// \brief Determine whether the given class is or inherits from a class by | 
|  | 5769 | /// the given name. | 
|  | 5770 | static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class, | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5771 | StringRef Name) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5772 | if (!Class) | 
|  | 5773 | return false; | 
|  | 5774 |  | 
|  | 5775 | if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name) | 
|  | 5776 | return true; | 
|  | 5777 |  | 
|  | 5778 | return InheritsFromClassNamed(Class->getSuperClass(), Name); | 
|  | 5779 | } | 
|  | 5780 |  | 
|  | 5781 | /// \brief Add code completions for Objective-C Key-Value Coding (KVC) and | 
|  | 5782 | /// Key-Value Observing (KVO). | 
|  | 5783 | static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property, | 
|  | 5784 | bool IsInstanceMethod, | 
|  | 5785 | QualType ReturnType, | 
|  | 5786 | ASTContext &Context, | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 5787 | VisitedSelectorSet &KnownSelectors, | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5788 | ResultBuilder &Results) { | 
|  | 5789 | IdentifierInfo *PropName = Property->getIdentifier(); | 
|  | 5790 | if (!PropName || PropName->getLength() == 0) | 
|  | 5791 | return; | 
|  | 5792 |  | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 5793 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); | 
|  | 5794 |  | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5795 | // Builder that will create each code completion. | 
|  | 5796 | typedef CodeCompletionResult Result; | 
|  | 5797 | CodeCompletionAllocator &Allocator = Results.getAllocator(); | 
|  | 5798 | CodeCompletionBuilder Builder(Allocator); | 
|  | 5799 |  | 
|  | 5800 | // The selector table. | 
|  | 5801 | SelectorTable &Selectors = Context.Selectors; | 
|  | 5802 |  | 
|  | 5803 | // The property name, copied into the code completion allocation region | 
|  | 5804 | // on demand. | 
|  | 5805 | struct KeyHolder { | 
|  | 5806 | CodeCompletionAllocator &Allocator; | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5807 | StringRef Key; | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5808 | const char *CopiedKey; | 
|  | 5809 |  | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5810 | KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key) | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5811 | : Allocator(Allocator), Key(Key), CopiedKey(0) { } | 
|  | 5812 |  | 
|  | 5813 | operator const char *() { | 
|  | 5814 | if (CopiedKey) | 
|  | 5815 | return CopiedKey; | 
|  | 5816 |  | 
|  | 5817 | return CopiedKey = Allocator.CopyString(Key); | 
|  | 5818 | } | 
|  | 5819 | } Key(Allocator, PropName->getName()); | 
|  | 5820 |  | 
|  | 5821 | // The uppercased name of the property name. | 
|  | 5822 | std::string UpperKey = PropName->getName(); | 
|  | 5823 | if (!UpperKey.empty()) | 
|  | 5824 | UpperKey[0] = toupper(UpperKey[0]); | 
|  | 5825 |  | 
|  | 5826 | bool ReturnTypeMatchesProperty = ReturnType.isNull() || | 
|  | 5827 | Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(), | 
|  | 5828 | Property->getType()); | 
|  | 5829 | bool ReturnTypeMatchesVoid | 
|  | 5830 | = ReturnType.isNull() || ReturnType->isVoidType(); | 
|  | 5831 |  | 
|  | 5832 | // Add the normal accessor -(type)key. | 
|  | 5833 | if (IsInstanceMethod && | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 5834 | KnownSelectors.insert(Selectors.getNullarySelector(PropName)) && | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5835 | ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) { | 
|  | 5836 | if (ReturnType.isNull()) | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 5837 | AddObjCPassingTypeChunk(Property->getType(), Context, Policy, Builder); | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5838 |  | 
|  | 5839 | Builder.AddTypedTextChunk(Key); | 
|  | 5840 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, | 
|  | 5841 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 5842 | } | 
|  | 5843 |  | 
|  | 5844 | // If we have an integral or boolean property (or the user has provided | 
|  | 5845 | // an integral or boolean return type), add the accessor -(type)isKey. | 
|  | 5846 | if (IsInstanceMethod && | 
|  | 5847 | ((!ReturnType.isNull() && | 
|  | 5848 | (ReturnType->isIntegerType() || ReturnType->isBooleanType())) || | 
|  | 5849 | (ReturnType.isNull() && | 
|  | 5850 | (Property->getType()->isIntegerType() || | 
|  | 5851 | Property->getType()->isBooleanType())))) { | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5852 | std::string SelectorName = (Twine("is") + UpperKey).str(); | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5853 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 5854 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5855 | if (ReturnType.isNull()) { | 
|  | 5856 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 5857 | Builder.AddTextChunk("BOOL"); | 
|  | 5858 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 5859 | } | 
|  | 5860 |  | 
|  | 5861 | Builder.AddTypedTextChunk( | 
|  | 5862 | Allocator.CopyString(SelectorId->getName())); | 
|  | 5863 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, | 
|  | 5864 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 5865 | } | 
|  | 5866 | } | 
|  | 5867 |  | 
|  | 5868 | // Add the normal mutator. | 
|  | 5869 | if (IsInstanceMethod && ReturnTypeMatchesVoid && | 
|  | 5870 | !Property->getSetterMethodDecl()) { | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5871 | std::string SelectorName = (Twine("set") + UpperKey).str(); | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5872 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 5873 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5874 | if (ReturnType.isNull()) { | 
|  | 5875 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 5876 | Builder.AddTextChunk("void"); | 
|  | 5877 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 5878 | } | 
|  | 5879 |  | 
|  | 5880 | Builder.AddTypedTextChunk( | 
|  | 5881 | Allocator.CopyString(SelectorId->getName())); | 
|  | 5882 | Builder.AddTypedTextChunk(":"); | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 5883 | AddObjCPassingTypeChunk(Property->getType(), Context, Policy, Builder); | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5884 | Builder.AddTextChunk(Key); | 
|  | 5885 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, | 
|  | 5886 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 5887 | } | 
|  | 5888 | } | 
|  | 5889 |  | 
|  | 5890 | // Indexed and unordered accessors | 
|  | 5891 | unsigned IndexedGetterPriority = CCP_CodePattern; | 
|  | 5892 | unsigned IndexedSetterPriority = CCP_CodePattern; | 
|  | 5893 | unsigned UnorderedGetterPriority = CCP_CodePattern; | 
|  | 5894 | unsigned UnorderedSetterPriority = CCP_CodePattern; | 
|  | 5895 | if (const ObjCObjectPointerType *ObjCPointer | 
|  | 5896 | = Property->getType()->getAs<ObjCObjectPointerType>()) { | 
|  | 5897 | if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) { | 
|  | 5898 | // If this interface type is not provably derived from a known | 
|  | 5899 | // collection, penalize the corresponding completions. | 
|  | 5900 | if (!InheritsFromClassNamed(IFace, "NSMutableArray")) { | 
|  | 5901 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; | 
|  | 5902 | if (!InheritsFromClassNamed(IFace, "NSArray")) | 
|  | 5903 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; | 
|  | 5904 | } | 
|  | 5905 |  | 
|  | 5906 | if (!InheritsFromClassNamed(IFace, "NSMutableSet")) { | 
|  | 5907 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; | 
|  | 5908 | if (!InheritsFromClassNamed(IFace, "NSSet")) | 
|  | 5909 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; | 
|  | 5910 | } | 
|  | 5911 | } | 
|  | 5912 | } else { | 
|  | 5913 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; | 
|  | 5914 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; | 
|  | 5915 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; | 
|  | 5916 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; | 
|  | 5917 | } | 
|  | 5918 |  | 
|  | 5919 | // Add -(NSUInteger)countOf<key> | 
|  | 5920 | if (IsInstanceMethod && | 
|  | 5921 | (ReturnType.isNull() || ReturnType->isIntegerType())) { | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5922 | std::string SelectorName = (Twine("countOf") + UpperKey).str(); | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5923 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 5924 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5925 | if (ReturnType.isNull()) { | 
|  | 5926 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 5927 | Builder.AddTextChunk("NSUInteger"); | 
|  | 5928 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 5929 | } | 
|  | 5930 |  | 
|  | 5931 | Builder.AddTypedTextChunk( | 
|  | 5932 | Allocator.CopyString(SelectorId->getName())); | 
|  | 5933 | Results.AddResult(Result(Builder.TakeString(), | 
|  | 5934 | std::min(IndexedGetterPriority, | 
|  | 5935 | UnorderedGetterPriority), | 
|  | 5936 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 5937 | } | 
|  | 5938 | } | 
|  | 5939 |  | 
|  | 5940 | // Indexed getters | 
|  | 5941 | // Add -(id)objectInKeyAtIndex:(NSUInteger)index | 
|  | 5942 | if (IsInstanceMethod && | 
|  | 5943 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5944 | std::string SelectorName | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5945 | = (Twine("objectIn") + UpperKey + "AtIndex").str(); | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5946 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 5947 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5948 | if (ReturnType.isNull()) { | 
|  | 5949 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 5950 | Builder.AddTextChunk("id"); | 
|  | 5951 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 5952 | } | 
|  | 5953 |  | 
|  | 5954 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | 
|  | 5955 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 5956 | Builder.AddTextChunk("NSUInteger"); | 
|  | 5957 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 5958 | Builder.AddTextChunk("index"); | 
|  | 5959 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, | 
|  | 5960 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 5961 | } | 
|  | 5962 | } | 
|  | 5963 |  | 
|  | 5964 | // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes | 
|  | 5965 | if (IsInstanceMethod && | 
|  | 5966 | (ReturnType.isNull() || | 
|  | 5967 | (ReturnType->isObjCObjectPointerType() && | 
|  | 5968 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && | 
|  | 5969 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() | 
|  | 5970 | ->getName() == "NSArray"))) { | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5971 | std::string SelectorName | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5972 | = (Twine(Property->getName()) + "AtIndexes").str(); | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5973 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 5974 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5975 | if (ReturnType.isNull()) { | 
|  | 5976 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 5977 | Builder.AddTextChunk("NSArray *"); | 
|  | 5978 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 5979 | } | 
|  | 5980 |  | 
|  | 5981 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | 
|  | 5982 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 5983 | Builder.AddTextChunk("NSIndexSet *"); | 
|  | 5984 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 5985 | Builder.AddTextChunk("indexes"); | 
|  | 5986 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, | 
|  | 5987 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 5988 | } | 
|  | 5989 | } | 
|  | 5990 |  | 
|  | 5991 | // Add -(void)getKey:(type **)buffer range:(NSRange)inRange | 
|  | 5992 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5993 | std::string SelectorName = (Twine("get") + UpperKey).str(); | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5994 | IdentifierInfo *SelectorIds[2] = { | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 5995 | &Context.Idents.get(SelectorName), | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 5996 | &Context.Idents.get("range") | 
|  | 5997 | }; | 
|  | 5998 |  | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 5999 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6000 | if (ReturnType.isNull()) { | 
|  | 6001 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6002 | Builder.AddTextChunk("void"); | 
|  | 6003 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6004 | } | 
|  | 6005 |  | 
|  | 6006 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | 
|  | 6007 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6008 | Builder.AddPlaceholderChunk("object-type"); | 
|  | 6009 | Builder.AddTextChunk(" **"); | 
|  | 6010 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6011 | Builder.AddTextChunk("buffer"); | 
|  | 6012 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6013 | Builder.AddTypedTextChunk("range:"); | 
|  | 6014 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6015 | Builder.AddTextChunk("NSRange"); | 
|  | 6016 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6017 | Builder.AddTextChunk("inRange"); | 
|  | 6018 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, | 
|  | 6019 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 6020 | } | 
|  | 6021 | } | 
|  | 6022 |  | 
|  | 6023 | // Mutable indexed accessors | 
|  | 6024 |  | 
|  | 6025 | // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index | 
|  | 6026 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6027 | std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str(); | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6028 | IdentifierInfo *SelectorIds[2] = { | 
|  | 6029 | &Context.Idents.get("insertObject"), | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6030 | &Context.Idents.get(SelectorName) | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6031 | }; | 
|  | 6032 |  | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6033 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6034 | if (ReturnType.isNull()) { | 
|  | 6035 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6036 | Builder.AddTextChunk("void"); | 
|  | 6037 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6038 | } | 
|  | 6039 |  | 
|  | 6040 | Builder.AddTypedTextChunk("insertObject:"); | 
|  | 6041 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6042 | Builder.AddPlaceholderChunk("object-type"); | 
|  | 6043 | Builder.AddTextChunk(" *"); | 
|  | 6044 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6045 | Builder.AddTextChunk("object"); | 
|  | 6046 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6047 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | 
|  | 6048 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6049 | Builder.AddPlaceholderChunk("NSUInteger"); | 
|  | 6050 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6051 | Builder.AddTextChunk("index"); | 
|  | 6052 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, | 
|  | 6053 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 6054 | } | 
|  | 6055 | } | 
|  | 6056 |  | 
|  | 6057 | // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes | 
|  | 6058 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6059 | std::string SelectorName = (Twine("insert") + UpperKey).str(); | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6060 | IdentifierInfo *SelectorIds[2] = { | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6061 | &Context.Idents.get(SelectorName), | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6062 | &Context.Idents.get("atIndexes") | 
|  | 6063 | }; | 
|  | 6064 |  | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6065 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6066 | if (ReturnType.isNull()) { | 
|  | 6067 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6068 | Builder.AddTextChunk("void"); | 
|  | 6069 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6070 | } | 
|  | 6071 |  | 
|  | 6072 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | 
|  | 6073 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6074 | Builder.AddTextChunk("NSArray *"); | 
|  | 6075 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6076 | Builder.AddTextChunk("array"); | 
|  | 6077 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6078 | Builder.AddTypedTextChunk("atIndexes:"); | 
|  | 6079 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6080 | Builder.AddPlaceholderChunk("NSIndexSet *"); | 
|  | 6081 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6082 | Builder.AddTextChunk("indexes"); | 
|  | 6083 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, | 
|  | 6084 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 6085 | } | 
|  | 6086 | } | 
|  | 6087 |  | 
|  | 6088 | // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index | 
|  | 6089 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6090 | std::string SelectorName | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6091 | = (Twine("removeObjectFrom") + UpperKey + "AtIndex").str(); | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6092 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6093 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6094 | if (ReturnType.isNull()) { | 
|  | 6095 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6096 | Builder.AddTextChunk("void"); | 
|  | 6097 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6098 | } | 
|  | 6099 |  | 
|  | 6100 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | 
|  | 6101 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6102 | Builder.AddTextChunk("NSUInteger"); | 
|  | 6103 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6104 | Builder.AddTextChunk("index"); | 
|  | 6105 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, | 
|  | 6106 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 6107 | } | 
|  | 6108 | } | 
|  | 6109 |  | 
|  | 6110 | // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes | 
|  | 6111 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6112 | std::string SelectorName | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6113 | = (Twine("remove") + UpperKey + "AtIndexes").str(); | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6114 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6115 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6116 | if (ReturnType.isNull()) { | 
|  | 6117 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6118 | Builder.AddTextChunk("void"); | 
|  | 6119 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6120 | } | 
|  | 6121 |  | 
|  | 6122 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | 
|  | 6123 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6124 | Builder.AddTextChunk("NSIndexSet *"); | 
|  | 6125 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6126 | Builder.AddTextChunk("indexes"); | 
|  | 6127 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, | 
|  | 6128 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 6129 | } | 
|  | 6130 | } | 
|  | 6131 |  | 
|  | 6132 | // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object | 
|  | 6133 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6134 | std::string SelectorName | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6135 | = (Twine("replaceObjectIn") + UpperKey + "AtIndex").str(); | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6136 | IdentifierInfo *SelectorIds[2] = { | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6137 | &Context.Idents.get(SelectorName), | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6138 | &Context.Idents.get("withObject") | 
|  | 6139 | }; | 
|  | 6140 |  | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6141 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6142 | if (ReturnType.isNull()) { | 
|  | 6143 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6144 | Builder.AddTextChunk("void"); | 
|  | 6145 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6146 | } | 
|  | 6147 |  | 
|  | 6148 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | 
|  | 6149 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6150 | Builder.AddPlaceholderChunk("NSUInteger"); | 
|  | 6151 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6152 | Builder.AddTextChunk("index"); | 
|  | 6153 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6154 | Builder.AddTypedTextChunk("withObject:"); | 
|  | 6155 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6156 | Builder.AddTextChunk("id"); | 
|  | 6157 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6158 | Builder.AddTextChunk("object"); | 
|  | 6159 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, | 
|  | 6160 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 6161 | } | 
|  | 6162 | } | 
|  | 6163 |  | 
|  | 6164 | // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array | 
|  | 6165 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6166 | std::string SelectorName1 | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6167 | = (Twine("replace") + UpperKey + "AtIndexes").str(); | 
|  | 6168 | std::string SelectorName2 = (Twine("with") + UpperKey).str(); | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6169 | IdentifierInfo *SelectorIds[2] = { | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6170 | &Context.Idents.get(SelectorName1), | 
|  | 6171 | &Context.Idents.get(SelectorName2) | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6172 | }; | 
|  | 6173 |  | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6174 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6175 | if (ReturnType.isNull()) { | 
|  | 6176 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6177 | Builder.AddTextChunk("void"); | 
|  | 6178 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6179 | } | 
|  | 6180 |  | 
|  | 6181 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":")); | 
|  | 6182 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6183 | Builder.AddPlaceholderChunk("NSIndexSet *"); | 
|  | 6184 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6185 | Builder.AddTextChunk("indexes"); | 
|  | 6186 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6187 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":")); | 
|  | 6188 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6189 | Builder.AddTextChunk("NSArray *"); | 
|  | 6190 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6191 | Builder.AddTextChunk("array"); | 
|  | 6192 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, | 
|  | 6193 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 6194 | } | 
|  | 6195 | } | 
|  | 6196 |  | 
|  | 6197 | // Unordered getters | 
|  | 6198 | // - (NSEnumerator *)enumeratorOfKey | 
|  | 6199 | if (IsInstanceMethod && | 
|  | 6200 | (ReturnType.isNull() || | 
|  | 6201 | (ReturnType->isObjCObjectPointerType() && | 
|  | 6202 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && | 
|  | 6203 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() | 
|  | 6204 | ->getName() == "NSEnumerator"))) { | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6205 | std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str(); | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6206 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6207 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6208 | if (ReturnType.isNull()) { | 
|  | 6209 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6210 | Builder.AddTextChunk("NSEnumerator *"); | 
|  | 6211 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6212 | } | 
|  | 6213 |  | 
|  | 6214 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); | 
|  | 6215 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, | 
|  | 6216 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 6217 | } | 
|  | 6218 | } | 
|  | 6219 |  | 
|  | 6220 | // - (type *)memberOfKey:(type *)object | 
|  | 6221 | if (IsInstanceMethod && | 
|  | 6222 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6223 | std::string SelectorName = (Twine("memberOf") + UpperKey).str(); | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6224 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6225 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6226 | if (ReturnType.isNull()) { | 
|  | 6227 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6228 | Builder.AddPlaceholderChunk("object-type"); | 
|  | 6229 | Builder.AddTextChunk(" *"); | 
|  | 6230 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6231 | } | 
|  | 6232 |  | 
|  | 6233 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | 
|  | 6234 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6235 | if (ReturnType.isNull()) { | 
|  | 6236 | Builder.AddPlaceholderChunk("object-type"); | 
|  | 6237 | Builder.AddTextChunk(" *"); | 
|  | 6238 | } else { | 
|  | 6239 | Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context, | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6240 | Policy, | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6241 | Builder.getAllocator())); | 
|  | 6242 | } | 
|  | 6243 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6244 | Builder.AddTextChunk("object"); | 
|  | 6245 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, | 
|  | 6246 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 6247 | } | 
|  | 6248 | } | 
|  | 6249 |  | 
|  | 6250 | // Mutable unordered accessors | 
|  | 6251 | // - (void)addKeyObject:(type *)object | 
|  | 6252 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6253 | std::string SelectorName | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6254 | = (Twine("add") + UpperKey + Twine("Object")).str(); | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6255 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6256 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6257 | if (ReturnType.isNull()) { | 
|  | 6258 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6259 | Builder.AddTextChunk("void"); | 
|  | 6260 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6261 | } | 
|  | 6262 |  | 
|  | 6263 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | 
|  | 6264 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6265 | Builder.AddPlaceholderChunk("object-type"); | 
|  | 6266 | Builder.AddTextChunk(" *"); | 
|  | 6267 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6268 | Builder.AddTextChunk("object"); | 
|  | 6269 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, | 
|  | 6270 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 6271 | } | 
|  | 6272 | } | 
|  | 6273 |  | 
|  | 6274 | // - (void)addKey:(NSSet *)objects | 
|  | 6275 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6276 | std::string SelectorName = (Twine("add") + UpperKey).str(); | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6277 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6278 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6279 | if (ReturnType.isNull()) { | 
|  | 6280 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6281 | Builder.AddTextChunk("void"); | 
|  | 6282 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6283 | } | 
|  | 6284 |  | 
|  | 6285 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | 
|  | 6286 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6287 | Builder.AddTextChunk("NSSet *"); | 
|  | 6288 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6289 | Builder.AddTextChunk("objects"); | 
|  | 6290 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, | 
|  | 6291 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 6292 | } | 
|  | 6293 | } | 
|  | 6294 |  | 
|  | 6295 | // - (void)removeKeyObject:(type *)object | 
|  | 6296 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6297 | std::string SelectorName | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6298 | = (Twine("remove") + UpperKey + Twine("Object")).str(); | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6299 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6300 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6301 | if (ReturnType.isNull()) { | 
|  | 6302 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6303 | Builder.AddTextChunk("void"); | 
|  | 6304 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6305 | } | 
|  | 6306 |  | 
|  | 6307 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | 
|  | 6308 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6309 | Builder.AddPlaceholderChunk("object-type"); | 
|  | 6310 | Builder.AddTextChunk(" *"); | 
|  | 6311 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6312 | Builder.AddTextChunk("object"); | 
|  | 6313 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, | 
|  | 6314 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 6315 | } | 
|  | 6316 | } | 
|  | 6317 |  | 
|  | 6318 | // - (void)removeKey:(NSSet *)objects | 
|  | 6319 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6320 | std::string SelectorName = (Twine("remove") + UpperKey).str(); | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6321 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6322 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6323 | if (ReturnType.isNull()) { | 
|  | 6324 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6325 | Builder.AddTextChunk("void"); | 
|  | 6326 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6327 | } | 
|  | 6328 |  | 
|  | 6329 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | 
|  | 6330 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6331 | Builder.AddTextChunk("NSSet *"); | 
|  | 6332 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6333 | Builder.AddTextChunk("objects"); | 
|  | 6334 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, | 
|  | 6335 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 6336 | } | 
|  | 6337 | } | 
|  | 6338 |  | 
|  | 6339 | // - (void)intersectKey:(NSSet *)objects | 
|  | 6340 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6341 | std::string SelectorName = (Twine("intersect") + UpperKey).str(); | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6342 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6343 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6344 | if (ReturnType.isNull()) { | 
|  | 6345 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6346 | Builder.AddTextChunk("void"); | 
|  | 6347 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6348 | } | 
|  | 6349 |  | 
|  | 6350 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | 
|  | 6351 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6352 | Builder.AddTextChunk("NSSet *"); | 
|  | 6353 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6354 | Builder.AddTextChunk("objects"); | 
|  | 6355 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, | 
|  | 6356 | CXCursor_ObjCInstanceMethodDecl)); | 
|  | 6357 | } | 
|  | 6358 | } | 
|  | 6359 |  | 
|  | 6360 | // Key-Value Observing | 
|  | 6361 | // + (NSSet *)keyPathsForValuesAffectingKey | 
|  | 6362 | if (!IsInstanceMethod && | 
|  | 6363 | (ReturnType.isNull() || | 
|  | 6364 | (ReturnType->isObjCObjectPointerType() && | 
|  | 6365 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && | 
|  | 6366 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() | 
|  | 6367 | ->getName() == "NSSet"))) { | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6368 | std::string SelectorName | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6369 | = (Twine("keyPathsForValuesAffecting") + UpperKey).str(); | 
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6370 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6371 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) { | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6372 | if (ReturnType.isNull()) { | 
|  | 6373 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6374 | Builder.AddTextChunk("NSSet *"); | 
|  | 6375 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6376 | } | 
|  | 6377 |  | 
|  | 6378 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); | 
|  | 6379 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, | 
| Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 6380 | CXCursor_ObjCClassMethodDecl)); | 
|  | 6381 | } | 
|  | 6382 | } | 
|  | 6383 |  | 
|  | 6384 | // + (BOOL)automaticallyNotifiesObserversForKey | 
|  | 6385 | if (!IsInstanceMethod && | 
|  | 6386 | (ReturnType.isNull() || | 
|  | 6387 | ReturnType->isIntegerType() || | 
|  | 6388 | ReturnType->isBooleanType())) { | 
|  | 6389 | std::string SelectorName | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6390 | = (Twine("automaticallyNotifiesObserversOf") + UpperKey).str(); | 
| Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 6391 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | 
|  | 6392 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) { | 
|  | 6393 | if (ReturnType.isNull()) { | 
|  | 6394 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6395 | Builder.AddTextChunk("BOOL"); | 
|  | 6396 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6397 | } | 
|  | 6398 |  | 
|  | 6399 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); | 
|  | 6400 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, | 
|  | 6401 | CXCursor_ObjCClassMethodDecl)); | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6402 | } | 
|  | 6403 | } | 
|  | 6404 | } | 
|  | 6405 |  | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6406 | void Sema::CodeCompleteObjCMethodDecl(Scope *S, | 
|  | 6407 | bool IsInstanceMethod, | 
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6408 | ParsedType ReturnTy) { | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6409 | // Determine the return type of the method we're declaring, if | 
|  | 6410 | // provided. | 
|  | 6411 | QualType ReturnType = GetTypeFromParser(ReturnTy); | 
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6412 | Decl *IDecl = 0; | 
|  | 6413 | if (CurContext->isObjCContainer()) { | 
|  | 6414 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); | 
|  | 6415 | IDecl = cast<Decl>(OCD); | 
|  | 6416 | } | 
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6417 | // Determine where we should start searching for methods. | 
|  | 6418 | ObjCContainerDecl *SearchDecl = 0; | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6419 | bool IsInImplementation = false; | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6420 | if (Decl *D = IDecl) { | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6421 | if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) { | 
|  | 6422 | SearchDecl = Impl->getClassInterface(); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6423 | IsInImplementation = true; | 
|  | 6424 | } else if (ObjCCategoryImplDecl *CatImpl | 
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6425 | = dyn_cast<ObjCCategoryImplDecl>(D)) { | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6426 | SearchDecl = CatImpl->getCategoryDecl(); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6427 | IsInImplementation = true; | 
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6428 | } else | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6429 | SearchDecl = dyn_cast<ObjCContainerDecl>(D); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6430 | } | 
|  | 6431 |  | 
|  | 6432 | if (!SearchDecl && S) { | 
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6433 | if (DeclContext *DC = static_cast<DeclContext *>(S->getEntity())) | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6434 | SearchDecl = dyn_cast<ObjCContainerDecl>(DC); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6435 | } | 
|  | 6436 |  | 
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6437 | if (!SearchDecl) { | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6438 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 6439 | CodeCompletionContext::CCC_Other, | 
|  | 6440 | 0, 0); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6441 | return; | 
|  | 6442 | } | 
|  | 6443 |  | 
|  | 6444 | // Find all of the methods that we could declare/implement here. | 
|  | 6445 | KnownMethodsMap KnownMethods; | 
|  | 6446 | FindImplementableMethods(Context, SearchDecl, IsInstanceMethod, | 
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6447 | ReturnType, KnownMethods); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6448 |  | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6449 | // Add declarations or definitions for each of the known methods. | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6450 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6451 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 6452 | CodeCompletionContext::CCC_Other); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6453 | Results.EnterNewScope(); | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6454 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6455 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), | 
|  | 6456 | MEnd = KnownMethods.end(); | 
|  | 6457 | M != MEnd; ++M) { | 
| Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6458 | ObjCMethodDecl *Method = M->second.first; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6459 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6460 |  | 
|  | 6461 | // If the result type was not already provided, add it to the | 
|  | 6462 | // pattern as (type). | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6463 | if (ReturnType.isNull()) | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6464 | AddObjCPassingTypeChunk(Method->getResultType(), Context, Policy, | 
|  | 6465 | Builder); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6466 |  | 
|  | 6467 | Selector Sel = Method->getSelector(); | 
|  | 6468 |  | 
|  | 6469 | // Add the first part of the selector to the pattern. | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6470 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( | 
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 6471 | Sel.getNameForSlot(0))); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6472 |  | 
|  | 6473 | // Add parameters to the pattern. | 
|  | 6474 | unsigned I = 0; | 
|  | 6475 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), | 
|  | 6476 | PEnd = Method->param_end(); | 
|  | 6477 | P != PEnd; (void)++P, ++I) { | 
|  | 6478 | // Add the part of the selector name. | 
|  | 6479 | if (I == 0) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6480 | Builder.AddTypedTextChunk(":"); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6481 | else if (I < Sel.getNumArgs()) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6482 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6483 | Builder.AddTypedTextChunk( | 
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 6484 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6485 | } else | 
|  | 6486 | break; | 
|  | 6487 |  | 
|  | 6488 | // Add the parameter type. | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6489 | AddObjCPassingTypeChunk((*P)->getOriginalType(), Context, Policy, | 
|  | 6490 | Builder); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6491 |  | 
|  | 6492 | if (IdentifierInfo *Id = (*P)->getIdentifier()) | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6493 | Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName())); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6494 | } | 
|  | 6495 |  | 
|  | 6496 | if (Method->isVariadic()) { | 
|  | 6497 | if (Method->param_size() > 0) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6498 | Builder.AddChunk(CodeCompletionString::CK_Comma); | 
|  | 6499 | Builder.AddTextChunk("..."); | 
| Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 6500 | } | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6501 |  | 
| Douglas Gregor | d37c59d | 2010-05-28 00:57:46 +0000 | [diff] [blame] | 6502 | if (IsInImplementation && Results.includeCodePatterns()) { | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6503 | // We will be defining the method here, so add a compound statement. | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6504 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6505 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | 
|  | 6506 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6507 | if (!Method->getResultType()->isVoidType()) { | 
|  | 6508 | // If the result type is not void, add a return clause. | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6509 | Builder.AddTextChunk("return"); | 
|  | 6510 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6511 | Builder.AddPlaceholderChunk("expression"); | 
|  | 6512 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6513 | } else | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6514 | Builder.AddPlaceholderChunk("statements"); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6515 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6516 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | 
|  | 6517 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6518 | } | 
|  | 6519 |  | 
| Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6520 | unsigned Priority = CCP_CodePattern; | 
|  | 6521 | if (!M->second.second) | 
|  | 6522 | Priority += CCD_InBaseClass; | 
|  | 6523 |  | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6524 | Results.AddResult(Result(Builder.TakeString(), Priority, | 
| Douglas Gregor | 7116a8c | 2010-08-17 16:06:07 +0000 | [diff] [blame] | 6525 | Method->isInstanceMethod() | 
|  | 6526 | ? CXCursor_ObjCInstanceMethodDecl | 
|  | 6527 | : CXCursor_ObjCClassMethodDecl)); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6528 | } | 
|  | 6529 |  | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6530 | // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of | 
|  | 6531 | // the properties in this class and its categories. | 
|  | 6532 | if (Context.getLangOptions().ObjC2) { | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6533 | SmallVector<ObjCContainerDecl *, 4> Containers; | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6534 | Containers.push_back(SearchDecl); | 
|  | 6535 |  | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6536 | VisitedSelectorSet KnownSelectors; | 
|  | 6537 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), | 
|  | 6538 | MEnd = KnownMethods.end(); | 
|  | 6539 | M != MEnd; ++M) | 
|  | 6540 | KnownSelectors.insert(M->first); | 
|  | 6541 |  | 
|  | 6542 |  | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6543 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl); | 
|  | 6544 | if (!IFace) | 
|  | 6545 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl)) | 
|  | 6546 | IFace = Category->getClassInterface(); | 
|  | 6547 |  | 
|  | 6548 | if (IFace) { | 
|  | 6549 | for (ObjCCategoryDecl *Category = IFace->getCategoryList(); Category; | 
|  | 6550 | Category = Category->getNextClassCategory()) | 
|  | 6551 | Containers.push_back(Category); | 
|  | 6552 | } | 
|  | 6553 |  | 
|  | 6554 | for (unsigned I = 0, N = Containers.size(); I != N; ++I) { | 
|  | 6555 | for (ObjCContainerDecl::prop_iterator P = Containers[I]->prop_begin(), | 
|  | 6556 | PEnd = Containers[I]->prop_end(); | 
|  | 6557 | P != PEnd; ++P) { | 
|  | 6558 | AddObjCKeyValueCompletions(*P, IsInstanceMethod, ReturnType, Context, | 
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6559 | KnownSelectors, Results); | 
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6560 | } | 
|  | 6561 | } | 
|  | 6562 | } | 
|  | 6563 |  | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6564 | Results.ExitScope(); | 
|  | 6565 |  | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6566 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 6567 | CodeCompletionContext::CCC_Other, | 
|  | 6568 | Results.data(),Results.size()); | 
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6569 | } | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6570 |  | 
|  | 6571 | void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S, | 
|  | 6572 | bool IsInstanceMethod, | 
| Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 6573 | bool AtParameterName, | 
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6574 | ParsedType ReturnTy, | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6575 | IdentifierInfo **SelIdents, | 
|  | 6576 | unsigned NumSelIdents) { | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6577 | // If we have an external source, load the entire class method | 
| Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 6578 | // pool from the AST file. | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6579 | if (ExternalSource) { | 
|  | 6580 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); | 
|  | 6581 | I != N; ++I) { | 
|  | 6582 | Selector Sel = ExternalSource->GetExternalSelector(I); | 
| Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6583 | if (Sel.isNull() || MethodPool.count(Sel)) | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6584 | continue; | 
| Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6585 |  | 
|  | 6586 | ReadMethodPool(Sel); | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6587 | } | 
|  | 6588 | } | 
|  | 6589 |  | 
|  | 6590 | // Build the set of methods we can see. | 
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6591 | typedef CodeCompletionResult Result; | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6592 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
|  | 6593 | CodeCompletionContext::CCC_Other); | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6594 |  | 
|  | 6595 | if (ReturnTy) | 
|  | 6596 | Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType()); | 
| Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6597 |  | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6598 | Results.EnterNewScope(); | 
| Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6599 | for (GlobalMethodPool::iterator M = MethodPool.begin(), | 
|  | 6600 | MEnd = MethodPool.end(); | 
|  | 6601 | M != MEnd; ++M) { | 
|  | 6602 | for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first : | 
|  | 6603 | &M->second.second; | 
|  | 6604 | MethList && MethList->Method; | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6605 | MethList = MethList->Next) { | 
|  | 6606 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, | 
|  | 6607 | NumSelIdents)) | 
|  | 6608 | continue; | 
|  | 6609 |  | 
| Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 6610 | if (AtParameterName) { | 
|  | 6611 | // Suggest parameter names we've seen before. | 
|  | 6612 | if (NumSelIdents && NumSelIdents <= MethList->Method->param_size()) { | 
|  | 6613 | ParmVarDecl *Param = MethList->Method->param_begin()[NumSelIdents-1]; | 
|  | 6614 | if (Param->getIdentifier()) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6615 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6616 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6617 | Param->getIdentifier()->getName())); | 
|  | 6618 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 6619 | } | 
|  | 6620 | } | 
|  | 6621 |  | 
|  | 6622 | continue; | 
|  | 6623 | } | 
|  | 6624 |  | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6625 | Result R(MethList->Method, 0); | 
|  | 6626 | R.StartParameter = NumSelIdents; | 
|  | 6627 | R.AllParametersAreInformative = false; | 
|  | 6628 | R.DeclaringEntity = true; | 
|  | 6629 | Results.MaybeAddResult(R, CurContext); | 
|  | 6630 | } | 
|  | 6631 | } | 
|  | 6632 |  | 
|  | 6633 | Results.ExitScope(); | 
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6634 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 6635 | CodeCompletionContext::CCC_Other, | 
|  | 6636 | Results.data(),Results.size()); | 
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 6637 | } | 
| Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 6638 |  | 
| Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6639 | void Sema::CodeCompletePreprocessorDirective(bool InConditional) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6640 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 6641 | CodeCompletionContext::CCC_PreprocessorDirective); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6642 | Results.EnterNewScope(); | 
|  | 6643 |  | 
|  | 6644 | // #if <condition> | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6645 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
|  | 6646 | Builder.AddTypedTextChunk("if"); | 
|  | 6647 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6648 | Builder.AddPlaceholderChunk("condition"); | 
|  | 6649 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6650 |  | 
|  | 6651 | // #ifdef <macro> | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6652 | Builder.AddTypedTextChunk("ifdef"); | 
|  | 6653 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6654 | Builder.AddPlaceholderChunk("macro"); | 
|  | 6655 | Results.AddResult(Builder.TakeString()); | 
|  | 6656 |  | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6657 | // #ifndef <macro> | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6658 | Builder.AddTypedTextChunk("ifndef"); | 
|  | 6659 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6660 | Builder.AddPlaceholderChunk("macro"); | 
|  | 6661 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6662 |  | 
|  | 6663 | if (InConditional) { | 
|  | 6664 | // #elif <condition> | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6665 | Builder.AddTypedTextChunk("elif"); | 
|  | 6666 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6667 | Builder.AddPlaceholderChunk("condition"); | 
|  | 6668 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6669 |  | 
|  | 6670 | // #else | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6671 | Builder.AddTypedTextChunk("else"); | 
|  | 6672 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6673 |  | 
|  | 6674 | // #endif | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6675 | Builder.AddTypedTextChunk("endif"); | 
|  | 6676 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6677 | } | 
|  | 6678 |  | 
|  | 6679 | // #include "header" | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6680 | Builder.AddTypedTextChunk("include"); | 
|  | 6681 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6682 | Builder.AddTextChunk("\""); | 
|  | 6683 | Builder.AddPlaceholderChunk("header"); | 
|  | 6684 | Builder.AddTextChunk("\""); | 
|  | 6685 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6686 |  | 
|  | 6687 | // #include <header> | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6688 | Builder.AddTypedTextChunk("include"); | 
|  | 6689 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6690 | Builder.AddTextChunk("<"); | 
|  | 6691 | Builder.AddPlaceholderChunk("header"); | 
|  | 6692 | Builder.AddTextChunk(">"); | 
|  | 6693 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6694 |  | 
|  | 6695 | // #define <macro> | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6696 | Builder.AddTypedTextChunk("define"); | 
|  | 6697 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6698 | Builder.AddPlaceholderChunk("macro"); | 
|  | 6699 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6700 |  | 
|  | 6701 | // #define <macro>(<args>) | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6702 | Builder.AddTypedTextChunk("define"); | 
|  | 6703 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6704 | Builder.AddPlaceholderChunk("macro"); | 
|  | 6705 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6706 | Builder.AddPlaceholderChunk("args"); | 
|  | 6707 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6708 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6709 |  | 
|  | 6710 | // #undef <macro> | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6711 | Builder.AddTypedTextChunk("undef"); | 
|  | 6712 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6713 | Builder.AddPlaceholderChunk("macro"); | 
|  | 6714 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6715 |  | 
|  | 6716 | // #line <number> | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6717 | Builder.AddTypedTextChunk("line"); | 
|  | 6718 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6719 | Builder.AddPlaceholderChunk("number"); | 
|  | 6720 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6721 |  | 
|  | 6722 | // #line <number> "filename" | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6723 | Builder.AddTypedTextChunk("line"); | 
|  | 6724 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6725 | Builder.AddPlaceholderChunk("number"); | 
|  | 6726 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6727 | Builder.AddTextChunk("\""); | 
|  | 6728 | Builder.AddPlaceholderChunk("filename"); | 
|  | 6729 | Builder.AddTextChunk("\""); | 
|  | 6730 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6731 |  | 
|  | 6732 | // #error <message> | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6733 | Builder.AddTypedTextChunk("error"); | 
|  | 6734 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6735 | Builder.AddPlaceholderChunk("message"); | 
|  | 6736 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6737 |  | 
|  | 6738 | // #pragma <arguments> | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6739 | Builder.AddTypedTextChunk("pragma"); | 
|  | 6740 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6741 | Builder.AddPlaceholderChunk("arguments"); | 
|  | 6742 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6743 |  | 
|  | 6744 | if (getLangOptions().ObjC1) { | 
|  | 6745 | // #import "header" | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6746 | Builder.AddTypedTextChunk("import"); | 
|  | 6747 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6748 | Builder.AddTextChunk("\""); | 
|  | 6749 | Builder.AddPlaceholderChunk("header"); | 
|  | 6750 | Builder.AddTextChunk("\""); | 
|  | 6751 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6752 |  | 
|  | 6753 | // #import <header> | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6754 | Builder.AddTypedTextChunk("import"); | 
|  | 6755 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6756 | Builder.AddTextChunk("<"); | 
|  | 6757 | Builder.AddPlaceholderChunk("header"); | 
|  | 6758 | Builder.AddTextChunk(">"); | 
|  | 6759 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6760 | } | 
|  | 6761 |  | 
|  | 6762 | // #include_next "header" | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6763 | Builder.AddTypedTextChunk("include_next"); | 
|  | 6764 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6765 | Builder.AddTextChunk("\""); | 
|  | 6766 | Builder.AddPlaceholderChunk("header"); | 
|  | 6767 | Builder.AddTextChunk("\""); | 
|  | 6768 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6769 |  | 
|  | 6770 | // #include_next <header> | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6771 | Builder.AddTypedTextChunk("include_next"); | 
|  | 6772 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6773 | Builder.AddTextChunk("<"); | 
|  | 6774 | Builder.AddPlaceholderChunk("header"); | 
|  | 6775 | Builder.AddTextChunk(">"); | 
|  | 6776 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6777 |  | 
|  | 6778 | // #warning <message> | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6779 | Builder.AddTypedTextChunk("warning"); | 
|  | 6780 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6781 | Builder.AddPlaceholderChunk("message"); | 
|  | 6782 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6783 |  | 
|  | 6784 | // Note: #ident and #sccs are such crazy anachronisms that we don't provide | 
|  | 6785 | // completions for them. And __include_macros is a Clang-internal extension | 
|  | 6786 | // that we don't want to encourage anyone to use. | 
|  | 6787 |  | 
|  | 6788 | // FIXME: we don't support #assert or #unassert, so don't suggest them. | 
|  | 6789 | Results.ExitScope(); | 
|  | 6790 |  | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6791 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 6792 | CodeCompletionContext::CCC_PreprocessorDirective, | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6793 | Results.data(), Results.size()); | 
|  | 6794 | } | 
|  | 6795 |  | 
|  | 6796 | void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) { | 
| Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6797 | CodeCompleteOrdinaryName(S, | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6798 | S->getFnParent()? Sema::PCC_RecoveryInFunction | 
|  | 6799 | : Sema::PCC_Namespace); | 
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 6800 | } | 
|  | 6801 |  | 
| Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6802 | void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6803 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 6804 | IsDefinition? CodeCompletionContext::CCC_MacroName | 
|  | 6805 | : CodeCompletionContext::CCC_MacroNameUse); | 
| Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 6806 | if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) { | 
|  | 6807 | // Add just the names of macros, not their arguments. | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6808 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
| Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 6809 | Results.EnterNewScope(); | 
|  | 6810 | for (Preprocessor::macro_iterator M = PP.macro_begin(), | 
|  | 6811 | MEnd = PP.macro_end(); | 
|  | 6812 | M != MEnd; ++M) { | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6813 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6814 | M->first->getName())); | 
|  | 6815 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 6816 | } | 
|  | 6817 | Results.ExitScope(); | 
|  | 6818 | } else if (IsDefinition) { | 
|  | 6819 | // FIXME: Can we detect when the user just wrote an include guard above? | 
|  | 6820 | } | 
|  | 6821 |  | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 6822 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), | 
| Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 6823 | Results.data(), Results.size()); | 
|  | 6824 | } | 
|  | 6825 |  | 
| Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6826 | void Sema::CodeCompletePreprocessorExpression() { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6827 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | 
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 6828 | CodeCompletionContext::CCC_PreprocessorExpression); | 
| Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6829 |  | 
|  | 6830 | if (!CodeCompleter || CodeCompleter->includeMacros()) | 
|  | 6831 | AddMacroResults(PP, Results); | 
|  | 6832 |  | 
|  | 6833 | // defined (<macro>) | 
|  | 6834 | Results.EnterNewScope(); | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6835 | CodeCompletionBuilder Builder(Results.getAllocator()); | 
|  | 6836 | Builder.AddTypedTextChunk("defined"); | 
|  | 6837 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | 
|  | 6838 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | 
|  | 6839 | Builder.AddPlaceholderChunk("macro"); | 
|  | 6840 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | 
|  | 6841 | Results.AddResult(Builder.TakeString()); | 
| Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6842 | Results.ExitScope(); | 
|  | 6843 |  | 
|  | 6844 | HandleCodeCompleteResults(this, CodeCompleter, | 
|  | 6845 | CodeCompletionContext::CCC_PreprocessorExpression, | 
|  | 6846 | Results.data(), Results.size()); | 
|  | 6847 | } | 
|  | 6848 |  | 
|  | 6849 | void Sema::CodeCompletePreprocessorMacroArgument(Scope *S, | 
|  | 6850 | IdentifierInfo *Macro, | 
|  | 6851 | MacroInfo *MacroInfo, | 
|  | 6852 | unsigned Argument) { | 
|  | 6853 | // FIXME: In the future, we could provide "overload" results, much like we | 
|  | 6854 | // do for function calls. | 
|  | 6855 |  | 
| Argyrios Kyrtzidis | 75f6cd2 | 2011-08-18 19:41:28 +0000 | [diff] [blame] | 6856 | // Now just ignore this. There will be another code-completion callback | 
|  | 6857 | // for the expanded tokens. | 
| Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 6858 | } | 
|  | 6859 |  | 
| Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 6860 | void Sema::CodeCompleteNaturalLanguage() { | 
| Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 6861 | HandleCodeCompleteResults(this, CodeCompleter, | 
| Douglas Gregor | ea73637 | 2010-08-25 17:10:00 +0000 | [diff] [blame] | 6862 | CodeCompletionContext::CCC_NaturalLanguage, | 
| Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 6863 | 0, 0); | 
|  | 6864 | } | 
|  | 6865 |  | 
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6866 | void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator, | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6867 | SmallVectorImpl<CodeCompletionResult> &Results) { | 
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6868 | ResultBuilder Builder(*this, Allocator, CodeCompletionContext::CCC_Recovery); | 
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 6869 | if (!CodeCompleter || CodeCompleter->includeGlobals()) { | 
|  | 6870 | CodeCompletionDeclConsumer Consumer(Builder, | 
|  | 6871 | Context.getTranslationUnitDecl()); | 
|  | 6872 | LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName, | 
|  | 6873 | Consumer); | 
|  | 6874 | } | 
| Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 6875 |  | 
|  | 6876 | if (!CodeCompleter || CodeCompleter->includeMacros()) | 
|  | 6877 | AddMacroResults(PP, Builder); | 
|  | 6878 |  | 
|  | 6879 | Results.clear(); | 
|  | 6880 | Results.insert(Results.end(), | 
|  | 6881 | Builder.data(), Builder.data() + Builder.size()); | 
|  | 6882 | } |