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