Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1 | //===---------------- SemaCodeComplete.cpp - Code Completion ----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the code-completion semantic actions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 13 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Lookup.h" |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Overload.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 16 | #include "clang/Sema/CodeCompleteConsumer.h" |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ExternalSemaSource.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Scope.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ScopeInfo.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 23 | #include "clang/Lex/MacroInfo.h" |
| 24 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 6a68403 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringSwitch.h" |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 29 | #include <list> |
| 30 | #include <map> |
| 31 | #include <vector> |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 34 | using namespace sema; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 35 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | /// \brief A container of code-completion results. |
| 38 | class ResultBuilder { |
| 39 | public: |
| 40 | /// \brief The type of a name-lookup filter, which can be provided to the |
| 41 | /// name-lookup routines to specify which declarations should be included in |
| 42 | /// the result set (when it returns true) and which declarations should be |
| 43 | /// filtered out (returns false). |
| 44 | typedef bool (ResultBuilder::*LookupFilter)(NamedDecl *) const; |
| 45 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 46 | typedef CodeCompletionResult Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 47 | |
| 48 | private: |
| 49 | /// \brief The actual results we have found. |
| 50 | std::vector<Result> Results; |
| 51 | |
| 52 | /// \brief A record of all of the declarations we have found and placed |
| 53 | /// into the result set, used to ensure that no declaration ever gets into |
| 54 | /// the result set twice. |
| 55 | llvm::SmallPtrSet<Decl*, 16> AllDeclsFound; |
| 56 | |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 57 | typedef std::pair<NamedDecl *, unsigned> DeclIndexPair; |
| 58 | |
| 59 | /// \brief An entry in the shadow map, which is optimized to store |
| 60 | /// a single (declaration, index) mapping (the common case) but |
| 61 | /// can also store a list of (declaration, index) mappings. |
| 62 | class ShadowMapEntry { |
| 63 | typedef llvm::SmallVector<DeclIndexPair, 4> DeclIndexPairVector; |
| 64 | |
| 65 | /// \brief Contains either the solitary NamedDecl * or a vector |
| 66 | /// of (declaration, index) pairs. |
| 67 | llvm::PointerUnion<NamedDecl *, DeclIndexPairVector*> DeclOrVector; |
| 68 | |
| 69 | /// \brief When the entry contains a single declaration, this is |
| 70 | /// the index associated with that entry. |
| 71 | unsigned SingleDeclIndex; |
| 72 | |
| 73 | public: |
| 74 | ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { } |
| 75 | |
| 76 | void Add(NamedDecl *ND, unsigned Index) { |
| 77 | if (DeclOrVector.isNull()) { |
| 78 | // 0 - > 1 elements: just set the single element information. |
| 79 | DeclOrVector = ND; |
| 80 | SingleDeclIndex = Index; |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | if (NamedDecl *PrevND = DeclOrVector.dyn_cast<NamedDecl *>()) { |
| 85 | // 1 -> 2 elements: create the vector of results and push in the |
| 86 | // existing declaration. |
| 87 | DeclIndexPairVector *Vec = new DeclIndexPairVector; |
| 88 | Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex)); |
| 89 | DeclOrVector = Vec; |
| 90 | } |
| 91 | |
| 92 | // Add the new element to the end of the vector. |
| 93 | DeclOrVector.get<DeclIndexPairVector*>()->push_back( |
| 94 | DeclIndexPair(ND, Index)); |
| 95 | } |
| 96 | |
| 97 | void Destroy() { |
| 98 | if (DeclIndexPairVector *Vec |
| 99 | = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) { |
| 100 | delete Vec; |
| 101 | DeclOrVector = ((NamedDecl *)0); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Iteration. |
| 106 | class iterator; |
| 107 | iterator begin() const; |
| 108 | iterator end() const; |
| 109 | }; |
| 110 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 111 | /// \brief A mapping from declaration names to the declarations that have |
| 112 | /// this name within a particular scope and their index within the list of |
| 113 | /// results. |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 114 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 115 | |
| 116 | /// \brief The semantic analysis object for which results are being |
| 117 | /// produced. |
| 118 | Sema &SemaRef; |
| 119 | |
| 120 | /// \brief If non-NULL, a filter function used to remove any code-completion |
| 121 | /// results that are not desirable. |
| 122 | LookupFilter Filter; |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 123 | |
| 124 | /// \brief Whether we should allow declarations as |
| 125 | /// nested-name-specifiers that would otherwise be filtered out. |
| 126 | bool AllowNestedNameSpecifiers; |
| 127 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 128 | /// \brief If set, the type that we would prefer our resulting value |
| 129 | /// declarations to have. |
| 130 | /// |
| 131 | /// Closely matching the preferred type gives a boost to a result's |
| 132 | /// priority. |
| 133 | CanQualType PreferredType; |
| 134 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 135 | /// \brief A list of shadow maps, which is used to model name hiding at |
| 136 | /// different levels of, e.g., the inheritance hierarchy. |
| 137 | std::list<ShadowMap> ShadowMaps; |
| 138 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 139 | /// \brief If we're potentially referring to a C++ member function, the set |
| 140 | /// of qualifiers applied to the object type. |
| 141 | Qualifiers ObjectTypeQualifiers; |
| 142 | |
| 143 | /// \brief Whether the \p ObjectTypeQualifiers field is active. |
| 144 | bool HasObjectTypeQualifiers; |
| 145 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 146 | /// \brief The selector that we prefer. |
| 147 | Selector PreferredSelector; |
| 148 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 149 | void AdjustResultPriorityForPreferredType(Result &R); |
| 150 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 151 | public: |
| 152 | explicit ResultBuilder(Sema &SemaRef, LookupFilter Filter = 0) |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 153 | : SemaRef(SemaRef), Filter(Filter), AllowNestedNameSpecifiers(false), |
| 154 | HasObjectTypeQualifiers(false) { } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 155 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 156 | /// \brief Whether we should include code patterns in the completion |
| 157 | /// results. |
| 158 | bool includeCodePatterns() const { |
| 159 | return SemaRef.CodeCompleter && |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 160 | SemaRef.CodeCompleter->includeCodePatterns(); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 163 | /// \brief Set the filter used for code-completion results. |
| 164 | void setFilter(LookupFilter Filter) { |
| 165 | this->Filter = Filter; |
| 166 | } |
| 167 | |
| 168 | typedef std::vector<Result>::iterator iterator; |
| 169 | iterator begin() { return Results.begin(); } |
| 170 | iterator end() { return Results.end(); } |
| 171 | |
| 172 | Result *data() { return Results.empty()? 0 : &Results.front(); } |
| 173 | unsigned size() const { return Results.size(); } |
| 174 | bool empty() const { return Results.empty(); } |
| 175 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 176 | /// \brief Specify the preferred type. |
| 177 | void setPreferredType(QualType T) { |
| 178 | PreferredType = SemaRef.Context.getCanonicalType(T); |
| 179 | } |
| 180 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 181 | /// \brief Set the cv-qualifiers on the object type, for us in filtering |
| 182 | /// calls to member functions. |
| 183 | /// |
| 184 | /// When there are qualifiers in this set, they will be used to filter |
| 185 | /// out member functions that aren't available (because there will be a |
| 186 | /// cv-qualifier mismatch) or prefer functions with an exact qualifier |
| 187 | /// match. |
| 188 | void setObjectTypeQualifiers(Qualifiers Quals) { |
| 189 | ObjectTypeQualifiers = Quals; |
| 190 | HasObjectTypeQualifiers = true; |
| 191 | } |
| 192 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 193 | /// \brief Set the preferred selector. |
| 194 | /// |
| 195 | /// When an Objective-C method declaration result is added, and that |
| 196 | /// method's selector matches this preferred selector, we give that method |
| 197 | /// a slight priority boost. |
| 198 | void setPreferredSelector(Selector Sel) { |
| 199 | PreferredSelector = Sel; |
| 200 | } |
| 201 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 202 | /// \brief Specify whether nested-name-specifiers are allowed. |
| 203 | void allowNestedNameSpecifiers(bool Allow = true) { |
| 204 | AllowNestedNameSpecifiers = Allow; |
| 205 | } |
| 206 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 207 | /// \brief Determine whether the given declaration is at all interesting |
| 208 | /// as a code-completion result. |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 209 | /// |
| 210 | /// \param ND the declaration that we are inspecting. |
| 211 | /// |
| 212 | /// \param AsNestedNameSpecifier will be set true if this declaration is |
| 213 | /// only interesting when it is a nested-name-specifier. |
| 214 | bool isInterestingDecl(NamedDecl *ND, bool &AsNestedNameSpecifier) const; |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 215 | |
| 216 | /// \brief Check whether the result is hidden by the Hiding declaration. |
| 217 | /// |
| 218 | /// \returns true if the result is hidden and cannot be found, false if |
| 219 | /// the hidden result could still be found. When false, \p R may be |
| 220 | /// modified to describe how the result can be found (e.g., via extra |
| 221 | /// qualification). |
| 222 | bool CheckHiddenResult(Result &R, DeclContext *CurContext, |
| 223 | NamedDecl *Hiding); |
| 224 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 225 | /// \brief Add a new result to this result set (if it isn't already in one |
| 226 | /// of the shadow maps), or replace an existing result (for, e.g., a |
| 227 | /// redeclaration). |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 228 | /// |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 229 | /// \param CurContext the result to add (if it is unique). |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 230 | /// |
| 231 | /// \param R the context in which this result will be named. |
| 232 | void MaybeAddResult(Result R, DeclContext *CurContext = 0); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 233 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 234 | /// \brief Add a new result to this result set, where we already know |
| 235 | /// the hiding declation (if any). |
| 236 | /// |
| 237 | /// \param R the result to add (if it is unique). |
| 238 | /// |
| 239 | /// \param CurContext the context in which this result will be named. |
| 240 | /// |
| 241 | /// \param Hiding the declaration that hides the result. |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 242 | /// |
| 243 | /// \param InBaseClass whether the result was found in a base |
| 244 | /// class of the searched context. |
| 245 | void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding, |
| 246 | bool InBaseClass); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 247 | |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 248 | /// \brief Add a new non-declaration result to this result set. |
| 249 | void AddResult(Result R); |
| 250 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 251 | /// \brief Enter into a new scope. |
| 252 | void EnterNewScope(); |
| 253 | |
| 254 | /// \brief Exit from the current scope. |
| 255 | void ExitScope(); |
| 256 | |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 257 | /// \brief Ignore this declaration, if it is seen again. |
| 258 | void Ignore(Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); } |
| 259 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 260 | /// \name Name lookup predicates |
| 261 | /// |
| 262 | /// These predicates can be passed to the name lookup functions to filter the |
| 263 | /// results of name lookup. All of the predicates have the same type, so that |
| 264 | /// |
| 265 | //@{ |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 266 | bool IsOrdinaryName(NamedDecl *ND) const; |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 267 | bool IsOrdinaryNonTypeName(NamedDecl *ND) const; |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 268 | bool IsIntegralConstantValue(NamedDecl *ND) const; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 269 | bool IsOrdinaryNonValueName(NamedDecl *ND) const; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 270 | bool IsNestedNameSpecifier(NamedDecl *ND) const; |
| 271 | bool IsEnum(NamedDecl *ND) const; |
| 272 | bool IsClassOrStruct(NamedDecl *ND) const; |
| 273 | bool IsUnion(NamedDecl *ND) const; |
| 274 | bool IsNamespace(NamedDecl *ND) const; |
| 275 | bool IsNamespaceOrAlias(NamedDecl *ND) const; |
| 276 | bool IsType(NamedDecl *ND) const; |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 277 | bool IsMember(NamedDecl *ND) const; |
Douglas Gregor | 80f4f4c | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 278 | bool IsObjCIvar(NamedDecl *ND) const; |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 279 | bool IsObjCMessageReceiver(NamedDecl *ND) const; |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 280 | bool IsObjCCollection(NamedDecl *ND) const; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 281 | //@} |
| 282 | }; |
| 283 | } |
| 284 | |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 285 | class ResultBuilder::ShadowMapEntry::iterator { |
| 286 | llvm::PointerUnion<NamedDecl*, const DeclIndexPair*> DeclOrIterator; |
| 287 | unsigned SingleDeclIndex; |
| 288 | |
| 289 | public: |
| 290 | typedef DeclIndexPair value_type; |
| 291 | typedef value_type reference; |
| 292 | typedef std::ptrdiff_t difference_type; |
| 293 | typedef std::input_iterator_tag iterator_category; |
| 294 | |
| 295 | class pointer { |
| 296 | DeclIndexPair Value; |
| 297 | |
| 298 | public: |
| 299 | pointer(const DeclIndexPair &Value) : Value(Value) { } |
| 300 | |
| 301 | const DeclIndexPair *operator->() const { |
| 302 | return &Value; |
| 303 | } |
| 304 | }; |
| 305 | |
| 306 | iterator() : DeclOrIterator((NamedDecl *)0), SingleDeclIndex(0) { } |
| 307 | |
| 308 | iterator(NamedDecl *SingleDecl, unsigned Index) |
| 309 | : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { } |
| 310 | |
| 311 | iterator(const DeclIndexPair *Iterator) |
| 312 | : DeclOrIterator(Iterator), SingleDeclIndex(0) { } |
| 313 | |
| 314 | iterator &operator++() { |
| 315 | if (DeclOrIterator.is<NamedDecl *>()) { |
| 316 | DeclOrIterator = (NamedDecl *)0; |
| 317 | SingleDeclIndex = 0; |
| 318 | return *this; |
| 319 | } |
| 320 | |
| 321 | const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>(); |
| 322 | ++I; |
| 323 | DeclOrIterator = I; |
| 324 | return *this; |
| 325 | } |
| 326 | |
| 327 | iterator operator++(int) { |
| 328 | iterator tmp(*this); |
| 329 | ++(*this); |
| 330 | return tmp; |
| 331 | } |
| 332 | |
| 333 | reference operator*() const { |
| 334 | if (NamedDecl *ND = DeclOrIterator.dyn_cast<NamedDecl *>()) |
| 335 | return reference(ND, SingleDeclIndex); |
| 336 | |
Douglas Gregor | d490f95 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 337 | return *DeclOrIterator.get<const DeclIndexPair*>(); |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | pointer operator->() const { |
| 341 | return pointer(**this); |
| 342 | } |
| 343 | |
| 344 | friend bool operator==(const iterator &X, const iterator &Y) { |
Douglas Gregor | d490f95 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 345 | return X.DeclOrIterator.getOpaqueValue() |
| 346 | == Y.DeclOrIterator.getOpaqueValue() && |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 347 | X.SingleDeclIndex == Y.SingleDeclIndex; |
| 348 | } |
| 349 | |
| 350 | friend bool operator!=(const iterator &X, const iterator &Y) { |
Douglas Gregor | d490f95 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 351 | return !(X == Y); |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 352 | } |
| 353 | }; |
| 354 | |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 355 | ResultBuilder::ShadowMapEntry::iterator |
| 356 | ResultBuilder::ShadowMapEntry::begin() const { |
| 357 | if (DeclOrVector.isNull()) |
| 358 | return iterator(); |
| 359 | |
| 360 | if (NamedDecl *ND = DeclOrVector.dyn_cast<NamedDecl *>()) |
| 361 | return iterator(ND, SingleDeclIndex); |
| 362 | |
| 363 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin()); |
| 364 | } |
| 365 | |
| 366 | ResultBuilder::ShadowMapEntry::iterator |
| 367 | ResultBuilder::ShadowMapEntry::end() const { |
| 368 | if (DeclOrVector.is<NamedDecl *>() || DeclOrVector.isNull()) |
| 369 | return iterator(); |
| 370 | |
| 371 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end()); |
| 372 | } |
| 373 | |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 374 | /// \brief Compute the qualification required to get from the current context |
| 375 | /// (\p CurContext) to the target context (\p TargetContext). |
| 376 | /// |
| 377 | /// \param Context the AST context in which the qualification will be used. |
| 378 | /// |
| 379 | /// \param CurContext the context where an entity is being named, which is |
| 380 | /// typically based on the current scope. |
| 381 | /// |
| 382 | /// \param TargetContext the context in which the named entity actually |
| 383 | /// resides. |
| 384 | /// |
| 385 | /// \returns a nested name specifier that refers into the target context, or |
| 386 | /// NULL if no qualification is needed. |
| 387 | static NestedNameSpecifier * |
| 388 | getRequiredQualification(ASTContext &Context, |
| 389 | DeclContext *CurContext, |
| 390 | DeclContext *TargetContext) { |
| 391 | llvm::SmallVector<DeclContext *, 4> TargetParents; |
| 392 | |
| 393 | for (DeclContext *CommonAncestor = TargetContext; |
| 394 | CommonAncestor && !CommonAncestor->Encloses(CurContext); |
| 395 | CommonAncestor = CommonAncestor->getLookupParent()) { |
| 396 | if (CommonAncestor->isTransparentContext() || |
| 397 | CommonAncestor->isFunctionOrMethod()) |
| 398 | continue; |
| 399 | |
| 400 | TargetParents.push_back(CommonAncestor); |
| 401 | } |
| 402 | |
| 403 | NestedNameSpecifier *Result = 0; |
| 404 | while (!TargetParents.empty()) { |
| 405 | DeclContext *Parent = TargetParents.back(); |
| 406 | TargetParents.pop_back(); |
| 407 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 408 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) { |
| 409 | if (!Namespace->getIdentifier()) |
| 410 | continue; |
| 411 | |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 412 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 413 | } |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 414 | else if (TagDecl *TD = dyn_cast<TagDecl>(Parent)) |
| 415 | Result = NestedNameSpecifier::Create(Context, Result, |
| 416 | false, |
| 417 | Context.getTypeDeclType(TD).getTypePtr()); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 418 | } |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 419 | return Result; |
| 420 | } |
| 421 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 422 | bool ResultBuilder::isInterestingDecl(NamedDecl *ND, |
| 423 | bool &AsNestedNameSpecifier) const { |
| 424 | AsNestedNameSpecifier = false; |
| 425 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 426 | ND = ND->getUnderlyingDecl(); |
| 427 | unsigned IDNS = ND->getIdentifierNamespace(); |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 428 | |
| 429 | // Skip unnamed entities. |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 430 | if (!ND->getDeclName()) |
| 431 | return false; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 432 | |
| 433 | // Friend declarations and declarations introduced due to friends are never |
| 434 | // added as results. |
John McCall | 92b7f70 | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 435 | if (IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend)) |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 436 | return false; |
| 437 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 438 | // Class template (partial) specializations are never added as results. |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 439 | if (isa<ClassTemplateSpecializationDecl>(ND) || |
| 440 | isa<ClassTemplatePartialSpecializationDecl>(ND)) |
| 441 | return false; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 442 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 443 | // Using declarations themselves are never added as results. |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 444 | if (isa<UsingDecl>(ND)) |
| 445 | return false; |
| 446 | |
| 447 | // Some declarations have reserved names that we don't want to ever show. |
| 448 | if (const IdentifierInfo *Id = ND->getIdentifier()) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 449 | // __va_list_tag is a freak of nature. Find it and skip it. |
| 450 | if (Id->isStr("__va_list_tag") || Id->isStr("__builtin_va_list")) |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 451 | return false; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 452 | |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 453 | // Filter out names reserved for the implementation (C99 7.1.3, |
Douglas Gregor | 797efb5 | 2010-07-14 17:44:04 +0000 | [diff] [blame] | 454 | // C++ [lib.global.names]) if they come from a system header. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 455 | // |
| 456 | // FIXME: Add predicate for this. |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 457 | if (Id->getLength() >= 2) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 458 | const char *Name = Id->getNameStart(); |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 459 | if (Name[0] == '_' && |
Douglas Gregor | 797efb5 | 2010-07-14 17:44:04 +0000 | [diff] [blame] | 460 | (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z')) && |
| 461 | (ND->getLocation().isInvalid() || |
| 462 | SemaRef.SourceMgr.isInSystemHeader( |
| 463 | SemaRef.SourceMgr.getSpellingLoc(ND->getLocation())))) |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 464 | return false; |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 465 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 466 | } |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 467 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 468 | // C++ constructors are never found by name lookup. |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 469 | if (isa<CXXConstructorDecl>(ND)) |
| 470 | return false; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 471 | |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 472 | if (Filter == &ResultBuilder::IsNestedNameSpecifier || |
| 473 | ((isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) && |
| 474 | Filter != &ResultBuilder::IsNamespace && |
| 475 | Filter != &ResultBuilder::IsNamespaceOrAlias)) |
| 476 | AsNestedNameSpecifier = true; |
| 477 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 478 | // Filter out any unwanted results. |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 479 | if (Filter && !(this->*Filter)(ND)) { |
| 480 | // Check whether it is interesting as a nested-name-specifier. |
| 481 | if (AllowNestedNameSpecifiers && SemaRef.getLangOptions().CPlusPlus && |
| 482 | IsNestedNameSpecifier(ND) && |
| 483 | (Filter != &ResultBuilder::IsMember || |
| 484 | (isa<CXXRecordDecl>(ND) && |
| 485 | cast<CXXRecordDecl>(ND)->isInjectedClassName()))) { |
| 486 | AsNestedNameSpecifier = true; |
| 487 | return true; |
| 488 | } |
| 489 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 490 | return false; |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 491 | } |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 492 | // ... then it must be interesting! |
| 493 | return true; |
| 494 | } |
| 495 | |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 496 | bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext, |
| 497 | NamedDecl *Hiding) { |
| 498 | // In C, there is no way to refer to a hidden name. |
| 499 | // FIXME: This isn't true; we can find a tag name hidden by an ordinary |
| 500 | // name if we introduce the tag type. |
| 501 | if (!SemaRef.getLangOptions().CPlusPlus) |
| 502 | return true; |
| 503 | |
| 504 | DeclContext *HiddenCtx = R.Declaration->getDeclContext()->getLookupContext(); |
| 505 | |
| 506 | // There is no way to qualify a name declared in a function or method. |
| 507 | if (HiddenCtx->isFunctionOrMethod()) |
| 508 | return true; |
| 509 | |
| 510 | if (HiddenCtx == Hiding->getDeclContext()->getLookupContext()) |
| 511 | return true; |
| 512 | |
| 513 | // We can refer to the result with the appropriate qualification. Do it. |
| 514 | R.Hidden = true; |
| 515 | R.QualifierIsInformative = false; |
| 516 | |
| 517 | if (!R.Qualifier) |
| 518 | R.Qualifier = getRequiredQualification(SemaRef.Context, |
| 519 | CurContext, |
| 520 | R.Declaration->getDeclContext()); |
| 521 | return false; |
| 522 | } |
| 523 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 524 | /// \brief A simplified classification of types used to determine whether two |
| 525 | /// types are "similar enough" when adjusting priorities. |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 526 | SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 527 | switch (T->getTypeClass()) { |
| 528 | case Type::Builtin: |
| 529 | switch (cast<BuiltinType>(T)->getKind()) { |
| 530 | case BuiltinType::Void: |
| 531 | return STC_Void; |
| 532 | |
| 533 | case BuiltinType::NullPtr: |
| 534 | return STC_Pointer; |
| 535 | |
| 536 | case BuiltinType::Overload: |
| 537 | case BuiltinType::Dependent: |
| 538 | case BuiltinType::UndeducedAuto: |
| 539 | return STC_Other; |
| 540 | |
| 541 | case BuiltinType::ObjCId: |
| 542 | case BuiltinType::ObjCClass: |
| 543 | case BuiltinType::ObjCSel: |
| 544 | return STC_ObjectiveC; |
| 545 | |
| 546 | default: |
| 547 | return STC_Arithmetic; |
| 548 | } |
| 549 | return STC_Other; |
| 550 | |
| 551 | case Type::Complex: |
| 552 | return STC_Arithmetic; |
| 553 | |
| 554 | case Type::Pointer: |
| 555 | return STC_Pointer; |
| 556 | |
| 557 | case Type::BlockPointer: |
| 558 | return STC_Block; |
| 559 | |
| 560 | case Type::LValueReference: |
| 561 | case Type::RValueReference: |
| 562 | return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType()); |
| 563 | |
| 564 | case Type::ConstantArray: |
| 565 | case Type::IncompleteArray: |
| 566 | case Type::VariableArray: |
| 567 | case Type::DependentSizedArray: |
| 568 | return STC_Array; |
| 569 | |
| 570 | case Type::DependentSizedExtVector: |
| 571 | case Type::Vector: |
| 572 | case Type::ExtVector: |
| 573 | return STC_Arithmetic; |
| 574 | |
| 575 | case Type::FunctionProto: |
| 576 | case Type::FunctionNoProto: |
| 577 | return STC_Function; |
| 578 | |
| 579 | case Type::Record: |
| 580 | return STC_Record; |
| 581 | |
| 582 | case Type::Enum: |
| 583 | return STC_Arithmetic; |
| 584 | |
| 585 | case Type::ObjCObject: |
| 586 | case Type::ObjCInterface: |
| 587 | case Type::ObjCObjectPointer: |
| 588 | return STC_ObjectiveC; |
| 589 | |
| 590 | default: |
| 591 | return STC_Other; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /// \brief Get the type that a given expression will have if this declaration |
| 596 | /// is used as an expression in its "typical" code-completion form. |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 597 | QualType clang::getDeclUsageType(ASTContext &C, NamedDecl *ND) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 598 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 599 | |
| 600 | if (TypeDecl *Type = dyn_cast<TypeDecl>(ND)) |
| 601 | return C.getTypeDeclType(Type); |
| 602 | if (ObjCInterfaceDecl *Iface = dyn_cast<ObjCInterfaceDecl>(ND)) |
| 603 | return C.getObjCInterfaceType(Iface); |
| 604 | |
| 605 | QualType T; |
| 606 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 607 | T = Function->getCallResultType(); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 608 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 609 | T = Method->getSendResultType(); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 610 | else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 611 | T = FunTmpl->getTemplatedDecl()->getCallResultType(); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 612 | else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
| 613 | T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext())); |
| 614 | else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
| 615 | T = Property->getType(); |
| 616 | else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND)) |
| 617 | T = Value->getType(); |
| 618 | else |
| 619 | return QualType(); |
| 620 | |
| 621 | return T.getNonReferenceType(); |
| 622 | } |
| 623 | |
| 624 | void ResultBuilder::AdjustResultPriorityForPreferredType(Result &R) { |
| 625 | QualType T = getDeclUsageType(SemaRef.Context, R.Declaration); |
| 626 | if (T.isNull()) |
| 627 | return; |
| 628 | |
| 629 | CanQualType TC = SemaRef.Context.getCanonicalType(T); |
| 630 | // Check for exactly-matching types (modulo qualifiers). |
Douglas Gregor | eb0d014 | 2010-08-24 23:58:17 +0000 | [diff] [blame] | 631 | if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) { |
| 632 | if (PreferredType->isVoidType()) |
| 633 | R.Priority += CCD_VoidMatch; |
| 634 | else |
| 635 | R.Priority /= CCF_ExactTypeMatch; |
| 636 | } // Check for nearly-matching types, based on classification of each. |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 637 | else if ((getSimplifiedTypeClass(PreferredType) |
| 638 | == getSimplifiedTypeClass(TC)) && |
| 639 | !(PreferredType->isEnumeralType() && TC->isEnumeralType())) |
| 640 | R.Priority /= CCF_SimilarTypeMatch; |
| 641 | } |
| 642 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 643 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { |
| 644 | assert(!ShadowMaps.empty() && "Must enter into a results scope"); |
| 645 | |
| 646 | if (R.Kind != Result::RK_Declaration) { |
| 647 | // For non-declaration results, just add the result. |
| 648 | Results.push_back(R); |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | // Look through using declarations. |
| 653 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
| 654 | MaybeAddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext); |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
| 659 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 660 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 661 | bool AsNestedNameSpecifier = false; |
| 662 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 663 | return; |
| 664 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 665 | ShadowMap &SMap = ShadowMaps.back(); |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 666 | ShadowMapEntry::iterator I, IEnd; |
| 667 | ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName()); |
| 668 | if (NamePos != SMap.end()) { |
| 669 | I = NamePos->second.begin(); |
| 670 | IEnd = NamePos->second.end(); |
| 671 | } |
| 672 | |
| 673 | for (; I != IEnd; ++I) { |
| 674 | NamedDecl *ND = I->first; |
| 675 | unsigned Index = I->second; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 676 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 677 | // This is a redeclaration. Always pick the newer declaration. |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 678 | Results[Index].Declaration = R.Declaration; |
| 679 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 680 | // We're done. |
| 681 | return; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | // This is a new declaration in this scope. However, check whether this |
| 686 | // declaration name is hidden by a similarly-named declaration in an outer |
| 687 | // scope. |
| 688 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 689 | --SMEnd; |
| 690 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 691 | ShadowMapEntry::iterator I, IEnd; |
| 692 | ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName()); |
| 693 | if (NamePos != SM->end()) { |
| 694 | I = NamePos->second.begin(); |
| 695 | IEnd = NamePos->second.end(); |
| 696 | } |
| 697 | for (; I != IEnd; ++I) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 698 | // A tag declaration does not hide a non-tag declaration. |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 699 | if (I->first->hasTagIdentifierNamespace() && |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 700 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 701 | Decl::IDNS_ObjCProtocol))) |
| 702 | continue; |
| 703 | |
| 704 | // Protocols are in distinct namespaces from everything else. |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 705 | if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 706 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 707 | I->first->getIdentifierNamespace() != IDNS) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 708 | continue; |
| 709 | |
| 710 | // The newly-added result is hidden by an entry in the shadow map. |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 711 | if (CheckHiddenResult(R, CurContext, I->first)) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 712 | return; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 713 | |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | // Make sure that any given declaration only shows up in the result set once. |
| 719 | if (!AllDeclsFound.insert(CanonDecl)) |
| 720 | return; |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 721 | |
| 722 | // If this is an Objective-C method declaration whose selector matches our |
| 723 | // preferred selector, give it a priority boost. |
| 724 | if (!PreferredSelector.isNull()) |
| 725 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration)) |
| 726 | if (PreferredSelector == Method->getSelector()) |
| 727 | R.Priority += CCD_SelectorMatch; |
| 728 | |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 729 | // If the filter is for nested-name-specifiers, then this result starts a |
| 730 | // nested-name-specifier. |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 731 | if (AsNestedNameSpecifier) { |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 732 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 733 | R.Priority = CCP_NestedNameSpecifier; |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 734 | } else if (!PreferredType.isNull()) |
| 735 | AdjustResultPriorityForPreferredType(R); |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 736 | |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 737 | // If this result is supposed to have an informative qualifier, add one. |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 738 | if (R.QualifierIsInformative && !R.Qualifier && |
| 739 | !R.StartsNestedNameSpecifier) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 740 | DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 741 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 742 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace); |
| 743 | else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
| 744 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false, |
| 745 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
| 746 | else |
| 747 | R.QualifierIsInformative = false; |
| 748 | } |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 749 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 750 | // Insert this result into the set of results and into the current shadow |
| 751 | // map. |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 752 | SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 753 | Results.push_back(R); |
| 754 | } |
| 755 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 756 | void ResultBuilder::AddResult(Result R, DeclContext *CurContext, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 757 | NamedDecl *Hiding, bool InBaseClass = false) { |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 758 | if (R.Kind != Result::RK_Declaration) { |
| 759 | // For non-declaration results, just add the result. |
| 760 | Results.push_back(R); |
| 761 | return; |
| 762 | } |
| 763 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 764 | // Look through using declarations. |
| 765 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
| 766 | AddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext, Hiding); |
| 767 | return; |
| 768 | } |
| 769 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 770 | bool AsNestedNameSpecifier = false; |
| 771 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 772 | return; |
| 773 | |
| 774 | if (Hiding && CheckHiddenResult(R, CurContext, Hiding)) |
| 775 | return; |
| 776 | |
| 777 | // Make sure that any given declaration only shows up in the result set once. |
| 778 | if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl())) |
| 779 | return; |
| 780 | |
| 781 | // If the filter is for nested-name-specifiers, then this result starts a |
| 782 | // nested-name-specifier. |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 783 | if (AsNestedNameSpecifier) { |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 784 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 785 | R.Priority = CCP_NestedNameSpecifier; |
| 786 | } |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 787 | else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass && |
| 788 | isa<CXXRecordDecl>(R.Declaration->getDeclContext() |
| 789 | ->getLookupContext())) |
| 790 | R.QualifierIsInformative = true; |
| 791 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 792 | // If this result is supposed to have an informative qualifier, add one. |
| 793 | if (R.QualifierIsInformative && !R.Qualifier && |
| 794 | !R.StartsNestedNameSpecifier) { |
| 795 | DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 796 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 797 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace); |
| 798 | else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
| 799 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false, |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 800 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 801 | else |
| 802 | R.QualifierIsInformative = false; |
| 803 | } |
| 804 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 805 | // Adjust the priority if this result comes from a base class. |
| 806 | if (InBaseClass) |
| 807 | R.Priority += CCD_InBaseClass; |
| 808 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 809 | // If this is an Objective-C method declaration whose selector matches our |
| 810 | // preferred selector, give it a priority boost. |
| 811 | if (!PreferredSelector.isNull()) |
| 812 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration)) |
| 813 | if (PreferredSelector == Method->getSelector()) |
| 814 | R.Priority += CCD_SelectorMatch; |
| 815 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 816 | if (!PreferredType.isNull()) |
| 817 | AdjustResultPriorityForPreferredType(R); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 818 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 819 | if (HasObjectTypeQualifiers) |
| 820 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(R.Declaration)) |
| 821 | if (Method->isInstance()) { |
| 822 | Qualifiers MethodQuals |
| 823 | = Qualifiers::fromCVRMask(Method->getTypeQualifiers()); |
| 824 | if (ObjectTypeQualifiers == MethodQuals) |
| 825 | R.Priority += CCD_ObjectQualifierMatch; |
| 826 | else if (ObjectTypeQualifiers - MethodQuals) { |
| 827 | // The method cannot be invoked, because doing so would drop |
| 828 | // qualifiers. |
| 829 | return; |
| 830 | } |
| 831 | } |
| 832 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 833 | // Insert this result into the set of results. |
| 834 | Results.push_back(R); |
| 835 | } |
| 836 | |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 837 | void ResultBuilder::AddResult(Result R) { |
| 838 | assert(R.Kind != Result::RK_Declaration && |
| 839 | "Declaration results need more context"); |
| 840 | Results.push_back(R); |
| 841 | } |
| 842 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 843 | /// \brief Enter into a new scope. |
| 844 | void ResultBuilder::EnterNewScope() { |
| 845 | ShadowMaps.push_back(ShadowMap()); |
| 846 | } |
| 847 | |
| 848 | /// \brief Exit from the current scope. |
| 849 | void ResultBuilder::ExitScope() { |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 850 | for (ShadowMap::iterator E = ShadowMaps.back().begin(), |
| 851 | EEnd = ShadowMaps.back().end(); |
| 852 | E != EEnd; |
| 853 | ++E) |
| 854 | E->second.Destroy(); |
| 855 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 856 | ShadowMaps.pop_back(); |
| 857 | } |
| 858 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 859 | /// \brief Determines whether this given declaration will be found by |
| 860 | /// ordinary name lookup. |
| 861 | bool ResultBuilder::IsOrdinaryName(NamedDecl *ND) const { |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 862 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 863 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 864 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 865 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 9b30b26 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 866 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 867 | else if (SemaRef.getLangOptions().ObjC1 && isa<ObjCIvarDecl>(ND)) |
| 868 | return true; |
| 869 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 870 | return ND->getIdentifierNamespace() & IDNS; |
| 871 | } |
| 872 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 873 | /// \brief Determines whether this given declaration will be found by |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 874 | /// ordinary name lookup but is not a type name. |
| 875 | bool ResultBuilder::IsOrdinaryNonTypeName(NamedDecl *ND) const { |
| 876 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 877 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) |
| 878 | return false; |
| 879 | |
| 880 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 881 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 9b30b26 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 882 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 883 | else if (SemaRef.getLangOptions().ObjC1 && isa<ObjCIvarDecl>(ND)) |
| 884 | return true; |
| 885 | |
| 886 | return ND->getIdentifierNamespace() & IDNS; |
| 887 | } |
| 888 | |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 889 | bool ResultBuilder::IsIntegralConstantValue(NamedDecl *ND) const { |
| 890 | if (!IsOrdinaryNonTypeName(ND)) |
| 891 | return 0; |
| 892 | |
| 893 | if (ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl())) |
| 894 | if (VD->getType()->isIntegralOrEnumerationType()) |
| 895 | return true; |
| 896 | |
| 897 | return false; |
| 898 | } |
| 899 | |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 900 | /// \brief Determines whether this given declaration will be found by |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 901 | /// ordinary name lookup. |
| 902 | bool ResultBuilder::IsOrdinaryNonValueName(NamedDecl *ND) const { |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 903 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 904 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 905 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 906 | if (SemaRef.getLangOptions().CPlusPlus) |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 907 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 908 | |
| 909 | return (ND->getIdentifierNamespace() & IDNS) && |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 910 | !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) && |
| 911 | !isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 914 | /// \brief Determines whether the given declaration is suitable as the |
| 915 | /// start of a C++ nested-name-specifier, e.g., a class or namespace. |
| 916 | bool ResultBuilder::IsNestedNameSpecifier(NamedDecl *ND) const { |
| 917 | // Allow us to find class templates, too. |
| 918 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 919 | ND = ClassTemplate->getTemplatedDecl(); |
| 920 | |
| 921 | return SemaRef.isAcceptableNestedNameSpecifier(ND); |
| 922 | } |
| 923 | |
| 924 | /// \brief Determines whether the given declaration is an enumeration. |
| 925 | bool ResultBuilder::IsEnum(NamedDecl *ND) const { |
| 926 | return isa<EnumDecl>(ND); |
| 927 | } |
| 928 | |
| 929 | /// \brief Determines whether the given declaration is a class or struct. |
| 930 | bool ResultBuilder::IsClassOrStruct(NamedDecl *ND) const { |
| 931 | // Allow us to find class templates, too. |
| 932 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 933 | ND = ClassTemplate->getTemplatedDecl(); |
| 934 | |
| 935 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 936 | return RD->getTagKind() == TTK_Class || |
| 937 | RD->getTagKind() == TTK_Struct; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 938 | |
| 939 | return false; |
| 940 | } |
| 941 | |
| 942 | /// \brief Determines whether the given declaration is a union. |
| 943 | bool ResultBuilder::IsUnion(NamedDecl *ND) const { |
| 944 | // Allow us to find class templates, too. |
| 945 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 946 | ND = ClassTemplate->getTemplatedDecl(); |
| 947 | |
| 948 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 949 | return RD->getTagKind() == TTK_Union; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 950 | |
| 951 | return false; |
| 952 | } |
| 953 | |
| 954 | /// \brief Determines whether the given declaration is a namespace. |
| 955 | bool ResultBuilder::IsNamespace(NamedDecl *ND) const { |
| 956 | return isa<NamespaceDecl>(ND); |
| 957 | } |
| 958 | |
| 959 | /// \brief Determines whether the given declaration is a namespace or |
| 960 | /// namespace alias. |
| 961 | bool ResultBuilder::IsNamespaceOrAlias(NamedDecl *ND) const { |
| 962 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
| 963 | } |
| 964 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 965 | /// \brief Determines whether the given declaration is a type. |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 966 | bool ResultBuilder::IsType(NamedDecl *ND) const { |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 967 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND)) |
| 968 | ND = Using->getTargetDecl(); |
| 969 | |
| 970 | return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 973 | /// \brief Determines which members of a class should be visible via |
| 974 | /// "." or "->". Only value declarations, nested name specifiers, and |
| 975 | /// using declarations thereof should show up. |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 976 | bool ResultBuilder::IsMember(NamedDecl *ND) const { |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 977 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND)) |
| 978 | ND = Using->getTargetDecl(); |
| 979 | |
Douglas Gregor | ce82196 | 2009-12-11 18:14:22 +0000 | [diff] [blame] | 980 | return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) || |
| 981 | isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 984 | static bool isObjCReceiverType(ASTContext &C, QualType T) { |
| 985 | T = C.getCanonicalType(T); |
| 986 | switch (T->getTypeClass()) { |
| 987 | case Type::ObjCObject: |
| 988 | case Type::ObjCInterface: |
| 989 | case Type::ObjCObjectPointer: |
| 990 | return true; |
| 991 | |
| 992 | case Type::Builtin: |
| 993 | switch (cast<BuiltinType>(T)->getKind()) { |
| 994 | case BuiltinType::ObjCId: |
| 995 | case BuiltinType::ObjCClass: |
| 996 | case BuiltinType::ObjCSel: |
| 997 | return true; |
| 998 | |
| 999 | default: |
| 1000 | break; |
| 1001 | } |
| 1002 | return false; |
| 1003 | |
| 1004 | default: |
| 1005 | break; |
| 1006 | } |
| 1007 | |
| 1008 | if (!C.getLangOptions().CPlusPlus) |
| 1009 | return false; |
| 1010 | |
| 1011 | // FIXME: We could perform more analysis here to determine whether a |
| 1012 | // particular class type has any conversions to Objective-C types. For now, |
| 1013 | // just accept all class types. |
| 1014 | return T->isDependentType() || T->isRecordType(); |
| 1015 | } |
| 1016 | |
| 1017 | bool ResultBuilder::IsObjCMessageReceiver(NamedDecl *ND) const { |
| 1018 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1019 | if (T.isNull()) |
| 1020 | return false; |
| 1021 | |
| 1022 | T = SemaRef.Context.getBaseElementType(T); |
| 1023 | return isObjCReceiverType(SemaRef.Context, T); |
| 1024 | } |
| 1025 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1026 | bool ResultBuilder::IsObjCCollection(NamedDecl *ND) const { |
| 1027 | if ((SemaRef.getLangOptions().CPlusPlus && !IsOrdinaryName(ND)) || |
| 1028 | (!SemaRef.getLangOptions().CPlusPlus && !IsOrdinaryNonTypeName(ND))) |
| 1029 | return false; |
| 1030 | |
| 1031 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1032 | if (T.isNull()) |
| 1033 | return false; |
| 1034 | |
| 1035 | T = SemaRef.Context.getBaseElementType(T); |
| 1036 | return T->isObjCObjectType() || T->isObjCObjectPointerType() || |
| 1037 | T->isObjCIdType() || |
| 1038 | (SemaRef.getLangOptions().CPlusPlus && T->isRecordType()); |
| 1039 | } |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1040 | |
Douglas Gregor | 80f4f4c | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1041 | /// \rief Determines whether the given declaration is an Objective-C |
| 1042 | /// instance variable. |
| 1043 | bool ResultBuilder::IsObjCIvar(NamedDecl *ND) const { |
| 1044 | return isa<ObjCIvarDecl>(ND); |
| 1045 | } |
| 1046 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1047 | namespace { |
| 1048 | /// \brief Visible declaration consumer that adds a code-completion result |
| 1049 | /// for each visible declaration. |
| 1050 | class CodeCompletionDeclConsumer : public VisibleDeclConsumer { |
| 1051 | ResultBuilder &Results; |
| 1052 | DeclContext *CurContext; |
| 1053 | |
| 1054 | public: |
| 1055 | CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext) |
| 1056 | : Results(Results), CurContext(CurContext) { } |
| 1057 | |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 1058 | virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, bool InBaseClass) { |
| 1059 | Results.AddResult(ND, CurContext, Hiding, InBaseClass); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1060 | } |
| 1061 | }; |
| 1062 | } |
| 1063 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1064 | /// \brief Add type specifiers for the current language as keyword results. |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1065 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1066 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1067 | typedef CodeCompletionResult Result; |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1068 | Results.AddResult(Result("short", CCP_Type)); |
| 1069 | Results.AddResult(Result("long", CCP_Type)); |
| 1070 | Results.AddResult(Result("signed", CCP_Type)); |
| 1071 | Results.AddResult(Result("unsigned", CCP_Type)); |
| 1072 | Results.AddResult(Result("void", CCP_Type)); |
| 1073 | Results.AddResult(Result("char", CCP_Type)); |
| 1074 | Results.AddResult(Result("int", CCP_Type)); |
| 1075 | Results.AddResult(Result("float", CCP_Type)); |
| 1076 | Results.AddResult(Result("double", CCP_Type)); |
| 1077 | Results.AddResult(Result("enum", CCP_Type)); |
| 1078 | Results.AddResult(Result("struct", CCP_Type)); |
| 1079 | Results.AddResult(Result("union", CCP_Type)); |
| 1080 | Results.AddResult(Result("const", CCP_Type)); |
| 1081 | Results.AddResult(Result("volatile", CCP_Type)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1082 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1083 | if (LangOpts.C99) { |
| 1084 | // C99-specific |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1085 | Results.AddResult(Result("_Complex", CCP_Type)); |
| 1086 | Results.AddResult(Result("_Imaginary", CCP_Type)); |
| 1087 | Results.AddResult(Result("_Bool", CCP_Type)); |
| 1088 | Results.AddResult(Result("restrict", CCP_Type)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | if (LangOpts.CPlusPlus) { |
| 1092 | // C++-specific |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1093 | Results.AddResult(Result("bool", CCP_Type)); |
| 1094 | Results.AddResult(Result("class", CCP_Type)); |
| 1095 | Results.AddResult(Result("wchar_t", CCP_Type)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1096 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1097 | // typename qualified-id |
| 1098 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 1099 | Pattern->AddTypedTextChunk("typename"); |
| 1100 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1101 | Pattern->AddPlaceholderChunk("qualifier"); |
| 1102 | Pattern->AddTextChunk("::"); |
| 1103 | Pattern->AddPlaceholderChunk("name"); |
| 1104 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1105 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1106 | if (LangOpts.CPlusPlus0x) { |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1107 | Results.AddResult(Result("auto", CCP_Type)); |
| 1108 | Results.AddResult(Result("char16_t", CCP_Type)); |
| 1109 | Results.AddResult(Result("char32_t", CCP_Type)); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1110 | |
| 1111 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 1112 | Pattern->AddTypedTextChunk("decltype"); |
| 1113 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1114 | Pattern->AddPlaceholderChunk("expression"); |
| 1115 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1116 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | // GNU extensions |
| 1121 | if (LangOpts.GNUMode) { |
| 1122 | // FIXME: Enable when we actually support decimal floating point. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1123 | // Results.AddResult(Result("_Decimal32")); |
| 1124 | // Results.AddResult(Result("_Decimal64")); |
| 1125 | // Results.AddResult(Result("_Decimal128")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1126 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1127 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 1128 | Pattern->AddTypedTextChunk("typeof"); |
| 1129 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1130 | Pattern->AddPlaceholderChunk("expression"); |
| 1131 | Results.AddResult(Result(Pattern)); |
| 1132 | |
| 1133 | Pattern = new CodeCompletionString; |
| 1134 | Pattern->AddTypedTextChunk("typeof"); |
| 1135 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1136 | Pattern->AddPlaceholderChunk("type"); |
| 1137 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1138 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1139 | } |
| 1140 | } |
| 1141 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1142 | static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1143 | const LangOptions &LangOpts, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1144 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1145 | typedef CodeCompletionResult Result; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1146 | // Note: we don't suggest either "auto" or "register", because both |
| 1147 | // are pointless as storage specifiers. Elsewhere, we suggest "auto" |
| 1148 | // in C++0x as a type specifier. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1149 | Results.AddResult(Result("extern")); |
| 1150 | Results.AddResult(Result("static")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1153 | static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1154 | const LangOptions &LangOpts, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1155 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1156 | typedef CodeCompletionResult Result; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1157 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1158 | case Sema::PCC_Class: |
| 1159 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1160 | if (LangOpts.CPlusPlus) { |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1161 | Results.AddResult(Result("explicit")); |
| 1162 | Results.AddResult(Result("friend")); |
| 1163 | Results.AddResult(Result("mutable")); |
| 1164 | Results.AddResult(Result("virtual")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1165 | } |
| 1166 | // Fall through |
| 1167 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1168 | case Sema::PCC_ObjCInterface: |
| 1169 | case Sema::PCC_ObjCImplementation: |
| 1170 | case Sema::PCC_Namespace: |
| 1171 | case Sema::PCC_Template: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1172 | if (LangOpts.CPlusPlus || LangOpts.C99) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1173 | Results.AddResult(Result("inline")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1174 | break; |
| 1175 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1176 | case Sema::PCC_ObjCInstanceVariableList: |
| 1177 | case Sema::PCC_Expression: |
| 1178 | case Sema::PCC_Statement: |
| 1179 | case Sema::PCC_ForInit: |
| 1180 | case Sema::PCC_Condition: |
| 1181 | case Sema::PCC_RecoveryInFunction: |
| 1182 | case Sema::PCC_Type: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1183 | break; |
| 1184 | } |
| 1185 | } |
| 1186 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1187 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); |
| 1188 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); |
| 1189 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1190 | ResultBuilder &Results, |
| 1191 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1192 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1193 | ResultBuilder &Results, |
| 1194 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1195 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1196 | ResultBuilder &Results, |
| 1197 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1198 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1199 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1200 | static void AddTypedefResult(ResultBuilder &Results) { |
| 1201 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 1202 | Pattern->AddTypedTextChunk("typedef"); |
| 1203 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1204 | Pattern->AddPlaceholderChunk("type"); |
| 1205 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1206 | Pattern->AddPlaceholderChunk("name"); |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1207 | Results.AddResult(CodeCompletionResult(Pattern)); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1210 | static bool WantTypesInContext(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1211 | const LangOptions &LangOpts) { |
| 1212 | if (LangOpts.CPlusPlus) |
| 1213 | return true; |
| 1214 | |
| 1215 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1216 | case Sema::PCC_Namespace: |
| 1217 | case Sema::PCC_Class: |
| 1218 | case Sema::PCC_ObjCInstanceVariableList: |
| 1219 | case Sema::PCC_Template: |
| 1220 | case Sema::PCC_MemberTemplate: |
| 1221 | case Sema::PCC_Statement: |
| 1222 | case Sema::PCC_RecoveryInFunction: |
| 1223 | case Sema::PCC_Type: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1224 | return true; |
| 1225 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1226 | case Sema::PCC_ObjCInterface: |
| 1227 | case Sema::PCC_ObjCImplementation: |
| 1228 | case Sema::PCC_Expression: |
| 1229 | case Sema::PCC_Condition: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1230 | return false; |
| 1231 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1232 | case Sema::PCC_ForInit: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1233 | return LangOpts.ObjC1 || LangOpts.C99; |
| 1234 | } |
| 1235 | |
| 1236 | return false; |
| 1237 | } |
| 1238 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1239 | /// \brief Add language constructs that show up for "ordinary" names. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1240 | static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1241 | Scope *S, |
| 1242 | Sema &SemaRef, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1243 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1244 | typedef CodeCompletionResult Result; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1245 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1246 | case Sema::PCC_Namespace: |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1247 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 1248 | CodeCompletionString *Pattern = 0; |
| 1249 | |
| 1250 | if (Results.includeCodePatterns()) { |
| 1251 | // namespace <identifier> { declarations } |
| 1252 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 1253 | Pattern->AddTypedTextChunk("namespace"); |
| 1254 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1255 | Pattern->AddPlaceholderChunk("identifier"); |
| 1256 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1257 | Pattern->AddPlaceholderChunk("declarations"); |
| 1258 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1259 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 1260 | Results.AddResult(Result(Pattern)); |
| 1261 | } |
| 1262 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1263 | // namespace identifier = identifier ; |
| 1264 | Pattern = new CodeCompletionString; |
| 1265 | Pattern->AddTypedTextChunk("namespace"); |
| 1266 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1267 | Pattern->AddPlaceholderChunk("name"); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1268 | Pattern->AddChunk(CodeCompletionString::CK_Equal); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1269 | Pattern->AddPlaceholderChunk("namespace"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1270 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1271 | |
| 1272 | // Using directives |
| 1273 | Pattern = new CodeCompletionString; |
| 1274 | Pattern->AddTypedTextChunk("using"); |
| 1275 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1276 | Pattern->AddTextChunk("namespace"); |
| 1277 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1278 | Pattern->AddPlaceholderChunk("identifier"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1279 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1280 | |
| 1281 | // asm(string-literal) |
| 1282 | Pattern = new CodeCompletionString; |
| 1283 | Pattern->AddTypedTextChunk("asm"); |
| 1284 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1285 | Pattern->AddPlaceholderChunk("string-literal"); |
| 1286 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1287 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1288 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1289 | if (Results.includeCodePatterns()) { |
| 1290 | // Explicit template instantiation |
| 1291 | Pattern = new CodeCompletionString; |
| 1292 | Pattern->AddTypedTextChunk("template"); |
| 1293 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1294 | Pattern->AddPlaceholderChunk("declaration"); |
| 1295 | Results.AddResult(Result(Pattern)); |
| 1296 | } |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1297 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1298 | |
| 1299 | if (SemaRef.getLangOptions().ObjC1) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1300 | AddObjCTopLevelResults(Results, true); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1301 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1302 | AddTypedefResult(Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1303 | // Fall through |
| 1304 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1305 | case Sema::PCC_Class: |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1306 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1307 | // Using declaration |
| 1308 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 1309 | Pattern->AddTypedTextChunk("using"); |
| 1310 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1311 | Pattern->AddPlaceholderChunk("qualifier"); |
| 1312 | Pattern->AddTextChunk("::"); |
| 1313 | Pattern->AddPlaceholderChunk("name"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1314 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1315 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1316 | // using typename qualifier::name (only in a dependent context) |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1317 | if (SemaRef.CurContext->isDependentContext()) { |
| 1318 | Pattern = new CodeCompletionString; |
| 1319 | Pattern->AddTypedTextChunk("using"); |
| 1320 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1321 | Pattern->AddTextChunk("typename"); |
| 1322 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1323 | Pattern->AddPlaceholderChunk("qualifier"); |
| 1324 | Pattern->AddTextChunk("::"); |
| 1325 | Pattern->AddPlaceholderChunk("name"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1326 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1329 | if (CCC == Sema::PCC_Class) { |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1330 | AddTypedefResult(Results); |
| 1331 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1332 | // public: |
| 1333 | Pattern = new CodeCompletionString; |
| 1334 | Pattern->AddTypedTextChunk("public"); |
| 1335 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1336 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1337 | |
| 1338 | // protected: |
| 1339 | Pattern = new CodeCompletionString; |
| 1340 | Pattern->AddTypedTextChunk("protected"); |
| 1341 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1342 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1343 | |
| 1344 | // private: |
| 1345 | Pattern = new CodeCompletionString; |
| 1346 | Pattern->AddTypedTextChunk("private"); |
| 1347 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1348 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | // Fall through |
| 1352 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1353 | case Sema::PCC_Template: |
| 1354 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1355 | if (SemaRef.getLangOptions().CPlusPlus && Results.includeCodePatterns()) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1356 | // template < parameters > |
| 1357 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 1358 | Pattern->AddTypedTextChunk("template"); |
| 1359 | Pattern->AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1360 | Pattern->AddPlaceholderChunk("parameters"); |
| 1361 | Pattern->AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1362 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1365 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 1366 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1367 | break; |
| 1368 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1369 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1370 | AddObjCInterfaceResults(SemaRef.getLangOptions(), Results, true); |
| 1371 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 1372 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1373 | break; |
| 1374 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1375 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1376 | AddObjCImplementationResults(SemaRef.getLangOptions(), Results, true); |
| 1377 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 1378 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1379 | break; |
| 1380 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1381 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1382 | AddObjCVisibilityResults(SemaRef.getLangOptions(), Results, true); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1383 | break; |
| 1384 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1385 | case Sema::PCC_RecoveryInFunction: |
| 1386 | case Sema::PCC_Statement: { |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1387 | AddTypedefResult(Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1388 | |
| 1389 | CodeCompletionString *Pattern = 0; |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1390 | if (SemaRef.getLangOptions().CPlusPlus && Results.includeCodePatterns()) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1391 | Pattern = new CodeCompletionString; |
| 1392 | Pattern->AddTypedTextChunk("try"); |
| 1393 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1394 | Pattern->AddPlaceholderChunk("statements"); |
| 1395 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1396 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 1397 | Pattern->AddTextChunk("catch"); |
| 1398 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1399 | Pattern->AddPlaceholderChunk("declaration"); |
| 1400 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1401 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1402 | Pattern->AddPlaceholderChunk("statements"); |
| 1403 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1404 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1405 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1406 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1407 | if (SemaRef.getLangOptions().ObjC1) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1408 | AddObjCStatementResults(Results, true); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1409 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1410 | if (Results.includeCodePatterns()) { |
| 1411 | // if (condition) { statements } |
| 1412 | Pattern = new CodeCompletionString; |
| 1413 | Pattern->AddTypedTextChunk("if"); |
| 1414 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1415 | if (SemaRef.getLangOptions().CPlusPlus) |
| 1416 | Pattern->AddPlaceholderChunk("condition"); |
| 1417 | else |
| 1418 | Pattern->AddPlaceholderChunk("expression"); |
| 1419 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1420 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1421 | Pattern->AddPlaceholderChunk("statements"); |
| 1422 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1423 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 1424 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1425 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1426 | // switch (condition) { } |
| 1427 | Pattern = new CodeCompletionString; |
| 1428 | Pattern->AddTypedTextChunk("switch"); |
| 1429 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1430 | if (SemaRef.getLangOptions().CPlusPlus) |
| 1431 | Pattern->AddPlaceholderChunk("condition"); |
| 1432 | else |
| 1433 | Pattern->AddPlaceholderChunk("expression"); |
| 1434 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1435 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1436 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1437 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 1438 | Results.AddResult(Result(Pattern)); |
| 1439 | } |
| 1440 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1441 | // Switch-specific statements. |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 1442 | if (!SemaRef.getCurFunction()->SwitchStack.empty()) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1443 | // case expression: |
| 1444 | Pattern = new CodeCompletionString; |
| 1445 | Pattern->AddTypedTextChunk("case"); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1446 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1447 | Pattern->AddPlaceholderChunk("expression"); |
| 1448 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1449 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1450 | |
| 1451 | // default: |
| 1452 | Pattern = new CodeCompletionString; |
| 1453 | Pattern->AddTypedTextChunk("default"); |
| 1454 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1455 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1458 | if (Results.includeCodePatterns()) { |
| 1459 | /// while (condition) { statements } |
| 1460 | Pattern = new CodeCompletionString; |
| 1461 | Pattern->AddTypedTextChunk("while"); |
| 1462 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1463 | if (SemaRef.getLangOptions().CPlusPlus) |
| 1464 | Pattern->AddPlaceholderChunk("condition"); |
| 1465 | else |
| 1466 | Pattern->AddPlaceholderChunk("expression"); |
| 1467 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1468 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1469 | Pattern->AddPlaceholderChunk("statements"); |
| 1470 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1471 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 1472 | Results.AddResult(Result(Pattern)); |
| 1473 | |
| 1474 | // do { statements } while ( expression ); |
| 1475 | Pattern = new CodeCompletionString; |
| 1476 | Pattern->AddTypedTextChunk("do"); |
| 1477 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1478 | Pattern->AddPlaceholderChunk("statements"); |
| 1479 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1480 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 1481 | Pattern->AddTextChunk("while"); |
| 1482 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1483 | Pattern->AddPlaceholderChunk("expression"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1484 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1485 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1486 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1487 | // for ( for-init-statement ; condition ; expression ) { statements } |
| 1488 | Pattern = new CodeCompletionString; |
| 1489 | Pattern->AddTypedTextChunk("for"); |
| 1490 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1491 | if (SemaRef.getLangOptions().CPlusPlus || SemaRef.getLangOptions().C99) |
| 1492 | Pattern->AddPlaceholderChunk("init-statement"); |
| 1493 | else |
| 1494 | Pattern->AddPlaceholderChunk("init-expression"); |
| 1495 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
| 1496 | Pattern->AddPlaceholderChunk("condition"); |
| 1497 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
| 1498 | Pattern->AddPlaceholderChunk("inc-expression"); |
| 1499 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1500 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1501 | Pattern->AddPlaceholderChunk("statements"); |
| 1502 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1503 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 1504 | Results.AddResult(Result(Pattern)); |
| 1505 | } |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1506 | |
| 1507 | if (S->getContinueParent()) { |
| 1508 | // continue ; |
| 1509 | Pattern = new CodeCompletionString; |
| 1510 | Pattern->AddTypedTextChunk("continue"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1511 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | if (S->getBreakParent()) { |
| 1515 | // break ; |
| 1516 | Pattern = new CodeCompletionString; |
| 1517 | Pattern->AddTypedTextChunk("break"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1518 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | // "return expression ;" or "return ;", depending on whether we |
| 1522 | // know the function is void or not. |
| 1523 | bool isVoid = false; |
| 1524 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) |
| 1525 | isVoid = Function->getResultType()->isVoidType(); |
| 1526 | else if (ObjCMethodDecl *Method |
| 1527 | = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) |
| 1528 | isVoid = Method->getResultType()->isVoidType(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1529 | else if (SemaRef.getCurBlock() && |
| 1530 | !SemaRef.getCurBlock()->ReturnType.isNull()) |
| 1531 | isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType(); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1532 | Pattern = new CodeCompletionString; |
| 1533 | Pattern->AddTypedTextChunk("return"); |
Douglas Gregor | 9329800 | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1534 | if (!isVoid) { |
| 1535 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1536 | Pattern->AddPlaceholderChunk("expression"); |
Douglas Gregor | 9329800 | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1537 | } |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1538 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1539 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1540 | // goto identifier ; |
| 1541 | Pattern = new CodeCompletionString; |
| 1542 | Pattern->AddTypedTextChunk("goto"); |
| 1543 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1544 | Pattern->AddPlaceholderChunk("label"); |
| 1545 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1546 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1547 | // Using directives |
| 1548 | Pattern = new CodeCompletionString; |
| 1549 | Pattern->AddTypedTextChunk("using"); |
| 1550 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1551 | Pattern->AddTextChunk("namespace"); |
| 1552 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1553 | Pattern->AddPlaceholderChunk("identifier"); |
| 1554 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | // Fall through (for statement expressions). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1558 | case Sema::PCC_ForInit: |
| 1559 | case Sema::PCC_Condition: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1560 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1561 | // Fall through: conditions and statements can have expressions. |
| 1562 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1563 | case Sema::PCC_Expression: { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1564 | CodeCompletionString *Pattern = 0; |
| 1565 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 1566 | // 'this', if we're in a non-static member function. |
| 1567 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) |
| 1568 | if (!Method->isStatic()) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1569 | Results.AddResult(Result("this")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1570 | |
| 1571 | // true, false |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1572 | Results.AddResult(Result("true")); |
| 1573 | Results.AddResult(Result("false")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1574 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1575 | // dynamic_cast < type-id > ( expression ) |
| 1576 | Pattern = new CodeCompletionString; |
| 1577 | Pattern->AddTypedTextChunk("dynamic_cast"); |
| 1578 | Pattern->AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1579 | Pattern->AddPlaceholderChunk("type"); |
| 1580 | Pattern->AddChunk(CodeCompletionString::CK_RightAngle); |
| 1581 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1582 | Pattern->AddPlaceholderChunk("expression"); |
| 1583 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1584 | Results.AddResult(Result(Pattern)); |
| 1585 | |
| 1586 | // static_cast < type-id > ( expression ) |
| 1587 | Pattern = new CodeCompletionString; |
| 1588 | Pattern->AddTypedTextChunk("static_cast"); |
| 1589 | Pattern->AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1590 | Pattern->AddPlaceholderChunk("type"); |
| 1591 | Pattern->AddChunk(CodeCompletionString::CK_RightAngle); |
| 1592 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1593 | Pattern->AddPlaceholderChunk("expression"); |
| 1594 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1595 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1596 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1597 | // reinterpret_cast < type-id > ( expression ) |
| 1598 | Pattern = new CodeCompletionString; |
| 1599 | Pattern->AddTypedTextChunk("reinterpret_cast"); |
| 1600 | Pattern->AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1601 | Pattern->AddPlaceholderChunk("type"); |
| 1602 | Pattern->AddChunk(CodeCompletionString::CK_RightAngle); |
| 1603 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1604 | Pattern->AddPlaceholderChunk("expression"); |
| 1605 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1606 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1607 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1608 | // const_cast < type-id > ( expression ) |
| 1609 | Pattern = new CodeCompletionString; |
| 1610 | Pattern->AddTypedTextChunk("const_cast"); |
| 1611 | Pattern->AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1612 | Pattern->AddPlaceholderChunk("type"); |
| 1613 | Pattern->AddChunk(CodeCompletionString::CK_RightAngle); |
| 1614 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1615 | Pattern->AddPlaceholderChunk("expression"); |
| 1616 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1617 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1618 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1619 | // typeid ( expression-or-type ) |
| 1620 | Pattern = new CodeCompletionString; |
| 1621 | Pattern->AddTypedTextChunk("typeid"); |
| 1622 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1623 | Pattern->AddPlaceholderChunk("expression-or-type"); |
| 1624 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1625 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1626 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1627 | // new T ( ... ) |
| 1628 | Pattern = new CodeCompletionString; |
| 1629 | Pattern->AddTypedTextChunk("new"); |
| 1630 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1631 | Pattern->AddPlaceholderChunk("type"); |
| 1632 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1633 | Pattern->AddPlaceholderChunk("expressions"); |
| 1634 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1635 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1636 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1637 | // new T [ ] ( ... ) |
| 1638 | Pattern = new CodeCompletionString; |
| 1639 | Pattern->AddTypedTextChunk("new"); |
| 1640 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1641 | Pattern->AddPlaceholderChunk("type"); |
| 1642 | Pattern->AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1643 | Pattern->AddPlaceholderChunk("size"); |
| 1644 | Pattern->AddChunk(CodeCompletionString::CK_RightBracket); |
| 1645 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1646 | Pattern->AddPlaceholderChunk("expressions"); |
| 1647 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1648 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1649 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1650 | // delete expression |
| 1651 | Pattern = new CodeCompletionString; |
| 1652 | Pattern->AddTypedTextChunk("delete"); |
| 1653 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1654 | Pattern->AddPlaceholderChunk("expression"); |
| 1655 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1656 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1657 | // delete [] expression |
| 1658 | Pattern = new CodeCompletionString; |
| 1659 | Pattern->AddTypedTextChunk("delete"); |
| 1660 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1661 | Pattern->AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1662 | Pattern->AddChunk(CodeCompletionString::CK_RightBracket); |
| 1663 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1664 | Pattern->AddPlaceholderChunk("expression"); |
| 1665 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1666 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1667 | // throw expression |
| 1668 | Pattern = new CodeCompletionString; |
| 1669 | Pattern->AddTypedTextChunk("throw"); |
| 1670 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1671 | Pattern->AddPlaceholderChunk("expression"); |
| 1672 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1673 | |
| 1674 | // FIXME: Rethrow? |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | if (SemaRef.getLangOptions().ObjC1) { |
| 1678 | // Add "super", if we're in an Objective-C class with a superclass. |
Ted Kremenek | 681e256 | 2010-05-31 21:43:10 +0000 | [diff] [blame] | 1679 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
| 1680 | // The interface can be NULL. |
| 1681 | if (ObjCInterfaceDecl *ID = Method->getClassInterface()) |
| 1682 | if (ID->getSuperClass()) |
| 1683 | Results.AddResult(Result("super")); |
| 1684 | } |
| 1685 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1686 | AddObjCExpressionResults(Results, true); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1689 | // sizeof expression |
| 1690 | Pattern = new CodeCompletionString; |
| 1691 | Pattern->AddTypedTextChunk("sizeof"); |
| 1692 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1693 | Pattern->AddPlaceholderChunk("expression-or-type"); |
| 1694 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1695 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1696 | break; |
| 1697 | } |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1698 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1699 | case Sema::PCC_Type: |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1700 | break; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1703 | if (WantTypesInContext(CCC, SemaRef.getLangOptions())) |
| 1704 | AddTypeSpecifierResults(SemaRef.getLangOptions(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1705 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1706 | if (SemaRef.getLangOptions().CPlusPlus && CCC != Sema::PCC_Type) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1707 | Results.AddResult(Result("operator")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1710 | /// \brief If the given declaration has an associated type, add it as a result |
| 1711 | /// type chunk. |
| 1712 | static void AddResultTypeChunk(ASTContext &Context, |
| 1713 | NamedDecl *ND, |
| 1714 | CodeCompletionString *Result) { |
| 1715 | if (!ND) |
| 1716 | return; |
| 1717 | |
| 1718 | // Determine the type of the declaration (if it has a type). |
| 1719 | QualType T; |
| 1720 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) |
| 1721 | T = Function->getResultType(); |
| 1722 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
| 1723 | T = Method->getResultType(); |
| 1724 | else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) |
| 1725 | T = FunTmpl->getTemplatedDecl()->getResultType(); |
| 1726 | else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
| 1727 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); |
| 1728 | else if (isa<UnresolvedUsingValueDecl>(ND)) { |
| 1729 | /* Do nothing: ignore unresolved using declarations*/ |
| 1730 | } else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND)) |
| 1731 | T = Value->getType(); |
| 1732 | else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
| 1733 | T = Property->getType(); |
| 1734 | |
| 1735 | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) |
| 1736 | return; |
| 1737 | |
Douglas Gregor | 84139d6 | 2010-04-05 21:25:31 +0000 | [diff] [blame] | 1738 | PrintingPolicy Policy(Context.PrintingPolicy); |
| 1739 | Policy.AnonymousTagLocations = false; |
| 1740 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1741 | std::string TypeStr; |
Douglas Gregor | 84139d6 | 2010-04-05 21:25:31 +0000 | [diff] [blame] | 1742 | T.getAsStringInternal(TypeStr, Policy); |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1743 | Result->AddResultTypeChunk(TypeStr); |
| 1744 | } |
| 1745 | |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1746 | static void MaybeAddSentinel(ASTContext &Context, NamedDecl *FunctionOrMethod, |
| 1747 | CodeCompletionString *Result) { |
| 1748 | if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) |
| 1749 | if (Sentinel->getSentinel() == 0) { |
| 1750 | if (Context.getLangOptions().ObjC1 && |
| 1751 | Context.Idents.get("nil").hasMacroDefinition()) |
| 1752 | Result->AddTextChunk(", nil"); |
| 1753 | else if (Context.Idents.get("NULL").hasMacroDefinition()) |
| 1754 | Result->AddTextChunk(", NULL"); |
| 1755 | else |
| 1756 | Result->AddTextChunk(", (void*)0"); |
| 1757 | } |
| 1758 | } |
| 1759 | |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1760 | static std::string FormatFunctionParameter(ASTContext &Context, |
| 1761 | ParmVarDecl *Param) { |
| 1762 | bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext()); |
| 1763 | if (Param->getType()->isDependentType() || |
| 1764 | !Param->getType()->isBlockPointerType()) { |
| 1765 | // The argument for a dependent or non-block parameter is a placeholder |
| 1766 | // containing that parameter's type. |
| 1767 | std::string Result; |
| 1768 | |
| 1769 | if (Param->getIdentifier() && !ObjCMethodParam) |
| 1770 | Result = Param->getIdentifier()->getName(); |
| 1771 | |
| 1772 | Param->getType().getAsStringInternal(Result, |
| 1773 | Context.PrintingPolicy); |
| 1774 | |
| 1775 | if (ObjCMethodParam) { |
| 1776 | Result = "(" + Result; |
| 1777 | Result += ")"; |
| 1778 | if (Param->getIdentifier()) |
| 1779 | Result += Param->getIdentifier()->getName(); |
| 1780 | } |
| 1781 | return Result; |
| 1782 | } |
| 1783 | |
| 1784 | // The argument for a block pointer parameter is a block literal with |
| 1785 | // the appropriate type. |
| 1786 | FunctionProtoTypeLoc *Block = 0; |
| 1787 | TypeLoc TL; |
| 1788 | if (TypeSourceInfo *TSInfo = Param->getTypeSourceInfo()) { |
| 1789 | TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 1790 | while (true) { |
| 1791 | // Look through typedefs. |
| 1792 | if (TypedefTypeLoc *TypedefTL = dyn_cast<TypedefTypeLoc>(&TL)) { |
| 1793 | if (TypeSourceInfo *InnerTSInfo |
| 1794 | = TypedefTL->getTypedefDecl()->getTypeSourceInfo()) { |
| 1795 | TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 1796 | continue; |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | // Look through qualified types |
| 1801 | if (QualifiedTypeLoc *QualifiedTL = dyn_cast<QualifiedTypeLoc>(&TL)) { |
| 1802 | TL = QualifiedTL->getUnqualifiedLoc(); |
| 1803 | continue; |
| 1804 | } |
| 1805 | |
| 1806 | // Try to get the function prototype behind the block pointer type, |
| 1807 | // then we're done. |
| 1808 | if (BlockPointerTypeLoc *BlockPtr |
| 1809 | = dyn_cast<BlockPointerTypeLoc>(&TL)) { |
| 1810 | TL = BlockPtr->getPointeeLoc(); |
| 1811 | Block = dyn_cast<FunctionProtoTypeLoc>(&TL); |
| 1812 | } |
| 1813 | break; |
| 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | if (!Block) { |
| 1818 | // We were unable to find a FunctionProtoTypeLoc with parameter names |
| 1819 | // for the block; just use the parameter type as a placeholder. |
| 1820 | std::string Result; |
| 1821 | Param->getType().getUnqualifiedType(). |
| 1822 | getAsStringInternal(Result, Context.PrintingPolicy); |
| 1823 | |
| 1824 | if (ObjCMethodParam) { |
| 1825 | Result = "(" + Result; |
| 1826 | Result += ")"; |
| 1827 | if (Param->getIdentifier()) |
| 1828 | Result += Param->getIdentifier()->getName(); |
| 1829 | } |
| 1830 | |
| 1831 | return Result; |
| 1832 | } |
| 1833 | |
| 1834 | // We have the function prototype behind the block pointer type, as it was |
| 1835 | // written in the source. |
| 1836 | std::string Result = "(^)("; |
| 1837 | for (unsigned I = 0, N = Block->getNumArgs(); I != N; ++I) { |
| 1838 | if (I) |
| 1839 | Result += ", "; |
| 1840 | Result += FormatFunctionParameter(Context, Block->getArg(I)); |
| 1841 | } |
| 1842 | if (Block->getTypePtr()->isVariadic()) { |
| 1843 | if (Block->getNumArgs() > 0) |
| 1844 | Result += ", ..."; |
| 1845 | else |
| 1846 | Result += "..."; |
| 1847 | } else if (Block->getNumArgs() == 0 && !Context.getLangOptions().CPlusPlus) |
| 1848 | Result += "void"; |
| 1849 | |
| 1850 | Result += ")"; |
| 1851 | Block->getTypePtr()->getResultType().getAsStringInternal(Result, |
| 1852 | Context.PrintingPolicy); |
| 1853 | return Result; |
| 1854 | } |
| 1855 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1856 | /// \brief Add function parameter chunks to the given code completion string. |
| 1857 | static void AddFunctionParameterChunks(ASTContext &Context, |
| 1858 | FunctionDecl *Function, |
| 1859 | CodeCompletionString *Result) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1860 | typedef CodeCompletionString::Chunk Chunk; |
| 1861 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1862 | CodeCompletionString *CCStr = Result; |
| 1863 | |
| 1864 | for (unsigned P = 0, N = Function->getNumParams(); P != N; ++P) { |
| 1865 | ParmVarDecl *Param = Function->getParamDecl(P); |
| 1866 | |
| 1867 | if (Param->hasDefaultArg()) { |
| 1868 | // When we see an optional default argument, put that argument and |
| 1869 | // the remaining default arguments into a new, optional string. |
| 1870 | CodeCompletionString *Opt = new CodeCompletionString; |
| 1871 | CCStr->AddOptionalChunk(std::auto_ptr<CodeCompletionString>(Opt)); |
| 1872 | CCStr = Opt; |
| 1873 | } |
| 1874 | |
| 1875 | if (P != 0) |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1876 | CCStr->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1877 | |
| 1878 | // Format the placeholder string. |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1879 | std::string PlaceholderStr = FormatFunctionParameter(Context, Param); |
| 1880 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1881 | // Add the placeholder string. |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1882 | CCStr->AddPlaceholderChunk(PlaceholderStr); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1883 | } |
Douglas Gregor | b3d4525 | 2009-09-22 21:42:17 +0000 | [diff] [blame] | 1884 | |
| 1885 | if (const FunctionProtoType *Proto |
| 1886 | = Function->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1887 | if (Proto->isVariadic()) { |
Douglas Gregor | b3d4525 | 2009-09-22 21:42:17 +0000 | [diff] [blame] | 1888 | CCStr->AddPlaceholderChunk(", ..."); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1889 | |
| 1890 | MaybeAddSentinel(Context, Function, CCStr); |
| 1891 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | /// \brief Add template parameter chunks to the given code completion string. |
| 1895 | static void AddTemplateParameterChunks(ASTContext &Context, |
| 1896 | TemplateDecl *Template, |
| 1897 | CodeCompletionString *Result, |
| 1898 | unsigned MaxParameters = 0) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1899 | typedef CodeCompletionString::Chunk Chunk; |
| 1900 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1901 | CodeCompletionString *CCStr = Result; |
| 1902 | bool FirstParameter = true; |
| 1903 | |
| 1904 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 1905 | TemplateParameterList::iterator PEnd = Params->end(); |
| 1906 | if (MaxParameters) |
| 1907 | PEnd = Params->begin() + MaxParameters; |
| 1908 | for (TemplateParameterList::iterator P = Params->begin(); P != PEnd; ++P) { |
| 1909 | bool HasDefaultArg = false; |
| 1910 | std::string PlaceholderStr; |
| 1911 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 1912 | if (TTP->wasDeclaredWithTypename()) |
| 1913 | PlaceholderStr = "typename"; |
| 1914 | else |
| 1915 | PlaceholderStr = "class"; |
| 1916 | |
| 1917 | if (TTP->getIdentifier()) { |
| 1918 | PlaceholderStr += ' '; |
| 1919 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 1920 | } |
| 1921 | |
| 1922 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 1923 | } else if (NonTypeTemplateParmDecl *NTTP |
| 1924 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 1925 | if (NTTP->getIdentifier()) |
| 1926 | PlaceholderStr = NTTP->getIdentifier()->getName(); |
| 1927 | NTTP->getType().getAsStringInternal(PlaceholderStr, |
| 1928 | Context.PrintingPolicy); |
| 1929 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 1930 | } else { |
| 1931 | assert(isa<TemplateTemplateParmDecl>(*P)); |
| 1932 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 1933 | |
| 1934 | // Since putting the template argument list into the placeholder would |
| 1935 | // be very, very long, we just use an abbreviation. |
| 1936 | PlaceholderStr = "template<...> class"; |
| 1937 | if (TTP->getIdentifier()) { |
| 1938 | PlaceholderStr += ' '; |
| 1939 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 1940 | } |
| 1941 | |
| 1942 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 1943 | } |
| 1944 | |
| 1945 | if (HasDefaultArg) { |
| 1946 | // When we see an optional default argument, put that argument and |
| 1947 | // the remaining default arguments into a new, optional string. |
| 1948 | CodeCompletionString *Opt = new CodeCompletionString; |
| 1949 | CCStr->AddOptionalChunk(std::auto_ptr<CodeCompletionString>(Opt)); |
| 1950 | CCStr = Opt; |
| 1951 | } |
| 1952 | |
| 1953 | if (FirstParameter) |
| 1954 | FirstParameter = false; |
| 1955 | else |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1956 | CCStr->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1957 | |
| 1958 | // Add the placeholder string. |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1959 | CCStr->AddPlaceholderChunk(PlaceholderStr); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1960 | } |
| 1961 | } |
| 1962 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1963 | /// \brief Add a qualifier to the given code-completion string, if the |
| 1964 | /// provided nested-name-specifier is non-NULL. |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 1965 | static void |
| 1966 | AddQualifierToCompletionString(CodeCompletionString *Result, |
| 1967 | NestedNameSpecifier *Qualifier, |
| 1968 | bool QualifierIsInformative, |
| 1969 | ASTContext &Context) { |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1970 | if (!Qualifier) |
| 1971 | return; |
| 1972 | |
| 1973 | std::string PrintedNNS; |
| 1974 | { |
| 1975 | llvm::raw_string_ostream OS(PrintedNNS); |
| 1976 | Qualifier->print(OS, Context.PrintingPolicy); |
| 1977 | } |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 1978 | if (QualifierIsInformative) |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1979 | Result->AddInformativeChunk(PrintedNNS); |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 1980 | else |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1981 | Result->AddTextChunk(PrintedNNS); |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 1984 | static void AddFunctionTypeQualsToCompletionString(CodeCompletionString *Result, |
| 1985 | FunctionDecl *Function) { |
| 1986 | const FunctionProtoType *Proto |
| 1987 | = Function->getType()->getAs<FunctionProtoType>(); |
| 1988 | if (!Proto || !Proto->getTypeQuals()) |
| 1989 | return; |
| 1990 | |
| 1991 | std::string QualsStr; |
| 1992 | if (Proto->getTypeQuals() & Qualifiers::Const) |
| 1993 | QualsStr += " const"; |
| 1994 | if (Proto->getTypeQuals() & Qualifiers::Volatile) |
| 1995 | QualsStr += " volatile"; |
| 1996 | if (Proto->getTypeQuals() & Qualifiers::Restrict) |
| 1997 | QualsStr += " restrict"; |
| 1998 | Result->AddInformativeChunk(QualsStr); |
| 1999 | } |
| 2000 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2001 | /// \brief If possible, create a new code completion string for the given |
| 2002 | /// result. |
| 2003 | /// |
| 2004 | /// \returns Either a new, heap-allocated code completion string describing |
| 2005 | /// how to use this result, or NULL to indicate that the string or name of the |
| 2006 | /// result is all that is needed. |
| 2007 | CodeCompletionString * |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2008 | CodeCompletionResult::CreateCodeCompletionString(Sema &S, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2009 | CodeCompletionString *Result) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2010 | typedef CodeCompletionString::Chunk Chunk; |
| 2011 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2012 | if (Kind == RK_Pattern) |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2013 | return Pattern->Clone(Result); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2014 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2015 | if (!Result) |
| 2016 | Result = new CodeCompletionString; |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2017 | |
| 2018 | if (Kind == RK_Keyword) { |
| 2019 | Result->AddTypedTextChunk(Keyword); |
| 2020 | return Result; |
| 2021 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2022 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2023 | if (Kind == RK_Macro) { |
| 2024 | MacroInfo *MI = S.PP.getMacroInfo(Macro); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2025 | assert(MI && "Not a macro?"); |
| 2026 | |
| 2027 | Result->AddTypedTextChunk(Macro->getName()); |
| 2028 | |
| 2029 | if (!MI->isFunctionLike()) |
| 2030 | return Result; |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2031 | |
| 2032 | // Format a function-like macro with placeholders for the arguments. |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2033 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2034 | for (MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end(); |
| 2035 | A != AEnd; ++A) { |
| 2036 | if (A != MI->arg_begin()) |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2037 | Result->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2038 | |
| 2039 | if (!MI->isVariadic() || A != AEnd - 1) { |
| 2040 | // Non-variadic argument. |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2041 | Result->AddPlaceholderChunk((*A)->getName()); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2042 | continue; |
| 2043 | } |
| 2044 | |
| 2045 | // Variadic argument; cope with the different between GNU and C99 |
| 2046 | // variadic macros, providing a single placeholder for the rest of the |
| 2047 | // arguments. |
| 2048 | if ((*A)->isStr("__VA_ARGS__")) |
| 2049 | Result->AddPlaceholderChunk("..."); |
| 2050 | else { |
| 2051 | std::string Arg = (*A)->getName(); |
| 2052 | Arg += "..."; |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2053 | Result->AddPlaceholderChunk(Arg); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2054 | } |
| 2055 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2056 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2057 | return Result; |
| 2058 | } |
| 2059 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2060 | assert(Kind == RK_Declaration && "Missed a result kind?"); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2061 | NamedDecl *ND = Declaration; |
| 2062 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2063 | if (StartsNestedNameSpecifier) { |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2064 | Result->AddTypedTextChunk(ND->getNameAsString()); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2065 | Result->AddTextChunk("::"); |
| 2066 | return Result; |
| 2067 | } |
| 2068 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2069 | AddResultTypeChunk(S.Context, ND, Result); |
| 2070 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2071 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2072 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2073 | S.Context); |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2074 | Result->AddTypedTextChunk(Function->getNameAsString()); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2075 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2076 | AddFunctionParameterChunks(S.Context, Function, Result); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2077 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2078 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2079 | return Result; |
| 2080 | } |
| 2081 | |
| 2082 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2083 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2084 | S.Context); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2085 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2086 | Result->AddTypedTextChunk(Function->getNameAsString()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2087 | |
| 2088 | // Figure out which template parameters are deduced (or have default |
| 2089 | // arguments). |
| 2090 | llvm::SmallVector<bool, 16> Deduced; |
| 2091 | S.MarkDeducedTemplateParameters(FunTmpl, Deduced); |
| 2092 | unsigned LastDeducibleArgument; |
| 2093 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
| 2094 | --LastDeducibleArgument) { |
| 2095 | if (!Deduced[LastDeducibleArgument - 1]) { |
| 2096 | // C++0x: Figure out if the template argument has a default. If so, |
| 2097 | // the user doesn't need to type this argument. |
| 2098 | // FIXME: We need to abstract template parameters better! |
| 2099 | bool HasDefaultArg = false; |
| 2100 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
| 2101 | LastDeducibleArgument - 1); |
| 2102 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 2103 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2104 | else if (NonTypeTemplateParmDecl *NTTP |
| 2105 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 2106 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2107 | else { |
| 2108 | assert(isa<TemplateTemplateParmDecl>(Param)); |
| 2109 | HasDefaultArg |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2110 | = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | if (!HasDefaultArg) |
| 2114 | break; |
| 2115 | } |
| 2116 | } |
| 2117 | |
| 2118 | if (LastDeducibleArgument) { |
| 2119 | // Some of the function template arguments cannot be deduced from a |
| 2120 | // function call, so we introduce an explicit template argument list |
| 2121 | // containing all of the arguments up to the first deducible argument. |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2122 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2123 | AddTemplateParameterChunks(S.Context, FunTmpl, Result, |
| 2124 | LastDeducibleArgument); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2125 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | // Add the function parameters |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2129 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2130 | AddFunctionParameterChunks(S.Context, Function, Result); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2131 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2132 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2133 | return Result; |
| 2134 | } |
| 2135 | |
| 2136 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2137 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2138 | S.Context); |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2139 | Result->AddTypedTextChunk(Template->getNameAsString()); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2140 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2141 | AddTemplateParameterChunks(S.Context, Template, Result); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2142 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2143 | return Result; |
| 2144 | } |
| 2145 | |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2146 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2147 | Selector Sel = Method->getSelector(); |
| 2148 | if (Sel.isUnarySelector()) { |
| 2149 | Result->AddTypedTextChunk(Sel.getIdentifierInfoForSlot(0)->getName()); |
| 2150 | return Result; |
| 2151 | } |
| 2152 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2153 | std::string SelName = Sel.getIdentifierInfoForSlot(0)->getName().str(); |
| 2154 | SelName += ':'; |
| 2155 | if (StartParameter == 0) |
| 2156 | Result->AddTypedTextChunk(SelName); |
| 2157 | else { |
| 2158 | Result->AddInformativeChunk(SelName); |
| 2159 | |
| 2160 | // If there is only one parameter, and we're past it, add an empty |
| 2161 | // typed-text chunk since there is nothing to type. |
| 2162 | if (Method->param_size() == 1) |
| 2163 | Result->AddTypedTextChunk(""); |
| 2164 | } |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2165 | unsigned Idx = 0; |
| 2166 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 2167 | PEnd = Method->param_end(); |
| 2168 | P != PEnd; (void)++P, ++Idx) { |
| 2169 | if (Idx > 0) { |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2170 | std::string Keyword; |
| 2171 | if (Idx > StartParameter) |
Douglas Gregor | 834389b | 2010-01-12 06:38:28 +0000 | [diff] [blame] | 2172 | Result->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2173 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) |
| 2174 | Keyword += II->getName().str(); |
| 2175 | Keyword += ":"; |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2176 | if (Idx < StartParameter || AllParametersAreInformative) |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2177 | Result->AddInformativeChunk(Keyword); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2178 | else if (Idx == StartParameter) |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2179 | Result->AddTypedTextChunk(Keyword); |
| 2180 | else |
| 2181 | Result->AddTextChunk(Keyword); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2182 | } |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2183 | |
| 2184 | // If we're before the starting parameter, skip the placeholder. |
| 2185 | if (Idx < StartParameter) |
| 2186 | continue; |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2187 | |
| 2188 | std::string Arg; |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2189 | |
| 2190 | if ((*P)->getType()->isBlockPointerType() && !DeclaringEntity) |
| 2191 | Arg = FormatFunctionParameter(S.Context, *P); |
| 2192 | else { |
| 2193 | (*P)->getType().getAsStringInternal(Arg, S.Context.PrintingPolicy); |
| 2194 | Arg = "(" + Arg + ")"; |
| 2195 | if (IdentifierInfo *II = (*P)->getIdentifier()) |
| 2196 | Arg += II->getName().str(); |
| 2197 | } |
| 2198 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2199 | if (DeclaringEntity) |
| 2200 | Result->AddTextChunk(Arg); |
| 2201 | else if (AllParametersAreInformative) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2202 | Result->AddInformativeChunk(Arg); |
| 2203 | else |
| 2204 | Result->AddPlaceholderChunk(Arg); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2205 | } |
| 2206 | |
Douglas Gregor | 2a17af0 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2207 | if (Method->isVariadic()) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2208 | if (DeclaringEntity) |
| 2209 | Result->AddTextChunk(", ..."); |
| 2210 | else if (AllParametersAreInformative) |
Douglas Gregor | 2a17af0 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2211 | Result->AddInformativeChunk(", ..."); |
| 2212 | else |
| 2213 | Result->AddPlaceholderChunk(", ..."); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2214 | |
| 2215 | MaybeAddSentinel(S.Context, Method, Result); |
Douglas Gregor | 2a17af0 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2218 | return Result; |
| 2219 | } |
| 2220 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2221 | if (Qualifier) |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2222 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2223 | S.Context); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2224 | |
| 2225 | Result->AddTypedTextChunk(ND->getNameAsString()); |
| 2226 | return Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2229 | CodeCompletionString * |
| 2230 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( |
| 2231 | unsigned CurrentArg, |
| 2232 | Sema &S) const { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2233 | typedef CodeCompletionString::Chunk Chunk; |
| 2234 | |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2235 | CodeCompletionString *Result = new CodeCompletionString; |
| 2236 | FunctionDecl *FDecl = getFunction(); |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2237 | AddResultTypeChunk(S.Context, FDecl, Result); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2238 | const FunctionProtoType *Proto |
| 2239 | = dyn_cast<FunctionProtoType>(getFunctionType()); |
| 2240 | if (!FDecl && !Proto) { |
| 2241 | // Function without a prototype. Just give the return type and a |
| 2242 | // highlighted ellipsis. |
| 2243 | const FunctionType *FT = getFunctionType(); |
| 2244 | Result->AddTextChunk( |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2245 | FT->getResultType().getAsString(S.Context.PrintingPolicy)); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2246 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
| 2247 | Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "...")); |
| 2248 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2249 | return Result; |
| 2250 | } |
| 2251 | |
| 2252 | if (FDecl) |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2253 | Result->AddTextChunk(FDecl->getNameAsString()); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2254 | else |
| 2255 | Result->AddTextChunk( |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2256 | Proto->getResultType().getAsString(S.Context.PrintingPolicy)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2257 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2258 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2259 | unsigned NumParams = FDecl? FDecl->getNumParams() : Proto->getNumArgs(); |
| 2260 | for (unsigned I = 0; I != NumParams; ++I) { |
| 2261 | if (I) |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2262 | Result->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2263 | |
| 2264 | std::string ArgString; |
| 2265 | QualType ArgType; |
| 2266 | |
| 2267 | if (FDecl) { |
| 2268 | ArgString = FDecl->getParamDecl(I)->getNameAsString(); |
| 2269 | ArgType = FDecl->getParamDecl(I)->getOriginalType(); |
| 2270 | } else { |
| 2271 | ArgType = Proto->getArgType(I); |
| 2272 | } |
| 2273 | |
| 2274 | ArgType.getAsStringInternal(ArgString, S.Context.PrintingPolicy); |
| 2275 | |
| 2276 | if (I == CurrentArg) |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2277 | Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2278 | ArgString)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2279 | else |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2280 | Result->AddTextChunk(ArgString); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | if (Proto && Proto->isVariadic()) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2284 | Result->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2285 | if (CurrentArg < NumParams) |
| 2286 | Result->AddTextChunk("..."); |
| 2287 | else |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2288 | Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "...")); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2289 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2290 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2291 | |
| 2292 | return Result; |
| 2293 | } |
| 2294 | |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2295 | unsigned clang::getMacroUsagePriority(llvm::StringRef MacroName, |
| 2296 | bool PreferredTypeIsPointer) { |
| 2297 | unsigned Priority = CCP_Macro; |
| 2298 | |
| 2299 | // Treat the "nil" and "NULL" macros as null pointer constants. |
| 2300 | if (MacroName.equals("nil") || MacroName.equals("NULL")) { |
| 2301 | Priority = CCP_Constant; |
| 2302 | if (PreferredTypeIsPointer) |
| 2303 | Priority = Priority / CCF_SimilarTypeMatch; |
| 2304 | } |
| 2305 | |
| 2306 | return Priority; |
| 2307 | } |
| 2308 | |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2309 | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, |
| 2310 | bool TargetTypeIsPointer = false) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2311 | typedef CodeCompletionResult Result; |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2312 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2313 | Results.EnterNewScope(); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2314 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 2315 | MEnd = PP.macro_end(); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2316 | M != MEnd; ++M) { |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2317 | Results.AddResult(Result(M->first, |
| 2318 | getMacroUsagePriority(M->first->getName(), |
| 2319 | TargetTypeIsPointer))); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2320 | } |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2321 | Results.ExitScope(); |
| 2322 | } |
| 2323 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2324 | static void AddPrettyFunctionResults(const LangOptions &LangOpts, |
| 2325 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2326 | typedef CodeCompletionResult Result; |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2327 | |
| 2328 | Results.EnterNewScope(); |
| 2329 | Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant)); |
| 2330 | Results.AddResult(Result("__FUNCTION__", CCP_Constant)); |
| 2331 | if (LangOpts.C99 || LangOpts.CPlusPlus0x) |
| 2332 | Results.AddResult(Result("__func__", CCP_Constant)); |
| 2333 | Results.ExitScope(); |
| 2334 | } |
| 2335 | |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2336 | static void HandleCodeCompleteResults(Sema *S, |
| 2337 | CodeCompleteConsumer *CodeCompleter, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2338 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2339 | CodeCompletionResult *Results, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2340 | unsigned NumResults) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2341 | if (CodeCompleter) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2342 | CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 2343 | |
| 2344 | for (unsigned I = 0; I != NumResults; ++I) |
| 2345 | Results[I].Destroy(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2348 | static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S, |
| 2349 | Sema::ParserCompletionContext PCC) { |
| 2350 | switch (PCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2351 | case Sema::PCC_Namespace: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2352 | return CodeCompletionContext::CCC_TopLevel; |
| 2353 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2354 | case Sema::PCC_Class: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2355 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 2356 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2357 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2358 | return CodeCompletionContext::CCC_ObjCInterface; |
| 2359 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2360 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2361 | return CodeCompletionContext::CCC_ObjCImplementation; |
| 2362 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2363 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2364 | return CodeCompletionContext::CCC_ObjCIvarList; |
| 2365 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2366 | case Sema::PCC_Template: |
| 2367 | case Sema::PCC_MemberTemplate: |
| 2368 | case Sema::PCC_RecoveryInFunction: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2369 | return CodeCompletionContext::CCC_Other; |
| 2370 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2371 | case Sema::PCC_Expression: |
| 2372 | case Sema::PCC_ForInit: |
| 2373 | case Sema::PCC_Condition: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2374 | return CodeCompletionContext::CCC_Expression; |
| 2375 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2376 | case Sema::PCC_Statement: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2377 | return CodeCompletionContext::CCC_Statement; |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2378 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2379 | case Sema::PCC_Type: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2380 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | return CodeCompletionContext::CCC_Other; |
| 2384 | } |
| 2385 | |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2386 | /// \brief If we're in a C++ virtual member function, add completion results |
| 2387 | /// that invoke the functions we override, since it's common to invoke the |
| 2388 | /// overridden function as well as adding new functionality. |
| 2389 | /// |
| 2390 | /// \param S The semantic analysis object for which we are generating results. |
| 2391 | /// |
| 2392 | /// \param InContext This context in which the nested-name-specifier preceding |
| 2393 | /// the code-completion point |
| 2394 | static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, |
| 2395 | ResultBuilder &Results) { |
| 2396 | // Look through blocks. |
| 2397 | DeclContext *CurContext = S.CurContext; |
| 2398 | while (isa<BlockDecl>(CurContext)) |
| 2399 | CurContext = CurContext->getParent(); |
| 2400 | |
| 2401 | |
| 2402 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext); |
| 2403 | if (!Method || !Method->isVirtual()) |
| 2404 | return; |
| 2405 | |
| 2406 | // We need to have names for all of the parameters, if we're going to |
| 2407 | // generate a forwarding call. |
| 2408 | for (CXXMethodDecl::param_iterator P = Method->param_begin(), |
| 2409 | PEnd = Method->param_end(); |
| 2410 | P != PEnd; |
| 2411 | ++P) { |
| 2412 | if (!(*P)->getDeclName()) |
| 2413 | return; |
| 2414 | } |
| 2415 | |
| 2416 | for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(), |
| 2417 | MEnd = Method->end_overridden_methods(); |
| 2418 | M != MEnd; ++M) { |
| 2419 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 2420 | CXXMethodDecl *Overridden = const_cast<CXXMethodDecl *>(*M); |
| 2421 | if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl()) |
| 2422 | continue; |
| 2423 | |
| 2424 | // If we need a nested-name-specifier, add one now. |
| 2425 | if (!InContext) { |
| 2426 | NestedNameSpecifier *NNS |
| 2427 | = getRequiredQualification(S.Context, CurContext, |
| 2428 | Overridden->getDeclContext()); |
| 2429 | if (NNS) { |
| 2430 | std::string Str; |
| 2431 | llvm::raw_string_ostream OS(Str); |
| 2432 | NNS->print(OS, S.Context.PrintingPolicy); |
| 2433 | Pattern->AddTextChunk(OS.str()); |
| 2434 | } |
| 2435 | } else if (!InContext->Equals(Overridden->getDeclContext())) |
| 2436 | continue; |
| 2437 | |
| 2438 | Pattern->AddTypedTextChunk(Overridden->getNameAsString()); |
| 2439 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 2440 | bool FirstParam = true; |
| 2441 | for (CXXMethodDecl::param_iterator P = Method->param_begin(), |
| 2442 | PEnd = Method->param_end(); |
| 2443 | P != PEnd; ++P) { |
| 2444 | if (FirstParam) |
| 2445 | FirstParam = false; |
| 2446 | else |
| 2447 | Pattern->AddChunk(CodeCompletionString::CK_Comma); |
| 2448 | |
| 2449 | Pattern->AddPlaceholderChunk((*P)->getIdentifier()->getName()); |
| 2450 | } |
| 2451 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 2452 | Results.AddResult(CodeCompletionResult(Pattern, |
| 2453 | CCP_SuperCompletion, |
| 2454 | CXCursor_CXXMethod)); |
| 2455 | Results.Ignore(Overridden); |
| 2456 | } |
| 2457 | } |
| 2458 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2459 | void Sema::CodeCompleteOrdinaryName(Scope *S, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2460 | ParserCompletionContext CompletionContext) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2461 | typedef CodeCompletionResult Result; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2462 | ResultBuilder Results(*this); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2463 | Results.EnterNewScope(); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2464 | |
| 2465 | // Determine how to filter results, e.g., so that the names of |
| 2466 | // values (functions, enumerators, function templates, etc.) are |
| 2467 | // only allowed where we can have an expression. |
| 2468 | switch (CompletionContext) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2469 | case PCC_Namespace: |
| 2470 | case PCC_Class: |
| 2471 | case PCC_ObjCInterface: |
| 2472 | case PCC_ObjCImplementation: |
| 2473 | case PCC_ObjCInstanceVariableList: |
| 2474 | case PCC_Template: |
| 2475 | case PCC_MemberTemplate: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2476 | case PCC_Type: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2477 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 2478 | break; |
| 2479 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2480 | case PCC_Statement: |
Douglas Gregor | eb0d014 | 2010-08-24 23:58:17 +0000 | [diff] [blame] | 2481 | // For statements that are expressions, we prefer to call 'void' functions |
| 2482 | // rather than functions that return a result, since then the result would |
| 2483 | // be ignored. |
| 2484 | Results.setPreferredType(Context.VoidTy); |
| 2485 | // Fall through |
| 2486 | |
| 2487 | case PCC_Expression: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2488 | case PCC_ForInit: |
| 2489 | case PCC_Condition: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 2490 | if (WantTypesInContext(CompletionContext, getLangOptions())) |
| 2491 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 2492 | else |
| 2493 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2494 | |
| 2495 | if (getLangOptions().CPlusPlus) |
| 2496 | MaybeAddOverrideCalls(*this, /*InContext=*/0, Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2497 | break; |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 2498 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2499 | case PCC_RecoveryInFunction: |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 2500 | // Unfiltered |
| 2501 | break; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 2504 | // If we are in a C++ non-static member function, check the qualifiers on |
| 2505 | // the member function to filter/prioritize the results list. |
| 2506 | if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext)) |
| 2507 | if (CurMethod->isInstance()) |
| 2508 | Results.setObjectTypeQualifiers( |
| 2509 | Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers())); |
| 2510 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 2511 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2512 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 2513 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 2a7925c | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 2514 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2515 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); |
Douglas Gregor | 2a7925c | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 2516 | Results.ExitScope(); |
| 2517 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2518 | switch (CompletionContext) { |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2519 | case PCC_Expression: |
| 2520 | case PCC_Statement: |
| 2521 | case PCC_RecoveryInFunction: |
| 2522 | if (S->getFnParent()) |
| 2523 | AddPrettyFunctionResults(PP.getLangOptions(), Results); |
| 2524 | break; |
| 2525 | |
| 2526 | case PCC_Namespace: |
| 2527 | case PCC_Class: |
| 2528 | case PCC_ObjCInterface: |
| 2529 | case PCC_ObjCImplementation: |
| 2530 | case PCC_ObjCInstanceVariableList: |
| 2531 | case PCC_Template: |
| 2532 | case PCC_MemberTemplate: |
| 2533 | case PCC_ForInit: |
| 2534 | case PCC_Condition: |
| 2535 | case PCC_Type: |
| 2536 | break; |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2539 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2540 | AddMacroResults(PP, Results); |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2541 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2542 | HandleCodeCompleteResults(this, CodeCompleter, |
| 2543 | mapCodeCompletionContext(*this, CompletionContext), |
| 2544 | Results.data(),Results.size()); |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2547 | void Sema::CodeCompleteDeclarator(Scope *S, |
| 2548 | bool AllowNonIdentifiers, |
| 2549 | bool AllowNestedNameSpecifiers) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2550 | typedef CodeCompletionResult Result; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2551 | ResultBuilder Results(*this); |
| 2552 | Results.EnterNewScope(); |
| 2553 | |
| 2554 | // Type qualifiers can come after names. |
| 2555 | Results.AddResult(Result("const")); |
| 2556 | Results.AddResult(Result("volatile")); |
| 2557 | if (getLangOptions().C99) |
| 2558 | Results.AddResult(Result("restrict")); |
| 2559 | |
| 2560 | if (getLangOptions().CPlusPlus) { |
| 2561 | if (AllowNonIdentifiers) { |
| 2562 | Results.AddResult(Result("operator")); |
| 2563 | } |
| 2564 | |
| 2565 | // Add nested-name-specifiers. |
| 2566 | if (AllowNestedNameSpecifiers) { |
| 2567 | Results.allowNestedNameSpecifiers(); |
| 2568 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 2569 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
| 2570 | CodeCompleter->includeGlobals()); |
| 2571 | } |
| 2572 | } |
| 2573 | Results.ExitScope(); |
| 2574 | |
Douglas Gregor | 4497dd4 | 2010-08-24 04:59:56 +0000 | [diff] [blame] | 2575 | // Note that we intentionally suppress macro results here, since we do not |
| 2576 | // encourage using macros to produce the names of entities. |
| 2577 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2578 | HandleCodeCompleteResults(this, CodeCompleter, |
| 2579 | AllowNestedNameSpecifiers |
| 2580 | ? CodeCompletionContext::CCC_PotentiallyQualifiedName |
| 2581 | : CodeCompletionContext::CCC_Name, |
| 2582 | Results.data(), Results.size()); |
| 2583 | } |
| 2584 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2585 | struct Sema::CodeCompleteExpressionData { |
| 2586 | CodeCompleteExpressionData(QualType PreferredType = QualType()) |
| 2587 | : PreferredType(PreferredType), IntegralConstantExpression(false), |
| 2588 | ObjCCollection(false) { } |
| 2589 | |
| 2590 | QualType PreferredType; |
| 2591 | bool IntegralConstantExpression; |
| 2592 | bool ObjCCollection; |
| 2593 | llvm::SmallVector<Decl *, 4> IgnoreDecls; |
| 2594 | }; |
| 2595 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2596 | /// \brief Perform code-completion in an expression context when we know what |
| 2597 | /// type we're looking for. |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 2598 | /// |
| 2599 | /// \param IntegralConstantExpression Only permit integral constant |
| 2600 | /// expressions. |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2601 | void Sema::CodeCompleteExpression(Scope *S, |
| 2602 | const CodeCompleteExpressionData &Data) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2603 | typedef CodeCompletionResult Result; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2604 | ResultBuilder Results(*this); |
| 2605 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2606 | if (Data.ObjCCollection) |
| 2607 | Results.setFilter(&ResultBuilder::IsObjCCollection); |
| 2608 | else if (Data.IntegralConstantExpression) |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 2609 | Results.setFilter(&ResultBuilder::IsIntegralConstantValue); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2610 | else if (WantTypesInContext(PCC_Expression, getLangOptions())) |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2611 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 2612 | else |
| 2613 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2614 | |
| 2615 | if (!Data.PreferredType.isNull()) |
| 2616 | Results.setPreferredType(Data.PreferredType.getNonReferenceType()); |
| 2617 | |
| 2618 | // Ignore any declarations that we were told that we don't care about. |
| 2619 | for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I) |
| 2620 | Results.Ignore(Data.IgnoreDecls[I]); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2621 | |
| 2622 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2623 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 2624 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2625 | |
| 2626 | Results.EnterNewScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2627 | AddOrdinaryNameResults(PCC_Expression, S, *this, Results); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2628 | Results.ExitScope(); |
| 2629 | |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2630 | bool PreferredTypeIsPointer = false; |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2631 | if (!Data.PreferredType.isNull()) |
| 2632 | PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType() |
| 2633 | || Data.PreferredType->isMemberPointerType() |
| 2634 | || Data.PreferredType->isBlockPointerType(); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2635 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2636 | if (S->getFnParent() && |
| 2637 | !Data.ObjCCollection && |
| 2638 | !Data.IntegralConstantExpression) |
| 2639 | AddPrettyFunctionResults(PP.getLangOptions(), Results); |
| 2640 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2641 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2642 | AddMacroResults(PP, Results, PreferredTypeIsPointer); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2643 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2644 | CodeCompletionContext(CodeCompletionContext::CCC_Expression, |
| 2645 | Data.PreferredType), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2646 | Results.data(),Results.size()); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2650 | static void AddObjCProperties(ObjCContainerDecl *Container, |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2651 | bool AllowCategories, |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2652 | DeclContext *CurContext, |
| 2653 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2654 | typedef CodeCompletionResult Result; |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2655 | |
| 2656 | // Add properties in this container. |
| 2657 | for (ObjCContainerDecl::prop_iterator P = Container->prop_begin(), |
| 2658 | PEnd = Container->prop_end(); |
| 2659 | P != PEnd; |
| 2660 | ++P) |
| 2661 | Results.MaybeAddResult(Result(*P, 0), CurContext); |
| 2662 | |
| 2663 | // Add properties in referenced protocols. |
| 2664 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 2665 | for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(), |
| 2666 | PEnd = Protocol->protocol_end(); |
| 2667 | P != PEnd; ++P) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2668 | AddObjCProperties(*P, AllowCategories, CurContext, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2669 | } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){ |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2670 | if (AllowCategories) { |
| 2671 | // Look through categories. |
| 2672 | for (ObjCCategoryDecl *Category = IFace->getCategoryList(); |
| 2673 | Category; Category = Category->getNextClassCategory()) |
| 2674 | AddObjCProperties(Category, AllowCategories, CurContext, Results); |
| 2675 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2676 | |
| 2677 | // Look through protocols. |
| 2678 | for (ObjCInterfaceDecl::protocol_iterator I = IFace->protocol_begin(), |
| 2679 | E = IFace->protocol_end(); |
| 2680 | I != E; ++I) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2681 | AddObjCProperties(*I, AllowCategories, CurContext, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2682 | |
| 2683 | // Look in the superclass. |
| 2684 | if (IFace->getSuperClass()) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2685 | AddObjCProperties(IFace->getSuperClass(), AllowCategories, CurContext, |
| 2686 | Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2687 | } else if (const ObjCCategoryDecl *Category |
| 2688 | = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 2689 | // Look through protocols. |
| 2690 | for (ObjCInterfaceDecl::protocol_iterator P = Category->protocol_begin(), |
| 2691 | PEnd = Category->protocol_end(); |
| 2692 | P != PEnd; ++P) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2693 | AddObjCProperties(*P, AllowCategories, CurContext, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2694 | } |
| 2695 | } |
| 2696 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 2697 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, ExprTy *BaseE, |
| 2698 | SourceLocation OpLoc, |
| 2699 | bool IsArrow) { |
| 2700 | if (!BaseE || !CodeCompleter) |
| 2701 | return; |
| 2702 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2703 | typedef CodeCompletionResult Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2704 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 2705 | Expr *Base = static_cast<Expr *>(BaseE); |
| 2706 | QualType BaseType = Base->getType(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2707 | |
| 2708 | if (IsArrow) { |
| 2709 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 2710 | BaseType = Ptr->getPointeeType(); |
| 2711 | else if (BaseType->isObjCObjectPointerType()) |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 2712 | /*Do nothing*/ ; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2713 | else |
| 2714 | return; |
| 2715 | } |
| 2716 | |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 2717 | ResultBuilder Results(*this, &ResultBuilder::IsMember); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2718 | Results.EnterNewScope(); |
| 2719 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 2720 | // Indicate that we are performing a member access, and the cv-qualifiers |
| 2721 | // for the base object type. |
| 2722 | Results.setObjectTypeQualifiers(BaseType.getQualifiers()); |
| 2723 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2724 | // Access to a C/C++ class, struct, or union. |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 2725 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2726 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2727 | LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer, |
| 2728 | CodeCompleter->includeGlobals()); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2729 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2730 | if (getLangOptions().CPlusPlus) { |
| 2731 | if (!Results.empty()) { |
| 2732 | // The "template" keyword can follow "->" or "." in the grammar. |
| 2733 | // However, we only want to suggest the template keyword if something |
| 2734 | // is dependent. |
| 2735 | bool IsDependent = BaseType->isDependentType(); |
| 2736 | if (!IsDependent) { |
| 2737 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) |
| 2738 | if (DeclContext *Ctx = (DeclContext *)DepScope->getEntity()) { |
| 2739 | IsDependent = Ctx->isDependentContext(); |
| 2740 | break; |
| 2741 | } |
| 2742 | } |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2743 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2744 | if (IsDependent) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2745 | Results.AddResult(Result("template")); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2746 | } |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2747 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2748 | } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) { |
| 2749 | // Objective-C property reference. |
| 2750 | |
| 2751 | // Add property results based on our interface. |
| 2752 | const ObjCObjectPointerType *ObjCPtr |
| 2753 | = BaseType->getAsObjCInterfacePointerType(); |
| 2754 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!"); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2755 | AddObjCProperties(ObjCPtr->getInterfaceDecl(), true, CurContext, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2756 | |
| 2757 | // Add properties from the protocols in a qualified interface. |
| 2758 | for (ObjCObjectPointerType::qual_iterator I = ObjCPtr->qual_begin(), |
| 2759 | E = ObjCPtr->qual_end(); |
| 2760 | I != E; ++I) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2761 | AddObjCProperties(*I, true, CurContext, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2762 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2763 | (!IsArrow && BaseType->isObjCObjectType())) { |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2764 | // Objective-C instance variable access. |
| 2765 | ObjCInterfaceDecl *Class = 0; |
| 2766 | if (const ObjCObjectPointerType *ObjCPtr |
| 2767 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 2768 | Class = ObjCPtr->getInterfaceDecl(); |
| 2769 | else |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2770 | Class = BaseType->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2771 | |
| 2772 | // Add all ivars from this class and its superclasses. |
Douglas Gregor | 80f4f4c | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 2773 | if (Class) { |
| 2774 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 2775 | Results.setFilter(&ResultBuilder::IsObjCIvar); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2776 | LookupVisibleDecls(Class, LookupMemberName, Consumer, |
| 2777 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2778 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2779 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2780 | |
| 2781 | // FIXME: How do we cope with isa? |
| 2782 | |
| 2783 | Results.ExitScope(); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2784 | |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2785 | // Hand off the results found for code completion. |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2786 | HandleCodeCompleteResults(this, CodeCompleter, |
| 2787 | CodeCompletionContext(CodeCompletionContext::CCC_MemberAccess, |
| 2788 | BaseType), |
| 2789 | Results.data(),Results.size()); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2792 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { |
| 2793 | if (!CodeCompleter) |
| 2794 | return; |
| 2795 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2796 | typedef CodeCompletionResult Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2797 | ResultBuilder::LookupFilter Filter = 0; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2798 | enum CodeCompletionContext::Kind ContextKind |
| 2799 | = CodeCompletionContext::CCC_Other; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2800 | switch ((DeclSpec::TST)TagSpec) { |
| 2801 | case DeclSpec::TST_enum: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2802 | Filter = &ResultBuilder::IsEnum; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2803 | ContextKind = CodeCompletionContext::CCC_EnumTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2804 | break; |
| 2805 | |
| 2806 | case DeclSpec::TST_union: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2807 | Filter = &ResultBuilder::IsUnion; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2808 | ContextKind = CodeCompletionContext::CCC_UnionTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2809 | break; |
| 2810 | |
| 2811 | case DeclSpec::TST_struct: |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2812 | case DeclSpec::TST_class: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2813 | Filter = &ResultBuilder::IsClassOrStruct; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2814 | ContextKind = CodeCompletionContext::CCC_ClassOrStructTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2815 | break; |
| 2816 | |
| 2817 | default: |
| 2818 | assert(false && "Unknown type specifier kind in CodeCompleteTag"); |
| 2819 | return; |
| 2820 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2821 | |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 2822 | ResultBuilder Results(*this); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 2823 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 2824 | |
| 2825 | // First pass: look for tags. |
| 2826 | Results.setFilter(Filter); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2827 | LookupVisibleDecls(S, LookupTagName, Consumer, |
| 2828 | CodeCompleter->includeGlobals()); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 2829 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2830 | if (CodeCompleter->includeGlobals()) { |
| 2831 | // Second pass: look for nested name specifiers. |
| 2832 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
| 2833 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer); |
| 2834 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2835 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2836 | HandleCodeCompleteResults(this, CodeCompleter, ContextKind, |
| 2837 | Results.data(),Results.size()); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 2840 | void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) { |
| 2841 | ResultBuilder Results(*this); |
| 2842 | Results.EnterNewScope(); |
| 2843 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const)) |
| 2844 | Results.AddResult("const"); |
| 2845 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile)) |
| 2846 | Results.AddResult("volatile"); |
| 2847 | if (getLangOptions().C99 && |
| 2848 | !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict)) |
| 2849 | Results.AddResult("restrict"); |
| 2850 | Results.ExitScope(); |
| 2851 | HandleCodeCompleteResults(this, CodeCompleter, |
| 2852 | CodeCompletionContext::CCC_TypeQualifiers, |
| 2853 | Results.data(), Results.size()); |
| 2854 | } |
| 2855 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2856 | void Sema::CodeCompleteCase(Scope *S) { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 2857 | if (getCurFunction()->SwitchStack.empty() || !CodeCompleter) |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2858 | return; |
| 2859 | |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 2860 | SwitchStmt *Switch = getCurFunction()->SwitchStack.back(); |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 2861 | if (!Switch->getCond()->getType()->isEnumeralType()) { |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2862 | CodeCompleteExpressionData Data(Switch->getCond()->getType()); |
| 2863 | Data.IntegralConstantExpression = true; |
| 2864 | CodeCompleteExpression(S, Data); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2865 | return; |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 2866 | } |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2867 | |
| 2868 | // Code-complete the cases of a switch statement over an enumeration type |
| 2869 | // by providing the list of |
| 2870 | EnumDecl *Enum = Switch->getCond()->getType()->getAs<EnumType>()->getDecl(); |
| 2871 | |
| 2872 | // Determine which enumerators we have already seen in the switch statement. |
| 2873 | // FIXME: Ideally, we would also be able to look *past* the code-completion |
| 2874 | // token, in case we are code-completing in the middle of the switch and not |
| 2875 | // at the end. However, we aren't able to do so at the moment. |
| 2876 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2877 | NestedNameSpecifier *Qualifier = 0; |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2878 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; |
| 2879 | SC = SC->getNextSwitchCase()) { |
| 2880 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); |
| 2881 | if (!Case) |
| 2882 | continue; |
| 2883 | |
| 2884 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); |
| 2885 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal)) |
| 2886 | if (EnumConstantDecl *Enumerator |
| 2887 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 2888 | // We look into the AST of the case statement to determine which |
| 2889 | // enumerator was named. Alternatively, we could compute the value of |
| 2890 | // the integral constant expression, then compare it against the |
| 2891 | // values of each enumerator. However, value-based approach would not |
| 2892 | // work as well with C++ templates where enumerators declared within a |
| 2893 | // template are type- and value-dependent. |
| 2894 | EnumeratorsSeen.insert(Enumerator); |
| 2895 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2896 | // If this is a qualified-id, keep track of the nested-name-specifier |
| 2897 | // so that we can reproduce it as part of code completion, e.g., |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2898 | // |
| 2899 | // switch (TagD.getKind()) { |
| 2900 | // case TagDecl::TK_enum: |
| 2901 | // break; |
| 2902 | // case XXX |
| 2903 | // |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2904 | // At the XXX, our completions are TagDecl::TK_union, |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2905 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, |
| 2906 | // TK_struct, and TK_class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2907 | Qualifier = DRE->getQualifier(); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2908 | } |
| 2909 | } |
| 2910 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2911 | if (getLangOptions().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { |
| 2912 | // If there are no prior enumerators in C++, check whether we have to |
| 2913 | // qualify the names of the enumerators that we suggest, because they |
| 2914 | // may not be visible in this scope. |
| 2915 | Qualifier = getRequiredQualification(Context, CurContext, |
| 2916 | Enum->getDeclContext()); |
| 2917 | |
| 2918 | // FIXME: Scoped enums need to start with "EnumDecl" as the context! |
| 2919 | } |
| 2920 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2921 | // Add any enumerators that have not yet been mentioned. |
| 2922 | ResultBuilder Results(*this); |
| 2923 | Results.EnterNewScope(); |
| 2924 | for (EnumDecl::enumerator_iterator E = Enum->enumerator_begin(), |
| 2925 | EEnd = Enum->enumerator_end(); |
| 2926 | E != EEnd; ++E) { |
| 2927 | if (EnumeratorsSeen.count(*E)) |
| 2928 | continue; |
| 2929 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2930 | Results.AddResult(CodeCompletionResult(*E, Qualifier), |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 2931 | CurContext, 0, false); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2932 | } |
| 2933 | Results.ExitScope(); |
Douglas Gregor | 2f880e4 | 2010-04-06 20:02:15 +0000 | [diff] [blame] | 2934 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2935 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2936 | AddMacroResults(PP, Results); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2937 | HandleCodeCompleteResults(this, CodeCompleter, |
| 2938 | CodeCompletionContext::CCC_Expression, |
| 2939 | Results.data(),Results.size()); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2942 | namespace { |
| 2943 | struct IsBetterOverloadCandidate { |
| 2944 | Sema &S; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2945 | SourceLocation Loc; |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2946 | |
| 2947 | public: |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2948 | explicit IsBetterOverloadCandidate(Sema &S, SourceLocation Loc) |
| 2949 | : S(S), Loc(Loc) { } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2950 | |
| 2951 | bool |
| 2952 | operator()(const OverloadCandidate &X, const OverloadCandidate &Y) const { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2953 | return isBetterOverloadCandidate(S, X, Y, Loc); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2954 | } |
| 2955 | }; |
| 2956 | } |
| 2957 | |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 2958 | static bool anyNullArguments(Expr **Args, unsigned NumArgs) { |
| 2959 | if (NumArgs && !Args) |
| 2960 | return true; |
| 2961 | |
| 2962 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2963 | if (!Args[I]) |
| 2964 | return true; |
| 2965 | |
| 2966 | return false; |
| 2967 | } |
| 2968 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2969 | void Sema::CodeCompleteCall(Scope *S, ExprTy *FnIn, |
| 2970 | ExprTy **ArgsIn, unsigned NumArgs) { |
| 2971 | if (!CodeCompleter) |
| 2972 | return; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 2973 | |
| 2974 | // When we're code-completing for a call, we fall back to ordinary |
| 2975 | // name code-completion whenever we can't produce specific |
| 2976 | // results. We may want to revisit this strategy in the future, |
| 2977 | // e.g., by merging the two kinds of results. |
| 2978 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2979 | Expr *Fn = (Expr *)FnIn; |
| 2980 | Expr **Args = (Expr **)ArgsIn; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 2981 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2982 | // Ignore type-dependent call expressions entirely. |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 2983 | if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args, NumArgs) || |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 2984 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2985 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2986 | return; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 2987 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2988 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2989 | // Build an overload candidate set based on the functions we find. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2990 | SourceLocation Loc = Fn->getExprLoc(); |
| 2991 | OverloadCandidateSet CandidateSet(Loc); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2992 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2993 | // FIXME: What if we're calling something that isn't a function declaration? |
| 2994 | // FIXME: What if we're calling a pseudo-destructor? |
| 2995 | // FIXME: What if we're calling a member function? |
| 2996 | |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 2997 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; |
| 2998 | llvm::SmallVector<ResultCandidate, 8> Results; |
| 2999 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3000 | Expr *NakedFn = Fn->IgnoreParenCasts(); |
| 3001 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) |
| 3002 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet, |
| 3003 | /*PartialOverloading=*/ true); |
| 3004 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(NakedFn)) { |
| 3005 | FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3006 | if (FDecl) { |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3007 | if (!getLangOptions().CPlusPlus || |
| 3008 | !FDecl->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3009 | Results.push_back(ResultCandidate(FDecl)); |
| 3010 | else |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3011 | // FIXME: access? |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3012 | AddOverloadCandidate(FDecl, DeclAccessPair::make(FDecl, AS_none), |
| 3013 | Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 3014 | false, /*PartialOverloading*/true); |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3015 | } |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3016 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3017 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3018 | QualType ParamType; |
| 3019 | |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3020 | if (!CandidateSet.empty()) { |
| 3021 | // Sort the overload candidate set by placing the best overloads first. |
| 3022 | std::stable_sort(CandidateSet.begin(), CandidateSet.end(), |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3023 | IsBetterOverloadCandidate(*this, Loc)); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3024 | |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3025 | // Add the remaining viable overload candidates as code-completion reslults. |
| 3026 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3027 | CandEnd = CandidateSet.end(); |
| 3028 | Cand != CandEnd; ++Cand) { |
| 3029 | if (Cand->Viable) |
| 3030 | Results.push_back(ResultCandidate(Cand->Function)); |
| 3031 | } |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3032 | |
| 3033 | // From the viable candidates, try to determine the type of this parameter. |
| 3034 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 3035 | if (const FunctionType *FType = Results[I].getFunctionType()) |
| 3036 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FType)) |
| 3037 | if (NumArgs < Proto->getNumArgs()) { |
| 3038 | if (ParamType.isNull()) |
| 3039 | ParamType = Proto->getArgType(NumArgs); |
| 3040 | else if (!Context.hasSameUnqualifiedType( |
| 3041 | ParamType.getNonReferenceType(), |
| 3042 | Proto->getArgType(NumArgs).getNonReferenceType())) { |
| 3043 | ParamType = QualType(); |
| 3044 | break; |
| 3045 | } |
| 3046 | } |
| 3047 | } |
| 3048 | } else { |
| 3049 | // Try to determine the parameter type from the type of the expression |
| 3050 | // being called. |
| 3051 | QualType FunctionType = Fn->getType(); |
| 3052 | if (const PointerType *Ptr = FunctionType->getAs<PointerType>()) |
| 3053 | FunctionType = Ptr->getPointeeType(); |
| 3054 | else if (const BlockPointerType *BlockPtr |
| 3055 | = FunctionType->getAs<BlockPointerType>()) |
| 3056 | FunctionType = BlockPtr->getPointeeType(); |
| 3057 | else if (const MemberPointerType *MemPtr |
| 3058 | = FunctionType->getAs<MemberPointerType>()) |
| 3059 | FunctionType = MemPtr->getPointeeType(); |
| 3060 | |
| 3061 | if (const FunctionProtoType *Proto |
| 3062 | = FunctionType->getAs<FunctionProtoType>()) { |
| 3063 | if (NumArgs < Proto->getNumArgs()) |
| 3064 | ParamType = Proto->getArgType(NumArgs); |
| 3065 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3066 | } |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3067 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3068 | if (ParamType.isNull()) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3069 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3070 | else |
| 3071 | CodeCompleteExpression(S, ParamType); |
| 3072 | |
Douglas Gregor | 2e4c7a5 | 2010-04-06 20:19:47 +0000 | [diff] [blame] | 3073 | if (!Results.empty()) |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3074 | CodeCompleter->ProcessOverloadCandidates(*this, NumArgs, Results.data(), |
| 3075 | Results.size()); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3076 | } |
| 3077 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3078 | void Sema::CodeCompleteInitializer(Scope *S, Decl *D) { |
| 3079 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3080 | if (!VD) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3081 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3082 | return; |
| 3083 | } |
| 3084 | |
| 3085 | CodeCompleteExpression(S, VD->getType()); |
| 3086 | } |
| 3087 | |
| 3088 | void Sema::CodeCompleteReturn(Scope *S) { |
| 3089 | QualType ResultType; |
| 3090 | if (isa<BlockDecl>(CurContext)) { |
| 3091 | if (BlockScopeInfo *BSI = getCurBlock()) |
| 3092 | ResultType = BSI->ReturnType; |
| 3093 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext)) |
| 3094 | ResultType = Function->getResultType(); |
| 3095 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext)) |
| 3096 | ResultType = Method->getResultType(); |
| 3097 | |
| 3098 | if (ResultType.isNull()) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3099 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3100 | else |
| 3101 | CodeCompleteExpression(S, ResultType); |
| 3102 | } |
| 3103 | |
| 3104 | void Sema::CodeCompleteAssignmentRHS(Scope *S, ExprTy *LHS) { |
| 3105 | if (LHS) |
| 3106 | CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType()); |
| 3107 | else |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3108 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3111 | void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3112 | bool EnteringContext) { |
| 3113 | if (!SS.getScopeRep() || !CodeCompleter) |
| 3114 | return; |
| 3115 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3116 | DeclContext *Ctx = computeDeclContext(SS, EnteringContext); |
| 3117 | if (!Ctx) |
| 3118 | return; |
Douglas Gregor | d1cd31a | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 3119 | |
| 3120 | // Try to instantiate any non-dependent declaration contexts before |
| 3121 | // we look in them. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3122 | if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx)) |
Douglas Gregor | d1cd31a | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 3123 | return; |
| 3124 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3125 | ResultBuilder Results(*this); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3126 | |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3127 | Results.EnterNewScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3128 | // The "template" keyword can follow "::" in the grammar, but only |
| 3129 | // put it into the grammar if the nested-name-specifier is dependent. |
| 3130 | NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 3131 | if (!Results.empty() && NNS->isDependent()) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3132 | Results.AddResult("template"); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3133 | |
| 3134 | // Add calls to overridden virtual functions, if there are any. |
| 3135 | // |
| 3136 | // FIXME: This isn't wonderful, because we don't know whether we're actually |
| 3137 | // in a context that permits expressions. This is a general issue with |
| 3138 | // qualified-id completions. |
| 3139 | if (!EnteringContext) |
| 3140 | MaybeAddOverrideCalls(*this, Ctx, Results); |
| 3141 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3142 | |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3143 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3144 | LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer); |
| 3145 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3146 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3147 | CodeCompletionContext::CCC_Name, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3148 | Results.data(),Results.size()); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3149 | } |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3150 | |
| 3151 | void Sema::CodeCompleteUsing(Scope *S) { |
| 3152 | if (!CodeCompleter) |
| 3153 | return; |
| 3154 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3155 | ResultBuilder Results(*this, &ResultBuilder::IsNestedNameSpecifier); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3156 | Results.EnterNewScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3157 | |
| 3158 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 3159 | if (!S->isClassScope()) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3160 | Results.AddResult(CodeCompletionResult("namespace")); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3161 | |
| 3162 | // After "using", we can see anything that would start a |
| 3163 | // nested-name-specifier. |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3164 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3165 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3166 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3167 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3168 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3169 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3170 | CodeCompletionContext::CCC_Other, |
| 3171 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3172 | } |
| 3173 | |
| 3174 | void Sema::CodeCompleteUsingDirective(Scope *S) { |
| 3175 | if (!CodeCompleter) |
| 3176 | return; |
| 3177 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3178 | // After "using namespace", we expect to see a namespace name or namespace |
| 3179 | // alias. |
| 3180 | ResultBuilder Results(*this, &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3181 | Results.EnterNewScope(); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3182 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3183 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3184 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3185 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3186 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3187 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3188 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3189 | } |
| 3190 | |
| 3191 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { |
| 3192 | if (!CodeCompleter) |
| 3193 | return; |
| 3194 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3195 | ResultBuilder Results(*this, &ResultBuilder::IsNamespace); |
| 3196 | DeclContext *Ctx = (DeclContext *)S->getEntity(); |
| 3197 | if (!S->getParent()) |
| 3198 | Ctx = Context.getTranslationUnitDecl(); |
| 3199 | |
| 3200 | if (Ctx && Ctx->isFileContext()) { |
| 3201 | // We only want to see those namespaces that have already been defined |
| 3202 | // within this scope, because its likely that the user is creating an |
| 3203 | // extended namespace declaration. Keep track of the most recent |
| 3204 | // definition of each namespace. |
| 3205 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
| 3206 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
| 3207 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); |
| 3208 | NS != NSEnd; ++NS) |
| 3209 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
| 3210 | |
| 3211 | // Add the most recent definition (or extended definition) of each |
| 3212 | // namespace to the list of results. |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3213 | Results.EnterNewScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3214 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
| 3215 | NS = OrigToLatest.begin(), NSEnd = OrigToLatest.end(); |
| 3216 | NS != NSEnd; ++NS) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3217 | Results.AddResult(CodeCompletionResult(NS->second, 0), |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 3218 | CurContext, 0, false); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3219 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3222 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3223 | CodeCompletionContext::CCC_Other, |
| 3224 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
| 3227 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { |
| 3228 | if (!CodeCompleter) |
| 3229 | return; |
| 3230 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3231 | // After "namespace", we expect to see a namespace or alias. |
| 3232 | ResultBuilder Results(*this, &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3233 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3234 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3235 | CodeCompleter->includeGlobals()); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3236 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3237 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3238 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3241 | void Sema::CodeCompleteOperatorName(Scope *S) { |
| 3242 | if (!CodeCompleter) |
| 3243 | return; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3244 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3245 | typedef CodeCompletionResult Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3246 | ResultBuilder Results(*this, &ResultBuilder::IsType); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3247 | Results.EnterNewScope(); |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3248 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3249 | // Add the names of overloadable operators. |
| 3250 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 3251 | if (std::strcmp(Spelling, "?")) \ |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3252 | Results.AddResult(Result(Spelling)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3253 | #include "clang/Basic/OperatorKinds.def" |
| 3254 | |
| 3255 | // Add any type names visible from the current scope |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 3256 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3257 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3258 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3259 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3260 | |
| 3261 | // Add any type specifiers |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3262 | AddTypeSpecifierResults(getLangOptions(), Results); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3263 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3264 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3265 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3266 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3267 | Results.data(),Results.size()); |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3268 | } |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3269 | |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame^] | 3270 | void Sema::CodeCompleteConstructorInitializer(Decl *ConstructorD, |
| 3271 | CXXBaseOrMemberInitializer** Initializers, |
| 3272 | unsigned NumInitializers) { |
| 3273 | CXXConstructorDecl *Constructor |
| 3274 | = static_cast<CXXConstructorDecl *>(ConstructorD); |
| 3275 | if (!Constructor) |
| 3276 | return; |
| 3277 | |
| 3278 | ResultBuilder Results(*this); |
| 3279 | Results.EnterNewScope(); |
| 3280 | |
| 3281 | // Fill in any already-initialized fields or base classes. |
| 3282 | llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields; |
| 3283 | llvm::SmallPtrSet<CanQualType, 4> InitializedBases; |
| 3284 | for (unsigned I = 0; I != NumInitializers; ++I) { |
| 3285 | if (Initializers[I]->isBaseInitializer()) |
| 3286 | InitializedBases.insert( |
| 3287 | Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0))); |
| 3288 | else |
| 3289 | InitializedFields.insert(cast<FieldDecl>(Initializers[I]->getMember())); |
| 3290 | } |
| 3291 | |
| 3292 | // Add completions for base classes. |
| 3293 | unsigned Priority = 1; |
| 3294 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 3295 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3296 | BaseEnd = ClassDecl->bases_end(); |
| 3297 | Base != BaseEnd; ++Base) { |
| 3298 | if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) |
| 3299 | continue; |
| 3300 | |
| 3301 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 3302 | Pattern->AddTypedTextChunk( |
| 3303 | Base->getType().getAsString(Context.PrintingPolicy)); |
| 3304 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 3305 | Pattern->AddPlaceholderChunk("args"); |
| 3306 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 3307 | Results.AddResult(CodeCompletionResult(Pattern, Priority++)); |
| 3308 | } |
| 3309 | |
| 3310 | // Add completions for virtual base classes. |
| 3311 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 3312 | BaseEnd = ClassDecl->vbases_end(); |
| 3313 | Base != BaseEnd; ++Base) { |
| 3314 | if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) |
| 3315 | continue; |
| 3316 | |
| 3317 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 3318 | Pattern->AddTypedTextChunk( |
| 3319 | Base->getType().getAsString(Context.PrintingPolicy)); |
| 3320 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 3321 | Pattern->AddPlaceholderChunk("args"); |
| 3322 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 3323 | Results.AddResult(CodeCompletionResult(Pattern, Priority++)); |
| 3324 | } |
| 3325 | |
| 3326 | // Add completions for members. |
| 3327 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 3328 | FieldEnd = ClassDecl->field_end(); |
| 3329 | Field != FieldEnd; ++Field) { |
| 3330 | if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl()))) |
| 3331 | continue; |
| 3332 | |
| 3333 | if (!Field->getDeclName()) |
| 3334 | continue; |
| 3335 | |
| 3336 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 3337 | Pattern->AddTypedTextChunk(Field->getIdentifier()->getName()); |
| 3338 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 3339 | Pattern->AddPlaceholderChunk("args"); |
| 3340 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 3341 | Results.AddResult(CodeCompletionResult(Pattern, Priority++)); |
| 3342 | } |
| 3343 | Results.ExitScope(); |
| 3344 | |
| 3345 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3346 | CodeCompletionContext::CCC_Name, |
| 3347 | Results.data(), Results.size()); |
| 3348 | } |
| 3349 | |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3350 | // Macro that expands to @Keyword or Keyword, depending on whether NeedAt is |
| 3351 | // true or false. |
| 3352 | #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) NeedAt? "@" #Keyword : #Keyword |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3353 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3354 | ResultBuilder &Results, |
| 3355 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3356 | typedef CodeCompletionResult Result; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3357 | // Since we have an implementation, we can end it. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3358 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3359 | |
| 3360 | CodeCompletionString *Pattern = 0; |
| 3361 | if (LangOpts.ObjC2) { |
| 3362 | // @dynamic |
| 3363 | Pattern = new CodeCompletionString; |
| 3364 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,dynamic)); |
| 3365 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3366 | Pattern->AddPlaceholderChunk("property"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3367 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3368 | |
| 3369 | // @synthesize |
| 3370 | Pattern = new CodeCompletionString; |
| 3371 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synthesize)); |
| 3372 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3373 | Pattern->AddPlaceholderChunk("property"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3374 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3375 | } |
| 3376 | } |
| 3377 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3378 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3379 | ResultBuilder &Results, |
| 3380 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3381 | typedef CodeCompletionResult Result; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3382 | |
| 3383 | // Since we have an interface or protocol, we can end it. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3384 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3385 | |
| 3386 | if (LangOpts.ObjC2) { |
| 3387 | // @property |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3388 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,property))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3389 | |
| 3390 | // @required |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3391 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,required))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3392 | |
| 3393 | // @optional |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3394 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,optional))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3395 | } |
| 3396 | } |
| 3397 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3398 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3399 | typedef CodeCompletionResult Result; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3400 | CodeCompletionString *Pattern = 0; |
| 3401 | |
| 3402 | // @class name ; |
| 3403 | Pattern = new CodeCompletionString; |
| 3404 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,class)); |
| 3405 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3406 | Pattern->AddPlaceholderChunk("name"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3407 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3408 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3409 | if (Results.includeCodePatterns()) { |
| 3410 | // @interface name |
| 3411 | // FIXME: Could introduce the whole pattern, including superclasses and |
| 3412 | // such. |
| 3413 | Pattern = new CodeCompletionString; |
| 3414 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,interface)); |
| 3415 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3416 | Pattern->AddPlaceholderChunk("class"); |
| 3417 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3418 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3419 | // @protocol name |
| 3420 | Pattern = new CodeCompletionString; |
| 3421 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol)); |
| 3422 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3423 | Pattern->AddPlaceholderChunk("protocol"); |
| 3424 | Results.AddResult(Result(Pattern)); |
| 3425 | |
| 3426 | // @implementation name |
| 3427 | Pattern = new CodeCompletionString; |
| 3428 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,implementation)); |
| 3429 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3430 | Pattern->AddPlaceholderChunk("class"); |
| 3431 | Results.AddResult(Result(Pattern)); |
| 3432 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3433 | |
| 3434 | // @compatibility_alias name |
| 3435 | Pattern = new CodeCompletionString; |
| 3436 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,compatibility_alias)); |
| 3437 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3438 | Pattern->AddPlaceholderChunk("alias"); |
| 3439 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3440 | Pattern->AddPlaceholderChunk("class"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3441 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3442 | } |
| 3443 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3444 | void Sema::CodeCompleteObjCAtDirective(Scope *S, Decl *ObjCImpDecl, |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 3445 | bool InInterface) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3446 | typedef CodeCompletionResult Result; |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 3447 | ResultBuilder Results(*this); |
| 3448 | Results.EnterNewScope(); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3449 | if (ObjCImpDecl) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3450 | AddObjCImplementationResults(getLangOptions(), Results, false); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3451 | else if (InInterface) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3452 | AddObjCInterfaceResults(getLangOptions(), Results, false); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3453 | else |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3454 | AddObjCTopLevelResults(Results, false); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 3455 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3456 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3457 | CodeCompletionContext::CCC_Other, |
| 3458 | Results.data(),Results.size()); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 3459 | } |
| 3460 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3461 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3462 | typedef CodeCompletionResult Result; |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3463 | CodeCompletionString *Pattern = 0; |
| 3464 | |
| 3465 | // @encode ( type-name ) |
| 3466 | Pattern = new CodeCompletionString; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3467 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,encode)); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3468 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 3469 | Pattern->AddPlaceholderChunk("type-name"); |
| 3470 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3471 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3472 | |
| 3473 | // @protocol ( protocol-name ) |
| 3474 | Pattern = new CodeCompletionString; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3475 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol)); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3476 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 3477 | Pattern->AddPlaceholderChunk("protocol-name"); |
| 3478 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3479 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3480 | |
| 3481 | // @selector ( selector ) |
| 3482 | Pattern = new CodeCompletionString; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3483 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,selector)); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3484 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 3485 | Pattern->AddPlaceholderChunk("selector"); |
| 3486 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3487 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3490 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3491 | typedef CodeCompletionResult Result; |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3492 | CodeCompletionString *Pattern = 0; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3493 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3494 | if (Results.includeCodePatterns()) { |
| 3495 | // @try { statements } @catch ( declaration ) { statements } @finally |
| 3496 | // { statements } |
| 3497 | Pattern = new CodeCompletionString; |
| 3498 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,try)); |
| 3499 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3500 | Pattern->AddPlaceholderChunk("statements"); |
| 3501 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 3502 | Pattern->AddTextChunk("@catch"); |
| 3503 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 3504 | Pattern->AddPlaceholderChunk("parameter"); |
| 3505 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 3506 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3507 | Pattern->AddPlaceholderChunk("statements"); |
| 3508 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 3509 | Pattern->AddTextChunk("@finally"); |
| 3510 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3511 | Pattern->AddPlaceholderChunk("statements"); |
| 3512 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 3513 | Results.AddResult(Result(Pattern)); |
| 3514 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3515 | |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3516 | // @throw |
| 3517 | Pattern = new CodeCompletionString; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3518 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,throw)); |
Douglas Gregor | 834389b | 2010-01-12 06:38:28 +0000 | [diff] [blame] | 3519 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3520 | Pattern->AddPlaceholderChunk("expression"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3521 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3522 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3523 | if (Results.includeCodePatterns()) { |
| 3524 | // @synchronized ( expression ) { statements } |
| 3525 | Pattern = new CodeCompletionString; |
| 3526 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synchronized)); |
| 3527 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3528 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 3529 | Pattern->AddPlaceholderChunk("expression"); |
| 3530 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 3531 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3532 | Pattern->AddPlaceholderChunk("statements"); |
| 3533 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 3534 | Results.AddResult(Result(Pattern)); |
| 3535 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3536 | } |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3537 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3538 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3539 | ResultBuilder &Results, |
| 3540 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3541 | typedef CodeCompletionResult Result; |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3542 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,private))); |
| 3543 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,protected))); |
| 3544 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,public))); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3545 | if (LangOpts.ObjC2) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3546 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,package))); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | void Sema::CodeCompleteObjCAtVisibility(Scope *S) { |
| 3550 | ResultBuilder Results(*this); |
| 3551 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3552 | AddObjCVisibilityResults(getLangOptions(), Results, false); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3553 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3554 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3555 | CodeCompletionContext::CCC_Other, |
| 3556 | Results.data(),Results.size()); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | void Sema::CodeCompleteObjCAtStatement(Scope *S) { |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3560 | ResultBuilder Results(*this); |
| 3561 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3562 | AddObjCStatementResults(Results, false); |
| 3563 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3564 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3565 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3566 | CodeCompletionContext::CCC_Other, |
| 3567 | Results.data(),Results.size()); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
| 3570 | void Sema::CodeCompleteObjCAtExpression(Scope *S) { |
| 3571 | ResultBuilder Results(*this); |
| 3572 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3573 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3574 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3575 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3576 | CodeCompletionContext::CCC_Other, |
| 3577 | Results.data(),Results.size()); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3578 | } |
| 3579 | |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3580 | /// \brief Determine whether the addition of the given flag to an Objective-C |
| 3581 | /// property's attributes will cause a conflict. |
| 3582 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { |
| 3583 | // Check if we've already added this flag. |
| 3584 | if (Attributes & NewFlag) |
| 3585 | return true; |
| 3586 | |
| 3587 | Attributes |= NewFlag; |
| 3588 | |
| 3589 | // Check for collisions with "readonly". |
| 3590 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 3591 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | |
| 3592 | ObjCDeclSpec::DQ_PR_assign | |
| 3593 | ObjCDeclSpec::DQ_PR_copy | |
| 3594 | ObjCDeclSpec::DQ_PR_retain))) |
| 3595 | return true; |
| 3596 | |
| 3597 | // Check for more than one of { assign, copy, retain }. |
| 3598 | unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign | |
| 3599 | ObjCDeclSpec::DQ_PR_copy | |
| 3600 | ObjCDeclSpec::DQ_PR_retain); |
| 3601 | if (AssignCopyRetMask && |
| 3602 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && |
| 3603 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && |
| 3604 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain) |
| 3605 | return true; |
| 3606 | |
| 3607 | return false; |
| 3608 | } |
| 3609 | |
Douglas Gregor | a93b108 | 2009-11-18 23:08:07 +0000 | [diff] [blame] | 3610 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 3611 | if (!CodeCompleter) |
| 3612 | return; |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3613 | |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 3614 | unsigned Attributes = ODS.getPropertyAttributes(); |
| 3615 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3616 | typedef CodeCompletionResult Result; |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 3617 | ResultBuilder Results(*this); |
| 3618 | Results.EnterNewScope(); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3619 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3620 | Results.AddResult(CodeCompletionResult("readonly")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3621 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3622 | Results.AddResult(CodeCompletionResult("assign")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3623 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3624 | Results.AddResult(CodeCompletionResult("readwrite")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3625 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3626 | Results.AddResult(CodeCompletionResult("retain")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3627 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3628 | Results.AddResult(CodeCompletionResult("copy")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3629 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3630 | Results.AddResult(CodeCompletionResult("nonatomic")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3631 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 3632 | CodeCompletionString *Setter = new CodeCompletionString; |
| 3633 | Setter->AddTypedTextChunk("setter"); |
| 3634 | Setter->AddTextChunk(" = "); |
| 3635 | Setter->AddPlaceholderChunk("method"); |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3636 | Results.AddResult(CodeCompletionResult(Setter)); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 3637 | } |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3638 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 3639 | CodeCompletionString *Getter = new CodeCompletionString; |
| 3640 | Getter->AddTypedTextChunk("getter"); |
| 3641 | Getter->AddTextChunk(" = "); |
| 3642 | Getter->AddPlaceholderChunk("method"); |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3643 | Results.AddResult(CodeCompletionResult(Getter)); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 3644 | } |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 3645 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3646 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3647 | CodeCompletionContext::CCC_Other, |
| 3648 | Results.data(),Results.size()); |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 3649 | } |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 3650 | |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3651 | /// \brief Descripts the kind of Objective-C method that we want to find |
| 3652 | /// via code completion. |
| 3653 | enum ObjCMethodKind { |
| 3654 | MK_Any, //< Any kind of method, provided it means other specified criteria. |
| 3655 | MK_ZeroArgSelector, //< Zero-argument (unary) selector. |
| 3656 | MK_OneArgSelector //< One-argument selector. |
| 3657 | }; |
| 3658 | |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 3659 | static bool isAcceptableObjCSelector(Selector Sel, |
| 3660 | ObjCMethodKind WantKind, |
| 3661 | IdentifierInfo **SelIdents, |
| 3662 | unsigned NumSelIdents) { |
| 3663 | if (NumSelIdents > Sel.getNumArgs()) |
| 3664 | return false; |
| 3665 | |
| 3666 | switch (WantKind) { |
| 3667 | case MK_Any: break; |
| 3668 | case MK_ZeroArgSelector: return Sel.isUnarySelector(); |
| 3669 | case MK_OneArgSelector: return Sel.getNumArgs() == 1; |
| 3670 | } |
| 3671 | |
| 3672 | for (unsigned I = 0; I != NumSelIdents; ++I) |
| 3673 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) |
| 3674 | return false; |
| 3675 | |
| 3676 | return true; |
| 3677 | } |
| 3678 | |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3679 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, |
| 3680 | ObjCMethodKind WantKind, |
| 3681 | IdentifierInfo **SelIdents, |
| 3682 | unsigned NumSelIdents) { |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 3683 | return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents, |
| 3684 | NumSelIdents); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3687 | /// \brief Add all of the Objective-C methods in the given Objective-C |
| 3688 | /// container to the set of results. |
| 3689 | /// |
| 3690 | /// The container will be a class, protocol, category, or implementation of |
| 3691 | /// any of the above. This mether will recurse to include methods from |
| 3692 | /// the superclasses of classes along with their categories, protocols, and |
| 3693 | /// implementations. |
| 3694 | /// |
| 3695 | /// \param Container the container in which we'll look to find methods. |
| 3696 | /// |
| 3697 | /// \param WantInstance whether to add instance methods (only); if false, this |
| 3698 | /// routine will add factory methods (only). |
| 3699 | /// |
| 3700 | /// \param CurContext the context in which we're performing the lookup that |
| 3701 | /// finds methods. |
| 3702 | /// |
| 3703 | /// \param Results the structure into which we'll add results. |
| 3704 | static void AddObjCMethods(ObjCContainerDecl *Container, |
| 3705 | bool WantInstanceMethods, |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3706 | ObjCMethodKind WantKind, |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3707 | IdentifierInfo **SelIdents, |
| 3708 | unsigned NumSelIdents, |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3709 | DeclContext *CurContext, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 3710 | ResultBuilder &Results, |
| 3711 | bool InOriginalClass = true) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3712 | typedef CodeCompletionResult Result; |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3713 | for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), |
| 3714 | MEnd = Container->meth_end(); |
| 3715 | M != MEnd; ++M) { |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3716 | if ((*M)->isInstanceMethod() == WantInstanceMethods) { |
| 3717 | // Check whether the selector identifiers we've been given are a |
| 3718 | // subset of the identifiers for this particular method. |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3719 | if (!isAcceptableObjCMethod(*M, WantKind, SelIdents, NumSelIdents)) |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3720 | continue; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3721 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3722 | Result R = Result(*M, 0); |
| 3723 | R.StartParameter = NumSelIdents; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3724 | R.AllParametersAreInformative = (WantKind != MK_Any); |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 3725 | if (!InOriginalClass) |
| 3726 | R.Priority += CCD_InBaseClass; |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3727 | Results.MaybeAddResult(R, CurContext); |
| 3728 | } |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); |
| 3732 | if (!IFace) |
| 3733 | return; |
| 3734 | |
| 3735 | // Add methods in protocols. |
| 3736 | const ObjCList<ObjCProtocolDecl> &Protocols= IFace->getReferencedProtocols(); |
| 3737 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 3738 | E = Protocols.end(); |
| 3739 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3740 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 3741 | CurContext, Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3742 | |
| 3743 | // Add methods in categories. |
| 3744 | for (ObjCCategoryDecl *CatDecl = IFace->getCategoryList(); CatDecl; |
| 3745 | CatDecl = CatDecl->getNextClassCategory()) { |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3746 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 3747 | NumSelIdents, CurContext, Results, InOriginalClass); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3748 | |
| 3749 | // Add a categories protocol methods. |
| 3750 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 3751 | = CatDecl->getReferencedProtocols(); |
| 3752 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 3753 | E = Protocols.end(); |
| 3754 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3755 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 3756 | NumSelIdents, CurContext, Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3757 | |
| 3758 | // Add methods in category implementations. |
| 3759 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3760 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 3761 | NumSelIdents, CurContext, Results, InOriginalClass); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3762 | } |
| 3763 | |
| 3764 | // Add methods in superclass. |
| 3765 | if (IFace->getSuperClass()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3766 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 3767 | SelIdents, NumSelIdents, CurContext, Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3768 | |
| 3769 | // Add methods in our implementation, if any. |
| 3770 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3771 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 3772 | NumSelIdents, CurContext, Results, InOriginalClass); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3773 | } |
| 3774 | |
| 3775 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3776 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S, Decl *ClassDecl, |
| 3777 | Decl **Methods, |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3778 | unsigned NumMethods) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3779 | typedef CodeCompletionResult Result; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3780 | |
| 3781 | // Try to find the interface where getters might live. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3782 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(ClassDecl); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3783 | if (!Class) { |
| 3784 | if (ObjCCategoryDecl *Category |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3785 | = dyn_cast_or_null<ObjCCategoryDecl>(ClassDecl)) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3786 | Class = Category->getClassInterface(); |
| 3787 | |
| 3788 | if (!Class) |
| 3789 | return; |
| 3790 | } |
| 3791 | |
| 3792 | // Find all of the potential getters. |
| 3793 | ResultBuilder Results(*this); |
| 3794 | Results.EnterNewScope(); |
| 3795 | |
| 3796 | // FIXME: We need to do this because Objective-C methods don't get |
| 3797 | // pushed into DeclContexts early enough. Argh! |
| 3798 | for (unsigned I = 0; I != NumMethods; ++I) { |
| 3799 | if (ObjCMethodDecl *Method |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3800 | = dyn_cast_or_null<ObjCMethodDecl>(Methods[I])) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3801 | if (Method->isInstanceMethod() && |
| 3802 | isAcceptableObjCMethod(Method, MK_ZeroArgSelector, 0, 0)) { |
| 3803 | Result R = Result(Method, 0); |
| 3804 | R.AllParametersAreInformative = true; |
| 3805 | Results.MaybeAddResult(R, CurContext); |
| 3806 | } |
| 3807 | } |
| 3808 | |
| 3809 | AddObjCMethods(Class, true, MK_ZeroArgSelector, 0, 0, CurContext, Results); |
| 3810 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3811 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3812 | CodeCompletionContext::CCC_Other, |
| 3813 | Results.data(),Results.size()); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3814 | } |
| 3815 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3816 | void Sema::CodeCompleteObjCPropertySetter(Scope *S, Decl *ObjCImplDecl, |
| 3817 | Decl **Methods, |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3818 | unsigned NumMethods) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3819 | typedef CodeCompletionResult Result; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3820 | |
| 3821 | // Try to find the interface where setters might live. |
| 3822 | ObjCInterfaceDecl *Class |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3823 | = dyn_cast_or_null<ObjCInterfaceDecl>(ObjCImplDecl); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3824 | if (!Class) { |
| 3825 | if (ObjCCategoryDecl *Category |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3826 | = dyn_cast_or_null<ObjCCategoryDecl>(ObjCImplDecl)) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3827 | Class = Category->getClassInterface(); |
| 3828 | |
| 3829 | if (!Class) |
| 3830 | return; |
| 3831 | } |
| 3832 | |
| 3833 | // Find all of the potential getters. |
| 3834 | ResultBuilder Results(*this); |
| 3835 | Results.EnterNewScope(); |
| 3836 | |
| 3837 | // FIXME: We need to do this because Objective-C methods don't get |
| 3838 | // pushed into DeclContexts early enough. Argh! |
| 3839 | for (unsigned I = 0; I != NumMethods; ++I) { |
| 3840 | if (ObjCMethodDecl *Method |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3841 | = dyn_cast_or_null<ObjCMethodDecl>(Methods[I])) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3842 | if (Method->isInstanceMethod() && |
| 3843 | isAcceptableObjCMethod(Method, MK_OneArgSelector, 0, 0)) { |
| 3844 | Result R = Result(Method, 0); |
| 3845 | R.AllParametersAreInformative = true; |
| 3846 | Results.MaybeAddResult(R, CurContext); |
| 3847 | } |
| 3848 | } |
| 3849 | |
| 3850 | AddObjCMethods(Class, true, MK_OneArgSelector, 0, 0, CurContext, Results); |
| 3851 | |
| 3852 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3853 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3854 | CodeCompletionContext::CCC_Other, |
| 3855 | Results.data(),Results.size()); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3856 | } |
| 3857 | |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 3858 | void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3859 | typedef CodeCompletionResult Result; |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 3860 | ResultBuilder Results(*this); |
| 3861 | Results.EnterNewScope(); |
| 3862 | |
| 3863 | // Add context-sensitive, Objective-C parameter-passing keywords. |
| 3864 | bool AddedInOut = false; |
| 3865 | if ((DS.getObjCDeclQualifier() & |
| 3866 | (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 3867 | Results.AddResult("in"); |
| 3868 | Results.AddResult("inout"); |
| 3869 | AddedInOut = true; |
| 3870 | } |
| 3871 | if ((DS.getObjCDeclQualifier() & |
| 3872 | (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 3873 | Results.AddResult("out"); |
| 3874 | if (!AddedInOut) |
| 3875 | Results.AddResult("inout"); |
| 3876 | } |
| 3877 | if ((DS.getObjCDeclQualifier() & |
| 3878 | (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref | |
| 3879 | ObjCDeclSpec::DQ_Oneway)) == 0) { |
| 3880 | Results.AddResult("bycopy"); |
| 3881 | Results.AddResult("byref"); |
| 3882 | Results.AddResult("oneway"); |
| 3883 | } |
| 3884 | |
| 3885 | // Add various builtin type names and specifiers. |
| 3886 | AddOrdinaryNameResults(PCC_Type, S, *this, Results); |
| 3887 | Results.ExitScope(); |
| 3888 | |
| 3889 | // Add the various type names |
| 3890 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 3891 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3892 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3893 | CodeCompleter->includeGlobals()); |
| 3894 | |
| 3895 | if (CodeCompleter->includeMacros()) |
| 3896 | AddMacroResults(PP, Results); |
| 3897 | |
| 3898 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3899 | CodeCompletionContext::CCC_Type, |
| 3900 | Results.data(), Results.size()); |
| 3901 | } |
| 3902 | |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 3903 | /// \brief When we have an expression with type "id", we may assume |
| 3904 | /// that it has some more-specific class type based on knowledge of |
| 3905 | /// common uses of Objective-C. This routine returns that class type, |
| 3906 | /// or NULL if no better result could be determined. |
| 3907 | static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) { |
| 3908 | ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E); |
| 3909 | if (!Msg) |
| 3910 | return 0; |
| 3911 | |
| 3912 | Selector Sel = Msg->getSelector(); |
| 3913 | if (Sel.isNull()) |
| 3914 | return 0; |
| 3915 | |
| 3916 | IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0); |
| 3917 | if (!Id) |
| 3918 | return 0; |
| 3919 | |
| 3920 | ObjCMethodDecl *Method = Msg->getMethodDecl(); |
| 3921 | if (!Method) |
| 3922 | return 0; |
| 3923 | |
| 3924 | // Determine the class that we're sending the message to. |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3925 | ObjCInterfaceDecl *IFace = 0; |
| 3926 | switch (Msg->getReceiverKind()) { |
| 3927 | case ObjCMessageExpr::Class: |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3928 | if (const ObjCObjectType *ObjType |
| 3929 | = Msg->getClassReceiver()->getAs<ObjCObjectType>()) |
| 3930 | IFace = ObjType->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3931 | break; |
| 3932 | |
| 3933 | case ObjCMessageExpr::Instance: { |
| 3934 | QualType T = Msg->getInstanceReceiver()->getType(); |
| 3935 | if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) |
| 3936 | IFace = Ptr->getInterfaceDecl(); |
| 3937 | break; |
| 3938 | } |
| 3939 | |
| 3940 | case ObjCMessageExpr::SuperInstance: |
| 3941 | case ObjCMessageExpr::SuperClass: |
| 3942 | break; |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
| 3945 | if (!IFace) |
| 3946 | return 0; |
| 3947 | |
| 3948 | ObjCInterfaceDecl *Super = IFace->getSuperClass(); |
| 3949 | if (Method->isInstanceMethod()) |
| 3950 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 3951 | .Case("retain", IFace) |
| 3952 | .Case("autorelease", IFace) |
| 3953 | .Case("copy", IFace) |
| 3954 | .Case("copyWithZone", IFace) |
| 3955 | .Case("mutableCopy", IFace) |
| 3956 | .Case("mutableCopyWithZone", IFace) |
| 3957 | .Case("awakeFromCoder", IFace) |
| 3958 | .Case("replacementObjectFromCoder", IFace) |
| 3959 | .Case("class", IFace) |
| 3960 | .Case("classForCoder", IFace) |
| 3961 | .Case("superclass", Super) |
| 3962 | .Default(0); |
| 3963 | |
| 3964 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 3965 | .Case("new", IFace) |
| 3966 | .Case("alloc", IFace) |
| 3967 | .Case("allocWithZone", IFace) |
| 3968 | .Case("class", IFace) |
| 3969 | .Case("superclass", Super) |
| 3970 | .Default(0); |
| 3971 | } |
| 3972 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 3973 | // Add a special completion for a message send to "super", which fills in the |
| 3974 | // most likely case of forwarding all of our arguments to the superclass |
| 3975 | // function. |
| 3976 | /// |
| 3977 | /// \param S The semantic analysis object. |
| 3978 | /// |
| 3979 | /// \param S NeedSuperKeyword Whether we need to prefix this completion with |
| 3980 | /// the "super" keyword. Otherwise, we just need to provide the arguments. |
| 3981 | /// |
| 3982 | /// \param SelIdents The identifiers in the selector that have already been |
| 3983 | /// provided as arguments for a send to "super". |
| 3984 | /// |
| 3985 | /// \param NumSelIdents The number of identifiers in \p SelIdents. |
| 3986 | /// |
| 3987 | /// \param Results The set of results to augment. |
| 3988 | /// |
| 3989 | /// \returns the Objective-C method declaration that would be invoked by |
| 3990 | /// this "super" completion. If NULL, no completion was added. |
| 3991 | static ObjCMethodDecl *AddSuperSendCompletion(Sema &S, bool NeedSuperKeyword, |
| 3992 | IdentifierInfo **SelIdents, |
| 3993 | unsigned NumSelIdents, |
| 3994 | ResultBuilder &Results) { |
| 3995 | ObjCMethodDecl *CurMethod = S.getCurMethodDecl(); |
| 3996 | if (!CurMethod) |
| 3997 | return 0; |
| 3998 | |
| 3999 | ObjCInterfaceDecl *Class = CurMethod->getClassInterface(); |
| 4000 | if (!Class) |
| 4001 | return 0; |
| 4002 | |
| 4003 | // Try to find a superclass method with the same selector. |
| 4004 | ObjCMethodDecl *SuperMethod = 0; |
| 4005 | while ((Class = Class->getSuperClass()) && !SuperMethod) |
| 4006 | SuperMethod = Class->getMethod(CurMethod->getSelector(), |
| 4007 | CurMethod->isInstanceMethod()); |
| 4008 | |
| 4009 | if (!SuperMethod) |
| 4010 | return 0; |
| 4011 | |
| 4012 | // Check whether the superclass method has the same signature. |
| 4013 | if (CurMethod->param_size() != SuperMethod->param_size() || |
| 4014 | CurMethod->isVariadic() != SuperMethod->isVariadic()) |
| 4015 | return 0; |
| 4016 | |
| 4017 | for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(), |
| 4018 | CurPEnd = CurMethod->param_end(), |
| 4019 | SuperP = SuperMethod->param_begin(); |
| 4020 | CurP != CurPEnd; ++CurP, ++SuperP) { |
| 4021 | // Make sure the parameter types are compatible. |
| 4022 | if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(), |
| 4023 | (*SuperP)->getType())) |
| 4024 | return 0; |
| 4025 | |
| 4026 | // Make sure we have a parameter name to forward! |
| 4027 | if (!(*CurP)->getIdentifier()) |
| 4028 | return 0; |
| 4029 | } |
| 4030 | |
| 4031 | // We have a superclass method. Now, form the send-to-super completion. |
| 4032 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 4033 | |
| 4034 | // Give this completion a return type. |
| 4035 | AddResultTypeChunk(S.Context, SuperMethod, Pattern); |
| 4036 | |
| 4037 | // If we need the "super" keyword, add it (plus some spacing). |
| 4038 | if (NeedSuperKeyword) { |
| 4039 | Pattern->AddTypedTextChunk("super"); |
| 4040 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4041 | } |
| 4042 | |
| 4043 | Selector Sel = CurMethod->getSelector(); |
| 4044 | if (Sel.isUnarySelector()) { |
| 4045 | if (NeedSuperKeyword) |
| 4046 | Pattern->AddTextChunk(Sel.getIdentifierInfoForSlot(0)->getName()); |
| 4047 | else |
| 4048 | Pattern->AddTypedTextChunk(Sel.getIdentifierInfoForSlot(0)->getName()); |
| 4049 | } else { |
| 4050 | ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(); |
| 4051 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) { |
| 4052 | if (I > NumSelIdents) |
| 4053 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4054 | |
| 4055 | if (I < NumSelIdents) |
| 4056 | Pattern->AddInformativeChunk( |
| 4057 | Sel.getIdentifierInfoForSlot(I)->getName().str() + ":"); |
| 4058 | else if (NeedSuperKeyword || I > NumSelIdents) { |
| 4059 | Pattern->AddTextChunk( |
| 4060 | Sel.getIdentifierInfoForSlot(I)->getName().str() + ":"); |
| 4061 | Pattern->AddPlaceholderChunk((*CurP)->getIdentifier()->getName()); |
| 4062 | } else { |
| 4063 | Pattern->AddTypedTextChunk( |
| 4064 | Sel.getIdentifierInfoForSlot(I)->getName().str() + ":"); |
| 4065 | Pattern->AddPlaceholderChunk((*CurP)->getIdentifier()->getName()); |
| 4066 | } |
| 4067 | } |
| 4068 | } |
| 4069 | |
| 4070 | Results.AddResult(CodeCompletionResult(Pattern, CCP_SuperCompletion, |
| 4071 | SuperMethod->isInstanceMethod() |
| 4072 | ? CXCursor_ObjCInstanceMethodDecl |
| 4073 | : CXCursor_ObjCClassMethodDecl)); |
| 4074 | return SuperMethod; |
| 4075 | } |
| 4076 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4077 | void Sema::CodeCompleteObjCMessageReceiver(Scope *S) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4078 | typedef CodeCompletionResult Result; |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4079 | ResultBuilder Results(*this); |
| 4080 | |
| 4081 | // Find anything that looks like it could be a message receiver. |
| 4082 | Results.setFilter(&ResultBuilder::IsObjCMessageReceiver); |
| 4083 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4084 | Results.EnterNewScope(); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4085 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4086 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4087 | |
| 4088 | // If we are in an Objective-C method inside a class that has a superclass, |
| 4089 | // add "super" as an option. |
| 4090 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 4091 | if (ObjCInterfaceDecl *Iface = Method->getClassInterface()) |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4092 | if (Iface->getSuperClass()) { |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4093 | Results.AddResult(Result("super")); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4094 | |
| 4095 | AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, 0, 0, Results); |
| 4096 | } |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4097 | |
| 4098 | Results.ExitScope(); |
| 4099 | |
| 4100 | if (CodeCompleter->includeMacros()) |
| 4101 | AddMacroResults(PP, Results); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4102 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4103 | CodeCompletionContext::CCC_ObjCMessageReceiver, |
| 4104 | Results.data(), Results.size()); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4105 | |
| 4106 | } |
| 4107 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4108 | void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, |
| 4109 | IdentifierInfo **SelIdents, |
| 4110 | unsigned NumSelIdents) { |
| 4111 | ObjCInterfaceDecl *CDecl = 0; |
| 4112 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 4113 | // Figure out which interface we're in. |
| 4114 | CDecl = CurMethod->getClassInterface(); |
| 4115 | if (!CDecl) |
| 4116 | return; |
| 4117 | |
| 4118 | // Find the superclass of this class. |
| 4119 | CDecl = CDecl->getSuperClass(); |
| 4120 | if (!CDecl) |
| 4121 | return; |
| 4122 | |
| 4123 | if (CurMethod->isInstanceMethod()) { |
| 4124 | // We are inside an instance method, which means that the message |
| 4125 | // send [super ...] is actually calling an instance method on the |
| 4126 | // current object. Build the super expression and handle this like |
| 4127 | // an instance method. |
| 4128 | QualType SuperTy = Context.getObjCInterfaceType(CDecl); |
| 4129 | SuperTy = Context.getObjCObjectPointerType(SuperTy); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4130 | ExprResult Super |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4131 | = Owned(new (Context) ObjCSuperExpr(SuperLoc, SuperTy)); |
| 4132 | return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get(), |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4133 | SelIdents, NumSelIdents, |
| 4134 | /*IsSuper=*/true); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | // Fall through to send to the superclass in CDecl. |
| 4138 | } else { |
| 4139 | // "super" may be the name of a type or variable. Figure out which |
| 4140 | // it is. |
| 4141 | IdentifierInfo *Super = &Context.Idents.get("super"); |
| 4142 | NamedDecl *ND = LookupSingleName(S, Super, SuperLoc, |
| 4143 | LookupOrdinaryName); |
| 4144 | if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) { |
| 4145 | // "super" names an interface. Use it. |
| 4146 | } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4147 | if (const ObjCObjectType *Iface |
| 4148 | = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>()) |
| 4149 | CDecl = Iface->getInterface(); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4150 | } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) { |
| 4151 | // "super" names an unresolved type; we can't be more specific. |
| 4152 | } else { |
| 4153 | // Assume that "super" names some kind of value and parse that way. |
| 4154 | CXXScopeSpec SS; |
| 4155 | UnqualifiedId id; |
| 4156 | id.setIdentifier(Super, SuperLoc); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4157 | ExprResult SuperExpr = ActOnIdExpression(S, SS, id, false, false); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4158 | return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(), |
| 4159 | SelIdents, NumSelIdents); |
| 4160 | } |
| 4161 | |
| 4162 | // Fall through |
| 4163 | } |
| 4164 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4165 | ParsedType Receiver; |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4166 | if (CDecl) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4167 | Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4168 | return CodeCompleteObjCClassMessage(S, Receiver, SelIdents, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4169 | NumSelIdents, /*IsSuper=*/true); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4172 | void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4173 | IdentifierInfo **SelIdents, |
| 4174 | unsigned NumSelIdents) { |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4175 | CodeCompleteObjCClassMessage(S, Receiver, SelIdents, NumSelIdents, false); |
| 4176 | } |
| 4177 | |
| 4178 | void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, |
| 4179 | IdentifierInfo **SelIdents, |
| 4180 | unsigned NumSelIdents, |
| 4181 | bool IsSuper) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4182 | typedef CodeCompletionResult Result; |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4183 | ObjCInterfaceDecl *CDecl = 0; |
| 4184 | |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4185 | // If the given name refers to an interface type, retrieve the |
| 4186 | // corresponding declaration. |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4187 | if (Receiver) { |
| 4188 | QualType T = GetTypeFromParser(Receiver, 0); |
| 4189 | if (!T.isNull()) |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4190 | if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>()) |
| 4191 | CDecl = Interface->getInterface(); |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4194 | // Add all of the factory methods in this Objective-C class, its protocols, |
| 4195 | // superclasses, categories, implementation, etc. |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4196 | ResultBuilder Results(*this); |
| 4197 | Results.EnterNewScope(); |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4198 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4199 | // If this is a send-to-super, try to add the special "super" send |
| 4200 | // completion. |
| 4201 | if (IsSuper) { |
| 4202 | if (ObjCMethodDecl *SuperMethod |
| 4203 | = AddSuperSendCompletion(*this, false, SelIdents, NumSelIdents, |
| 4204 | Results)) |
| 4205 | Results.Ignore(SuperMethod); |
| 4206 | } |
| 4207 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 4208 | // If we're inside an Objective-C method definition, prefer its selector to |
| 4209 | // others. |
| 4210 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 4211 | Results.setPreferredSelector(CurMethod->getSelector()); |
| 4212 | |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4213 | if (CDecl) |
| 4214 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, NumSelIdents, CurContext, |
| 4215 | Results); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4216 | else { |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4217 | // We're messaging "id" as a type; provide all class/factory methods. |
| 4218 | |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4219 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 4220 | // pool from the AST file. |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4221 | if (ExternalSource) { |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 4222 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 4223 | I != N; ++I) { |
| 4224 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4225 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4226 | continue; |
| 4227 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4228 | ReadMethodPool(Sel); |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4229 | } |
| 4230 | } |
| 4231 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4232 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 4233 | MEnd = MethodPool.end(); |
| 4234 | M != MEnd; ++M) { |
| 4235 | for (ObjCMethodList *MethList = &M->second.second; |
| 4236 | MethList && MethList->Method; |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4237 | MethList = MethList->Next) { |
| 4238 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 4239 | NumSelIdents)) |
| 4240 | continue; |
| 4241 | |
| 4242 | Result R(MethList->Method, 0); |
| 4243 | R.StartParameter = NumSelIdents; |
| 4244 | R.AllParametersAreInformative = false; |
| 4245 | Results.MaybeAddResult(R, CurContext); |
| 4246 | } |
| 4247 | } |
| 4248 | } |
| 4249 | |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4250 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4251 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4252 | CodeCompletionContext::CCC_Other, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4253 | Results.data(), Results.size()); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4254 | } |
| 4255 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4256 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver, |
| 4257 | IdentifierInfo **SelIdents, |
| 4258 | unsigned NumSelIdents) { |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4259 | CodeCompleteObjCInstanceMessage(S, Receiver, SelIdents, NumSelIdents, false); |
| 4260 | } |
| 4261 | |
| 4262 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver, |
| 4263 | IdentifierInfo **SelIdents, |
| 4264 | unsigned NumSelIdents, |
| 4265 | bool IsSuper) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4266 | typedef CodeCompletionResult Result; |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4267 | |
| 4268 | Expr *RecExpr = static_cast<Expr *>(Receiver); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4269 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4270 | // If necessary, apply function/array conversion to the receiver. |
| 4271 | // C99 6.7.5.3p[7,8]. |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 4272 | DefaultFunctionArrayLvalueConversion(RecExpr); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4273 | QualType ReceiverType = RecExpr->getType(); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4274 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4275 | // Build the set of methods we can see. |
| 4276 | ResultBuilder Results(*this); |
| 4277 | Results.EnterNewScope(); |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4278 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4279 | // If this is a send-to-super, try to add the special "super" send |
| 4280 | // completion. |
| 4281 | if (IsSuper) { |
| 4282 | if (ObjCMethodDecl *SuperMethod |
| 4283 | = AddSuperSendCompletion(*this, false, SelIdents, NumSelIdents, |
| 4284 | Results)) |
| 4285 | Results.Ignore(SuperMethod); |
| 4286 | } |
| 4287 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 4288 | // If we're inside an Objective-C method definition, prefer its selector to |
| 4289 | // others. |
| 4290 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 4291 | Results.setPreferredSelector(CurMethod->getSelector()); |
| 4292 | |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4293 | // If we're messaging an expression with type "id" or "Class", check |
| 4294 | // whether we know something special about the receiver that allows |
| 4295 | // us to assume a more-specific receiver type. |
| 4296 | if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) |
| 4297 | if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) |
| 4298 | ReceiverType = Context.getObjCObjectPointerType( |
| 4299 | Context.getObjCInterfaceType(IFace)); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4300 | |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4301 | // Handle messages to Class. This really isn't a message to an instance |
| 4302 | // method, so we treat it the same way we would treat a message send to a |
| 4303 | // class method. |
| 4304 | if (ReceiverType->isObjCClassType() || |
| 4305 | ReceiverType->isObjCQualifiedClassType()) { |
| 4306 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 4307 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4308 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, NumSelIdents, |
| 4309 | CurContext, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4310 | } |
| 4311 | } |
| 4312 | // Handle messages to a qualified ID ("id<foo>"). |
| 4313 | else if (const ObjCObjectPointerType *QualID |
| 4314 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 4315 | // Search protocols for instance methods. |
| 4316 | for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(), |
| 4317 | E = QualID->qual_end(); |
| 4318 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4319 | AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext, |
| 4320 | Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4321 | } |
| 4322 | // Handle messages to a pointer to interface type. |
| 4323 | else if (const ObjCObjectPointerType *IFacePtr |
| 4324 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 4325 | // Search the class, its superclasses, etc., for instance methods. |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4326 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, |
| 4327 | NumSelIdents, CurContext, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4328 | |
| 4329 | // Search protocols for instance methods. |
| 4330 | for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(), |
| 4331 | E = IFacePtr->qual_end(); |
| 4332 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4333 | AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext, |
| 4334 | Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4335 | } |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4336 | // Handle messages to "id". |
| 4337 | else if (ReceiverType->isObjCIdType()) { |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4338 | // We're messaging "id", so provide all instance methods we know |
| 4339 | // about as code-completion results. |
| 4340 | |
| 4341 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 4342 | // pool from the AST file. |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4343 | if (ExternalSource) { |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 4344 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 4345 | I != N; ++I) { |
| 4346 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4347 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4348 | continue; |
| 4349 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4350 | ReadMethodPool(Sel); |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4351 | } |
| 4352 | } |
| 4353 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4354 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 4355 | MEnd = MethodPool.end(); |
| 4356 | M != MEnd; ++M) { |
| 4357 | for (ObjCMethodList *MethList = &M->second.first; |
| 4358 | MethList && MethList->Method; |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4359 | MethList = MethList->Next) { |
| 4360 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 4361 | NumSelIdents)) |
| 4362 | continue; |
| 4363 | |
| 4364 | Result R(MethList->Method, 0); |
| 4365 | R.StartParameter = NumSelIdents; |
| 4366 | R.AllParametersAreInformative = false; |
| 4367 | Results.MaybeAddResult(R, CurContext); |
| 4368 | } |
| 4369 | } |
| 4370 | } |
| 4371 | |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4372 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4373 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4374 | CodeCompletionContext::CCC_Other, |
| 4375 | Results.data(),Results.size()); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4376 | } |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4377 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4378 | void Sema::CodeCompleteObjCForCollection(Scope *S, |
| 4379 | DeclGroupPtrTy IterationVar) { |
| 4380 | CodeCompleteExpressionData Data; |
| 4381 | Data.ObjCCollection = true; |
| 4382 | |
| 4383 | if (IterationVar.getAsOpaquePtr()) { |
| 4384 | DeclGroupRef DG = IterationVar.getAsVal<DeclGroupRef>(); |
| 4385 | for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) { |
| 4386 | if (*I) |
| 4387 | Data.IgnoreDecls.push_back(*I); |
| 4388 | } |
| 4389 | } |
| 4390 | |
| 4391 | CodeCompleteExpression(S, Data); |
| 4392 | } |
| 4393 | |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4394 | void Sema::CodeCompleteObjCSelector(Scope *S, IdentifierInfo **SelIdents, |
| 4395 | unsigned NumSelIdents) { |
| 4396 | // If we have an external source, load the entire class method |
| 4397 | // pool from the AST file. |
| 4398 | if (ExternalSource) { |
| 4399 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 4400 | I != N; ++I) { |
| 4401 | Selector Sel = ExternalSource->GetExternalSelector(I); |
| 4402 | if (Sel.isNull() || MethodPool.count(Sel)) |
| 4403 | continue; |
| 4404 | |
| 4405 | ReadMethodPool(Sel); |
| 4406 | } |
| 4407 | } |
| 4408 | |
| 4409 | ResultBuilder Results(*this); |
| 4410 | Results.EnterNewScope(); |
| 4411 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 4412 | MEnd = MethodPool.end(); |
| 4413 | M != MEnd; ++M) { |
| 4414 | |
| 4415 | Selector Sel = M->first; |
| 4416 | if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents, NumSelIdents)) |
| 4417 | continue; |
| 4418 | |
| 4419 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 4420 | if (Sel.isUnarySelector()) { |
| 4421 | Pattern->AddTypedTextChunk(Sel.getIdentifierInfoForSlot(0)->getName()); |
| 4422 | Results.AddResult(Pattern); |
| 4423 | continue; |
| 4424 | } |
| 4425 | |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 4426 | std::string Accumulator; |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4427 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) { |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 4428 | if (I == NumSelIdents) { |
| 4429 | if (!Accumulator.empty()) { |
| 4430 | Pattern->AddInformativeChunk(Accumulator); |
| 4431 | Accumulator.clear(); |
| 4432 | } |
| 4433 | } |
| 4434 | |
| 4435 | Accumulator += Sel.getIdentifierInfoForSlot(I)->getName().str(); |
| 4436 | Accumulator += ':'; |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4437 | } |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 4438 | Pattern->AddTypedTextChunk(Accumulator); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4439 | Results.AddResult(Pattern); |
| 4440 | } |
| 4441 | Results.ExitScope(); |
| 4442 | |
| 4443 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4444 | CodeCompletionContext::CCC_SelectorName, |
| 4445 | Results.data(), Results.size()); |
| 4446 | } |
| 4447 | |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4448 | /// \brief Add all of the protocol declarations that we find in the given |
| 4449 | /// (translation unit) context. |
| 4450 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4451 | bool OnlyForwardDeclarations, |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4452 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4453 | typedef CodeCompletionResult Result; |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4454 | |
| 4455 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 4456 | DEnd = Ctx->decls_end(); |
| 4457 | D != DEnd; ++D) { |
| 4458 | // Record any protocols we find. |
| 4459 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*D)) |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4460 | if (!OnlyForwardDeclarations || Proto->isForwardDecl()) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 4461 | Results.AddResult(Result(Proto, 0), CurContext, 0, false); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4462 | |
| 4463 | // Record any forward-declared protocols we find. |
| 4464 | if (ObjCForwardProtocolDecl *Forward |
| 4465 | = dyn_cast<ObjCForwardProtocolDecl>(*D)) { |
| 4466 | for (ObjCForwardProtocolDecl::protocol_iterator |
| 4467 | P = Forward->protocol_begin(), |
| 4468 | PEnd = Forward->protocol_end(); |
| 4469 | P != PEnd; ++P) |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4470 | if (!OnlyForwardDeclarations || (*P)->isForwardDecl()) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 4471 | Results.AddResult(Result(*P, 0), CurContext, 0, false); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4472 | } |
| 4473 | } |
| 4474 | } |
| 4475 | |
| 4476 | void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols, |
| 4477 | unsigned NumProtocols) { |
| 4478 | ResultBuilder Results(*this); |
| 4479 | Results.EnterNewScope(); |
| 4480 | |
| 4481 | // Tell the result set to ignore all of the protocols we have |
| 4482 | // already seen. |
| 4483 | for (unsigned I = 0; I != NumProtocols; ++I) |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4484 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first, |
| 4485 | Protocols[I].second)) |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4486 | Results.Ignore(Protocol); |
| 4487 | |
| 4488 | // Add all protocols. |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4489 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 4490 | Results); |
| 4491 | |
| 4492 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4493 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4494 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 4495 | Results.data(),Results.size()); |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4496 | } |
| 4497 | |
| 4498 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { |
| 4499 | ResultBuilder Results(*this); |
| 4500 | Results.EnterNewScope(); |
| 4501 | |
| 4502 | // Add all protocols. |
| 4503 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 4504 | Results); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4505 | |
| 4506 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4507 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4508 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 4509 | Results.data(),Results.size()); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4510 | } |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4511 | |
| 4512 | /// \brief Add all of the Objective-C interface declarations that we find in |
| 4513 | /// the given (translation unit) context. |
| 4514 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, |
| 4515 | bool OnlyForwardDeclarations, |
| 4516 | bool OnlyUnimplemented, |
| 4517 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4518 | typedef CodeCompletionResult Result; |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4519 | |
| 4520 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 4521 | DEnd = Ctx->decls_end(); |
| 4522 | D != DEnd; ++D) { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4523 | // Record any interfaces we find. |
| 4524 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*D)) |
| 4525 | if ((!OnlyForwardDeclarations || Class->isForwardDecl()) && |
| 4526 | (!OnlyUnimplemented || !Class->getImplementation())) |
| 4527 | Results.AddResult(Result(Class, 0), CurContext, 0, false); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4528 | |
| 4529 | // Record any forward-declared interfaces we find. |
| 4530 | if (ObjCClassDecl *Forward = dyn_cast<ObjCClassDecl>(*D)) { |
| 4531 | for (ObjCClassDecl::iterator C = Forward->begin(), CEnd = Forward->end(); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4532 | C != CEnd; ++C) |
| 4533 | if ((!OnlyForwardDeclarations || C->getInterface()->isForwardDecl()) && |
| 4534 | (!OnlyUnimplemented || !C->getInterface()->getImplementation())) |
| 4535 | Results.AddResult(Result(C->getInterface(), 0), CurContext, |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 4536 | 0, false); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4537 | } |
| 4538 | } |
| 4539 | } |
| 4540 | |
| 4541 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { |
| 4542 | ResultBuilder Results(*this); |
| 4543 | Results.EnterNewScope(); |
| 4544 | |
| 4545 | // Add all classes. |
| 4546 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 4547 | false, Results); |
| 4548 | |
| 4549 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4550 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4551 | CodeCompletionContext::CCC_Other, |
| 4552 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4553 | } |
| 4554 | |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4555 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName, |
| 4556 | SourceLocation ClassNameLoc) { |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4557 | ResultBuilder Results(*this); |
| 4558 | Results.EnterNewScope(); |
| 4559 | |
| 4560 | // Make sure that we ignore the class we're currently defining. |
| 4561 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4562 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 4563 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4564 | Results.Ignore(CurClass); |
| 4565 | |
| 4566 | // Add all classes. |
| 4567 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 4568 | false, Results); |
| 4569 | |
| 4570 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4571 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4572 | CodeCompletionContext::CCC_Other, |
| 4573 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4574 | } |
| 4575 | |
| 4576 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { |
| 4577 | ResultBuilder Results(*this); |
| 4578 | Results.EnterNewScope(); |
| 4579 | |
| 4580 | // Add all unimplemented classes. |
| 4581 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 4582 | true, Results); |
| 4583 | |
| 4584 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4585 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4586 | CodeCompletionContext::CCC_Other, |
| 4587 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4588 | } |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 4589 | |
| 4590 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4591 | IdentifierInfo *ClassName, |
| 4592 | SourceLocation ClassNameLoc) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4593 | typedef CodeCompletionResult Result; |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 4594 | |
| 4595 | ResultBuilder Results(*this); |
| 4596 | |
| 4597 | // Ignore any categories we find that have already been implemented by this |
| 4598 | // interface. |
| 4599 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 4600 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4601 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 4602 | if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)) |
| 4603 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; |
| 4604 | Category = Category->getNextClassCategory()) |
| 4605 | CategoryNames.insert(Category->getIdentifier()); |
| 4606 | |
| 4607 | // Add all of the categories we know about. |
| 4608 | Results.EnterNewScope(); |
| 4609 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
| 4610 | for (DeclContext::decl_iterator D = TU->decls_begin(), |
| 4611 | DEnd = TU->decls_end(); |
| 4612 | D != DEnd; ++D) |
| 4613 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(*D)) |
| 4614 | if (CategoryNames.insert(Category->getIdentifier())) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 4615 | Results.AddResult(Result(Category, 0), CurContext, 0, false); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 4616 | Results.ExitScope(); |
| 4617 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4618 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4619 | CodeCompletionContext::CCC_Other, |
| 4620 | Results.data(),Results.size()); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4624 | IdentifierInfo *ClassName, |
| 4625 | SourceLocation ClassNameLoc) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4626 | typedef CodeCompletionResult Result; |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 4627 | |
| 4628 | // Find the corresponding interface. If we couldn't find the interface, the |
| 4629 | // program itself is ill-formed. However, we'll try to be helpful still by |
| 4630 | // providing the list of all of the categories we know about. |
| 4631 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4632 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 4633 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); |
| 4634 | if (!Class) |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4635 | return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 4636 | |
| 4637 | ResultBuilder Results(*this); |
| 4638 | |
| 4639 | // Add all of the categories that have have corresponding interface |
| 4640 | // declarations in this class and any of its superclasses, except for |
| 4641 | // already-implemented categories in the class itself. |
| 4642 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 4643 | Results.EnterNewScope(); |
| 4644 | bool IgnoreImplemented = true; |
| 4645 | while (Class) { |
| 4646 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; |
| 4647 | Category = Category->getNextClassCategory()) |
| 4648 | if ((!IgnoreImplemented || !Category->getImplementation()) && |
| 4649 | CategoryNames.insert(Category->getIdentifier())) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 4650 | Results.AddResult(Result(Category, 0), CurContext, 0, false); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 4651 | |
| 4652 | Class = Class->getSuperClass(); |
| 4653 | IgnoreImplemented = false; |
| 4654 | } |
| 4655 | Results.ExitScope(); |
| 4656 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4657 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4658 | CodeCompletionContext::CCC_Other, |
| 4659 | Results.data(),Results.size()); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 4660 | } |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 4661 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4662 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S, Decl *ObjCImpDecl) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4663 | typedef CodeCompletionResult Result; |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 4664 | ResultBuilder Results(*this); |
| 4665 | |
| 4666 | // Figure out where this @synthesize lives. |
| 4667 | ObjCContainerDecl *Container |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4668 | = dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 4669 | if (!Container || |
| 4670 | (!isa<ObjCImplementationDecl>(Container) && |
| 4671 | !isa<ObjCCategoryImplDecl>(Container))) |
| 4672 | return; |
| 4673 | |
| 4674 | // Ignore any properties that have already been implemented. |
| 4675 | for (DeclContext::decl_iterator D = Container->decls_begin(), |
| 4676 | DEnd = Container->decls_end(); |
| 4677 | D != DEnd; ++D) |
| 4678 | if (ObjCPropertyImplDecl *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(*D)) |
| 4679 | Results.Ignore(PropertyImpl->getPropertyDecl()); |
| 4680 | |
| 4681 | // Add any properties that we find. |
| 4682 | Results.EnterNewScope(); |
| 4683 | if (ObjCImplementationDecl *ClassImpl |
| 4684 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| 4685 | AddObjCProperties(ClassImpl->getClassInterface(), false, CurContext, |
| 4686 | Results); |
| 4687 | else |
| 4688 | AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), |
| 4689 | false, CurContext, Results); |
| 4690 | Results.ExitScope(); |
| 4691 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4692 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4693 | CodeCompletionContext::CCC_Other, |
| 4694 | Results.data(),Results.size()); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 4695 | } |
| 4696 | |
| 4697 | void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S, |
| 4698 | IdentifierInfo *PropertyName, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4699 | Decl *ObjCImpDecl) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4700 | typedef CodeCompletionResult Result; |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 4701 | ResultBuilder Results(*this); |
| 4702 | |
| 4703 | // Figure out where this @synthesize lives. |
| 4704 | ObjCContainerDecl *Container |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4705 | = dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 4706 | if (!Container || |
| 4707 | (!isa<ObjCImplementationDecl>(Container) && |
| 4708 | !isa<ObjCCategoryImplDecl>(Container))) |
| 4709 | return; |
| 4710 | |
| 4711 | // Figure out which interface we're looking into. |
| 4712 | ObjCInterfaceDecl *Class = 0; |
| 4713 | if (ObjCImplementationDecl *ClassImpl |
| 4714 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| 4715 | Class = ClassImpl->getClassInterface(); |
| 4716 | else |
| 4717 | Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl() |
| 4718 | ->getClassInterface(); |
| 4719 | |
| 4720 | // Add all of the instance variables in this class and its superclasses. |
| 4721 | Results.EnterNewScope(); |
| 4722 | for(; Class; Class = Class->getSuperClass()) { |
| 4723 | // FIXME: We could screen the type of each ivar for compatibility with |
| 4724 | // the property, but is that being too paternal? |
| 4725 | for (ObjCInterfaceDecl::ivar_iterator IVar = Class->ivar_begin(), |
| 4726 | IVarEnd = Class->ivar_end(); |
| 4727 | IVar != IVarEnd; ++IVar) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 4728 | Results.AddResult(Result(*IVar, 0), CurContext, 0, false); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 4729 | } |
| 4730 | Results.ExitScope(); |
| 4731 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4732 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4733 | CodeCompletionContext::CCC_Other, |
| 4734 | Results.data(),Results.size()); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 4735 | } |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4736 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4737 | // Mapping from selectors to the methods that implement that selector, along |
| 4738 | // with the "in original class" flag. |
| 4739 | typedef llvm::DenseMap<Selector, std::pair<ObjCMethodDecl *, bool> > |
| 4740 | KnownMethodsMap; |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4741 | |
| 4742 | /// \brief Find all of the methods that reside in the given container |
| 4743 | /// (and its superclasses, protocols, etc.) that meet the given |
| 4744 | /// criteria. Insert those methods into the map of known methods, |
| 4745 | /// indexed by selector so they can be easily found. |
| 4746 | static void FindImplementableMethods(ASTContext &Context, |
| 4747 | ObjCContainerDecl *Container, |
| 4748 | bool WantInstanceMethods, |
| 4749 | QualType ReturnType, |
| 4750 | bool IsInImplementation, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4751 | KnownMethodsMap &KnownMethods, |
| 4752 | bool InOriginalClass = true) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4753 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 4754 | // Recurse into protocols. |
| 4755 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 4756 | = IFace->getReferencedProtocols(); |
| 4757 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 4758 | E = Protocols.end(); |
| 4759 | I != E; ++I) |
| 4760 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4761 | IsInImplementation, KnownMethods, |
| 4762 | InOriginalClass); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4763 | |
| 4764 | // If we're not in the implementation of a class, also visit the |
| 4765 | // superclass. |
| 4766 | if (!IsInImplementation && IFace->getSuperClass()) |
| 4767 | FindImplementableMethods(Context, IFace->getSuperClass(), |
| 4768 | WantInstanceMethods, ReturnType, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4769 | IsInImplementation, KnownMethods, |
| 4770 | false); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4771 | |
| 4772 | // Add methods from any class extensions (but not from categories; |
| 4773 | // those should go into category implementations). |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 4774 | for (const ObjCCategoryDecl *Cat = IFace->getFirstClassExtension(); Cat; |
| 4775 | Cat = Cat->getNextClassExtension()) |
| 4776 | FindImplementableMethods(Context, const_cast<ObjCCategoryDecl*>(Cat), |
| 4777 | WantInstanceMethods, ReturnType, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4778 | IsInImplementation, KnownMethods, |
| 4779 | InOriginalClass); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
| 4782 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 4783 | // Recurse into protocols. |
| 4784 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 4785 | = Category->getReferencedProtocols(); |
| 4786 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 4787 | E = Protocols.end(); |
| 4788 | I != E; ++I) |
| 4789 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4790 | IsInImplementation, KnownMethods, |
| 4791 | InOriginalClass); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
| 4794 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 4795 | // Recurse into protocols. |
| 4796 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 4797 | = Protocol->getReferencedProtocols(); |
| 4798 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 4799 | E = Protocols.end(); |
| 4800 | I != E; ++I) |
| 4801 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4802 | IsInImplementation, KnownMethods, false); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | // Add methods in this container. This operation occurs last because |
| 4806 | // we want the methods from this container to override any methods |
| 4807 | // we've previously seen with the same selector. |
| 4808 | for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), |
| 4809 | MEnd = Container->meth_end(); |
| 4810 | M != MEnd; ++M) { |
| 4811 | if ((*M)->isInstanceMethod() == WantInstanceMethods) { |
| 4812 | if (!ReturnType.isNull() && |
| 4813 | !Context.hasSameUnqualifiedType(ReturnType, (*M)->getResultType())) |
| 4814 | continue; |
| 4815 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4816 | KnownMethods[(*M)->getSelector()] = std::make_pair(*M, InOriginalClass); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4817 | } |
| 4818 | } |
| 4819 | } |
| 4820 | |
| 4821 | void Sema::CodeCompleteObjCMethodDecl(Scope *S, |
| 4822 | bool IsInstanceMethod, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4823 | ParsedType ReturnTy, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4824 | Decl *IDecl) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4825 | // Determine the return type of the method we're declaring, if |
| 4826 | // provided. |
| 4827 | QualType ReturnType = GetTypeFromParser(ReturnTy); |
| 4828 | |
| 4829 | // Determine where we should start searching for methods, and where we |
| 4830 | ObjCContainerDecl *SearchDecl = 0, *CurrentDecl = 0; |
| 4831 | bool IsInImplementation = false; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4832 | if (Decl *D = IDecl) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4833 | if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) { |
| 4834 | SearchDecl = Impl->getClassInterface(); |
| 4835 | CurrentDecl = Impl; |
| 4836 | IsInImplementation = true; |
| 4837 | } else if (ObjCCategoryImplDecl *CatImpl |
| 4838 | = dyn_cast<ObjCCategoryImplDecl>(D)) { |
| 4839 | SearchDecl = CatImpl->getCategoryDecl(); |
| 4840 | CurrentDecl = CatImpl; |
| 4841 | IsInImplementation = true; |
| 4842 | } else { |
| 4843 | SearchDecl = dyn_cast<ObjCContainerDecl>(D); |
| 4844 | CurrentDecl = SearchDecl; |
| 4845 | } |
| 4846 | } |
| 4847 | |
| 4848 | if (!SearchDecl && S) { |
| 4849 | if (DeclContext *DC = static_cast<DeclContext *>(S->getEntity())) { |
| 4850 | SearchDecl = dyn_cast<ObjCContainerDecl>(DC); |
| 4851 | CurrentDecl = SearchDecl; |
| 4852 | } |
| 4853 | } |
| 4854 | |
| 4855 | if (!SearchDecl || !CurrentDecl) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4856 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4857 | CodeCompletionContext::CCC_Other, |
| 4858 | 0, 0); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4859 | return; |
| 4860 | } |
| 4861 | |
| 4862 | // Find all of the methods that we could declare/implement here. |
| 4863 | KnownMethodsMap KnownMethods; |
| 4864 | FindImplementableMethods(Context, SearchDecl, IsInstanceMethod, |
| 4865 | ReturnType, IsInImplementation, KnownMethods); |
| 4866 | |
| 4867 | // Erase any methods that have already been declared or |
| 4868 | // implemented here. |
| 4869 | for (ObjCContainerDecl::method_iterator M = CurrentDecl->meth_begin(), |
| 4870 | MEnd = CurrentDecl->meth_end(); |
| 4871 | M != MEnd; ++M) { |
| 4872 | if ((*M)->isInstanceMethod() != IsInstanceMethod) |
| 4873 | continue; |
| 4874 | |
| 4875 | KnownMethodsMap::iterator Pos = KnownMethods.find((*M)->getSelector()); |
| 4876 | if (Pos != KnownMethods.end()) |
| 4877 | KnownMethods.erase(Pos); |
| 4878 | } |
| 4879 | |
| 4880 | // Add declarations or definitions for each of the known methods. |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4881 | typedef CodeCompletionResult Result; |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4882 | ResultBuilder Results(*this); |
| 4883 | Results.EnterNewScope(); |
| 4884 | PrintingPolicy Policy(Context.PrintingPolicy); |
| 4885 | Policy.AnonymousTagLocations = false; |
| 4886 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
| 4887 | MEnd = KnownMethods.end(); |
| 4888 | M != MEnd; ++M) { |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4889 | ObjCMethodDecl *Method = M->second.first; |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4890 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 4891 | |
| 4892 | // If the result type was not already provided, add it to the |
| 4893 | // pattern as (type). |
| 4894 | if (ReturnType.isNull()) { |
| 4895 | std::string TypeStr; |
| 4896 | Method->getResultType().getAsStringInternal(TypeStr, Policy); |
| 4897 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 4898 | Pattern->AddTextChunk(TypeStr); |
| 4899 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 4900 | } |
| 4901 | |
| 4902 | Selector Sel = Method->getSelector(); |
| 4903 | |
| 4904 | // Add the first part of the selector to the pattern. |
| 4905 | Pattern->AddTypedTextChunk(Sel.getIdentifierInfoForSlot(0)->getName()); |
| 4906 | |
| 4907 | // Add parameters to the pattern. |
| 4908 | unsigned I = 0; |
| 4909 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 4910 | PEnd = Method->param_end(); |
| 4911 | P != PEnd; (void)++P, ++I) { |
| 4912 | // Add the part of the selector name. |
| 4913 | if (I == 0) |
| 4914 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
| 4915 | else if (I < Sel.getNumArgs()) { |
| 4916 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 47c03a7 | 2010-08-17 15:53:35 +0000 | [diff] [blame] | 4917 | Pattern->AddTextChunk(Sel.getIdentifierInfoForSlot(I)->getName()); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4918 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
| 4919 | } else |
| 4920 | break; |
| 4921 | |
| 4922 | // Add the parameter type. |
| 4923 | std::string TypeStr; |
| 4924 | (*P)->getOriginalType().getAsStringInternal(TypeStr, Policy); |
| 4925 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 4926 | Pattern->AddTextChunk(TypeStr); |
| 4927 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 4928 | |
| 4929 | if (IdentifierInfo *Id = (*P)->getIdentifier()) |
| 4930 | Pattern->AddTextChunk(Id->getName()); |
| 4931 | } |
| 4932 | |
| 4933 | if (Method->isVariadic()) { |
| 4934 | if (Method->param_size() > 0) |
| 4935 | Pattern->AddChunk(CodeCompletionString::CK_Comma); |
| 4936 | Pattern->AddTextChunk("..."); |
| 4937 | } |
| 4938 | |
Douglas Gregor | 447107d | 2010-05-28 00:57:46 +0000 | [diff] [blame] | 4939 | if (IsInImplementation && Results.includeCodePatterns()) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4940 | // We will be defining the method here, so add a compound statement. |
| 4941 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4942 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4943 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4944 | if (!Method->getResultType()->isVoidType()) { |
| 4945 | // If the result type is not void, add a return clause. |
| 4946 | Pattern->AddTextChunk("return"); |
| 4947 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4948 | Pattern->AddPlaceholderChunk("expression"); |
| 4949 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
| 4950 | } else |
| 4951 | Pattern->AddPlaceholderChunk("statements"); |
| 4952 | |
| 4953 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4954 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 4955 | } |
| 4956 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4957 | unsigned Priority = CCP_CodePattern; |
| 4958 | if (!M->second.second) |
| 4959 | Priority += CCD_InBaseClass; |
| 4960 | |
| 4961 | Results.AddResult(Result(Pattern, Priority, |
Douglas Gregor | 16ed9ad | 2010-08-17 16:06:07 +0000 | [diff] [blame] | 4962 | Method->isInstanceMethod() |
| 4963 | ? CXCursor_ObjCInstanceMethodDecl |
| 4964 | : CXCursor_ObjCClassMethodDecl)); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
| 4967 | Results.ExitScope(); |
| 4968 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4969 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4970 | CodeCompletionContext::CCC_Other, |
| 4971 | Results.data(),Results.size()); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 4972 | } |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 4973 | |
| 4974 | void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S, |
| 4975 | bool IsInstanceMethod, |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 4976 | bool AtParameterName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4977 | ParsedType ReturnTy, |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 4978 | IdentifierInfo **SelIdents, |
| 4979 | unsigned NumSelIdents) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 4980 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 4981 | // pool from the AST file. |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 4982 | if (ExternalSource) { |
| 4983 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 4984 | I != N; ++I) { |
| 4985 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4986 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 4987 | continue; |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4988 | |
| 4989 | ReadMethodPool(Sel); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 4990 | } |
| 4991 | } |
| 4992 | |
| 4993 | // Build the set of methods we can see. |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4994 | typedef CodeCompletionResult Result; |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 4995 | ResultBuilder Results(*this); |
| 4996 | |
| 4997 | if (ReturnTy) |
| 4998 | Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType()); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4999 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5000 | Results.EnterNewScope(); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5001 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 5002 | MEnd = MethodPool.end(); |
| 5003 | M != MEnd; ++M) { |
| 5004 | for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first : |
| 5005 | &M->second.second; |
| 5006 | MethList && MethList->Method; |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5007 | MethList = MethList->Next) { |
| 5008 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 5009 | NumSelIdents)) |
| 5010 | continue; |
| 5011 | |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 5012 | if (AtParameterName) { |
| 5013 | // Suggest parameter names we've seen before. |
| 5014 | if (NumSelIdents && NumSelIdents <= MethList->Method->param_size()) { |
| 5015 | ParmVarDecl *Param = MethList->Method->param_begin()[NumSelIdents-1]; |
| 5016 | if (Param->getIdentifier()) { |
| 5017 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 5018 | Pattern->AddTypedTextChunk(Param->getIdentifier()->getName()); |
| 5019 | Results.AddResult(Pattern); |
| 5020 | } |
| 5021 | } |
| 5022 | |
| 5023 | continue; |
| 5024 | } |
| 5025 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5026 | Result R(MethList->Method, 0); |
| 5027 | R.StartParameter = NumSelIdents; |
| 5028 | R.AllParametersAreInformative = false; |
| 5029 | R.DeclaringEntity = true; |
| 5030 | Results.MaybeAddResult(R, CurContext); |
| 5031 | } |
| 5032 | } |
| 5033 | |
| 5034 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5035 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5036 | CodeCompletionContext::CCC_Other, |
| 5037 | Results.data(),Results.size()); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5038 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 5039 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5040 | void Sema::CodeCompletePreprocessorDirective(bool InConditional) { |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5041 | ResultBuilder Results(*this); |
| 5042 | Results.EnterNewScope(); |
| 5043 | |
| 5044 | // #if <condition> |
| 5045 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 5046 | Pattern->AddTypedTextChunk("if"); |
| 5047 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5048 | Pattern->AddPlaceholderChunk("condition"); |
| 5049 | Results.AddResult(Pattern); |
| 5050 | |
| 5051 | // #ifdef <macro> |
| 5052 | Pattern = new CodeCompletionString; |
| 5053 | Pattern->AddTypedTextChunk("ifdef"); |
| 5054 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5055 | Pattern->AddPlaceholderChunk("macro"); |
| 5056 | Results.AddResult(Pattern); |
| 5057 | |
| 5058 | // #ifndef <macro> |
| 5059 | Pattern = new CodeCompletionString; |
| 5060 | Pattern->AddTypedTextChunk("ifndef"); |
| 5061 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5062 | Pattern->AddPlaceholderChunk("macro"); |
| 5063 | Results.AddResult(Pattern); |
| 5064 | |
| 5065 | if (InConditional) { |
| 5066 | // #elif <condition> |
| 5067 | Pattern = new CodeCompletionString; |
| 5068 | Pattern->AddTypedTextChunk("elif"); |
| 5069 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5070 | Pattern->AddPlaceholderChunk("condition"); |
| 5071 | Results.AddResult(Pattern); |
| 5072 | |
| 5073 | // #else |
| 5074 | Pattern = new CodeCompletionString; |
| 5075 | Pattern->AddTypedTextChunk("else"); |
| 5076 | Results.AddResult(Pattern); |
| 5077 | |
| 5078 | // #endif |
| 5079 | Pattern = new CodeCompletionString; |
| 5080 | Pattern->AddTypedTextChunk("endif"); |
| 5081 | Results.AddResult(Pattern); |
| 5082 | } |
| 5083 | |
| 5084 | // #include "header" |
| 5085 | Pattern = new CodeCompletionString; |
| 5086 | Pattern->AddTypedTextChunk("include"); |
| 5087 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5088 | Pattern->AddTextChunk("\""); |
| 5089 | Pattern->AddPlaceholderChunk("header"); |
| 5090 | Pattern->AddTextChunk("\""); |
| 5091 | Results.AddResult(Pattern); |
| 5092 | |
| 5093 | // #include <header> |
| 5094 | Pattern = new CodeCompletionString; |
| 5095 | Pattern->AddTypedTextChunk("include"); |
| 5096 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5097 | Pattern->AddTextChunk("<"); |
| 5098 | Pattern->AddPlaceholderChunk("header"); |
| 5099 | Pattern->AddTextChunk(">"); |
| 5100 | Results.AddResult(Pattern); |
| 5101 | |
| 5102 | // #define <macro> |
| 5103 | Pattern = new CodeCompletionString; |
| 5104 | Pattern->AddTypedTextChunk("define"); |
| 5105 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5106 | Pattern->AddPlaceholderChunk("macro"); |
| 5107 | Results.AddResult(Pattern); |
| 5108 | |
| 5109 | // #define <macro>(<args>) |
| 5110 | Pattern = new CodeCompletionString; |
| 5111 | Pattern->AddTypedTextChunk("define"); |
| 5112 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5113 | Pattern->AddPlaceholderChunk("macro"); |
| 5114 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 5115 | Pattern->AddPlaceholderChunk("args"); |
| 5116 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 5117 | Results.AddResult(Pattern); |
| 5118 | |
| 5119 | // #undef <macro> |
| 5120 | Pattern = new CodeCompletionString; |
| 5121 | Pattern->AddTypedTextChunk("undef"); |
| 5122 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5123 | Pattern->AddPlaceholderChunk("macro"); |
| 5124 | Results.AddResult(Pattern); |
| 5125 | |
| 5126 | // #line <number> |
| 5127 | Pattern = new CodeCompletionString; |
| 5128 | Pattern->AddTypedTextChunk("line"); |
| 5129 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5130 | Pattern->AddPlaceholderChunk("number"); |
| 5131 | Results.AddResult(Pattern); |
| 5132 | |
| 5133 | // #line <number> "filename" |
| 5134 | Pattern = new CodeCompletionString; |
| 5135 | Pattern->AddTypedTextChunk("line"); |
| 5136 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5137 | Pattern->AddPlaceholderChunk("number"); |
| 5138 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5139 | Pattern->AddTextChunk("\""); |
| 5140 | Pattern->AddPlaceholderChunk("filename"); |
| 5141 | Pattern->AddTextChunk("\""); |
| 5142 | Results.AddResult(Pattern); |
| 5143 | |
| 5144 | // #error <message> |
| 5145 | Pattern = new CodeCompletionString; |
| 5146 | Pattern->AddTypedTextChunk("error"); |
| 5147 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5148 | Pattern->AddPlaceholderChunk("message"); |
| 5149 | Results.AddResult(Pattern); |
| 5150 | |
| 5151 | // #pragma <arguments> |
| 5152 | Pattern = new CodeCompletionString; |
| 5153 | Pattern->AddTypedTextChunk("pragma"); |
| 5154 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5155 | Pattern->AddPlaceholderChunk("arguments"); |
| 5156 | Results.AddResult(Pattern); |
| 5157 | |
| 5158 | if (getLangOptions().ObjC1) { |
| 5159 | // #import "header" |
| 5160 | Pattern = new CodeCompletionString; |
| 5161 | Pattern->AddTypedTextChunk("import"); |
| 5162 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5163 | Pattern->AddTextChunk("\""); |
| 5164 | Pattern->AddPlaceholderChunk("header"); |
| 5165 | Pattern->AddTextChunk("\""); |
| 5166 | Results.AddResult(Pattern); |
| 5167 | |
| 5168 | // #import <header> |
| 5169 | Pattern = new CodeCompletionString; |
| 5170 | Pattern->AddTypedTextChunk("import"); |
| 5171 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5172 | Pattern->AddTextChunk("<"); |
| 5173 | Pattern->AddPlaceholderChunk("header"); |
| 5174 | Pattern->AddTextChunk(">"); |
| 5175 | Results.AddResult(Pattern); |
| 5176 | } |
| 5177 | |
| 5178 | // #include_next "header" |
| 5179 | Pattern = new CodeCompletionString; |
| 5180 | Pattern->AddTypedTextChunk("include_next"); |
| 5181 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5182 | Pattern->AddTextChunk("\""); |
| 5183 | Pattern->AddPlaceholderChunk("header"); |
| 5184 | Pattern->AddTextChunk("\""); |
| 5185 | Results.AddResult(Pattern); |
| 5186 | |
| 5187 | // #include_next <header> |
| 5188 | Pattern = new CodeCompletionString; |
| 5189 | Pattern->AddTypedTextChunk("include_next"); |
| 5190 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5191 | Pattern->AddTextChunk("<"); |
| 5192 | Pattern->AddPlaceholderChunk("header"); |
| 5193 | Pattern->AddTextChunk(">"); |
| 5194 | Results.AddResult(Pattern); |
| 5195 | |
| 5196 | // #warning <message> |
| 5197 | Pattern = new CodeCompletionString; |
| 5198 | Pattern->AddTypedTextChunk("warning"); |
| 5199 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5200 | Pattern->AddPlaceholderChunk("message"); |
| 5201 | Results.AddResult(Pattern); |
| 5202 | |
| 5203 | // Note: #ident and #sccs are such crazy anachronisms that we don't provide |
| 5204 | // completions for them. And __include_macros is a Clang-internal extension |
| 5205 | // that we don't want to encourage anyone to use. |
| 5206 | |
| 5207 | // FIXME: we don't support #assert or #unassert, so don't suggest them. |
| 5208 | Results.ExitScope(); |
| 5209 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5210 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 5211 | CodeCompletionContext::CCC_PreprocessorDirective, |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5212 | Results.data(), Results.size()); |
| 5213 | } |
| 5214 | |
| 5215 | void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) { |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5216 | CodeCompleteOrdinaryName(S, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5217 | S->getFnParent()? Sema::PCC_RecoveryInFunction |
| 5218 | : Sema::PCC_Namespace); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5221 | void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) { |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 5222 | ResultBuilder Results(*this); |
| 5223 | if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) { |
| 5224 | // Add just the names of macros, not their arguments. |
| 5225 | Results.EnterNewScope(); |
| 5226 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 5227 | MEnd = PP.macro_end(); |
| 5228 | M != MEnd; ++M) { |
| 5229 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 5230 | Pattern->AddTypedTextChunk(M->first->getName()); |
| 5231 | Results.AddResult(Pattern); |
| 5232 | } |
| 5233 | Results.ExitScope(); |
| 5234 | } else if (IsDefinition) { |
| 5235 | // FIXME: Can we detect when the user just wrote an include guard above? |
| 5236 | } |
| 5237 | |
| 5238 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5239 | IsDefinition? CodeCompletionContext::CCC_MacroName |
| 5240 | : CodeCompletionContext::CCC_MacroNameUse, |
| 5241 | Results.data(), Results.size()); |
| 5242 | } |
| 5243 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5244 | void Sema::CodeCompletePreprocessorExpression() { |
| 5245 | ResultBuilder Results(*this); |
| 5246 | |
| 5247 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
| 5248 | AddMacroResults(PP, Results); |
| 5249 | |
| 5250 | // defined (<macro>) |
| 5251 | Results.EnterNewScope(); |
| 5252 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 5253 | Pattern->AddTypedTextChunk("defined"); |
| 5254 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5255 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 5256 | Pattern->AddPlaceholderChunk("macro"); |
| 5257 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 5258 | Results.AddResult(Pattern); |
| 5259 | Results.ExitScope(); |
| 5260 | |
| 5261 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5262 | CodeCompletionContext::CCC_PreprocessorExpression, |
| 5263 | Results.data(), Results.size()); |
| 5264 | } |
| 5265 | |
| 5266 | void Sema::CodeCompletePreprocessorMacroArgument(Scope *S, |
| 5267 | IdentifierInfo *Macro, |
| 5268 | MacroInfo *MacroInfo, |
| 5269 | unsigned Argument) { |
| 5270 | // FIXME: In the future, we could provide "overload" results, much like we |
| 5271 | // do for function calls. |
| 5272 | |
| 5273 | CodeCompleteOrdinaryName(S, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5274 | S->getFnParent()? Sema::PCC_RecoveryInFunction |
| 5275 | : Sema::PCC_Namespace); |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5276 | } |
| 5277 | |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 5278 | void Sema::CodeCompleteNaturalLanguage() { |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 5279 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | af1c6b5 | 2010-08-25 17:10:00 +0000 | [diff] [blame] | 5280 | CodeCompletionContext::CCC_NaturalLanguage, |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 5281 | 0, 0); |
| 5282 | } |
| 5283 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 5284 | void Sema::GatherGlobalCodeCompletions( |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5285 | llvm::SmallVectorImpl<CodeCompletionResult> &Results) { |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 5286 | ResultBuilder Builder(*this); |
| 5287 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5288 | if (!CodeCompleter || CodeCompleter->includeGlobals()) { |
| 5289 | CodeCompletionDeclConsumer Consumer(Builder, |
| 5290 | Context.getTranslationUnitDecl()); |
| 5291 | LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName, |
| 5292 | Consumer); |
| 5293 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 5294 | |
| 5295 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
| 5296 | AddMacroResults(PP, Builder); |
| 5297 | |
| 5298 | Results.clear(); |
| 5299 | Results.insert(Results.end(), |
| 5300 | Builder.data(), Builder.data() + Builder.size()); |
| 5301 | } |