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 | //===----------------------------------------------------------------------===// |
| 13 | #include "Sema.h" |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 14 | #include "Lookup.h" |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 15 | #include "clang/Sema/CodeCompleteConsumer.h" |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 16 | #include "clang/Sema/ExternalSemaSource.h" |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 19 | #include "clang/Lex/MacroInfo.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | e6688e6 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringSwitch.h" |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 24 | #include <list> |
| 25 | #include <map> |
| 26 | #include <vector> |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | /// \brief A container of code-completion results. |
| 32 | class ResultBuilder { |
| 33 | public: |
| 34 | /// \brief The type of a name-lookup filter, which can be provided to the |
| 35 | /// name-lookup routines to specify which declarations should be included in |
| 36 | /// the result set (when it returns true) and which declarations should be |
| 37 | /// filtered out (returns false). |
| 38 | typedef bool (ResultBuilder::*LookupFilter)(NamedDecl *) const; |
| 39 | |
| 40 | typedef CodeCompleteConsumer::Result Result; |
| 41 | |
| 42 | private: |
| 43 | /// \brief The actual results we have found. |
| 44 | std::vector<Result> Results; |
| 45 | |
| 46 | /// \brief A record of all of the declarations we have found and placed |
| 47 | /// into the result set, used to ensure that no declaration ever gets into |
| 48 | /// the result set twice. |
| 49 | llvm::SmallPtrSet<Decl*, 16> AllDeclsFound; |
| 50 | |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 51 | typedef std::pair<NamedDecl *, unsigned> DeclIndexPair; |
| 52 | |
| 53 | /// \brief An entry in the shadow map, which is optimized to store |
| 54 | /// a single (declaration, index) mapping (the common case) but |
| 55 | /// can also store a list of (declaration, index) mappings. |
| 56 | class ShadowMapEntry { |
| 57 | typedef llvm::SmallVector<DeclIndexPair, 4> DeclIndexPairVector; |
| 58 | |
| 59 | /// \brief Contains either the solitary NamedDecl * or a vector |
| 60 | /// of (declaration, index) pairs. |
| 61 | llvm::PointerUnion<NamedDecl *, DeclIndexPairVector*> DeclOrVector; |
| 62 | |
| 63 | /// \brief When the entry contains a single declaration, this is |
| 64 | /// the index associated with that entry. |
| 65 | unsigned SingleDeclIndex; |
| 66 | |
| 67 | public: |
| 68 | ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { } |
| 69 | |
| 70 | void Add(NamedDecl *ND, unsigned Index) { |
| 71 | if (DeclOrVector.isNull()) { |
| 72 | // 0 - > 1 elements: just set the single element information. |
| 73 | DeclOrVector = ND; |
| 74 | SingleDeclIndex = Index; |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | if (NamedDecl *PrevND = DeclOrVector.dyn_cast<NamedDecl *>()) { |
| 79 | // 1 -> 2 elements: create the vector of results and push in the |
| 80 | // existing declaration. |
| 81 | DeclIndexPairVector *Vec = new DeclIndexPairVector; |
| 82 | Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex)); |
| 83 | DeclOrVector = Vec; |
| 84 | } |
| 85 | |
| 86 | // Add the new element to the end of the vector. |
| 87 | DeclOrVector.get<DeclIndexPairVector*>()->push_back( |
| 88 | DeclIndexPair(ND, Index)); |
| 89 | } |
| 90 | |
| 91 | void Destroy() { |
| 92 | if (DeclIndexPairVector *Vec |
| 93 | = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) { |
| 94 | delete Vec; |
| 95 | DeclOrVector = ((NamedDecl *)0); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Iteration. |
| 100 | class iterator; |
| 101 | iterator begin() const; |
| 102 | iterator end() const; |
| 103 | }; |
| 104 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 105 | /// \brief A mapping from declaration names to the declarations that have |
| 106 | /// this name within a particular scope and their index within the list of |
| 107 | /// results. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 108 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 109 | |
| 110 | /// \brief The semantic analysis object for which results are being |
| 111 | /// produced. |
| 112 | Sema &SemaRef; |
| 113 | |
| 114 | /// \brief If non-NULL, a filter function used to remove any code-completion |
| 115 | /// results that are not desirable. |
| 116 | LookupFilter Filter; |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 117 | |
| 118 | /// \brief Whether we should allow declarations as |
| 119 | /// nested-name-specifiers that would otherwise be filtered out. |
| 120 | bool AllowNestedNameSpecifiers; |
| 121 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 122 | /// \brief A list of shadow maps, which is used to model name hiding at |
| 123 | /// different levels of, e.g., the inheritance hierarchy. |
| 124 | std::list<ShadowMap> ShadowMaps; |
| 125 | |
| 126 | public: |
| 127 | explicit ResultBuilder(Sema &SemaRef, LookupFilter Filter = 0) |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 128 | : SemaRef(SemaRef), Filter(Filter), AllowNestedNameSpecifiers(false) { } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 129 | |
| 130 | /// \brief Set the filter used for code-completion results. |
| 131 | void setFilter(LookupFilter Filter) { |
| 132 | this->Filter = Filter; |
| 133 | } |
| 134 | |
| 135 | typedef std::vector<Result>::iterator iterator; |
| 136 | iterator begin() { return Results.begin(); } |
| 137 | iterator end() { return Results.end(); } |
| 138 | |
| 139 | Result *data() { return Results.empty()? 0 : &Results.front(); } |
| 140 | unsigned size() const { return Results.size(); } |
| 141 | bool empty() const { return Results.empty(); } |
| 142 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 143 | /// \brief Specify whether nested-name-specifiers are allowed. |
| 144 | void allowNestedNameSpecifiers(bool Allow = true) { |
| 145 | AllowNestedNameSpecifiers = Allow; |
| 146 | } |
| 147 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 148 | /// \brief Determine whether the given declaration is at all interesting |
| 149 | /// as a code-completion result. |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 150 | /// |
| 151 | /// \param ND the declaration that we are inspecting. |
| 152 | /// |
| 153 | /// \param AsNestedNameSpecifier will be set true if this declaration is |
| 154 | /// only interesting when it is a nested-name-specifier. |
| 155 | bool isInterestingDecl(NamedDecl *ND, bool &AsNestedNameSpecifier) const; |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 156 | |
| 157 | /// \brief Check whether the result is hidden by the Hiding declaration. |
| 158 | /// |
| 159 | /// \returns true if the result is hidden and cannot be found, false if |
| 160 | /// the hidden result could still be found. When false, \p R may be |
| 161 | /// modified to describe how the result can be found (e.g., via extra |
| 162 | /// qualification). |
| 163 | bool CheckHiddenResult(Result &R, DeclContext *CurContext, |
| 164 | NamedDecl *Hiding); |
| 165 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 166 | /// \brief Add a new result to this result set (if it isn't already in one |
| 167 | /// of the shadow maps), or replace an existing result (for, e.g., a |
| 168 | /// redeclaration). |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 169 | /// |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 170 | /// \param CurContext the result to add (if it is unique). |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 171 | /// |
| 172 | /// \param R the context in which this result will be named. |
| 173 | void MaybeAddResult(Result R, DeclContext *CurContext = 0); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 174 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 175 | /// \brief Add a new result to this result set, where we already know |
| 176 | /// the hiding declation (if any). |
| 177 | /// |
| 178 | /// \param R the result to add (if it is unique). |
| 179 | /// |
| 180 | /// \param CurContext the context in which this result will be named. |
| 181 | /// |
| 182 | /// \param Hiding the declaration that hides the result. |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 183 | /// |
| 184 | /// \param InBaseClass whether the result was found in a base |
| 185 | /// class of the searched context. |
| 186 | void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding, |
| 187 | bool InBaseClass); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 188 | |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 189 | /// \brief Add a new non-declaration result to this result set. |
| 190 | void AddResult(Result R); |
| 191 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 192 | /// \brief Enter into a new scope. |
| 193 | void EnterNewScope(); |
| 194 | |
| 195 | /// \brief Exit from the current scope. |
| 196 | void ExitScope(); |
| 197 | |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 198 | /// \brief Ignore this declaration, if it is seen again. |
| 199 | void Ignore(Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); } |
| 200 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 201 | /// \name Name lookup predicates |
| 202 | /// |
| 203 | /// These predicates can be passed to the name lookup functions to filter the |
| 204 | /// results of name lookup. All of the predicates have the same type, so that |
| 205 | /// |
| 206 | //@{ |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 207 | bool IsOrdinaryName(NamedDecl *ND) const; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 208 | bool IsOrdinaryNonValueName(NamedDecl *ND) const; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 209 | bool IsNestedNameSpecifier(NamedDecl *ND) const; |
| 210 | bool IsEnum(NamedDecl *ND) const; |
| 211 | bool IsClassOrStruct(NamedDecl *ND) const; |
| 212 | bool IsUnion(NamedDecl *ND) const; |
| 213 | bool IsNamespace(NamedDecl *ND) const; |
| 214 | bool IsNamespaceOrAlias(NamedDecl *ND) const; |
| 215 | bool IsType(NamedDecl *ND) const; |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 216 | bool IsMember(NamedDecl *ND) const; |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 217 | bool IsObjCIvar(NamedDecl *ND) const; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 218 | //@} |
| 219 | }; |
| 220 | } |
| 221 | |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 222 | class ResultBuilder::ShadowMapEntry::iterator { |
| 223 | llvm::PointerUnion<NamedDecl*, const DeclIndexPair*> DeclOrIterator; |
| 224 | unsigned SingleDeclIndex; |
| 225 | |
| 226 | public: |
| 227 | typedef DeclIndexPair value_type; |
| 228 | typedef value_type reference; |
| 229 | typedef std::ptrdiff_t difference_type; |
| 230 | typedef std::input_iterator_tag iterator_category; |
| 231 | |
| 232 | class pointer { |
| 233 | DeclIndexPair Value; |
| 234 | |
| 235 | public: |
| 236 | pointer(const DeclIndexPair &Value) : Value(Value) { } |
| 237 | |
| 238 | const DeclIndexPair *operator->() const { |
| 239 | return &Value; |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | iterator() : DeclOrIterator((NamedDecl *)0), SingleDeclIndex(0) { } |
| 244 | |
| 245 | iterator(NamedDecl *SingleDecl, unsigned Index) |
| 246 | : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { } |
| 247 | |
| 248 | iterator(const DeclIndexPair *Iterator) |
| 249 | : DeclOrIterator(Iterator), SingleDeclIndex(0) { } |
| 250 | |
| 251 | iterator &operator++() { |
| 252 | if (DeclOrIterator.is<NamedDecl *>()) { |
| 253 | DeclOrIterator = (NamedDecl *)0; |
| 254 | SingleDeclIndex = 0; |
| 255 | return *this; |
| 256 | } |
| 257 | |
| 258 | const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>(); |
| 259 | ++I; |
| 260 | DeclOrIterator = I; |
| 261 | return *this; |
| 262 | } |
| 263 | |
| 264 | iterator operator++(int) { |
| 265 | iterator tmp(*this); |
| 266 | ++(*this); |
| 267 | return tmp; |
| 268 | } |
| 269 | |
| 270 | reference operator*() const { |
| 271 | if (NamedDecl *ND = DeclOrIterator.dyn_cast<NamedDecl *>()) |
| 272 | return reference(ND, SingleDeclIndex); |
| 273 | |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 274 | return *DeclOrIterator.get<const DeclIndexPair*>(); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | pointer operator->() const { |
| 278 | return pointer(**this); |
| 279 | } |
| 280 | |
| 281 | friend bool operator==(const iterator &X, const iterator &Y) { |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 282 | return X.DeclOrIterator.getOpaqueValue() |
| 283 | == Y.DeclOrIterator.getOpaqueValue() && |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 284 | X.SingleDeclIndex == Y.SingleDeclIndex; |
| 285 | } |
| 286 | |
| 287 | friend bool operator!=(const iterator &X, const iterator &Y) { |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 288 | return !(X == Y); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 289 | } |
| 290 | }; |
| 291 | |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 292 | ResultBuilder::ShadowMapEntry::iterator |
| 293 | ResultBuilder::ShadowMapEntry::begin() const { |
| 294 | if (DeclOrVector.isNull()) |
| 295 | return iterator(); |
| 296 | |
| 297 | if (NamedDecl *ND = DeclOrVector.dyn_cast<NamedDecl *>()) |
| 298 | return iterator(ND, SingleDeclIndex); |
| 299 | |
| 300 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin()); |
| 301 | } |
| 302 | |
| 303 | ResultBuilder::ShadowMapEntry::iterator |
| 304 | ResultBuilder::ShadowMapEntry::end() const { |
| 305 | if (DeclOrVector.is<NamedDecl *>() || DeclOrVector.isNull()) |
| 306 | return iterator(); |
| 307 | |
| 308 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end()); |
| 309 | } |
| 310 | |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 311 | /// \brief Compute the qualification required to get from the current context |
| 312 | /// (\p CurContext) to the target context (\p TargetContext). |
| 313 | /// |
| 314 | /// \param Context the AST context in which the qualification will be used. |
| 315 | /// |
| 316 | /// \param CurContext the context where an entity is being named, which is |
| 317 | /// typically based on the current scope. |
| 318 | /// |
| 319 | /// \param TargetContext the context in which the named entity actually |
| 320 | /// resides. |
| 321 | /// |
| 322 | /// \returns a nested name specifier that refers into the target context, or |
| 323 | /// NULL if no qualification is needed. |
| 324 | static NestedNameSpecifier * |
| 325 | getRequiredQualification(ASTContext &Context, |
| 326 | DeclContext *CurContext, |
| 327 | DeclContext *TargetContext) { |
| 328 | llvm::SmallVector<DeclContext *, 4> TargetParents; |
| 329 | |
| 330 | for (DeclContext *CommonAncestor = TargetContext; |
| 331 | CommonAncestor && !CommonAncestor->Encloses(CurContext); |
| 332 | CommonAncestor = CommonAncestor->getLookupParent()) { |
| 333 | if (CommonAncestor->isTransparentContext() || |
| 334 | CommonAncestor->isFunctionOrMethod()) |
| 335 | continue; |
| 336 | |
| 337 | TargetParents.push_back(CommonAncestor); |
| 338 | } |
| 339 | |
| 340 | NestedNameSpecifier *Result = 0; |
| 341 | while (!TargetParents.empty()) { |
| 342 | DeclContext *Parent = TargetParents.back(); |
| 343 | TargetParents.pop_back(); |
| 344 | |
| 345 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) |
| 346 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); |
| 347 | else if (TagDecl *TD = dyn_cast<TagDecl>(Parent)) |
| 348 | Result = NestedNameSpecifier::Create(Context, Result, |
| 349 | false, |
| 350 | Context.getTypeDeclType(TD).getTypePtr()); |
| 351 | else |
| 352 | assert(Parent->isTranslationUnit()); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 353 | } |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 354 | return Result; |
| 355 | } |
| 356 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 357 | bool ResultBuilder::isInterestingDecl(NamedDecl *ND, |
| 358 | bool &AsNestedNameSpecifier) const { |
| 359 | AsNestedNameSpecifier = false; |
| 360 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 361 | ND = ND->getUnderlyingDecl(); |
| 362 | unsigned IDNS = ND->getIdentifierNamespace(); |
Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 363 | |
| 364 | // Skip unnamed entities. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 365 | if (!ND->getDeclName()) |
| 366 | return false; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 367 | |
| 368 | // Friend declarations and declarations introduced due to friends are never |
| 369 | // added as results. |
John McCall | bbbbe4e | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 370 | if (IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend)) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 371 | return false; |
| 372 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 373 | // Class template (partial) specializations are never added as results. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 374 | if (isa<ClassTemplateSpecializationDecl>(ND) || |
| 375 | isa<ClassTemplatePartialSpecializationDecl>(ND)) |
| 376 | return false; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 377 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 378 | // Using declarations themselves are never added as results. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 379 | if (isa<UsingDecl>(ND)) |
| 380 | return false; |
| 381 | |
| 382 | // Some declarations have reserved names that we don't want to ever show. |
| 383 | if (const IdentifierInfo *Id = ND->getIdentifier()) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 384 | // __va_list_tag is a freak of nature. Find it and skip it. |
| 385 | if (Id->isStr("__va_list_tag") || Id->isStr("__builtin_va_list")) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 386 | return false; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 387 | |
Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 388 | // Filter out names reserved for the implementation (C99 7.1.3, |
| 389 | // C++ [lib.global.names]). Users don't need to see those. |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 390 | // |
| 391 | // FIXME: Add predicate for this. |
Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 392 | if (Id->getLength() >= 2) { |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 393 | const char *Name = Id->getNameStart(); |
Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 394 | if (Name[0] == '_' && |
| 395 | (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z'))) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 396 | return false; |
Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 397 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 398 | } |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 399 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 400 | // C++ constructors are never found by name lookup. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 401 | if (isa<CXXConstructorDecl>(ND)) |
| 402 | return false; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 403 | |
| 404 | // Filter out any unwanted results. |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 405 | if (Filter && !(this->*Filter)(ND)) { |
| 406 | // Check whether it is interesting as a nested-name-specifier. |
| 407 | if (AllowNestedNameSpecifiers && SemaRef.getLangOptions().CPlusPlus && |
| 408 | IsNestedNameSpecifier(ND) && |
| 409 | (Filter != &ResultBuilder::IsMember || |
| 410 | (isa<CXXRecordDecl>(ND) && |
| 411 | cast<CXXRecordDecl>(ND)->isInjectedClassName()))) { |
| 412 | AsNestedNameSpecifier = true; |
| 413 | return true; |
| 414 | } |
| 415 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 416 | return false; |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 417 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 418 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 419 | // ... then it must be interesting! |
| 420 | return true; |
| 421 | } |
| 422 | |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 423 | bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext, |
| 424 | NamedDecl *Hiding) { |
| 425 | // In C, there is no way to refer to a hidden name. |
| 426 | // FIXME: This isn't true; we can find a tag name hidden by an ordinary |
| 427 | // name if we introduce the tag type. |
| 428 | if (!SemaRef.getLangOptions().CPlusPlus) |
| 429 | return true; |
| 430 | |
| 431 | DeclContext *HiddenCtx = R.Declaration->getDeclContext()->getLookupContext(); |
| 432 | |
| 433 | // There is no way to qualify a name declared in a function or method. |
| 434 | if (HiddenCtx->isFunctionOrMethod()) |
| 435 | return true; |
| 436 | |
| 437 | if (HiddenCtx == Hiding->getDeclContext()->getLookupContext()) |
| 438 | return true; |
| 439 | |
| 440 | // We can refer to the result with the appropriate qualification. Do it. |
| 441 | R.Hidden = true; |
| 442 | R.QualifierIsInformative = false; |
| 443 | |
| 444 | if (!R.Qualifier) |
| 445 | R.Qualifier = getRequiredQualification(SemaRef.Context, |
| 446 | CurContext, |
| 447 | R.Declaration->getDeclContext()); |
| 448 | return false; |
| 449 | } |
| 450 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 451 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { |
| 452 | assert(!ShadowMaps.empty() && "Must enter into a results scope"); |
| 453 | |
| 454 | if (R.Kind != Result::RK_Declaration) { |
| 455 | // For non-declaration results, just add the result. |
| 456 | Results.push_back(R); |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | // Look through using declarations. |
| 461 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
| 462 | MaybeAddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext); |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
| 467 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 468 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 469 | bool AsNestedNameSpecifier = false; |
| 470 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 471 | return; |
| 472 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 473 | ShadowMap &SMap = ShadowMaps.back(); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 474 | ShadowMapEntry::iterator I, IEnd; |
| 475 | ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName()); |
| 476 | if (NamePos != SMap.end()) { |
| 477 | I = NamePos->second.begin(); |
| 478 | IEnd = NamePos->second.end(); |
| 479 | } |
| 480 | |
| 481 | for (; I != IEnd; ++I) { |
| 482 | NamedDecl *ND = I->first; |
| 483 | unsigned Index = I->second; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 484 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 485 | // This is a redeclaration. Always pick the newer declaration. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 486 | Results[Index].Declaration = R.Declaration; |
| 487 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 488 | // We're done. |
| 489 | return; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // This is a new declaration in this scope. However, check whether this |
| 494 | // declaration name is hidden by a similarly-named declaration in an outer |
| 495 | // scope. |
| 496 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 497 | --SMEnd; |
| 498 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 499 | ShadowMapEntry::iterator I, IEnd; |
| 500 | ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName()); |
| 501 | if (NamePos != SM->end()) { |
| 502 | I = NamePos->second.begin(); |
| 503 | IEnd = NamePos->second.end(); |
| 504 | } |
| 505 | for (; I != IEnd; ++I) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 506 | // A tag declaration does not hide a non-tag declaration. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 507 | if (I->first->getIdentifierNamespace() == Decl::IDNS_Tag && |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 508 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 509 | Decl::IDNS_ObjCProtocol))) |
| 510 | continue; |
| 511 | |
| 512 | // Protocols are in distinct namespaces from everything else. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 513 | if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 514 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 515 | I->first->getIdentifierNamespace() != IDNS) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 516 | continue; |
| 517 | |
| 518 | // 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] | 519 | if (CheckHiddenResult(R, CurContext, I->first)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 520 | return; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 521 | |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | // Make sure that any given declaration only shows up in the result set once. |
| 527 | if (!AllDeclsFound.insert(CanonDecl)) |
| 528 | return; |
| 529 | |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 530 | // If the filter is for nested-name-specifiers, then this result starts a |
| 531 | // nested-name-specifier. |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 532 | if (AsNestedNameSpecifier) |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 533 | R.StartsNestedNameSpecifier = true; |
| 534 | |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 535 | // If this result is supposed to have an informative qualifier, add one. |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 536 | if (R.QualifierIsInformative && !R.Qualifier && |
| 537 | !R.StartsNestedNameSpecifier) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 538 | DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 539 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 540 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace); |
| 541 | else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
| 542 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false, |
| 543 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
| 544 | else |
| 545 | R.QualifierIsInformative = false; |
| 546 | } |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 547 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 548 | // Insert this result into the set of results and into the current shadow |
| 549 | // map. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 550 | SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 551 | Results.push_back(R); |
| 552 | } |
| 553 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 554 | void ResultBuilder::AddResult(Result R, DeclContext *CurContext, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 555 | NamedDecl *Hiding, bool InBaseClass = false) { |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 556 | if (R.Kind != Result::RK_Declaration) { |
| 557 | // For non-declaration results, just add the result. |
| 558 | Results.push_back(R); |
| 559 | return; |
| 560 | } |
| 561 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 562 | // Look through using declarations. |
| 563 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
| 564 | AddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext, Hiding); |
| 565 | return; |
| 566 | } |
| 567 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 568 | bool AsNestedNameSpecifier = false; |
| 569 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 570 | return; |
| 571 | |
| 572 | if (Hiding && CheckHiddenResult(R, CurContext, Hiding)) |
| 573 | return; |
| 574 | |
| 575 | // Make sure that any given declaration only shows up in the result set once. |
| 576 | if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl())) |
| 577 | return; |
| 578 | |
| 579 | // If the filter is for nested-name-specifiers, then this result starts a |
| 580 | // nested-name-specifier. |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 581 | if (AsNestedNameSpecifier) |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 582 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 583 | else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass && |
| 584 | isa<CXXRecordDecl>(R.Declaration->getDeclContext() |
| 585 | ->getLookupContext())) |
| 586 | R.QualifierIsInformative = true; |
| 587 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 588 | // If this result is supposed to have an informative qualifier, add one. |
| 589 | if (R.QualifierIsInformative && !R.Qualifier && |
| 590 | !R.StartsNestedNameSpecifier) { |
| 591 | DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 592 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
| 593 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace); |
| 594 | else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
| 595 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false, |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 596 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 597 | else |
| 598 | R.QualifierIsInformative = false; |
| 599 | } |
| 600 | |
| 601 | // Insert this result into the set of results. |
| 602 | Results.push_back(R); |
| 603 | } |
| 604 | |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 605 | void ResultBuilder::AddResult(Result R) { |
| 606 | assert(R.Kind != Result::RK_Declaration && |
| 607 | "Declaration results need more context"); |
| 608 | Results.push_back(R); |
| 609 | } |
| 610 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 611 | /// \brief Enter into a new scope. |
| 612 | void ResultBuilder::EnterNewScope() { |
| 613 | ShadowMaps.push_back(ShadowMap()); |
| 614 | } |
| 615 | |
| 616 | /// \brief Exit from the current scope. |
| 617 | void ResultBuilder::ExitScope() { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 618 | for (ShadowMap::iterator E = ShadowMaps.back().begin(), |
| 619 | EEnd = ShadowMaps.back().end(); |
| 620 | E != EEnd; |
| 621 | ++E) |
| 622 | E->second.Destroy(); |
| 623 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 624 | ShadowMaps.pop_back(); |
| 625 | } |
| 626 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 627 | /// \brief Determines whether this given declaration will be found by |
| 628 | /// ordinary name lookup. |
| 629 | bool ResultBuilder::IsOrdinaryName(NamedDecl *ND) const { |
| 630 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 631 | if (SemaRef.getLangOptions().CPlusPlus) |
| 632 | IDNS |= Decl::IDNS_Tag; |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 633 | else if (SemaRef.getLangOptions().ObjC1 && isa<ObjCIvarDecl>(ND)) |
| 634 | return true; |
| 635 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 636 | return ND->getIdentifierNamespace() & IDNS; |
| 637 | } |
| 638 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 639 | /// \brief Determines whether this given declaration will be found by |
| 640 | /// ordinary name lookup. |
| 641 | bool ResultBuilder::IsOrdinaryNonValueName(NamedDecl *ND) const { |
| 642 | unsigned IDNS = Decl::IDNS_Ordinary; |
| 643 | if (SemaRef.getLangOptions().CPlusPlus) |
| 644 | IDNS |= Decl::IDNS_Tag; |
| 645 | |
| 646 | return (ND->getIdentifierNamespace() & IDNS) && |
| 647 | !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND); |
| 648 | } |
| 649 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 650 | /// \brief Determines whether the given declaration is suitable as the |
| 651 | /// start of a C++ nested-name-specifier, e.g., a class or namespace. |
| 652 | bool ResultBuilder::IsNestedNameSpecifier(NamedDecl *ND) const { |
| 653 | // Allow us to find class templates, too. |
| 654 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 655 | ND = ClassTemplate->getTemplatedDecl(); |
| 656 | |
| 657 | return SemaRef.isAcceptableNestedNameSpecifier(ND); |
| 658 | } |
| 659 | |
| 660 | /// \brief Determines whether the given declaration is an enumeration. |
| 661 | bool ResultBuilder::IsEnum(NamedDecl *ND) const { |
| 662 | return isa<EnumDecl>(ND); |
| 663 | } |
| 664 | |
| 665 | /// \brief Determines whether the given declaration is a class or struct. |
| 666 | bool ResultBuilder::IsClassOrStruct(NamedDecl *ND) const { |
| 667 | // Allow us to find class templates, too. |
| 668 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 669 | ND = ClassTemplate->getTemplatedDecl(); |
| 670 | |
| 671 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
| 672 | return RD->getTagKind() == TagDecl::TK_class || |
| 673 | RD->getTagKind() == TagDecl::TK_struct; |
| 674 | |
| 675 | return false; |
| 676 | } |
| 677 | |
| 678 | /// \brief Determines whether the given declaration is a union. |
| 679 | bool ResultBuilder::IsUnion(NamedDecl *ND) const { |
| 680 | // Allow us to find class templates, too. |
| 681 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| 682 | ND = ClassTemplate->getTemplatedDecl(); |
| 683 | |
| 684 | if (RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
| 685 | return RD->getTagKind() == TagDecl::TK_union; |
| 686 | |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | /// \brief Determines whether the given declaration is a namespace. |
| 691 | bool ResultBuilder::IsNamespace(NamedDecl *ND) const { |
| 692 | return isa<NamespaceDecl>(ND); |
| 693 | } |
| 694 | |
| 695 | /// \brief Determines whether the given declaration is a namespace or |
| 696 | /// namespace alias. |
| 697 | bool ResultBuilder::IsNamespaceOrAlias(NamedDecl *ND) const { |
| 698 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
| 699 | } |
| 700 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 701 | /// \brief Determines whether the given declaration is a type. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 702 | bool ResultBuilder::IsType(NamedDecl *ND) const { |
| 703 | return isa<TypeDecl>(ND); |
| 704 | } |
| 705 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 706 | /// \brief Determines which members of a class should be visible via |
| 707 | /// "." or "->". Only value declarations, nested name specifiers, and |
| 708 | /// using declarations thereof should show up. |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 709 | bool ResultBuilder::IsMember(NamedDecl *ND) const { |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 710 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND)) |
| 711 | ND = Using->getTargetDecl(); |
| 712 | |
Douglas Gregor | 7078839 | 2009-12-11 18:14:22 +0000 | [diff] [blame] | 713 | return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) || |
| 714 | isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 717 | /// \rief Determines whether the given declaration is an Objective-C |
| 718 | /// instance variable. |
| 719 | bool ResultBuilder::IsObjCIvar(NamedDecl *ND) const { |
| 720 | return isa<ObjCIvarDecl>(ND); |
| 721 | } |
| 722 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 723 | namespace { |
| 724 | /// \brief Visible declaration consumer that adds a code-completion result |
| 725 | /// for each visible declaration. |
| 726 | class CodeCompletionDeclConsumer : public VisibleDeclConsumer { |
| 727 | ResultBuilder &Results; |
| 728 | DeclContext *CurContext; |
| 729 | |
| 730 | public: |
| 731 | CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext) |
| 732 | : Results(Results), CurContext(CurContext) { } |
| 733 | |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 734 | virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, bool InBaseClass) { |
| 735 | Results.AddResult(ND, CurContext, Hiding, InBaseClass); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 736 | } |
| 737 | }; |
| 738 | } |
| 739 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 740 | /// \brief Add type specifiers for the current language as keyword results. |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 741 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 742 | ResultBuilder &Results) { |
| 743 | typedef CodeCompleteConsumer::Result Result; |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 744 | Results.AddResult(Result("short")); |
| 745 | Results.AddResult(Result("long")); |
| 746 | Results.AddResult(Result("signed")); |
| 747 | Results.AddResult(Result("unsigned")); |
| 748 | Results.AddResult(Result("void")); |
| 749 | Results.AddResult(Result("char")); |
| 750 | Results.AddResult(Result("int")); |
| 751 | Results.AddResult(Result("float")); |
| 752 | Results.AddResult(Result("double")); |
| 753 | Results.AddResult(Result("enum")); |
| 754 | Results.AddResult(Result("struct")); |
| 755 | Results.AddResult(Result("union")); |
| 756 | Results.AddResult(Result("const")); |
| 757 | Results.AddResult(Result("volatile")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 758 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 759 | if (LangOpts.C99) { |
| 760 | // C99-specific |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 761 | Results.AddResult(Result("_Complex")); |
| 762 | Results.AddResult(Result("_Imaginary")); |
| 763 | Results.AddResult(Result("_Bool")); |
| 764 | Results.AddResult(Result("restrict")); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | if (LangOpts.CPlusPlus) { |
| 768 | // C++-specific |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 769 | Results.AddResult(Result("bool")); |
| 770 | Results.AddResult(Result("class")); |
| 771 | Results.AddResult(Result("wchar_t")); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 772 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 773 | // typename qualified-id |
| 774 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 775 | Pattern->AddTypedTextChunk("typename"); |
| 776 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 777 | Pattern->AddPlaceholderChunk("qualified-id"); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 778 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 779 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 780 | if (LangOpts.CPlusPlus0x) { |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 781 | Results.AddResult(Result("auto")); |
| 782 | Results.AddResult(Result("char16_t")); |
| 783 | Results.AddResult(Result("char32_t")); |
| 784 | Results.AddResult(Result("decltype")); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
| 788 | // GNU extensions |
| 789 | if (LangOpts.GNUMode) { |
| 790 | // FIXME: Enable when we actually support decimal floating point. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 791 | // Results.AddResult(Result("_Decimal32")); |
| 792 | // Results.AddResult(Result("_Decimal64")); |
| 793 | // Results.AddResult(Result("_Decimal128")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 794 | |
| 795 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 796 | Pattern->AddTypedTextChunk("typeof"); |
| 797 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 798 | Pattern->AddPlaceholderChunk("expression-or-type"); |
| 799 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 800 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 804 | static void AddStorageSpecifiers(Action::CodeCompletionContext CCC, |
| 805 | const LangOptions &LangOpts, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 806 | ResultBuilder &Results) { |
| 807 | typedef CodeCompleteConsumer::Result Result; |
| 808 | // Note: we don't suggest either "auto" or "register", because both |
| 809 | // are pointless as storage specifiers. Elsewhere, we suggest "auto" |
| 810 | // in C++0x as a type specifier. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 811 | Results.AddResult(Result("extern")); |
| 812 | Results.AddResult(Result("static")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | static void AddFunctionSpecifiers(Action::CodeCompletionContext CCC, |
| 816 | const LangOptions &LangOpts, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 817 | ResultBuilder &Results) { |
| 818 | typedef CodeCompleteConsumer::Result Result; |
| 819 | switch (CCC) { |
| 820 | case Action::CCC_Class: |
| 821 | case Action::CCC_MemberTemplate: |
| 822 | if (LangOpts.CPlusPlus) { |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 823 | Results.AddResult(Result("explicit")); |
| 824 | Results.AddResult(Result("friend")); |
| 825 | Results.AddResult(Result("mutable")); |
| 826 | Results.AddResult(Result("virtual")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 827 | } |
| 828 | // Fall through |
| 829 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 830 | case Action::CCC_ObjCInterface: |
| 831 | case Action::CCC_ObjCImplementation: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 832 | case Action::CCC_Namespace: |
| 833 | case Action::CCC_Template: |
| 834 | if (LangOpts.CPlusPlus || LangOpts.C99) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 835 | Results.AddResult(Result("inline")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 836 | break; |
| 837 | |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 838 | case Action::CCC_ObjCInstanceVariableList: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 839 | case Action::CCC_Expression: |
| 840 | case Action::CCC_Statement: |
| 841 | case Action::CCC_ForInit: |
| 842 | case Action::CCC_Condition: |
| 843 | break; |
| 844 | } |
| 845 | } |
| 846 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 847 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); |
| 848 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); |
| 849 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 850 | ResultBuilder &Results, |
| 851 | bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 852 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 853 | ResultBuilder &Results, |
| 854 | bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 855 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 856 | ResultBuilder &Results, |
| 857 | bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 858 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 859 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 860 | /// \brief Add language constructs that show up for "ordinary" names. |
| 861 | static void AddOrdinaryNameResults(Action::CodeCompletionContext CCC, |
| 862 | Scope *S, |
| 863 | Sema &SemaRef, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 864 | ResultBuilder &Results) { |
| 865 | typedef CodeCompleteConsumer::Result Result; |
| 866 | switch (CCC) { |
| 867 | case Action::CCC_Namespace: |
| 868 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 869 | // namespace <identifier> { } |
| 870 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 871 | Pattern->AddTypedTextChunk("namespace"); |
| 872 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 873 | Pattern->AddPlaceholderChunk("identifier"); |
| 874 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 875 | Pattern->AddPlaceholderChunk("declarations"); |
| 876 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 877 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 878 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 879 | |
| 880 | // namespace identifier = identifier ; |
| 881 | Pattern = new CodeCompletionString; |
| 882 | Pattern->AddTypedTextChunk("namespace"); |
| 883 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 884 | Pattern->AddPlaceholderChunk("identifier"); |
| 885 | Pattern->AddChunk(CodeCompletionString::CK_Equal); |
| 886 | Pattern->AddPlaceholderChunk("identifier"); |
| 887 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 888 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 889 | |
| 890 | // Using directives |
| 891 | Pattern = new CodeCompletionString; |
| 892 | Pattern->AddTypedTextChunk("using"); |
| 893 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 894 | Pattern->AddTextChunk("namespace"); |
| 895 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 896 | Pattern->AddPlaceholderChunk("identifier"); |
| 897 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 898 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 899 | |
| 900 | // asm(string-literal) |
| 901 | Pattern = new CodeCompletionString; |
| 902 | Pattern->AddTypedTextChunk("asm"); |
| 903 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 904 | Pattern->AddPlaceholderChunk("string-literal"); |
| 905 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 906 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 907 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 908 | |
| 909 | // Explicit template instantiation |
| 910 | Pattern = new CodeCompletionString; |
| 911 | Pattern->AddTypedTextChunk("template"); |
| 912 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 913 | Pattern->AddPlaceholderChunk("declaration"); |
| 914 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 915 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 916 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 917 | |
| 918 | if (SemaRef.getLangOptions().ObjC1) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 919 | AddObjCTopLevelResults(Results, true); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 920 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 921 | // Fall through |
| 922 | |
| 923 | case Action::CCC_Class: |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 924 | Results.AddResult(Result("typedef")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 925 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 926 | // Using declaration |
| 927 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 928 | Pattern->AddTypedTextChunk("using"); |
| 929 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 930 | Pattern->AddPlaceholderChunk("qualified-id"); |
| 931 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 932 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 933 | |
| 934 | // using typename qualified-id; (only in a dependent context) |
| 935 | if (SemaRef.CurContext->isDependentContext()) { |
| 936 | Pattern = new CodeCompletionString; |
| 937 | Pattern->AddTypedTextChunk("using"); |
| 938 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 939 | Pattern->AddTextChunk("typename"); |
| 940 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 941 | Pattern->AddPlaceholderChunk("qualified-id"); |
| 942 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 943 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | if (CCC == Action::CCC_Class) { |
| 947 | // public: |
| 948 | Pattern = new CodeCompletionString; |
| 949 | Pattern->AddTypedTextChunk("public"); |
| 950 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 951 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 952 | |
| 953 | // protected: |
| 954 | Pattern = new CodeCompletionString; |
| 955 | Pattern->AddTypedTextChunk("protected"); |
| 956 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 957 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 958 | |
| 959 | // private: |
| 960 | Pattern = new CodeCompletionString; |
| 961 | Pattern->AddTypedTextChunk("private"); |
| 962 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 963 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | // Fall through |
| 967 | |
| 968 | case Action::CCC_Template: |
| 969 | case Action::CCC_MemberTemplate: |
| 970 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 971 | // template < parameters > |
| 972 | CodeCompletionString *Pattern = new CodeCompletionString; |
| 973 | Pattern->AddTypedTextChunk("template"); |
| 974 | Pattern->AddChunk(CodeCompletionString::CK_LeftAngle); |
| 975 | Pattern->AddPlaceholderChunk("parameters"); |
| 976 | Pattern->AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 977 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 980 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 981 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 982 | break; |
| 983 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 984 | case Action::CCC_ObjCInterface: |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 985 | AddObjCInterfaceResults(SemaRef.getLangOptions(), Results, true); |
| 986 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 987 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 988 | break; |
| 989 | |
| 990 | case Action::CCC_ObjCImplementation: |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 991 | AddObjCImplementationResults(SemaRef.getLangOptions(), Results, true); |
| 992 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
| 993 | AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 994 | break; |
| 995 | |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 996 | case Action::CCC_ObjCInstanceVariableList: |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 997 | AddObjCVisibilityResults(SemaRef.getLangOptions(), Results, true); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 998 | break; |
| 999 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1000 | case Action::CCC_Statement: { |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1001 | Results.AddResult(Result("typedef")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1002 | |
| 1003 | CodeCompletionString *Pattern = 0; |
| 1004 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 1005 | Pattern = new CodeCompletionString; |
| 1006 | Pattern->AddTypedTextChunk("try"); |
| 1007 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1008 | Pattern->AddPlaceholderChunk("statements"); |
| 1009 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1010 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 1011 | Pattern->AddTextChunk("catch"); |
| 1012 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1013 | Pattern->AddPlaceholderChunk("declaration"); |
| 1014 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1015 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1016 | Pattern->AddPlaceholderChunk("statements"); |
| 1017 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1018 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1019 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1020 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1021 | if (SemaRef.getLangOptions().ObjC1) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1022 | AddObjCStatementResults(Results, true); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1023 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1024 | // if (condition) { statements } |
| 1025 | Pattern = new CodeCompletionString; |
| 1026 | Pattern->AddTypedTextChunk("if"); |
| 1027 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1028 | if (SemaRef.getLangOptions().CPlusPlus) |
| 1029 | Pattern->AddPlaceholderChunk("condition"); |
| 1030 | else |
| 1031 | Pattern->AddPlaceholderChunk("expression"); |
| 1032 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1033 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1034 | Pattern->AddPlaceholderChunk("statements"); |
| 1035 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1036 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1037 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1038 | |
| 1039 | // switch (condition) { } |
| 1040 | Pattern = new CodeCompletionString; |
| 1041 | Pattern->AddTypedTextChunk("switch"); |
| 1042 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1043 | if (SemaRef.getLangOptions().CPlusPlus) |
| 1044 | Pattern->AddPlaceholderChunk("condition"); |
| 1045 | else |
| 1046 | Pattern->AddPlaceholderChunk("expression"); |
| 1047 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1048 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1049 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1050 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1051 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1052 | |
| 1053 | // Switch-specific statements. |
| 1054 | if (!SemaRef.getSwitchStack().empty()) { |
| 1055 | // case expression: |
| 1056 | Pattern = new CodeCompletionString; |
| 1057 | Pattern->AddTypedTextChunk("case"); |
| 1058 | Pattern->AddPlaceholderChunk("expression"); |
| 1059 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1060 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1061 | |
| 1062 | // default: |
| 1063 | Pattern = new CodeCompletionString; |
| 1064 | Pattern->AddTypedTextChunk("default"); |
| 1065 | Pattern->AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1066 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | /// while (condition) { statements } |
| 1070 | Pattern = new CodeCompletionString; |
| 1071 | Pattern->AddTypedTextChunk("while"); |
| 1072 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1073 | if (SemaRef.getLangOptions().CPlusPlus) |
| 1074 | Pattern->AddPlaceholderChunk("condition"); |
| 1075 | else |
| 1076 | Pattern->AddPlaceholderChunk("expression"); |
| 1077 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1078 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1079 | Pattern->AddPlaceholderChunk("statements"); |
| 1080 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1081 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1082 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1083 | |
| 1084 | // do { statements } while ( expression ); |
| 1085 | Pattern = new CodeCompletionString; |
| 1086 | Pattern->AddTypedTextChunk("do"); |
| 1087 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1088 | Pattern->AddPlaceholderChunk("statements"); |
| 1089 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1090 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 1091 | Pattern->AddTextChunk("while"); |
| 1092 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1093 | Pattern->AddPlaceholderChunk("expression"); |
| 1094 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1095 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1096 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1097 | |
| 1098 | // for ( for-init-statement ; condition ; expression ) { statements } |
| 1099 | Pattern = new CodeCompletionString; |
| 1100 | Pattern->AddTypedTextChunk("for"); |
| 1101 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1102 | if (SemaRef.getLangOptions().CPlusPlus || SemaRef.getLangOptions().C99) |
| 1103 | Pattern->AddPlaceholderChunk("init-statement"); |
| 1104 | else |
| 1105 | Pattern->AddPlaceholderChunk("init-expression"); |
| 1106 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
| 1107 | Pattern->AddPlaceholderChunk("condition"); |
| 1108 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
| 1109 | Pattern->AddPlaceholderChunk("inc-expression"); |
| 1110 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 1111 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1112 | Pattern->AddPlaceholderChunk("statements"); |
| 1113 | Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1114 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1115 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1116 | |
| 1117 | if (S->getContinueParent()) { |
| 1118 | // continue ; |
| 1119 | Pattern = new CodeCompletionString; |
| 1120 | Pattern->AddTypedTextChunk("continue"); |
| 1121 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1122 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | if (S->getBreakParent()) { |
| 1126 | // break ; |
| 1127 | Pattern = new CodeCompletionString; |
| 1128 | Pattern->AddTypedTextChunk("break"); |
| 1129 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1130 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | // "return expression ;" or "return ;", depending on whether we |
| 1134 | // know the function is void or not. |
| 1135 | bool isVoid = false; |
| 1136 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) |
| 1137 | isVoid = Function->getResultType()->isVoidType(); |
| 1138 | else if (ObjCMethodDecl *Method |
| 1139 | = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) |
| 1140 | isVoid = Method->getResultType()->isVoidType(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1141 | else if (SemaRef.getCurBlock() && |
| 1142 | !SemaRef.getCurBlock()->ReturnType.isNull()) |
| 1143 | isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType(); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1144 | Pattern = new CodeCompletionString; |
| 1145 | Pattern->AddTypedTextChunk("return"); |
Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1146 | if (!isVoid) { |
| 1147 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1148 | Pattern->AddPlaceholderChunk("expression"); |
Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1149 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1150 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1151 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1152 | |
| 1153 | // goto identifier ; |
| 1154 | Pattern = new CodeCompletionString; |
| 1155 | Pattern->AddTypedTextChunk("goto"); |
| 1156 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1157 | Pattern->AddPlaceholderChunk("identifier"); |
| 1158 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1159 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1160 | |
| 1161 | // Using directives |
| 1162 | Pattern = new CodeCompletionString; |
| 1163 | Pattern->AddTypedTextChunk("using"); |
| 1164 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1165 | Pattern->AddTextChunk("namespace"); |
| 1166 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1167 | Pattern->AddPlaceholderChunk("identifier"); |
| 1168 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1169 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | // Fall through (for statement expressions). |
| 1173 | case Action::CCC_ForInit: |
| 1174 | case Action::CCC_Condition: |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1175 | AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1176 | // Fall through: conditions and statements can have expressions. |
| 1177 | |
| 1178 | case Action::CCC_Expression: { |
| 1179 | CodeCompletionString *Pattern = 0; |
| 1180 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 1181 | // 'this', if we're in a non-static member function. |
| 1182 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) |
| 1183 | if (!Method->isStatic()) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1184 | Results.AddResult(Result("this")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1185 | |
| 1186 | // true, false |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1187 | Results.AddResult(Result("true")); |
| 1188 | Results.AddResult(Result("false")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1189 | |
| 1190 | // dynamic_cast < type-id > ( expression ) |
| 1191 | Pattern = new CodeCompletionString; |
| 1192 | Pattern->AddTypedTextChunk("dynamic_cast"); |
| 1193 | Pattern->AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1194 | Pattern->AddPlaceholderChunk("type-id"); |
| 1195 | Pattern->AddChunk(CodeCompletionString::CK_RightAngle); |
| 1196 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1197 | Pattern->AddPlaceholderChunk("expression"); |
| 1198 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1199 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1200 | |
| 1201 | // static_cast < type-id > ( expression ) |
| 1202 | Pattern = new CodeCompletionString; |
| 1203 | Pattern->AddTypedTextChunk("static_cast"); |
| 1204 | Pattern->AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1205 | Pattern->AddPlaceholderChunk("type-id"); |
| 1206 | Pattern->AddChunk(CodeCompletionString::CK_RightAngle); |
| 1207 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1208 | Pattern->AddPlaceholderChunk("expression"); |
| 1209 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1210 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1211 | |
| 1212 | // reinterpret_cast < type-id > ( expression ) |
| 1213 | Pattern = new CodeCompletionString; |
| 1214 | Pattern->AddTypedTextChunk("reinterpret_cast"); |
| 1215 | Pattern->AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1216 | Pattern->AddPlaceholderChunk("type-id"); |
| 1217 | Pattern->AddChunk(CodeCompletionString::CK_RightAngle); |
| 1218 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1219 | Pattern->AddPlaceholderChunk("expression"); |
| 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 | |
| 1223 | // const_cast < type-id > ( expression ) |
| 1224 | Pattern = new CodeCompletionString; |
| 1225 | Pattern->AddTypedTextChunk("const_cast"); |
| 1226 | Pattern->AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1227 | Pattern->AddPlaceholderChunk("type-id"); |
| 1228 | Pattern->AddChunk(CodeCompletionString::CK_RightAngle); |
| 1229 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1230 | Pattern->AddPlaceholderChunk("expression"); |
| 1231 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1232 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1233 | |
| 1234 | // typeid ( expression-or-type ) |
| 1235 | Pattern = new CodeCompletionString; |
| 1236 | Pattern->AddTypedTextChunk("typeid"); |
| 1237 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1238 | Pattern->AddPlaceholderChunk("expression-or-type"); |
| 1239 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1240 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1241 | |
| 1242 | // new T ( ... ) |
| 1243 | Pattern = new CodeCompletionString; |
| 1244 | Pattern->AddTypedTextChunk("new"); |
| 1245 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1246 | Pattern->AddPlaceholderChunk("type-id"); |
| 1247 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1248 | Pattern->AddPlaceholderChunk("expressions"); |
| 1249 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1250 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1251 | |
| 1252 | // new T [ ] ( ... ) |
| 1253 | Pattern = new CodeCompletionString; |
| 1254 | Pattern->AddTypedTextChunk("new"); |
| 1255 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1256 | Pattern->AddPlaceholderChunk("type-id"); |
| 1257 | Pattern->AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1258 | Pattern->AddPlaceholderChunk("size"); |
| 1259 | Pattern->AddChunk(CodeCompletionString::CK_RightBracket); |
| 1260 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1261 | Pattern->AddPlaceholderChunk("expressions"); |
| 1262 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1263 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1264 | |
| 1265 | // delete expression |
| 1266 | Pattern = new CodeCompletionString; |
| 1267 | Pattern->AddTypedTextChunk("delete"); |
| 1268 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1269 | Pattern->AddPlaceholderChunk("expression"); |
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 | // delete [] expression |
| 1273 | Pattern = new CodeCompletionString; |
| 1274 | Pattern->AddTypedTextChunk("delete"); |
| 1275 | Pattern->AddChunk(CodeCompletionString::CK_LeftBracket); |
| 1276 | Pattern->AddChunk(CodeCompletionString::CK_RightBracket); |
| 1277 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1278 | Pattern->AddPlaceholderChunk("expression"); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1279 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1280 | |
| 1281 | // throw expression |
| 1282 | Pattern = new CodeCompletionString; |
| 1283 | Pattern->AddTypedTextChunk("throw"); |
| 1284 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1285 | Pattern->AddPlaceholderChunk("expression"); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1286 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | if (SemaRef.getLangOptions().ObjC1) { |
| 1290 | // Add "super", if we're in an Objective-C class with a superclass. |
| 1291 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) |
| 1292 | if (Method->getClassInterface()->getSuperClass()) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1293 | Results.AddResult(Result("super")); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1294 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1295 | AddObjCExpressionResults(Results, true); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | // sizeof expression |
| 1299 | Pattern = new CodeCompletionString; |
| 1300 | Pattern->AddTypedTextChunk("sizeof"); |
| 1301 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 1302 | Pattern->AddPlaceholderChunk("expression-or-type"); |
| 1303 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1304 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1305 | break; |
| 1306 | } |
| 1307 | } |
| 1308 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1309 | AddTypeSpecifierResults(SemaRef.getLangOptions(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1310 | |
| 1311 | if (SemaRef.getLangOptions().CPlusPlus) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1312 | Results.AddResult(Result("operator")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1315 | /// \brief If the given declaration has an associated type, add it as a result |
| 1316 | /// type chunk. |
| 1317 | static void AddResultTypeChunk(ASTContext &Context, |
| 1318 | NamedDecl *ND, |
| 1319 | CodeCompletionString *Result) { |
| 1320 | if (!ND) |
| 1321 | return; |
| 1322 | |
| 1323 | // Determine the type of the declaration (if it has a type). |
| 1324 | QualType T; |
| 1325 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) |
| 1326 | T = Function->getResultType(); |
| 1327 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
| 1328 | T = Method->getResultType(); |
| 1329 | else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) |
| 1330 | T = FunTmpl->getTemplatedDecl()->getResultType(); |
| 1331 | else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
| 1332 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); |
| 1333 | else if (isa<UnresolvedUsingValueDecl>(ND)) { |
| 1334 | /* Do nothing: ignore unresolved using declarations*/ |
| 1335 | } else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND)) |
| 1336 | T = Value->getType(); |
| 1337 | else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
| 1338 | T = Property->getType(); |
| 1339 | |
| 1340 | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) |
| 1341 | return; |
| 1342 | |
Douglas Gregor | cf04b02 | 2010-04-05 21:25:31 +0000 | [diff] [blame] | 1343 | PrintingPolicy Policy(Context.PrintingPolicy); |
| 1344 | Policy.AnonymousTagLocations = false; |
| 1345 | |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1346 | std::string TypeStr; |
Douglas Gregor | cf04b02 | 2010-04-05 21:25:31 +0000 | [diff] [blame] | 1347 | T.getAsStringInternal(TypeStr, Policy); |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1348 | Result->AddResultTypeChunk(TypeStr); |
| 1349 | } |
| 1350 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1351 | /// \brief Add function parameter chunks to the given code completion string. |
| 1352 | static void AddFunctionParameterChunks(ASTContext &Context, |
| 1353 | FunctionDecl *Function, |
| 1354 | CodeCompletionString *Result) { |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1355 | typedef CodeCompletionString::Chunk Chunk; |
| 1356 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1357 | CodeCompletionString *CCStr = Result; |
| 1358 | |
| 1359 | for (unsigned P = 0, N = Function->getNumParams(); P != N; ++P) { |
| 1360 | ParmVarDecl *Param = Function->getParamDecl(P); |
| 1361 | |
| 1362 | if (Param->hasDefaultArg()) { |
| 1363 | // When we see an optional default argument, put that argument and |
| 1364 | // the remaining default arguments into a new, optional string. |
| 1365 | CodeCompletionString *Opt = new CodeCompletionString; |
| 1366 | CCStr->AddOptionalChunk(std::auto_ptr<CodeCompletionString>(Opt)); |
| 1367 | CCStr = Opt; |
| 1368 | } |
| 1369 | |
| 1370 | if (P != 0) |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1371 | CCStr->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1372 | |
| 1373 | // Format the placeholder string. |
| 1374 | std::string PlaceholderStr; |
| 1375 | if (Param->getIdentifier()) |
| 1376 | PlaceholderStr = Param->getIdentifier()->getName(); |
| 1377 | |
| 1378 | Param->getType().getAsStringInternal(PlaceholderStr, |
| 1379 | Context.PrintingPolicy); |
| 1380 | |
| 1381 | // Add the placeholder string. |
Benjamin Kramer | b33a97c | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1382 | CCStr->AddPlaceholderChunk(PlaceholderStr); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1383 | } |
Douglas Gregor | ba44903 | 2009-09-22 21:42:17 +0000 | [diff] [blame] | 1384 | |
| 1385 | if (const FunctionProtoType *Proto |
| 1386 | = Function->getType()->getAs<FunctionProtoType>()) |
| 1387 | if (Proto->isVariadic()) |
| 1388 | CCStr->AddPlaceholderChunk(", ..."); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | /// \brief Add template parameter chunks to the given code completion string. |
| 1392 | static void AddTemplateParameterChunks(ASTContext &Context, |
| 1393 | TemplateDecl *Template, |
| 1394 | CodeCompletionString *Result, |
| 1395 | unsigned MaxParameters = 0) { |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1396 | typedef CodeCompletionString::Chunk Chunk; |
| 1397 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1398 | CodeCompletionString *CCStr = Result; |
| 1399 | bool FirstParameter = true; |
| 1400 | |
| 1401 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 1402 | TemplateParameterList::iterator PEnd = Params->end(); |
| 1403 | if (MaxParameters) |
| 1404 | PEnd = Params->begin() + MaxParameters; |
| 1405 | for (TemplateParameterList::iterator P = Params->begin(); P != PEnd; ++P) { |
| 1406 | bool HasDefaultArg = false; |
| 1407 | std::string PlaceholderStr; |
| 1408 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 1409 | if (TTP->wasDeclaredWithTypename()) |
| 1410 | PlaceholderStr = "typename"; |
| 1411 | else |
| 1412 | PlaceholderStr = "class"; |
| 1413 | |
| 1414 | if (TTP->getIdentifier()) { |
| 1415 | PlaceholderStr += ' '; |
| 1416 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 1417 | } |
| 1418 | |
| 1419 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 1420 | } else if (NonTypeTemplateParmDecl *NTTP |
| 1421 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 1422 | if (NTTP->getIdentifier()) |
| 1423 | PlaceholderStr = NTTP->getIdentifier()->getName(); |
| 1424 | NTTP->getType().getAsStringInternal(PlaceholderStr, |
| 1425 | Context.PrintingPolicy); |
| 1426 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 1427 | } else { |
| 1428 | assert(isa<TemplateTemplateParmDecl>(*P)); |
| 1429 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 1430 | |
| 1431 | // Since putting the template argument list into the placeholder would |
| 1432 | // be very, very long, we just use an abbreviation. |
| 1433 | PlaceholderStr = "template<...> class"; |
| 1434 | if (TTP->getIdentifier()) { |
| 1435 | PlaceholderStr += ' '; |
| 1436 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 1437 | } |
| 1438 | |
| 1439 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 1440 | } |
| 1441 | |
| 1442 | if (HasDefaultArg) { |
| 1443 | // When we see an optional default argument, put that argument and |
| 1444 | // the remaining default arguments into a new, optional string. |
| 1445 | CodeCompletionString *Opt = new CodeCompletionString; |
| 1446 | CCStr->AddOptionalChunk(std::auto_ptr<CodeCompletionString>(Opt)); |
| 1447 | CCStr = Opt; |
| 1448 | } |
| 1449 | |
| 1450 | if (FirstParameter) |
| 1451 | FirstParameter = false; |
| 1452 | else |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1453 | CCStr->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1454 | |
| 1455 | // Add the placeholder string. |
Benjamin Kramer | b33a97c | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1456 | CCStr->AddPlaceholderChunk(PlaceholderStr); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1457 | } |
| 1458 | } |
| 1459 | |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1460 | /// \brief Add a qualifier to the given code-completion string, if the |
| 1461 | /// provided nested-name-specifier is non-NULL. |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 1462 | static void |
| 1463 | AddQualifierToCompletionString(CodeCompletionString *Result, |
| 1464 | NestedNameSpecifier *Qualifier, |
| 1465 | bool QualifierIsInformative, |
| 1466 | ASTContext &Context) { |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1467 | if (!Qualifier) |
| 1468 | return; |
| 1469 | |
| 1470 | std::string PrintedNNS; |
| 1471 | { |
| 1472 | llvm::raw_string_ostream OS(PrintedNNS); |
| 1473 | Qualifier->print(OS, Context.PrintingPolicy); |
| 1474 | } |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 1475 | if (QualifierIsInformative) |
Benjamin Kramer | b33a97c | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1476 | Result->AddInformativeChunk(PrintedNNS); |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 1477 | else |
Benjamin Kramer | b33a97c | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1478 | Result->AddTextChunk(PrintedNNS); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 1481 | static void AddFunctionTypeQualsToCompletionString(CodeCompletionString *Result, |
| 1482 | FunctionDecl *Function) { |
| 1483 | const FunctionProtoType *Proto |
| 1484 | = Function->getType()->getAs<FunctionProtoType>(); |
| 1485 | if (!Proto || !Proto->getTypeQuals()) |
| 1486 | return; |
| 1487 | |
| 1488 | std::string QualsStr; |
| 1489 | if (Proto->getTypeQuals() & Qualifiers::Const) |
| 1490 | QualsStr += " const"; |
| 1491 | if (Proto->getTypeQuals() & Qualifiers::Volatile) |
| 1492 | QualsStr += " volatile"; |
| 1493 | if (Proto->getTypeQuals() & Qualifiers::Restrict) |
| 1494 | QualsStr += " restrict"; |
| 1495 | Result->AddInformativeChunk(QualsStr); |
| 1496 | } |
| 1497 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1498 | /// \brief If possible, create a new code completion string for the given |
| 1499 | /// result. |
| 1500 | /// |
| 1501 | /// \returns Either a new, heap-allocated code completion string describing |
| 1502 | /// how to use this result, or NULL to indicate that the string or name of the |
| 1503 | /// result is all that is needed. |
| 1504 | CodeCompletionString * |
| 1505 | CodeCompleteConsumer::Result::CreateCodeCompletionString(Sema &S) { |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1506 | typedef CodeCompletionString::Chunk Chunk; |
| 1507 | |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 1508 | if (Kind == RK_Pattern) |
| 1509 | return Pattern->Clone(); |
| 1510 | |
| 1511 | CodeCompletionString *Result = new CodeCompletionString; |
| 1512 | |
| 1513 | if (Kind == RK_Keyword) { |
| 1514 | Result->AddTypedTextChunk(Keyword); |
| 1515 | return Result; |
| 1516 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1517 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1518 | if (Kind == RK_Macro) { |
| 1519 | MacroInfo *MI = S.PP.getMacroInfo(Macro); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 1520 | assert(MI && "Not a macro?"); |
| 1521 | |
| 1522 | Result->AddTypedTextChunk(Macro->getName()); |
| 1523 | |
| 1524 | if (!MI->isFunctionLike()) |
| 1525 | return Result; |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1526 | |
| 1527 | // Format a function-like macro with placeholders for the arguments. |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1528 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1529 | for (MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end(); |
| 1530 | A != AEnd; ++A) { |
| 1531 | if (A != MI->arg_begin()) |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1532 | Result->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1533 | |
| 1534 | if (!MI->isVariadic() || A != AEnd - 1) { |
| 1535 | // Non-variadic argument. |
Benjamin Kramer | b33a97c | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1536 | Result->AddPlaceholderChunk((*A)->getName()); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1537 | continue; |
| 1538 | } |
| 1539 | |
| 1540 | // Variadic argument; cope with the different between GNU and C99 |
| 1541 | // variadic macros, providing a single placeholder for the rest of the |
| 1542 | // arguments. |
| 1543 | if ((*A)->isStr("__VA_ARGS__")) |
| 1544 | Result->AddPlaceholderChunk("..."); |
| 1545 | else { |
| 1546 | std::string Arg = (*A)->getName(); |
| 1547 | Arg += "..."; |
Benjamin Kramer | b33a97c | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1548 | Result->AddPlaceholderChunk(Arg); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1549 | } |
| 1550 | } |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1551 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1552 | return Result; |
| 1553 | } |
| 1554 | |
| 1555 | assert(Kind == RK_Declaration && "Missed a macro kind?"); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1556 | NamedDecl *ND = Declaration; |
| 1557 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1558 | if (StartsNestedNameSpecifier) { |
Benjamin Kramer | b33a97c | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1559 | Result->AddTypedTextChunk(ND->getNameAsString()); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1560 | Result->AddTextChunk("::"); |
| 1561 | return Result; |
| 1562 | } |
| 1563 | |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1564 | AddResultTypeChunk(S.Context, ND, Result); |
| 1565 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1566 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 1567 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 1568 | S.Context); |
Benjamin Kramer | b33a97c | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1569 | Result->AddTypedTextChunk(Function->getNameAsString()); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1570 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1571 | AddFunctionParameterChunks(S.Context, Function, Result); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1572 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 1573 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1574 | return Result; |
| 1575 | } |
| 1576 | |
| 1577 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 1578 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 1579 | S.Context); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1580 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
Benjamin Kramer | b33a97c | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1581 | Result->AddTypedTextChunk(Function->getNameAsString()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1582 | |
| 1583 | // Figure out which template parameters are deduced (or have default |
| 1584 | // arguments). |
| 1585 | llvm::SmallVector<bool, 16> Deduced; |
| 1586 | S.MarkDeducedTemplateParameters(FunTmpl, Deduced); |
| 1587 | unsigned LastDeducibleArgument; |
| 1588 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
| 1589 | --LastDeducibleArgument) { |
| 1590 | if (!Deduced[LastDeducibleArgument - 1]) { |
| 1591 | // C++0x: Figure out if the template argument has a default. If so, |
| 1592 | // the user doesn't need to type this argument. |
| 1593 | // FIXME: We need to abstract template parameters better! |
| 1594 | bool HasDefaultArg = false; |
| 1595 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
| 1596 | LastDeducibleArgument - 1); |
| 1597 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 1598 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 1599 | else if (NonTypeTemplateParmDecl *NTTP |
| 1600 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 1601 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 1602 | else { |
| 1603 | assert(isa<TemplateTemplateParmDecl>(Param)); |
| 1604 | HasDefaultArg |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1605 | = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | if (!HasDefaultArg) |
| 1609 | break; |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | if (LastDeducibleArgument) { |
| 1614 | // Some of the function template arguments cannot be deduced from a |
| 1615 | // function call, so we introduce an explicit template argument list |
| 1616 | // containing all of the arguments up to the first deducible argument. |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1617 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1618 | AddTemplateParameterChunks(S.Context, FunTmpl, Result, |
| 1619 | LastDeducibleArgument); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1620 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | // Add the function parameters |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1624 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1625 | AddFunctionParameterChunks(S.Context, Function, Result); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1626 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 1627 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1628 | return Result; |
| 1629 | } |
| 1630 | |
| 1631 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 1632 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 1633 | S.Context); |
Benjamin Kramer | b33a97c | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1634 | Result->AddTypedTextChunk(Template->getNameAsString()); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1635 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1636 | AddTemplateParameterChunks(S.Context, Template, Result); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1637 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1638 | return Result; |
| 1639 | } |
| 1640 | |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 1641 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 1642 | Selector Sel = Method->getSelector(); |
| 1643 | if (Sel.isUnarySelector()) { |
| 1644 | Result->AddTypedTextChunk(Sel.getIdentifierInfoForSlot(0)->getName()); |
| 1645 | return Result; |
| 1646 | } |
| 1647 | |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1648 | std::string SelName = Sel.getIdentifierInfoForSlot(0)->getName().str(); |
| 1649 | SelName += ':'; |
| 1650 | if (StartParameter == 0) |
| 1651 | Result->AddTypedTextChunk(SelName); |
| 1652 | else { |
| 1653 | Result->AddInformativeChunk(SelName); |
| 1654 | |
| 1655 | // If there is only one parameter, and we're past it, add an empty |
| 1656 | // typed-text chunk since there is nothing to type. |
| 1657 | if (Method->param_size() == 1) |
| 1658 | Result->AddTypedTextChunk(""); |
| 1659 | } |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 1660 | unsigned Idx = 0; |
| 1661 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 1662 | PEnd = Method->param_end(); |
| 1663 | P != PEnd; (void)++P, ++Idx) { |
| 1664 | if (Idx > 0) { |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1665 | std::string Keyword; |
| 1666 | if (Idx > StartParameter) |
Douglas Gregor | 6a80393 | 2010-01-12 06:38:28 +0000 | [diff] [blame] | 1667 | Result->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 1668 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) |
| 1669 | Keyword += II->getName().str(); |
| 1670 | Keyword += ":"; |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 1671 | if (Idx < StartParameter || AllParametersAreInformative) { |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1672 | Result->AddInformativeChunk(Keyword); |
| 1673 | } else if (Idx == StartParameter) |
| 1674 | Result->AddTypedTextChunk(Keyword); |
| 1675 | else |
| 1676 | Result->AddTextChunk(Keyword); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 1677 | } |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1678 | |
| 1679 | // If we're before the starting parameter, skip the placeholder. |
| 1680 | if (Idx < StartParameter) |
| 1681 | continue; |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 1682 | |
| 1683 | std::string Arg; |
| 1684 | (*P)->getType().getAsStringInternal(Arg, S.Context.PrintingPolicy); |
| 1685 | Arg = "(" + Arg + ")"; |
| 1686 | if (IdentifierInfo *II = (*P)->getIdentifier()) |
| 1687 | Arg += II->getName().str(); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 1688 | if (AllParametersAreInformative) |
| 1689 | Result->AddInformativeChunk(Arg); |
| 1690 | else |
| 1691 | Result->AddPlaceholderChunk(Arg); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 1694 | if (Method->isVariadic()) { |
| 1695 | if (AllParametersAreInformative) |
| 1696 | Result->AddInformativeChunk(", ..."); |
| 1697 | else |
| 1698 | Result->AddPlaceholderChunk(", ..."); |
| 1699 | } |
| 1700 | |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 1701 | return Result; |
| 1702 | } |
| 1703 | |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 1704 | if (Qualifier) |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 1705 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| 1706 | S.Context); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 1707 | |
| 1708 | Result->AddTypedTextChunk(ND->getNameAsString()); |
| 1709 | return Result; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1712 | CodeCompletionString * |
| 1713 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( |
| 1714 | unsigned CurrentArg, |
| 1715 | Sema &S) const { |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1716 | typedef CodeCompletionString::Chunk Chunk; |
| 1717 | |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1718 | CodeCompletionString *Result = new CodeCompletionString; |
| 1719 | FunctionDecl *FDecl = getFunction(); |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1720 | AddResultTypeChunk(S.Context, FDecl, Result); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1721 | const FunctionProtoType *Proto |
| 1722 | = dyn_cast<FunctionProtoType>(getFunctionType()); |
| 1723 | if (!FDecl && !Proto) { |
| 1724 | // Function without a prototype. Just give the return type and a |
| 1725 | // highlighted ellipsis. |
| 1726 | const FunctionType *FT = getFunctionType(); |
| 1727 | Result->AddTextChunk( |
Benjamin Kramer | b33a97c | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1728 | FT->getResultType().getAsString(S.Context.PrintingPolicy)); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1729 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
| 1730 | Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "...")); |
| 1731 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1732 | return Result; |
| 1733 | } |
| 1734 | |
| 1735 | if (FDecl) |
Benjamin Kramer | b33a97c | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1736 | Result->AddTextChunk(FDecl->getNameAsString()); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1737 | else |
| 1738 | Result->AddTextChunk( |
Benjamin Kramer | b33a97c | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1739 | Proto->getResultType().getAsString(S.Context.PrintingPolicy)); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1740 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1741 | Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen)); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1742 | unsigned NumParams = FDecl? FDecl->getNumParams() : Proto->getNumArgs(); |
| 1743 | for (unsigned I = 0; I != NumParams; ++I) { |
| 1744 | if (I) |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1745 | Result->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1746 | |
| 1747 | std::string ArgString; |
| 1748 | QualType ArgType; |
| 1749 | |
| 1750 | if (FDecl) { |
| 1751 | ArgString = FDecl->getParamDecl(I)->getNameAsString(); |
| 1752 | ArgType = FDecl->getParamDecl(I)->getOriginalType(); |
| 1753 | } else { |
| 1754 | ArgType = Proto->getArgType(I); |
| 1755 | } |
| 1756 | |
| 1757 | ArgType.getAsStringInternal(ArgString, S.Context.PrintingPolicy); |
| 1758 | |
| 1759 | if (I == CurrentArg) |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1760 | Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, |
Benjamin Kramer | b33a97c | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1761 | ArgString)); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1762 | else |
Benjamin Kramer | b33a97c | 2009-11-29 20:18:50 +0000 | [diff] [blame] | 1763 | Result->AddTextChunk(ArgString); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | if (Proto && Proto->isVariadic()) { |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1767 | Result->AddChunk(Chunk(CodeCompletionString::CK_Comma)); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1768 | if (CurrentArg < NumParams) |
| 1769 | Result->AddTextChunk("..."); |
| 1770 | else |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1771 | Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "...")); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1772 | } |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1773 | Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen)); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 1774 | |
| 1775 | return Result; |
| 1776 | } |
| 1777 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1778 | namespace { |
| 1779 | struct SortCodeCompleteResult { |
| 1780 | typedef CodeCompleteConsumer::Result Result; |
| 1781 | |
Douglas Gregor | e6688e6 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 1782 | bool isEarlierDeclarationName(DeclarationName X, DeclarationName Y) const { |
Douglas Gregor | 249d682 | 2009-12-05 09:08:56 +0000 | [diff] [blame] | 1783 | Selector XSel = X.getObjCSelector(); |
| 1784 | Selector YSel = Y.getObjCSelector(); |
| 1785 | if (!XSel.isNull() && !YSel.isNull()) { |
| 1786 | // We are comparing two selectors. |
| 1787 | unsigned N = std::min(XSel.getNumArgs(), YSel.getNumArgs()); |
| 1788 | if (N == 0) |
| 1789 | ++N; |
| 1790 | for (unsigned I = 0; I != N; ++I) { |
| 1791 | IdentifierInfo *XId = XSel.getIdentifierInfoForSlot(I); |
| 1792 | IdentifierInfo *YId = YSel.getIdentifierInfoForSlot(I); |
| 1793 | if (!XId || !YId) |
| 1794 | return XId && !YId; |
| 1795 | |
| 1796 | switch (XId->getName().compare_lower(YId->getName())) { |
| 1797 | case -1: return true; |
| 1798 | case 1: return false; |
| 1799 | default: break; |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | return XSel.getNumArgs() < YSel.getNumArgs(); |
| 1804 | } |
| 1805 | |
| 1806 | // For non-selectors, order by kind. |
| 1807 | if (X.getNameKind() != Y.getNameKind()) |
Douglas Gregor | e6688e6 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 1808 | return X.getNameKind() < Y.getNameKind(); |
| 1809 | |
Douglas Gregor | 249d682 | 2009-12-05 09:08:56 +0000 | [diff] [blame] | 1810 | // Order identifiers by comparison of their lowercased names. |
| 1811 | if (IdentifierInfo *XId = X.getAsIdentifierInfo()) |
| 1812 | return XId->getName().compare_lower( |
| 1813 | Y.getAsIdentifierInfo()->getName()) < 0; |
| 1814 | |
| 1815 | // Order overloaded operators by the order in which they appear |
| 1816 | // in our list of operators. |
| 1817 | if (OverloadedOperatorKind XOp = X.getCXXOverloadedOperator()) |
| 1818 | return XOp < Y.getCXXOverloadedOperator(); |
| 1819 | |
| 1820 | // Order C++0x user-defined literal operators lexically by their |
| 1821 | // lowercased suffixes. |
| 1822 | if (IdentifierInfo *XLit = X.getCXXLiteralIdentifier()) |
| 1823 | return XLit->getName().compare_lower( |
| 1824 | Y.getCXXLiteralIdentifier()->getName()) < 0; |
| 1825 | |
| 1826 | // The only stable ordering we have is to turn the name into a |
| 1827 | // string and then compare the lower-case strings. This is |
| 1828 | // inefficient, but thankfully does not happen too often. |
Benjamin Kramer | 4053e5d | 2009-12-05 10:22:15 +0000 | [diff] [blame] | 1829 | return llvm::StringRef(X.getAsString()).compare_lower( |
| 1830 | Y.getAsString()) < 0; |
Douglas Gregor | e6688e6 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Douglas Gregor | 52ce62f | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 1833 | /// \brief Retrieve the name that should be used to order a result. |
| 1834 | /// |
| 1835 | /// If the name needs to be constructed as a string, that string will be |
| 1836 | /// saved into Saved and the returned StringRef will refer to it. |
| 1837 | static llvm::StringRef getOrderedName(const Result &R, |
| 1838 | std::string &Saved) { |
| 1839 | switch (R.Kind) { |
| 1840 | case Result::RK_Keyword: |
| 1841 | return R.Keyword; |
| 1842 | |
| 1843 | case Result::RK_Pattern: |
| 1844 | return R.Pattern->getTypedText(); |
| 1845 | |
| 1846 | case Result::RK_Macro: |
| 1847 | return R.Macro->getName(); |
| 1848 | |
| 1849 | case Result::RK_Declaration: |
| 1850 | // Handle declarations below. |
| 1851 | break; |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 1852 | } |
Douglas Gregor | 52ce62f | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 1853 | |
| 1854 | DeclarationName Name = R.Declaration->getDeclName(); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 1855 | |
Douglas Gregor | 52ce62f | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 1856 | // If the name is a simple identifier (by far the common case), or a |
| 1857 | // zero-argument selector, just return a reference to that identifier. |
| 1858 | if (IdentifierInfo *Id = Name.getAsIdentifierInfo()) |
| 1859 | return Id->getName(); |
| 1860 | if (Name.isObjCZeroArgSelector()) |
| 1861 | if (IdentifierInfo *Id |
| 1862 | = Name.getObjCSelector().getIdentifierInfoForSlot(0)) |
| 1863 | return Id->getName(); |
| 1864 | |
| 1865 | Saved = Name.getAsString(); |
| 1866 | return Saved; |
| 1867 | } |
| 1868 | |
| 1869 | bool operator()(const Result &X, const Result &Y) const { |
| 1870 | std::string XSaved, YSaved; |
| 1871 | llvm::StringRef XStr = getOrderedName(X, XSaved); |
| 1872 | llvm::StringRef YStr = getOrderedName(Y, YSaved); |
| 1873 | int cmp = XStr.compare_lower(YStr); |
| 1874 | if (cmp) |
| 1875 | return cmp < 0; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Non-hidden names precede hidden names. |
| 1878 | if (X.Hidden != Y.Hidden) |
| 1879 | return !X.Hidden; |
| 1880 | |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1881 | // Non-nested-name-specifiers precede nested-name-specifiers. |
| 1882 | if (X.StartsNestedNameSpecifier != Y.StartsNestedNameSpecifier) |
| 1883 | return !X.StartsNestedNameSpecifier; |
| 1884 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1885 | return false; |
| 1886 | } |
| 1887 | }; |
| 1888 | } |
| 1889 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1890 | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results) { |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1891 | Results.EnterNewScope(); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1892 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| 1893 | MEnd = PP.macro_end(); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1894 | M != MEnd; ++M) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1895 | Results.AddResult(M->first); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 1896 | Results.ExitScope(); |
| 1897 | } |
| 1898 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1899 | static void HandleCodeCompleteResults(Sema *S, |
| 1900 | CodeCompleteConsumer *CodeCompleter, |
| 1901 | CodeCompleteConsumer::Result *Results, |
| 1902 | unsigned NumResults) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1903 | std::stable_sort(Results, Results + NumResults, SortCodeCompleteResult()); |
| 1904 | |
| 1905 | if (CodeCompleter) |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1906 | CodeCompleter->ProcessCodeCompleteResults(*S, Results, NumResults); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 1907 | |
| 1908 | for (unsigned I = 0; I != NumResults; ++I) |
| 1909 | Results[I].Destroy(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1912 | void Sema::CodeCompleteOrdinaryName(Scope *S, |
| 1913 | CodeCompletionContext CompletionContext) { |
Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 1914 | typedef CodeCompleteConsumer::Result Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1915 | ResultBuilder Results(*this); |
| 1916 | |
| 1917 | // Determine how to filter results, e.g., so that the names of |
| 1918 | // values (functions, enumerators, function templates, etc.) are |
| 1919 | // only allowed where we can have an expression. |
| 1920 | switch (CompletionContext) { |
| 1921 | case CCC_Namespace: |
| 1922 | case CCC_Class: |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1923 | case CCC_ObjCInterface: |
| 1924 | case CCC_ObjCImplementation: |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1925 | case CCC_ObjCInstanceVariableList: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1926 | case CCC_Template: |
| 1927 | case CCC_MemberTemplate: |
| 1928 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 1929 | break; |
| 1930 | |
| 1931 | case CCC_Expression: |
| 1932 | case CCC_Statement: |
| 1933 | case CCC_ForInit: |
| 1934 | case CCC_Condition: |
| 1935 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 1936 | break; |
| 1937 | } |
| 1938 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1939 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 1940 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer); |
Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 1941 | |
| 1942 | Results.EnterNewScope(); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1943 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); |
Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 1944 | Results.ExitScope(); |
| 1945 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1946 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1947 | AddMacroResults(PP, Results); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 1948 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1951 | static void AddObjCProperties(ObjCContainerDecl *Container, |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1952 | bool AllowCategories, |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1953 | DeclContext *CurContext, |
| 1954 | ResultBuilder &Results) { |
| 1955 | typedef CodeCompleteConsumer::Result Result; |
| 1956 | |
| 1957 | // Add properties in this container. |
| 1958 | for (ObjCContainerDecl::prop_iterator P = Container->prop_begin(), |
| 1959 | PEnd = Container->prop_end(); |
| 1960 | P != PEnd; |
| 1961 | ++P) |
| 1962 | Results.MaybeAddResult(Result(*P, 0), CurContext); |
| 1963 | |
| 1964 | // Add properties in referenced protocols. |
| 1965 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 1966 | for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(), |
| 1967 | PEnd = Protocol->protocol_end(); |
| 1968 | P != PEnd; ++P) |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1969 | AddObjCProperties(*P, AllowCategories, CurContext, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1970 | } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){ |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1971 | if (AllowCategories) { |
| 1972 | // Look through categories. |
| 1973 | for (ObjCCategoryDecl *Category = IFace->getCategoryList(); |
| 1974 | Category; Category = Category->getNextClassCategory()) |
| 1975 | AddObjCProperties(Category, AllowCategories, CurContext, Results); |
| 1976 | } |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1977 | |
| 1978 | // Look through protocols. |
| 1979 | for (ObjCInterfaceDecl::protocol_iterator I = IFace->protocol_begin(), |
| 1980 | E = IFace->protocol_end(); |
| 1981 | I != E; ++I) |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1982 | AddObjCProperties(*I, AllowCategories, CurContext, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1983 | |
| 1984 | // Look in the superclass. |
| 1985 | if (IFace->getSuperClass()) |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1986 | AddObjCProperties(IFace->getSuperClass(), AllowCategories, CurContext, |
| 1987 | Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1988 | } else if (const ObjCCategoryDecl *Category |
| 1989 | = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 1990 | // Look through protocols. |
| 1991 | for (ObjCInterfaceDecl::protocol_iterator P = Category->protocol_begin(), |
| 1992 | PEnd = Category->protocol_end(); |
| 1993 | P != PEnd; ++P) |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1994 | AddObjCProperties(*P, AllowCategories, CurContext, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 1995 | } |
| 1996 | } |
| 1997 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1998 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, ExprTy *BaseE, |
| 1999 | SourceLocation OpLoc, |
| 2000 | bool IsArrow) { |
| 2001 | if (!BaseE || !CodeCompleter) |
| 2002 | return; |
| 2003 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2004 | typedef CodeCompleteConsumer::Result Result; |
| 2005 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 2006 | Expr *Base = static_cast<Expr *>(BaseE); |
| 2007 | QualType BaseType = Base->getType(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2008 | |
| 2009 | if (IsArrow) { |
| 2010 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 2011 | BaseType = Ptr->getPointeeType(); |
| 2012 | else if (BaseType->isObjCObjectPointerType()) |
| 2013 | /*Do nothing*/ ; |
| 2014 | else |
| 2015 | return; |
| 2016 | } |
| 2017 | |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 2018 | ResultBuilder Results(*this, &ResultBuilder::IsMember); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2019 | Results.EnterNewScope(); |
| 2020 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
| 2021 | // Access to a C/C++ class, struct, or union. |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 2022 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 2023 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 2024 | LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2025 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2026 | if (getLangOptions().CPlusPlus) { |
| 2027 | if (!Results.empty()) { |
| 2028 | // The "template" keyword can follow "->" or "." in the grammar. |
| 2029 | // However, we only want to suggest the template keyword if something |
| 2030 | // is dependent. |
| 2031 | bool IsDependent = BaseType->isDependentType(); |
| 2032 | if (!IsDependent) { |
| 2033 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) |
| 2034 | if (DeclContext *Ctx = (DeclContext *)DepScope->getEntity()) { |
| 2035 | IsDependent = Ctx->isDependentContext(); |
| 2036 | break; |
| 2037 | } |
| 2038 | } |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2039 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2040 | if (IsDependent) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2041 | Results.AddResult(Result("template")); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2042 | } |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2043 | } |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2044 | } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) { |
| 2045 | // Objective-C property reference. |
| 2046 | |
| 2047 | // Add property results based on our interface. |
| 2048 | const ObjCObjectPointerType *ObjCPtr |
| 2049 | = BaseType->getAsObjCInterfacePointerType(); |
| 2050 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!"); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2051 | AddObjCProperties(ObjCPtr->getInterfaceDecl(), true, CurContext, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2052 | |
| 2053 | // Add properties from the protocols in a qualified interface. |
| 2054 | for (ObjCObjectPointerType::qual_iterator I = ObjCPtr->qual_begin(), |
| 2055 | E = ObjCPtr->qual_end(); |
| 2056 | I != E; ++I) |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 2057 | AddObjCProperties(*I, true, CurContext, Results); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2058 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
| 2059 | (!IsArrow && BaseType->isObjCInterfaceType())) { |
| 2060 | // Objective-C instance variable access. |
| 2061 | ObjCInterfaceDecl *Class = 0; |
| 2062 | if (const ObjCObjectPointerType *ObjCPtr |
| 2063 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 2064 | Class = ObjCPtr->getInterfaceDecl(); |
| 2065 | else |
| 2066 | Class = BaseType->getAs<ObjCInterfaceType>()->getDecl(); |
| 2067 | |
| 2068 | // Add all ivars from this class and its superclasses. |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 2069 | if (Class) { |
| 2070 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 2071 | Results.setFilter(&ResultBuilder::IsObjCIvar); |
| 2072 | LookupVisibleDecls(Class, LookupMemberName, Consumer); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2073 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2074 | } |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 2075 | |
| 2076 | // FIXME: How do we cope with isa? |
| 2077 | |
| 2078 | Results.ExitScope(); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2079 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2080 | // Hand off the results found for code completion. |
| 2081 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2084 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { |
| 2085 | if (!CodeCompleter) |
| 2086 | return; |
| 2087 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2088 | typedef CodeCompleteConsumer::Result Result; |
| 2089 | ResultBuilder::LookupFilter Filter = 0; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2090 | switch ((DeclSpec::TST)TagSpec) { |
| 2091 | case DeclSpec::TST_enum: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2092 | Filter = &ResultBuilder::IsEnum; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2093 | break; |
| 2094 | |
| 2095 | case DeclSpec::TST_union: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2096 | Filter = &ResultBuilder::IsUnion; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2097 | break; |
| 2098 | |
| 2099 | case DeclSpec::TST_struct: |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2100 | case DeclSpec::TST_class: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2101 | Filter = &ResultBuilder::IsClassOrStruct; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2102 | break; |
| 2103 | |
| 2104 | default: |
| 2105 | assert(false && "Unknown type specifier kind in CodeCompleteTag"); |
| 2106 | return; |
| 2107 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2108 | |
| 2109 | ResultBuilder Results(*this, Filter); |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 2110 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 2111 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 2112 | LookupVisibleDecls(S, LookupTagName, Consumer); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2113 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2114 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2117 | void Sema::CodeCompleteCase(Scope *S) { |
| 2118 | if (getSwitchStack().empty() || !CodeCompleter) |
| 2119 | return; |
| 2120 | |
| 2121 | SwitchStmt *Switch = getSwitchStack().back(); |
| 2122 | if (!Switch->getCond()->getType()->isEnumeralType()) |
| 2123 | return; |
| 2124 | |
| 2125 | // Code-complete the cases of a switch statement over an enumeration type |
| 2126 | // by providing the list of |
| 2127 | EnumDecl *Enum = Switch->getCond()->getType()->getAs<EnumType>()->getDecl(); |
| 2128 | |
| 2129 | // Determine which enumerators we have already seen in the switch statement. |
| 2130 | // FIXME: Ideally, we would also be able to look *past* the code-completion |
| 2131 | // token, in case we are code-completing in the middle of the switch and not |
| 2132 | // at the end. However, we aren't able to do so at the moment. |
| 2133 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2134 | NestedNameSpecifier *Qualifier = 0; |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2135 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; |
| 2136 | SC = SC->getNextSwitchCase()) { |
| 2137 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); |
| 2138 | if (!Case) |
| 2139 | continue; |
| 2140 | |
| 2141 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); |
| 2142 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal)) |
| 2143 | if (EnumConstantDecl *Enumerator |
| 2144 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 2145 | // We look into the AST of the case statement to determine which |
| 2146 | // enumerator was named. Alternatively, we could compute the value of |
| 2147 | // the integral constant expression, then compare it against the |
| 2148 | // values of each enumerator. However, value-based approach would not |
| 2149 | // work as well with C++ templates where enumerators declared within a |
| 2150 | // template are type- and value-dependent. |
| 2151 | EnumeratorsSeen.insert(Enumerator); |
| 2152 | |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2153 | // If this is a qualified-id, keep track of the nested-name-specifier |
| 2154 | // 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] | 2155 | // |
| 2156 | // switch (TagD.getKind()) { |
| 2157 | // case TagDecl::TK_enum: |
| 2158 | // break; |
| 2159 | // case XXX |
| 2160 | // |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2161 | // At the XXX, our completions are TagDecl::TK_union, |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2162 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, |
| 2163 | // TK_struct, and TK_class. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2164 | Qualifier = DRE->getQualifier(); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2165 | } |
| 2166 | } |
| 2167 | |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2168 | if (getLangOptions().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { |
| 2169 | // If there are no prior enumerators in C++, check whether we have to |
| 2170 | // qualify the names of the enumerators that we suggest, because they |
| 2171 | // may not be visible in this scope. |
| 2172 | Qualifier = getRequiredQualification(Context, CurContext, |
| 2173 | Enum->getDeclContext()); |
| 2174 | |
| 2175 | // FIXME: Scoped enums need to start with "EnumDecl" as the context! |
| 2176 | } |
| 2177 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2178 | // Add any enumerators that have not yet been mentioned. |
| 2179 | ResultBuilder Results(*this); |
| 2180 | Results.EnterNewScope(); |
| 2181 | for (EnumDecl::enumerator_iterator E = Enum->enumerator_begin(), |
| 2182 | EEnd = Enum->enumerator_end(); |
| 2183 | E != EEnd; ++E) { |
| 2184 | if (EnumeratorsSeen.count(*E)) |
| 2185 | continue; |
| 2186 | |
Douglas Gregor | fc59ce1 | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 2187 | Results.AddResult(CodeCompleteConsumer::Result(*E, Qualifier), |
| 2188 | CurContext, 0, false); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2189 | } |
| 2190 | Results.ExitScope(); |
Douglas Gregor | 28556092 | 2010-04-06 20:02:15 +0000 | [diff] [blame^] | 2191 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2192 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2193 | AddMacroResults(PP, Results); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2194 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 2195 | } |
| 2196 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2197 | namespace { |
| 2198 | struct IsBetterOverloadCandidate { |
| 2199 | Sema &S; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2200 | SourceLocation Loc; |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2201 | |
| 2202 | public: |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2203 | explicit IsBetterOverloadCandidate(Sema &S, SourceLocation Loc) |
| 2204 | : S(S), Loc(Loc) { } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2205 | |
| 2206 | bool |
| 2207 | operator()(const OverloadCandidate &X, const OverloadCandidate &Y) const { |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2208 | return S.isBetterOverloadCandidate(X, Y, Loc); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2209 | } |
| 2210 | }; |
| 2211 | } |
| 2212 | |
| 2213 | void Sema::CodeCompleteCall(Scope *S, ExprTy *FnIn, |
| 2214 | ExprTy **ArgsIn, unsigned NumArgs) { |
| 2215 | if (!CodeCompleter) |
| 2216 | return; |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 2217 | |
| 2218 | // When we're code-completing for a call, we fall back to ordinary |
| 2219 | // name code-completion whenever we can't produce specific |
| 2220 | // results. We may want to revisit this strategy in the future, |
| 2221 | // e.g., by merging the two kinds of results. |
| 2222 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2223 | Expr *Fn = (Expr *)FnIn; |
| 2224 | Expr **Args = (Expr **)ArgsIn; |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 2225 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2226 | // Ignore type-dependent call expressions entirely. |
| 2227 | if (Fn->isTypeDependent() || |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 2228 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2229 | CodeCompleteOrdinaryName(S, CCC_Expression); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2230 | return; |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 2231 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2232 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2233 | // Build an overload candidate set based on the functions we find. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2234 | SourceLocation Loc = Fn->getExprLoc(); |
| 2235 | OverloadCandidateSet CandidateSet(Loc); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2236 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2237 | // FIXME: What if we're calling something that isn't a function declaration? |
| 2238 | // FIXME: What if we're calling a pseudo-destructor? |
| 2239 | // FIXME: What if we're calling a member function? |
| 2240 | |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 2241 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; |
| 2242 | llvm::SmallVector<ResultCandidate, 8> Results; |
| 2243 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2244 | Expr *NakedFn = Fn->IgnoreParenCasts(); |
| 2245 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) |
| 2246 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet, |
| 2247 | /*PartialOverloading=*/ true); |
| 2248 | else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(NakedFn)) { |
| 2249 | FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 2250 | if (FDecl) { |
| 2251 | if (!FDecl->getType()->getAs<FunctionProtoType>()) |
| 2252 | Results.push_back(ResultCandidate(FDecl)); |
| 2253 | else |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2254 | // FIXME: access? |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2255 | AddOverloadCandidate(FDecl, DeclAccessPair::make(FDecl, AS_none), |
| 2256 | Args, NumArgs, CandidateSet, |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 2257 | false, false, /*PartialOverloading*/ true); |
| 2258 | } |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2259 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2260 | |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 2261 | if (!CandidateSet.empty()) { |
| 2262 | // Sort the overload candidate set by placing the best overloads first. |
| 2263 | std::stable_sort(CandidateSet.begin(), CandidateSet.end(), |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2264 | IsBetterOverloadCandidate(*this, Loc)); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2265 | |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 2266 | // Add the remaining viable overload candidates as code-completion reslults. |
| 2267 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 2268 | CandEnd = CandidateSet.end(); |
| 2269 | Cand != CandEnd; ++Cand) { |
| 2270 | if (Cand->Viable) |
| 2271 | Results.push_back(ResultCandidate(Cand->Function)); |
| 2272 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2273 | } |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 2274 | |
| 2275 | if (Results.empty()) |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2276 | CodeCompleteOrdinaryName(S, CCC_Expression); |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 2277 | else |
| 2278 | CodeCompleter->ProcessOverloadCandidates(*this, NumArgs, Results.data(), |
| 2279 | Results.size()); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 2282 | void Sema::CodeCompleteQualifiedId(Scope *S, const CXXScopeSpec &SS, |
| 2283 | bool EnteringContext) { |
| 2284 | if (!SS.getScopeRep() || !CodeCompleter) |
| 2285 | return; |
| 2286 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2287 | DeclContext *Ctx = computeDeclContext(SS, EnteringContext); |
| 2288 | if (!Ctx) |
| 2289 | return; |
Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 2290 | |
| 2291 | // Try to instantiate any non-dependent declaration contexts before |
| 2292 | // we look in them. |
| 2293 | if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS)) |
| 2294 | return; |
| 2295 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2296 | ResultBuilder Results(*this); |
Douglas Gregor | 200c99d | 2010-01-14 03:35:48 +0000 | [diff] [blame] | 2297 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 2298 | LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2299 | |
| 2300 | // The "template" keyword can follow "::" in the grammar, but only |
| 2301 | // put it into the grammar if the nested-name-specifier is dependent. |
| 2302 | NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 2303 | if (!Results.empty() && NNS->isDependent()) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2304 | Results.AddResult("template"); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2305 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2306 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 2307 | } |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 2308 | |
| 2309 | void Sema::CodeCompleteUsing(Scope *S) { |
| 2310 | if (!CodeCompleter) |
| 2311 | return; |
| 2312 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2313 | ResultBuilder Results(*this, &ResultBuilder::IsNestedNameSpecifier); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 2314 | Results.EnterNewScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2315 | |
| 2316 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 2317 | if (!S->isClassScope()) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2318 | Results.AddResult(CodeCompleteConsumer::Result("namespace")); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2319 | |
| 2320 | // After "using", we can see anything that would start a |
| 2321 | // nested-name-specifier. |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 2322 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 2323 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 2324 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2325 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2326 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | void Sema::CodeCompleteUsingDirective(Scope *S) { |
| 2330 | if (!CodeCompleter) |
| 2331 | return; |
| 2332 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2333 | // After "using namespace", we expect to see a namespace name or namespace |
| 2334 | // alias. |
| 2335 | ResultBuilder Results(*this, &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 2336 | Results.EnterNewScope(); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 2337 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 2338 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 2339 | Results.ExitScope(); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2340 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { |
| 2344 | if (!CodeCompleter) |
| 2345 | return; |
| 2346 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2347 | ResultBuilder Results(*this, &ResultBuilder::IsNamespace); |
| 2348 | DeclContext *Ctx = (DeclContext *)S->getEntity(); |
| 2349 | if (!S->getParent()) |
| 2350 | Ctx = Context.getTranslationUnitDecl(); |
| 2351 | |
| 2352 | if (Ctx && Ctx->isFileContext()) { |
| 2353 | // We only want to see those namespaces that have already been defined |
| 2354 | // within this scope, because its likely that the user is creating an |
| 2355 | // extended namespace declaration. Keep track of the most recent |
| 2356 | // definition of each namespace. |
| 2357 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
| 2358 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
| 2359 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); |
| 2360 | NS != NSEnd; ++NS) |
| 2361 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
| 2362 | |
| 2363 | // Add the most recent definition (or extended definition) of each |
| 2364 | // namespace to the list of results. |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 2365 | Results.EnterNewScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2366 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
| 2367 | NS = OrigToLatest.begin(), NSEnd = OrigToLatest.end(); |
| 2368 | NS != NSEnd; ++NS) |
Douglas Gregor | fc59ce1 | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 2369 | Results.AddResult(CodeCompleteConsumer::Result(NS->second, 0), |
| 2370 | CurContext, 0, false); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 2371 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2374 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { |
| 2378 | if (!CodeCompleter) |
| 2379 | return; |
| 2380 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2381 | // After "namespace", we expect to see a namespace or alias. |
| 2382 | ResultBuilder Results(*this, &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 2383 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 2384 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2385 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 2388 | void Sema::CodeCompleteOperatorName(Scope *S) { |
| 2389 | if (!CodeCompleter) |
| 2390 | return; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2391 | |
| 2392 | typedef CodeCompleteConsumer::Result Result; |
| 2393 | ResultBuilder Results(*this, &ResultBuilder::IsType); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 2394 | Results.EnterNewScope(); |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 2395 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2396 | // Add the names of overloadable operators. |
| 2397 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 2398 | if (std::strcmp(Spelling, "?")) \ |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2399 | Results.AddResult(Result(Spelling)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2400 | #include "clang/Basic/OperatorKinds.def" |
| 2401 | |
| 2402 | // Add any type names visible from the current scope |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 2403 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 2404 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 2405 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2406 | |
| 2407 | // Add any type specifiers |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2408 | AddTypeSpecifierResults(getLangOptions(), Results); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 2409 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2410 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2411 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 2412 | } |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 2413 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2414 | // Macro that expands to @Keyword or Keyword, depending on whether NeedAt is |
| 2415 | // true or false. |
| 2416 | #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) NeedAt? "@" #Keyword : #Keyword |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2417 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2418 | ResultBuilder &Results, |
| 2419 | bool NeedAt) { |
| 2420 | typedef CodeCompleteConsumer::Result Result; |
| 2421 | // Since we have an implementation, we can end it. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2422 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2423 | |
| 2424 | CodeCompletionString *Pattern = 0; |
| 2425 | if (LangOpts.ObjC2) { |
| 2426 | // @dynamic |
| 2427 | Pattern = new CodeCompletionString; |
| 2428 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,dynamic)); |
| 2429 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2430 | Pattern->AddPlaceholderChunk("property"); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2431 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2432 | |
| 2433 | // @synthesize |
| 2434 | Pattern = new CodeCompletionString; |
| 2435 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synthesize)); |
| 2436 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2437 | Pattern->AddPlaceholderChunk("property"); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2438 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2439 | } |
| 2440 | } |
| 2441 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2442 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2443 | ResultBuilder &Results, |
| 2444 | bool NeedAt) { |
| 2445 | typedef CodeCompleteConsumer::Result Result; |
| 2446 | |
| 2447 | // Since we have an interface or protocol, we can end it. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2448 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2449 | |
| 2450 | if (LangOpts.ObjC2) { |
| 2451 | // @property |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2452 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,property))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2453 | |
| 2454 | // @required |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2455 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,required))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2456 | |
| 2457 | // @optional |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2458 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,optional))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2459 | } |
| 2460 | } |
| 2461 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2462 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) { |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2463 | typedef CodeCompleteConsumer::Result Result; |
| 2464 | CodeCompletionString *Pattern = 0; |
| 2465 | |
| 2466 | // @class name ; |
| 2467 | Pattern = new CodeCompletionString; |
| 2468 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,class)); |
| 2469 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2470 | Pattern->AddPlaceholderChunk("identifier"); |
| 2471 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2472 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2473 | |
| 2474 | // @interface name |
| 2475 | // FIXME: Could introduce the whole pattern, including superclasses and |
| 2476 | // such. |
| 2477 | Pattern = new CodeCompletionString; |
| 2478 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,interface)); |
| 2479 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2480 | Pattern->AddPlaceholderChunk("class"); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2481 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2482 | |
| 2483 | // @protocol name |
| 2484 | Pattern = new CodeCompletionString; |
| 2485 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol)); |
| 2486 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2487 | Pattern->AddPlaceholderChunk("protocol"); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2488 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2489 | |
| 2490 | // @implementation name |
| 2491 | Pattern = new CodeCompletionString; |
| 2492 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,implementation)); |
| 2493 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2494 | Pattern->AddPlaceholderChunk("class"); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2495 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2496 | |
| 2497 | // @compatibility_alias name |
| 2498 | Pattern = new CodeCompletionString; |
| 2499 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,compatibility_alias)); |
| 2500 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2501 | Pattern->AddPlaceholderChunk("alias"); |
| 2502 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2503 | Pattern->AddPlaceholderChunk("class"); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2504 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 2507 | void Sema::CodeCompleteObjCAtDirective(Scope *S, DeclPtrTy ObjCImpDecl, |
| 2508 | bool InInterface) { |
| 2509 | typedef CodeCompleteConsumer::Result Result; |
| 2510 | ResultBuilder Results(*this); |
| 2511 | Results.EnterNewScope(); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2512 | if (ObjCImpDecl) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2513 | AddObjCImplementationResults(getLangOptions(), Results, false); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2514 | else if (InInterface) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2515 | AddObjCInterfaceResults(getLangOptions(), Results, false); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2516 | else |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2517 | AddObjCTopLevelResults(Results, false); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 2518 | Results.ExitScope(); |
| 2519 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 2520 | } |
| 2521 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2522 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) { |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2523 | typedef CodeCompleteConsumer::Result Result; |
| 2524 | CodeCompletionString *Pattern = 0; |
| 2525 | |
| 2526 | // @encode ( type-name ) |
| 2527 | Pattern = new CodeCompletionString; |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2528 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,encode)); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2529 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 2530 | Pattern->AddPlaceholderChunk("type-name"); |
| 2531 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2532 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2533 | |
| 2534 | // @protocol ( protocol-name ) |
| 2535 | Pattern = new CodeCompletionString; |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2536 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol)); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2537 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 2538 | Pattern->AddPlaceholderChunk("protocol-name"); |
| 2539 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2540 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2541 | |
| 2542 | // @selector ( selector ) |
| 2543 | Pattern = new CodeCompletionString; |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2544 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,selector)); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2545 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 2546 | Pattern->AddPlaceholderChunk("selector"); |
| 2547 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2548 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2551 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) { |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2552 | typedef CodeCompleteConsumer::Result Result; |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2553 | CodeCompletionString *Pattern = 0; |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2554 | |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2555 | // @try { statements } @catch ( declaration ) { statements } @finally |
| 2556 | // { statements } |
| 2557 | Pattern = new CodeCompletionString; |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2558 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,try)); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2559 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2560 | Pattern->AddPlaceholderChunk("statements"); |
| 2561 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 2562 | Pattern->AddTextChunk("@catch"); |
| 2563 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 2564 | Pattern->AddPlaceholderChunk("parameter"); |
| 2565 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 2566 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2567 | Pattern->AddPlaceholderChunk("statements"); |
| 2568 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
| 2569 | Pattern->AddTextChunk("@finally"); |
| 2570 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2571 | Pattern->AddPlaceholderChunk("statements"); |
| 2572 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2573 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2574 | |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2575 | // @throw |
| 2576 | Pattern = new CodeCompletionString; |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2577 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,throw)); |
Douglas Gregor | 6a80393 | 2010-01-12 06:38:28 +0000 | [diff] [blame] | 2578 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2579 | Pattern->AddPlaceholderChunk("expression"); |
Douglas Gregor | 6a80393 | 2010-01-12 06:38:28 +0000 | [diff] [blame] | 2580 | Pattern->AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2581 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2582 | |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2583 | // @synchronized ( expression ) { statements } |
| 2584 | Pattern = new CodeCompletionString; |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2585 | Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synchronized)); |
Douglas Gregor | 6a80393 | 2010-01-12 06:38:28 +0000 | [diff] [blame] | 2586 | Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2587 | Pattern->AddChunk(CodeCompletionString::CK_LeftParen); |
| 2588 | Pattern->AddPlaceholderChunk("expression"); |
| 2589 | Pattern->AddChunk(CodeCompletionString::CK_RightParen); |
| 2590 | Pattern->AddChunk(CodeCompletionString::CK_LeftBrace); |
| 2591 | Pattern->AddPlaceholderChunk("statements"); |
| 2592 | Pattern->AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2593 | Results.AddResult(Result(Pattern)); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2594 | } |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2595 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2596 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 2597 | ResultBuilder &Results, |
| 2598 | bool NeedAt) { |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2599 | typedef CodeCompleteConsumer::Result Result; |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2600 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,private))); |
| 2601 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,protected))); |
| 2602 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,public))); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 2603 | if (LangOpts.ObjC2) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2604 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,package))); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
| 2607 | void Sema::CodeCompleteObjCAtVisibility(Scope *S) { |
| 2608 | ResultBuilder Results(*this); |
| 2609 | Results.EnterNewScope(); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2610 | AddObjCVisibilityResults(getLangOptions(), Results, false); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 2611 | Results.ExitScope(); |
| 2612 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 2613 | } |
| 2614 | |
| 2615 | void Sema::CodeCompleteObjCAtStatement(Scope *S) { |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 2616 | ResultBuilder Results(*this); |
| 2617 | Results.EnterNewScope(); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2618 | AddObjCStatementResults(Results, false); |
| 2619 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2620 | Results.ExitScope(); |
| 2621 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 2622 | } |
| 2623 | |
| 2624 | void Sema::CodeCompleteObjCAtExpression(Scope *S) { |
| 2625 | ResultBuilder Results(*this); |
| 2626 | Results.EnterNewScope(); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2627 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2628 | Results.ExitScope(); |
| 2629 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 2630 | } |
| 2631 | |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 2632 | /// \brief Determine whether the addition of the given flag to an Objective-C |
| 2633 | /// property's attributes will cause a conflict. |
| 2634 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { |
| 2635 | // Check if we've already added this flag. |
| 2636 | if (Attributes & NewFlag) |
| 2637 | return true; |
| 2638 | |
| 2639 | Attributes |= NewFlag; |
| 2640 | |
| 2641 | // Check for collisions with "readonly". |
| 2642 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 2643 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | |
| 2644 | ObjCDeclSpec::DQ_PR_assign | |
| 2645 | ObjCDeclSpec::DQ_PR_copy | |
| 2646 | ObjCDeclSpec::DQ_PR_retain))) |
| 2647 | return true; |
| 2648 | |
| 2649 | // Check for more than one of { assign, copy, retain }. |
| 2650 | unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign | |
| 2651 | ObjCDeclSpec::DQ_PR_copy | |
| 2652 | ObjCDeclSpec::DQ_PR_retain); |
| 2653 | if (AssignCopyRetMask && |
| 2654 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && |
| 2655 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && |
| 2656 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain) |
| 2657 | return true; |
| 2658 | |
| 2659 | return false; |
| 2660 | } |
| 2661 | |
Douglas Gregor | 36029f4 | 2009-11-18 23:08:07 +0000 | [diff] [blame] | 2662 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 2663 | if (!CodeCompleter) |
| 2664 | return; |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2665 | |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 2666 | unsigned Attributes = ODS.getPropertyAttributes(); |
| 2667 | |
| 2668 | typedef CodeCompleteConsumer::Result Result; |
| 2669 | ResultBuilder Results(*this); |
| 2670 | Results.EnterNewScope(); |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 2671 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2672 | Results.AddResult(CodeCompleteConsumer::Result("readonly")); |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 2673 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2674 | Results.AddResult(CodeCompleteConsumer::Result("assign")); |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 2675 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2676 | Results.AddResult(CodeCompleteConsumer::Result("readwrite")); |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 2677 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2678 | Results.AddResult(CodeCompleteConsumer::Result("retain")); |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 2679 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2680 | Results.AddResult(CodeCompleteConsumer::Result("copy")); |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 2681 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2682 | Results.AddResult(CodeCompleteConsumer::Result("nonatomic")); |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 2683 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 2684 | CodeCompletionString *Setter = new CodeCompletionString; |
| 2685 | Setter->AddTypedTextChunk("setter"); |
| 2686 | Setter->AddTextChunk(" = "); |
| 2687 | Setter->AddPlaceholderChunk("method"); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2688 | Results.AddResult(CodeCompleteConsumer::Result(Setter)); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 2689 | } |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 2690 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 2691 | CodeCompletionString *Getter = new CodeCompletionString; |
| 2692 | Getter->AddTypedTextChunk("getter"); |
| 2693 | Getter->AddTextChunk(" = "); |
| 2694 | Getter->AddPlaceholderChunk("method"); |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2695 | Results.AddResult(CodeCompleteConsumer::Result(Getter)); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 2696 | } |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 2697 | Results.ExitScope(); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 2698 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 2699 | } |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 2700 | |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2701 | /// \brief Descripts the kind of Objective-C method that we want to find |
| 2702 | /// via code completion. |
| 2703 | enum ObjCMethodKind { |
| 2704 | MK_Any, //< Any kind of method, provided it means other specified criteria. |
| 2705 | MK_ZeroArgSelector, //< Zero-argument (unary) selector. |
| 2706 | MK_OneArgSelector //< One-argument selector. |
| 2707 | }; |
| 2708 | |
| 2709 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, |
| 2710 | ObjCMethodKind WantKind, |
| 2711 | IdentifierInfo **SelIdents, |
| 2712 | unsigned NumSelIdents) { |
| 2713 | Selector Sel = Method->getSelector(); |
| 2714 | if (NumSelIdents > Sel.getNumArgs()) |
| 2715 | return false; |
| 2716 | |
| 2717 | switch (WantKind) { |
| 2718 | case MK_Any: break; |
| 2719 | case MK_ZeroArgSelector: return Sel.isUnarySelector(); |
| 2720 | case MK_OneArgSelector: return Sel.getNumArgs() == 1; |
| 2721 | } |
| 2722 | |
| 2723 | for (unsigned I = 0; I != NumSelIdents; ++I) |
| 2724 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) |
| 2725 | return false; |
| 2726 | |
| 2727 | return true; |
| 2728 | } |
| 2729 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 2730 | /// \brief Add all of the Objective-C methods in the given Objective-C |
| 2731 | /// container to the set of results. |
| 2732 | /// |
| 2733 | /// The container will be a class, protocol, category, or implementation of |
| 2734 | /// any of the above. This mether will recurse to include methods from |
| 2735 | /// the superclasses of classes along with their categories, protocols, and |
| 2736 | /// implementations. |
| 2737 | /// |
| 2738 | /// \param Container the container in which we'll look to find methods. |
| 2739 | /// |
| 2740 | /// \param WantInstance whether to add instance methods (only); if false, this |
| 2741 | /// routine will add factory methods (only). |
| 2742 | /// |
| 2743 | /// \param CurContext the context in which we're performing the lookup that |
| 2744 | /// finds methods. |
| 2745 | /// |
| 2746 | /// \param Results the structure into which we'll add results. |
| 2747 | static void AddObjCMethods(ObjCContainerDecl *Container, |
| 2748 | bool WantInstanceMethods, |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2749 | ObjCMethodKind WantKind, |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2750 | IdentifierInfo **SelIdents, |
| 2751 | unsigned NumSelIdents, |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 2752 | DeclContext *CurContext, |
| 2753 | ResultBuilder &Results) { |
| 2754 | typedef CodeCompleteConsumer::Result Result; |
| 2755 | for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), |
| 2756 | MEnd = Container->meth_end(); |
| 2757 | M != MEnd; ++M) { |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2758 | if ((*M)->isInstanceMethod() == WantInstanceMethods) { |
| 2759 | // Check whether the selector identifiers we've been given are a |
| 2760 | // subset of the identifiers for this particular method. |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2761 | if (!isAcceptableObjCMethod(*M, WantKind, SelIdents, NumSelIdents)) |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2762 | continue; |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2763 | |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2764 | Result R = Result(*M, 0); |
| 2765 | R.StartParameter = NumSelIdents; |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2766 | R.AllParametersAreInformative = (WantKind != MK_Any); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2767 | Results.MaybeAddResult(R, CurContext); |
| 2768 | } |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
| 2771 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); |
| 2772 | if (!IFace) |
| 2773 | return; |
| 2774 | |
| 2775 | // Add methods in protocols. |
| 2776 | const ObjCList<ObjCProtocolDecl> &Protocols= IFace->getReferencedProtocols(); |
| 2777 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 2778 | E = Protocols.end(); |
| 2779 | I != E; ++I) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2780 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents, |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2781 | CurContext, Results); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 2782 | |
| 2783 | // Add methods in categories. |
| 2784 | for (ObjCCategoryDecl *CatDecl = IFace->getCategoryList(); CatDecl; |
| 2785 | CatDecl = CatDecl->getNextClassCategory()) { |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2786 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, |
| 2787 | NumSelIdents, CurContext, Results); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 2788 | |
| 2789 | // Add a categories protocol methods. |
| 2790 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 2791 | = CatDecl->getReferencedProtocols(); |
| 2792 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 2793 | E = Protocols.end(); |
| 2794 | I != E; ++I) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2795 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, |
| 2796 | NumSelIdents, CurContext, Results); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 2797 | |
| 2798 | // Add methods in category implementations. |
| 2799 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2800 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
| 2801 | NumSelIdents, CurContext, Results); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | // Add methods in superclass. |
| 2805 | if (IFace->getSuperClass()) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2806 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, |
| 2807 | SelIdents, NumSelIdents, CurContext, Results); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 2808 | |
| 2809 | // Add methods in our implementation, if any. |
| 2810 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 2811 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, |
| 2812 | NumSelIdents, CurContext, Results); |
| 2813 | } |
| 2814 | |
| 2815 | |
| 2816 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S, DeclPtrTy ClassDecl, |
| 2817 | DeclPtrTy *Methods, |
| 2818 | unsigned NumMethods) { |
| 2819 | typedef CodeCompleteConsumer::Result Result; |
| 2820 | |
| 2821 | // Try to find the interface where getters might live. |
| 2822 | ObjCInterfaceDecl *Class |
| 2823 | = dyn_cast_or_null<ObjCInterfaceDecl>(ClassDecl.getAs<Decl>()); |
| 2824 | if (!Class) { |
| 2825 | if (ObjCCategoryDecl *Category |
| 2826 | = dyn_cast_or_null<ObjCCategoryDecl>(ClassDecl.getAs<Decl>())) |
| 2827 | Class = Category->getClassInterface(); |
| 2828 | |
| 2829 | if (!Class) |
| 2830 | return; |
| 2831 | } |
| 2832 | |
| 2833 | // Find all of the potential getters. |
| 2834 | ResultBuilder Results(*this); |
| 2835 | Results.EnterNewScope(); |
| 2836 | |
| 2837 | // FIXME: We need to do this because Objective-C methods don't get |
| 2838 | // pushed into DeclContexts early enough. Argh! |
| 2839 | for (unsigned I = 0; I != NumMethods; ++I) { |
| 2840 | if (ObjCMethodDecl *Method |
| 2841 | = dyn_cast_or_null<ObjCMethodDecl>(Methods[I].getAs<Decl>())) |
| 2842 | if (Method->isInstanceMethod() && |
| 2843 | isAcceptableObjCMethod(Method, MK_ZeroArgSelector, 0, 0)) { |
| 2844 | Result R = Result(Method, 0); |
| 2845 | R.AllParametersAreInformative = true; |
| 2846 | Results.MaybeAddResult(R, CurContext); |
| 2847 | } |
| 2848 | } |
| 2849 | |
| 2850 | AddObjCMethods(Class, true, MK_ZeroArgSelector, 0, 0, CurContext, Results); |
| 2851 | Results.ExitScope(); |
| 2852 | HandleCodeCompleteResults(this, CodeCompleter,Results.data(),Results.size()); |
| 2853 | } |
| 2854 | |
| 2855 | void Sema::CodeCompleteObjCPropertySetter(Scope *S, DeclPtrTy ObjCImplDecl, |
| 2856 | DeclPtrTy *Methods, |
| 2857 | unsigned NumMethods) { |
| 2858 | typedef CodeCompleteConsumer::Result Result; |
| 2859 | |
| 2860 | // Try to find the interface where setters might live. |
| 2861 | ObjCInterfaceDecl *Class |
| 2862 | = dyn_cast_or_null<ObjCInterfaceDecl>(ObjCImplDecl.getAs<Decl>()); |
| 2863 | if (!Class) { |
| 2864 | if (ObjCCategoryDecl *Category |
| 2865 | = dyn_cast_or_null<ObjCCategoryDecl>(ObjCImplDecl.getAs<Decl>())) |
| 2866 | Class = Category->getClassInterface(); |
| 2867 | |
| 2868 | if (!Class) |
| 2869 | return; |
| 2870 | } |
| 2871 | |
| 2872 | // Find all of the potential getters. |
| 2873 | ResultBuilder Results(*this); |
| 2874 | Results.EnterNewScope(); |
| 2875 | |
| 2876 | // FIXME: We need to do this because Objective-C methods don't get |
| 2877 | // pushed into DeclContexts early enough. Argh! |
| 2878 | for (unsigned I = 0; I != NumMethods; ++I) { |
| 2879 | if (ObjCMethodDecl *Method |
| 2880 | = dyn_cast_or_null<ObjCMethodDecl>(Methods[I].getAs<Decl>())) |
| 2881 | if (Method->isInstanceMethod() && |
| 2882 | isAcceptableObjCMethod(Method, MK_OneArgSelector, 0, 0)) { |
| 2883 | Result R = Result(Method, 0); |
| 2884 | R.AllParametersAreInformative = true; |
| 2885 | Results.MaybeAddResult(R, CurContext); |
| 2886 | } |
| 2887 | } |
| 2888 | |
| 2889 | AddObjCMethods(Class, true, MK_OneArgSelector, 0, 0, CurContext, Results); |
| 2890 | |
| 2891 | Results.ExitScope(); |
| 2892 | HandleCodeCompleteResults(this, CodeCompleter,Results.data(),Results.size()); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 2895 | /// \brief When we have an expression with type "id", we may assume |
| 2896 | /// that it has some more-specific class type based on knowledge of |
| 2897 | /// common uses of Objective-C. This routine returns that class type, |
| 2898 | /// or NULL if no better result could be determined. |
| 2899 | static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) { |
| 2900 | ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E); |
| 2901 | if (!Msg) |
| 2902 | return 0; |
| 2903 | |
| 2904 | Selector Sel = Msg->getSelector(); |
| 2905 | if (Sel.isNull()) |
| 2906 | return 0; |
| 2907 | |
| 2908 | IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0); |
| 2909 | if (!Id) |
| 2910 | return 0; |
| 2911 | |
| 2912 | ObjCMethodDecl *Method = Msg->getMethodDecl(); |
| 2913 | if (!Method) |
| 2914 | return 0; |
| 2915 | |
| 2916 | // Determine the class that we're sending the message to. |
| 2917 | ObjCInterfaceDecl *IFace = Msg->getClassInfo().Decl; |
| 2918 | if (!IFace) { |
| 2919 | if (Expr *Receiver = Msg->getReceiver()) { |
| 2920 | QualType T = Receiver->getType(); |
| 2921 | if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) |
| 2922 | IFace = Ptr->getInterfaceDecl(); |
| 2923 | } |
| 2924 | } |
| 2925 | |
| 2926 | if (!IFace) |
| 2927 | return 0; |
| 2928 | |
| 2929 | ObjCInterfaceDecl *Super = IFace->getSuperClass(); |
| 2930 | if (Method->isInstanceMethod()) |
| 2931 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 2932 | .Case("retain", IFace) |
| 2933 | .Case("autorelease", IFace) |
| 2934 | .Case("copy", IFace) |
| 2935 | .Case("copyWithZone", IFace) |
| 2936 | .Case("mutableCopy", IFace) |
| 2937 | .Case("mutableCopyWithZone", IFace) |
| 2938 | .Case("awakeFromCoder", IFace) |
| 2939 | .Case("replacementObjectFromCoder", IFace) |
| 2940 | .Case("class", IFace) |
| 2941 | .Case("classForCoder", IFace) |
| 2942 | .Case("superclass", Super) |
| 2943 | .Default(0); |
| 2944 | |
| 2945 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 2946 | .Case("new", IFace) |
| 2947 | .Case("alloc", IFace) |
| 2948 | .Case("allocWithZone", IFace) |
| 2949 | .Case("class", IFace) |
| 2950 | .Case("superclass", Super) |
| 2951 | .Default(0); |
| 2952 | } |
| 2953 | |
Douglas Gregor | 090dd18 | 2009-11-17 23:31:36 +0000 | [diff] [blame] | 2954 | void Sema::CodeCompleteObjCClassMessage(Scope *S, IdentifierInfo *FName, |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2955 | SourceLocation FNameLoc, |
| 2956 | IdentifierInfo **SelIdents, |
| 2957 | unsigned NumSelIdents) { |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 2958 | typedef CodeCompleteConsumer::Result Result; |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 2959 | ObjCInterfaceDecl *CDecl = 0; |
| 2960 | |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 2961 | if (FName->isStr("super")) { |
| 2962 | // We're sending a message to "super". |
| 2963 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 2964 | // Figure out which interface we're in. |
| 2965 | CDecl = CurMethod->getClassInterface(); |
| 2966 | if (!CDecl) |
| 2967 | return; |
| 2968 | |
| 2969 | // Find the superclass of this class. |
| 2970 | CDecl = CDecl->getSuperClass(); |
| 2971 | if (!CDecl) |
| 2972 | return; |
| 2973 | |
| 2974 | if (CurMethod->isInstanceMethod()) { |
| 2975 | // We are inside an instance method, which means that the message |
| 2976 | // send [super ...] is actually calling an instance method on the |
| 2977 | // current object. Build the super expression and handle this like |
| 2978 | // an instance method. |
| 2979 | QualType SuperTy = Context.getObjCInterfaceType(CDecl); |
| 2980 | SuperTy = Context.getObjCObjectPointerType(SuperTy); |
| 2981 | OwningExprResult Super |
Douglas Gregor | 090dd18 | 2009-11-17 23:31:36 +0000 | [diff] [blame] | 2982 | = Owned(new (Context) ObjCSuperExpr(FNameLoc, SuperTy)); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2983 | return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get(), |
| 2984 | SelIdents, NumSelIdents); |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | // Okay, we're calling a factory method in our superclass. |
| 2988 | } |
| 2989 | } |
| 2990 | |
| 2991 | // If the given name refers to an interface type, retrieve the |
| 2992 | // corresponding declaration. |
| 2993 | if (!CDecl) |
Douglas Gregor | 090dd18 | 2009-11-17 23:31:36 +0000 | [diff] [blame] | 2994 | if (TypeTy *Ty = getTypeName(*FName, FNameLoc, S, 0, false)) { |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 2995 | QualType T = GetTypeFromParser(Ty, 0); |
| 2996 | if (!T.isNull()) |
| 2997 | if (const ObjCInterfaceType *Interface = T->getAs<ObjCInterfaceType>()) |
| 2998 | CDecl = Interface->getDecl(); |
| 2999 | } |
| 3000 | |
| 3001 | if (!CDecl && FName->isStr("super")) { |
| 3002 | // "super" may be the name of a variable, in which case we are |
| 3003 | // probably calling an instance method. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3004 | CXXScopeSpec SS; |
| 3005 | UnqualifiedId id; |
| 3006 | id.setIdentifier(FName, FNameLoc); |
| 3007 | OwningExprResult Super = ActOnIdExpression(S, SS, id, false, false); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3008 | return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get(), |
| 3009 | SelIdents, NumSelIdents); |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3012 | // Add all of the factory methods in this Objective-C class, its protocols, |
| 3013 | // superclasses, categories, implementation, etc. |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 3014 | ResultBuilder Results(*this); |
| 3015 | Results.EnterNewScope(); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 3016 | |
| 3017 | if (CDecl) |
| 3018 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, NumSelIdents, CurContext, |
| 3019 | Results); |
| 3020 | else if (FName->isStr("id")) { |
| 3021 | // We're messaging "id" as a type; provide all class/factory methods. |
| 3022 | |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 3023 | // If we have an external source, load the entire class method |
| 3024 | // pool from the PCH file. |
| 3025 | if (ExternalSource) { |
| 3026 | for (uint32_t I = 0, N = ExternalSource->GetNumKnownSelectors(); I != N; |
| 3027 | ++I) { |
| 3028 | Selector Sel = ExternalSource->GetSelector(I); |
| 3029 | if (Sel.isNull() || FactoryMethodPool.count(Sel) || |
| 3030 | InstanceMethodPool.count(Sel)) |
| 3031 | continue; |
| 3032 | |
| 3033 | ReadMethodPool(Sel, /*isInstance=*/false); |
| 3034 | } |
| 3035 | } |
| 3036 | |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 3037 | for (llvm::DenseMap<Selector, ObjCMethodList>::iterator |
| 3038 | M = FactoryMethodPool.begin(), |
| 3039 | MEnd = FactoryMethodPool.end(); |
| 3040 | M != MEnd; |
| 3041 | ++M) { |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 3042 | for (ObjCMethodList *MethList = &M->second; MethList && MethList->Method; |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 3043 | MethList = MethList->Next) { |
| 3044 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 3045 | NumSelIdents)) |
| 3046 | continue; |
| 3047 | |
| 3048 | Result R(MethList->Method, 0); |
| 3049 | R.StartParameter = NumSelIdents; |
| 3050 | R.AllParametersAreInformative = false; |
| 3051 | Results.MaybeAddResult(R, CurContext); |
| 3052 | } |
| 3053 | } |
| 3054 | } |
| 3055 | |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 3056 | Results.ExitScope(); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3057 | |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 3058 | // This also suppresses remaining diagnostics. |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3059 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 3060 | } |
| 3061 | |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 3062 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver, |
| 3063 | IdentifierInfo **SelIdents, |
| 3064 | unsigned NumSelIdents) { |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 3065 | typedef CodeCompleteConsumer::Result Result; |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 3066 | |
| 3067 | Expr *RecExpr = static_cast<Expr *>(Receiver); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 3068 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3069 | // If necessary, apply function/array conversion to the receiver. |
| 3070 | // C99 6.7.5.3p[7,8]. |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3071 | DefaultFunctionArrayLvalueConversion(RecExpr); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3072 | QualType ReceiverType = RecExpr->getType(); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 3073 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3074 | // Build the set of methods we can see. |
| 3075 | ResultBuilder Results(*this); |
| 3076 | Results.EnterNewScope(); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 3077 | |
| 3078 | // If we're messaging an expression with type "id" or "Class", check |
| 3079 | // whether we know something special about the receiver that allows |
| 3080 | // us to assume a more-specific receiver type. |
| 3081 | if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) |
| 3082 | if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) |
| 3083 | ReceiverType = Context.getObjCObjectPointerType( |
| 3084 | Context.getObjCInterfaceType(IFace)); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 3085 | |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 3086 | // Handle messages to Class. This really isn't a message to an instance |
| 3087 | // method, so we treat it the same way we would treat a message send to a |
| 3088 | // class method. |
| 3089 | if (ReceiverType->isObjCClassType() || |
| 3090 | ReceiverType->isObjCQualifiedClassType()) { |
| 3091 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 3092 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3093 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, NumSelIdents, |
| 3094 | CurContext, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 3095 | } |
| 3096 | } |
| 3097 | // Handle messages to a qualified ID ("id<foo>"). |
| 3098 | else if (const ObjCObjectPointerType *QualID |
| 3099 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 3100 | // Search protocols for instance methods. |
| 3101 | for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(), |
| 3102 | E = QualID->qual_end(); |
| 3103 | I != E; ++I) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3104 | AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext, |
| 3105 | Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 3106 | } |
| 3107 | // Handle messages to a pointer to interface type. |
| 3108 | else if (const ObjCObjectPointerType *IFacePtr |
| 3109 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 3110 | // Search the class, its superclasses, etc., for instance methods. |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3111 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, |
| 3112 | NumSelIdents, CurContext, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 3113 | |
| 3114 | // Search protocols for instance methods. |
| 3115 | for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(), |
| 3116 | E = IFacePtr->qual_end(); |
| 3117 | I != E; ++I) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3118 | AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext, |
| 3119 | Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 3120 | } |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 3121 | // Handle messages to "id". |
| 3122 | else if (ReceiverType->isObjCIdType()) { |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 3123 | // We're messaging "id", so provide all instance methods we know |
| 3124 | // about as code-completion results. |
| 3125 | |
| 3126 | // If we have an external source, load the entire class method |
| 3127 | // pool from the PCH file. |
| 3128 | if (ExternalSource) { |
| 3129 | for (uint32_t I = 0, N = ExternalSource->GetNumKnownSelectors(); I != N; |
| 3130 | ++I) { |
| 3131 | Selector Sel = ExternalSource->GetSelector(I); |
| 3132 | if (Sel.isNull() || InstanceMethodPool.count(Sel) || |
| 3133 | FactoryMethodPool.count(Sel)) |
| 3134 | continue; |
| 3135 | |
| 3136 | ReadMethodPool(Sel, /*isInstance=*/true); |
| 3137 | } |
| 3138 | } |
| 3139 | |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 3140 | for (llvm::DenseMap<Selector, ObjCMethodList>::iterator |
| 3141 | M = InstanceMethodPool.begin(), |
| 3142 | MEnd = InstanceMethodPool.end(); |
| 3143 | M != MEnd; |
| 3144 | ++M) { |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 3145 | for (ObjCMethodList *MethList = &M->second; MethList && MethList->Method; |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 3146 | MethList = MethList->Next) { |
| 3147 | if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents, |
| 3148 | NumSelIdents)) |
| 3149 | continue; |
| 3150 | |
| 3151 | Result R(MethList->Method, 0); |
| 3152 | R.StartParameter = NumSelIdents; |
| 3153 | R.AllParametersAreInformative = false; |
| 3154 | Results.MaybeAddResult(R, CurContext); |
| 3155 | } |
| 3156 | } |
| 3157 | } |
| 3158 | |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 3159 | Results.ExitScope(); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3160 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 3161 | } |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 3162 | |
| 3163 | /// \brief Add all of the protocol declarations that we find in the given |
| 3164 | /// (translation unit) context. |
| 3165 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 3166 | bool OnlyForwardDeclarations, |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 3167 | ResultBuilder &Results) { |
| 3168 | typedef CodeCompleteConsumer::Result Result; |
| 3169 | |
| 3170 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 3171 | DEnd = Ctx->decls_end(); |
| 3172 | D != DEnd; ++D) { |
| 3173 | // Record any protocols we find. |
| 3174 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*D)) |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 3175 | if (!OnlyForwardDeclarations || Proto->isForwardDecl()) |
Douglas Gregor | fc59ce1 | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 3176 | Results.AddResult(Result(Proto, 0), CurContext, 0, false); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 3177 | |
| 3178 | // Record any forward-declared protocols we find. |
| 3179 | if (ObjCForwardProtocolDecl *Forward |
| 3180 | = dyn_cast<ObjCForwardProtocolDecl>(*D)) { |
| 3181 | for (ObjCForwardProtocolDecl::protocol_iterator |
| 3182 | P = Forward->protocol_begin(), |
| 3183 | PEnd = Forward->protocol_end(); |
| 3184 | P != PEnd; ++P) |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 3185 | if (!OnlyForwardDeclarations || (*P)->isForwardDecl()) |
Douglas Gregor | fc59ce1 | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 3186 | Results.AddResult(Result(*P, 0), CurContext, 0, false); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 3187 | } |
| 3188 | } |
| 3189 | } |
| 3190 | |
| 3191 | void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols, |
| 3192 | unsigned NumProtocols) { |
| 3193 | ResultBuilder Results(*this); |
| 3194 | Results.EnterNewScope(); |
| 3195 | |
| 3196 | // Tell the result set to ignore all of the protocols we have |
| 3197 | // already seen. |
| 3198 | for (unsigned I = 0; I != NumProtocols; ++I) |
| 3199 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first)) |
| 3200 | Results.Ignore(Protocol); |
| 3201 | |
| 3202 | // Add all protocols. |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 3203 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 3204 | Results); |
| 3205 | |
| 3206 | Results.ExitScope(); |
| 3207 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 3208 | } |
| 3209 | |
| 3210 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { |
| 3211 | ResultBuilder Results(*this); |
| 3212 | Results.EnterNewScope(); |
| 3213 | |
| 3214 | // Add all protocols. |
| 3215 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 3216 | Results); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 3217 | |
| 3218 | Results.ExitScope(); |
| 3219 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 3220 | } |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 3221 | |
| 3222 | /// \brief Add all of the Objective-C interface declarations that we find in |
| 3223 | /// the given (translation unit) context. |
| 3224 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, |
| 3225 | bool OnlyForwardDeclarations, |
| 3226 | bool OnlyUnimplemented, |
| 3227 | ResultBuilder &Results) { |
| 3228 | typedef CodeCompleteConsumer::Result Result; |
| 3229 | |
| 3230 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 3231 | DEnd = Ctx->decls_end(); |
| 3232 | D != DEnd; ++D) { |
| 3233 | // Record any interfaces we find. |
| 3234 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*D)) |
| 3235 | if ((!OnlyForwardDeclarations || Class->isForwardDecl()) && |
| 3236 | (!OnlyUnimplemented || !Class->getImplementation())) |
Douglas Gregor | fc59ce1 | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 3237 | Results.AddResult(Result(Class, 0), CurContext, 0, false); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 3238 | |
| 3239 | // Record any forward-declared interfaces we find. |
| 3240 | if (ObjCClassDecl *Forward = dyn_cast<ObjCClassDecl>(*D)) { |
| 3241 | for (ObjCClassDecl::iterator C = Forward->begin(), CEnd = Forward->end(); |
| 3242 | C != CEnd; ++C) |
| 3243 | if ((!OnlyForwardDeclarations || C->getInterface()->isForwardDecl()) && |
| 3244 | (!OnlyUnimplemented || !C->getInterface()->getImplementation())) |
Douglas Gregor | fc59ce1 | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 3245 | Results.AddResult(Result(C->getInterface(), 0), CurContext, |
| 3246 | 0, false); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 3247 | } |
| 3248 | } |
| 3249 | } |
| 3250 | |
| 3251 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { |
| 3252 | ResultBuilder Results(*this); |
| 3253 | Results.EnterNewScope(); |
| 3254 | |
| 3255 | // Add all classes. |
| 3256 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 3257 | false, Results); |
| 3258 | |
| 3259 | Results.ExitScope(); |
| 3260 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 3261 | } |
| 3262 | |
| 3263 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName) { |
| 3264 | ResultBuilder Results(*this); |
| 3265 | Results.EnterNewScope(); |
| 3266 | |
| 3267 | // Make sure that we ignore the class we're currently defining. |
| 3268 | NamedDecl *CurClass |
| 3269 | = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 3270 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 3271 | Results.Ignore(CurClass); |
| 3272 | |
| 3273 | // Add all classes. |
| 3274 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 3275 | false, Results); |
| 3276 | |
| 3277 | Results.ExitScope(); |
| 3278 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 3279 | } |
| 3280 | |
| 3281 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { |
| 3282 | ResultBuilder Results(*this); |
| 3283 | Results.EnterNewScope(); |
| 3284 | |
| 3285 | // Add all unimplemented classes. |
| 3286 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 3287 | true, Results); |
| 3288 | |
| 3289 | Results.ExitScope(); |
| 3290 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 3291 | } |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 3292 | |
| 3293 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, |
| 3294 | IdentifierInfo *ClassName) { |
| 3295 | typedef CodeCompleteConsumer::Result Result; |
| 3296 | |
| 3297 | ResultBuilder Results(*this); |
| 3298 | |
| 3299 | // Ignore any categories we find that have already been implemented by this |
| 3300 | // interface. |
| 3301 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 3302 | NamedDecl *CurClass |
| 3303 | = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); |
| 3304 | if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)) |
| 3305 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; |
| 3306 | Category = Category->getNextClassCategory()) |
| 3307 | CategoryNames.insert(Category->getIdentifier()); |
| 3308 | |
| 3309 | // Add all of the categories we know about. |
| 3310 | Results.EnterNewScope(); |
| 3311 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
| 3312 | for (DeclContext::decl_iterator D = TU->decls_begin(), |
| 3313 | DEnd = TU->decls_end(); |
| 3314 | D != DEnd; ++D) |
| 3315 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(*D)) |
| 3316 | if (CategoryNames.insert(Category->getIdentifier())) |
Douglas Gregor | fc59ce1 | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 3317 | Results.AddResult(Result(Category, 0), CurContext, 0, false); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 3318 | Results.ExitScope(); |
| 3319 | |
| 3320 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 3321 | } |
| 3322 | |
| 3323 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, |
| 3324 | IdentifierInfo *ClassName) { |
| 3325 | typedef CodeCompleteConsumer::Result Result; |
| 3326 | |
| 3327 | // Find the corresponding interface. If we couldn't find the interface, the |
| 3328 | // program itself is ill-formed. However, we'll try to be helpful still by |
| 3329 | // providing the list of all of the categories we know about. |
| 3330 | NamedDecl *CurClass |
| 3331 | = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); |
| 3332 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); |
| 3333 | if (!Class) |
| 3334 | return CodeCompleteObjCInterfaceCategory(S, ClassName); |
| 3335 | |
| 3336 | ResultBuilder Results(*this); |
| 3337 | |
| 3338 | // Add all of the categories that have have corresponding interface |
| 3339 | // declarations in this class and any of its superclasses, except for |
| 3340 | // already-implemented categories in the class itself. |
| 3341 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 3342 | Results.EnterNewScope(); |
| 3343 | bool IgnoreImplemented = true; |
| 3344 | while (Class) { |
| 3345 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category; |
| 3346 | Category = Category->getNextClassCategory()) |
| 3347 | if ((!IgnoreImplemented || !Category->getImplementation()) && |
| 3348 | CategoryNames.insert(Category->getIdentifier())) |
Douglas Gregor | fc59ce1 | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 3349 | Results.AddResult(Result(Category, 0), CurContext, 0, false); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 3350 | |
| 3351 | Class = Class->getSuperClass(); |
| 3352 | IgnoreImplemented = false; |
| 3353 | } |
| 3354 | Results.ExitScope(); |
| 3355 | |
| 3356 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 3357 | } |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3358 | |
Douglas Gregor | 52e78bd | 2009-11-18 22:56:13 +0000 | [diff] [blame] | 3359 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S, DeclPtrTy ObjCImpDecl) { |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3360 | typedef CodeCompleteConsumer::Result Result; |
| 3361 | ResultBuilder Results(*this); |
| 3362 | |
| 3363 | // Figure out where this @synthesize lives. |
| 3364 | ObjCContainerDecl *Container |
| 3365 | = dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl.getAs<Decl>()); |
| 3366 | if (!Container || |
| 3367 | (!isa<ObjCImplementationDecl>(Container) && |
| 3368 | !isa<ObjCCategoryImplDecl>(Container))) |
| 3369 | return; |
| 3370 | |
| 3371 | // Ignore any properties that have already been implemented. |
| 3372 | for (DeclContext::decl_iterator D = Container->decls_begin(), |
| 3373 | DEnd = Container->decls_end(); |
| 3374 | D != DEnd; ++D) |
| 3375 | if (ObjCPropertyImplDecl *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(*D)) |
| 3376 | Results.Ignore(PropertyImpl->getPropertyDecl()); |
| 3377 | |
| 3378 | // Add any properties that we find. |
| 3379 | Results.EnterNewScope(); |
| 3380 | if (ObjCImplementationDecl *ClassImpl |
| 3381 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| 3382 | AddObjCProperties(ClassImpl->getClassInterface(), false, CurContext, |
| 3383 | Results); |
| 3384 | else |
| 3385 | AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), |
| 3386 | false, CurContext, Results); |
| 3387 | Results.ExitScope(); |
| 3388 | |
| 3389 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 3390 | } |
| 3391 | |
| 3392 | void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S, |
| 3393 | IdentifierInfo *PropertyName, |
| 3394 | DeclPtrTy ObjCImpDecl) { |
| 3395 | typedef CodeCompleteConsumer::Result Result; |
| 3396 | ResultBuilder Results(*this); |
| 3397 | |
| 3398 | // Figure out where this @synthesize lives. |
| 3399 | ObjCContainerDecl *Container |
| 3400 | = dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl.getAs<Decl>()); |
| 3401 | if (!Container || |
| 3402 | (!isa<ObjCImplementationDecl>(Container) && |
| 3403 | !isa<ObjCCategoryImplDecl>(Container))) |
| 3404 | return; |
| 3405 | |
| 3406 | // Figure out which interface we're looking into. |
| 3407 | ObjCInterfaceDecl *Class = 0; |
| 3408 | if (ObjCImplementationDecl *ClassImpl |
| 3409 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| 3410 | Class = ClassImpl->getClassInterface(); |
| 3411 | else |
| 3412 | Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl() |
| 3413 | ->getClassInterface(); |
| 3414 | |
| 3415 | // Add all of the instance variables in this class and its superclasses. |
| 3416 | Results.EnterNewScope(); |
| 3417 | for(; Class; Class = Class->getSuperClass()) { |
| 3418 | // FIXME: We could screen the type of each ivar for compatibility with |
| 3419 | // the property, but is that being too paternal? |
| 3420 | for (ObjCInterfaceDecl::ivar_iterator IVar = Class->ivar_begin(), |
| 3421 | IVarEnd = Class->ivar_end(); |
| 3422 | IVar != IVarEnd; ++IVar) |
Douglas Gregor | fc59ce1 | 2010-01-14 16:14:35 +0000 | [diff] [blame] | 3423 | Results.AddResult(Result(*IVar, 0), CurContext, 0, false); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3424 | } |
| 3425 | Results.ExitScope(); |
| 3426 | |
| 3427 | HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); |
| 3428 | } |