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