Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1 | //===---------------- SemaCodeComplete.cpp - Code Completion ----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the code-completion semantic actions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 13 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Lookup.h" |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Overload.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 16 | #include "clang/Sema/CodeCompleteConsumer.h" |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ExternalSemaSource.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Scope.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ScopeInfo.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 23 | #include "clang/Lex/MacroInfo.h" |
| 24 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 6a68403 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringSwitch.h" |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 30 | #include <list> |
| 31 | #include <map> |
| 32 | #include <vector> |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 35 | using namespace sema; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 36 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 37 | namespace { |
| 38 | /// \brief A container of code-completion results. |
| 39 | class ResultBuilder { |
| 40 | public: |
| 41 | /// \brief The type of a name-lookup filter, which can be provided to the |
| 42 | /// name-lookup routines to specify which declarations should be included in |
| 43 | /// the result set (when it returns true) and which declarations should be |
| 44 | /// filtered out (returns false). |
| 45 | typedef bool (ResultBuilder::*LookupFilter)(NamedDecl *) const; |
| 46 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 47 | typedef CodeCompletionResult Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 48 | |
| 49 | private: |
| 50 | /// \brief The actual results we have found. |
| 51 | std::vector<Result> Results; |
| 52 | |
| 53 | /// \brief A record of all of the declarations we have found and placed |
| 54 | /// into the result set, used to ensure that no declaration ever gets into |
| 55 | /// the result set twice. |
| 56 | llvm::SmallPtrSet<Decl*, 16> AllDeclsFound; |
| 57 | |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 58 | typedef std::pair<NamedDecl *, unsigned> DeclIndexPair; |
| 59 | |
| 60 | /// \brief An entry in the shadow map, which is optimized to store |
| 61 | /// a single (declaration, index) mapping (the common case) but |
| 62 | /// can also store a list of (declaration, index) mappings. |
| 63 | class ShadowMapEntry { |
| 64 | typedef llvm::SmallVector<DeclIndexPair, 4> DeclIndexPairVector; |
| 65 | |
| 66 | /// \brief Contains either the solitary NamedDecl * or a vector |
| 67 | /// of (declaration, index) pairs. |
| 68 | llvm::PointerUnion<NamedDecl *, DeclIndexPairVector*> DeclOrVector; |
| 69 | |
| 70 | /// \brief When the entry contains a single declaration, this is |
| 71 | /// the index associated with that entry. |
| 72 | unsigned SingleDeclIndex; |
| 73 | |
| 74 | public: |
| 75 | ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { } |
| 76 | |
| 77 | void Add(NamedDecl *ND, unsigned Index) { |
| 78 | if (DeclOrVector.isNull()) { |
| 79 | // 0 - > 1 elements: just set the single element information. |
| 80 | DeclOrVector = ND; |
| 81 | SingleDeclIndex = Index; |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | if (NamedDecl *PrevND = DeclOrVector.dyn_cast<NamedDecl *>()) { |
| 86 | // 1 -> 2 elements: create the vector of results and push in the |
| 87 | // existing declaration. |
| 88 | DeclIndexPairVector *Vec = new DeclIndexPairVector; |
| 89 | Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex)); |
| 90 | DeclOrVector = Vec; |
| 91 | } |
| 92 | |
| 93 | // Add the new element to the end of the vector. |
| 94 | DeclOrVector.get<DeclIndexPairVector*>()->push_back( |
| 95 | DeclIndexPair(ND, Index)); |
| 96 | } |
| 97 | |
| 98 | void Destroy() { |
| 99 | if (DeclIndexPairVector *Vec |
| 100 | = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) { |
| 101 | delete Vec; |
| 102 | DeclOrVector = ((NamedDecl *)0); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Iteration. |
| 107 | class iterator; |
| 108 | iterator begin() const; |
| 109 | iterator end() const; |
| 110 | }; |
| 111 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 112 | /// \brief A mapping from declaration names to the declarations that have |
| 113 | /// this name within a particular scope and their index within the list of |
| 114 | /// results. |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 115 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 116 | |
| 117 | /// \brief The semantic analysis object for which results are being |
| 118 | /// produced. |
| 119 | Sema &SemaRef; |
| 120 | |
| 121 | /// \brief If non-NULL, a filter function used to remove any code-completion |
| 122 | /// results that are not desirable. |
| 123 | LookupFilter Filter; |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 124 | |
| 125 | /// \brief Whether we should allow declarations as |
| 126 | /// nested-name-specifiers that would otherwise be filtered out. |
| 127 | bool AllowNestedNameSpecifiers; |
| 128 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 129 | /// \brief If set, the type that we would prefer our resulting value |
| 130 | /// declarations to have. |
| 131 | /// |
| 132 | /// Closely matching the preferred type gives a boost to a result's |
| 133 | /// priority. |
| 134 | CanQualType PreferredType; |
| 135 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 136 | /// \brief A list of shadow maps, which is used to model name hiding at |
| 137 | /// different levels of, e.g., the inheritance hierarchy. |
| 138 | std::list<ShadowMap> ShadowMaps; |
| 139 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 140 | /// \brief If we're potentially referring to a C++ member function, the set |
| 141 | /// of qualifiers applied to the object type. |
| 142 | Qualifiers ObjectTypeQualifiers; |
| 143 | |
| 144 | /// \brief Whether the \p ObjectTypeQualifiers field is active. |
| 145 | bool HasObjectTypeQualifiers; |
| 146 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 147 | /// \brief The selector that we prefer. |
| 148 | Selector PreferredSelector; |
| 149 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 150 | /// \brief The completion context in which |
| 151 | CodeCompletionContext CompletionContext; |
| 152 | |
| 153 | void AdjustResultPriorityForDecl(Result &R); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 154 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 155 | void MaybeAddConstructorResults(Result R); |
| 156 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 157 | public: |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 158 | explicit ResultBuilder(Sema &SemaRef, |
| 159 | const CodeCompletionContext &CompletionContext, |
| 160 | LookupFilter Filter = 0) |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 161 | : SemaRef(SemaRef), Filter(Filter), AllowNestedNameSpecifiers(false), |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 162 | HasObjectTypeQualifiers(false), |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 163 | CompletionContext(CompletionContext) { } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 164 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 165 | /// \brief Whether we should include code patterns in the completion |
| 166 | /// results. |
| 167 | bool includeCodePatterns() const { |
| 168 | return SemaRef.CodeCompleter && |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 169 | SemaRef.CodeCompleter->includeCodePatterns(); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 172 | /// \brief Set the filter used for code-completion results. |
| 173 | void setFilter(LookupFilter Filter) { |
| 174 | this->Filter = Filter; |
| 175 | } |
| 176 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 177 | Result *data() { return Results.empty()? 0 : &Results.front(); } |
| 178 | unsigned size() const { return Results.size(); } |
| 179 | bool empty() const { return Results.empty(); } |
| 180 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 181 | /// \brief Specify the preferred type. |
| 182 | void setPreferredType(QualType T) { |
| 183 | PreferredType = SemaRef.Context.getCanonicalType(T); |
| 184 | } |
| 185 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 186 | /// \brief Set the cv-qualifiers on the object type, for us in filtering |
| 187 | /// calls to member functions. |
| 188 | /// |
| 189 | /// When there are qualifiers in this set, they will be used to filter |
| 190 | /// out member functions that aren't available (because there will be a |
| 191 | /// cv-qualifier mismatch) or prefer functions with an exact qualifier |
| 192 | /// match. |
| 193 | void setObjectTypeQualifiers(Qualifiers Quals) { |
| 194 | ObjectTypeQualifiers = Quals; |
| 195 | HasObjectTypeQualifiers = true; |
| 196 | } |
| 197 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 198 | /// \brief Set the preferred selector. |
| 199 | /// |
| 200 | /// When an Objective-C method declaration result is added, and that |
| 201 | /// method's selector matches this preferred selector, we give that method |
| 202 | /// a slight priority boost. |
| 203 | void setPreferredSelector(Selector Sel) { |
| 204 | PreferredSelector = Sel; |
| 205 | } |
| 206 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 207 | /// \brief Retrieve the code-completion context for which results are |
| 208 | /// being collected. |
| 209 | const CodeCompletionContext &getCompletionContext() const { |
| 210 | return CompletionContext; |
| 211 | } |
| 212 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 213 | /// \brief Specify whether nested-name-specifiers are allowed. |
| 214 | void allowNestedNameSpecifiers(bool Allow = true) { |
| 215 | AllowNestedNameSpecifiers = Allow; |
| 216 | } |
| 217 | |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 218 | /// \brief Return the semantic analysis object for which we are collecting |
| 219 | /// code completion results. |
| 220 | Sema &getSema() const { return SemaRef; } |
| 221 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 222 | /// \brief Determine whether the given declaration is at all interesting |
| 223 | /// as a code-completion result. |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 224 | /// |
| 225 | /// \param ND the declaration that we are inspecting. |
| 226 | /// |
| 227 | /// \param AsNestedNameSpecifier will be set true if this declaration is |
| 228 | /// only interesting when it is a nested-name-specifier. |
| 229 | bool isInterestingDecl(NamedDecl *ND, bool &AsNestedNameSpecifier) const; |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 230 | |
| 231 | /// \brief Check whether the result is hidden by the Hiding declaration. |
| 232 | /// |
| 233 | /// \returns true if the result is hidden and cannot be found, false if |
| 234 | /// the hidden result could still be found. When false, \p R may be |
| 235 | /// modified to describe how the result can be found (e.g., via extra |
| 236 | /// qualification). |
| 237 | bool CheckHiddenResult(Result &R, DeclContext *CurContext, |
| 238 | NamedDecl *Hiding); |
| 239 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 240 | /// \brief Add a new result to this result set (if it isn't already in one |
| 241 | /// of the shadow maps), or replace an existing result (for, e.g., a |
| 242 | /// redeclaration). |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 243 | /// |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 244 | /// \param CurContext the result to add (if it is unique). |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 245 | /// |
| 246 | /// \param R the context in which this result will be named. |
| 247 | void MaybeAddResult(Result R, DeclContext *CurContext = 0); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 248 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 249 | /// \brief Add a new result to this result set, where we already know |
| 250 | /// the hiding declation (if any). |
| 251 | /// |
| 252 | /// \param R the result to add (if it is unique). |
| 253 | /// |
| 254 | /// \param CurContext the context in which this result will be named. |
| 255 | /// |
| 256 | /// \param Hiding the declaration that hides the result. |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 257 | /// |
| 258 | /// \param InBaseClass whether the result was found in a base |
| 259 | /// class of the searched context. |
| 260 | void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding, |
| 261 | bool InBaseClass); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 262 | |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 263 | /// \brief Add a new non-declaration result to this result set. |
| 264 | void AddResult(Result R); |
| 265 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 266 | /// \brief Enter into a new scope. |
| 267 | void EnterNewScope(); |
| 268 | |
| 269 | /// \brief Exit from the current scope. |
| 270 | void ExitScope(); |
| 271 | |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 272 | /// \brief Ignore this declaration, if it is seen again. |
| 273 | void Ignore(Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); } |
| 274 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 275 | /// \name Name lookup predicates |
| 276 | /// |
| 277 | /// These predicates can be passed to the name lookup functions to filter the |
| 278 | /// results of name lookup. All of the predicates have the same type, so that |
| 279 | /// |
| 280 | //@{ |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 281 | bool IsOrdinaryName(NamedDecl *ND) const; |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 282 | bool IsOrdinaryNonTypeName(NamedDecl *ND) const; |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 283 | bool IsIntegralConstantValue(NamedDecl *ND) const; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 284 | bool IsOrdinaryNonValueName(NamedDecl *ND) const; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 285 | bool IsNestedNameSpecifier(NamedDecl *ND) const; |
| 286 | bool IsEnum(NamedDecl *ND) const; |
| 287 | bool IsClassOrStruct(NamedDecl *ND) const; |
| 288 | bool IsUnion(NamedDecl *ND) const; |
| 289 | bool IsNamespace(NamedDecl *ND) const; |
| 290 | bool IsNamespaceOrAlias(NamedDecl *ND) const; |
| 291 | bool IsType(NamedDecl *ND) const; |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 292 | bool IsMember(NamedDecl *ND) const; |
Douglas Gregor | 80f4f4c | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 293 | bool IsObjCIvar(NamedDecl *ND) const; |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 294 | bool IsObjCMessageReceiver(NamedDecl *ND) const; |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 295 | bool IsObjCCollection(NamedDecl *ND) const; |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 296 | bool IsImpossibleToSatisfy(NamedDecl *ND) const; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 297 | //@} |
| 298 | }; |
| 299 | } |
| 300 | |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 301 | class ResultBuilder::ShadowMapEntry::iterator { |
| 302 | llvm::PointerUnion<NamedDecl*, const DeclIndexPair*> DeclOrIterator; |
| 303 | unsigned SingleDeclIndex; |
| 304 | |
| 305 | public: |
| 306 | typedef DeclIndexPair value_type; |
| 307 | typedef value_type reference; |
| 308 | typedef std::ptrdiff_t difference_type; |
| 309 | typedef std::input_iterator_tag iterator_category; |
| 310 | |
| 311 | class pointer { |
| 312 | DeclIndexPair Value; |
| 313 | |
| 314 | public: |
| 315 | pointer(const DeclIndexPair &Value) : Value(Value) { } |
| 316 | |
| 317 | const DeclIndexPair *operator->() const { |
| 318 | return &Value; |
| 319 | } |
| 320 | }; |
| 321 | |
| 322 | iterator() : DeclOrIterator((NamedDecl *)0), SingleDeclIndex(0) { } |
| 323 | |
| 324 | iterator(NamedDecl *SingleDecl, unsigned Index) |
| 325 | : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { } |
| 326 | |
| 327 | iterator(const DeclIndexPair *Iterator) |
| 328 | : DeclOrIterator(Iterator), SingleDeclIndex(0) { } |
| 329 | |
| 330 | iterator &operator++() { |
| 331 | if (DeclOrIterator.is<NamedDecl *>()) { |
| 332 | DeclOrIterator = (NamedDecl *)0; |
| 333 | SingleDeclIndex = 0; |
| 334 | return *this; |
| 335 | } |
| 336 | |
| 337 | const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>(); |
| 338 | ++I; |
| 339 | DeclOrIterator = I; |
| 340 | return *this; |
| 341 | } |
| 342 | |
Chris Lattner | 66392d4 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 343 | /*iterator operator++(int) { |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 344 | iterator tmp(*this); |
| 345 | ++(*this); |
| 346 | return tmp; |
Chris Lattner | 66392d4 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 347 | }*/ |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 348 | |
| 349 | reference operator*() const { |
| 350 | if (NamedDecl *ND = DeclOrIterator.dyn_cast<NamedDecl *>()) |
| 351 | return reference(ND, SingleDeclIndex); |
| 352 | |
Douglas Gregor | d490f95 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 353 | return *DeclOrIterator.get<const DeclIndexPair*>(); |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | pointer operator->() const { |
| 357 | return pointer(**this); |
| 358 | } |
| 359 | |
| 360 | friend bool operator==(const iterator &X, const iterator &Y) { |
Douglas Gregor | d490f95 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 361 | return X.DeclOrIterator.getOpaqueValue() |
| 362 | == Y.DeclOrIterator.getOpaqueValue() && |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 363 | X.SingleDeclIndex == Y.SingleDeclIndex; |
| 364 | } |
| 365 | |
| 366 | friend bool operator!=(const iterator &X, const iterator &Y) { |
Douglas Gregor | d490f95 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 367 | return !(X == Y); |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 368 | } |
| 369 | }; |
| 370 | |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 371 | ResultBuilder::ShadowMapEntry::iterator |
| 372 | ResultBuilder::ShadowMapEntry::begin() const { |
| 373 | if (DeclOrVector.isNull()) |
| 374 | return iterator(); |
| 375 | |
| 376 | if (NamedDecl *ND = DeclOrVector.dyn_cast<NamedDecl *>()) |
| 377 | return iterator(ND, SingleDeclIndex); |
| 378 | |
| 379 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin()); |
| 380 | } |
| 381 | |
| 382 | ResultBuilder::ShadowMapEntry::iterator |
| 383 | ResultBuilder::ShadowMapEntry::end() const { |
| 384 | if (DeclOrVector.is<NamedDecl *>() || DeclOrVector.isNull()) |
| 385 | return iterator(); |
| 386 | |
| 387 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end()); |
| 388 | } |
| 389 | |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 390 | /// \brief Compute the qualification required to get from the current context |
| 391 | /// (\p CurContext) to the target context (\p TargetContext). |
| 392 | /// |
| 393 | /// \param Context the AST context in which the qualification will be used. |
| 394 | /// |
| 395 | /// \param CurContext the context where an entity is being named, which is |
| 396 | /// typically based on the current scope. |
| 397 | /// |
| 398 | /// \param TargetContext the context in which the named entity actually |
| 399 | /// resides. |
| 400 | /// |
| 401 | /// \returns a nested name specifier that refers into the target context, or |
| 402 | /// NULL if no qualification is needed. |
| 403 | static NestedNameSpecifier * |
| 404 | getRequiredQualification(ASTContext &Context, |
| 405 | DeclContext *CurContext, |
| 406 | DeclContext *TargetContext) { |
| 407 | llvm::SmallVector<DeclContext *, 4> TargetParents; |
| 408 | |
| 409 | for (DeclContext *CommonAncestor = TargetContext; |
| 410 | CommonAncestor && !CommonAncestor->Encloses(CurContext); |
| 411 | CommonAncestor = CommonAncestor->getLookupParent()) { |
| 412 | if (CommonAncestor->isTransparentContext() || |
| 413 | CommonAncestor->isFunctionOrMethod()) |
| 414 | continue; |
| 415 | |
| 416 | TargetParents.push_back(CommonAncestor); |
| 417 | } |
| 418 | |
| 419 | NestedNameSpecifier *Result = 0; |
| 420 | while (!TargetParents.empty()) { |
| 421 | DeclContext *Parent = TargetParents.back(); |
| 422 | TargetParents.pop_back(); |
| 423 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 424 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) { |
| 425 | if (!Namespace->getIdentifier()) |
| 426 | continue; |
| 427 | |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 428 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 429 | } |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 430 | else if (TagDecl *TD = dyn_cast<TagDecl>(Parent)) |
| 431 | Result = NestedNameSpecifier::Create(Context, Result, |
| 432 | false, |
| 433 | Context.getTypeDeclType(TD).getTypePtr()); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 434 | } |
Douglas Gregor | 456c4a1 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 435 | return Result; |
| 436 | } |
| 437 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 438 | bool ResultBuilder::isInterestingDecl(NamedDecl *ND, |
| 439 | bool &AsNestedNameSpecifier) const { |
| 440 | AsNestedNameSpecifier = false; |
| 441 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 442 | ND = ND->getUnderlyingDecl(); |
| 443 | unsigned IDNS = ND->getIdentifierNamespace(); |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 444 | |
| 445 | // Skip unnamed entities. |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 446 | if (!ND->getDeclName()) |
| 447 | return false; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 448 | |
| 449 | // Friend declarations and declarations introduced due to friends are never |
| 450 | // added as results. |
John McCall | 92b7f70 | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 451 | if (IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend)) |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 452 | return false; |
| 453 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 454 | // Class template (partial) specializations are never added as results. |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 455 | if (isa<ClassTemplateSpecializationDecl>(ND) || |
| 456 | isa<ClassTemplatePartialSpecializationDecl>(ND)) |
| 457 | return false; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 458 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 459 | // Using declarations themselves are never added as results. |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 460 | if (isa<UsingDecl>(ND)) |
| 461 | return false; |
| 462 | |
| 463 | // Some declarations have reserved names that we don't want to ever show. |
| 464 | if (const IdentifierInfo *Id = ND->getIdentifier()) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 465 | // __va_list_tag is a freak of nature. Find it and skip it. |
| 466 | if (Id->isStr("__va_list_tag") || Id->isStr("__builtin_va_list")) |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 467 | return false; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 468 | |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 469 | // Filter out names reserved for the implementation (C99 7.1.3, |
Douglas Gregor | 797efb5 | 2010-07-14 17:44:04 +0000 | [diff] [blame] | 470 | // C++ [lib.global.names]) if they come from a system header. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 471 | // |
| 472 | // FIXME: Add predicate for this. |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 473 | if (Id->getLength() >= 2) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 474 | const char *Name = Id->getNameStart(); |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 475 | if (Name[0] == '_' && |
Douglas Gregor | 797efb5 | 2010-07-14 17:44:04 +0000 | [diff] [blame] | 476 | (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z')) && |
| 477 | (ND->getLocation().isInvalid() || |
| 478 | SemaRef.SourceMgr.isInSystemHeader( |
| 479 | SemaRef.SourceMgr.getSpellingLoc(ND->getLocation())))) |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 480 | return false; |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 481 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 482 | } |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 483 | |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 484 | if (Filter == &ResultBuilder::IsNestedNameSpecifier || |
| 485 | ((isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) && |
| 486 | Filter != &ResultBuilder::IsNamespace && |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 487 | Filter != &ResultBuilder::IsNamespaceOrAlias && |
| 488 | Filter != 0)) |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 489 | AsNestedNameSpecifier = true; |
| 490 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 491 | // Filter out any unwanted results. |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 492 | if (Filter && !(this->*Filter)(ND)) { |
| 493 | // Check whether it is interesting as a nested-name-specifier. |
| 494 | if (AllowNestedNameSpecifiers && SemaRef.getLangOptions().CPlusPlus && |
| 495 | IsNestedNameSpecifier(ND) && |
| 496 | (Filter != &ResultBuilder::IsMember || |
| 497 | (isa<CXXRecordDecl>(ND) && |
| 498 | cast<CXXRecordDecl>(ND)->isInjectedClassName()))) { |
| 499 | AsNestedNameSpecifier = true; |
| 500 | return true; |
| 501 | } |
| 502 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 503 | return false; |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 504 | } |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 505 | // ... then it must be interesting! |
| 506 | return true; |
| 507 | } |
| 508 | |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 509 | bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext, |
| 510 | NamedDecl *Hiding) { |
| 511 | // In C, there is no way to refer to a hidden name. |
| 512 | // FIXME: This isn't true; we can find a tag name hidden by an ordinary |
| 513 | // name if we introduce the tag type. |
| 514 | if (!SemaRef.getLangOptions().CPlusPlus) |
| 515 | return true; |
| 516 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 517 | DeclContext *HiddenCtx = R.Declaration->getDeclContext()->getRedeclContext(); |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 518 | |
| 519 | // There is no way to qualify a name declared in a function or method. |
| 520 | if (HiddenCtx->isFunctionOrMethod()) |
| 521 | return true; |
| 522 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 523 | if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext()) |
Douglas Gregor | 6660d84 | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 524 | return true; |
| 525 | |
| 526 | // We can refer to the result with the appropriate qualification. Do it. |
| 527 | R.Hidden = true; |
| 528 | R.QualifierIsInformative = false; |
| 529 | |
| 530 | if (!R.Qualifier) |
| 531 | R.Qualifier = getRequiredQualification(SemaRef.Context, |
| 532 | CurContext, |
| 533 | R.Declaration->getDeclContext()); |
| 534 | return false; |
| 535 | } |
| 536 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 537 | /// \brief A simplified classification of types used to determine whether two |
| 538 | /// types are "similar enough" when adjusting priorities. |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 539 | SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 540 | switch (T->getTypeClass()) { |
| 541 | case Type::Builtin: |
| 542 | switch (cast<BuiltinType>(T)->getKind()) { |
| 543 | case BuiltinType::Void: |
| 544 | return STC_Void; |
| 545 | |
| 546 | case BuiltinType::NullPtr: |
| 547 | return STC_Pointer; |
| 548 | |
| 549 | case BuiltinType::Overload: |
| 550 | case BuiltinType::Dependent: |
| 551 | case BuiltinType::UndeducedAuto: |
| 552 | return STC_Other; |
| 553 | |
| 554 | case BuiltinType::ObjCId: |
| 555 | case BuiltinType::ObjCClass: |
| 556 | case BuiltinType::ObjCSel: |
| 557 | return STC_ObjectiveC; |
| 558 | |
| 559 | default: |
| 560 | return STC_Arithmetic; |
| 561 | } |
| 562 | return STC_Other; |
| 563 | |
| 564 | case Type::Complex: |
| 565 | return STC_Arithmetic; |
| 566 | |
| 567 | case Type::Pointer: |
| 568 | return STC_Pointer; |
| 569 | |
| 570 | case Type::BlockPointer: |
| 571 | return STC_Block; |
| 572 | |
| 573 | case Type::LValueReference: |
| 574 | case Type::RValueReference: |
| 575 | return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType()); |
| 576 | |
| 577 | case Type::ConstantArray: |
| 578 | case Type::IncompleteArray: |
| 579 | case Type::VariableArray: |
| 580 | case Type::DependentSizedArray: |
| 581 | return STC_Array; |
| 582 | |
| 583 | case Type::DependentSizedExtVector: |
| 584 | case Type::Vector: |
| 585 | case Type::ExtVector: |
| 586 | return STC_Arithmetic; |
| 587 | |
| 588 | case Type::FunctionProto: |
| 589 | case Type::FunctionNoProto: |
| 590 | return STC_Function; |
| 591 | |
| 592 | case Type::Record: |
| 593 | return STC_Record; |
| 594 | |
| 595 | case Type::Enum: |
| 596 | return STC_Arithmetic; |
| 597 | |
| 598 | case Type::ObjCObject: |
| 599 | case Type::ObjCInterface: |
| 600 | case Type::ObjCObjectPointer: |
| 601 | return STC_ObjectiveC; |
| 602 | |
| 603 | default: |
| 604 | return STC_Other; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | /// \brief Get the type that a given expression will have if this declaration |
| 609 | /// is used as an expression in its "typical" code-completion form. |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 610 | QualType clang::getDeclUsageType(ASTContext &C, NamedDecl *ND) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 611 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 612 | |
| 613 | if (TypeDecl *Type = dyn_cast<TypeDecl>(ND)) |
| 614 | return C.getTypeDeclType(Type); |
| 615 | if (ObjCInterfaceDecl *Iface = dyn_cast<ObjCInterfaceDecl>(ND)) |
| 616 | return C.getObjCInterfaceType(Iface); |
| 617 | |
| 618 | QualType T; |
| 619 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 620 | T = Function->getCallResultType(); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 621 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 622 | T = Method->getSendResultType(); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 623 | else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 624 | T = FunTmpl->getTemplatedDecl()->getCallResultType(); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 625 | else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
| 626 | T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext())); |
| 627 | else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
| 628 | T = Property->getType(); |
| 629 | else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND)) |
| 630 | T = Value->getType(); |
| 631 | else |
| 632 | return QualType(); |
| 633 | |
| 634 | return T.getNonReferenceType(); |
| 635 | } |
| 636 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 637 | void ResultBuilder::AdjustResultPriorityForDecl(Result &R) { |
| 638 | // If this is an Objective-C method declaration whose selector matches our |
| 639 | // preferred selector, give it a priority boost. |
| 640 | if (!PreferredSelector.isNull()) |
| 641 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration)) |
| 642 | if (PreferredSelector == Method->getSelector()) |
| 643 | R.Priority += CCD_SelectorMatch; |
Douglas Gregor | 08f43cd | 2010-09-20 23:11:55 +0000 | [diff] [blame] | 644 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 645 | // If we have a preferred type, adjust the priority for results with exactly- |
| 646 | // matching or nearly-matching types. |
| 647 | if (!PreferredType.isNull()) { |
| 648 | QualType T = getDeclUsageType(SemaRef.Context, R.Declaration); |
| 649 | if (!T.isNull()) { |
| 650 | CanQualType TC = SemaRef.Context.getCanonicalType(T); |
| 651 | // Check for exactly-matching types (modulo qualifiers). |
| 652 | if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) |
| 653 | R.Priority /= CCF_ExactTypeMatch; |
| 654 | // Check for nearly-matching types, based on classification of each. |
| 655 | else if ((getSimplifiedTypeClass(PreferredType) |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 656 | == getSimplifiedTypeClass(TC)) && |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 657 | !(PreferredType->isEnumeralType() && TC->isEnumeralType())) |
| 658 | R.Priority /= CCF_SimilarTypeMatch; |
| 659 | } |
| 660 | } |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 663 | void ResultBuilder::MaybeAddConstructorResults(Result R) { |
| 664 | if (!SemaRef.getLangOptions().CPlusPlus || !R.Declaration || |
| 665 | !CompletionContext.wantConstructorResults()) |
| 666 | return; |
| 667 | |
| 668 | ASTContext &Context = SemaRef.Context; |
| 669 | NamedDecl *D = R.Declaration; |
| 670 | CXXRecordDecl *Record = 0; |
| 671 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) |
| 672 | Record = ClassTemplate->getTemplatedDecl(); |
| 673 | else if ((Record = dyn_cast<CXXRecordDecl>(D))) { |
| 674 | // Skip specializations and partial specializations. |
| 675 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 676 | return; |
| 677 | } else { |
| 678 | // There are no constructors here. |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | Record = Record->getDefinition(); |
| 683 | if (!Record) |
| 684 | return; |
| 685 | |
| 686 | |
| 687 | QualType RecordTy = Context.getTypeDeclType(Record); |
| 688 | DeclarationName ConstructorName |
| 689 | = Context.DeclarationNames.getCXXConstructorName( |
| 690 | Context.getCanonicalType(RecordTy)); |
| 691 | for (DeclContext::lookup_result Ctors = Record->lookup(ConstructorName); |
| 692 | Ctors.first != Ctors.second; ++Ctors.first) { |
| 693 | R.Declaration = *Ctors.first; |
| 694 | R.CursorKind = getCursorKindForDecl(R.Declaration); |
| 695 | Results.push_back(R); |
| 696 | } |
| 697 | } |
| 698 | |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 699 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { |
| 700 | assert(!ShadowMaps.empty() && "Must enter into a results scope"); |
| 701 | |
| 702 | if (R.Kind != Result::RK_Declaration) { |
| 703 | // For non-declaration results, just add the result. |
| 704 | Results.push_back(R); |
| 705 | return; |
| 706 | } |
| 707 | |
| 708 | // Look through using declarations. |
| 709 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
| 710 | MaybeAddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext); |
| 711 | return; |
| 712 | } |
| 713 | |
| 714 | Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
| 715 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 716 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 717 | bool AsNestedNameSpecifier = false; |
| 718 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | e495b7f | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 719 | return; |
| 720 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 721 | // C++ constructors are never found by name lookup. |
| 722 | if (isa<CXXConstructorDecl>(R.Declaration)) |
| 723 | return; |
| 724 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 725 | ShadowMap &SMap = ShadowMaps.back(); |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 726 | ShadowMapEntry::iterator I, IEnd; |
| 727 | ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName()); |
| 728 | if (NamePos != SMap.end()) { |
| 729 | I = NamePos->second.begin(); |
| 730 | IEnd = NamePos->second.end(); |
| 731 | } |
| 732 | |
| 733 | for (; I != IEnd; ++I) { |
| 734 | NamedDecl *ND = I->first; |
| 735 | unsigned Index = I->second; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 736 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 737 | // This is a redeclaration. Always pick the newer declaration. |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 738 | Results[Index].Declaration = R.Declaration; |
| 739 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 740 | // We're done. |
| 741 | return; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | // This is a new declaration in this scope. However, check whether this |
| 746 | // declaration name is hidden by a similarly-named declaration in an outer |
| 747 | // scope. |
| 748 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 749 | --SMEnd; |
| 750 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 751 | ShadowMapEntry::iterator I, IEnd; |
| 752 | ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName()); |
| 753 | if (NamePos != SM->end()) { |
| 754 | I = NamePos->second.begin(); |
| 755 | IEnd = NamePos->second.end(); |
| 756 | } |
| 757 | for (; I != IEnd; ++I) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 758 | // A tag declaration does not hide a non-tag declaration. |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 759 | if (I->first->hasTagIdentifierNamespace() && |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 760 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 761 | Decl::IDNS_ObjCProtocol))) |
| 762 | continue; |
| 763 | |
| 764 | // Protocols are in distinct namespaces from everything else. |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 765 | if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 766 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 767 | I->first->getIdentifierNamespace() != IDNS) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 768 | continue; |
| 769 | |
| 770 | // 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] | 771 | if (CheckHiddenResult(R, CurContext, I->first)) |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 772 | return; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 773 | |
| 774 | break; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | // Make sure that any given declaration only shows up in the result set once. |
| 779 | if (!AllDeclsFound.insert(CanonDecl)) |
| 780 | return; |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 781 | |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 782 | // If the filter is for nested-name-specifiers, then this result starts a |
| 783 | // nested-name-specifier. |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 784 | if (AsNestedNameSpecifier) { |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 785 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 786 | R.Priority = CCP_NestedNameSpecifier; |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 787 | } else |
| 788 | AdjustResultPriorityForDecl(R); |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 789 | |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 790 | // If this result is supposed to have an informative qualifier, add one. |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 791 | if (R.QualifierIsInformative && !R.Qualifier && |
| 792 | !R.StartsNestedNameSpecifier) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 793 | DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 794 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 795 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace); |
| 796 | else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
| 797 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false, |
| 798 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
| 799 | else |
| 800 | R.QualifierIsInformative = false; |
| 801 | } |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 802 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 803 | // Insert this result into the set of results and into the current shadow |
| 804 | // map. |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 805 | SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 806 | Results.push_back(R); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 807 | |
| 808 | if (!AsNestedNameSpecifier) |
| 809 | MaybeAddConstructorResults(R); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 812 | void ResultBuilder::AddResult(Result R, DeclContext *CurContext, |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 813 | NamedDecl *Hiding, bool InBaseClass = false) { |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 814 | if (R.Kind != Result::RK_Declaration) { |
| 815 | // For non-declaration results, just add the result. |
| 816 | Results.push_back(R); |
| 817 | return; |
| 818 | } |
| 819 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 820 | // Look through using declarations. |
| 821 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
| 822 | AddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext, Hiding); |
| 823 | return; |
| 824 | } |
| 825 | |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 826 | bool AsNestedNameSpecifier = false; |
| 827 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 828 | return; |
| 829 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 830 | // C++ constructors are never found by name lookup. |
| 831 | if (isa<CXXConstructorDecl>(R.Declaration)) |
| 832 | return; |
| 833 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 834 | if (Hiding && CheckHiddenResult(R, CurContext, Hiding)) |
| 835 | return; |
| 836 | |
| 837 | // Make sure that any given declaration only shows up in the result set once. |
| 838 | if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl())) |
| 839 | return; |
| 840 | |
| 841 | // If the filter is for nested-name-specifiers, then this result starts a |
| 842 | // nested-name-specifier. |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 843 | if (AsNestedNameSpecifier) { |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 844 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 845 | R.Priority = CCP_NestedNameSpecifier; |
| 846 | } |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 847 | else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass && |
| 848 | isa<CXXRecordDecl>(R.Declaration->getDeclContext() |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 849 | ->getRedeclContext())) |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 850 | R.QualifierIsInformative = true; |
| 851 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 852 | // If this result is supposed to have an informative qualifier, add one. |
| 853 | if (R.QualifierIsInformative && !R.Qualifier && |
| 854 | !R.StartsNestedNameSpecifier) { |
| 855 | DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 856 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 857 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace); |
| 858 | else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
| 859 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false, |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 860 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 861 | else |
| 862 | R.QualifierIsInformative = false; |
| 863 | } |
| 864 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 865 | // Adjust the priority if this result comes from a base class. |
| 866 | if (InBaseClass) |
| 867 | R.Priority += CCD_InBaseClass; |
| 868 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 869 | AdjustResultPriorityForDecl(R); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 870 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 871 | if (HasObjectTypeQualifiers) |
| 872 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(R.Declaration)) |
| 873 | if (Method->isInstance()) { |
| 874 | Qualifiers MethodQuals |
| 875 | = Qualifiers::fromCVRMask(Method->getTypeQualifiers()); |
| 876 | if (ObjectTypeQualifiers == MethodQuals) |
| 877 | R.Priority += CCD_ObjectQualifierMatch; |
| 878 | else if (ObjectTypeQualifiers - MethodQuals) { |
| 879 | // The method cannot be invoked, because doing so would drop |
| 880 | // qualifiers. |
| 881 | return; |
| 882 | } |
| 883 | } |
| 884 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 885 | // Insert this result into the set of results. |
| 886 | Results.push_back(R); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 887 | |
| 888 | if (!AsNestedNameSpecifier) |
| 889 | MaybeAddConstructorResults(R); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 892 | void ResultBuilder::AddResult(Result R) { |
| 893 | assert(R.Kind != Result::RK_Declaration && |
| 894 | "Declaration results need more context"); |
| 895 | Results.push_back(R); |
| 896 | } |
| 897 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 898 | /// \brief Enter into a new scope. |
| 899 | void ResultBuilder::EnterNewScope() { |
| 900 | ShadowMaps.push_back(ShadowMap()); |
| 901 | } |
| 902 | |
| 903 | /// \brief Exit from the current scope. |
| 904 | void ResultBuilder::ExitScope() { |
Douglas Gregor | fbcb5d6 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 905 | for (ShadowMap::iterator E = ShadowMaps.back().begin(), |
| 906 | EEnd = ShadowMaps.back().end(); |
| 907 | E != EEnd; |
| 908 | ++E) |
| 909 | E->second.Destroy(); |
| 910 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 911 | ShadowMaps.pop_back(); |
| 912 | } |
| 913 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 914 | /// \brief Determines whether this given declaration will be found by |
| 915 | /// ordinary name lookup. |
| 916 | bool ResultBuilder::IsOrdinaryName(NamedDecl *ND) const { |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 917 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 918 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 919 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 920 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 9b30b26 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 921 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 922 | else if (SemaRef.getLangOptions().ObjC1 && isa<ObjCIvarDecl>(ND)) |
| 923 | return true; |
| 924 | |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 925 | return ND->getIdentifierNamespace() & IDNS; |
| 926 | } |
| 927 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 928 | /// \brief Determines whether this given declaration will be found by |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 929 | /// ordinary name lookup but is not a type name. |
| 930 | bool ResultBuilder::IsOrdinaryNonTypeName(NamedDecl *ND) const { |
| 931 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 932 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) |
| 933 | return false; |
| 934 | |
| 935 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 936 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 9b30b26 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 937 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 938 | else if (SemaRef.getLangOptions().ObjC1 && isa<ObjCIvarDecl>(ND)) |
| 939 | return true; |
| 940 | |
| 941 | return ND->getIdentifierNamespace() & IDNS; |
| 942 | } |
| 943 | |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 944 | bool ResultBuilder::IsIntegralConstantValue(NamedDecl *ND) const { |
| 945 | if (!IsOrdinaryNonTypeName(ND)) |
| 946 | return 0; |
| 947 | |
| 948 | if (ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl())) |
| 949 | if (VD->getType()->isIntegralOrEnumerationType()) |
| 950 | return true; |
| 951 | |
| 952 | return false; |
| 953 | } |
| 954 | |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 955 | /// \brief Determines whether this given declaration will be found by |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 956 | /// ordinary name lookup. |
| 957 | bool ResultBuilder::IsOrdinaryNonValueName(NamedDecl *ND) const { |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 958 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 959 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 960 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 961 | if (SemaRef.getLangOptions().CPlusPlus) |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 962 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 963 | |
| 964 | return (ND->getIdentifierNamespace() & IDNS) && |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 965 | !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) && |
| 966 | !isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 969 | /// \brief Determines whether the given declaration is suitable as the |
| 970 | /// start of a C++ nested-name-specifier, e.g., a class or namespace. |
| 971 | bool ResultBuilder::IsNestedNameSpecifier(NamedDecl *ND) const { |
| 972 | // Allow us to find class templates, too. |
| 973 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 974 | ND = ClassTemplate->getTemplatedDecl(); |
| 975 | |
| 976 | return SemaRef.isAcceptableNestedNameSpecifier(ND); |
| 977 | } |
| 978 | |
| 979 | /// \brief Determines whether the given declaration is an enumeration. |
| 980 | bool ResultBuilder::IsEnum(NamedDecl *ND) const { |
| 981 | return isa<EnumDecl>(ND); |
| 982 | } |
| 983 | |
| 984 | /// \brief Determines whether the given declaration is a class or struct. |
| 985 | bool ResultBuilder::IsClassOrStruct(NamedDecl *ND) const { |
| 986 | // Allow us to find class templates, too. |
| 987 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 988 | ND = ClassTemplate->getTemplatedDecl(); |
| 989 | |
| 990 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 991 | return RD->getTagKind() == TTK_Class || |
| 992 | RD->getTagKind() == TTK_Struct; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 993 | |
| 994 | return false; |
| 995 | } |
| 996 | |
| 997 | /// \brief Determines whether the given declaration is a union. |
| 998 | bool ResultBuilder::IsUnion(NamedDecl *ND) const { |
| 999 | // Allow us to find class templates, too. |
| 1000 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 1001 | ND = ClassTemplate->getTemplatedDecl(); |
| 1002 | |
| 1003 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1004 | return RD->getTagKind() == TTK_Union; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1005 | |
| 1006 | return false; |
| 1007 | } |
| 1008 | |
| 1009 | /// \brief Determines whether the given declaration is a namespace. |
| 1010 | bool ResultBuilder::IsNamespace(NamedDecl *ND) const { |
| 1011 | return isa<NamespaceDecl>(ND); |
| 1012 | } |
| 1013 | |
| 1014 | /// \brief Determines whether the given declaration is a namespace or |
| 1015 | /// namespace alias. |
| 1016 | bool ResultBuilder::IsNamespaceOrAlias(NamedDecl *ND) const { |
| 1017 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
| 1018 | } |
| 1019 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1020 | /// \brief Determines whether the given declaration is a type. |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1021 | bool ResultBuilder::IsType(NamedDecl *ND) const { |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1022 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND)) |
| 1023 | ND = Using->getTargetDecl(); |
| 1024 | |
| 1025 | return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1028 | /// \brief Determines which members of a class should be visible via |
| 1029 | /// "." or "->". Only value declarations, nested name specifiers, and |
| 1030 | /// using declarations thereof should show up. |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1031 | bool ResultBuilder::IsMember(NamedDecl *ND) const { |
Douglas Gregor | 7628294 | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1032 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND)) |
| 1033 | ND = Using->getTargetDecl(); |
| 1034 | |
Douglas Gregor | ce82196 | 2009-12-11 18:14:22 +0000 | [diff] [blame] | 1035 | return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) || |
| 1036 | isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | eb5758b | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1039 | static bool isObjCReceiverType(ASTContext &C, QualType T) { |
| 1040 | T = C.getCanonicalType(T); |
| 1041 | switch (T->getTypeClass()) { |
| 1042 | case Type::ObjCObject: |
| 1043 | case Type::ObjCInterface: |
| 1044 | case Type::ObjCObjectPointer: |
| 1045 | return true; |
| 1046 | |
| 1047 | case Type::Builtin: |
| 1048 | switch (cast<BuiltinType>(T)->getKind()) { |
| 1049 | case BuiltinType::ObjCId: |
| 1050 | case BuiltinType::ObjCClass: |
| 1051 | case BuiltinType::ObjCSel: |
| 1052 | return true; |
| 1053 | |
| 1054 | default: |
| 1055 | break; |
| 1056 | } |
| 1057 | return false; |
| 1058 | |
| 1059 | default: |
| 1060 | break; |
| 1061 | } |
| 1062 | |
| 1063 | if (!C.getLangOptions().CPlusPlus) |
| 1064 | return false; |
| 1065 | |
| 1066 | // FIXME: We could perform more analysis here to determine whether a |
| 1067 | // particular class type has any conversions to Objective-C types. For now, |
| 1068 | // just accept all class types. |
| 1069 | return T->isDependentType() || T->isRecordType(); |
| 1070 | } |
| 1071 | |
| 1072 | bool ResultBuilder::IsObjCMessageReceiver(NamedDecl *ND) const { |
| 1073 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1074 | if (T.isNull()) |
| 1075 | return false; |
| 1076 | |
| 1077 | T = SemaRef.Context.getBaseElementType(T); |
| 1078 | return isObjCReceiverType(SemaRef.Context, T); |
| 1079 | } |
| 1080 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1081 | bool ResultBuilder::IsObjCCollection(NamedDecl *ND) const { |
| 1082 | if ((SemaRef.getLangOptions().CPlusPlus && !IsOrdinaryName(ND)) || |
| 1083 | (!SemaRef.getLangOptions().CPlusPlus && !IsOrdinaryNonTypeName(ND))) |
| 1084 | return false; |
| 1085 | |
| 1086 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1087 | if (T.isNull()) |
| 1088 | return false; |
| 1089 | |
| 1090 | T = SemaRef.Context.getBaseElementType(T); |
| 1091 | return T->isObjCObjectType() || T->isObjCObjectPointerType() || |
| 1092 | T->isObjCIdType() || |
| 1093 | (SemaRef.getLangOptions().CPlusPlus && T->isRecordType()); |
| 1094 | } |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1095 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1096 | bool ResultBuilder::IsImpossibleToSatisfy(NamedDecl *ND) const { |
| 1097 | return false; |
| 1098 | } |
| 1099 | |
Douglas Gregor | 80f4f4c | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1100 | /// \rief Determines whether the given declaration is an Objective-C |
| 1101 | /// instance variable. |
| 1102 | bool ResultBuilder::IsObjCIvar(NamedDecl *ND) const { |
| 1103 | return isa<ObjCIvarDecl>(ND); |
| 1104 | } |
| 1105 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1106 | namespace { |
| 1107 | /// \brief Visible declaration consumer that adds a code-completion result |
| 1108 | /// for each visible declaration. |
| 1109 | class CodeCompletionDeclConsumer : public VisibleDeclConsumer { |
| 1110 | ResultBuilder &Results; |
| 1111 | DeclContext *CurContext; |
| 1112 | |
| 1113 | public: |
| 1114 | CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext) |
| 1115 | : Results(Results), CurContext(CurContext) { } |
| 1116 | |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 1117 | virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, bool InBaseClass) { |
| 1118 | Results.AddResult(ND, CurContext, Hiding, InBaseClass); |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1119 | } |
| 1120 | }; |
| 1121 | } |
| 1122 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1123 | /// \brief Add type specifiers for the current language as keyword results. |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1124 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1125 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1126 | typedef CodeCompletionResult Result; |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1127 | Results.AddResult(Result("short", CCP_Type)); |
| 1128 | Results.AddResult(Result("long", CCP_Type)); |
| 1129 | Results.AddResult(Result("signed", CCP_Type)); |
| 1130 | Results.AddResult(Result("unsigned", CCP_Type)); |
| 1131 | Results.AddResult(Result("void", CCP_Type)); |
| 1132 | Results.AddResult(Result("char", CCP_Type)); |
| 1133 | Results.AddResult(Result("int", CCP_Type)); |
| 1134 | Results.AddResult(Result("float", CCP_Type)); |
| 1135 | Results.AddResult(Result("double", CCP_Type)); |
| 1136 | Results.AddResult(Result("enum", CCP_Type)); |
| 1137 | Results.AddResult(Result("struct", CCP_Type)); |
| 1138 | Results.AddResult(Result("union", CCP_Type)); |
| 1139 | Results.AddResult(Result("const", CCP_Type)); |
| 1140 | Results.AddResult(Result("volatile", CCP_Type)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1141 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1142 | if (LangOpts.C99) { |
| 1143 | // C99-specific |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1144 | Results.AddResult(Result("_Complex", CCP_Type)); |
| 1145 | Results.AddResult(Result("_Imaginary", CCP_Type)); |
| 1146 | Results.AddResult(Result("_Bool", CCP_Type)); |
| 1147 | Results.AddResult(Result("restrict", CCP_Type)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | if (LangOpts.CPlusPlus) { |
| 1151 | // C++-specific |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 1152 | Results.AddResult(Result("bool", CCP_Type + |
| 1153 | (LangOpts.ObjC1? CCD_bool_in_ObjC : 0))); |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1154 | Results.AddResult(Result("class", CCP_Type)); |
| 1155 | Results.AddResult(Result("wchar_t", CCP_Type)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1156 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1157 | // typename qualified-id |
| 1158 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 1159 | Pattern->AddTypedTextChunk("typename"); |
| 1160 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1161 | Pattern->AddPlaceholderChunk("qualifier"); |
| 1162 | Pattern->AddTextChunk("::"); |
| 1163 | Pattern->AddPlaceholderChunk("name"); |
| 1164 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1165 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1166 | if (LangOpts.CPlusPlus0x) { |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1167 | Results.AddResult(Result("auto", CCP_Type)); |
| 1168 | Results.AddResult(Result("char16_t", CCP_Type)); |
| 1169 | Results.AddResult(Result("char32_t", CCP_Type)); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1170 | |
| 1171 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 1172 | Pattern->AddTypedTextChunk("decltype"); |
| 1173 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1174 | Pattern->AddPlaceholderChunk("expression"); |
| 1175 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1176 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | // GNU extensions |
| 1181 | if (LangOpts.GNUMode) { |
| 1182 | // FIXME: Enable when we actually support decimal floating point. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1183 | // Results.AddResult(Result("_Decimal32")); |
| 1184 | // Results.AddResult(Result("_Decimal64")); |
| 1185 | // Results.AddResult(Result("_Decimal128")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1186 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1187 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 1188 | Pattern->AddTypedTextChunk("typeof"); |
| 1189 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1190 | Pattern->AddPlaceholderChunk("expression"); |
| 1191 | Results.AddResult(Result(Pattern)); |
| 1192 | |
| 1193 | Pattern = new CodeCompletionString; |
| 1194 | Pattern->AddTypedTextChunk("typeof"); |
| 1195 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1196 | Pattern->AddPlaceholderChunk("type"); |
| 1197 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1198 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1202 | static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1203 | const LangOptions &LangOpts, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1204 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1205 | typedef CodeCompletionResult Result; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1206 | // Note: we don't suggest either "auto" or "register", because both |
| 1207 | // are pointless as storage specifiers. Elsewhere, we suggest "auto" |
| 1208 | // in C++0x as a type specifier. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1209 | Results.AddResult(Result("extern")); |
| 1210 | Results.AddResult(Result("static")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1213 | static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1214 | const LangOptions &LangOpts, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1215 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1216 | typedef CodeCompletionResult Result; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1217 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1218 | case Sema::PCC_Class: |
| 1219 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1220 | if (LangOpts.CPlusPlus) { |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1221 | Results.AddResult(Result("explicit")); |
| 1222 | Results.AddResult(Result("friend")); |
| 1223 | Results.AddResult(Result("mutable")); |
| 1224 | Results.AddResult(Result("virtual")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1225 | } |
| 1226 | // Fall through |
| 1227 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1228 | case Sema::PCC_ObjCInterface: |
| 1229 | case Sema::PCC_ObjCImplementation: |
| 1230 | case Sema::PCC_Namespace: |
| 1231 | case Sema::PCC_Template: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1232 | if (LangOpts.CPlusPlus || LangOpts.C99) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1233 | Results.AddResult(Result("inline")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1234 | break; |
| 1235 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1236 | case Sema::PCC_ObjCInstanceVariableList: |
| 1237 | case Sema::PCC_Expression: |
| 1238 | case Sema::PCC_Statement: |
| 1239 | case Sema::PCC_ForInit: |
| 1240 | case Sema::PCC_Condition: |
| 1241 | case Sema::PCC_RecoveryInFunction: |
| 1242 | case Sema::PCC_Type: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1243 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1244 | break; |
| 1245 | } |
| 1246 | } |
| 1247 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1248 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); |
| 1249 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); |
| 1250 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1251 | ResultBuilder &Results, |
| 1252 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1253 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1254 | ResultBuilder &Results, |
| 1255 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1256 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1257 | ResultBuilder &Results, |
| 1258 | bool NeedAt); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1259 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1260 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1261 | static void AddTypedefResult(ResultBuilder &Results) { |
| 1262 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 1263 | Pattern->AddTypedTextChunk("typedef"); |
| 1264 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1265 | Pattern->AddPlaceholderChunk("type"); |
| 1266 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1267 | Pattern->AddPlaceholderChunk("name"); |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1268 | Results.AddResult(CodeCompletionResult(Pattern)); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1271 | static bool WantTypesInContext(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1272 | const LangOptions &LangOpts) { |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1273 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1274 | case Sema::PCC_Namespace: |
| 1275 | case Sema::PCC_Class: |
| 1276 | case Sema::PCC_ObjCInstanceVariableList: |
| 1277 | case Sema::PCC_Template: |
| 1278 | case Sema::PCC_MemberTemplate: |
| 1279 | case Sema::PCC_Statement: |
| 1280 | case Sema::PCC_RecoveryInFunction: |
| 1281 | case Sema::PCC_Type: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1282 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1283 | return true; |
| 1284 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1285 | case Sema::PCC_Expression: |
| 1286 | case Sema::PCC_Condition: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1287 | return LangOpts.CPlusPlus; |
| 1288 | |
| 1289 | case Sema::PCC_ObjCInterface: |
| 1290 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1291 | return false; |
| 1292 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1293 | case Sema::PCC_ForInit: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1294 | return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99; |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | return false; |
| 1298 | } |
| 1299 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1300 | /// \brief Add language constructs that show up for "ordinary" names. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1301 | static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1302 | Scope *S, |
| 1303 | Sema &SemaRef, |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1304 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1305 | typedef CodeCompletionResult Result; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1306 | switch (CCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1307 | case Sema::PCC_Namespace: |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1308 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 1309 | CodeCompletionString *Pattern = 0; |
| 1310 | |
| 1311 | if (Results.includeCodePatterns()) { |
| 1312 | // namespace <identifier> { declarations } |
| 1313 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 1314 | Pattern->AddTypedTextChunk("namespace"); |
| 1315 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1316 | Pattern->AddPlaceholderChunk("identifier"); |
| 1317 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1318 | Pattern->AddPlaceholderChunk("declarations"); |
| 1319 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1320 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 1321 | Results.AddResult(Result(Pattern)); |
| 1322 | } |
| 1323 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1324 | // namespace identifier = identifier ; |
| 1325 | Pattern = new CodeCompletionString; |
| 1326 | Pattern->AddTypedTextChunk("namespace"); |
| 1327 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1328 | Pattern->AddPlaceholderChunk("name"); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1329 | Pattern->AddChunk(CodeCompletionString::CK_Equal); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1330 | Pattern->AddPlaceholderChunk("namespace"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1331 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1332 | |
| 1333 | // Using directives |
| 1334 | Pattern = new CodeCompletionString; |
| 1335 | Pattern->AddTypedTextChunk("using"); |
| 1336 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1337 | Pattern->AddTextChunk("namespace"); |
| 1338 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1339 | Pattern->AddPlaceholderChunk("identifier"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1340 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1341 | |
| 1342 | // asm(string-literal) |
| 1343 | Pattern = new CodeCompletionString; |
| 1344 | Pattern->AddTypedTextChunk("asm"); |
| 1345 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1346 | Pattern->AddPlaceholderChunk("string-literal"); |
| 1347 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
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 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1350 | if (Results.includeCodePatterns()) { |
| 1351 | // Explicit template instantiation |
| 1352 | Pattern = new CodeCompletionString; |
| 1353 | Pattern->AddTypedTextChunk("template"); |
| 1354 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1355 | Pattern->AddPlaceholderChunk("declaration"); |
| 1356 | Results.AddResult(Result(Pattern)); |
| 1357 | } |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1358 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1359 | |
| 1360 | if (SemaRef.getLangOptions().ObjC1) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1361 | AddObjCTopLevelResults(Results, true); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1362 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1363 | AddTypedefResult(Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1364 | // Fall through |
| 1365 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1366 | case Sema::PCC_Class: |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1367 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1368 | // Using declaration |
| 1369 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 1370 | Pattern->AddTypedTextChunk("using"); |
| 1371 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1372 | Pattern->AddPlaceholderChunk("qualifier"); |
| 1373 | Pattern->AddTextChunk("::"); |
| 1374 | Pattern->AddPlaceholderChunk("name"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1375 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1376 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1377 | // using typename qualifier::name (only in a dependent context) |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1378 | if (SemaRef.CurContext->isDependentContext()) { |
| 1379 | Pattern = new CodeCompletionString; |
| 1380 | Pattern->AddTypedTextChunk("using"); |
| 1381 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1382 | Pattern->AddTextChunk("typename"); |
| 1383 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1384 | Pattern->AddPlaceholderChunk("qualifier"); |
| 1385 | Pattern->AddTextChunk("::"); |
| 1386 | Pattern->AddPlaceholderChunk("name"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1387 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1390 | if (CCC == Sema::PCC_Class) { |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1391 | AddTypedefResult(Results); |
| 1392 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1393 | // public: |
| 1394 | Pattern = new CodeCompletionString; |
| 1395 | Pattern->AddTypedTextChunk("public"); |
| 1396 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1397 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1398 | |
| 1399 | // protected: |
| 1400 | Pattern = new CodeCompletionString; |
| 1401 | Pattern->AddTypedTextChunk("protected"); |
| 1402 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1403 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1404 | |
| 1405 | // private: |
| 1406 | Pattern = new CodeCompletionString; |
| 1407 | Pattern->AddTypedTextChunk("private"); |
| 1408 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1409 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | // Fall through |
| 1413 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1414 | case Sema::PCC_Template: |
| 1415 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1416 | if (SemaRef.getLangOptions().CPlusPlus && Results.includeCodePatterns()) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1417 | // template < parameters > |
| 1418 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 1419 | Pattern->AddTypedTextChunk("template"); |
| 1420 | Pattern->AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1421 | Pattern->AddPlaceholderChunk("parameters"); |
| 1422 | Pattern->AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1423 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1426 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 1427 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1428 | break; |
| 1429 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1430 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1431 | AddObjCInterfaceResults(SemaRef.getLangOptions(), Results, true); |
| 1432 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 1433 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1434 | break; |
| 1435 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1436 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1437 | AddObjCImplementationResults(SemaRef.getLangOptions(), Results, true); |
| 1438 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 1439 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1440 | break; |
| 1441 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1442 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1443 | AddObjCVisibilityResults(SemaRef.getLangOptions(), Results, true); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1444 | break; |
| 1445 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1446 | case Sema::PCC_RecoveryInFunction: |
| 1447 | case Sema::PCC_Statement: { |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1448 | AddTypedefResult(Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1449 | |
| 1450 | CodeCompletionString *Pattern = 0; |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1451 | if (SemaRef.getLangOptions().CPlusPlus && Results.includeCodePatterns()) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1452 | Pattern = new CodeCompletionString; |
| 1453 | Pattern->AddTypedTextChunk("try"); |
| 1454 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1455 | Pattern->AddPlaceholderChunk("statements"); |
| 1456 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1457 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 1458 | Pattern->AddTextChunk("catch"); |
| 1459 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1460 | Pattern->AddPlaceholderChunk("declaration"); |
| 1461 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1462 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1463 | Pattern->AddPlaceholderChunk("statements"); |
| 1464 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1465 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1466 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1467 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1468 | if (SemaRef.getLangOptions().ObjC1) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1469 | AddObjCStatementResults(Results, true); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1470 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1471 | if (Results.includeCodePatterns()) { |
| 1472 | // if (condition) { statements } |
| 1473 | Pattern = new CodeCompletionString; |
| 1474 | Pattern->AddTypedTextChunk("if"); |
| 1475 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1476 | if (SemaRef.getLangOptions().CPlusPlus) |
| 1477 | Pattern->AddPlaceholderChunk("condition"); |
| 1478 | else |
| 1479 | Pattern->AddPlaceholderChunk("expression"); |
| 1480 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1481 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1482 | Pattern->AddPlaceholderChunk("statements"); |
| 1483 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1484 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 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 | // switch (condition) { } |
| 1488 | Pattern = new CodeCompletionString; |
| 1489 | Pattern->AddTypedTextChunk("switch"); |
| 1490 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1491 | if (SemaRef.getLangOptions().CPlusPlus) |
| 1492 | Pattern->AddPlaceholderChunk("condition"); |
| 1493 | else |
| 1494 | Pattern->AddPlaceholderChunk("expression"); |
| 1495 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1496 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1497 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1498 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 1499 | Results.AddResult(Result(Pattern)); |
| 1500 | } |
| 1501 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1502 | // Switch-specific statements. |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 1503 | if (!SemaRef.getCurFunction()->SwitchStack.empty()) { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1504 | // case expression: |
| 1505 | Pattern = new CodeCompletionString; |
| 1506 | Pattern->AddTypedTextChunk("case"); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1507 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1508 | Pattern->AddPlaceholderChunk("expression"); |
| 1509 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1510 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1511 | |
| 1512 | // default: |
| 1513 | Pattern = new CodeCompletionString; |
| 1514 | Pattern->AddTypedTextChunk("default"); |
| 1515 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1516 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1519 | if (Results.includeCodePatterns()) { |
| 1520 | /// while (condition) { statements } |
| 1521 | Pattern = new CodeCompletionString; |
| 1522 | Pattern->AddTypedTextChunk("while"); |
| 1523 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1524 | if (SemaRef.getLangOptions().CPlusPlus) |
| 1525 | Pattern->AddPlaceholderChunk("condition"); |
| 1526 | else |
| 1527 | Pattern->AddPlaceholderChunk("expression"); |
| 1528 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1529 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1530 | Pattern->AddPlaceholderChunk("statements"); |
| 1531 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1532 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 1533 | Results.AddResult(Result(Pattern)); |
| 1534 | |
| 1535 | // do { statements } while ( expression ); |
| 1536 | Pattern = new CodeCompletionString; |
| 1537 | Pattern->AddTypedTextChunk("do"); |
| 1538 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1539 | Pattern->AddPlaceholderChunk("statements"); |
| 1540 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1541 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 1542 | Pattern->AddTextChunk("while"); |
| 1543 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1544 | Pattern->AddPlaceholderChunk("expression"); |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1545 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1546 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1547 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1548 | // for ( for-init-statement ; condition ; expression ) { statements } |
| 1549 | Pattern = new CodeCompletionString; |
| 1550 | Pattern->AddTypedTextChunk("for"); |
| 1551 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1552 | if (SemaRef.getLangOptions().CPlusPlus || SemaRef.getLangOptions().C99) |
| 1553 | Pattern->AddPlaceholderChunk("init-statement"); |
| 1554 | else |
| 1555 | Pattern->AddPlaceholderChunk("init-expression"); |
| 1556 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
| 1557 | Pattern->AddPlaceholderChunk("condition"); |
| 1558 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
| 1559 | Pattern->AddPlaceholderChunk("inc-expression"); |
| 1560 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1561 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1562 | Pattern->AddPlaceholderChunk("statements"); |
| 1563 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1564 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 1565 | Results.AddResult(Result(Pattern)); |
| 1566 | } |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1567 | |
| 1568 | if (S->getContinueParent()) { |
| 1569 | // continue ; |
| 1570 | Pattern = new CodeCompletionString; |
| 1571 | Pattern->AddTypedTextChunk("continue"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1572 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | if (S->getBreakParent()) { |
| 1576 | // break ; |
| 1577 | Pattern = new CodeCompletionString; |
| 1578 | Pattern->AddTypedTextChunk("break"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1579 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | // "return expression ;" or "return ;", depending on whether we |
| 1583 | // know the function is void or not. |
| 1584 | bool isVoid = false; |
| 1585 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) |
| 1586 | isVoid = Function->getResultType()->isVoidType(); |
| 1587 | else if (ObjCMethodDecl *Method |
| 1588 | = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) |
| 1589 | isVoid = Method->getResultType()->isVoidType(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1590 | else if (SemaRef.getCurBlock() && |
| 1591 | !SemaRef.getCurBlock()->ReturnType.isNull()) |
| 1592 | isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType(); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1593 | Pattern = new CodeCompletionString; |
| 1594 | Pattern->AddTypedTextChunk("return"); |
Douglas Gregor | 9329800 | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1595 | if (!isVoid) { |
| 1596 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1597 | Pattern->AddPlaceholderChunk("expression"); |
Douglas Gregor | 9329800 | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1598 | } |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1599 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1600 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1601 | // goto identifier ; |
| 1602 | Pattern = new CodeCompletionString; |
| 1603 | Pattern->AddTypedTextChunk("goto"); |
| 1604 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1605 | Pattern->AddPlaceholderChunk("label"); |
| 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 | // Using directives |
| 1609 | Pattern = new CodeCompletionString; |
| 1610 | Pattern->AddTypedTextChunk("using"); |
| 1611 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1612 | Pattern->AddTextChunk("namespace"); |
| 1613 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1614 | Pattern->AddPlaceholderChunk("identifier"); |
| 1615 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | // Fall through (for statement expressions). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1619 | case Sema::PCC_ForInit: |
| 1620 | case Sema::PCC_Condition: |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1621 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1622 | // Fall through: conditions and statements can have expressions. |
| 1623 | |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1624 | case Sema::PCC_ParenthesizedExpression: |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1625 | case Sema::PCC_Expression: { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1626 | CodeCompletionString *Pattern = 0; |
| 1627 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 1628 | // 'this', if we're in a non-static member function. |
| 1629 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) |
| 1630 | if (!Method->isStatic()) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1631 | Results.AddResult(Result("this")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1632 | |
| 1633 | // true, false |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1634 | Results.AddResult(Result("true")); |
| 1635 | Results.AddResult(Result("false")); |
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 | // dynamic_cast < type-id > ( expression ) |
| 1638 | Pattern = new CodeCompletionString; |
| 1639 | Pattern->AddTypedTextChunk("dynamic_cast"); |
| 1640 | Pattern->AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1641 | Pattern->AddPlaceholderChunk("type"); |
| 1642 | Pattern->AddChunk(CodeCompletionString::CK_RightAngle); |
| 1643 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1644 | Pattern->AddPlaceholderChunk("expression"); |
| 1645 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1646 | Results.AddResult(Result(Pattern)); |
| 1647 | |
| 1648 | // static_cast < type-id > ( expression ) |
| 1649 | Pattern = new CodeCompletionString; |
| 1650 | Pattern->AddTypedTextChunk("static_cast"); |
| 1651 | Pattern->AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1652 | Pattern->AddPlaceholderChunk("type"); |
| 1653 | Pattern->AddChunk(CodeCompletionString::CK_RightAngle); |
| 1654 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1655 | Pattern->AddPlaceholderChunk("expression"); |
| 1656 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1657 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1658 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1659 | // reinterpret_cast < type-id > ( expression ) |
| 1660 | Pattern = new CodeCompletionString; |
| 1661 | Pattern->AddTypedTextChunk("reinterpret_cast"); |
| 1662 | Pattern->AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1663 | Pattern->AddPlaceholderChunk("type"); |
| 1664 | Pattern->AddChunk(CodeCompletionString::CK_RightAngle); |
| 1665 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1666 | Pattern->AddPlaceholderChunk("expression"); |
| 1667 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1668 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1669 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1670 | // const_cast < type-id > ( expression ) |
| 1671 | Pattern = new CodeCompletionString; |
| 1672 | Pattern->AddTypedTextChunk("const_cast"); |
| 1673 | Pattern->AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1674 | Pattern->AddPlaceholderChunk("type"); |
| 1675 | Pattern->AddChunk(CodeCompletionString::CK_RightAngle); |
| 1676 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1677 | Pattern->AddPlaceholderChunk("expression"); |
| 1678 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1679 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1680 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1681 | // typeid ( expression-or-type ) |
| 1682 | Pattern = new CodeCompletionString; |
| 1683 | Pattern->AddTypedTextChunk("typeid"); |
| 1684 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1685 | Pattern->AddPlaceholderChunk("expression-or-type"); |
| 1686 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1687 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1688 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1689 | // new T ( ... ) |
| 1690 | Pattern = new CodeCompletionString; |
| 1691 | Pattern->AddTypedTextChunk("new"); |
| 1692 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1693 | Pattern->AddPlaceholderChunk("type"); |
| 1694 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1695 | Pattern->AddPlaceholderChunk("expressions"); |
| 1696 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1697 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1698 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1699 | // new T [ ] ( ... ) |
| 1700 | Pattern = new CodeCompletionString; |
| 1701 | Pattern->AddTypedTextChunk("new"); |
| 1702 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1703 | Pattern->AddPlaceholderChunk("type"); |
| 1704 | Pattern->AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1705 | Pattern->AddPlaceholderChunk("size"); |
| 1706 | Pattern->AddChunk(CodeCompletionString::CK_RightBracket); |
| 1707 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1708 | Pattern->AddPlaceholderChunk("expressions"); |
| 1709 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1710 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1711 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1712 | // delete expression |
| 1713 | Pattern = new CodeCompletionString; |
| 1714 | Pattern->AddTypedTextChunk("delete"); |
| 1715 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1716 | Pattern->AddPlaceholderChunk("expression"); |
| 1717 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1718 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1719 | // delete [] expression |
| 1720 | Pattern = new CodeCompletionString; |
| 1721 | Pattern->AddTypedTextChunk("delete"); |
| 1722 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1723 | Pattern->AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1724 | Pattern->AddChunk(CodeCompletionString::CK_RightBracket); |
| 1725 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1726 | Pattern->AddPlaceholderChunk("expression"); |
| 1727 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1728 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1729 | // throw expression |
| 1730 | Pattern = new CodeCompletionString; |
| 1731 | Pattern->AddTypedTextChunk("throw"); |
| 1732 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1733 | Pattern->AddPlaceholderChunk("expression"); |
| 1734 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1735 | |
| 1736 | // FIXME: Rethrow? |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | if (SemaRef.getLangOptions().ObjC1) { |
| 1740 | // 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] | 1741 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
| 1742 | // The interface can be NULL. |
| 1743 | if (ObjCInterfaceDecl *ID = Method->getClassInterface()) |
| 1744 | if (ID->getSuperClass()) |
| 1745 | Results.AddResult(Result("super")); |
| 1746 | } |
| 1747 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1748 | AddObjCExpressionResults(Results, true); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1751 | // sizeof expression |
| 1752 | Pattern = new CodeCompletionString; |
| 1753 | Pattern->AddTypedTextChunk("sizeof"); |
| 1754 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1755 | Pattern->AddPlaceholderChunk("expression-or-type"); |
| 1756 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1757 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1758 | break; |
| 1759 | } |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1760 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1761 | case Sema::PCC_Type: |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1762 | break; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1765 | if (WantTypesInContext(CCC, SemaRef.getLangOptions())) |
| 1766 | AddTypeSpecifierResults(SemaRef.getLangOptions(), Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1767 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1768 | if (SemaRef.getLangOptions().CPlusPlus && CCC != Sema::PCC_Type) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1769 | Results.AddResult(Result("operator")); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1772 | /// \brief If the given declaration has an associated type, add it as a result |
| 1773 | /// type chunk. |
| 1774 | static void AddResultTypeChunk(ASTContext &Context, |
| 1775 | NamedDecl *ND, |
| 1776 | CodeCompletionString *Result) { |
| 1777 | if (!ND) |
| 1778 | return; |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1779 | |
| 1780 | // Skip constructors and conversion functions, which have their return types |
| 1781 | // built into their names. |
| 1782 | if (isa<CXXConstructorDecl>(ND) || isa<CXXConversionDecl>(ND)) |
| 1783 | return; |
| 1784 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1785 | // Determine the type of the declaration (if it has a type). |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1786 | QualType T; |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1787 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) |
| 1788 | T = Function->getResultType(); |
| 1789 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
| 1790 | T = Method->getResultType(); |
| 1791 | else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) |
| 1792 | T = FunTmpl->getTemplatedDecl()->getResultType(); |
| 1793 | else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
| 1794 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); |
| 1795 | else if (isa<UnresolvedUsingValueDecl>(ND)) { |
| 1796 | /* Do nothing: ignore unresolved using declarations*/ |
| 1797 | } else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND)) |
| 1798 | T = Value->getType(); |
| 1799 | else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
| 1800 | T = Property->getType(); |
| 1801 | |
| 1802 | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) |
| 1803 | return; |
| 1804 | |
Douglas Gregor | 84139d6 | 2010-04-05 21:25:31 +0000 | [diff] [blame] | 1805 | PrintingPolicy Policy(Context.PrintingPolicy); |
| 1806 | Policy.AnonymousTagLocations = false; |
| 1807 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1808 | std::string TypeStr; |
Douglas Gregor | 84139d6 | 2010-04-05 21:25:31 +0000 | [diff] [blame] | 1809 | T.getAsStringInternal(TypeStr, Policy); |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1810 | Result->AddResultTypeChunk(TypeStr); |
| 1811 | } |
| 1812 | |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1813 | static void MaybeAddSentinel(ASTContext &Context, NamedDecl *FunctionOrMethod, |
| 1814 | CodeCompletionString *Result) { |
| 1815 | if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) |
| 1816 | if (Sentinel->getSentinel() == 0) { |
| 1817 | if (Context.getLangOptions().ObjC1 && |
| 1818 | Context.Idents.get("nil").hasMacroDefinition()) |
| 1819 | Result->AddTextChunk(", nil"); |
| 1820 | else if (Context.Idents.get("NULL").hasMacroDefinition()) |
| 1821 | Result->AddTextChunk(", NULL"); |
| 1822 | else |
| 1823 | Result->AddTextChunk(", (void*)0"); |
| 1824 | } |
| 1825 | } |
| 1826 | |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1827 | static std::string FormatFunctionParameter(ASTContext &Context, |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 1828 | ParmVarDecl *Param, |
| 1829 | bool SuppressName = false) { |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1830 | bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext()); |
| 1831 | if (Param->getType()->isDependentType() || |
| 1832 | !Param->getType()->isBlockPointerType()) { |
| 1833 | // The argument for a dependent or non-block parameter is a placeholder |
| 1834 | // containing that parameter's type. |
| 1835 | std::string Result; |
| 1836 | |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 1837 | if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName) |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1838 | Result = Param->getIdentifier()->getName(); |
| 1839 | |
| 1840 | Param->getType().getAsStringInternal(Result, |
| 1841 | Context.PrintingPolicy); |
| 1842 | |
| 1843 | if (ObjCMethodParam) { |
| 1844 | Result = "(" + Result; |
| 1845 | Result += ")"; |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 1846 | if (Param->getIdentifier() && !SuppressName) |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1847 | Result += Param->getIdentifier()->getName(); |
| 1848 | } |
| 1849 | return Result; |
| 1850 | } |
| 1851 | |
| 1852 | // The argument for a block pointer parameter is a block literal with |
| 1853 | // the appropriate type. |
| 1854 | FunctionProtoTypeLoc *Block = 0; |
| 1855 | TypeLoc TL; |
| 1856 | if (TypeSourceInfo *TSInfo = Param->getTypeSourceInfo()) { |
| 1857 | TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 1858 | while (true) { |
| 1859 | // Look through typedefs. |
| 1860 | if (TypedefTypeLoc *TypedefTL = dyn_cast<TypedefTypeLoc>(&TL)) { |
| 1861 | if (TypeSourceInfo *InnerTSInfo |
| 1862 | = TypedefTL->getTypedefDecl()->getTypeSourceInfo()) { |
| 1863 | TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 1864 | continue; |
| 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | // Look through qualified types |
| 1869 | if (QualifiedTypeLoc *QualifiedTL = dyn_cast<QualifiedTypeLoc>(&TL)) { |
| 1870 | TL = QualifiedTL->getUnqualifiedLoc(); |
| 1871 | continue; |
| 1872 | } |
| 1873 | |
| 1874 | // Try to get the function prototype behind the block pointer type, |
| 1875 | // then we're done. |
| 1876 | if (BlockPointerTypeLoc *BlockPtr |
| 1877 | = dyn_cast<BlockPointerTypeLoc>(&TL)) { |
| 1878 | TL = BlockPtr->getPointeeLoc(); |
| 1879 | Block = dyn_cast<FunctionProtoTypeLoc>(&TL); |
| 1880 | } |
| 1881 | break; |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | if (!Block) { |
| 1886 | // We were unable to find a FunctionProtoTypeLoc with parameter names |
| 1887 | // for the block; just use the parameter type as a placeholder. |
| 1888 | std::string Result; |
| 1889 | Param->getType().getUnqualifiedType(). |
| 1890 | getAsStringInternal(Result, Context.PrintingPolicy); |
| 1891 | |
| 1892 | if (ObjCMethodParam) { |
| 1893 | Result = "(" + Result; |
| 1894 | Result += ")"; |
| 1895 | if (Param->getIdentifier()) |
| 1896 | Result += Param->getIdentifier()->getName(); |
| 1897 | } |
| 1898 | |
| 1899 | return Result; |
| 1900 | } |
| 1901 | |
| 1902 | // We have the function prototype behind the block pointer type, as it was |
| 1903 | // written in the source. |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 1904 | std::string Result; |
| 1905 | QualType ResultType = Block->getTypePtr()->getResultType(); |
| 1906 | if (!ResultType->isVoidType()) |
| 1907 | ResultType.getAsStringInternal(Result, Context.PrintingPolicy); |
| 1908 | |
| 1909 | Result = '^' + Result; |
| 1910 | if (Block->getNumArgs() == 0) { |
| 1911 | if (Block->getTypePtr()->isVariadic()) |
| 1912 | Result += "(...)"; |
Douglas Gregor | c2760bc | 2010-10-02 23:49:58 +0000 | [diff] [blame^] | 1913 | else |
| 1914 | Result += "(void)"; |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 1915 | } else { |
| 1916 | Result += "("; |
| 1917 | for (unsigned I = 0, N = Block->getNumArgs(); I != N; ++I) { |
| 1918 | if (I) |
| 1919 | Result += ", "; |
| 1920 | Result += FormatFunctionParameter(Context, Block->getArg(I)); |
| 1921 | |
| 1922 | if (I == N - 1 && Block->getTypePtr()->isVariadic()) |
| 1923 | Result += ", ..."; |
| 1924 | } |
| 1925 | Result += ")"; |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 1926 | } |
Douglas Gregor | 3827625 | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 1927 | |
Douglas Gregor | c2760bc | 2010-10-02 23:49:58 +0000 | [diff] [blame^] | 1928 | if (Param->getIdentifier()) |
| 1929 | Result += Param->getIdentifier()->getName(); |
| 1930 | |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1931 | return Result; |
| 1932 | } |
| 1933 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1934 | /// \brief Add function parameter chunks to the given code completion string. |
| 1935 | static void AddFunctionParameterChunks(ASTContext &Context, |
| 1936 | FunctionDecl *Function, |
| 1937 | CodeCompletionString *Result) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1938 | typedef CodeCompletionString::Chunk Chunk; |
| 1939 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1940 | CodeCompletionString *CCStr = Result; |
| 1941 | |
| 1942 | for (unsigned P = 0, N = Function->getNumParams(); P != N; ++P) { |
| 1943 | ParmVarDecl *Param = Function->getParamDecl(P); |
| 1944 | |
| 1945 | if (Param->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 (P != 0) |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1954 | CCStr->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1955 | |
| 1956 | // Format the placeholder string. |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 1957 | std::string PlaceholderStr = FormatFunctionParameter(Context, Param); |
| 1958 | |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 1959 | if (Function->isVariadic() && P == N - 1) |
| 1960 | PlaceholderStr += ", ..."; |
| 1961 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1962 | // Add the placeholder string. |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1963 | CCStr->AddPlaceholderChunk(PlaceholderStr); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1964 | } |
Douglas Gregor | b3d4525 | 2009-09-22 21:42:17 +0000 | [diff] [blame] | 1965 | |
| 1966 | if (const FunctionProtoType *Proto |
| 1967 | = Function->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1968 | if (Proto->isVariadic()) { |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 1969 | if (Proto->getNumArgs() == 0) |
| 1970 | CCStr->AddPlaceholderChunk("..."); |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 1971 | |
| 1972 | MaybeAddSentinel(Context, Function, CCStr); |
| 1973 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | /// \brief Add template parameter chunks to the given code completion string. |
| 1977 | static void AddTemplateParameterChunks(ASTContext &Context, |
| 1978 | TemplateDecl *Template, |
| 1979 | CodeCompletionString *Result, |
| 1980 | unsigned MaxParameters = 0) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1981 | typedef CodeCompletionString::Chunk Chunk; |
| 1982 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1983 | CodeCompletionString *CCStr = Result; |
| 1984 | bool FirstParameter = true; |
| 1985 | |
| 1986 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 1987 | TemplateParameterList::iterator PEnd = Params->end(); |
| 1988 | if (MaxParameters) |
| 1989 | PEnd = Params->begin() + MaxParameters; |
| 1990 | for (TemplateParameterList::iterator P = Params->begin(); P != PEnd; ++P) { |
| 1991 | bool HasDefaultArg = false; |
| 1992 | std::string PlaceholderStr; |
| 1993 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 1994 | if (TTP->wasDeclaredWithTypename()) |
| 1995 | PlaceholderStr = "typename"; |
| 1996 | else |
| 1997 | PlaceholderStr = "class"; |
| 1998 | |
| 1999 | if (TTP->getIdentifier()) { |
| 2000 | PlaceholderStr += ' '; |
| 2001 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2002 | } |
| 2003 | |
| 2004 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2005 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2006 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 2007 | if (NTTP->getIdentifier()) |
| 2008 | PlaceholderStr = NTTP->getIdentifier()->getName(); |
| 2009 | NTTP->getType().getAsStringInternal(PlaceholderStr, |
| 2010 | Context.PrintingPolicy); |
| 2011 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2012 | } else { |
| 2013 | assert(isa<TemplateTemplateParmDecl>(*P)); |
| 2014 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 2015 | |
| 2016 | // Since putting the template argument list into the placeholder would |
| 2017 | // be very, very long, we just use an abbreviation. |
| 2018 | PlaceholderStr = "template<...> class"; |
| 2019 | if (TTP->getIdentifier()) { |
| 2020 | PlaceholderStr += ' '; |
| 2021 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2022 | } |
| 2023 | |
| 2024 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2025 | } |
| 2026 | |
| 2027 | if (HasDefaultArg) { |
| 2028 | // When we see an optional default argument, put that argument and |
| 2029 | // the remaining default arguments into a new, optional string. |
| 2030 | CodeCompletionString *Opt = new CodeCompletionString; |
| 2031 | CCStr->AddOptionalChunk(std::auto_ptr<CodeCompletionString>(Opt)); |
| 2032 | CCStr = Opt; |
| 2033 | } |
| 2034 | |
| 2035 | if (FirstParameter) |
| 2036 | FirstParameter = false; |
| 2037 | else |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2038 | CCStr->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2039 | |
| 2040 | // Add the placeholder string. |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2041 | CCStr->AddPlaceholderChunk(PlaceholderStr); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2042 | } |
| 2043 | } |
| 2044 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2045 | /// \brief Add a qualifier to the given code-completion string, if the |
| 2046 | /// provided nested-name-specifier is non-NULL. |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2047 | static void |
| 2048 | AddQualifierToCompletionString(CodeCompletionString *Result, |
| 2049 | NestedNameSpecifier *Qualifier, |
| 2050 | bool QualifierIsInformative, |
| 2051 | ASTContext &Context) { |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2052 | if (!Qualifier) |
| 2053 | return; |
| 2054 | |
| 2055 | std::string PrintedNNS; |
| 2056 | { |
| 2057 | llvm::raw_string_ostream OS(PrintedNNS); |
| 2058 | Qualifier->print(OS, Context.PrintingPolicy); |
| 2059 | } |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2060 | if (QualifierIsInformative) |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2061 | Result->AddInformativeChunk(PrintedNNS); |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2062 | else |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2063 | Result->AddTextChunk(PrintedNNS); |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2066 | static void AddFunctionTypeQualsToCompletionString(CodeCompletionString *Result, |
| 2067 | FunctionDecl *Function) { |
| 2068 | const FunctionProtoType *Proto |
| 2069 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2070 | if (!Proto || !Proto->getTypeQuals()) |
| 2071 | return; |
| 2072 | |
| 2073 | std::string QualsStr; |
| 2074 | if (Proto->getTypeQuals() & Qualifiers::Const) |
| 2075 | QualsStr += " const"; |
| 2076 | if (Proto->getTypeQuals() & Qualifiers::Volatile) |
| 2077 | QualsStr += " volatile"; |
| 2078 | if (Proto->getTypeQuals() & Qualifiers::Restrict) |
| 2079 | QualsStr += " restrict"; |
| 2080 | Result->AddInformativeChunk(QualsStr); |
| 2081 | } |
| 2082 | |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2083 | /// \brief Add the name of the given declaration |
| 2084 | static void AddTypedNameChunk(ASTContext &Context, NamedDecl *ND, |
| 2085 | CodeCompletionString *Result) { |
| 2086 | typedef CodeCompletionString::Chunk Chunk; |
| 2087 | |
| 2088 | DeclarationName Name = ND->getDeclName(); |
| 2089 | if (!Name) |
| 2090 | return; |
| 2091 | |
| 2092 | switch (Name.getNameKind()) { |
| 2093 | case DeclarationName::Identifier: |
| 2094 | case DeclarationName::CXXConversionFunctionName: |
| 2095 | case DeclarationName::CXXOperatorName: |
| 2096 | case DeclarationName::CXXDestructorName: |
| 2097 | case DeclarationName::CXXLiteralOperatorName: |
| 2098 | Result->AddTypedTextChunk(ND->getNameAsString()); |
| 2099 | break; |
| 2100 | |
| 2101 | case DeclarationName::CXXUsingDirective: |
| 2102 | case DeclarationName::ObjCZeroArgSelector: |
| 2103 | case DeclarationName::ObjCOneArgSelector: |
| 2104 | case DeclarationName::ObjCMultiArgSelector: |
| 2105 | break; |
| 2106 | |
| 2107 | case DeclarationName::CXXConstructorName: { |
| 2108 | CXXRecordDecl *Record = 0; |
| 2109 | QualType Ty = Name.getCXXNameType(); |
| 2110 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) |
| 2111 | Record = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 2112 | else if (const InjectedClassNameType *InjectedTy |
| 2113 | = Ty->getAs<InjectedClassNameType>()) |
| 2114 | Record = InjectedTy->getDecl(); |
| 2115 | else { |
| 2116 | Result->AddTypedTextChunk(ND->getNameAsString()); |
| 2117 | break; |
| 2118 | } |
| 2119 | |
| 2120 | Result->AddTypedTextChunk(Record->getNameAsString()); |
| 2121 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { |
| 2122 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
| 2123 | AddTemplateParameterChunks(Context, Template, Result); |
| 2124 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
| 2125 | } |
| 2126 | break; |
| 2127 | } |
| 2128 | } |
| 2129 | } |
| 2130 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2131 | /// \brief If possible, create a new code completion string for the given |
| 2132 | /// result. |
| 2133 | /// |
| 2134 | /// \returns Either a new, heap-allocated code completion string describing |
| 2135 | /// how to use this result, or NULL to indicate that the string or name of the |
| 2136 | /// result is all that is needed. |
| 2137 | CodeCompletionString * |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2138 | CodeCompletionResult::CreateCodeCompletionString(Sema &S, |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2139 | CodeCompletionString *Result) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2140 | typedef CodeCompletionString::Chunk Chunk; |
| 2141 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2142 | if (Kind == RK_Pattern) |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2143 | return Pattern->Clone(Result); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2144 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2145 | if (!Result) |
| 2146 | Result = new CodeCompletionString; |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2147 | |
| 2148 | if (Kind == RK_Keyword) { |
| 2149 | Result->AddTypedTextChunk(Keyword); |
| 2150 | return Result; |
| 2151 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2152 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2153 | if (Kind == RK_Macro) { |
| 2154 | MacroInfo *MI = S.PP.getMacroInfo(Macro); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2155 | assert(MI && "Not a macro?"); |
| 2156 | |
| 2157 | Result->AddTypedTextChunk(Macro->getName()); |
| 2158 | |
| 2159 | if (!MI->isFunctionLike()) |
| 2160 | return Result; |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2161 | |
| 2162 | // Format a function-like macro with placeholders for the arguments. |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2163 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2164 | for (MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end(); |
| 2165 | A != AEnd; ++A) { |
| 2166 | if (A != MI->arg_begin()) |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2167 | Result->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2168 | |
| 2169 | if (!MI->isVariadic() || A != AEnd - 1) { |
| 2170 | // Non-variadic argument. |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2171 | Result->AddPlaceholderChunk((*A)->getName()); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2172 | continue; |
| 2173 | } |
| 2174 | |
| 2175 | // Variadic argument; cope with the different between GNU and C99 |
| 2176 | // variadic macros, providing a single placeholder for the rest of the |
| 2177 | // arguments. |
| 2178 | if ((*A)->isStr("__VA_ARGS__")) |
| 2179 | Result->AddPlaceholderChunk("..."); |
| 2180 | else { |
| 2181 | std::string Arg = (*A)->getName(); |
| 2182 | Arg += "..."; |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2183 | Result->AddPlaceholderChunk(Arg); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2184 | } |
| 2185 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2186 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2187 | return Result; |
| 2188 | } |
| 2189 | |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2190 | assert(Kind == RK_Declaration && "Missed a result kind?"); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2191 | NamedDecl *ND = Declaration; |
| 2192 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2193 | if (StartsNestedNameSpecifier) { |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2194 | Result->AddTypedTextChunk(ND->getNameAsString()); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2195 | Result->AddTextChunk("::"); |
| 2196 | return Result; |
| 2197 | } |
| 2198 | |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2199 | AddResultTypeChunk(S.Context, ND, Result); |
| 2200 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2201 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2202 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2203 | S.Context); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2204 | AddTypedNameChunk(S.Context, ND, Result); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2205 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2206 | AddFunctionParameterChunks(S.Context, Function, Result); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2207 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2208 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2209 | return Result; |
| 2210 | } |
| 2211 | |
| 2212 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2213 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2214 | S.Context); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2215 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2216 | AddTypedNameChunk(S.Context, Function, Result); |
| 2217 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2218 | // Figure out which template parameters are deduced (or have default |
| 2219 | // arguments). |
| 2220 | llvm::SmallVector<bool, 16> Deduced; |
| 2221 | S.MarkDeducedTemplateParameters(FunTmpl, Deduced); |
| 2222 | unsigned LastDeducibleArgument; |
| 2223 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
| 2224 | --LastDeducibleArgument) { |
| 2225 | if (!Deduced[LastDeducibleArgument - 1]) { |
| 2226 | // C++0x: Figure out if the template argument has a default. If so, |
| 2227 | // the user doesn't need to type this argument. |
| 2228 | // FIXME: We need to abstract template parameters better! |
| 2229 | bool HasDefaultArg = false; |
| 2230 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
| 2231 | LastDeducibleArgument - 1); |
| 2232 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 2233 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2234 | else if (NonTypeTemplateParmDecl *NTTP |
| 2235 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 2236 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2237 | else { |
| 2238 | assert(isa<TemplateTemplateParmDecl>(Param)); |
| 2239 | HasDefaultArg |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2240 | = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | if (!HasDefaultArg) |
| 2244 | break; |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | if (LastDeducibleArgument) { |
| 2249 | // Some of the function template arguments cannot be deduced from a |
| 2250 | // function call, so we introduce an explicit template argument list |
| 2251 | // containing all of the arguments up to the first deducible argument. |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2252 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2253 | AddTemplateParameterChunks(S.Context, FunTmpl, Result, |
| 2254 | LastDeducibleArgument); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2255 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | // Add the function parameters |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2259 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2260 | AddFunctionParameterChunks(S.Context, Function, Result); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2261 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | a61a879 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2262 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2263 | return Result; |
| 2264 | } |
| 2265 | |
| 2266 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2267 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2268 | S.Context); |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2269 | Result->AddTypedTextChunk(Template->getNameAsString()); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2270 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2271 | AddTemplateParameterChunks(S.Context, Template, Result); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2272 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2273 | return Result; |
| 2274 | } |
| 2275 | |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2276 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2277 | Selector Sel = Method->getSelector(); |
| 2278 | if (Sel.isUnarySelector()) { |
| 2279 | Result->AddTypedTextChunk(Sel.getIdentifierInfoForSlot(0)->getName()); |
| 2280 | return Result; |
| 2281 | } |
| 2282 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2283 | std::string SelName = Sel.getIdentifierInfoForSlot(0)->getName().str(); |
| 2284 | SelName += ':'; |
| 2285 | if (StartParameter == 0) |
| 2286 | Result->AddTypedTextChunk(SelName); |
| 2287 | else { |
| 2288 | Result->AddInformativeChunk(SelName); |
| 2289 | |
| 2290 | // If there is only one parameter, and we're past it, add an empty |
| 2291 | // typed-text chunk since there is nothing to type. |
| 2292 | if (Method->param_size() == 1) |
| 2293 | Result->AddTypedTextChunk(""); |
| 2294 | } |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2295 | unsigned Idx = 0; |
| 2296 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 2297 | PEnd = Method->param_end(); |
| 2298 | P != PEnd; (void)++P, ++Idx) { |
| 2299 | if (Idx > 0) { |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2300 | std::string Keyword; |
| 2301 | if (Idx > StartParameter) |
Douglas Gregor | 834389b | 2010-01-12 06:38:28 +0000 | [diff] [blame] | 2302 | Result->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2303 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) |
| 2304 | Keyword += II->getName().str(); |
| 2305 | Keyword += ":"; |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2306 | if (Idx < StartParameter || AllParametersAreInformative) |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2307 | Result->AddInformativeChunk(Keyword); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2308 | else if (Idx == StartParameter) |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2309 | Result->AddTypedTextChunk(Keyword); |
| 2310 | else |
| 2311 | Result->AddTextChunk(Keyword); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2312 | } |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2313 | |
| 2314 | // If we're before the starting parameter, skip the placeholder. |
| 2315 | if (Idx < StartParameter) |
| 2316 | continue; |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2317 | |
| 2318 | std::string Arg; |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2319 | |
| 2320 | if ((*P)->getType()->isBlockPointerType() && !DeclaringEntity) |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2321 | Arg = FormatFunctionParameter(S.Context, *P, true); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2322 | else { |
| 2323 | (*P)->getType().getAsStringInternal(Arg, S.Context.PrintingPolicy); |
| 2324 | Arg = "(" + Arg + ")"; |
| 2325 | if (IdentifierInfo *II = (*P)->getIdentifier()) |
Douglas Gregor | aba4808 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2326 | if (DeclaringEntity || AllParametersAreInformative) |
| 2327 | Arg += II->getName().str(); |
Douglas Gregor | 83482d1 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2330 | if (Method->isVariadic() && (P + 1) == PEnd) |
| 2331 | Arg += ", ..."; |
| 2332 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2333 | if (DeclaringEntity) |
| 2334 | Result->AddTextChunk(Arg); |
| 2335 | else if (AllParametersAreInformative) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2336 | Result->AddInformativeChunk(Arg); |
| 2337 | else |
| 2338 | Result->AddPlaceholderChunk(Arg); |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
Douglas Gregor | 2a17af0 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2341 | if (Method->isVariadic()) { |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2342 | if (Method->param_size() == 0) { |
| 2343 | if (DeclaringEntity) |
| 2344 | Result->AddTextChunk(", ..."); |
| 2345 | else if (AllParametersAreInformative) |
| 2346 | Result->AddInformativeChunk(", ..."); |
| 2347 | else |
| 2348 | Result->AddPlaceholderChunk(", ..."); |
| 2349 | } |
Douglas Gregor | aaa107a | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2350 | |
| 2351 | MaybeAddSentinel(S.Context, Method, Result); |
Douglas Gregor | 2a17af0 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
Douglas Gregor | 9630eb6 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2354 | return Result; |
| 2355 | } |
| 2356 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2357 | if (Qualifier) |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2358 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 2359 | S.Context); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2360 | |
| 2361 | Result->AddTypedTextChunk(ND->getNameAsString()); |
| 2362 | return Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2365 | CodeCompletionString * |
| 2366 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( |
| 2367 | unsigned CurrentArg, |
| 2368 | Sema &S) const { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2369 | typedef CodeCompletionString::Chunk Chunk; |
| 2370 | |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2371 | CodeCompletionString *Result = new CodeCompletionString; |
| 2372 | FunctionDecl *FDecl = getFunction(); |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2373 | AddResultTypeChunk(S.Context, FDecl, Result); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2374 | const FunctionProtoType *Proto |
| 2375 | = dyn_cast<FunctionProtoType>(getFunctionType()); |
| 2376 | if (!FDecl && !Proto) { |
| 2377 | // Function without a prototype. Just give the return type and a |
| 2378 | // highlighted ellipsis. |
| 2379 | const FunctionType *FT = getFunctionType(); |
| 2380 | Result->AddTextChunk( |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2381 | FT->getResultType().getAsString(S.Context.PrintingPolicy)); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2382 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
| 2383 | Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "...")); |
| 2384 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2385 | return Result; |
| 2386 | } |
| 2387 | |
| 2388 | if (FDecl) |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2389 | Result->AddTextChunk(FDecl->getNameAsString()); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2390 | else |
| 2391 | Result->AddTextChunk( |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2392 | Proto->getResultType().getAsString(S.Context.PrintingPolicy)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2393 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2394 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2395 | unsigned NumParams = FDecl? FDecl->getNumParams() : Proto->getNumArgs(); |
| 2396 | for (unsigned I = 0; I != NumParams; ++I) { |
| 2397 | if (I) |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2398 | Result->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2399 | |
| 2400 | std::string ArgString; |
| 2401 | QualType ArgType; |
| 2402 | |
| 2403 | if (FDecl) { |
| 2404 | ArgString = FDecl->getParamDecl(I)->getNameAsString(); |
| 2405 | ArgType = FDecl->getParamDecl(I)->getOriginalType(); |
| 2406 | } else { |
| 2407 | ArgType = Proto->getArgType(I); |
| 2408 | } |
| 2409 | |
| 2410 | ArgType.getAsStringInternal(ArgString, S.Context.PrintingPolicy); |
| 2411 | |
| 2412 | if (I == CurrentArg) |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2413 | Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2414 | ArgString)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2415 | else |
Benjamin Kramer | 660cc18 | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 2416 | Result->AddTextChunk(ArgString); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | if (Proto && Proto->isVariadic()) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2420 | Result->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2421 | if (CurrentArg < NumParams) |
| 2422 | Result->AddTextChunk("..."); |
| 2423 | else |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2424 | Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "...")); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2425 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2426 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 2427 | |
| 2428 | return Result; |
| 2429 | } |
| 2430 | |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2431 | unsigned clang::getMacroUsagePriority(llvm::StringRef MacroName, |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2432 | const LangOptions &LangOpts, |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2433 | bool PreferredTypeIsPointer) { |
| 2434 | unsigned Priority = CCP_Macro; |
| 2435 | |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2436 | // Treat the "nil", "Nil" and "NULL" macros as null pointer constants. |
| 2437 | if (MacroName.equals("nil") || MacroName.equals("NULL") || |
| 2438 | MacroName.equals("Nil")) { |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2439 | Priority = CCP_Constant; |
| 2440 | if (PreferredTypeIsPointer) |
| 2441 | Priority = Priority / CCF_SimilarTypeMatch; |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2442 | } |
| 2443 | // Treat "YES", "NO", "true", and "false" as constants. |
| 2444 | else if (MacroName.equals("YES") || MacroName.equals("NO") || |
| 2445 | MacroName.equals("true") || MacroName.equals("false")) |
| 2446 | Priority = CCP_Constant; |
| 2447 | // Treat "bool" as a type. |
| 2448 | else if (MacroName.equals("bool")) |
| 2449 | Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0); |
| 2450 | |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2451 | |
| 2452 | return Priority; |
| 2453 | } |
| 2454 | |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 2455 | CXCursorKind clang::getCursorKindForDecl(Decl *D) { |
| 2456 | if (!D) |
| 2457 | return CXCursor_UnexposedDecl; |
| 2458 | |
| 2459 | switch (D->getKind()) { |
| 2460 | case Decl::Enum: return CXCursor_EnumDecl; |
| 2461 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 2462 | case Decl::Field: return CXCursor_FieldDecl; |
| 2463 | case Decl::Function: |
| 2464 | return CXCursor_FunctionDecl; |
| 2465 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 2466 | case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryImplDecl; |
| 2467 | case Decl::ObjCClass: |
| 2468 | // FIXME |
| 2469 | return CXCursor_UnexposedDecl; |
| 2470 | case Decl::ObjCForwardProtocol: |
| 2471 | // FIXME |
| 2472 | return CXCursor_UnexposedDecl; |
| 2473 | case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl; |
| 2474 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
| 2475 | case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl; |
| 2476 | case Decl::ObjCMethod: |
| 2477 | return cast<ObjCMethodDecl>(D)->isInstanceMethod() |
| 2478 | ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl; |
| 2479 | case Decl::CXXMethod: return CXCursor_CXXMethod; |
| 2480 | case Decl::CXXConstructor: return CXCursor_Constructor; |
| 2481 | case Decl::CXXDestructor: return CXCursor_Destructor; |
| 2482 | case Decl::CXXConversion: return CXCursor_ConversionFunction; |
| 2483 | case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl; |
| 2484 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
| 2485 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 2486 | case Decl::Typedef: return CXCursor_TypedefDecl; |
| 2487 | case Decl::Var: return CXCursor_VarDecl; |
| 2488 | case Decl::Namespace: return CXCursor_Namespace; |
| 2489 | case Decl::NamespaceAlias: return CXCursor_NamespaceAlias; |
| 2490 | case Decl::TemplateTypeParm: return CXCursor_TemplateTypeParameter; |
| 2491 | case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter; |
| 2492 | case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter; |
| 2493 | case Decl::FunctionTemplate: return CXCursor_FunctionTemplate; |
| 2494 | case Decl::ClassTemplate: return CXCursor_ClassTemplate; |
| 2495 | case Decl::ClassTemplatePartialSpecialization: |
| 2496 | return CXCursor_ClassTemplatePartialSpecialization; |
| 2497 | case Decl::UsingDirective: return CXCursor_UsingDirective; |
| 2498 | |
| 2499 | case Decl::Using: |
| 2500 | case Decl::UnresolvedUsingValue: |
| 2501 | case Decl::UnresolvedUsingTypename: |
| 2502 | return CXCursor_UsingDeclaration; |
| 2503 | |
| 2504 | default: |
| 2505 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 2506 | switch (TD->getTagKind()) { |
| 2507 | case TTK_Struct: return CXCursor_StructDecl; |
| 2508 | case TTK_Class: return CXCursor_ClassDecl; |
| 2509 | case TTK_Union: return CXCursor_UnionDecl; |
| 2510 | case TTK_Enum: return CXCursor_EnumDecl; |
| 2511 | } |
| 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | return CXCursor_UnexposedDecl; |
| 2516 | } |
| 2517 | |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2518 | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, |
| 2519 | bool TargetTypeIsPointer = false) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2520 | typedef CodeCompletionResult Result; |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2521 | |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2522 | Results.EnterNewScope(); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2523 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 2524 | MEnd = PP.macro_end(); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2525 | M != MEnd; ++M) { |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2526 | Results.AddResult(Result(M->first, |
| 2527 | getMacroUsagePriority(M->first->getName(), |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2528 | PP.getLangOptions(), |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 2529 | TargetTypeIsPointer))); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2530 | } |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 2531 | Results.ExitScope(); |
| 2532 | } |
| 2533 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2534 | static void AddPrettyFunctionResults(const LangOptions &LangOpts, |
| 2535 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2536 | typedef CodeCompletionResult Result; |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2537 | |
| 2538 | Results.EnterNewScope(); |
| 2539 | Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant)); |
| 2540 | Results.AddResult(Result("__FUNCTION__", CCP_Constant)); |
| 2541 | if (LangOpts.C99 || LangOpts.CPlusPlus0x) |
| 2542 | Results.AddResult(Result("__func__", CCP_Constant)); |
| 2543 | Results.ExitScope(); |
| 2544 | } |
| 2545 | |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2546 | static void HandleCodeCompleteResults(Sema *S, |
| 2547 | CodeCompleteConsumer *CodeCompleter, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2548 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2549 | CodeCompletionResult *Results, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2550 | unsigned NumResults) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2551 | if (CodeCompleter) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2552 | CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 2553 | |
| 2554 | for (unsigned I = 0; I != NumResults; ++I) |
| 2555 | Results[I].Destroy(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2558 | static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S, |
| 2559 | Sema::ParserCompletionContext PCC) { |
| 2560 | switch (PCC) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2561 | case Sema::PCC_Namespace: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2562 | return CodeCompletionContext::CCC_TopLevel; |
| 2563 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2564 | case Sema::PCC_Class: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2565 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 2566 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2567 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2568 | return CodeCompletionContext::CCC_ObjCInterface; |
| 2569 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2570 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2571 | return CodeCompletionContext::CCC_ObjCImplementation; |
| 2572 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2573 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2574 | return CodeCompletionContext::CCC_ObjCIvarList; |
| 2575 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2576 | case Sema::PCC_Template: |
| 2577 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2578 | if (S.CurContext->isFileContext()) |
| 2579 | return CodeCompletionContext::CCC_TopLevel; |
| 2580 | else if (S.CurContext->isRecord()) |
| 2581 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 2582 | else |
| 2583 | return CodeCompletionContext::CCC_Other; |
| 2584 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2585 | case Sema::PCC_RecoveryInFunction: |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2586 | return CodeCompletionContext::CCC_Recovery; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2587 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2588 | case Sema::PCC_Expression: |
| 2589 | case Sema::PCC_ForInit: |
| 2590 | case Sema::PCC_Condition: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2591 | return CodeCompletionContext::CCC_Expression; |
| 2592 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2593 | case Sema::PCC_Statement: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2594 | return CodeCompletionContext::CCC_Statement; |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2595 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2596 | case Sema::PCC_Type: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2597 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2598 | |
| 2599 | case Sema::PCC_ParenthesizedExpression: |
| 2600 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | return CodeCompletionContext::CCC_Other; |
| 2604 | } |
| 2605 | |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2606 | /// \brief If we're in a C++ virtual member function, add completion results |
| 2607 | /// that invoke the functions we override, since it's common to invoke the |
| 2608 | /// overridden function as well as adding new functionality. |
| 2609 | /// |
| 2610 | /// \param S The semantic analysis object for which we are generating results. |
| 2611 | /// |
| 2612 | /// \param InContext This context in which the nested-name-specifier preceding |
| 2613 | /// the code-completion point |
| 2614 | static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, |
| 2615 | ResultBuilder &Results) { |
| 2616 | // Look through blocks. |
| 2617 | DeclContext *CurContext = S.CurContext; |
| 2618 | while (isa<BlockDecl>(CurContext)) |
| 2619 | CurContext = CurContext->getParent(); |
| 2620 | |
| 2621 | |
| 2622 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext); |
| 2623 | if (!Method || !Method->isVirtual()) |
| 2624 | return; |
| 2625 | |
| 2626 | // We need to have names for all of the parameters, if we're going to |
| 2627 | // generate a forwarding call. |
| 2628 | for (CXXMethodDecl::param_iterator P = Method->param_begin(), |
| 2629 | PEnd = Method->param_end(); |
| 2630 | P != PEnd; |
| 2631 | ++P) { |
| 2632 | if (!(*P)->getDeclName()) |
| 2633 | return; |
| 2634 | } |
| 2635 | |
| 2636 | for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(), |
| 2637 | MEnd = Method->end_overridden_methods(); |
| 2638 | M != MEnd; ++M) { |
| 2639 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 2640 | CXXMethodDecl *Overridden = const_cast<CXXMethodDecl *>(*M); |
| 2641 | if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl()) |
| 2642 | continue; |
| 2643 | |
| 2644 | // If we need a nested-name-specifier, add one now. |
| 2645 | if (!InContext) { |
| 2646 | NestedNameSpecifier *NNS |
| 2647 | = getRequiredQualification(S.Context, CurContext, |
| 2648 | Overridden->getDeclContext()); |
| 2649 | if (NNS) { |
| 2650 | std::string Str; |
| 2651 | llvm::raw_string_ostream OS(Str); |
| 2652 | NNS->print(OS, S.Context.PrintingPolicy); |
| 2653 | Pattern->AddTextChunk(OS.str()); |
| 2654 | } |
| 2655 | } else if (!InContext->Equals(Overridden->getDeclContext())) |
| 2656 | continue; |
| 2657 | |
| 2658 | Pattern->AddTypedTextChunk(Overridden->getNameAsString()); |
| 2659 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 2660 | bool FirstParam = true; |
| 2661 | for (CXXMethodDecl::param_iterator P = Method->param_begin(), |
| 2662 | PEnd = Method->param_end(); |
| 2663 | P != PEnd; ++P) { |
| 2664 | if (FirstParam) |
| 2665 | FirstParam = false; |
| 2666 | else |
| 2667 | Pattern->AddChunk(CodeCompletionString::CK_Comma); |
| 2668 | |
| 2669 | Pattern->AddPlaceholderChunk((*P)->getIdentifier()->getName()); |
| 2670 | } |
| 2671 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 2672 | Results.AddResult(CodeCompletionResult(Pattern, |
| 2673 | CCP_SuperCompletion, |
| 2674 | CXCursor_CXXMethod)); |
| 2675 | Results.Ignore(Overridden); |
| 2676 | } |
| 2677 | } |
| 2678 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2679 | void Sema::CodeCompleteOrdinaryName(Scope *S, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2680 | ParserCompletionContext CompletionContext) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2681 | typedef CodeCompletionResult Result; |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2682 | ResultBuilder Results(*this, |
| 2683 | mapCodeCompletionContext(*this, CompletionContext)); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2684 | Results.EnterNewScope(); |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 2685 | |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2686 | // Determine how to filter results, e.g., so that the names of |
| 2687 | // values (functions, enumerators, function templates, etc.) are |
| 2688 | // only allowed where we can have an expression. |
| 2689 | switch (CompletionContext) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2690 | case PCC_Namespace: |
| 2691 | case PCC_Class: |
| 2692 | case PCC_ObjCInterface: |
| 2693 | case PCC_ObjCImplementation: |
| 2694 | case PCC_ObjCInstanceVariableList: |
| 2695 | case PCC_Template: |
| 2696 | case PCC_MemberTemplate: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2697 | case PCC_Type: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2698 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 2699 | break; |
| 2700 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2701 | case PCC_Statement: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2702 | case PCC_ParenthesizedExpression: |
Douglas Gregor | eb0d014 | 2010-08-24 23:58:17 +0000 | [diff] [blame] | 2703 | case PCC_Expression: |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2704 | case PCC_ForInit: |
| 2705 | case PCC_Condition: |
Douglas Gregor | 4710e5b | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 2706 | if (WantTypesInContext(CompletionContext, getLangOptions())) |
| 2707 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 2708 | else |
| 2709 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 2710 | |
| 2711 | if (getLangOptions().CPlusPlus) |
| 2712 | MaybeAddOverrideCalls(*this, /*InContext=*/0, Results); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2713 | break; |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 2714 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2715 | case PCC_RecoveryInFunction: |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 2716 | // Unfiltered |
| 2717 | break; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 2720 | // If we are in a C++ non-static member function, check the qualifiers on |
| 2721 | // the member function to filter/prioritize the results list. |
| 2722 | if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext)) |
| 2723 | if (CurMethod->isInstance()) |
| 2724 | Results.setObjectTypeQualifiers( |
| 2725 | Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers())); |
| 2726 | |
Douglas Gregor | 1ca6ae8 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 2727 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2728 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 2729 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 2a7925c | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 2730 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2731 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); |
Douglas Gregor | 2a7925c | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 2732 | Results.ExitScope(); |
| 2733 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2734 | switch (CompletionContext) { |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2735 | case PCC_ParenthesizedExpression: |
Douglas Gregor | 72db108 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 2736 | case PCC_Expression: |
| 2737 | case PCC_Statement: |
| 2738 | case PCC_RecoveryInFunction: |
| 2739 | if (S->getFnParent()) |
| 2740 | AddPrettyFunctionResults(PP.getLangOptions(), Results); |
| 2741 | break; |
| 2742 | |
| 2743 | case PCC_Namespace: |
| 2744 | case PCC_Class: |
| 2745 | case PCC_ObjCInterface: |
| 2746 | case PCC_ObjCImplementation: |
| 2747 | case PCC_ObjCInstanceVariableList: |
| 2748 | case PCC_Template: |
| 2749 | case PCC_MemberTemplate: |
| 2750 | case PCC_ForInit: |
| 2751 | case PCC_Condition: |
| 2752 | case PCC_Type: |
| 2753 | break; |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2754 | } |
| 2755 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2756 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2757 | AddMacroResults(PP, Results); |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2758 | |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 2759 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2760 | Results.data(),Results.size()); |
Douglas Gregor | 791215b | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2763 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| 2764 | ParsedType Receiver, |
| 2765 | IdentifierInfo **SelIdents, |
| 2766 | unsigned NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2767 | bool AtArgumentExpression, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2768 | bool IsSuper, |
| 2769 | ResultBuilder &Results); |
| 2770 | |
| 2771 | void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, |
| 2772 | bool AllowNonIdentifiers, |
| 2773 | bool AllowNestedNameSpecifiers) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2774 | typedef CodeCompletionResult Result; |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2775 | ResultBuilder Results(*this, |
| 2776 | AllowNestedNameSpecifiers |
| 2777 | ? CodeCompletionContext::CCC_PotentiallyQualifiedName |
| 2778 | : CodeCompletionContext::CCC_Name); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2779 | Results.EnterNewScope(); |
| 2780 | |
| 2781 | // Type qualifiers can come after names. |
| 2782 | Results.AddResult(Result("const")); |
| 2783 | Results.AddResult(Result("volatile")); |
| 2784 | if (getLangOptions().C99) |
| 2785 | Results.AddResult(Result("restrict")); |
| 2786 | |
| 2787 | if (getLangOptions().CPlusPlus) { |
| 2788 | if (AllowNonIdentifiers) { |
| 2789 | Results.AddResult(Result("operator")); |
| 2790 | } |
| 2791 | |
| 2792 | // Add nested-name-specifiers. |
| 2793 | if (AllowNestedNameSpecifiers) { |
| 2794 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2795 | Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2796 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 2797 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
| 2798 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2799 | Results.setFilter(0); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2800 | } |
| 2801 | } |
| 2802 | Results.ExitScope(); |
| 2803 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2804 | // If we're in a context where we might have an expression (rather than a |
| 2805 | // declaration), and what we've seen so far is an Objective-C type that could |
| 2806 | // be a receiver of a class message, this may be a class message send with |
| 2807 | // the initial opening bracket '[' missing. Add appropriate completions. |
| 2808 | if (AllowNonIdentifiers && !AllowNestedNameSpecifiers && |
| 2809 | DS.getTypeSpecType() == DeclSpec::TST_typename && |
| 2810 | DS.getStorageClassSpecAsWritten() == DeclSpec::SCS_unspecified && |
| 2811 | !DS.isThreadSpecified() && !DS.isExternInLinkageSpec() && |
| 2812 | DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified && |
| 2813 | DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 2814 | DS.getTypeQualifiers() == 0 && |
| 2815 | S && |
| 2816 | (S->getFlags() & Scope::DeclScope) != 0 && |
| 2817 | (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope | |
| 2818 | Scope::FunctionPrototypeScope | |
| 2819 | Scope::AtCatchScope)) == 0) { |
| 2820 | ParsedType T = DS.getRepAsType(); |
| 2821 | if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType()) |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2822 | AddClassMessageCompletions(*this, S, T, 0, 0, false, false, Results); |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
Douglas Gregor | 4497dd4 | 2010-08-24 04:59:56 +0000 | [diff] [blame] | 2825 | // Note that we intentionally suppress macro results here, since we do not |
| 2826 | // encourage using macros to produce the names of entities. |
| 2827 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2828 | HandleCodeCompleteResults(this, CodeCompleter, |
| 2829 | Results.getCompletionContext(), |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2830 | Results.data(), Results.size()); |
| 2831 | } |
| 2832 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2833 | struct Sema::CodeCompleteExpressionData { |
| 2834 | CodeCompleteExpressionData(QualType PreferredType = QualType()) |
| 2835 | : PreferredType(PreferredType), IntegralConstantExpression(false), |
| 2836 | ObjCCollection(false) { } |
| 2837 | |
| 2838 | QualType PreferredType; |
| 2839 | bool IntegralConstantExpression; |
| 2840 | bool ObjCCollection; |
| 2841 | llvm::SmallVector<Decl *, 4> IgnoreDecls; |
| 2842 | }; |
| 2843 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2844 | /// \brief Perform code-completion in an expression context when we know what |
| 2845 | /// type we're looking for. |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 2846 | /// |
| 2847 | /// \param IntegralConstantExpression Only permit integral constant |
| 2848 | /// expressions. |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2849 | void Sema::CodeCompleteExpression(Scope *S, |
| 2850 | const CodeCompleteExpressionData &Data) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2851 | typedef CodeCompletionResult Result; |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2852 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Expression); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2853 | if (Data.ObjCCollection) |
| 2854 | Results.setFilter(&ResultBuilder::IsObjCCollection); |
| 2855 | else if (Data.IntegralConstantExpression) |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 2856 | Results.setFilter(&ResultBuilder::IsIntegralConstantValue); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2857 | else if (WantTypesInContext(PCC_Expression, getLangOptions())) |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2858 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 2859 | else |
| 2860 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2861 | |
| 2862 | if (!Data.PreferredType.isNull()) |
| 2863 | Results.setPreferredType(Data.PreferredType.getNonReferenceType()); |
| 2864 | |
| 2865 | // Ignore any declarations that we were told that we don't care about. |
| 2866 | for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I) |
| 2867 | Results.Ignore(Data.IgnoreDecls[I]); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2868 | |
| 2869 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2870 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 2871 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2872 | |
| 2873 | Results.EnterNewScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2874 | AddOrdinaryNameResults(PCC_Expression, S, *this, Results); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2875 | Results.ExitScope(); |
| 2876 | |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2877 | bool PreferredTypeIsPointer = false; |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2878 | if (!Data.PreferredType.isNull()) |
| 2879 | PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType() |
| 2880 | || Data.PreferredType->isMemberPointerType() |
| 2881 | || Data.PreferredType->isBlockPointerType(); |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2882 | |
Douglas Gregor | aa5f77b | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 2883 | if (S->getFnParent() && |
| 2884 | !Data.ObjCCollection && |
| 2885 | !Data.IntegralConstantExpression) |
| 2886 | AddPrettyFunctionResults(PP.getLangOptions(), Results); |
| 2887 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2888 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 590c7d5 | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 2889 | AddMacroResults(PP, Results, PreferredTypeIsPointer); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2890 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 2891 | CodeCompletionContext(CodeCompletionContext::CCC_Expression, |
| 2892 | Data.PreferredType), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 2893 | Results.data(),Results.size()); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
Douglas Gregor | ac5fd84 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 2896 | void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) { |
| 2897 | if (E.isInvalid()) |
| 2898 | CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction); |
| 2899 | else if (getLangOptions().ObjC1) |
| 2900 | CodeCompleteObjCInstanceMessage(S, E.take(), 0, 0, false); |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 2901 | } |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2902 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2903 | static void AddObjCProperties(ObjCContainerDecl *Container, |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2904 | bool AllowCategories, |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2905 | DeclContext *CurContext, |
| 2906 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2907 | typedef CodeCompletionResult Result; |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2908 | |
| 2909 | // Add properties in this container. |
| 2910 | for (ObjCContainerDecl::prop_iterator P = Container->prop_begin(), |
| 2911 | PEnd = Container->prop_end(); |
| 2912 | P != PEnd; |
| 2913 | ++P) |
| 2914 | Results.MaybeAddResult(Result(*P, 0), CurContext); |
| 2915 | |
| 2916 | // Add properties in referenced protocols. |
| 2917 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 2918 | for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(), |
| 2919 | PEnd = Protocol->protocol_end(); |
| 2920 | P != PEnd; ++P) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2921 | AddObjCProperties(*P, AllowCategories, CurContext, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2922 | } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){ |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2923 | if (AllowCategories) { |
| 2924 | // Look through categories. |
| 2925 | for (ObjCCategoryDecl *Category = IFace->getCategoryList(); |
| 2926 | Category; Category = Category->getNextClassCategory()) |
| 2927 | AddObjCProperties(Category, AllowCategories, CurContext, Results); |
| 2928 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2929 | |
| 2930 | // Look through protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2931 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 2932 | I = IFace->all_referenced_protocol_begin(), |
| 2933 | E = IFace->all_referenced_protocol_end(); I != E; ++I) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2934 | AddObjCProperties(*I, AllowCategories, CurContext, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2935 | |
| 2936 | // Look in the superclass. |
| 2937 | if (IFace->getSuperClass()) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2938 | AddObjCProperties(IFace->getSuperClass(), AllowCategories, CurContext, |
| 2939 | Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2940 | } else if (const ObjCCategoryDecl *Category |
| 2941 | = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 2942 | // Look through protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2943 | for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(), |
| 2944 | PEnd = Category->protocol_end(); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2945 | P != PEnd; ++P) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2946 | AddObjCProperties(*P, AllowCategories, CurContext, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2947 | } |
| 2948 | } |
| 2949 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 2950 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, ExprTy *BaseE, |
| 2951 | SourceLocation OpLoc, |
| 2952 | bool IsArrow) { |
| 2953 | if (!BaseE || !CodeCompleter) |
| 2954 | return; |
| 2955 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2956 | typedef CodeCompletionResult Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2957 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 2958 | Expr *Base = static_cast<Expr *>(BaseE); |
| 2959 | QualType BaseType = Base->getType(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2960 | |
| 2961 | if (IsArrow) { |
| 2962 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 2963 | BaseType = Ptr->getPointeeType(); |
| 2964 | else if (BaseType->isObjCObjectPointerType()) |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 2965 | /*Do nothing*/ ; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2966 | else |
| 2967 | return; |
| 2968 | } |
| 2969 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2970 | ResultBuilder Results(*this, |
| 2971 | CodeCompletionContext(CodeCompletionContext::CCC_MemberAccess, |
| 2972 | BaseType), |
| 2973 | &ResultBuilder::IsMember); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2974 | Results.EnterNewScope(); |
| 2975 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
Douglas Gregor | 3cdee12 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 2976 | // Indicate that we are performing a member access, and the cv-qualifiers |
| 2977 | // for the base object type. |
| 2978 | Results.setObjectTypeQualifiers(BaseType.getQualifiers()); |
| 2979 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2980 | // Access to a C/C++ class, struct, or union. |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 2981 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 0cc8404 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2982 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2983 | LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer, |
| 2984 | CodeCompleter->includeGlobals()); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2985 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2986 | if (getLangOptions().CPlusPlus) { |
| 2987 | if (!Results.empty()) { |
| 2988 | // The "template" keyword can follow "->" or "." in the grammar. |
| 2989 | // However, we only want to suggest the template keyword if something |
| 2990 | // is dependent. |
| 2991 | bool IsDependent = BaseType->isDependentType(); |
| 2992 | if (!IsDependent) { |
| 2993 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) |
| 2994 | if (DeclContext *Ctx = (DeclContext *)DepScope->getEntity()) { |
| 2995 | IsDependent = Ctx->isDependentContext(); |
| 2996 | break; |
| 2997 | } |
| 2998 | } |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2999 | |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3000 | if (IsDependent) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3001 | Results.AddResult(Result("template")); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3002 | } |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3003 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3004 | } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) { |
| 3005 | // Objective-C property reference. |
| 3006 | |
| 3007 | // Add property results based on our interface. |
| 3008 | const ObjCObjectPointerType *ObjCPtr |
| 3009 | = BaseType->getAsObjCInterfacePointerType(); |
| 3010 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!"); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3011 | AddObjCProperties(ObjCPtr->getInterfaceDecl(), true, CurContext, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3012 | |
| 3013 | // Add properties from the protocols in a qualified interface. |
| 3014 | for (ObjCObjectPointerType::qual_iterator I = ObjCPtr->qual_begin(), |
| 3015 | E = ObjCPtr->qual_end(); |
| 3016 | I != E; ++I) |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3017 | AddObjCProperties(*I, true, CurContext, Results); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3018 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3019 | (!IsArrow && BaseType->isObjCObjectType())) { |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3020 | // Objective-C instance variable access. |
| 3021 | ObjCInterfaceDecl *Class = 0; |
| 3022 | if (const ObjCObjectPointerType *ObjCPtr |
| 3023 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 3024 | Class = ObjCPtr->getInterfaceDecl(); |
| 3025 | else |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3026 | Class = BaseType->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3027 | |
| 3028 | // Add all ivars from this class and its superclasses. |
Douglas Gregor | 80f4f4c | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 3029 | if (Class) { |
| 3030 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3031 | Results.setFilter(&ResultBuilder::IsObjCIvar); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3032 | LookupVisibleDecls(Class, LookupMemberName, Consumer, |
| 3033 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3034 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3035 | } |
Douglas Gregor | 95ac655 | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3036 | |
| 3037 | // FIXME: How do we cope with isa? |
| 3038 | |
| 3039 | Results.ExitScope(); |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3040 | |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3041 | // Hand off the results found for code completion. |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3042 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3043 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3044 | Results.data(),Results.size()); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3045 | } |
| 3046 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3047 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { |
| 3048 | if (!CodeCompleter) |
| 3049 | return; |
| 3050 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3051 | typedef CodeCompletionResult Result; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3052 | ResultBuilder::LookupFilter Filter = 0; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3053 | enum CodeCompletionContext::Kind ContextKind |
| 3054 | = CodeCompletionContext::CCC_Other; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3055 | switch ((DeclSpec::TST)TagSpec) { |
| 3056 | case DeclSpec::TST_enum: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3057 | Filter = &ResultBuilder::IsEnum; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3058 | ContextKind = CodeCompletionContext::CCC_EnumTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3059 | break; |
| 3060 | |
| 3061 | case DeclSpec::TST_union: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3062 | Filter = &ResultBuilder::IsUnion; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3063 | ContextKind = CodeCompletionContext::CCC_UnionTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3064 | break; |
| 3065 | |
| 3066 | case DeclSpec::TST_struct: |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3067 | case DeclSpec::TST_class: |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3068 | Filter = &ResultBuilder::IsClassOrStruct; |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3069 | ContextKind = CodeCompletionContext::CCC_ClassOrStructTag; |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3070 | break; |
| 3071 | |
| 3072 | default: |
| 3073 | assert(false && "Unknown type specifier kind in CodeCompleteTag"); |
| 3074 | return; |
| 3075 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3076 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3077 | ResultBuilder Results(*this, ContextKind); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3078 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 3079 | |
| 3080 | // First pass: look for tags. |
| 3081 | Results.setFilter(Filter); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3082 | LookupVisibleDecls(S, LookupTagName, Consumer, |
| 3083 | CodeCompleter->includeGlobals()); |
John McCall | 0d6b164 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 3084 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3085 | if (CodeCompleter->includeGlobals()) { |
| 3086 | // Second pass: look for nested name specifiers. |
| 3087 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
| 3088 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer); |
| 3089 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3090 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3091 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3092 | Results.data(),Results.size()); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3095 | void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3096 | ResultBuilder Results(*this, CodeCompletionContext::CCC_TypeQualifiers); |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3097 | Results.EnterNewScope(); |
| 3098 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const)) |
| 3099 | Results.AddResult("const"); |
| 3100 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile)) |
| 3101 | Results.AddResult("volatile"); |
| 3102 | if (getLangOptions().C99 && |
| 3103 | !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict)) |
| 3104 | Results.AddResult("restrict"); |
| 3105 | Results.ExitScope(); |
| 3106 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3107 | Results.getCompletionContext(), |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3108 | Results.data(), Results.size()); |
| 3109 | } |
| 3110 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3111 | void Sema::CodeCompleteCase(Scope *S) { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3112 | if (getCurFunction()->SwitchStack.empty() || !CodeCompleter) |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3113 | return; |
| 3114 | |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3115 | SwitchStmt *Switch = getCurFunction()->SwitchStack.back(); |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3116 | if (!Switch->getCond()->getType()->isEnumeralType()) { |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3117 | CodeCompleteExpressionData Data(Switch->getCond()->getType()); |
| 3118 | Data.IntegralConstantExpression = true; |
| 3119 | CodeCompleteExpression(S, Data); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3120 | return; |
Douglas Gregor | f957843 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3121 | } |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3122 | |
| 3123 | // Code-complete the cases of a switch statement over an enumeration type |
| 3124 | // by providing the list of |
| 3125 | EnumDecl *Enum = Switch->getCond()->getType()->getAs<EnumType>()->getDecl(); |
| 3126 | |
| 3127 | // Determine which enumerators we have already seen in the switch statement. |
| 3128 | // FIXME: Ideally, we would also be able to look *past* the code-completion |
| 3129 | // token, in case we are code-completing in the middle of the switch and not |
| 3130 | // at the end. However, we aren't able to do so at the moment. |
| 3131 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3132 | NestedNameSpecifier *Qualifier = 0; |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3133 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; |
| 3134 | SC = SC->getNextSwitchCase()) { |
| 3135 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); |
| 3136 | if (!Case) |
| 3137 | continue; |
| 3138 | |
| 3139 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); |
| 3140 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal)) |
| 3141 | if (EnumConstantDecl *Enumerator |
| 3142 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 3143 | // We look into the AST of the case statement to determine which |
| 3144 | // enumerator was named. Alternatively, we could compute the value of |
| 3145 | // the integral constant expression, then compare it against the |
| 3146 | // values of each enumerator. However, value-based approach would not |
| 3147 | // work as well with C++ templates where enumerators declared within a |
| 3148 | // template are type- and value-dependent. |
| 3149 | EnumeratorsSeen.insert(Enumerator); |
| 3150 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3151 | // If this is a qualified-id, keep track of the nested-name-specifier |
| 3152 | // 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] | 3153 | // |
| 3154 | // switch (TagD.getKind()) { |
| 3155 | // case TagDecl::TK_enum: |
| 3156 | // break; |
| 3157 | // case XXX |
| 3158 | // |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3159 | // At the XXX, our completions are TagDecl::TK_union, |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3160 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, |
| 3161 | // TK_struct, and TK_class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3162 | Qualifier = DRE->getQualifier(); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3163 | } |
| 3164 | } |
| 3165 | |
Douglas Gregor | b9d0ef7 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 3166 | if (getLangOptions().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { |
| 3167 | // If there are no prior enumerators in C++, check whether we have to |
| 3168 | // qualify the names of the enumerators that we suggest, because they |
| 3169 | // may not be visible in this scope. |
| 3170 | Qualifier = getRequiredQualification(Context, CurContext, |
| 3171 | Enum->getDeclContext()); |
| 3172 | |
| 3173 | // FIXME: Scoped enums need to start with "EnumDecl" as the context! |
| 3174 | } |
| 3175 | |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3176 | // Add any enumerators that have not yet been mentioned. |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3177 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Expression); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3178 | Results.EnterNewScope(); |
| 3179 | for (EnumDecl::enumerator_iterator E = Enum->enumerator_begin(), |
| 3180 | EEnd = Enum->enumerator_end(); |
| 3181 | E != EEnd; ++E) { |
| 3182 | if (EnumeratorsSeen.count(*E)) |
| 3183 | continue; |
| 3184 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3185 | Results.AddResult(CodeCompletionResult(*E, Qualifier), |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 3186 | CurContext, 0, false); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3187 | } |
| 3188 | Results.ExitScope(); |
Douglas Gregor | 2f880e4 | 2010-04-06 20:02:15 +0000 | [diff] [blame] | 3189 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3190 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3191 | AddMacroResults(PP, Results); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3192 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3193 | CodeCompletionContext::CCC_Expression, |
| 3194 | Results.data(),Results.size()); |
Douglas Gregor | 3e1005f | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3197 | namespace { |
| 3198 | struct IsBetterOverloadCandidate { |
| 3199 | Sema &S; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3200 | SourceLocation Loc; |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3201 | |
| 3202 | public: |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3203 | explicit IsBetterOverloadCandidate(Sema &S, SourceLocation Loc) |
| 3204 | : S(S), Loc(Loc) { } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3205 | |
| 3206 | bool |
| 3207 | operator()(const OverloadCandidate &X, const OverloadCandidate &Y) const { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3208 | return isBetterOverloadCandidate(S, X, Y, Loc); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3209 | } |
| 3210 | }; |
| 3211 | } |
| 3212 | |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3213 | static bool anyNullArguments(Expr **Args, unsigned NumArgs) { |
| 3214 | if (NumArgs && !Args) |
| 3215 | return true; |
| 3216 | |
| 3217 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3218 | if (!Args[I]) |
| 3219 | return true; |
| 3220 | |
| 3221 | return false; |
| 3222 | } |
| 3223 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3224 | void Sema::CodeCompleteCall(Scope *S, ExprTy *FnIn, |
| 3225 | ExprTy **ArgsIn, unsigned NumArgs) { |
| 3226 | if (!CodeCompleter) |
| 3227 | return; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3228 | |
| 3229 | // When we're code-completing for a call, we fall back to ordinary |
| 3230 | // name code-completion whenever we can't produce specific |
| 3231 | // results. We may want to revisit this strategy in the future, |
| 3232 | // e.g., by merging the two kinds of results. |
| 3233 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3234 | Expr *Fn = (Expr *)FnIn; |
| 3235 | Expr **Args = (Expr **)ArgsIn; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3236 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3237 | // Ignore type-dependent call expressions entirely. |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3238 | if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args, NumArgs) || |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3239 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3240 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3241 | return; |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3242 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3243 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3244 | // Build an overload candidate set based on the functions we find. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3245 | SourceLocation Loc = Fn->getExprLoc(); |
| 3246 | OverloadCandidateSet CandidateSet(Loc); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3247 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3248 | // FIXME: What if we're calling something that isn't a function declaration? |
| 3249 | // FIXME: What if we're calling a pseudo-destructor? |
| 3250 | // FIXME: What if we're calling a member function? |
| 3251 | |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3252 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; |
| 3253 | llvm::SmallVector<ResultCandidate, 8> Results; |
| 3254 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3255 | Expr *NakedFn = Fn->IgnoreParenCasts(); |
| 3256 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) |
| 3257 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet, |
| 3258 | /*PartialOverloading=*/ true); |
| 3259 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(NakedFn)) { |
| 3260 | FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3261 | if (FDecl) { |
Douglas Gregor | d28dcd7 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 3262 | if (!getLangOptions().CPlusPlus || |
| 3263 | !FDecl->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3264 | Results.push_back(ResultCandidate(FDecl)); |
| 3265 | else |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3266 | // FIXME: access? |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3267 | AddOverloadCandidate(FDecl, DeclAccessPair::make(FDecl, AS_none), |
| 3268 | Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 3269 | false, /*PartialOverloading*/true); |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3270 | } |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3271 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3272 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3273 | QualType ParamType; |
| 3274 | |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3275 | if (!CandidateSet.empty()) { |
| 3276 | // Sort the overload candidate set by placing the best overloads first. |
| 3277 | std::stable_sort(CandidateSet.begin(), CandidateSet.end(), |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3278 | IsBetterOverloadCandidate(*this, Loc)); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3279 | |
Douglas Gregor | c026540 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 3280 | // Add the remaining viable overload candidates as code-completion reslults. |
| 3281 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3282 | CandEnd = CandidateSet.end(); |
| 3283 | Cand != CandEnd; ++Cand) { |
| 3284 | if (Cand->Viable) |
| 3285 | Results.push_back(ResultCandidate(Cand->Function)); |
| 3286 | } |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3287 | |
| 3288 | // From the viable candidates, try to determine the type of this parameter. |
| 3289 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 3290 | if (const FunctionType *FType = Results[I].getFunctionType()) |
| 3291 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FType)) |
| 3292 | if (NumArgs < Proto->getNumArgs()) { |
| 3293 | if (ParamType.isNull()) |
| 3294 | ParamType = Proto->getArgType(NumArgs); |
| 3295 | else if (!Context.hasSameUnqualifiedType( |
| 3296 | ParamType.getNonReferenceType(), |
| 3297 | Proto->getArgType(NumArgs).getNonReferenceType())) { |
| 3298 | ParamType = QualType(); |
| 3299 | break; |
| 3300 | } |
| 3301 | } |
| 3302 | } |
| 3303 | } else { |
| 3304 | // Try to determine the parameter type from the type of the expression |
| 3305 | // being called. |
| 3306 | QualType FunctionType = Fn->getType(); |
| 3307 | if (const PointerType *Ptr = FunctionType->getAs<PointerType>()) |
| 3308 | FunctionType = Ptr->getPointeeType(); |
| 3309 | else if (const BlockPointerType *BlockPtr |
| 3310 | = FunctionType->getAs<BlockPointerType>()) |
| 3311 | FunctionType = BlockPtr->getPointeeType(); |
| 3312 | else if (const MemberPointerType *MemPtr |
| 3313 | = FunctionType->getAs<MemberPointerType>()) |
| 3314 | FunctionType = MemPtr->getPointeeType(); |
| 3315 | |
| 3316 | if (const FunctionProtoType *Proto |
| 3317 | = FunctionType->getAs<FunctionProtoType>()) { |
| 3318 | if (NumArgs < Proto->getNumArgs()) |
| 3319 | ParamType = Proto->getArgType(NumArgs); |
| 3320 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3321 | } |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3322 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3323 | if (ParamType.isNull()) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3324 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3325 | else |
| 3326 | CodeCompleteExpression(S, ParamType); |
| 3327 | |
Douglas Gregor | 2e4c7a5 | 2010-04-06 20:19:47 +0000 | [diff] [blame] | 3328 | if (!Results.empty()) |
Douglas Gregor | ef96eac | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 3329 | CodeCompleter->ProcessOverloadCandidates(*this, NumArgs, Results.data(), |
| 3330 | Results.size()); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3331 | } |
| 3332 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3333 | void Sema::CodeCompleteInitializer(Scope *S, Decl *D) { |
| 3334 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3335 | if (!VD) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3336 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3337 | return; |
| 3338 | } |
| 3339 | |
| 3340 | CodeCompleteExpression(S, VD->getType()); |
| 3341 | } |
| 3342 | |
| 3343 | void Sema::CodeCompleteReturn(Scope *S) { |
| 3344 | QualType ResultType; |
| 3345 | if (isa<BlockDecl>(CurContext)) { |
| 3346 | if (BlockScopeInfo *BSI = getCurBlock()) |
| 3347 | ResultType = BSI->ReturnType; |
| 3348 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext)) |
| 3349 | ResultType = Function->getResultType(); |
| 3350 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext)) |
| 3351 | ResultType = Method->getResultType(); |
| 3352 | |
| 3353 | if (ResultType.isNull()) |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3354 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3355 | else |
| 3356 | CodeCompleteExpression(S, ResultType); |
| 3357 | } |
| 3358 | |
| 3359 | void Sema::CodeCompleteAssignmentRHS(Scope *S, ExprTy *LHS) { |
| 3360 | if (LHS) |
| 3361 | CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType()); |
| 3362 | else |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3363 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3364 | } |
| 3365 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3366 | void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3367 | bool EnteringContext) { |
| 3368 | if (!SS.getScopeRep() || !CodeCompleter) |
| 3369 | return; |
| 3370 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3371 | DeclContext *Ctx = computeDeclContext(SS, EnteringContext); |
| 3372 | if (!Ctx) |
| 3373 | return; |
Douglas Gregor | d1cd31a | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 3374 | |
| 3375 | // Try to instantiate any non-dependent declaration contexts before |
| 3376 | // we look in them. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3377 | if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx)) |
Douglas Gregor | d1cd31a | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 3378 | return; |
| 3379 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3380 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Name); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3381 | Results.EnterNewScope(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3382 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3383 | // The "template" keyword can follow "::" in the grammar, but only |
| 3384 | // put it into the grammar if the nested-name-specifier is dependent. |
| 3385 | NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 3386 | if (!Results.empty() && NNS->isDependent()) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3387 | Results.AddResult("template"); |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3388 | |
| 3389 | // Add calls to overridden virtual functions, if there are any. |
| 3390 | // |
| 3391 | // FIXME: This isn't wonderful, because we don't know whether we're actually |
| 3392 | // in a context that permits expressions. This is a general issue with |
| 3393 | // qualified-id completions. |
| 3394 | if (!EnteringContext) |
| 3395 | MaybeAddOverrideCalls(*this, Ctx, Results); |
| 3396 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3397 | |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3398 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3399 | LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer); |
| 3400 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3401 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | f696152 | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3402 | CodeCompletionContext::CCC_Name, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3403 | Results.data(),Results.size()); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 3404 | } |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3405 | |
| 3406 | void Sema::CodeCompleteUsing(Scope *S) { |
| 3407 | if (!CodeCompleter) |
| 3408 | return; |
| 3409 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3410 | ResultBuilder Results(*this, |
| 3411 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
| 3412 | &ResultBuilder::IsNestedNameSpecifier); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3413 | Results.EnterNewScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3414 | |
| 3415 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 3416 | if (!S->isClassScope()) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3417 | Results.AddResult(CodeCompletionResult("namespace")); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3418 | |
| 3419 | // After "using", we can see anything that would start a |
| 3420 | // nested-name-specifier. |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3421 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3422 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3423 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3424 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3425 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3426 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3427 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3428 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | void Sema::CodeCompleteUsingDirective(Scope *S) { |
| 3432 | if (!CodeCompleter) |
| 3433 | return; |
| 3434 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3435 | // After "using namespace", we expect to see a namespace name or namespace |
| 3436 | // alias. |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3437 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Namespace, |
| 3438 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3439 | Results.EnterNewScope(); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3440 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3441 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3442 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3443 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3444 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3445 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3446 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { |
| 3450 | if (!CodeCompleter) |
| 3451 | return; |
| 3452 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3453 | DeclContext *Ctx = (DeclContext *)S->getEntity(); |
| 3454 | if (!S->getParent()) |
| 3455 | Ctx = Context.getTranslationUnitDecl(); |
| 3456 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3457 | bool SuppressedGlobalResults |
| 3458 | = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx); |
| 3459 | |
| 3460 | ResultBuilder Results(*this, |
| 3461 | SuppressedGlobalResults |
| 3462 | ? CodeCompletionContext::CCC_Namespace |
| 3463 | : CodeCompletionContext::CCC_Other, |
| 3464 | &ResultBuilder::IsNamespace); |
| 3465 | |
| 3466 | if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) { |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3467 | // We only want to see those namespaces that have already been defined |
| 3468 | // within this scope, because its likely that the user is creating an |
| 3469 | // extended namespace declaration. Keep track of the most recent |
| 3470 | // definition of each namespace. |
| 3471 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
| 3472 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
| 3473 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); |
| 3474 | NS != NSEnd; ++NS) |
| 3475 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
| 3476 | |
| 3477 | // Add the most recent definition (or extended definition) of each |
| 3478 | // namespace to the list of results. |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3479 | Results.EnterNewScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3480 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
| 3481 | NS = OrigToLatest.begin(), NSEnd = OrigToLatest.end(); |
| 3482 | NS != NSEnd; ++NS) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3483 | Results.AddResult(CodeCompletionResult(NS->second, 0), |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 3484 | CurContext, 0, false); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3485 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3486 | } |
| 3487 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3488 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3489 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3490 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { |
| 3494 | if (!CodeCompleter) |
| 3495 | return; |
| 3496 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3497 | // After "namespace", we expect to see a namespace or alias. |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3498 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Namespace, |
| 3499 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3500 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3501 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3502 | CodeCompleter->includeGlobals()); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3503 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3504 | Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3505 | Results.data(),Results.size()); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3508 | void Sema::CodeCompleteOperatorName(Scope *S) { |
| 3509 | if (!CodeCompleter) |
| 3510 | return; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3511 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3512 | typedef CodeCompletionResult Result; |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3513 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Type, |
| 3514 | &ResultBuilder::IsType); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3515 | Results.EnterNewScope(); |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3516 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3517 | // Add the names of overloadable operators. |
| 3518 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 3519 | if (std::strcmp(Spelling, "?")) \ |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3520 | Results.AddResult(Result(Spelling)); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3521 | #include "clang/Basic/OperatorKinds.def" |
| 3522 | |
| 3523 | // Add any type names visible from the current scope |
Douglas Gregor | 45bcd43 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 3524 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 5d2fc40 | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 3525 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3526 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 3527 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3528 | |
| 3529 | // Add any type specifiers |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3530 | AddTypeSpecifierResults(getLangOptions(), Results); |
Douglas Gregor | 8e0a0e4 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 3531 | Results.ExitScope(); |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3532 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3533 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3534 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3535 | Results.data(),Results.size()); |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 3536 | } |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 3537 | |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3538 | void Sema::CodeCompleteConstructorInitializer(Decl *ConstructorD, |
| 3539 | CXXBaseOrMemberInitializer** Initializers, |
| 3540 | unsigned NumInitializers) { |
| 3541 | CXXConstructorDecl *Constructor |
| 3542 | = static_cast<CXXConstructorDecl *>(ConstructorD); |
| 3543 | if (!Constructor) |
| 3544 | return; |
| 3545 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3546 | ResultBuilder Results(*this, |
| 3547 | CodeCompletionContext::CCC_PotentiallyQualifiedName); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3548 | Results.EnterNewScope(); |
| 3549 | |
| 3550 | // Fill in any already-initialized fields or base classes. |
| 3551 | llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields; |
| 3552 | llvm::SmallPtrSet<CanQualType, 4> InitializedBases; |
| 3553 | for (unsigned I = 0; I != NumInitializers; ++I) { |
| 3554 | if (Initializers[I]->isBaseInitializer()) |
| 3555 | InitializedBases.insert( |
| 3556 | Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0))); |
| 3557 | else |
| 3558 | InitializedFields.insert(cast<FieldDecl>(Initializers[I]->getMember())); |
| 3559 | } |
| 3560 | |
| 3561 | // Add completions for base classes. |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3562 | bool SawLastInitializer = (NumInitializers == 0); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3563 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 3564 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3565 | BaseEnd = ClassDecl->bases_end(); |
| 3566 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3567 | if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) { |
| 3568 | SawLastInitializer |
| 3569 | = NumInitializers > 0 && |
| 3570 | Initializers[NumInitializers - 1]->isBaseInitializer() && |
| 3571 | Context.hasSameUnqualifiedType(Base->getType(), |
| 3572 | QualType(Initializers[NumInitializers - 1]->getBaseClass(), 0)); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3573 | continue; |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3574 | } |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3575 | |
| 3576 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 3577 | Pattern->AddTypedTextChunk( |
| 3578 | Base->getType().getAsString(Context.PrintingPolicy)); |
| 3579 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 3580 | Pattern->AddPlaceholderChunk("args"); |
| 3581 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3582 | Results.AddResult(CodeCompletionResult(Pattern, |
| 3583 | SawLastInitializer? CCP_NextInitializer |
| 3584 | : CCP_MemberDeclaration)); |
| 3585 | SawLastInitializer = false; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
| 3588 | // Add completions for virtual base classes. |
| 3589 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 3590 | BaseEnd = ClassDecl->vbases_end(); |
| 3591 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3592 | if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) { |
| 3593 | SawLastInitializer |
| 3594 | = NumInitializers > 0 && |
| 3595 | Initializers[NumInitializers - 1]->isBaseInitializer() && |
| 3596 | Context.hasSameUnqualifiedType(Base->getType(), |
| 3597 | QualType(Initializers[NumInitializers - 1]->getBaseClass(), 0)); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3598 | continue; |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3599 | } |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3600 | |
| 3601 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 3602 | Pattern->AddTypedTextChunk( |
| 3603 | Base->getType().getAsString(Context.PrintingPolicy)); |
| 3604 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 3605 | Pattern->AddPlaceholderChunk("args"); |
| 3606 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3607 | Results.AddResult(CodeCompletionResult(Pattern, |
| 3608 | SawLastInitializer? CCP_NextInitializer |
| 3609 | : CCP_MemberDeclaration)); |
| 3610 | SawLastInitializer = false; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | // Add completions for members. |
| 3614 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 3615 | FieldEnd = ClassDecl->field_end(); |
| 3616 | Field != FieldEnd; ++Field) { |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3617 | if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl()))) { |
| 3618 | SawLastInitializer |
| 3619 | = NumInitializers > 0 && |
| 3620 | Initializers[NumInitializers - 1]->isMemberInitializer() && |
| 3621 | Initializers[NumInitializers - 1]->getMember() == *Field; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3622 | continue; |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3623 | } |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3624 | |
| 3625 | if (!Field->getDeclName()) |
| 3626 | continue; |
| 3627 | |
| 3628 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 3629 | Pattern->AddTypedTextChunk(Field->getIdentifier()->getName()); |
| 3630 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 3631 | Pattern->AddPlaceholderChunk("args"); |
| 3632 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3633 | Results.AddResult(CodeCompletionResult(Pattern, |
| 3634 | SawLastInitializer? CCP_NextInitializer |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3635 | : CCP_MemberDeclaration, |
| 3636 | CXCursor_MemberRef)); |
Douglas Gregor | 0c431c8 | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 3637 | SawLastInitializer = false; |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3638 | } |
| 3639 | Results.ExitScope(); |
| 3640 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3641 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3642 | Results.data(), Results.size()); |
| 3643 | } |
| 3644 | |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3645 | // Macro that expands to @Keyword or Keyword, depending on whether NeedAt is |
| 3646 | // true or false. |
| 3647 | #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) NeedAt? "@" #Keyword : #Keyword |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3648 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3649 | ResultBuilder &Results, |
| 3650 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3651 | typedef CodeCompletionResult Result; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3652 | // Since we have an implementation, we can end it. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3653 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3654 | |
| 3655 | CodeCompletionString *Pattern = 0; |
| 3656 | if (LangOpts.ObjC2) { |
| 3657 | // @dynamic |
| 3658 | Pattern = new CodeCompletionString; |
| 3659 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,dynamic)); |
| 3660 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3661 | Pattern->AddPlaceholderChunk("property"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3662 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3663 | |
| 3664 | // @synthesize |
| 3665 | Pattern = new CodeCompletionString; |
| 3666 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synthesize)); |
| 3667 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3668 | Pattern->AddPlaceholderChunk("property"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3669 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3670 | } |
| 3671 | } |
| 3672 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3673 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3674 | ResultBuilder &Results, |
| 3675 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3676 | typedef CodeCompletionResult Result; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3677 | |
| 3678 | // Since we have an interface or protocol, we can end it. |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3679 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3680 | |
| 3681 | if (LangOpts.ObjC2) { |
| 3682 | // @property |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3683 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,property))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3684 | |
| 3685 | // @required |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3686 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,required))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3687 | |
| 3688 | // @optional |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3689 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,optional))); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3690 | } |
| 3691 | } |
| 3692 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3693 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3694 | typedef CodeCompletionResult Result; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3695 | CodeCompletionString *Pattern = 0; |
| 3696 | |
| 3697 | // @class name ; |
| 3698 | Pattern = new CodeCompletionString; |
| 3699 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,class)); |
| 3700 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3701 | Pattern->AddPlaceholderChunk("name"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3702 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3703 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3704 | if (Results.includeCodePatterns()) { |
| 3705 | // @interface name |
| 3706 | // FIXME: Could introduce the whole pattern, including superclasses and |
| 3707 | // such. |
| 3708 | Pattern = new CodeCompletionString; |
| 3709 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,interface)); |
| 3710 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3711 | Pattern->AddPlaceholderChunk("class"); |
| 3712 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3713 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3714 | // @protocol name |
| 3715 | Pattern = new CodeCompletionString; |
| 3716 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol)); |
| 3717 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3718 | Pattern->AddPlaceholderChunk("protocol"); |
| 3719 | Results.AddResult(Result(Pattern)); |
| 3720 | |
| 3721 | // @implementation name |
| 3722 | Pattern = new CodeCompletionString; |
| 3723 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,implementation)); |
| 3724 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3725 | Pattern->AddPlaceholderChunk("class"); |
| 3726 | Results.AddResult(Result(Pattern)); |
| 3727 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3728 | |
| 3729 | // @compatibility_alias name |
| 3730 | Pattern = new CodeCompletionString; |
| 3731 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,compatibility_alias)); |
| 3732 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3733 | Pattern->AddPlaceholderChunk("alias"); |
| 3734 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3735 | Pattern->AddPlaceholderChunk("class"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3736 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3739 | void Sema::CodeCompleteObjCAtDirective(Scope *S, Decl *ObjCImpDecl, |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 3740 | bool InInterface) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3741 | typedef CodeCompletionResult Result; |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3742 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 3743 | Results.EnterNewScope(); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3744 | if (ObjCImpDecl) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3745 | AddObjCImplementationResults(getLangOptions(), Results, false); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3746 | else if (InInterface) |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3747 | AddObjCInterfaceResults(getLangOptions(), Results, false); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3748 | else |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3749 | AddObjCTopLevelResults(Results, false); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 3750 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3751 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3752 | CodeCompletionContext::CCC_Other, |
| 3753 | Results.data(),Results.size()); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 3754 | } |
| 3755 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3756 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3757 | typedef CodeCompletionResult Result; |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3758 | CodeCompletionString *Pattern = 0; |
| 3759 | |
| 3760 | // @encode ( type-name ) |
| 3761 | Pattern = new CodeCompletionString; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3762 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,encode)); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3763 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 3764 | Pattern->AddPlaceholderChunk("type-name"); |
| 3765 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3766 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3767 | |
| 3768 | // @protocol ( protocol-name ) |
| 3769 | Pattern = new CodeCompletionString; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3770 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol)); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3771 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 3772 | Pattern->AddPlaceholderChunk("protocol-name"); |
| 3773 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3774 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3775 | |
| 3776 | // @selector ( selector ) |
| 3777 | Pattern = new CodeCompletionString; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3778 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,selector)); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3779 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 3780 | Pattern->AddPlaceholderChunk("selector"); |
| 3781 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3782 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3783 | } |
| 3784 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3785 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3786 | typedef CodeCompletionResult Result; |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3787 | CodeCompletionString *Pattern = 0; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3788 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3789 | if (Results.includeCodePatterns()) { |
| 3790 | // @try { statements } @catch ( declaration ) { statements } @finally |
| 3791 | // { statements } |
| 3792 | Pattern = new CodeCompletionString; |
| 3793 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,try)); |
| 3794 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3795 | Pattern->AddPlaceholderChunk("statements"); |
| 3796 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 3797 | Pattern->AddTextChunk("@catch"); |
| 3798 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 3799 | Pattern->AddPlaceholderChunk("parameter"); |
| 3800 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 3801 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3802 | Pattern->AddPlaceholderChunk("statements"); |
| 3803 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 3804 | Pattern->AddTextChunk("@finally"); |
| 3805 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3806 | Pattern->AddPlaceholderChunk("statements"); |
| 3807 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 3808 | Results.AddResult(Result(Pattern)); |
| 3809 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3810 | |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3811 | // @throw |
| 3812 | Pattern = new CodeCompletionString; |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3813 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,throw)); |
Douglas Gregor | 834389b | 2010-01-12 06:38:28 +0000 | [diff] [blame] | 3814 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3815 | Pattern->AddPlaceholderChunk("expression"); |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3816 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3817 | |
Douglas Gregor | c8bddde | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 3818 | if (Results.includeCodePatterns()) { |
| 3819 | // @synchronized ( expression ) { statements } |
| 3820 | Pattern = new CodeCompletionString; |
| 3821 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synchronized)); |
| 3822 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 3823 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 3824 | Pattern->AddPlaceholderChunk("expression"); |
| 3825 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 3826 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 3827 | Pattern->AddPlaceholderChunk("statements"); |
| 3828 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 3829 | Results.AddResult(Result(Pattern)); |
| 3830 | } |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3831 | } |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3832 | |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3833 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3834 | ResultBuilder &Results, |
| 3835 | bool NeedAt) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3836 | typedef CodeCompletionResult Result; |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3837 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,private))); |
| 3838 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,protected))); |
| 3839 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,public))); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3840 | if (LangOpts.ObjC2) |
Douglas Gregor | a447781 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 3841 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,package))); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
| 3844 | void Sema::CodeCompleteObjCAtVisibility(Scope *S) { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3845 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3846 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3847 | AddObjCVisibilityResults(getLangOptions(), Results, false); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3848 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3849 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3850 | CodeCompletionContext::CCC_Other, |
| 3851 | Results.data(),Results.size()); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
| 3854 | void Sema::CodeCompleteObjCAtStatement(Scope *S) { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3855 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 3856 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3857 | AddObjCStatementResults(Results, false); |
| 3858 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3859 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3860 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3861 | CodeCompletionContext::CCC_Other, |
| 3862 | Results.data(),Results.size()); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3863 | } |
| 3864 | |
| 3865 | void Sema::CodeCompleteObjCAtExpression(Scope *S) { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3866 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3867 | Results.EnterNewScope(); |
Douglas Gregor | bca403c | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3868 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3869 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3870 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3871 | CodeCompletionContext::CCC_Other, |
| 3872 | Results.data(),Results.size()); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 3873 | } |
| 3874 | |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3875 | /// \brief Determine whether the addition of the given flag to an Objective-C |
| 3876 | /// property's attributes will cause a conflict. |
| 3877 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { |
| 3878 | // Check if we've already added this flag. |
| 3879 | if (Attributes & NewFlag) |
| 3880 | return true; |
| 3881 | |
| 3882 | Attributes |= NewFlag; |
| 3883 | |
| 3884 | // Check for collisions with "readonly". |
| 3885 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 3886 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | |
| 3887 | ObjCDeclSpec::DQ_PR_assign | |
| 3888 | ObjCDeclSpec::DQ_PR_copy | |
| 3889 | ObjCDeclSpec::DQ_PR_retain))) |
| 3890 | return true; |
| 3891 | |
| 3892 | // Check for more than one of { assign, copy, retain }. |
| 3893 | unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign | |
| 3894 | ObjCDeclSpec::DQ_PR_copy | |
| 3895 | ObjCDeclSpec::DQ_PR_retain); |
| 3896 | if (AssignCopyRetMask && |
| 3897 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && |
| 3898 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && |
| 3899 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain) |
| 3900 | return true; |
| 3901 | |
| 3902 | return false; |
| 3903 | } |
| 3904 | |
Douglas Gregor | a93b108 | 2009-11-18 23:08:07 +0000 | [diff] [blame] | 3905 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 3906 | if (!CodeCompleter) |
| 3907 | return; |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3908 | |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 3909 | unsigned Attributes = ODS.getPropertyAttributes(); |
| 3910 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3911 | typedef CodeCompletionResult Result; |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3912 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 3913 | Results.EnterNewScope(); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3914 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3915 | Results.AddResult(CodeCompletionResult("readonly")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3916 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3917 | Results.AddResult(CodeCompletionResult("assign")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3918 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3919 | Results.AddResult(CodeCompletionResult("readwrite")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3920 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3921 | Results.AddResult(CodeCompletionResult("retain")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3922 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3923 | Results.AddResult(CodeCompletionResult("copy")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3924 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3925 | Results.AddResult(CodeCompletionResult("nonatomic")); |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3926 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 3927 | CodeCompletionString *Setter = new CodeCompletionString; |
| 3928 | Setter->AddTypedTextChunk("setter"); |
| 3929 | Setter->AddTextChunk(" = "); |
| 3930 | Setter->AddPlaceholderChunk("method"); |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3931 | Results.AddResult(CodeCompletionResult(Setter)); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 3932 | } |
Douglas Gregor | 988358f | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 3933 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 3934 | CodeCompletionString *Getter = new CodeCompletionString; |
| 3935 | Getter->AddTypedTextChunk("getter"); |
| 3936 | Getter->AddTextChunk(" = "); |
| 3937 | Getter->AddPlaceholderChunk("method"); |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3938 | Results.AddResult(CodeCompletionResult(Getter)); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 3939 | } |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 3940 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3941 | HandleCodeCompleteResults(this, CodeCompleter, |
| 3942 | CodeCompletionContext::CCC_Other, |
| 3943 | Results.data(),Results.size()); |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 3944 | } |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 3945 | |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3946 | /// \brief Descripts the kind of Objective-C method that we want to find |
| 3947 | /// via code completion. |
| 3948 | enum ObjCMethodKind { |
| 3949 | MK_Any, //< Any kind of method, provided it means other specified criteria. |
| 3950 | MK_ZeroArgSelector, //< Zero-argument (unary) selector. |
| 3951 | MK_OneArgSelector //< One-argument selector. |
| 3952 | }; |
| 3953 | |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 3954 | static bool isAcceptableObjCSelector(Selector Sel, |
| 3955 | ObjCMethodKind WantKind, |
| 3956 | IdentifierInfo **SelIdents, |
| 3957 | unsigned NumSelIdents) { |
| 3958 | if (NumSelIdents > Sel.getNumArgs()) |
| 3959 | return false; |
| 3960 | |
| 3961 | switch (WantKind) { |
| 3962 | case MK_Any: break; |
| 3963 | case MK_ZeroArgSelector: return Sel.isUnarySelector(); |
| 3964 | case MK_OneArgSelector: return Sel.getNumArgs() == 1; |
| 3965 | } |
| 3966 | |
| 3967 | for (unsigned I = 0; I != NumSelIdents; ++I) |
| 3968 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) |
| 3969 | return false; |
| 3970 | |
| 3971 | return true; |
| 3972 | } |
| 3973 | |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3974 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, |
| 3975 | ObjCMethodKind WantKind, |
| 3976 | IdentifierInfo **SelIdents, |
| 3977 | unsigned NumSelIdents) { |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 3978 | return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents, |
| 3979 | NumSelIdents); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3980 | } |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 3981 | |
| 3982 | namespace { |
| 3983 | /// \brief A set of selectors, which is used to avoid introducing multiple |
| 3984 | /// completions with the same selector into the result set. |
| 3985 | typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet; |
| 3986 | } |
| 3987 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3988 | /// \brief Add all of the Objective-C methods in the given Objective-C |
| 3989 | /// container to the set of results. |
| 3990 | /// |
| 3991 | /// The container will be a class, protocol, category, or implementation of |
| 3992 | /// any of the above. This mether will recurse to include methods from |
| 3993 | /// the superclasses of classes along with their categories, protocols, and |
| 3994 | /// implementations. |
| 3995 | /// |
| 3996 | /// \param Container the container in which we'll look to find methods. |
| 3997 | /// |
| 3998 | /// \param WantInstance whether to add instance methods (only); if false, this |
| 3999 | /// routine will add factory methods (only). |
| 4000 | /// |
| 4001 | /// \param CurContext the context in which we're performing the lookup that |
| 4002 | /// finds methods. |
| 4003 | /// |
| 4004 | /// \param Results the structure into which we'll add results. |
| 4005 | static void AddObjCMethods(ObjCContainerDecl *Container, |
| 4006 | bool WantInstanceMethods, |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4007 | ObjCMethodKind WantKind, |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4008 | IdentifierInfo **SelIdents, |
| 4009 | unsigned NumSelIdents, |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4010 | DeclContext *CurContext, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4011 | VisitedSelectorSet &Selectors, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4012 | ResultBuilder &Results, |
| 4013 | bool InOriginalClass = true) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4014 | typedef CodeCompletionResult Result; |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4015 | for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), |
| 4016 | MEnd = Container->meth_end(); |
| 4017 | M != MEnd; ++M) { |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4018 | if ((*M)->isInstanceMethod() == WantInstanceMethods) { |
| 4019 | // Check whether the selector identifiers we've been given are a |
| 4020 | // subset of the identifiers for this particular method. |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4021 | if (!isAcceptableObjCMethod(*M, WantKind, SelIdents, NumSelIdents)) |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4022 | continue; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4023 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4024 | if (!Selectors.insert((*M)->getSelector())) |
| 4025 | continue; |
| 4026 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4027 | Result R = Result(*M, 0); |
| 4028 | R.StartParameter = NumSelIdents; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4029 | R.AllParametersAreInformative = (WantKind != MK_Any); |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 4030 | if (!InOriginalClass) |
| 4031 | R.Priority += CCD_InBaseClass; |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4032 | Results.MaybeAddResult(R, CurContext); |
| 4033 | } |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4034 | } |
| 4035 | |
Douglas Gregor | e396c7b | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 4036 | // Visit the protocols of protocols. |
| 4037 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 4038 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 4039 | = Protocol->getReferencedProtocols(); |
| 4040 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 4041 | E = Protocols.end(); |
| 4042 | I != E; ++I) |
| 4043 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4044 | CurContext, Selectors, Results, false); |
Douglas Gregor | e396c7b | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4047 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); |
| 4048 | if (!IFace) |
| 4049 | return; |
| 4050 | |
| 4051 | // Add methods in protocols. |
| 4052 | const ObjCList<ObjCProtocolDecl> &Protocols= IFace->getReferencedProtocols(); |
| 4053 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 4054 | E = Protocols.end(); |
| 4055 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4056 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4057 | CurContext, Selectors, Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4058 | |
| 4059 | // Add methods in categories. |
| 4060 | for (ObjCCategoryDecl *CatDecl = IFace->getCategoryList(); CatDecl; |
| 4061 | CatDecl = CatDecl->getNextClassCategory()) { |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4062 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4063 | NumSelIdents, CurContext, Selectors, Results, |
| 4064 | InOriginalClass); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4065 | |
| 4066 | // Add a categories protocol methods. |
| 4067 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 4068 | = CatDecl->getReferencedProtocols(); |
| 4069 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 4070 | E = Protocols.end(); |
| 4071 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4072 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4073 | NumSelIdents, CurContext, Selectors, Results, false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4074 | |
| 4075 | // Add methods in category implementations. |
| 4076 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4077 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4078 | NumSelIdents, CurContext, Selectors, Results, |
| 4079 | InOriginalClass); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | // Add methods in superclass. |
| 4083 | if (IFace->getSuperClass()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4084 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4085 | SelIdents, NumSelIdents, CurContext, Selectors, Results, |
| 4086 | false); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4087 | |
| 4088 | // Add methods in our implementation, if any. |
| 4089 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4090 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4091 | NumSelIdents, CurContext, Selectors, Results, |
| 4092 | InOriginalClass); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4093 | } |
| 4094 | |
| 4095 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4096 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S, Decl *ClassDecl, |
| 4097 | Decl **Methods, |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4098 | unsigned NumMethods) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4099 | typedef CodeCompletionResult Result; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4100 | |
| 4101 | // Try to find the interface where getters might live. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4102 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(ClassDecl); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4103 | if (!Class) { |
| 4104 | if (ObjCCategoryDecl *Category |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4105 | = dyn_cast_or_null<ObjCCategoryDecl>(ClassDecl)) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4106 | Class = Category->getClassInterface(); |
| 4107 | |
| 4108 | if (!Class) |
| 4109 | return; |
| 4110 | } |
| 4111 | |
| 4112 | // Find all of the potential getters. |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4113 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4114 | Results.EnterNewScope(); |
| 4115 | |
| 4116 | // FIXME: We need to do this because Objective-C methods don't get |
| 4117 | // pushed into DeclContexts early enough. Argh! |
| 4118 | for (unsigned I = 0; I != NumMethods; ++I) { |
| 4119 | if (ObjCMethodDecl *Method |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4120 | = dyn_cast_or_null<ObjCMethodDecl>(Methods[I])) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4121 | if (Method->isInstanceMethod() && |
| 4122 | isAcceptableObjCMethod(Method, MK_ZeroArgSelector, 0, 0)) { |
| 4123 | Result R = Result(Method, 0); |
| 4124 | R.AllParametersAreInformative = true; |
| 4125 | Results.MaybeAddResult(R, CurContext); |
| 4126 | } |
| 4127 | } |
| 4128 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4129 | VisitedSelectorSet Selectors; |
| 4130 | AddObjCMethods(Class, true, MK_ZeroArgSelector, 0, 0, CurContext, Selectors, |
| 4131 | Results); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4132 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4133 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4134 | CodeCompletionContext::CCC_Other, |
| 4135 | Results.data(),Results.size()); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4136 | } |
| 4137 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4138 | void Sema::CodeCompleteObjCPropertySetter(Scope *S, Decl *ObjCImplDecl, |
| 4139 | Decl **Methods, |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4140 | unsigned NumMethods) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4141 | typedef CodeCompletionResult Result; |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4142 | |
| 4143 | // Try to find the interface where setters might live. |
| 4144 | ObjCInterfaceDecl *Class |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4145 | = dyn_cast_or_null<ObjCInterfaceDecl>(ObjCImplDecl); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4146 | if (!Class) { |
| 4147 | if (ObjCCategoryDecl *Category |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4148 | = dyn_cast_or_null<ObjCCategoryDecl>(ObjCImplDecl)) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4149 | Class = Category->getClassInterface(); |
| 4150 | |
| 4151 | if (!Class) |
| 4152 | return; |
| 4153 | } |
| 4154 | |
| 4155 | // Find all of the potential getters. |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4156 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4157 | Results.EnterNewScope(); |
| 4158 | |
| 4159 | // FIXME: We need to do this because Objective-C methods don't get |
| 4160 | // pushed into DeclContexts early enough. Argh! |
| 4161 | for (unsigned I = 0; I != NumMethods; ++I) { |
| 4162 | if (ObjCMethodDecl *Method |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4163 | = dyn_cast_or_null<ObjCMethodDecl>(Methods[I])) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4164 | if (Method->isInstanceMethod() && |
| 4165 | isAcceptableObjCMethod(Method, MK_OneArgSelector, 0, 0)) { |
| 4166 | Result R = Result(Method, 0); |
| 4167 | R.AllParametersAreInformative = true; |
| 4168 | Results.MaybeAddResult(R, CurContext); |
| 4169 | } |
| 4170 | } |
| 4171 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4172 | VisitedSelectorSet Selectors; |
| 4173 | AddObjCMethods(Class, true, MK_OneArgSelector, 0, 0, CurContext, |
| 4174 | Selectors, Results); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4175 | |
| 4176 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4177 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4178 | CodeCompletionContext::CCC_Other, |
| 4179 | Results.data(),Results.size()); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4180 | } |
| 4181 | |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 4182 | void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4183 | typedef CodeCompletionResult Result; |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4184 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Type); |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 4185 | Results.EnterNewScope(); |
| 4186 | |
| 4187 | // Add context-sensitive, Objective-C parameter-passing keywords. |
| 4188 | bool AddedInOut = false; |
| 4189 | if ((DS.getObjCDeclQualifier() & |
| 4190 | (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 4191 | Results.AddResult("in"); |
| 4192 | Results.AddResult("inout"); |
| 4193 | AddedInOut = true; |
| 4194 | } |
| 4195 | if ((DS.getObjCDeclQualifier() & |
| 4196 | (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 4197 | Results.AddResult("out"); |
| 4198 | if (!AddedInOut) |
| 4199 | Results.AddResult("inout"); |
| 4200 | } |
| 4201 | if ((DS.getObjCDeclQualifier() & |
| 4202 | (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref | |
| 4203 | ObjCDeclSpec::DQ_Oneway)) == 0) { |
| 4204 | Results.AddResult("bycopy"); |
| 4205 | Results.AddResult("byref"); |
| 4206 | Results.AddResult("oneway"); |
| 4207 | } |
| 4208 | |
| 4209 | // Add various builtin type names and specifiers. |
| 4210 | AddOrdinaryNameResults(PCC_Type, S, *this, Results); |
| 4211 | Results.ExitScope(); |
| 4212 | |
| 4213 | // Add the various type names |
| 4214 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 4215 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4216 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4217 | CodeCompleter->includeGlobals()); |
| 4218 | |
| 4219 | if (CodeCompleter->includeMacros()) |
| 4220 | AddMacroResults(PP, Results); |
| 4221 | |
| 4222 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4223 | CodeCompletionContext::CCC_Type, |
| 4224 | Results.data(), Results.size()); |
| 4225 | } |
| 4226 | |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4227 | /// \brief When we have an expression with type "id", we may assume |
| 4228 | /// that it has some more-specific class type based on knowledge of |
| 4229 | /// common uses of Objective-C. This routine returns that class type, |
| 4230 | /// or NULL if no better result could be determined. |
| 4231 | static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) { |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 4232 | ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E); |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4233 | if (!Msg) |
| 4234 | return 0; |
| 4235 | |
| 4236 | Selector Sel = Msg->getSelector(); |
| 4237 | if (Sel.isNull()) |
| 4238 | return 0; |
| 4239 | |
| 4240 | IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0); |
| 4241 | if (!Id) |
| 4242 | return 0; |
| 4243 | |
| 4244 | ObjCMethodDecl *Method = Msg->getMethodDecl(); |
| 4245 | if (!Method) |
| 4246 | return 0; |
| 4247 | |
| 4248 | // Determine the class that we're sending the message to. |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 4249 | ObjCInterfaceDecl *IFace = 0; |
| 4250 | switch (Msg->getReceiverKind()) { |
| 4251 | case ObjCMessageExpr::Class: |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4252 | if (const ObjCObjectType *ObjType |
| 4253 | = Msg->getClassReceiver()->getAs<ObjCObjectType>()) |
| 4254 | IFace = ObjType->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 4255 | break; |
| 4256 | |
| 4257 | case ObjCMessageExpr::Instance: { |
| 4258 | QualType T = Msg->getInstanceReceiver()->getType(); |
| 4259 | if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) |
| 4260 | IFace = Ptr->getInterfaceDecl(); |
| 4261 | break; |
| 4262 | } |
| 4263 | |
| 4264 | case ObjCMessageExpr::SuperInstance: |
| 4265 | case ObjCMessageExpr::SuperClass: |
| 4266 | break; |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | if (!IFace) |
| 4270 | return 0; |
| 4271 | |
| 4272 | ObjCInterfaceDecl *Super = IFace->getSuperClass(); |
| 4273 | if (Method->isInstanceMethod()) |
| 4274 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 4275 | .Case("retain", IFace) |
| 4276 | .Case("autorelease", IFace) |
| 4277 | .Case("copy", IFace) |
| 4278 | .Case("copyWithZone", IFace) |
| 4279 | .Case("mutableCopy", IFace) |
| 4280 | .Case("mutableCopyWithZone", IFace) |
| 4281 | .Case("awakeFromCoder", IFace) |
| 4282 | .Case("replacementObjectFromCoder", IFace) |
| 4283 | .Case("class", IFace) |
| 4284 | .Case("classForCoder", IFace) |
| 4285 | .Case("superclass", Super) |
| 4286 | .Default(0); |
| 4287 | |
| 4288 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 4289 | .Case("new", IFace) |
| 4290 | .Case("alloc", IFace) |
| 4291 | .Case("allocWithZone", IFace) |
| 4292 | .Case("class", IFace) |
| 4293 | .Case("superclass", Super) |
| 4294 | .Default(0); |
| 4295 | } |
| 4296 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4297 | // Add a special completion for a message send to "super", which fills in the |
| 4298 | // most likely case of forwarding all of our arguments to the superclass |
| 4299 | // function. |
| 4300 | /// |
| 4301 | /// \param S The semantic analysis object. |
| 4302 | /// |
| 4303 | /// \param S NeedSuperKeyword Whether we need to prefix this completion with |
| 4304 | /// the "super" keyword. Otherwise, we just need to provide the arguments. |
| 4305 | /// |
| 4306 | /// \param SelIdents The identifiers in the selector that have already been |
| 4307 | /// provided as arguments for a send to "super". |
| 4308 | /// |
| 4309 | /// \param NumSelIdents The number of identifiers in \p SelIdents. |
| 4310 | /// |
| 4311 | /// \param Results The set of results to augment. |
| 4312 | /// |
| 4313 | /// \returns the Objective-C method declaration that would be invoked by |
| 4314 | /// this "super" completion. If NULL, no completion was added. |
| 4315 | static ObjCMethodDecl *AddSuperSendCompletion(Sema &S, bool NeedSuperKeyword, |
| 4316 | IdentifierInfo **SelIdents, |
| 4317 | unsigned NumSelIdents, |
| 4318 | ResultBuilder &Results) { |
| 4319 | ObjCMethodDecl *CurMethod = S.getCurMethodDecl(); |
| 4320 | if (!CurMethod) |
| 4321 | return 0; |
| 4322 | |
| 4323 | ObjCInterfaceDecl *Class = CurMethod->getClassInterface(); |
| 4324 | if (!Class) |
| 4325 | return 0; |
| 4326 | |
| 4327 | // Try to find a superclass method with the same selector. |
| 4328 | ObjCMethodDecl *SuperMethod = 0; |
| 4329 | while ((Class = Class->getSuperClass()) && !SuperMethod) |
| 4330 | SuperMethod = Class->getMethod(CurMethod->getSelector(), |
| 4331 | CurMethod->isInstanceMethod()); |
| 4332 | |
| 4333 | if (!SuperMethod) |
| 4334 | return 0; |
| 4335 | |
| 4336 | // Check whether the superclass method has the same signature. |
| 4337 | if (CurMethod->param_size() != SuperMethod->param_size() || |
| 4338 | CurMethod->isVariadic() != SuperMethod->isVariadic()) |
| 4339 | return 0; |
| 4340 | |
| 4341 | for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(), |
| 4342 | CurPEnd = CurMethod->param_end(), |
| 4343 | SuperP = SuperMethod->param_begin(); |
| 4344 | CurP != CurPEnd; ++CurP, ++SuperP) { |
| 4345 | // Make sure the parameter types are compatible. |
| 4346 | if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(), |
| 4347 | (*SuperP)->getType())) |
| 4348 | return 0; |
| 4349 | |
| 4350 | // Make sure we have a parameter name to forward! |
| 4351 | if (!(*CurP)->getIdentifier()) |
| 4352 | return 0; |
| 4353 | } |
| 4354 | |
| 4355 | // We have a superclass method. Now, form the send-to-super completion. |
| 4356 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 4357 | |
| 4358 | // Give this completion a return type. |
| 4359 | AddResultTypeChunk(S.Context, SuperMethod, Pattern); |
| 4360 | |
| 4361 | // If we need the "super" keyword, add it (plus some spacing). |
| 4362 | if (NeedSuperKeyword) { |
| 4363 | Pattern->AddTypedTextChunk("super"); |
| 4364 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4365 | } |
| 4366 | |
| 4367 | Selector Sel = CurMethod->getSelector(); |
| 4368 | if (Sel.isUnarySelector()) { |
| 4369 | if (NeedSuperKeyword) |
| 4370 | Pattern->AddTextChunk(Sel.getIdentifierInfoForSlot(0)->getName()); |
| 4371 | else |
| 4372 | Pattern->AddTypedTextChunk(Sel.getIdentifierInfoForSlot(0)->getName()); |
| 4373 | } else { |
| 4374 | ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(); |
| 4375 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) { |
| 4376 | if (I > NumSelIdents) |
| 4377 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4378 | |
| 4379 | if (I < NumSelIdents) |
| 4380 | Pattern->AddInformativeChunk( |
| 4381 | Sel.getIdentifierInfoForSlot(I)->getName().str() + ":"); |
| 4382 | else if (NeedSuperKeyword || I > NumSelIdents) { |
| 4383 | Pattern->AddTextChunk( |
| 4384 | Sel.getIdentifierInfoForSlot(I)->getName().str() + ":"); |
| 4385 | Pattern->AddPlaceholderChunk((*CurP)->getIdentifier()->getName()); |
| 4386 | } else { |
| 4387 | Pattern->AddTypedTextChunk( |
| 4388 | Sel.getIdentifierInfoForSlot(I)->getName().str() + ":"); |
| 4389 | Pattern->AddPlaceholderChunk((*CurP)->getIdentifier()->getName()); |
| 4390 | } |
| 4391 | } |
| 4392 | } |
| 4393 | |
| 4394 | Results.AddResult(CodeCompletionResult(Pattern, CCP_SuperCompletion, |
| 4395 | SuperMethod->isInstanceMethod() |
| 4396 | ? CXCursor_ObjCInstanceMethodDecl |
| 4397 | : CXCursor_ObjCClassMethodDecl)); |
| 4398 | return SuperMethod; |
| 4399 | } |
| 4400 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4401 | void Sema::CodeCompleteObjCMessageReceiver(Scope *S) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4402 | typedef CodeCompletionResult Result; |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4403 | ResultBuilder Results(*this, CodeCompletionContext::CCC_ObjCMessageReceiver, |
| 4404 | &ResultBuilder::IsObjCMessageReceiver); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4405 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4406 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4407 | Results.EnterNewScope(); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4408 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| 4409 | CodeCompleter->includeGlobals()); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4410 | |
| 4411 | // If we are in an Objective-C method inside a class that has a superclass, |
| 4412 | // add "super" as an option. |
| 4413 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 4414 | if (ObjCInterfaceDecl *Iface = Method->getClassInterface()) |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4415 | if (Iface->getSuperClass()) { |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4416 | Results.AddResult(Result("super")); |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4417 | |
| 4418 | AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, 0, 0, Results); |
| 4419 | } |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4420 | |
| 4421 | Results.ExitScope(); |
| 4422 | |
| 4423 | if (CodeCompleter->includeMacros()) |
| 4424 | AddMacroResults(PP, Results); |
Douglas Gregor | cee9ff1 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 4425 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4426 | Results.data(), Results.size()); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 4427 | |
| 4428 | } |
| 4429 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4430 | void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, |
| 4431 | IdentifierInfo **SelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4432 | unsigned NumSelIdents, |
| 4433 | bool AtArgumentExpression) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4434 | ObjCInterfaceDecl *CDecl = 0; |
| 4435 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 4436 | // Figure out which interface we're in. |
| 4437 | CDecl = CurMethod->getClassInterface(); |
| 4438 | if (!CDecl) |
| 4439 | return; |
| 4440 | |
| 4441 | // Find the superclass of this class. |
| 4442 | CDecl = CDecl->getSuperClass(); |
| 4443 | if (!CDecl) |
| 4444 | return; |
| 4445 | |
| 4446 | if (CurMethod->isInstanceMethod()) { |
| 4447 | // We are inside an instance method, which means that the message |
| 4448 | // send [super ...] is actually calling an instance method on the |
| 4449 | // current object. Build the super expression and handle this like |
| 4450 | // an instance method. |
| 4451 | QualType SuperTy = Context.getObjCInterfaceType(CDecl); |
| 4452 | SuperTy = Context.getObjCObjectPointerType(SuperTy); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4453 | ExprResult Super |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4454 | = Owned(new (Context) ObjCSuperExpr(SuperLoc, SuperTy)); |
| 4455 | return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get(), |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4456 | SelIdents, NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4457 | AtArgumentExpression, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4458 | /*IsSuper=*/true); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
| 4461 | // Fall through to send to the superclass in CDecl. |
| 4462 | } else { |
| 4463 | // "super" may be the name of a type or variable. Figure out which |
| 4464 | // it is. |
| 4465 | IdentifierInfo *Super = &Context.Idents.get("super"); |
| 4466 | NamedDecl *ND = LookupSingleName(S, Super, SuperLoc, |
| 4467 | LookupOrdinaryName); |
| 4468 | if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) { |
| 4469 | // "super" names an interface. Use it. |
| 4470 | } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4471 | if (const ObjCObjectType *Iface |
| 4472 | = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>()) |
| 4473 | CDecl = Iface->getInterface(); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4474 | } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) { |
| 4475 | // "super" names an unresolved type; we can't be more specific. |
| 4476 | } else { |
| 4477 | // Assume that "super" names some kind of value and parse that way. |
| 4478 | CXXScopeSpec SS; |
| 4479 | UnqualifiedId id; |
| 4480 | id.setIdentifier(Super, SuperLoc); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4481 | ExprResult SuperExpr = ActOnIdExpression(S, SS, id, false, false); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4482 | return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(), |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4483 | SelIdents, NumSelIdents, |
| 4484 | AtArgumentExpression); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4485 | } |
| 4486 | |
| 4487 | // Fall through |
| 4488 | } |
| 4489 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4490 | ParsedType Receiver; |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4491 | if (CDecl) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4492 | Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4493 | return CodeCompleteObjCClassMessage(S, Receiver, SelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4494 | NumSelIdents, AtArgumentExpression, |
| 4495 | /*IsSuper=*/true); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4496 | } |
| 4497 | |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 4498 | /// \brief Given a set of code-completion results for the argument of a message |
| 4499 | /// send, determine the preferred type (if any) for that argument expression. |
| 4500 | static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results, |
| 4501 | unsigned NumSelIdents) { |
| 4502 | typedef CodeCompletionResult Result; |
| 4503 | ASTContext &Context = Results.getSema().Context; |
| 4504 | |
| 4505 | QualType PreferredType; |
| 4506 | unsigned BestPriority = CCP_Unlikely * 2; |
| 4507 | Result *ResultsData = Results.data(); |
| 4508 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 4509 | Result &R = ResultsData[I]; |
| 4510 | if (R.Kind == Result::RK_Declaration && |
| 4511 | isa<ObjCMethodDecl>(R.Declaration)) { |
| 4512 | if (R.Priority <= BestPriority) { |
| 4513 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration); |
| 4514 | if (NumSelIdents <= Method->param_size()) { |
| 4515 | QualType MyPreferredType = Method->param_begin()[NumSelIdents - 1] |
| 4516 | ->getType(); |
| 4517 | if (R.Priority < BestPriority || PreferredType.isNull()) { |
| 4518 | BestPriority = R.Priority; |
| 4519 | PreferredType = MyPreferredType; |
| 4520 | } else if (!Context.hasSameUnqualifiedType(PreferredType, |
| 4521 | MyPreferredType)) { |
| 4522 | PreferredType = QualType(); |
| 4523 | } |
| 4524 | } |
| 4525 | } |
| 4526 | } |
| 4527 | } |
| 4528 | |
| 4529 | return PreferredType; |
| 4530 | } |
| 4531 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4532 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| 4533 | ParsedType Receiver, |
| 4534 | IdentifierInfo **SelIdents, |
| 4535 | unsigned NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4536 | bool AtArgumentExpression, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4537 | bool IsSuper, |
| 4538 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4539 | typedef CodeCompletionResult Result; |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4540 | ObjCInterfaceDecl *CDecl = 0; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4541 | |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4542 | // If the given name refers to an interface type, retrieve the |
| 4543 | // corresponding declaration. |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4544 | if (Receiver) { |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4545 | QualType T = SemaRef.GetTypeFromParser(Receiver, 0); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4546 | if (!T.isNull()) |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4547 | if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>()) |
| 4548 | CDecl = Interface->getInterface(); |
Douglas Gregor | 24a069f | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 4549 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4550 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4551 | // Add all of the factory methods in this Objective-C class, its protocols, |
| 4552 | // superclasses, categories, implementation, etc. |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4553 | Results.EnterNewScope(); |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4554 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4555 | // If this is a send-to-super, try to add the special "super" send |
| 4556 | // completion. |
| 4557 | if (IsSuper) { |
| 4558 | if (ObjCMethodDecl *SuperMethod |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4559 | = AddSuperSendCompletion(SemaRef, false, SelIdents, NumSelIdents, |
| 4560 | Results)) |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4561 | Results.Ignore(SuperMethod); |
| 4562 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4563 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 4564 | // If we're inside an Objective-C method definition, prefer its selector to |
| 4565 | // others. |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4566 | if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl()) |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 4567 | Results.setPreferredSelector(CurMethod->getSelector()); |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4568 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4569 | VisitedSelectorSet Selectors; |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4570 | if (CDecl) |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4571 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, NumSelIdents, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4572 | SemaRef.CurContext, Selectors, Results); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 4573 | else { |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4574 | // We're messaging "id" as a type; provide all class/factory methods. |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4575 | |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4576 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 4577 | // pool from the AST file. |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4578 | if (SemaRef.ExternalSource) { |
| 4579 | for (uint32_t I = 0, |
| 4580 | N = SemaRef.ExternalSource->GetNumExternalSelectors(); |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 4581 | I != N; ++I) { |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4582 | Selector Sel = SemaRef.ExternalSource->GetExternalSelector(I); |
| 4583 | if (Sel.isNull() || SemaRef.MethodPool.count(Sel)) |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4584 | continue; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4585 | |
| 4586 | SemaRef.ReadMethodPool(Sel); |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4587 | } |
| 4588 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4589 | |
| 4590 | for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(), |
| 4591 | MEnd = SemaRef.MethodPool.end(); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4592 | M != MEnd; ++M) { |
| 4593 | for (ObjCMethodList *MethList = &M->second.second; |
| 4594 | MethList && MethList->Method; |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4595 | MethList = MethList->Next) { |
| 4596 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 4597 | NumSelIdents)) |
| 4598 | continue; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4599 | |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4600 | Result R(MethList->Method, 0); |
| 4601 | R.StartParameter = NumSelIdents; |
| 4602 | R.AllParametersAreInformative = false; |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4603 | Results.MaybeAddResult(R, SemaRef.CurContext); |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4604 | } |
| 4605 | } |
| 4606 | } |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4607 | |
| 4608 | Results.ExitScope(); |
| 4609 | } |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4610 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4611 | void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, |
| 4612 | IdentifierInfo **SelIdents, |
| 4613 | unsigned NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4614 | bool AtArgumentExpression, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 4615 | bool IsSuper) { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4616 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4617 | AddClassMessageCompletions(*this, S, Receiver, SelIdents, NumSelIdents, |
| 4618 | AtArgumentExpression, IsSuper, Results); |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 4619 | |
| 4620 | // If we're actually at the argument expression (rather than prior to the |
| 4621 | // selector), we're actually performing code completion for an expression. |
| 4622 | // Determine whether we have a single, best method. If so, we can |
| 4623 | // code-complete the expression using the corresponding parameter type as |
| 4624 | // our preferred type, improving completion results. |
| 4625 | if (AtArgumentExpression) { |
| 4626 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
| 4627 | NumSelIdents); |
| 4628 | if (PreferredType.isNull()) |
| 4629 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 4630 | else |
| 4631 | CodeCompleteExpression(S, PreferredType); |
| 4632 | return; |
| 4633 | } |
| 4634 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4635 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4636 | CodeCompletionContext::CCC_Other, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4637 | Results.data(), Results.size()); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4638 | } |
| 4639 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 4640 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver, |
| 4641 | IdentifierInfo **SelIdents, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4642 | unsigned NumSelIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 4643 | bool AtArgumentExpression, |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4644 | bool IsSuper) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4645 | typedef CodeCompletionResult Result; |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4646 | |
| 4647 | Expr *RecExpr = static_cast<Expr *>(Receiver); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4648 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4649 | // If necessary, apply function/array conversion to the receiver. |
| 4650 | // C99 6.7.5.3p[7,8]. |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 4651 | if (RecExpr) |
| 4652 | DefaultFunctionArrayLvalueConversion(RecExpr); |
| 4653 | QualType ReceiverType = RecExpr? RecExpr->getType() : Context.getObjCIdType(); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4654 | |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4655 | // Build the set of methods we can see. |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4656 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4657 | Results.EnterNewScope(); |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4658 | |
Douglas Gregor | 03d8aec | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 4659 | // If this is a send-to-super, try to add the special "super" send |
| 4660 | // completion. |
| 4661 | if (IsSuper) { |
| 4662 | if (ObjCMethodDecl *SuperMethod |
| 4663 | = AddSuperSendCompletion(*this, false, SelIdents, NumSelIdents, |
| 4664 | Results)) |
| 4665 | Results.Ignore(SuperMethod); |
| 4666 | } |
| 4667 | |
Douglas Gregor | 265f749 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 4668 | // If we're inside an Objective-C method definition, prefer its selector to |
| 4669 | // others. |
| 4670 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 4671 | Results.setPreferredSelector(CurMethod->getSelector()); |
| 4672 | |
Douglas Gregor | 22f5699 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 4673 | // If we're messaging an expression with type "id" or "Class", check |
| 4674 | // whether we know something special about the receiver that allows |
| 4675 | // us to assume a more-specific receiver type. |
| 4676 | if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) |
| 4677 | if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) |
| 4678 | ReceiverType = Context.getObjCObjectPointerType( |
| 4679 | Context.getObjCInterfaceType(IFace)); |
Douglas Gregor | 36ecb04 | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 4680 | |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4681 | // Keep track of the selectors we've already added. |
| 4682 | VisitedSelectorSet Selectors; |
| 4683 | |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4684 | // Handle messages to Class. This really isn't a message to an instance |
| 4685 | // method, so we treat it the same way we would treat a message send to a |
| 4686 | // class method. |
| 4687 | if (ReceiverType->isObjCClassType() || |
| 4688 | ReceiverType->isObjCQualifiedClassType()) { |
| 4689 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 4690 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4691 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, NumSelIdents, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4692 | CurContext, Selectors, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4693 | } |
| 4694 | } |
| 4695 | // Handle messages to a qualified ID ("id<foo>"). |
| 4696 | else if (const ObjCObjectPointerType *QualID |
| 4697 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 4698 | // Search protocols for instance methods. |
| 4699 | for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(), |
| 4700 | E = QualID->qual_end(); |
| 4701 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4702 | AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4703 | Selectors, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4704 | } |
| 4705 | // Handle messages to a pointer to interface type. |
| 4706 | else if (const ObjCObjectPointerType *IFacePtr |
| 4707 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 4708 | // Search the class, its superclasses, etc., for instance methods. |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4709 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4710 | NumSelIdents, CurContext, Selectors, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4711 | |
| 4712 | // Search protocols for instance methods. |
| 4713 | for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(), |
| 4714 | E = IFacePtr->qual_end(); |
| 4715 | I != E; ++I) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 4716 | AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext, |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4717 | Selectors, Results); |
Douglas Gregor | f74a419 | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 4718 | } |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4719 | // Handle messages to "id". |
| 4720 | else if (ReceiverType->isObjCIdType()) { |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4721 | // We're messaging "id", so provide all instance methods we know |
| 4722 | // about as code-completion results. |
| 4723 | |
| 4724 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 4725 | // pool from the AST file. |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4726 | if (ExternalSource) { |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 4727 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 4728 | I != N; ++I) { |
| 4729 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4730 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4731 | continue; |
| 4732 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4733 | ReadMethodPool(Sel); |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 4734 | } |
| 4735 | } |
| 4736 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 4737 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 4738 | MEnd = MethodPool.end(); |
| 4739 | M != MEnd; ++M) { |
| 4740 | for (ObjCMethodList *MethList = &M->second.first; |
| 4741 | MethList && MethList->Method; |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4742 | MethList = MethList->Next) { |
| 4743 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 4744 | NumSelIdents)) |
| 4745 | continue; |
Douglas Gregor | d36adf5 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 4746 | |
| 4747 | if (!Selectors.insert(MethList->Method->getSelector())) |
| 4748 | continue; |
| 4749 | |
Douglas Gregor | 13438f9 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 4750 | Result R(MethList->Method, 0); |
| 4751 | R.StartParameter = NumSelIdents; |
| 4752 | R.AllParametersAreInformative = false; |
| 4753 | Results.MaybeAddResult(R, CurContext); |
| 4754 | } |
| 4755 | } |
| 4756 | } |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4757 | Results.ExitScope(); |
Douglas Gregor | b9d7757 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 4758 | |
| 4759 | |
| 4760 | // If we're actually at the argument expression (rather than prior to the |
| 4761 | // selector), we're actually performing code completion for an expression. |
| 4762 | // Determine whether we have a single, best method. If so, we can |
| 4763 | // code-complete the expression using the corresponding parameter type as |
| 4764 | // our preferred type, improving completion results. |
| 4765 | if (AtArgumentExpression) { |
| 4766 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
| 4767 | NumSelIdents); |
| 4768 | if (PreferredType.isNull()) |
| 4769 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 4770 | else |
| 4771 | CodeCompleteExpression(S, PreferredType); |
| 4772 | return; |
| 4773 | } |
| 4774 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4775 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4776 | CodeCompletionContext::CCC_Other, |
| 4777 | Results.data(),Results.size()); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 4778 | } |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4779 | |
Douglas Gregor | fb62941 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4780 | void Sema::CodeCompleteObjCForCollection(Scope *S, |
| 4781 | DeclGroupPtrTy IterationVar) { |
| 4782 | CodeCompleteExpressionData Data; |
| 4783 | Data.ObjCCollection = true; |
| 4784 | |
| 4785 | if (IterationVar.getAsOpaquePtr()) { |
| 4786 | DeclGroupRef DG = IterationVar.getAsVal<DeclGroupRef>(); |
| 4787 | for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) { |
| 4788 | if (*I) |
| 4789 | Data.IgnoreDecls.push_back(*I); |
| 4790 | } |
| 4791 | } |
| 4792 | |
| 4793 | CodeCompleteExpression(S, Data); |
| 4794 | } |
| 4795 | |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4796 | void Sema::CodeCompleteObjCSelector(Scope *S, IdentifierInfo **SelIdents, |
| 4797 | unsigned NumSelIdents) { |
| 4798 | // If we have an external source, load the entire class method |
| 4799 | // pool from the AST file. |
| 4800 | if (ExternalSource) { |
| 4801 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 4802 | I != N; ++I) { |
| 4803 | Selector Sel = ExternalSource->GetExternalSelector(I); |
| 4804 | if (Sel.isNull() || MethodPool.count(Sel)) |
| 4805 | continue; |
| 4806 | |
| 4807 | ReadMethodPool(Sel); |
| 4808 | } |
| 4809 | } |
| 4810 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4811 | ResultBuilder Results(*this, CodeCompletionContext::CCC_SelectorName); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4812 | Results.EnterNewScope(); |
| 4813 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 4814 | MEnd = MethodPool.end(); |
| 4815 | M != MEnd; ++M) { |
| 4816 | |
| 4817 | Selector Sel = M->first; |
| 4818 | if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents, NumSelIdents)) |
| 4819 | continue; |
| 4820 | |
| 4821 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 4822 | if (Sel.isUnarySelector()) { |
| 4823 | Pattern->AddTypedTextChunk(Sel.getIdentifierInfoForSlot(0)->getName()); |
| 4824 | Results.AddResult(Pattern); |
| 4825 | continue; |
| 4826 | } |
| 4827 | |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 4828 | std::string Accumulator; |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4829 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) { |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 4830 | if (I == NumSelIdents) { |
| 4831 | if (!Accumulator.empty()) { |
| 4832 | Pattern->AddInformativeChunk(Accumulator); |
| 4833 | Accumulator.clear(); |
| 4834 | } |
| 4835 | } |
| 4836 | |
| 4837 | Accumulator += Sel.getIdentifierInfoForSlot(I)->getName().str(); |
| 4838 | Accumulator += ':'; |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4839 | } |
Douglas Gregor | 2d9e21f | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 4840 | Pattern->AddTypedTextChunk(Accumulator); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 4841 | Results.AddResult(Pattern); |
| 4842 | } |
| 4843 | Results.ExitScope(); |
| 4844 | |
| 4845 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4846 | CodeCompletionContext::CCC_SelectorName, |
| 4847 | Results.data(), Results.size()); |
| 4848 | } |
| 4849 | |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4850 | /// \brief Add all of the protocol declarations that we find in the given |
| 4851 | /// (translation unit) context. |
| 4852 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4853 | bool OnlyForwardDeclarations, |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4854 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4855 | typedef CodeCompletionResult Result; |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4856 | |
| 4857 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 4858 | DEnd = Ctx->decls_end(); |
| 4859 | D != DEnd; ++D) { |
| 4860 | // Record any protocols we find. |
| 4861 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*D)) |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4862 | if (!OnlyForwardDeclarations || Proto->isForwardDecl()) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 4863 | Results.AddResult(Result(Proto, 0), CurContext, 0, false); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4864 | |
| 4865 | // Record any forward-declared protocols we find. |
| 4866 | if (ObjCForwardProtocolDecl *Forward |
| 4867 | = dyn_cast<ObjCForwardProtocolDecl>(*D)) { |
| 4868 | for (ObjCForwardProtocolDecl::protocol_iterator |
| 4869 | P = Forward->protocol_begin(), |
| 4870 | PEnd = Forward->protocol_end(); |
| 4871 | P != PEnd; ++P) |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4872 | if (!OnlyForwardDeclarations || (*P)->isForwardDecl()) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 4873 | Results.AddResult(Result(*P, 0), CurContext, 0, false); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4874 | } |
| 4875 | } |
| 4876 | } |
| 4877 | |
| 4878 | void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols, |
| 4879 | unsigned NumProtocols) { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4880 | ResultBuilder Results(*this, CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4881 | Results.EnterNewScope(); |
| 4882 | |
| 4883 | // Tell the result set to ignore all of the protocols we have |
| 4884 | // already seen. |
| 4885 | for (unsigned I = 0; I != NumProtocols; ++I) |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4886 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first, |
| 4887 | Protocols[I].second)) |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4888 | Results.Ignore(Protocol); |
| 4889 | |
| 4890 | // Add all protocols. |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4891 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 4892 | Results); |
| 4893 | |
| 4894 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4895 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4896 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 4897 | Results.data(),Results.size()); |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4898 | } |
| 4899 | |
| 4900 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4901 | ResultBuilder Results(*this, CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 4902 | Results.EnterNewScope(); |
| 4903 | |
| 4904 | // Add all protocols. |
| 4905 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 4906 | Results); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4907 | |
| 4908 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4909 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4910 | CodeCompletionContext::CCC_ObjCProtocolName, |
| 4911 | Results.data(),Results.size()); |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 4912 | } |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4913 | |
| 4914 | /// \brief Add all of the Objective-C interface declarations that we find in |
| 4915 | /// the given (translation unit) context. |
| 4916 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, |
| 4917 | bool OnlyForwardDeclarations, |
| 4918 | bool OnlyUnimplemented, |
| 4919 | ResultBuilder &Results) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4920 | typedef CodeCompletionResult Result; |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4921 | |
| 4922 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 4923 | DEnd = Ctx->decls_end(); |
| 4924 | D != DEnd; ++D) { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4925 | // Record any interfaces we find. |
| 4926 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*D)) |
| 4927 | if ((!OnlyForwardDeclarations || Class->isForwardDecl()) && |
| 4928 | (!OnlyUnimplemented || !Class->getImplementation())) |
| 4929 | Results.AddResult(Result(Class, 0), CurContext, 0, false); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4930 | |
| 4931 | // Record any forward-declared interfaces we find. |
| 4932 | if (ObjCClassDecl *Forward = dyn_cast<ObjCClassDecl>(*D)) { |
| 4933 | for (ObjCClassDecl::iterator C = Forward->begin(), CEnd = Forward->end(); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4934 | C != CEnd; ++C) |
| 4935 | if ((!OnlyForwardDeclarations || C->getInterface()->isForwardDecl()) && |
| 4936 | (!OnlyUnimplemented || !C->getInterface()->getImplementation())) |
| 4937 | Results.AddResult(Result(C->getInterface(), 0), CurContext, |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 4938 | 0, false); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4939 | } |
| 4940 | } |
| 4941 | } |
| 4942 | |
| 4943 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4944 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4945 | Results.EnterNewScope(); |
| 4946 | |
| 4947 | // Add all classes. |
| 4948 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 4949 | false, Results); |
| 4950 | |
| 4951 | Results.ExitScope(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4952 | // FIXME: Add a special context for this, use cached global completion |
| 4953 | // results. |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4954 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4955 | CodeCompletionContext::CCC_Other, |
| 4956 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4957 | } |
| 4958 | |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4959 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName, |
| 4960 | SourceLocation ClassNameLoc) { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4961 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4962 | Results.EnterNewScope(); |
| 4963 | |
| 4964 | // Make sure that we ignore the class we're currently defining. |
| 4965 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4966 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 4967 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4968 | Results.Ignore(CurClass); |
| 4969 | |
| 4970 | // Add all classes. |
| 4971 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 4972 | false, Results); |
| 4973 | |
| 4974 | Results.ExitScope(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4975 | // FIXME: Add a special context for this, use cached global completion |
| 4976 | // results. |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4977 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4978 | CodeCompletionContext::CCC_Other, |
| 4979 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
| 4982 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4983 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4984 | Results.EnterNewScope(); |
| 4985 | |
| 4986 | // Add all unimplemented classes. |
| 4987 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 4988 | true, Results); |
| 4989 | |
| 4990 | Results.ExitScope(); |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4991 | // FIXME: Add a special context for this, use cached global completion |
| 4992 | // results. |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4993 | HandleCodeCompleteResults(this, CodeCompleter, |
| 4994 | CodeCompletionContext::CCC_Other, |
| 4995 | Results.data(),Results.size()); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 4996 | } |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 4997 | |
| 4998 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4999 | IdentifierInfo *ClassName, |
| 5000 | SourceLocation ClassNameLoc) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5001 | typedef CodeCompletionResult Result; |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5002 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5003 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5004 | |
| 5005 | // Ignore any categories we find that have already been implemented by this |
| 5006 | // interface. |
| 5007 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 5008 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5009 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5010 | if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)) |
| 5011 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; |
| 5012 | Category = Category->getNextClassCategory()) |
| 5013 | CategoryNames.insert(Category->getIdentifier()); |
| 5014 | |
| 5015 | // Add all of the categories we know about. |
| 5016 | Results.EnterNewScope(); |
| 5017 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
| 5018 | for (DeclContext::decl_iterator D = TU->decls_begin(), |
| 5019 | DEnd = TU->decls_end(); |
| 5020 | D != DEnd; ++D) |
| 5021 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(*D)) |
| 5022 | if (CategoryNames.insert(Category->getIdentifier())) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5023 | Results.AddResult(Result(Category, 0), CurContext, 0, false); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5024 | Results.ExitScope(); |
| 5025 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5026 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5027 | CodeCompletionContext::CCC_Other, |
| 5028 | Results.data(),Results.size()); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5029 | } |
| 5030 | |
| 5031 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5032 | IdentifierInfo *ClassName, |
| 5033 | SourceLocation ClassNameLoc) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5034 | typedef CodeCompletionResult Result; |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5035 | |
| 5036 | // Find the corresponding interface. If we couldn't find the interface, the |
| 5037 | // program itself is ill-formed. However, we'll try to be helpful still by |
| 5038 | // providing the list of all of the categories we know about. |
| 5039 | NamedDecl *CurClass |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5040 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5041 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); |
| 5042 | if (!Class) |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5043 | return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5044 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5045 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5046 | |
| 5047 | // Add all of the categories that have have corresponding interface |
| 5048 | // declarations in this class and any of its superclasses, except for |
| 5049 | // already-implemented categories in the class itself. |
| 5050 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 5051 | Results.EnterNewScope(); |
| 5052 | bool IgnoreImplemented = true; |
| 5053 | while (Class) { |
| 5054 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; |
| 5055 | Category = Category->getNextClassCategory()) |
| 5056 | if ((!IgnoreImplemented || !Category->getImplementation()) && |
| 5057 | CategoryNames.insert(Category->getIdentifier())) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5058 | Results.AddResult(Result(Category, 0), CurContext, 0, false); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5059 | |
| 5060 | Class = Class->getSuperClass(); |
| 5061 | IgnoreImplemented = false; |
| 5062 | } |
| 5063 | Results.ExitScope(); |
| 5064 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5065 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5066 | CodeCompletionContext::CCC_Other, |
| 5067 | Results.data(),Results.size()); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 5068 | } |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5069 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5070 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S, Decl *ObjCImpDecl) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5071 | typedef CodeCompletionResult Result; |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5072 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5073 | |
| 5074 | // Figure out where this @synthesize lives. |
| 5075 | ObjCContainerDecl *Container |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5076 | = dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5077 | if (!Container || |
| 5078 | (!isa<ObjCImplementationDecl>(Container) && |
| 5079 | !isa<ObjCCategoryImplDecl>(Container))) |
| 5080 | return; |
| 5081 | |
| 5082 | // Ignore any properties that have already been implemented. |
| 5083 | for (DeclContext::decl_iterator D = Container->decls_begin(), |
| 5084 | DEnd = Container->decls_end(); |
| 5085 | D != DEnd; ++D) |
| 5086 | if (ObjCPropertyImplDecl *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(*D)) |
| 5087 | Results.Ignore(PropertyImpl->getPropertyDecl()); |
| 5088 | |
| 5089 | // Add any properties that we find. |
| 5090 | Results.EnterNewScope(); |
| 5091 | if (ObjCImplementationDecl *ClassImpl |
| 5092 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| 5093 | AddObjCProperties(ClassImpl->getClassInterface(), false, CurContext, |
| 5094 | Results); |
| 5095 | else |
| 5096 | AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), |
| 5097 | false, CurContext, Results); |
| 5098 | Results.ExitScope(); |
| 5099 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5100 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5101 | CodeCompletionContext::CCC_Other, |
| 5102 | Results.data(),Results.size()); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5103 | } |
| 5104 | |
| 5105 | void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S, |
| 5106 | IdentifierInfo *PropertyName, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5107 | Decl *ObjCImpDecl) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5108 | typedef CodeCompletionResult Result; |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5109 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5110 | |
| 5111 | // Figure out where this @synthesize lives. |
| 5112 | ObjCContainerDecl *Container |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5113 | = dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5114 | if (!Container || |
| 5115 | (!isa<ObjCImplementationDecl>(Container) && |
| 5116 | !isa<ObjCCategoryImplDecl>(Container))) |
| 5117 | return; |
| 5118 | |
| 5119 | // Figure out which interface we're looking into. |
| 5120 | ObjCInterfaceDecl *Class = 0; |
| 5121 | if (ObjCImplementationDecl *ClassImpl |
| 5122 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| 5123 | Class = ClassImpl->getClassInterface(); |
| 5124 | else |
| 5125 | Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl() |
| 5126 | ->getClassInterface(); |
| 5127 | |
| 5128 | // Add all of the instance variables in this class and its superclasses. |
| 5129 | Results.EnterNewScope(); |
| 5130 | for(; Class; Class = Class->getSuperClass()) { |
| 5131 | // FIXME: We could screen the type of each ivar for compatibility with |
| 5132 | // the property, but is that being too paternal? |
| 5133 | for (ObjCInterfaceDecl::ivar_iterator IVar = Class->ivar_begin(), |
| 5134 | IVarEnd = Class->ivar_end(); |
| 5135 | IVar != IVarEnd; ++IVar) |
Douglas Gregor | 608300b | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 5136 | Results.AddResult(Result(*IVar, 0), CurContext, 0, false); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5137 | } |
| 5138 | Results.ExitScope(); |
| 5139 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5140 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5141 | CodeCompletionContext::CCC_Other, |
| 5142 | Results.data(),Results.size()); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 5143 | } |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5144 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5145 | // Mapping from selectors to the methods that implement that selector, along |
| 5146 | // with the "in original class" flag. |
| 5147 | typedef llvm::DenseMap<Selector, std::pair<ObjCMethodDecl *, bool> > |
| 5148 | KnownMethodsMap; |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5149 | |
| 5150 | /// \brief Find all of the methods that reside in the given container |
| 5151 | /// (and its superclasses, protocols, etc.) that meet the given |
| 5152 | /// criteria. Insert those methods into the map of known methods, |
| 5153 | /// indexed by selector so they can be easily found. |
| 5154 | static void FindImplementableMethods(ASTContext &Context, |
| 5155 | ObjCContainerDecl *Container, |
| 5156 | bool WantInstanceMethods, |
| 5157 | QualType ReturnType, |
| 5158 | bool IsInImplementation, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5159 | KnownMethodsMap &KnownMethods, |
| 5160 | bool InOriginalClass = true) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5161 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 5162 | // Recurse into protocols. |
| 5163 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5164 | = IFace->getReferencedProtocols(); |
| 5165 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 5166 | E = Protocols.end(); |
| 5167 | I != E; ++I) |
| 5168 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5169 | IsInImplementation, KnownMethods, |
| 5170 | InOriginalClass); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5171 | |
| 5172 | // If we're not in the implementation of a class, also visit the |
| 5173 | // superclass. |
| 5174 | if (!IsInImplementation && IFace->getSuperClass()) |
| 5175 | FindImplementableMethods(Context, IFace->getSuperClass(), |
| 5176 | WantInstanceMethods, ReturnType, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5177 | IsInImplementation, KnownMethods, |
| 5178 | false); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5179 | |
| 5180 | // Add methods from any class extensions (but not from categories; |
| 5181 | // those should go into category implementations). |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 5182 | for (const ObjCCategoryDecl *Cat = IFace->getFirstClassExtension(); Cat; |
| 5183 | Cat = Cat->getNextClassExtension()) |
| 5184 | FindImplementableMethods(Context, const_cast<ObjCCategoryDecl*>(Cat), |
| 5185 | WantInstanceMethods, ReturnType, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5186 | IsInImplementation, KnownMethods, |
| 5187 | InOriginalClass); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5188 | } |
| 5189 | |
| 5190 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 5191 | // Recurse into protocols. |
| 5192 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5193 | = Category->getReferencedProtocols(); |
| 5194 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 5195 | E = Protocols.end(); |
| 5196 | I != E; ++I) |
| 5197 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5198 | IsInImplementation, KnownMethods, |
| 5199 | InOriginalClass); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5200 | } |
| 5201 | |
| 5202 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 5203 | // Recurse into protocols. |
| 5204 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5205 | = Protocol->getReferencedProtocols(); |
| 5206 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 5207 | E = Protocols.end(); |
| 5208 | I != E; ++I) |
| 5209 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5210 | IsInImplementation, KnownMethods, false); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5211 | } |
| 5212 | |
| 5213 | // Add methods in this container. This operation occurs last because |
| 5214 | // we want the methods from this container to override any methods |
| 5215 | // we've previously seen with the same selector. |
| 5216 | for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), |
| 5217 | MEnd = Container->meth_end(); |
| 5218 | M != MEnd; ++M) { |
| 5219 | if ((*M)->isInstanceMethod() == WantInstanceMethods) { |
| 5220 | if (!ReturnType.isNull() && |
| 5221 | !Context.hasSameUnqualifiedType(ReturnType, (*M)->getResultType())) |
| 5222 | continue; |
| 5223 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5224 | KnownMethods[(*M)->getSelector()] = std::make_pair(*M, InOriginalClass); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5225 | } |
| 5226 | } |
| 5227 | } |
| 5228 | |
| 5229 | void Sema::CodeCompleteObjCMethodDecl(Scope *S, |
| 5230 | bool IsInstanceMethod, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5231 | ParsedType ReturnTy, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5232 | Decl *IDecl) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5233 | // Determine the return type of the method we're declaring, if |
| 5234 | // provided. |
| 5235 | QualType ReturnType = GetTypeFromParser(ReturnTy); |
| 5236 | |
| 5237 | // Determine where we should start searching for methods, and where we |
| 5238 | ObjCContainerDecl *SearchDecl = 0, *CurrentDecl = 0; |
| 5239 | bool IsInImplementation = false; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5240 | if (Decl *D = IDecl) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5241 | if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) { |
| 5242 | SearchDecl = Impl->getClassInterface(); |
| 5243 | CurrentDecl = Impl; |
| 5244 | IsInImplementation = true; |
| 5245 | } else if (ObjCCategoryImplDecl *CatImpl |
| 5246 | = dyn_cast<ObjCCategoryImplDecl>(D)) { |
| 5247 | SearchDecl = CatImpl->getCategoryDecl(); |
| 5248 | CurrentDecl = CatImpl; |
| 5249 | IsInImplementation = true; |
| 5250 | } else { |
| 5251 | SearchDecl = dyn_cast<ObjCContainerDecl>(D); |
| 5252 | CurrentDecl = SearchDecl; |
| 5253 | } |
| 5254 | } |
| 5255 | |
| 5256 | if (!SearchDecl && S) { |
| 5257 | if (DeclContext *DC = static_cast<DeclContext *>(S->getEntity())) { |
| 5258 | SearchDecl = dyn_cast<ObjCContainerDecl>(DC); |
| 5259 | CurrentDecl = SearchDecl; |
| 5260 | } |
| 5261 | } |
| 5262 | |
| 5263 | if (!SearchDecl || !CurrentDecl) { |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5264 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5265 | CodeCompletionContext::CCC_Other, |
| 5266 | 0, 0); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5267 | return; |
| 5268 | } |
| 5269 | |
| 5270 | // Find all of the methods that we could declare/implement here. |
| 5271 | KnownMethodsMap KnownMethods; |
| 5272 | FindImplementableMethods(Context, SearchDecl, IsInstanceMethod, |
| 5273 | ReturnType, IsInImplementation, KnownMethods); |
| 5274 | |
| 5275 | // Erase any methods that have already been declared or |
| 5276 | // implemented here. |
| 5277 | for (ObjCContainerDecl::method_iterator M = CurrentDecl->meth_begin(), |
| 5278 | MEnd = CurrentDecl->meth_end(); |
| 5279 | M != MEnd; ++M) { |
| 5280 | if ((*M)->isInstanceMethod() != IsInstanceMethod) |
| 5281 | continue; |
| 5282 | |
| 5283 | KnownMethodsMap::iterator Pos = KnownMethods.find((*M)->getSelector()); |
| 5284 | if (Pos != KnownMethods.end()) |
| 5285 | KnownMethods.erase(Pos); |
| 5286 | } |
| 5287 | |
| 5288 | // Add declarations or definitions for each of the known methods. |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5289 | typedef CodeCompletionResult Result; |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5290 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5291 | Results.EnterNewScope(); |
| 5292 | PrintingPolicy Policy(Context.PrintingPolicy); |
| 5293 | Policy.AnonymousTagLocations = false; |
| 5294 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
| 5295 | MEnd = KnownMethods.end(); |
| 5296 | M != MEnd; ++M) { |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5297 | ObjCMethodDecl *Method = M->second.first; |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5298 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 5299 | |
| 5300 | // If the result type was not already provided, add it to the |
| 5301 | // pattern as (type). |
| 5302 | if (ReturnType.isNull()) { |
| 5303 | std::string TypeStr; |
| 5304 | Method->getResultType().getAsStringInternal(TypeStr, Policy); |
| 5305 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 5306 | Pattern->AddTextChunk(TypeStr); |
| 5307 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 5308 | } |
| 5309 | |
| 5310 | Selector Sel = Method->getSelector(); |
| 5311 | |
| 5312 | // Add the first part of the selector to the pattern. |
| 5313 | Pattern->AddTypedTextChunk(Sel.getIdentifierInfoForSlot(0)->getName()); |
| 5314 | |
| 5315 | // Add parameters to the pattern. |
| 5316 | unsigned I = 0; |
| 5317 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 5318 | PEnd = Method->param_end(); |
| 5319 | P != PEnd; (void)++P, ++I) { |
| 5320 | // Add the part of the selector name. |
| 5321 | if (I == 0) |
| 5322 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
| 5323 | else if (I < Sel.getNumArgs()) { |
| 5324 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 47c03a7 | 2010-08-17 15:53:35 +0000 | [diff] [blame] | 5325 | Pattern->AddTextChunk(Sel.getIdentifierInfoForSlot(I)->getName()); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5326 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
| 5327 | } else |
| 5328 | break; |
| 5329 | |
| 5330 | // Add the parameter type. |
| 5331 | std::string TypeStr; |
| 5332 | (*P)->getOriginalType().getAsStringInternal(TypeStr, Policy); |
| 5333 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 5334 | Pattern->AddTextChunk(TypeStr); |
| 5335 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 5336 | |
| 5337 | if (IdentifierInfo *Id = (*P)->getIdentifier()) |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 5338 | Pattern->AddTextChunk(Id->getName()); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5339 | } |
| 5340 | |
| 5341 | if (Method->isVariadic()) { |
| 5342 | if (Method->param_size() > 0) |
| 5343 | Pattern->AddChunk(CodeCompletionString::CK_Comma); |
| 5344 | Pattern->AddTextChunk("..."); |
Douglas Gregor | e17794f | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 5345 | } |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5346 | |
Douglas Gregor | 447107d | 2010-05-28 00:57:46 +0000 | [diff] [blame] | 5347 | if (IsInImplementation && Results.includeCodePatterns()) { |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5348 | // We will be defining the method here, so add a compound statement. |
| 5349 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5350 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5351 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 5352 | if (!Method->getResultType()->isVoidType()) { |
| 5353 | // If the result type is not void, add a return clause. |
| 5354 | Pattern->AddTextChunk("return"); |
| 5355 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5356 | Pattern->AddPlaceholderChunk("expression"); |
| 5357 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
| 5358 | } else |
| 5359 | Pattern->AddPlaceholderChunk("statements"); |
| 5360 | |
| 5361 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 5362 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 5363 | } |
| 5364 | |
Douglas Gregor | 408be5a | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5365 | unsigned Priority = CCP_CodePattern; |
| 5366 | if (!M->second.second) |
| 5367 | Priority += CCD_InBaseClass; |
| 5368 | |
| 5369 | Results.AddResult(Result(Pattern, Priority, |
Douglas Gregor | 16ed9ad | 2010-08-17 16:06:07 +0000 | [diff] [blame] | 5370 | Method->isInstanceMethod() |
| 5371 | ? CXCursor_ObjCInstanceMethodDecl |
| 5372 | : CXCursor_ObjCClassMethodDecl)); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5373 | } |
| 5374 | |
| 5375 | Results.ExitScope(); |
| 5376 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5377 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5378 | CodeCompletionContext::CCC_Other, |
| 5379 | Results.data(),Results.size()); |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 5380 | } |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5381 | |
| 5382 | void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S, |
| 5383 | bool IsInstanceMethod, |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 5384 | bool AtParameterName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5385 | ParsedType ReturnTy, |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5386 | IdentifierInfo **SelIdents, |
| 5387 | unsigned NumSelIdents) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5388 | // If we have an external source, load the entire class method |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 5389 | // pool from the AST file. |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5390 | if (ExternalSource) { |
| 5391 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 5392 | I != N; ++I) { |
| 5393 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5394 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5395 | continue; |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5396 | |
| 5397 | ReadMethodPool(Sel); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5398 | } |
| 5399 | } |
| 5400 | |
| 5401 | // Build the set of methods we can see. |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5402 | typedef CodeCompletionResult Result; |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5403 | ResultBuilder Results(*this, CodeCompletionContext::CCC_Other); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5404 | |
| 5405 | if (ReturnTy) |
| 5406 | Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType()); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5407 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5408 | Results.EnterNewScope(); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 5409 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 5410 | MEnd = MethodPool.end(); |
| 5411 | M != MEnd; ++M) { |
| 5412 | for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first : |
| 5413 | &M->second.second; |
| 5414 | MethList && MethList->Method; |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5415 | MethList = MethList->Next) { |
| 5416 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 5417 | NumSelIdents)) |
| 5418 | continue; |
| 5419 | |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 5420 | if (AtParameterName) { |
| 5421 | // Suggest parameter names we've seen before. |
| 5422 | if (NumSelIdents && NumSelIdents <= MethList->Method->param_size()) { |
| 5423 | ParmVarDecl *Param = MethList->Method->param_begin()[NumSelIdents-1]; |
| 5424 | if (Param->getIdentifier()) { |
| 5425 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 5426 | Pattern->AddTypedTextChunk(Param->getIdentifier()->getName()); |
| 5427 | Results.AddResult(Pattern); |
| 5428 | } |
| 5429 | } |
| 5430 | |
| 5431 | continue; |
| 5432 | } |
| 5433 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5434 | Result R(MethList->Method, 0); |
| 5435 | R.StartParameter = NumSelIdents; |
| 5436 | R.AllParametersAreInformative = false; |
| 5437 | R.DeclaringEntity = true; |
| 5438 | Results.MaybeAddResult(R, CurContext); |
| 5439 | } |
| 5440 | } |
| 5441 | |
| 5442 | Results.ExitScope(); |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5443 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5444 | CodeCompletionContext::CCC_Other, |
| 5445 | Results.data(),Results.size()); |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 5446 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 5447 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5448 | void Sema::CodeCompletePreprocessorDirective(bool InConditional) { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5449 | ResultBuilder Results(*this, |
| 5450 | CodeCompletionContext::CCC_PreprocessorDirective); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5451 | Results.EnterNewScope(); |
| 5452 | |
| 5453 | // #if <condition> |
| 5454 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 5455 | Pattern->AddTypedTextChunk("if"); |
| 5456 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5457 | Pattern->AddPlaceholderChunk("condition"); |
| 5458 | Results.AddResult(Pattern); |
| 5459 | |
| 5460 | // #ifdef <macro> |
| 5461 | Pattern = new CodeCompletionString; |
| 5462 | Pattern->AddTypedTextChunk("ifdef"); |
| 5463 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5464 | Pattern->AddPlaceholderChunk("macro"); |
| 5465 | Results.AddResult(Pattern); |
| 5466 | |
| 5467 | // #ifndef <macro> |
| 5468 | Pattern = new CodeCompletionString; |
| 5469 | Pattern->AddTypedTextChunk("ifndef"); |
| 5470 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5471 | Pattern->AddPlaceholderChunk("macro"); |
| 5472 | Results.AddResult(Pattern); |
| 5473 | |
| 5474 | if (InConditional) { |
| 5475 | // #elif <condition> |
| 5476 | Pattern = new CodeCompletionString; |
| 5477 | Pattern->AddTypedTextChunk("elif"); |
| 5478 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5479 | Pattern->AddPlaceholderChunk("condition"); |
| 5480 | Results.AddResult(Pattern); |
| 5481 | |
| 5482 | // #else |
| 5483 | Pattern = new CodeCompletionString; |
| 5484 | Pattern->AddTypedTextChunk("else"); |
| 5485 | Results.AddResult(Pattern); |
| 5486 | |
| 5487 | // #endif |
| 5488 | Pattern = new CodeCompletionString; |
| 5489 | Pattern->AddTypedTextChunk("endif"); |
| 5490 | Results.AddResult(Pattern); |
| 5491 | } |
| 5492 | |
| 5493 | // #include "header" |
| 5494 | Pattern = new CodeCompletionString; |
| 5495 | Pattern->AddTypedTextChunk("include"); |
| 5496 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5497 | Pattern->AddTextChunk("\""); |
| 5498 | Pattern->AddPlaceholderChunk("header"); |
| 5499 | Pattern->AddTextChunk("\""); |
| 5500 | Results.AddResult(Pattern); |
| 5501 | |
| 5502 | // #include <header> |
| 5503 | Pattern = new CodeCompletionString; |
| 5504 | Pattern->AddTypedTextChunk("include"); |
| 5505 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5506 | Pattern->AddTextChunk("<"); |
| 5507 | Pattern->AddPlaceholderChunk("header"); |
| 5508 | Pattern->AddTextChunk(">"); |
| 5509 | Results.AddResult(Pattern); |
| 5510 | |
| 5511 | // #define <macro> |
| 5512 | Pattern = new CodeCompletionString; |
| 5513 | Pattern->AddTypedTextChunk("define"); |
| 5514 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5515 | Pattern->AddPlaceholderChunk("macro"); |
| 5516 | Results.AddResult(Pattern); |
| 5517 | |
| 5518 | // #define <macro>(<args>) |
| 5519 | Pattern = new CodeCompletionString; |
| 5520 | Pattern->AddTypedTextChunk("define"); |
| 5521 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5522 | Pattern->AddPlaceholderChunk("macro"); |
| 5523 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 5524 | Pattern->AddPlaceholderChunk("args"); |
| 5525 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 5526 | Results.AddResult(Pattern); |
| 5527 | |
| 5528 | // #undef <macro> |
| 5529 | Pattern = new CodeCompletionString; |
| 5530 | Pattern->AddTypedTextChunk("undef"); |
| 5531 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5532 | Pattern->AddPlaceholderChunk("macro"); |
| 5533 | Results.AddResult(Pattern); |
| 5534 | |
| 5535 | // #line <number> |
| 5536 | Pattern = new CodeCompletionString; |
| 5537 | Pattern->AddTypedTextChunk("line"); |
| 5538 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5539 | Pattern->AddPlaceholderChunk("number"); |
| 5540 | Results.AddResult(Pattern); |
| 5541 | |
| 5542 | // #line <number> "filename" |
| 5543 | Pattern = new CodeCompletionString; |
| 5544 | Pattern->AddTypedTextChunk("line"); |
| 5545 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5546 | Pattern->AddPlaceholderChunk("number"); |
| 5547 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5548 | Pattern->AddTextChunk("\""); |
| 5549 | Pattern->AddPlaceholderChunk("filename"); |
| 5550 | Pattern->AddTextChunk("\""); |
| 5551 | Results.AddResult(Pattern); |
| 5552 | |
| 5553 | // #error <message> |
| 5554 | Pattern = new CodeCompletionString; |
| 5555 | Pattern->AddTypedTextChunk("error"); |
| 5556 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5557 | Pattern->AddPlaceholderChunk("message"); |
| 5558 | Results.AddResult(Pattern); |
| 5559 | |
| 5560 | // #pragma <arguments> |
| 5561 | Pattern = new CodeCompletionString; |
| 5562 | Pattern->AddTypedTextChunk("pragma"); |
| 5563 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5564 | Pattern->AddPlaceholderChunk("arguments"); |
| 5565 | Results.AddResult(Pattern); |
| 5566 | |
| 5567 | if (getLangOptions().ObjC1) { |
| 5568 | // #import "header" |
| 5569 | Pattern = new CodeCompletionString; |
| 5570 | Pattern->AddTypedTextChunk("import"); |
| 5571 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5572 | Pattern->AddTextChunk("\""); |
| 5573 | Pattern->AddPlaceholderChunk("header"); |
| 5574 | Pattern->AddTextChunk("\""); |
| 5575 | Results.AddResult(Pattern); |
| 5576 | |
| 5577 | // #import <header> |
| 5578 | Pattern = new CodeCompletionString; |
| 5579 | Pattern->AddTypedTextChunk("import"); |
| 5580 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5581 | Pattern->AddTextChunk("<"); |
| 5582 | Pattern->AddPlaceholderChunk("header"); |
| 5583 | Pattern->AddTextChunk(">"); |
| 5584 | Results.AddResult(Pattern); |
| 5585 | } |
| 5586 | |
| 5587 | // #include_next "header" |
| 5588 | Pattern = new CodeCompletionString; |
| 5589 | Pattern->AddTypedTextChunk("include_next"); |
| 5590 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5591 | Pattern->AddTextChunk("\""); |
| 5592 | Pattern->AddPlaceholderChunk("header"); |
| 5593 | Pattern->AddTextChunk("\""); |
| 5594 | Results.AddResult(Pattern); |
| 5595 | |
| 5596 | // #include_next <header> |
| 5597 | Pattern = new CodeCompletionString; |
| 5598 | Pattern->AddTypedTextChunk("include_next"); |
| 5599 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5600 | Pattern->AddTextChunk("<"); |
| 5601 | Pattern->AddPlaceholderChunk("header"); |
| 5602 | Pattern->AddTextChunk(">"); |
| 5603 | Results.AddResult(Pattern); |
| 5604 | |
| 5605 | // #warning <message> |
| 5606 | Pattern = new CodeCompletionString; |
| 5607 | Pattern->AddTypedTextChunk("warning"); |
| 5608 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5609 | Pattern->AddPlaceholderChunk("message"); |
| 5610 | Results.AddResult(Pattern); |
| 5611 | |
| 5612 | // Note: #ident and #sccs are such crazy anachronisms that we don't provide |
| 5613 | // completions for them. And __include_macros is a Clang-internal extension |
| 5614 | // that we don't want to encourage anyone to use. |
| 5615 | |
| 5616 | // FIXME: we don't support #assert or #unassert, so don't suggest them. |
| 5617 | Results.ExitScope(); |
| 5618 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5619 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 5620 | CodeCompletionContext::CCC_PreprocessorDirective, |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5621 | Results.data(), Results.size()); |
| 5622 | } |
| 5623 | |
| 5624 | void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) { |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5625 | CodeCompleteOrdinaryName(S, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5626 | S->getFnParent()? Sema::PCC_RecoveryInFunction |
| 5627 | : Sema::PCC_Namespace); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 5628 | } |
| 5629 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5630 | void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5631 | ResultBuilder Results(*this, |
| 5632 | IsDefinition? CodeCompletionContext::CCC_MacroName |
| 5633 | : CodeCompletionContext::CCC_MacroNameUse); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 5634 | if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) { |
| 5635 | // Add just the names of macros, not their arguments. |
| 5636 | Results.EnterNewScope(); |
| 5637 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 5638 | MEnd = PP.macro_end(); |
| 5639 | M != MEnd; ++M) { |
| 5640 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 5641 | Pattern->AddTypedTextChunk(M->first->getName()); |
| 5642 | Results.AddResult(Pattern); |
| 5643 | } |
| 5644 | Results.ExitScope(); |
| 5645 | } else if (IsDefinition) { |
| 5646 | // FIXME: Can we detect when the user just wrote an include guard above? |
| 5647 | } |
| 5648 | |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5649 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 5650 | Results.data(), Results.size()); |
| 5651 | } |
| 5652 | |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5653 | void Sema::CodeCompletePreprocessorExpression() { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5654 | ResultBuilder Results(*this, |
| 5655 | CodeCompletionContext::CCC_PreprocessorExpression); |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5656 | |
| 5657 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
| 5658 | AddMacroResults(PP, Results); |
| 5659 | |
| 5660 | // defined (<macro>) |
| 5661 | Results.EnterNewScope(); |
| 5662 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 5663 | Pattern->AddTypedTextChunk("defined"); |
| 5664 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5665 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 5666 | Pattern->AddPlaceholderChunk("macro"); |
| 5667 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 5668 | Results.AddResult(Pattern); |
| 5669 | Results.ExitScope(); |
| 5670 | |
| 5671 | HandleCodeCompleteResults(this, CodeCompleter, |
| 5672 | CodeCompletionContext::CCC_PreprocessorExpression, |
| 5673 | Results.data(), Results.size()); |
| 5674 | } |
| 5675 | |
| 5676 | void Sema::CodeCompletePreprocessorMacroArgument(Scope *S, |
| 5677 | IdentifierInfo *Macro, |
| 5678 | MacroInfo *MacroInfo, |
| 5679 | unsigned Argument) { |
| 5680 | // FIXME: In the future, we could provide "overload" results, much like we |
| 5681 | // do for function calls. |
| 5682 | |
| 5683 | CodeCompleteOrdinaryName(S, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5684 | S->getFnParent()? Sema::PCC_RecoveryInFunction |
| 5685 | : Sema::PCC_Namespace); |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 5686 | } |
| 5687 | |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 5688 | void Sema::CodeCompleteNaturalLanguage() { |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 5689 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | af1c6b5 | 2010-08-25 17:10:00 +0000 | [diff] [blame] | 5690 | CodeCompletionContext::CCC_NaturalLanguage, |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 5691 | 0, 0); |
| 5692 | } |
| 5693 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 5694 | void Sema::GatherGlobalCodeCompletions( |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5695 | llvm::SmallVectorImpl<CodeCompletionResult> &Results) { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5696 | ResultBuilder Builder(*this, CodeCompletionContext::CCC_Recovery); |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 5697 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5698 | if (!CodeCompleter || CodeCompleter->includeGlobals()) { |
| 5699 | CodeCompletionDeclConsumer Consumer(Builder, |
| 5700 | Context.getTranslationUnitDecl()); |
| 5701 | LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName, |
| 5702 | Consumer); |
| 5703 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 5704 | |
| 5705 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
| 5706 | AddMacroResults(PP, Builder); |
| 5707 | |
| 5708 | Results.clear(); |
| 5709 | Results.insert(Results.end(), |
| 5710 | Builder.data(), Builder.data() + Builder.size()); |
| 5711 | } |