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 | //===----------------------------------------------------------------------===// |
Ilya Biryukov | 4e7a6fe | 2017-09-22 19:07:37 +0000 | [diff] [blame] | 13 | #include "clang/AST/DeclCXX.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 15 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprObjC.h" |
Ilya Biryukov | b5da91c | 2017-11-08 10:39:09 +0000 | [diff] [blame] | 17 | #include "clang/AST/QualTypeNames.h" |
Jordan Rose | 4938f27 | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 18 | #include "clang/Basic/CharInfo.h" |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 19 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 20 | #include "clang/Lex/MacroInfo.h" |
| 21 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Sema/CodeCompleteConsumer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Sema/Lookup.h" |
| 24 | #include "clang/Sema/Overload.h" |
| 25 | #include "clang/Sema/Scope.h" |
| 26 | #include "clang/Sema/ScopeInfo.h" |
Ilya Biryukov | 4e7a6fe | 2017-09-22 19:07:37 +0000 | [diff] [blame] | 27 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseSet.h" |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallBitVector.h" |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallPtrSet.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | e6688e6 | 2009-09-28 03:51:44 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/StringSwitch.h" |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 35 | #include <list> |
| 36 | #include <map> |
| 37 | #include <vector> |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 38 | |
| 39 | using namespace clang; |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 40 | using namespace sema; |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 41 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 42 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 43 | /// A container of code-completion results. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 44 | class ResultBuilder { |
| 45 | public: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 46 | /// The type of a name-lookup filter, which can be provided to the |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 47 | /// name-lookup routines to specify which declarations should be included in |
| 48 | /// the result set (when it returns true) and which declarations should be |
| 49 | /// filtered out (returns false). |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 50 | typedef bool (ResultBuilder::*LookupFilter)(const NamedDecl *) const; |
Ivan Donchevskii | 13d9054 | 2017-10-27 11:05:40 +0000 | [diff] [blame] | 51 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 52 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 53 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 54 | private: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 55 | /// The actual results we have found. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 56 | std::vector<Result> Results; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 57 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 58 | /// A record of all of the declarations we have found and placed |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 59 | /// into the result set, used to ensure that no declaration ever gets into |
| 60 | /// the result set twice. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 61 | llvm::SmallPtrSet<const Decl*, 16> AllDeclsFound; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 62 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 63 | typedef std::pair<const NamedDecl *, unsigned> DeclIndexPair; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 64 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 65 | /// An entry in the shadow map, which is optimized to store |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 66 | /// a single (declaration, index) mapping (the common case) but |
| 67 | /// can also store a list of (declaration, index) mappings. |
| 68 | class ShadowMapEntry { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 69 | typedef SmallVector<DeclIndexPair, 4> DeclIndexPairVector; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 70 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 71 | /// Contains either the solitary NamedDecl * or a vector |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 72 | /// of (declaration, index) pairs. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 73 | llvm::PointerUnion<const NamedDecl *, DeclIndexPairVector*> DeclOrVector; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 74 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 75 | /// When the entry contains a single declaration, this is |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 76 | /// the index associated with that entry. |
| 77 | unsigned SingleDeclIndex; |
| 78 | |
| 79 | public: |
| 80 | ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { } |
| 81 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 82 | void Add(const NamedDecl *ND, unsigned Index) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 83 | if (DeclOrVector.isNull()) { |
| 84 | // 0 - > 1 elements: just set the single element information. |
| 85 | DeclOrVector = ND; |
| 86 | SingleDeclIndex = Index; |
| 87 | return; |
| 88 | } |
| 89 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 90 | if (const NamedDecl *PrevND = |
| 91 | DeclOrVector.dyn_cast<const NamedDecl *>()) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 92 | // 1 -> 2 elements: create the vector of results and push in the |
| 93 | // existing declaration. |
| 94 | DeclIndexPairVector *Vec = new DeclIndexPairVector; |
| 95 | Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex)); |
| 96 | DeclOrVector = Vec; |
| 97 | } |
| 98 | |
| 99 | // Add the new element to the end of the vector. |
| 100 | DeclOrVector.get<DeclIndexPairVector*>()->push_back( |
| 101 | DeclIndexPair(ND, Index)); |
| 102 | } |
| 103 | |
| 104 | void Destroy() { |
| 105 | if (DeclIndexPairVector *Vec |
| 106 | = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) { |
| 107 | delete Vec; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 108 | DeclOrVector = ((NamedDecl *)nullptr); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | // Iteration. |
| 113 | class iterator; |
| 114 | iterator begin() const; |
| 115 | iterator end() const; |
| 116 | }; |
| 117 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 118 | /// A mapping from declaration names to the declarations that have |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 119 | /// this name within a particular scope and their index within the list of |
| 120 | /// results. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 121 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 122 | |
| 123 | /// The semantic analysis object for which results are being |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 124 | /// produced. |
| 125 | Sema &SemaRef; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 126 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 127 | /// The allocator used to allocate new code-completion strings. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 128 | CodeCompletionAllocator &Allocator; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 129 | |
| 130 | CodeCompletionTUInfo &CCTUInfo; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 131 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 132 | /// If non-NULL, a filter function used to remove any code-completion |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 133 | /// results that are not desirable. |
| 134 | LookupFilter Filter; |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 135 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 136 | /// Whether we should allow declarations as |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 137 | /// nested-name-specifiers that would otherwise be filtered out. |
| 138 | bool AllowNestedNameSpecifiers; |
| 139 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 140 | /// If set, the type that we would prefer our resulting value |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 141 | /// declarations to have. |
| 142 | /// |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 143 | /// Closely matching the preferred type gives a boost to a result's |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 144 | /// priority. |
| 145 | CanQualType PreferredType; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 146 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 147 | /// A list of shadow maps, which is used to model name hiding at |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 148 | /// different levels of, e.g., the inheritance hierarchy. |
| 149 | std::list<ShadowMap> ShadowMaps; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 150 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 151 | /// If we're potentially referring to a C++ member function, the set |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 152 | /// of qualifiers applied to the object type. |
| 153 | Qualifiers ObjectTypeQualifiers; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 154 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 155 | /// Whether the \p ObjectTypeQualifiers field is active. |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 156 | bool HasObjectTypeQualifiers; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 157 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 158 | /// The selector that we prefer. |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 159 | Selector PreferredSelector; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 160 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 161 | /// The completion context in which we are gathering results. |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 162 | CodeCompletionContext CompletionContext; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 163 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 164 | /// If we are in an instance method definition, the \@implementation |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 165 | /// object. |
| 166 | ObjCImplementationDecl *ObjCImplementation; |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 167 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 168 | void AdjustResultPriorityForDecl(Result &R); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 169 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 170 | void MaybeAddConstructorResults(Result R); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 171 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 172 | public: |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 173 | explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator, |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 174 | CodeCompletionTUInfo &CCTUInfo, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 175 | const CodeCompletionContext &CompletionContext, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 176 | LookupFilter Filter = nullptr) |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 177 | : SemaRef(SemaRef), Allocator(Allocator), CCTUInfo(CCTUInfo), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 178 | Filter(Filter), |
| 179 | AllowNestedNameSpecifiers(false), HasObjectTypeQualifiers(false), |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 180 | CompletionContext(CompletionContext), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 181 | ObjCImplementation(nullptr) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 182 | { |
| 183 | // If this is an Objective-C instance method definition, dig out the |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 184 | // corresponding implementation. |
| 185 | switch (CompletionContext.getKind()) { |
| 186 | case CodeCompletionContext::CCC_Expression: |
| 187 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
| 188 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
| 189 | case CodeCompletionContext::CCC_Statement: |
| 190 | case CodeCompletionContext::CCC_Recovery: |
| 191 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) |
| 192 | if (Method->isInstanceMethod()) |
| 193 | if (ObjCInterfaceDecl *Interface = Method->getClassInterface()) |
| 194 | ObjCImplementation = Interface->getImplementation(); |
| 195 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 196 | |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 197 | default: |
| 198 | break; |
| 199 | } |
| 200 | } |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 201 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 202 | /// Determine the priority for a reference to the given declaration. |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 203 | unsigned getBasePriority(const NamedDecl *D); |
| 204 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 205 | /// Whether we should include code patterns in the completion |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 206 | /// results. |
| 207 | bool includeCodePatterns() const { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 208 | return SemaRef.CodeCompleter && |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 209 | SemaRef.CodeCompleter->includeCodePatterns(); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 210 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 211 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 212 | /// Set the filter used for code-completion results. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 213 | void setFilter(LookupFilter Filter) { |
| 214 | this->Filter = Filter; |
| 215 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 216 | |
| 217 | Result *data() { return Results.empty()? nullptr : &Results.front(); } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 218 | unsigned size() const { return Results.size(); } |
| 219 | bool empty() const { return Results.empty(); } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 220 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 221 | /// Specify the preferred type. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 222 | void setPreferredType(QualType T) { |
| 223 | PreferredType = SemaRef.Context.getCanonicalType(T); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 224 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 225 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 226 | /// Set the cv-qualifiers on the object type, for us in filtering |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 227 | /// calls to member functions. |
| 228 | /// |
| 229 | /// When there are qualifiers in this set, they will be used to filter |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 230 | /// out member functions that aren't available (because there will be a |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 231 | /// cv-qualifier mismatch) or prefer functions with an exact qualifier |
| 232 | /// match. |
| 233 | void setObjectTypeQualifiers(Qualifiers Quals) { |
| 234 | ObjectTypeQualifiers = Quals; |
| 235 | HasObjectTypeQualifiers = true; |
| 236 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 237 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 238 | /// Set the preferred selector. |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 239 | /// |
| 240 | /// When an Objective-C method declaration result is added, and that |
| 241 | /// method's selector matches this preferred selector, we give that method |
| 242 | /// a slight priority boost. |
| 243 | void setPreferredSelector(Selector Sel) { |
| 244 | PreferredSelector = Sel; |
| 245 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 246 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 247 | /// Retrieve the code-completion context for which results are |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 248 | /// being collected. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 249 | const CodeCompletionContext &getCompletionContext() const { |
| 250 | return CompletionContext; |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 251 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 252 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 253 | /// Specify whether nested-name-specifiers are allowed. |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 254 | void allowNestedNameSpecifiers(bool Allow = true) { |
| 255 | AllowNestedNameSpecifiers = Allow; |
| 256 | } |
| 257 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 258 | /// Return the semantic analysis object for which we are collecting |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 259 | /// code completion results. |
| 260 | Sema &getSema() const { return SemaRef; } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 261 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 262 | /// Retrieve the allocator used to allocate code completion strings. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 263 | CodeCompletionAllocator &getAllocator() const { return Allocator; } |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 264 | |
| 265 | CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 266 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 267 | /// Determine whether the given declaration is at all interesting |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 268 | /// as a code-completion result. |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 269 | /// |
| 270 | /// \param ND the declaration that we are inspecting. |
| 271 | /// |
| 272 | /// \param AsNestedNameSpecifier will be set true if this declaration is |
| 273 | /// only interesting when it is a nested-name-specifier. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 274 | bool isInterestingDecl(const NamedDecl *ND, |
| 275 | bool &AsNestedNameSpecifier) const; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 276 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 277 | /// Check whether the result is hidden by the Hiding declaration. |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 278 | /// |
| 279 | /// \returns true if the result is hidden and cannot be found, false if |
| 280 | /// the hidden result could still be found. When false, \p R may be |
| 281 | /// modified to describe how the result can be found (e.g., via extra |
| 282 | /// qualification). |
| 283 | bool CheckHiddenResult(Result &R, DeclContext *CurContext, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 284 | const NamedDecl *Hiding); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 285 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 286 | /// Add a new result to this result set (if it isn't already in one |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 287 | /// of the shadow maps), or replace an existing result (for, e.g., a |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 288 | /// redeclaration). |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 289 | /// |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 290 | /// \param R the result to add (if it is unique). |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 291 | /// |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 292 | /// \param CurContext the context in which this result will be named. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 293 | void MaybeAddResult(Result R, DeclContext *CurContext = nullptr); |
| 294 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 295 | /// Add a new result to this result set, where we already know |
Yaron Keren | 8fbe4398 | 2014-11-14 18:33:42 +0000 | [diff] [blame] | 296 | /// the hiding declaration (if any). |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 297 | /// |
| 298 | /// \param R the result to add (if it is unique). |
| 299 | /// |
| 300 | /// \param CurContext the context in which this result will be named. |
| 301 | /// |
| 302 | /// \param Hiding the declaration that hides the result. |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 303 | /// |
| 304 | /// \param InBaseClass whether the result was found in a base |
| 305 | /// class of the searched context. |
| 306 | void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding, |
| 307 | bool InBaseClass); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 308 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 309 | /// Add a new non-declaration result to this result set. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 310 | void AddResult(Result R); |
| 311 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 312 | /// Enter into a new scope. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 313 | void EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 314 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 315 | /// Exit from the current scope. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 316 | void ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 317 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 318 | /// Ignore this declaration, if it is seen again. |
Dmitri Gribenko | 6cfb153 | 2013-02-14 13:53:30 +0000 | [diff] [blame] | 319 | void Ignore(const Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); } |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 320 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 321 | /// Add a visited context. |
Haojian Wu | 10d95c5 | 2018-01-17 14:29:25 +0000 | [diff] [blame] | 322 | void addVisitedContext(DeclContext *Ctx) { |
| 323 | CompletionContext.addVisitedContext(Ctx); |
| 324 | } |
| 325 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 326 | /// \name Name lookup predicates |
| 327 | /// |
| 328 | /// These predicates can be passed to the name lookup functions to filter the |
| 329 | /// results of name lookup. All of the predicates have the same type, so that |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 330 | /// |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 331 | //@{ |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 332 | bool IsOrdinaryName(const NamedDecl *ND) const; |
| 333 | bool IsOrdinaryNonTypeName(const NamedDecl *ND) const; |
| 334 | bool IsIntegralConstantValue(const NamedDecl *ND) const; |
| 335 | bool IsOrdinaryNonValueName(const NamedDecl *ND) const; |
| 336 | bool IsNestedNameSpecifier(const NamedDecl *ND) const; |
| 337 | bool IsEnum(const NamedDecl *ND) const; |
| 338 | bool IsClassOrStruct(const NamedDecl *ND) const; |
| 339 | bool IsUnion(const NamedDecl *ND) const; |
| 340 | bool IsNamespace(const NamedDecl *ND) const; |
| 341 | bool IsNamespaceOrAlias(const NamedDecl *ND) const; |
| 342 | bool IsType(const NamedDecl *ND) const; |
| 343 | bool IsMember(const NamedDecl *ND) const; |
| 344 | bool IsObjCIvar(const NamedDecl *ND) const; |
| 345 | bool IsObjCMessageReceiver(const NamedDecl *ND) const; |
| 346 | bool IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const; |
| 347 | bool IsObjCCollection(const NamedDecl *ND) const; |
| 348 | bool IsImpossibleToSatisfy(const NamedDecl *ND) const; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 349 | //@} |
| 350 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 351 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 352 | |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 353 | class ResultBuilder::ShadowMapEntry::iterator { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 354 | llvm::PointerUnion<const NamedDecl *, const DeclIndexPair *> DeclOrIterator; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 355 | unsigned SingleDeclIndex; |
| 356 | |
| 357 | public: |
| 358 | typedef DeclIndexPair value_type; |
| 359 | typedef value_type reference; |
| 360 | typedef std::ptrdiff_t difference_type; |
| 361 | typedef std::input_iterator_tag iterator_category; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 362 | |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 363 | class pointer { |
| 364 | DeclIndexPair Value; |
| 365 | |
| 366 | public: |
| 367 | pointer(const DeclIndexPair &Value) : Value(Value) { } |
| 368 | |
| 369 | const DeclIndexPair *operator->() const { |
| 370 | return &Value; |
| 371 | } |
| 372 | }; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 373 | |
| 374 | iterator() : DeclOrIterator((NamedDecl *)nullptr), SingleDeclIndex(0) {} |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 375 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 376 | iterator(const NamedDecl *SingleDecl, unsigned Index) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 377 | : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { } |
| 378 | |
| 379 | iterator(const DeclIndexPair *Iterator) |
| 380 | : DeclOrIterator(Iterator), SingleDeclIndex(0) { } |
| 381 | |
| 382 | iterator &operator++() { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 383 | if (DeclOrIterator.is<const NamedDecl *>()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 384 | DeclOrIterator = (NamedDecl *)nullptr; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 385 | SingleDeclIndex = 0; |
| 386 | return *this; |
| 387 | } |
| 388 | |
| 389 | const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>(); |
| 390 | ++I; |
| 391 | DeclOrIterator = I; |
| 392 | return *this; |
| 393 | } |
| 394 | |
Chris Lattner | 9795b39 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 395 | /*iterator operator++(int) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 396 | iterator tmp(*this); |
| 397 | ++(*this); |
| 398 | return tmp; |
Chris Lattner | 9795b39 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 399 | }*/ |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 400 | |
| 401 | reference operator*() const { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 402 | if (const NamedDecl *ND = DeclOrIterator.dyn_cast<const NamedDecl *>()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 403 | return reference(ND, SingleDeclIndex); |
| 404 | |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 405 | return *DeclOrIterator.get<const DeclIndexPair*>(); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | pointer operator->() const { |
| 409 | return pointer(**this); |
| 410 | } |
| 411 | |
| 412 | friend bool operator==(const iterator &X, const iterator &Y) { |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 413 | return X.DeclOrIterator.getOpaqueValue() |
| 414 | == Y.DeclOrIterator.getOpaqueValue() && |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 415 | X.SingleDeclIndex == Y.SingleDeclIndex; |
| 416 | } |
| 417 | |
| 418 | friend bool operator!=(const iterator &X, const iterator &Y) { |
Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 419 | return !(X == Y); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 420 | } |
| 421 | }; |
| 422 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 423 | ResultBuilder::ShadowMapEntry::iterator |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 424 | ResultBuilder::ShadowMapEntry::begin() const { |
| 425 | if (DeclOrVector.isNull()) |
| 426 | return iterator(); |
| 427 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 428 | if (const NamedDecl *ND = DeclOrVector.dyn_cast<const NamedDecl *>()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 429 | return iterator(ND, SingleDeclIndex); |
| 430 | |
| 431 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin()); |
| 432 | } |
| 433 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 434 | ResultBuilder::ShadowMapEntry::iterator |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 435 | ResultBuilder::ShadowMapEntry::end() const { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 436 | if (DeclOrVector.is<const NamedDecl *>() || DeclOrVector.isNull()) |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 437 | return iterator(); |
| 438 | |
| 439 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end()); |
| 440 | } |
| 441 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 442 | /// Compute the qualification required to get from the current context |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 443 | /// (\p CurContext) to the target context (\p TargetContext). |
| 444 | /// |
| 445 | /// \param Context the AST context in which the qualification will be used. |
| 446 | /// |
| 447 | /// \param CurContext the context where an entity is being named, which is |
| 448 | /// typically based on the current scope. |
| 449 | /// |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 450 | /// \param TargetContext the context in which the named entity actually |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 451 | /// resides. |
| 452 | /// |
| 453 | /// \returns a nested name specifier that refers into the target context, or |
| 454 | /// NULL if no qualification is needed. |
| 455 | static NestedNameSpecifier * |
| 456 | getRequiredQualification(ASTContext &Context, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 457 | const DeclContext *CurContext, |
| 458 | const DeclContext *TargetContext) { |
| 459 | SmallVector<const DeclContext *, 4> TargetParents; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 460 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 461 | for (const DeclContext *CommonAncestor = TargetContext; |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 462 | CommonAncestor && !CommonAncestor->Encloses(CurContext); |
| 463 | CommonAncestor = CommonAncestor->getLookupParent()) { |
| 464 | if (CommonAncestor->isTransparentContext() || |
| 465 | CommonAncestor->isFunctionOrMethod()) |
| 466 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 467 | |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 468 | TargetParents.push_back(CommonAncestor); |
| 469 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 470 | |
| 471 | NestedNameSpecifier *Result = nullptr; |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 472 | while (!TargetParents.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 473 | const DeclContext *Parent = TargetParents.pop_back_val(); |
| 474 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 475 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) { |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 476 | if (!Namespace->getIdentifier()) |
| 477 | continue; |
| 478 | |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 479 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 480 | } |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 481 | else if (const TagDecl *TD = dyn_cast<TagDecl>(Parent)) |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 482 | Result = NestedNameSpecifier::Create(Context, Result, |
| 483 | false, |
| 484 | Context.getTypeDeclType(TD).getTypePtr()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 485 | } |
Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 486 | return Result; |
| 487 | } |
| 488 | |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 489 | /// Determine whether \p Id is a name reserved for the implementation (C99 |
| 490 | /// 7.1.3, C++ [lib.global.names]). |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 491 | static bool isReservedName(const IdentifierInfo *Id, |
| 492 | bool doubleUnderscoreOnly = false) { |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 493 | if (Id->getLength() < 2) |
| 494 | return false; |
| 495 | const char *Name = Id->getNameStart(); |
| 496 | return Name[0] == '_' && |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 497 | (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z' && |
| 498 | !doubleUnderscoreOnly)); |
| 499 | } |
| 500 | |
| 501 | // Some declarations have reserved names that we don't want to ever show. |
| 502 | // Filter out names reserved for the implementation if they come from a |
| 503 | // system header. |
| 504 | static bool shouldIgnoreDueToReservedName(const NamedDecl *ND, Sema &SemaRef) { |
| 505 | const IdentifierInfo *Id = ND->getIdentifier(); |
| 506 | if (!Id) |
| 507 | return false; |
| 508 | |
| 509 | // Ignore reserved names for compiler provided decls. |
| 510 | if (isReservedName(Id) && ND->getLocation().isInvalid()) |
| 511 | return true; |
| 512 | |
| 513 | // For system headers ignore only double-underscore names. |
| 514 | // This allows for system headers providing private symbols with a single |
| 515 | // underscore. |
| 516 | if (isReservedName(Id, /*doubleUnderscoreOnly=*/true) && |
| 517 | SemaRef.SourceMgr.isInSystemHeader( |
| 518 | SemaRef.SourceMgr.getSpellingLoc(ND->getLocation()))) |
| 519 | return true; |
| 520 | |
| 521 | return false; |
Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 524 | bool ResultBuilder::isInterestingDecl(const NamedDecl *ND, |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 525 | bool &AsNestedNameSpecifier) const { |
| 526 | AsNestedNameSpecifier = false; |
| 527 | |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 528 | auto *Named = ND; |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 529 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 530 | |
| 531 | // Skip unnamed entities. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 532 | if (!ND->getDeclName()) |
| 533 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 534 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 535 | // Friend declarations and declarations introduced due to friends are never |
| 536 | // added as results. |
Richard Smith | 6eece29 | 2015-01-15 02:27:20 +0000 | [diff] [blame] | 537 | if (ND->getFriendObjectKind() == Decl::FOK_Undeclared) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 538 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 539 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 540 | // Class template (partial) specializations are never added as results. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 541 | if (isa<ClassTemplateSpecializationDecl>(ND) || |
| 542 | isa<ClassTemplatePartialSpecializationDecl>(ND)) |
| 543 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 544 | |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 545 | // Using declarations themselves are never added as results. |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 546 | if (isa<UsingDecl>(ND)) |
| 547 | return false; |
Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 548 | |
| 549 | if (shouldIgnoreDueToReservedName(ND, SemaRef)) |
| 550 | return false; |
Douglas Gregor | 2927c0c | 2010-11-09 03:59:40 +0000 | [diff] [blame] | 551 | |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 552 | if (Filter == &ResultBuilder::IsNestedNameSpecifier || |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 553 | (isa<NamespaceDecl>(ND) && |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 554 | Filter != &ResultBuilder::IsNamespace && |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 555 | Filter != &ResultBuilder::IsNamespaceOrAlias && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 556 | Filter != nullptr)) |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 557 | AsNestedNameSpecifier = true; |
| 558 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 559 | // Filter out any unwanted results. |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 560 | if (Filter && !(this->*Filter)(Named)) { |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 561 | // Check whether it is interesting as a nested-name-specifier. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 562 | if (AllowNestedNameSpecifiers && SemaRef.getLangOpts().CPlusPlus && |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 563 | IsNestedNameSpecifier(ND) && |
| 564 | (Filter != &ResultBuilder::IsMember || |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 565 | (isa<CXXRecordDecl>(ND) && |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 566 | cast<CXXRecordDecl>(ND)->isInjectedClassName()))) { |
| 567 | AsNestedNameSpecifier = true; |
| 568 | return true; |
| 569 | } |
| 570 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 571 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 572 | } |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 573 | // ... then it must be interesting! |
| 574 | return true; |
| 575 | } |
| 576 | |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 577 | bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 578 | const NamedDecl *Hiding) { |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 579 | // In C, there is no way to refer to a hidden name. |
| 580 | // FIXME: This isn't true; we can find a tag name hidden by an ordinary |
| 581 | // name if we introduce the tag type. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 582 | if (!SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 583 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 584 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 585 | const DeclContext *HiddenCtx = |
| 586 | R.Declaration->getDeclContext()->getRedeclContext(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 587 | |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 588 | // There is no way to qualify a name declared in a function or method. |
| 589 | if (HiddenCtx->isFunctionOrMethod()) |
| 590 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 591 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 592 | if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext()) |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 593 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 594 | |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 595 | // We can refer to the result with the appropriate qualification. Do it. |
| 596 | R.Hidden = true; |
| 597 | R.QualifierIsInformative = false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 598 | |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 599 | if (!R.Qualifier) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 600 | R.Qualifier = getRequiredQualification(SemaRef.Context, |
| 601 | CurContext, |
Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 602 | R.Declaration->getDeclContext()); |
| 603 | return false; |
| 604 | } |
| 605 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 606 | /// A simplified classification of types used to determine whether two |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 607 | /// types are "similar enough" when adjusting priorities. |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 608 | SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 609 | switch (T->getTypeClass()) { |
| 610 | case Type::Builtin: |
| 611 | switch (cast<BuiltinType>(T)->getKind()) { |
| 612 | case BuiltinType::Void: |
| 613 | return STC_Void; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 614 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 615 | case BuiltinType::NullPtr: |
| 616 | return STC_Pointer; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 617 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 618 | case BuiltinType::Overload: |
| 619 | case BuiltinType::Dependent: |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 620 | return STC_Other; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 621 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 622 | case BuiltinType::ObjCId: |
| 623 | case BuiltinType::ObjCClass: |
| 624 | case BuiltinType::ObjCSel: |
| 625 | return STC_ObjectiveC; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 626 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 627 | default: |
| 628 | return STC_Arithmetic; |
| 629 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 630 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 631 | case Type::Complex: |
| 632 | return STC_Arithmetic; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 633 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 634 | case Type::Pointer: |
| 635 | return STC_Pointer; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 636 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 637 | case Type::BlockPointer: |
| 638 | return STC_Block; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 639 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 640 | case Type::LValueReference: |
| 641 | case Type::RValueReference: |
| 642 | return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 643 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 644 | case Type::ConstantArray: |
| 645 | case Type::IncompleteArray: |
| 646 | case Type::VariableArray: |
| 647 | case Type::DependentSizedArray: |
| 648 | return STC_Array; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 649 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 650 | case Type::DependentSizedExtVector: |
| 651 | case Type::Vector: |
| 652 | case Type::ExtVector: |
| 653 | return STC_Arithmetic; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 654 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 655 | case Type::FunctionProto: |
| 656 | case Type::FunctionNoProto: |
| 657 | return STC_Function; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 658 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 659 | case Type::Record: |
| 660 | return STC_Record; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 661 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 662 | case Type::Enum: |
| 663 | return STC_Arithmetic; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 664 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 665 | case Type::ObjCObject: |
| 666 | case Type::ObjCInterface: |
| 667 | case Type::ObjCObjectPointer: |
| 668 | return STC_ObjectiveC; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 669 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 670 | default: |
| 671 | return STC_Other; |
| 672 | } |
| 673 | } |
| 674 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 675 | /// Get the type that a given expression will have if this declaration |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 676 | /// is used as an expression in its "typical" code-completion form. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 677 | QualType clang::getDeclUsageType(ASTContext &C, const NamedDecl *ND) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 678 | ND = ND->getUnderlyingDecl(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 679 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 680 | if (const TypeDecl *Type = dyn_cast<TypeDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 681 | return C.getTypeDeclType(Type); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 682 | if (const ObjCInterfaceDecl *Iface = dyn_cast<ObjCInterfaceDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 683 | return C.getObjCInterfaceType(Iface); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 684 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 685 | QualType T; |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 686 | if (const FunctionDecl *Function = ND->getAsFunction()) |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 687 | T = Function->getCallResultType(); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 688 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 689 | T = Method->getSendResultType(); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 690 | else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 691 | T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext())); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 692 | else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 693 | T = Property->getType(); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 694 | else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 695 | T = Value->getType(); |
| 696 | else |
| 697 | return QualType(); |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 698 | |
| 699 | // Dig through references, function pointers, and block pointers to |
| 700 | // get down to the likely type of an expression when the entity is |
| 701 | // used. |
| 702 | do { |
| 703 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) { |
| 704 | T = Ref->getPointeeType(); |
| 705 | continue; |
| 706 | } |
| 707 | |
| 708 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 709 | if (Pointer->getPointeeType()->isFunctionType()) { |
| 710 | T = Pointer->getPointeeType(); |
| 711 | continue; |
| 712 | } |
| 713 | |
| 714 | break; |
| 715 | } |
| 716 | |
| 717 | if (const BlockPointerType *Block = T->getAs<BlockPointerType>()) { |
| 718 | T = Block->getPointeeType(); |
| 719 | continue; |
| 720 | } |
| 721 | |
| 722 | if (const FunctionType *Function = T->getAs<FunctionType>()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 723 | T = Function->getReturnType(); |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 724 | continue; |
| 725 | } |
| 726 | |
| 727 | break; |
| 728 | } while (true); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 729 | |
Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 730 | return T; |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 733 | unsigned ResultBuilder::getBasePriority(const NamedDecl *ND) { |
| 734 | if (!ND) |
| 735 | return CCP_Unlikely; |
| 736 | |
| 737 | // Context-based decisions. |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 738 | const DeclContext *LexicalDC = ND->getLexicalDeclContext(); |
| 739 | if (LexicalDC->isFunctionOrMethod()) { |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 740 | // _cmd is relatively rare |
| 741 | if (const ImplicitParamDecl *ImplicitParam = |
| 742 | dyn_cast<ImplicitParamDecl>(ND)) |
| 743 | if (ImplicitParam->getIdentifier() && |
| 744 | ImplicitParam->getIdentifier()->isStr("_cmd")) |
| 745 | return CCP_ObjC_cmd; |
| 746 | |
| 747 | return CCP_LocalDeclaration; |
| 748 | } |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 749 | |
| 750 | const DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
Ilya Biryukov | 4e7a6fe | 2017-09-22 19:07:37 +0000 | [diff] [blame] | 751 | if (DC->isRecord() || isa<ObjCContainerDecl>(DC)) { |
| 752 | // Explicit destructor calls are very rare. |
| 753 | if (isa<CXXDestructorDecl>(ND)) |
| 754 | return CCP_Unlikely; |
| 755 | // Explicit operator and conversion function calls are also very rare. |
| 756 | auto DeclNameKind = ND->getDeclName().getNameKind(); |
| 757 | if (DeclNameKind == DeclarationName::CXXOperatorName || |
| 758 | DeclNameKind == DeclarationName::CXXLiteralOperatorName || |
| 759 | DeclNameKind == DeclarationName::CXXConversionFunctionName) |
| 760 | return CCP_Unlikely; |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 761 | return CCP_MemberDeclaration; |
Ilya Biryukov | 4e7a6fe | 2017-09-22 19:07:37 +0000 | [diff] [blame] | 762 | } |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 763 | |
| 764 | // Content-based decisions. |
| 765 | if (isa<EnumConstantDecl>(ND)) |
| 766 | return CCP_Constant; |
| 767 | |
Douglas Gregor | 52e0de4 | 2013-01-31 05:03:46 +0000 | [diff] [blame] | 768 | // Use CCP_Type for type declarations unless we're in a statement, Objective-C |
| 769 | // message receiver, or parenthesized expression context. There, it's as |
| 770 | // likely that the user will want to write a type as other declarations. |
| 771 | if ((isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) && |
| 772 | !(CompletionContext.getKind() == CodeCompletionContext::CCC_Statement || |
| 773 | CompletionContext.getKind() |
| 774 | == CodeCompletionContext::CCC_ObjCMessageReceiver || |
| 775 | CompletionContext.getKind() |
| 776 | == CodeCompletionContext::CCC_ParenthesizedExpression)) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 777 | return CCP_Type; |
| 778 | |
| 779 | return CCP_Declaration; |
| 780 | } |
| 781 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 782 | void ResultBuilder::AdjustResultPriorityForDecl(Result &R) { |
| 783 | // If this is an Objective-C method declaration whose selector matches our |
| 784 | // preferred selector, give it a priority boost. |
| 785 | if (!PreferredSelector.isNull()) |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 786 | if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration)) |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 787 | if (PreferredSelector == Method->getSelector()) |
| 788 | R.Priority += CCD_SelectorMatch; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 789 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 790 | // If we have a preferred type, adjust the priority for results with exactly- |
| 791 | // matching or nearly-matching types. |
| 792 | if (!PreferredType.isNull()) { |
| 793 | QualType T = getDeclUsageType(SemaRef.Context, R.Declaration); |
| 794 | if (!T.isNull()) { |
| 795 | CanQualType TC = SemaRef.Context.getCanonicalType(T); |
| 796 | // Check for exactly-matching types (modulo qualifiers). |
| 797 | if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) |
| 798 | R.Priority /= CCF_ExactTypeMatch; |
| 799 | // Check for nearly-matching types, based on classification of each. |
| 800 | else if ((getSimplifiedTypeClass(PreferredType) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 801 | == getSimplifiedTypeClass(TC)) && |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 802 | !(PreferredType->isEnumeralType() && TC->isEnumeralType())) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 803 | R.Priority /= CCF_SimilarTypeMatch; |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 804 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 805 | } |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 808 | void ResultBuilder::MaybeAddConstructorResults(Result R) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 809 | if (!SemaRef.getLangOpts().CPlusPlus || !R.Declaration || |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 810 | !CompletionContext.wantConstructorResults()) |
| 811 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 812 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 813 | ASTContext &Context = SemaRef.Context; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 814 | const NamedDecl *D = R.Declaration; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 815 | const CXXRecordDecl *Record = nullptr; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 816 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 817 | Record = ClassTemplate->getTemplatedDecl(); |
| 818 | else if ((Record = dyn_cast<CXXRecordDecl>(D))) { |
| 819 | // Skip specializations and partial specializations. |
| 820 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 821 | return; |
| 822 | } else { |
| 823 | // There are no constructors here. |
| 824 | return; |
| 825 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 826 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 827 | Record = Record->getDefinition(); |
| 828 | if (!Record) |
| 829 | return; |
| 830 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 831 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 832 | QualType RecordTy = Context.getTypeDeclType(Record); |
| 833 | DeclarationName ConstructorName |
| 834 | = Context.DeclarationNames.getCXXConstructorName( |
| 835 | Context.getCanonicalType(RecordTy)); |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 836 | DeclContext::lookup_result Ctors = Record->lookup(ConstructorName); |
| 837 | for (DeclContext::lookup_iterator I = Ctors.begin(), |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 838 | E = Ctors.end(); |
| 839 | I != E; ++I) { |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 840 | R.Declaration = *I; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 841 | R.CursorKind = getCursorKindForDecl(R.Declaration); |
| 842 | Results.push_back(R); |
| 843 | } |
| 844 | } |
| 845 | |
Sam McCall | 63c5972 | 2018-01-22 20:44:47 +0000 | [diff] [blame] | 846 | static bool isConstructor(const Decl *ND) { |
| 847 | if (const auto *Tmpl = dyn_cast<FunctionTemplateDecl>(ND)) |
| 848 | ND = Tmpl->getTemplatedDecl(); |
| 849 | return isa<CXXConstructorDecl>(ND); |
| 850 | } |
| 851 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 852 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { |
| 853 | assert(!ShadowMaps.empty() && "Must enter into a results scope"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 854 | |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 855 | if (R.Kind != Result::RK_Declaration) { |
| 856 | // For non-declaration results, just add the result. |
| 857 | Results.push_back(R); |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | // Look through using declarations. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 862 | if (const UsingShadowDecl *Using = |
| 863 | dyn_cast<UsingShadowDecl>(R.Declaration)) { |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 864 | MaybeAddResult(Result(Using->getTargetDecl(), |
| 865 | getBasePriority(Using->getTargetDecl()), |
| 866 | R.Qualifier), |
| 867 | CurContext); |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 868 | return; |
| 869 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 870 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 871 | const Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 872 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 873 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 874 | bool AsNestedNameSpecifier = false; |
| 875 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 876 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 877 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 878 | // C++ constructors are never found by name lookup. |
Sam McCall | 63c5972 | 2018-01-22 20:44:47 +0000 | [diff] [blame] | 879 | if (isConstructor(R.Declaration)) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 880 | return; |
| 881 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 882 | ShadowMap &SMap = ShadowMaps.back(); |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 883 | ShadowMapEntry::iterator I, IEnd; |
| 884 | ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName()); |
| 885 | if (NamePos != SMap.end()) { |
| 886 | I = NamePos->second.begin(); |
| 887 | IEnd = NamePos->second.end(); |
| 888 | } |
| 889 | |
| 890 | for (; I != IEnd; ++I) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 891 | const NamedDecl *ND = I->first; |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 892 | unsigned Index = I->second; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 893 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 894 | // This is a redeclaration. Always pick the newer declaration. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 895 | Results[Index].Declaration = R.Declaration; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 896 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 897 | // We're done. |
| 898 | return; |
| 899 | } |
| 900 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 901 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 902 | // This is a new declaration in this scope. However, check whether this |
| 903 | // declaration name is hidden by a similarly-named declaration in an outer |
| 904 | // scope. |
| 905 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 906 | --SMEnd; |
| 907 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 908 | ShadowMapEntry::iterator I, IEnd; |
| 909 | ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName()); |
| 910 | if (NamePos != SM->end()) { |
| 911 | I = NamePos->second.begin(); |
| 912 | IEnd = NamePos->second.end(); |
| 913 | } |
| 914 | for (; I != IEnd; ++I) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 915 | // A tag declaration does not hide a non-tag declaration. |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 916 | if (I->first->hasTagIdentifierNamespace() && |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 917 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 918 | Decl::IDNS_LocalExtern | Decl::IDNS_ObjCProtocol))) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 919 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 920 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 921 | // Protocols are in distinct namespaces from everything else. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 922 | if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 923 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 924 | I->first->getIdentifierNamespace() != IDNS) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 925 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 926 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 927 | // 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] | 928 | if (CheckHiddenResult(R, CurContext, I->first)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 929 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 930 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 931 | break; |
| 932 | } |
| 933 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 934 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 935 | // Make sure that any given declaration only shows up in the result set once. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 936 | if (!AllDeclsFound.insert(CanonDecl).second) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 937 | return; |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 938 | |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 939 | // If the filter is for nested-name-specifiers, then this result starts a |
| 940 | // nested-name-specifier. |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 941 | if (AsNestedNameSpecifier) { |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 942 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 943 | R.Priority = CCP_NestedNameSpecifier; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 944 | } else |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 945 | AdjustResultPriorityForDecl(R); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 946 | |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 947 | // If this result is supposed to have an informative qualifier, add one. |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 948 | if (R.QualifierIsInformative && !R.Qualifier && |
| 949 | !R.StartsNestedNameSpecifier) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 950 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 951 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 952 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 953 | Namespace); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 954 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 955 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 956 | false, SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 957 | else |
| 958 | R.QualifierIsInformative = false; |
| 959 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 960 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 961 | // Insert this result into the set of results and into the current shadow |
| 962 | // map. |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 963 | SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 964 | Results.push_back(R); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 965 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 966 | if (!AsNestedNameSpecifier) |
| 967 | MaybeAddConstructorResults(R); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 970 | void ResultBuilder::AddResult(Result R, DeclContext *CurContext, |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 971 | NamedDecl *Hiding, bool InBaseClass = false) { |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 972 | if (R.Kind != Result::RK_Declaration) { |
| 973 | // For non-declaration results, just add the result. |
| 974 | Results.push_back(R); |
| 975 | return; |
| 976 | } |
| 977 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 978 | // Look through using declarations. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 979 | if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 980 | AddResult(Result(Using->getTargetDecl(), |
| 981 | getBasePriority(Using->getTargetDecl()), |
| 982 | R.Qualifier), |
| 983 | CurContext, Hiding); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 984 | return; |
| 985 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 986 | |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 987 | bool AsNestedNameSpecifier = false; |
| 988 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 989 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 990 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 991 | // C++ constructors are never found by name lookup. |
Sam McCall | 63c5972 | 2018-01-22 20:44:47 +0000 | [diff] [blame] | 992 | if (isConstructor(R.Declaration)) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 993 | return; |
| 994 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 995 | if (Hiding && CheckHiddenResult(R, CurContext, Hiding)) |
| 996 | return; |
Nick Lewycky | c392148 | 2012-04-03 21:44:08 +0000 | [diff] [blame] | 997 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 998 | // Make sure that any given declaration only shows up in the result set once. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 999 | if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()).second) |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1000 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1001 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1002 | // If the filter is for nested-name-specifiers, then this result starts a |
| 1003 | // nested-name-specifier. |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1004 | if (AsNestedNameSpecifier) { |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1005 | R.StartsNestedNameSpecifier = true; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1006 | R.Priority = CCP_NestedNameSpecifier; |
| 1007 | } |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 1008 | else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass && |
| 1009 | isa<CXXRecordDecl>(R.Declaration->getDeclContext() |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1010 | ->getRedeclContext())) |
Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 1011 | R.QualifierIsInformative = true; |
| 1012 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1013 | // If this result is supposed to have an informative qualifier, add one. |
| 1014 | if (R.QualifierIsInformative && !R.Qualifier && |
| 1015 | !R.StartsNestedNameSpecifier) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1016 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 1017 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1018 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 1019 | Namespace); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1020 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1021 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, false, |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 1022 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1023 | else |
| 1024 | R.QualifierIsInformative = false; |
| 1025 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1026 | |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1027 | // Adjust the priority if this result comes from a base class. |
| 1028 | if (InBaseClass) |
| 1029 | R.Priority += CCD_InBaseClass; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1030 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 1031 | AdjustResultPriorityForDecl(R); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1032 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1033 | if (HasObjectTypeQualifiers) |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1034 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(R.Declaration)) |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1035 | if (Method->isInstance()) { |
| 1036 | Qualifiers MethodQuals |
| 1037 | = Qualifiers::fromCVRMask(Method->getTypeQualifiers()); |
| 1038 | if (ObjectTypeQualifiers == MethodQuals) |
| 1039 | R.Priority += CCD_ObjectQualifierMatch; |
| 1040 | else if (ObjectTypeQualifiers - MethodQuals) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1041 | // The method cannot be invoked, because doing so would drop |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1042 | // qualifiers. |
| 1043 | return; |
| 1044 | } |
| 1045 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1046 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1047 | // Insert this result into the set of results. |
| 1048 | Results.push_back(R); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1049 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1050 | if (!AsNestedNameSpecifier) |
| 1051 | MaybeAddConstructorResults(R); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1054 | void ResultBuilder::AddResult(Result R) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1055 | assert(R.Kind != Result::RK_Declaration && |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1056 | "Declaration results need more context"); |
| 1057 | Results.push_back(R); |
| 1058 | } |
| 1059 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1060 | /// Enter into a new scope. |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1061 | void ResultBuilder::EnterNewScope() { ShadowMaps.emplace_back(); } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1062 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1063 | /// Exit from the current scope. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1064 | void ResultBuilder::ExitScope() { |
Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1065 | for (ShadowMap::iterator E = ShadowMaps.back().begin(), |
| 1066 | EEnd = ShadowMaps.back().end(); |
| 1067 | E != EEnd; |
| 1068 | ++E) |
| 1069 | E->second.Destroy(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1070 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1071 | ShadowMaps.pop_back(); |
| 1072 | } |
| 1073 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1074 | /// Determines whether this given declaration will be found by |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1075 | /// ordinary name lookup. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1076 | bool ResultBuilder::IsOrdinaryName(const NamedDecl *ND) const { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1077 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1078 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1079 | // If name lookup finds a local extern declaration, then we are in a |
| 1080 | // context where it behaves like an ordinary name. |
| 1081 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1082 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 9858ed5 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1083 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1084 | else if (SemaRef.getLangOpts().ObjC1) { |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1085 | if (isa<ObjCIvarDecl>(ND)) |
| 1086 | return true; |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1087 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1088 | |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1089 | return ND->getIdentifierNamespace() & IDNS; |
| 1090 | } |
| 1091 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1092 | /// Determines whether this given declaration will be found by |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1093 | /// ordinary name lookup but is not a type name. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1094 | bool ResultBuilder::IsOrdinaryNonTypeName(const NamedDecl *ND) const { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1095 | ND = ND->getUnderlyingDecl(); |
Alex Lorenz | f3df1f7 | 2017-11-14 01:46:24 +0000 | [diff] [blame] | 1096 | if (isa<TypeDecl>(ND)) |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1097 | return false; |
Alex Lorenz | f3df1f7 | 2017-11-14 01:46:24 +0000 | [diff] [blame] | 1098 | // Objective-C interfaces names are not filtered by this method because they |
| 1099 | // can be used in a class property expression. We can still filter out |
| 1100 | // @class declarations though. |
| 1101 | if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND)) { |
| 1102 | if (!ID->getDefinition()) |
| 1103 | return false; |
| 1104 | } |
| 1105 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1106 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1107 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | 9858ed5 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1108 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1109 | else if (SemaRef.getLangOpts().ObjC1) { |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1110 | if (isa<ObjCIvarDecl>(ND)) |
| 1111 | return true; |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1112 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1113 | |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1114 | return ND->getIdentifierNamespace() & IDNS; |
| 1115 | } |
| 1116 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1117 | bool ResultBuilder::IsIntegralConstantValue(const NamedDecl *ND) const { |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1118 | if (!IsOrdinaryNonTypeName(ND)) |
| 1119 | return 0; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1120 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1121 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl())) |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1122 | if (VD->getType()->isIntegralOrEnumerationType()) |
| 1123 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1124 | |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1125 | return false; |
| 1126 | } |
| 1127 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1128 | /// Determines whether this given declaration will be found by |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1129 | /// ordinary name lookup. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1130 | bool ResultBuilder::IsOrdinaryNonValueName(const NamedDecl *ND) const { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1131 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1132 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1133 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1134 | if (SemaRef.getLangOpts().CPlusPlus) |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1135 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1136 | |
| 1137 | return (ND->getIdentifierNamespace() & IDNS) && |
| 1138 | !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) && |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1139 | !isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1142 | /// Determines whether the given declaration is suitable as the |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1143 | /// start of a C++ nested-name-specifier, e.g., a class or namespace. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1144 | bool ResultBuilder::IsNestedNameSpecifier(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1145 | // Allow us to find class templates, too. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1146 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1147 | ND = ClassTemplate->getTemplatedDecl(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1148 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1149 | return SemaRef.isAcceptableNestedNameSpecifier(ND); |
| 1150 | } |
| 1151 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1152 | /// Determines whether the given declaration is an enumeration. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1153 | bool ResultBuilder::IsEnum(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1154 | return isa<EnumDecl>(ND); |
| 1155 | } |
| 1156 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1157 | /// Determines whether the given declaration is a class or struct. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1158 | bool ResultBuilder::IsClassOrStruct(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1159 | // Allow us to find class templates, too. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1160 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1161 | ND = ClassTemplate->getTemplatedDecl(); |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1162 | |
| 1163 | // For purposes of this check, interfaces match too. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1164 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1165 | return RD->getTagKind() == TTK_Class || |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1166 | RD->getTagKind() == TTK_Struct || |
| 1167 | RD->getTagKind() == TTK_Interface; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1168 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1169 | return false; |
| 1170 | } |
| 1171 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1172 | /// Determines whether the given declaration is a union. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1173 | bool ResultBuilder::IsUnion(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1174 | // Allow us to find class templates, too. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1175 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1176 | ND = ClassTemplate->getTemplatedDecl(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1177 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1178 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1179 | return RD->getTagKind() == TTK_Union; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1180 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1181 | return false; |
| 1182 | } |
| 1183 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1184 | /// Determines whether the given declaration is a namespace. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1185 | bool ResultBuilder::IsNamespace(const NamedDecl *ND) const { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1186 | return isa<NamespaceDecl>(ND); |
| 1187 | } |
| 1188 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1189 | /// Determines whether the given declaration is a namespace or |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1190 | /// namespace alias. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1191 | bool ResultBuilder::IsNamespaceOrAlias(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1192 | return isa<NamespaceDecl>(ND->getUnderlyingDecl()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1195 | /// Determines whether the given declaration is a type. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1196 | bool ResultBuilder::IsType(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1197 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1198 | return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1201 | /// Determines which members of a class should be visible via |
Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1202 | /// "." or "->". Only value declarations, nested name specifiers, and |
| 1203 | /// using declarations thereof should show up. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1204 | bool ResultBuilder::IsMember(const NamedDecl *ND) const { |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1205 | ND = ND->getUnderlyingDecl(); |
Douglas Gregor | 7078839 | 2009-12-11 18:14:22 +0000 | [diff] [blame] | 1206 | return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) || |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1207 | isa<ObjCPropertyDecl>(ND); |
Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1210 | static bool isObjCReceiverType(ASTContext &C, QualType T) { |
| 1211 | T = C.getCanonicalType(T); |
| 1212 | switch (T->getTypeClass()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1213 | case Type::ObjCObject: |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1214 | case Type::ObjCInterface: |
| 1215 | case Type::ObjCObjectPointer: |
| 1216 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1217 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1218 | case Type::Builtin: |
| 1219 | switch (cast<BuiltinType>(T)->getKind()) { |
| 1220 | case BuiltinType::ObjCId: |
| 1221 | case BuiltinType::ObjCClass: |
| 1222 | case BuiltinType::ObjCSel: |
| 1223 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1224 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1225 | default: |
| 1226 | break; |
| 1227 | } |
| 1228 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1229 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1230 | default: |
| 1231 | break; |
| 1232 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1233 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1234 | if (!C.getLangOpts().CPlusPlus) |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1235 | return false; |
| 1236 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1237 | // FIXME: We could perform more analysis here to determine whether a |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1238 | // particular class type has any conversions to Objective-C types. For now, |
| 1239 | // just accept all class types. |
| 1240 | return T->isDependentType() || T->isRecordType(); |
| 1241 | } |
| 1242 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1243 | bool ResultBuilder::IsObjCMessageReceiver(const NamedDecl *ND) const { |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1244 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1245 | if (T.isNull()) |
| 1246 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1247 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1248 | T = SemaRef.Context.getBaseElementType(T); |
| 1249 | return isObjCReceiverType(SemaRef.Context, T); |
| 1250 | } |
| 1251 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1252 | bool ResultBuilder::IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const { |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1253 | if (IsObjCMessageReceiver(ND)) |
| 1254 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1255 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1256 | const VarDecl *Var = dyn_cast<VarDecl>(ND); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1257 | if (!Var) |
| 1258 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1259 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1260 | return Var->hasLocalStorage() && !Var->hasAttr<BlocksAttr>(); |
| 1261 | } |
| 1262 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1263 | bool ResultBuilder::IsObjCCollection(const NamedDecl *ND) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1264 | if ((SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryName(ND)) || |
| 1265 | (!SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryNonTypeName(ND))) |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1266 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1267 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1268 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1269 | if (T.isNull()) |
| 1270 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1271 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1272 | T = SemaRef.Context.getBaseElementType(T); |
| 1273 | return T->isObjCObjectType() || T->isObjCObjectPointerType() || |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1274 | T->isObjCIdType() || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1275 | (SemaRef.getLangOpts().CPlusPlus && T->isRecordType()); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1276 | } |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1277 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1278 | bool ResultBuilder::IsImpossibleToSatisfy(const NamedDecl *ND) const { |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1279 | return false; |
| 1280 | } |
| 1281 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1282 | /// Determines whether the given declaration is an Objective-C |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1283 | /// instance variable. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1284 | bool ResultBuilder::IsObjCIvar(const NamedDecl *ND) const { |
Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1285 | return isa<ObjCIvarDecl>(ND); |
| 1286 | } |
| 1287 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1288 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1289 | /// Visible declaration consumer that adds a code-completion result |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1290 | /// for each visible declaration. |
| 1291 | class CodeCompletionDeclConsumer : public VisibleDeclConsumer { |
| 1292 | ResultBuilder &Results; |
| 1293 | DeclContext *CurContext; |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 1294 | std::vector<FixItHint> FixIts; |
Haojian Wu | 10d95c5 | 2018-01-17 14:29:25 +0000 | [diff] [blame] | 1295 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1296 | public: |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 1297 | CodeCompletionDeclConsumer( |
| 1298 | ResultBuilder &Results, DeclContext *CurContext, |
| 1299 | std::vector<FixItHint> FixIts = std::vector<FixItHint>()) |
| 1300 | : Results(Results), CurContext(CurContext), FixIts(std::move(FixIts)) {} |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1301 | |
| 1302 | void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx, |
| 1303 | bool InBaseClass) override { |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1304 | bool Accessible = true; |
Douglas Gregor | 03ba188 | 2011-11-03 16:51:37 +0000 | [diff] [blame] | 1305 | if (Ctx) |
| 1306 | Accessible = Results.getSema().IsSimplyAccessible(ND, Ctx); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1307 | |
| 1308 | ResultBuilder::Result Result(ND, Results.getBasePriority(ND), nullptr, |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 1309 | false, Accessible, FixIts); |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1310 | Results.AddResult(Result, CurContext, Hiding, InBaseClass); |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1311 | } |
Haojian Wu | 10d95c5 | 2018-01-17 14:29:25 +0000 | [diff] [blame] | 1312 | |
| 1313 | void EnteredContext(DeclContext* Ctx) override { |
| 1314 | Results.addVisitedContext(Ctx); |
| 1315 | } |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1316 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1317 | } |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1318 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1319 | /// Add type specifiers for the current language as keyword results. |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1320 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1321 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1322 | typedef CodeCompletionResult Result; |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1323 | Results.AddResult(Result("short", CCP_Type)); |
| 1324 | Results.AddResult(Result("long", CCP_Type)); |
| 1325 | Results.AddResult(Result("signed", CCP_Type)); |
| 1326 | Results.AddResult(Result("unsigned", CCP_Type)); |
| 1327 | Results.AddResult(Result("void", CCP_Type)); |
| 1328 | Results.AddResult(Result("char", CCP_Type)); |
| 1329 | Results.AddResult(Result("int", CCP_Type)); |
| 1330 | Results.AddResult(Result("float", CCP_Type)); |
| 1331 | Results.AddResult(Result("double", CCP_Type)); |
| 1332 | Results.AddResult(Result("enum", CCP_Type)); |
| 1333 | Results.AddResult(Result("struct", CCP_Type)); |
| 1334 | Results.AddResult(Result("union", CCP_Type)); |
| 1335 | Results.AddResult(Result("const", CCP_Type)); |
| 1336 | Results.AddResult(Result("volatile", CCP_Type)); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1337 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1338 | if (LangOpts.C99) { |
| 1339 | // C99-specific |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1340 | Results.AddResult(Result("_Complex", CCP_Type)); |
| 1341 | Results.AddResult(Result("_Imaginary", CCP_Type)); |
| 1342 | Results.AddResult(Result("_Bool", CCP_Type)); |
| 1343 | Results.AddResult(Result("restrict", CCP_Type)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1344 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1345 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1346 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 1347 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1348 | if (LangOpts.CPlusPlus) { |
| 1349 | // C++-specific |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1350 | Results.AddResult(Result("bool", CCP_Type + |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 1351 | (LangOpts.ObjC1? CCD_bool_in_ObjC : 0))); |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1352 | Results.AddResult(Result("class", CCP_Type)); |
| 1353 | Results.AddResult(Result("wchar_t", CCP_Type)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1354 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1355 | // typename qualified-id |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1356 | Builder.AddTypedTextChunk("typename"); |
| 1357 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1358 | Builder.AddPlaceholderChunk("qualifier"); |
| 1359 | Builder.AddTextChunk("::"); |
| 1360 | Builder.AddPlaceholderChunk("name"); |
| 1361 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1362 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1363 | if (LangOpts.CPlusPlus11) { |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1364 | Results.AddResult(Result("auto", CCP_Type)); |
| 1365 | Results.AddResult(Result("char16_t", CCP_Type)); |
| 1366 | Results.AddResult(Result("char32_t", CCP_Type)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1367 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1368 | Builder.AddTypedTextChunk("decltype"); |
| 1369 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1370 | Builder.AddPlaceholderChunk("expression"); |
| 1371 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1372 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1373 | } |
Alex Lorenz | 46eed9d | 2017-02-13 23:35:59 +0000 | [diff] [blame] | 1374 | } else |
| 1375 | Results.AddResult(Result("__auto_type", CCP_Type)); |
| 1376 | |
Richard Smith | 7b301e2 | 2018-05-24 21:51:52 +0000 | [diff] [blame] | 1377 | // GNU keywords |
| 1378 | if (LangOpts.GNUKeywords) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1379 | // FIXME: Enable when we actually support decimal floating point. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1380 | // Results.AddResult(Result("_Decimal32")); |
| 1381 | // Results.AddResult(Result("_Decimal64")); |
| 1382 | // Results.AddResult(Result("_Decimal128")); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1383 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1384 | Builder.AddTypedTextChunk("typeof"); |
| 1385 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1386 | Builder.AddPlaceholderChunk("expression"); |
| 1387 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1388 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1389 | Builder.AddTypedTextChunk("typeof"); |
| 1390 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1391 | Builder.AddPlaceholderChunk("type"); |
| 1392 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1393 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1394 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 1395 | |
| 1396 | // Nullability |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 1397 | Results.AddResult(Result("_Nonnull", CCP_Type)); |
| 1398 | Results.AddResult(Result("_Null_unspecified", CCP_Type)); |
| 1399 | Results.AddResult(Result("_Nullable", CCP_Type)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1402 | static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1403 | const LangOptions &LangOpts, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1404 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1405 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1406 | // Note: we don't suggest either "auto" or "register", because both |
| 1407 | // are pointless as storage specifiers. Elsewhere, we suggest "auto" |
| 1408 | // in C++0x as a type specifier. |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1409 | Results.AddResult(Result("extern")); |
| 1410 | Results.AddResult(Result("static")); |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1411 | |
| 1412 | if (LangOpts.CPlusPlus11) { |
| 1413 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
| 1414 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
| 1415 | |
| 1416 | // alignas |
| 1417 | Builder.AddTypedTextChunk("alignas"); |
| 1418 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1419 | Builder.AddPlaceholderChunk("expression"); |
| 1420 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1421 | Results.AddResult(Result(Builder.TakeString())); |
| 1422 | |
| 1423 | Results.AddResult(Result("constexpr")); |
| 1424 | Results.AddResult(Result("thread_local")); |
| 1425 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1428 | static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1429 | const LangOptions &LangOpts, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1430 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1431 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1432 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1433 | case Sema::PCC_Class: |
| 1434 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1435 | if (LangOpts.CPlusPlus) { |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1436 | Results.AddResult(Result("explicit")); |
| 1437 | Results.AddResult(Result("friend")); |
| 1438 | Results.AddResult(Result("mutable")); |
| 1439 | Results.AddResult(Result("virtual")); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1440 | } |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 1441 | LLVM_FALLTHROUGH; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1442 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1443 | case Sema::PCC_ObjCInterface: |
| 1444 | case Sema::PCC_ObjCImplementation: |
| 1445 | case Sema::PCC_Namespace: |
| 1446 | case Sema::PCC_Template: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1447 | if (LangOpts.CPlusPlus || LangOpts.C99) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1448 | Results.AddResult(Result("inline")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1449 | break; |
| 1450 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1451 | case Sema::PCC_ObjCInstanceVariableList: |
| 1452 | case Sema::PCC_Expression: |
| 1453 | case Sema::PCC_Statement: |
| 1454 | case Sema::PCC_ForInit: |
| 1455 | case Sema::PCC_Condition: |
| 1456 | case Sema::PCC_RecoveryInFunction: |
| 1457 | case Sema::PCC_Type: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1458 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1459 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1460 | break; |
| 1461 | } |
| 1462 | } |
| 1463 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1464 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); |
| 1465 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); |
| 1466 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1467 | ResultBuilder &Results, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1468 | bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1469 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1470 | ResultBuilder &Results, |
| 1471 | bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1472 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1473 | ResultBuilder &Results, |
| 1474 | bool NeedAt); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1475 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1476 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1477 | static void AddTypedefResult(ResultBuilder &Results) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1478 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 1479 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1480 | Builder.AddTypedTextChunk("typedef"); |
| 1481 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1482 | Builder.AddPlaceholderChunk("type"); |
| 1483 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1484 | Builder.AddPlaceholderChunk("name"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1485 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1488 | static bool WantTypesInContext(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1489 | const LangOptions &LangOpts) { |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1490 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1491 | case Sema::PCC_Namespace: |
| 1492 | case Sema::PCC_Class: |
| 1493 | case Sema::PCC_ObjCInstanceVariableList: |
| 1494 | case Sema::PCC_Template: |
| 1495 | case Sema::PCC_MemberTemplate: |
| 1496 | case Sema::PCC_Statement: |
| 1497 | case Sema::PCC_RecoveryInFunction: |
| 1498 | case Sema::PCC_Type: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1499 | case Sema::PCC_ParenthesizedExpression: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1500 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1501 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1502 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1503 | case Sema::PCC_Expression: |
| 1504 | case Sema::PCC_Condition: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1505 | return LangOpts.CPlusPlus; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1506 | |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1507 | case Sema::PCC_ObjCInterface: |
| 1508 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1509 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1510 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1511 | case Sema::PCC_ForInit: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1512 | return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99; |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1513 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1514 | |
| 1515 | llvm_unreachable("Invalid ParserCompletionContext!"); |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 1518 | static PrintingPolicy getCompletionPrintingPolicy(const ASTContext &Context, |
| 1519 | const Preprocessor &PP) { |
| 1520 | PrintingPolicy Policy = Sema::getPrintingPolicy(Context, PP); |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1521 | Policy.AnonymousTagLocations = false; |
| 1522 | Policy.SuppressStrongLifetime = true; |
Douglas Gregor | 2e10cf9 | 2011-11-03 00:16:13 +0000 | [diff] [blame] | 1523 | Policy.SuppressUnwrittenScope = true; |
Ilya Biryukov | b5da91c | 2017-11-08 10:39:09 +0000 | [diff] [blame] | 1524 | Policy.SuppressScope = true; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1525 | return Policy; |
| 1526 | } |
| 1527 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1528 | /// Retrieve a printing policy suitable for code completion. |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 1529 | static PrintingPolicy getCompletionPrintingPolicy(Sema &S) { |
| 1530 | return getCompletionPrintingPolicy(S.Context, S.PP); |
| 1531 | } |
| 1532 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1533 | /// Retrieve the string representation of the given type as a string |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1534 | /// that has the appropriate lifetime for code completion. |
| 1535 | /// |
| 1536 | /// This routine provides a fast path where we provide constant strings for |
| 1537 | /// common type names. |
| 1538 | static const char *GetCompletionTypeString(QualType T, |
| 1539 | ASTContext &Context, |
| 1540 | const PrintingPolicy &Policy, |
| 1541 | CodeCompletionAllocator &Allocator) { |
| 1542 | if (!T.getLocalQualifiers()) { |
| 1543 | // Built-in type names are constant strings. |
| 1544 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) |
Argyrios Kyrtzidis | bbff3da | 2012-05-05 04:20:28 +0000 | [diff] [blame] | 1545 | return BT->getNameAsCString(Policy); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1546 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1547 | // Anonymous tag types are constant strings. |
| 1548 | if (const TagType *TagT = dyn_cast<TagType>(T)) |
| 1549 | if (TagDecl *Tag = TagT->getDecl()) |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 1550 | if (!Tag->hasNameForLinkage()) { |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1551 | switch (Tag->getTagKind()) { |
| 1552 | case TTK_Struct: return "struct <anonymous>"; |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1553 | case TTK_Interface: return "__interface <anonymous>"; |
| 1554 | case TTK_Class: return "class <anonymous>"; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1555 | case TTK_Union: return "union <anonymous>"; |
| 1556 | case TTK_Enum: return "enum <anonymous>"; |
| 1557 | } |
| 1558 | } |
| 1559 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1560 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1561 | // Slow path: format the type as a string. |
| 1562 | std::string Result; |
| 1563 | T.getAsStringInternal(Result, Policy); |
| 1564 | return Allocator.CopyString(Result); |
| 1565 | } |
| 1566 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1567 | /// Add a completion for "this", if we're in a member function. |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1568 | static void addThisCompletion(Sema &S, ResultBuilder &Results) { |
| 1569 | QualType ThisTy = S.getCurrentThisType(); |
| 1570 | if (ThisTy.isNull()) |
| 1571 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1572 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1573 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1574 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1575 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1576 | Builder.AddResultTypeChunk(GetCompletionTypeString(ThisTy, |
| 1577 | S.Context, |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1578 | Policy, |
| 1579 | Allocator)); |
| 1580 | Builder.AddTypedTextChunk("this"); |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1581 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1584 | static void AddStaticAssertResult(CodeCompletionBuilder &Builder, |
| 1585 | ResultBuilder &Results, |
| 1586 | const LangOptions &LangOpts) { |
| 1587 | if (!LangOpts.CPlusPlus11) |
| 1588 | return; |
| 1589 | |
| 1590 | Builder.AddTypedTextChunk("static_assert"); |
| 1591 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1592 | Builder.AddPlaceholderChunk("expression"); |
| 1593 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 1594 | Builder.AddPlaceholderChunk("message"); |
| 1595 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1596 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 1597 | } |
| 1598 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1599 | /// Add language constructs that show up for "ordinary" names. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1600 | static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1601 | Scope *S, |
| 1602 | Sema &SemaRef, |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1603 | ResultBuilder &Results) { |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1604 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1605 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1606 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1607 | typedef CodeCompletionResult Result; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1608 | switch (CCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1609 | case Sema::PCC_Namespace: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1610 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1611 | if (Results.includeCodePatterns()) { |
| 1612 | // namespace <identifier> { declarations } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1613 | Builder.AddTypedTextChunk("namespace"); |
| 1614 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1615 | Builder.AddPlaceholderChunk("identifier"); |
| 1616 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1617 | Builder.AddPlaceholderChunk("declarations"); |
| 1618 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1619 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1620 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1621 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1622 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1623 | // namespace identifier = identifier ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1624 | Builder.AddTypedTextChunk("namespace"); |
| 1625 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1626 | Builder.AddPlaceholderChunk("name"); |
| 1627 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
| 1628 | Builder.AddPlaceholderChunk("namespace"); |
| 1629 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1630 | |
| 1631 | // Using directives |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1632 | Builder.AddTypedTextChunk("using"); |
| 1633 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1634 | Builder.AddTextChunk("namespace"); |
| 1635 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1636 | Builder.AddPlaceholderChunk("identifier"); |
| 1637 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1638 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1639 | // asm(string-literal) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1640 | Builder.AddTypedTextChunk("asm"); |
| 1641 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1642 | Builder.AddPlaceholderChunk("string-literal"); |
| 1643 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1644 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1645 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1646 | if (Results.includeCodePatterns()) { |
| 1647 | // Explicit template instantiation |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1648 | Builder.AddTypedTextChunk("template"); |
| 1649 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1650 | Builder.AddPlaceholderChunk("declaration"); |
| 1651 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1652 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1653 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1654 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1655 | if (SemaRef.getLangOpts().ObjC1) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1656 | AddObjCTopLevelResults(Results, true); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1657 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1658 | AddTypedefResult(Results); |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 1659 | LLVM_FALLTHROUGH; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1660 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1661 | case Sema::PCC_Class: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1662 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1663 | // Using declaration |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1664 | Builder.AddTypedTextChunk("using"); |
| 1665 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1666 | Builder.AddPlaceholderChunk("qualifier"); |
| 1667 | Builder.AddTextChunk("::"); |
| 1668 | Builder.AddPlaceholderChunk("name"); |
| 1669 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1670 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1671 | // using typename qualifier::name (only in a dependent context) |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1672 | if (SemaRef.CurContext->isDependentContext()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1673 | Builder.AddTypedTextChunk("using"); |
| 1674 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1675 | Builder.AddTextChunk("typename"); |
| 1676 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1677 | Builder.AddPlaceholderChunk("qualifier"); |
| 1678 | Builder.AddTextChunk("::"); |
| 1679 | Builder.AddPlaceholderChunk("name"); |
| 1680 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1683 | AddStaticAssertResult(Builder, Results, SemaRef.getLangOpts()); |
| 1684 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1685 | if (CCC == Sema::PCC_Class) { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1686 | AddTypedefResult(Results); |
| 1687 | |
Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 1688 | bool IsNotInheritanceScope = |
| 1689 | !(S->getFlags() & Scope::ClassInheritanceScope); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1690 | // public: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1691 | Builder.AddTypedTextChunk("public"); |
Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 1692 | if (IsNotInheritanceScope && Results.includeCodePatterns()) |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1693 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1694 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1695 | |
| 1696 | // protected: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1697 | Builder.AddTypedTextChunk("protected"); |
Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 1698 | if (IsNotInheritanceScope && Results.includeCodePatterns()) |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1699 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1700 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1701 | |
| 1702 | // private: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1703 | Builder.AddTypedTextChunk("private"); |
Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 1704 | if (IsNotInheritanceScope && Results.includeCodePatterns()) |
Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1705 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1706 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1707 | } |
| 1708 | } |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 1709 | LLVM_FALLTHROUGH; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1710 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1711 | case Sema::PCC_Template: |
| 1712 | case Sema::PCC_MemberTemplate: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1713 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns()) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1714 | // template < parameters > |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1715 | Builder.AddTypedTextChunk("template"); |
| 1716 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1717 | Builder.AddPlaceholderChunk("parameters"); |
| 1718 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1719 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1722 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1723 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1724 | break; |
| 1725 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1726 | case Sema::PCC_ObjCInterface: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1727 | AddObjCInterfaceResults(SemaRef.getLangOpts(), Results, true); |
| 1728 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1729 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1730 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1731 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1732 | case Sema::PCC_ObjCImplementation: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1733 | AddObjCImplementationResults(SemaRef.getLangOpts(), Results, true); |
| 1734 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1735 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1736 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1737 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1738 | case Sema::PCC_ObjCInstanceVariableList: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1739 | AddObjCVisibilityResults(SemaRef.getLangOpts(), Results, true); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1740 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1741 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1742 | case Sema::PCC_RecoveryInFunction: |
| 1743 | case Sema::PCC_Statement: { |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1744 | AddTypedefResult(Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1745 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1746 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns() && |
| 1747 | SemaRef.getLangOpts().CXXExceptions) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1748 | Builder.AddTypedTextChunk("try"); |
| 1749 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1750 | Builder.AddPlaceholderChunk("statements"); |
| 1751 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1752 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1753 | Builder.AddTextChunk("catch"); |
| 1754 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1755 | Builder.AddPlaceholderChunk("declaration"); |
| 1756 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1757 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1758 | Builder.AddPlaceholderChunk("statements"); |
| 1759 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1760 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1761 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1762 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1763 | if (SemaRef.getLangOpts().ObjC1) |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1764 | AddObjCStatementResults(Results, true); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1765 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1766 | if (Results.includeCodePatterns()) { |
| 1767 | // if (condition) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1768 | Builder.AddTypedTextChunk("if"); |
| 1769 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1770 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1771 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1772 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1773 | Builder.AddPlaceholderChunk("expression"); |
| 1774 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1775 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1776 | Builder.AddPlaceholderChunk("statements"); |
| 1777 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1778 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1779 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1780 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1781 | // switch (condition) { } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1782 | Builder.AddTypedTextChunk("switch"); |
| 1783 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1784 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1785 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1786 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1787 | Builder.AddPlaceholderChunk("expression"); |
| 1788 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1789 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1790 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1791 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1792 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1793 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1794 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1795 | // Switch-specific statements. |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 1796 | if (!SemaRef.getCurFunction()->SwitchStack.empty()) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1797 | // case expression: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1798 | Builder.AddTypedTextChunk("case"); |
| 1799 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1800 | Builder.AddPlaceholderChunk("expression"); |
| 1801 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1802 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1803 | |
| 1804 | // default: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1805 | Builder.AddTypedTextChunk("default"); |
| 1806 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1807 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1810 | if (Results.includeCodePatterns()) { |
| 1811 | /// while (condition) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1812 | Builder.AddTypedTextChunk("while"); |
| 1813 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1814 | if (SemaRef.getLangOpts().CPlusPlus) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1815 | Builder.AddPlaceholderChunk("condition"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1816 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1817 | Builder.AddPlaceholderChunk("expression"); |
| 1818 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1819 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1820 | Builder.AddPlaceholderChunk("statements"); |
| 1821 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1822 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1823 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1824 | |
| 1825 | // do { statements } while ( expression ); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1826 | Builder.AddTypedTextChunk("do"); |
| 1827 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1828 | Builder.AddPlaceholderChunk("statements"); |
| 1829 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1830 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1831 | Builder.AddTextChunk("while"); |
| 1832 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1833 | Builder.AddPlaceholderChunk("expression"); |
| 1834 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1835 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1836 | |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1837 | // for ( for-init-statement ; condition ; expression ) { statements } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1838 | Builder.AddTypedTextChunk("for"); |
| 1839 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1840 | if (SemaRef.getLangOpts().CPlusPlus || SemaRef.getLangOpts().C99) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1841 | Builder.AddPlaceholderChunk("init-statement"); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1842 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1843 | Builder.AddPlaceholderChunk("init-expression"); |
| 1844 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1845 | Builder.AddPlaceholderChunk("condition"); |
| 1846 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1847 | Builder.AddPlaceholderChunk("inc-expression"); |
| 1848 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1849 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1850 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1851 | Builder.AddPlaceholderChunk("statements"); |
| 1852 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1853 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1854 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1855 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1856 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1857 | if (S->getContinueParent()) { |
| 1858 | // continue ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1859 | Builder.AddTypedTextChunk("continue"); |
| 1860 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | if (S->getBreakParent()) { |
| 1864 | // break ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1865 | Builder.AddTypedTextChunk("break"); |
| 1866 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | // "return expression ;" or "return ;", depending on whether we |
| 1870 | // know the function is void or not. |
| 1871 | bool isVoid = false; |
| 1872 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1873 | isVoid = Function->getReturnType()->isVoidType(); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1874 | else if (ObjCMethodDecl *Method |
| 1875 | = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1876 | isVoid = Method->getReturnType()->isVoidType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1877 | else if (SemaRef.getCurBlock() && |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1878 | !SemaRef.getCurBlock()->ReturnType.isNull()) |
| 1879 | isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType(); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1880 | Builder.AddTypedTextChunk("return"); |
Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1881 | if (!isVoid) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1882 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1883 | Builder.AddPlaceholderChunk("expression"); |
Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1884 | } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1885 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1886 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1887 | // goto identifier ; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1888 | Builder.AddTypedTextChunk("goto"); |
| 1889 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1890 | Builder.AddPlaceholderChunk("label"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1891 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1892 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1893 | // Using directives |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1894 | Builder.AddTypedTextChunk("using"); |
| 1895 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1896 | Builder.AddTextChunk("namespace"); |
| 1897 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1898 | Builder.AddPlaceholderChunk("identifier"); |
| 1899 | Results.AddResult(Result(Builder.TakeString())); |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1900 | |
| 1901 | AddStaticAssertResult(Builder, Results, SemaRef.getLangOpts()); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1902 | } |
Galina Kistanova | be3ba9da | 2017-06-07 06:31:55 +0000 | [diff] [blame] | 1903 | LLVM_FALLTHROUGH; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1904 | |
| 1905 | // Fall through (for statement expressions). |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1906 | case Sema::PCC_ForInit: |
| 1907 | case Sema::PCC_Condition: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1908 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1909 | // Fall through: conditions and statements can have expressions. |
Galina Kistanova | 3339911 | 2017-06-03 06:35:06 +0000 | [diff] [blame] | 1910 | LLVM_FALLTHROUGH; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1911 | |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1912 | case Sema::PCC_ParenthesizedExpression: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1913 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1914 | CCC == Sema::PCC_ParenthesizedExpression) { |
| 1915 | // (__bridge <type>)<expression> |
| 1916 | Builder.AddTypedTextChunk("__bridge"); |
| 1917 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1918 | Builder.AddPlaceholderChunk("type"); |
| 1919 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1920 | Builder.AddPlaceholderChunk("expression"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1921 | Results.AddResult(Result(Builder.TakeString())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1922 | |
| 1923 | // (__bridge_transfer <Objective-C type>)<expression> |
| 1924 | Builder.AddTypedTextChunk("__bridge_transfer"); |
| 1925 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1926 | Builder.AddPlaceholderChunk("Objective-C type"); |
| 1927 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1928 | Builder.AddPlaceholderChunk("expression"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1929 | Results.AddResult(Result(Builder.TakeString())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1930 | |
| 1931 | // (__bridge_retained <CF type>)<expression> |
| 1932 | Builder.AddTypedTextChunk("__bridge_retained"); |
| 1933 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1934 | Builder.AddPlaceholderChunk("CF type"); |
| 1935 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1936 | Builder.AddPlaceholderChunk("expression"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1937 | Results.AddResult(Result(Builder.TakeString())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1938 | } |
| 1939 | // Fall through |
Galina Kistanova | 3339911 | 2017-06-03 06:35:06 +0000 | [diff] [blame] | 1940 | LLVM_FALLTHROUGH; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1941 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1942 | case Sema::PCC_Expression: { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1943 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1944 | // 'this', if we're in a non-static member function. |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1945 | addThisCompletion(SemaRef, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1946 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1947 | // true |
| 1948 | Builder.AddResultTypeChunk("bool"); |
| 1949 | Builder.AddTypedTextChunk("true"); |
| 1950 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1951 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1952 | // false |
| 1953 | Builder.AddResultTypeChunk("bool"); |
| 1954 | Builder.AddTypedTextChunk("false"); |
| 1955 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1956 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1957 | if (SemaRef.getLangOpts().RTTI) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1958 | // dynamic_cast < type-id > ( expression ) |
| 1959 | Builder.AddTypedTextChunk("dynamic_cast"); |
| 1960 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1961 | Builder.AddPlaceholderChunk("type"); |
| 1962 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1963 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1964 | Builder.AddPlaceholderChunk("expression"); |
| 1965 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1966 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1967 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1968 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1969 | // static_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1970 | Builder.AddTypedTextChunk("static_cast"); |
| 1971 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1972 | Builder.AddPlaceholderChunk("type"); |
| 1973 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1974 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1975 | Builder.AddPlaceholderChunk("expression"); |
| 1976 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1977 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1978 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1979 | // reinterpret_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1980 | Builder.AddTypedTextChunk("reinterpret_cast"); |
| 1981 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1982 | Builder.AddPlaceholderChunk("type"); |
| 1983 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1984 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1985 | Builder.AddPlaceholderChunk("expression"); |
| 1986 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1987 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1988 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1989 | // const_cast < type-id > ( expression ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1990 | Builder.AddTypedTextChunk("const_cast"); |
| 1991 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1992 | Builder.AddPlaceholderChunk("type"); |
| 1993 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1994 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1995 | Builder.AddPlaceholderChunk("expression"); |
| 1996 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1997 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1998 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1999 | if (SemaRef.getLangOpts().RTTI) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2000 | // typeid ( expression-or-type ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2001 | Builder.AddResultTypeChunk("std::type_info"); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2002 | Builder.AddTypedTextChunk("typeid"); |
| 2003 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2004 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 2005 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2006 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2007 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2008 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2009 | // new T ( ... ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2010 | Builder.AddTypedTextChunk("new"); |
| 2011 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2012 | Builder.AddPlaceholderChunk("type"); |
| 2013 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2014 | Builder.AddPlaceholderChunk("expressions"); |
| 2015 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2016 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2017 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2018 | // new T [ ] ( ... ) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2019 | Builder.AddTypedTextChunk("new"); |
| 2020 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2021 | Builder.AddPlaceholderChunk("type"); |
| 2022 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 2023 | Builder.AddPlaceholderChunk("size"); |
| 2024 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 2025 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2026 | Builder.AddPlaceholderChunk("expressions"); |
| 2027 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2028 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2029 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2030 | // delete expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2031 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2032 | Builder.AddTypedTextChunk("delete"); |
| 2033 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2034 | Builder.AddPlaceholderChunk("expression"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2035 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2036 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2037 | // delete [] expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2038 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2039 | Builder.AddTypedTextChunk("delete"); |
| 2040 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2041 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 2042 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 2043 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2044 | Builder.AddPlaceholderChunk("expression"); |
| 2045 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2046 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2047 | if (SemaRef.getLangOpts().CXXExceptions) { |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2048 | // throw expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2049 | Builder.AddResultTypeChunk("void"); |
Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2050 | Builder.AddTypedTextChunk("throw"); |
| 2051 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2052 | Builder.AddPlaceholderChunk("expression"); |
| 2053 | Results.AddResult(Result(Builder.TakeString())); |
| 2054 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2055 | |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 2056 | // FIXME: Rethrow? |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2057 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2058 | if (SemaRef.getLangOpts().CPlusPlus11) { |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2059 | // nullptr |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2060 | Builder.AddResultTypeChunk("std::nullptr_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2061 | Builder.AddTypedTextChunk("nullptr"); |
| 2062 | Results.AddResult(Result(Builder.TakeString())); |
| 2063 | |
| 2064 | // alignof |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2065 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2066 | Builder.AddTypedTextChunk("alignof"); |
| 2067 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2068 | Builder.AddPlaceholderChunk("type"); |
| 2069 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2070 | Results.AddResult(Result(Builder.TakeString())); |
| 2071 | |
| 2072 | // noexcept |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2073 | Builder.AddResultTypeChunk("bool"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2074 | Builder.AddTypedTextChunk("noexcept"); |
| 2075 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2076 | Builder.AddPlaceholderChunk("expression"); |
| 2077 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2078 | Results.AddResult(Result(Builder.TakeString())); |
| 2079 | |
| 2080 | // sizeof... expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2081 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2082 | Builder.AddTypedTextChunk("sizeof..."); |
| 2083 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2084 | Builder.AddPlaceholderChunk("parameter-pack"); |
| 2085 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2086 | Results.AddResult(Result(Builder.TakeString())); |
| 2087 | } |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2090 | if (SemaRef.getLangOpts().ObjC1) { |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2091 | // Add "super", if we're in an Objective-C class with a superclass. |
Ted Kremenek | 305a0a7 | 2010-05-31 21:43:10 +0000 | [diff] [blame] | 2092 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
| 2093 | // The interface can be NULL. |
| 2094 | if (ObjCInterfaceDecl *ID = Method->getClassInterface()) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2095 | if (ID->getSuperClass()) { |
| 2096 | std::string SuperType; |
| 2097 | SuperType = ID->getSuperClass()->getNameAsString(); |
| 2098 | if (Method->isInstanceMethod()) |
| 2099 | SuperType += " *"; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2100 | |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2101 | Builder.AddResultTypeChunk(Allocator.CopyString(SuperType)); |
| 2102 | Builder.AddTypedTextChunk("super"); |
| 2103 | Results.AddResult(Result(Builder.TakeString())); |
| 2104 | } |
Ted Kremenek | 305a0a7 | 2010-05-31 21:43:10 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2107 | AddObjCExpressionResults(Results, true); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2110 | if (SemaRef.getLangOpts().C11) { |
| 2111 | // _Alignof |
| 2112 | Builder.AddResultTypeChunk("size_t"); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2113 | if (SemaRef.PP.isMacroDefined("alignof")) |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2114 | Builder.AddTypedTextChunk("alignof"); |
| 2115 | else |
| 2116 | Builder.AddTypedTextChunk("_Alignof"); |
| 2117 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2118 | Builder.AddPlaceholderChunk("type"); |
| 2119 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2120 | Results.AddResult(Result(Builder.TakeString())); |
| 2121 | } |
| 2122 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2123 | // sizeof expression |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2124 | Builder.AddResultTypeChunk("size_t"); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2125 | Builder.AddTypedTextChunk("sizeof"); |
| 2126 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2127 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 2128 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2129 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2130 | break; |
| 2131 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2132 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2133 | case Sema::PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2134 | case Sema::PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 2135 | break; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2138 | if (WantTypesInContext(CCC, SemaRef.getLangOpts())) |
| 2139 | AddTypeSpecifierResults(SemaRef.getLangOpts(), Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2140 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2141 | if (SemaRef.getLangOpts().CPlusPlus && CCC != Sema::PCC_Type) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2142 | Results.AddResult(Result("operator")); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2145 | /// If the given declaration has an associated type, add it as a result |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2146 | /// type chunk. |
| 2147 | static void AddResultTypeChunk(ASTContext &Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2148 | const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2149 | const NamedDecl *ND, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2150 | QualType BaseType, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2151 | CodeCompletionBuilder &Result) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2152 | if (!ND) |
| 2153 | return; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2154 | |
| 2155 | // Skip constructors and conversion functions, which have their return types |
| 2156 | // built into their names. |
Sam McCall | 63c5972 | 2018-01-22 20:44:47 +0000 | [diff] [blame] | 2157 | if (isConstructor(ND) || isa<CXXConversionDecl>(ND)) |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2158 | return; |
| 2159 | |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2160 | // Determine the type of the declaration (if it has a type). |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 2161 | QualType T; |
| 2162 | if (const FunctionDecl *Function = ND->getAsFunction()) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2163 | T = Function->getReturnType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2164 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
| 2165 | if (!BaseType.isNull()) |
| 2166 | T = Method->getSendResultType(BaseType); |
| 2167 | else |
| 2168 | T = Method->getReturnType(); |
Ilya Biryukov | b5da91c | 2017-11-08 10:39:09 +0000 | [diff] [blame] | 2169 | } else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2170 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); |
Ilya Biryukov | b5da91c | 2017-11-08 10:39:09 +0000 | [diff] [blame] | 2171 | T = clang::TypeName::getFullyQualifiedType(T, Context); |
| 2172 | } else if (isa<UnresolvedUsingValueDecl>(ND)) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2173 | /* Do nothing: ignore unresolved using declarations*/ |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2174 | } else if (const ObjCIvarDecl *Ivar = dyn_cast<ObjCIvarDecl>(ND)) { |
| 2175 | if (!BaseType.isNull()) |
| 2176 | T = Ivar->getUsageType(BaseType); |
| 2177 | else |
| 2178 | T = Ivar->getType(); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2179 | } else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND)) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2180 | T = Value->getType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2181 | } else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) { |
| 2182 | if (!BaseType.isNull()) |
| 2183 | T = Property->getUsageType(BaseType); |
| 2184 | else |
| 2185 | T = Property->getType(); |
| 2186 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2187 | |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2188 | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) |
| 2189 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2190 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2191 | Result.AddResultTypeChunk(GetCompletionTypeString(T, Context, Policy, |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2192 | Result.getAllocator())); |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2195 | static void MaybeAddSentinel(Preprocessor &PP, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2196 | const NamedDecl *FunctionOrMethod, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2197 | CodeCompletionBuilder &Result) { |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2198 | if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) |
| 2199 | if (Sentinel->getSentinel() == 0) { |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2200 | if (PP.getLangOpts().ObjC1 && PP.isMacroDefined("nil")) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2201 | Result.AddTextChunk(", nil"); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2202 | else if (PP.isMacroDefined("NULL")) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2203 | Result.AddTextChunk(", NULL"); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2204 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2205 | Result.AddTextChunk(", (void*)0"); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2206 | } |
| 2207 | } |
| 2208 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2209 | static std::string formatObjCParamQualifiers(unsigned ObjCQuals, |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2210 | QualType &Type) { |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2211 | std::string Result; |
| 2212 | if (ObjCQuals & Decl::OBJC_TQ_In) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2213 | Result += "in "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2214 | else if (ObjCQuals & Decl::OBJC_TQ_Inout) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2215 | Result += "inout "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2216 | else if (ObjCQuals & Decl::OBJC_TQ_Out) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2217 | Result += "out "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2218 | if (ObjCQuals & Decl::OBJC_TQ_Bycopy) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2219 | Result += "bycopy "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2220 | else if (ObjCQuals & Decl::OBJC_TQ_Byref) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2221 | Result += "byref "; |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2222 | if (ObjCQuals & Decl::OBJC_TQ_Oneway) |
Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2223 | Result += "oneway "; |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2224 | if (ObjCQuals & Decl::OBJC_TQ_CSNullability) { |
| 2225 | if (auto nullability = AttributedType::stripOuterNullability(Type)) { |
| 2226 | switch (*nullability) { |
| 2227 | case NullabilityKind::NonNull: |
| 2228 | Result += "nonnull "; |
| 2229 | break; |
| 2230 | |
| 2231 | case NullabilityKind::Nullable: |
| 2232 | Result += "nullable "; |
| 2233 | break; |
| 2234 | |
| 2235 | case NullabilityKind::Unspecified: |
| 2236 | Result += "null_unspecified "; |
| 2237 | break; |
| 2238 | } |
| 2239 | } |
| 2240 | } |
Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2241 | return Result; |
| 2242 | } |
| 2243 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2244 | /// Tries to find the most appropriate type location for an Objective-C |
Alex Lorenz | a195120 | 2016-10-18 10:35:27 +0000 | [diff] [blame] | 2245 | /// block placeholder. |
| 2246 | /// |
| 2247 | /// This function ignores things like typedefs and qualifiers in order to |
| 2248 | /// present the most relevant and accurate block placeholders in code completion |
| 2249 | /// results. |
| 2250 | static void findTypeLocationForBlockDecl(const TypeSourceInfo *TSInfo, |
| 2251 | FunctionTypeLoc &Block, |
| 2252 | FunctionProtoTypeLoc &BlockProto, |
| 2253 | bool SuppressBlock = false) { |
| 2254 | if (!TSInfo) |
| 2255 | return; |
| 2256 | TypeLoc TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 2257 | while (true) { |
| 2258 | // Look through typedefs. |
| 2259 | if (!SuppressBlock) { |
| 2260 | if (TypedefTypeLoc TypedefTL = TL.getAs<TypedefTypeLoc>()) { |
| 2261 | if (TypeSourceInfo *InnerTSInfo = |
| 2262 | TypedefTL.getTypedefNameDecl()->getTypeSourceInfo()) { |
| 2263 | TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 2264 | continue; |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | // Look through qualified types |
| 2269 | if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>()) { |
| 2270 | TL = QualifiedTL.getUnqualifiedLoc(); |
| 2271 | continue; |
| 2272 | } |
| 2273 | |
| 2274 | if (AttributedTypeLoc AttrTL = TL.getAs<AttributedTypeLoc>()) { |
| 2275 | TL = AttrTL.getModifiedLoc(); |
| 2276 | continue; |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | // Try to get the function prototype behind the block pointer type, |
| 2281 | // then we're done. |
| 2282 | if (BlockPointerTypeLoc BlockPtr = TL.getAs<BlockPointerTypeLoc>()) { |
| 2283 | TL = BlockPtr.getPointeeLoc().IgnoreParens(); |
| 2284 | Block = TL.getAs<FunctionTypeLoc>(); |
| 2285 | BlockProto = TL.getAs<FunctionProtoTypeLoc>(); |
| 2286 | } |
| 2287 | break; |
| 2288 | } |
| 2289 | } |
| 2290 | |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2291 | static std::string |
| 2292 | formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, |
| 2293 | FunctionTypeLoc &Block, FunctionProtoTypeLoc &BlockProto, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2294 | bool SuppressBlockName = false, |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2295 | bool SuppressBlock = false, |
| 2296 | Optional<ArrayRef<QualType>> ObjCSubsts = None); |
| 2297 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2298 | static std::string FormatFunctionParameter(const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2299 | const ParmVarDecl *Param, |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2300 | bool SuppressName = false, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2301 | bool SuppressBlock = false, |
| 2302 | Optional<ArrayRef<QualType>> ObjCSubsts = None) { |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2303 | bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext()); |
| 2304 | if (Param->getType()->isDependentType() || |
| 2305 | !Param->getType()->isBlockPointerType()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2306 | // The argument for a dependent or non-block parameter is a placeholder |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2307 | // containing that parameter's type. |
| 2308 | std::string Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2309 | |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2310 | if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName) |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2311 | Result = Param->getIdentifier()->getName(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2312 | |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2313 | QualType Type = Param->getType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2314 | if (ObjCSubsts) |
| 2315 | Type = Type.substObjCTypeArgs(Param->getASTContext(), *ObjCSubsts, |
| 2316 | ObjCSubstitutionContext::Parameter); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2317 | if (ObjCMethodParam) { |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2318 | Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier(), |
| 2319 | Type); |
| 2320 | Result += Type.getAsString(Policy) + ")"; |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2321 | if (Param->getIdentifier() && !SuppressName) |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2322 | Result += Param->getIdentifier()->getName(); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2323 | } else { |
| 2324 | Type.getAsStringInternal(Result, Policy); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2325 | } |
| 2326 | return Result; |
| 2327 | } |
Alex Lorenz | a195120 | 2016-10-18 10:35:27 +0000 | [diff] [blame] | 2328 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2329 | // The argument for a block pointer parameter is a block literal with |
| 2330 | // the appropriate type. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2331 | FunctionTypeLoc Block; |
| 2332 | FunctionProtoTypeLoc BlockProto; |
Alex Lorenz | a195120 | 2016-10-18 10:35:27 +0000 | [diff] [blame] | 2333 | findTypeLocationForBlockDecl(Param->getTypeSourceInfo(), Block, BlockProto, |
| 2334 | SuppressBlock); |
Alex Lorenz | 6bf4a58 | 2017-03-13 15:43:42 +0000 | [diff] [blame] | 2335 | // Try to retrieve the block type information from the property if this is a |
| 2336 | // parameter in a setter. |
| 2337 | if (!Block && ObjCMethodParam && |
| 2338 | cast<ObjCMethodDecl>(Param->getDeclContext())->isPropertyAccessor()) { |
| 2339 | if (const auto *PD = cast<ObjCMethodDecl>(Param->getDeclContext()) |
| 2340 | ->findPropertyDecl(/*CheckOverrides=*/false)) |
| 2341 | findTypeLocationForBlockDecl(PD->getTypeSourceInfo(), Block, BlockProto, |
| 2342 | SuppressBlock); |
| 2343 | } |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2344 | |
| 2345 | if (!Block) { |
| 2346 | // We were unable to find a FunctionProtoTypeLoc with parameter names |
| 2347 | // for the block; just use the parameter type as a placeholder. |
| 2348 | std::string Result; |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2349 | if (!ObjCMethodParam && Param->getIdentifier()) |
| 2350 | Result = Param->getIdentifier()->getName(); |
| 2351 | |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2352 | QualType Type = Param->getType().getUnqualifiedType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2353 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2354 | if (ObjCMethodParam) { |
Alex Lorenz | 01bcfc1 | 2016-11-23 16:28:34 +0000 | [diff] [blame] | 2355 | Result = Type.getAsString(Policy); |
| 2356 | std::string Quals = |
| 2357 | formatObjCParamQualifiers(Param->getObjCDeclQualifier(), Type); |
| 2358 | if (!Quals.empty()) |
| 2359 | Result = "(" + Quals + " " + Result + ")"; |
| 2360 | if (Result.back() != ')') |
| 2361 | Result += " "; |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2362 | if (Param->getIdentifier()) |
| 2363 | Result += Param->getIdentifier()->getName(); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2364 | } else { |
| 2365 | Type.getAsStringInternal(Result, Policy); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2366 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2367 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2368 | return Result; |
| 2369 | } |
Alex Lorenz | 01bcfc1 | 2016-11-23 16:28:34 +0000 | [diff] [blame] | 2370 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2371 | // We have the function prototype behind the block pointer type, as it was |
| 2372 | // written in the source. |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2373 | return formatBlockPlaceholder(Policy, Param, Block, BlockProto, |
| 2374 | /*SuppressBlockName=*/false, SuppressBlock, |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2375 | ObjCSubsts); |
| 2376 | } |
| 2377 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2378 | /// Returns a placeholder string that corresponds to an Objective-C block |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2379 | /// declaration. |
| 2380 | /// |
| 2381 | /// \param BlockDecl A declaration with an Objective-C block type. |
| 2382 | /// |
| 2383 | /// \param Block The most relevant type location for that block type. |
| 2384 | /// |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 2385 | /// \param SuppressBlockName Determines whether or not the name of the block |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2386 | /// declaration is included in the resulting string. |
| 2387 | static std::string |
| 2388 | formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, |
| 2389 | FunctionTypeLoc &Block, FunctionProtoTypeLoc &BlockProto, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2390 | bool SuppressBlockName, bool SuppressBlock, |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2391 | Optional<ArrayRef<QualType>> ObjCSubsts) { |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2392 | std::string Result; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2393 | QualType ResultType = Block.getTypePtr()->getReturnType(); |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2394 | if (ObjCSubsts) |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2395 | ResultType = |
| 2396 | ResultType.substObjCTypeArgs(BlockDecl->getASTContext(), *ObjCSubsts, |
| 2397 | ObjCSubstitutionContext::Result); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2398 | if (!ResultType->isVoidType() || SuppressBlock) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2399 | ResultType.getAsStringInternal(Result, Policy); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2400 | |
| 2401 | // Format the parameter list. |
| 2402 | std::string Params; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2403 | if (!BlockProto || Block.getNumParams() == 0) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2404 | if (BlockProto && BlockProto.getTypePtr()->isVariadic()) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2405 | Params = "(...)"; |
Douglas Gregor | af25cfa | 2010-10-02 23:49:58 +0000 | [diff] [blame] | 2406 | else |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2407 | Params = "(void)"; |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2408 | } else { |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2409 | Params += "("; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2410 | for (unsigned I = 0, N = Block.getNumParams(); I != N; ++I) { |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2411 | if (I) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2412 | Params += ", "; |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2413 | Params += FormatFunctionParameter(Policy, Block.getParam(I), |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2414 | /*SuppressName=*/false, |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2415 | /*SuppressBlock=*/true, ObjCSubsts); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2416 | |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2417 | if (I == N - 1 && BlockProto.getTypePtr()->isVariadic()) |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2418 | Params += ", ..."; |
Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2419 | } |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2420 | Params += ")"; |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2421 | } |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2422 | |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2423 | if (SuppressBlock) { |
| 2424 | // Format as a parameter. |
| 2425 | Result = Result + " (^"; |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2426 | if (!SuppressBlockName && BlockDecl->getIdentifier()) |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2427 | Result += BlockDecl->getIdentifier()->getName(); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2428 | Result += ")"; |
| 2429 | Result += Params; |
| 2430 | } else { |
| 2431 | // Format as a block literal argument. |
| 2432 | Result = '^' + Result; |
| 2433 | Result += Params; |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2434 | |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2435 | if (!SuppressBlockName && BlockDecl->getIdentifier()) |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2436 | Result += BlockDecl->getIdentifier()->getName(); |
Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2437 | } |
Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2438 | |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2439 | return Result; |
| 2440 | } |
| 2441 | |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2442 | static std::string GetDefaultValueString(const ParmVarDecl *Param, |
| 2443 | const SourceManager &SM, |
| 2444 | const LangOptions &LangOpts) { |
Ilya Biryukov | b6d1ec8 | 2017-07-21 09:24:00 +0000 | [diff] [blame] | 2445 | const SourceRange SrcRange = Param->getDefaultArgRange(); |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2446 | CharSourceRange CharSrcRange = CharSourceRange::getTokenRange(SrcRange); |
| 2447 | bool Invalid = CharSrcRange.isInvalid(); |
| 2448 | if (Invalid) |
| 2449 | return ""; |
| 2450 | StringRef srcText = Lexer::getSourceText(CharSrcRange, SM, LangOpts, &Invalid); |
| 2451 | if (Invalid) |
| 2452 | return ""; |
| 2453 | |
| 2454 | if (srcText.empty() || srcText == "=") { |
| 2455 | // Lexer can't determine the value. |
| 2456 | // This happens if the code is incorrect (for example class is forward declared). |
| 2457 | return ""; |
| 2458 | } |
Erik Verbruggen | 797980e | 2017-07-19 11:15:36 +0000 | [diff] [blame] | 2459 | std::string DefValue(srcText.str()); |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2460 | // FIXME: remove this check if the Lexer::getSourceText value is fixed and |
| 2461 | // this value always has (or always does not have) '=' in front of it |
| 2462 | if (DefValue.at(0) != '=') { |
| 2463 | // If we don't have '=' in front of value. |
| 2464 | // Lexer returns built-in types values without '=' and user-defined types values with it. |
| 2465 | return " = " + DefValue; |
| 2466 | } |
| 2467 | return " " + DefValue; |
| 2468 | } |
| 2469 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2470 | /// Add function parameter chunks to the given code completion string. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2471 | static void AddFunctionParameterChunks(Preprocessor &PP, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2472 | const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2473 | const FunctionDecl *Function, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2474 | CodeCompletionBuilder &Result, |
| 2475 | unsigned Start = 0, |
| 2476 | bool InOptional = false) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2477 | bool FirstParameter = true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2478 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2479 | for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2480 | const ParmVarDecl *Param = Function->getParamDecl(P); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2481 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2482 | if (Param->hasDefaultArg() && !InOptional) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2483 | // When we see an optional default argument, put that argument and |
| 2484 | // the remaining default arguments into a new, optional string. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2485 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2486 | Result.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2487 | if (!FirstParameter) |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2488 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2489 | AddFunctionParameterChunks(PP, Policy, Function, Opt, P, true); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2490 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2491 | break; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2492 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2493 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2494 | if (FirstParameter) |
| 2495 | FirstParameter = false; |
| 2496 | else |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2497 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2498 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2499 | InOptional = false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2500 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2501 | // Format the placeholder string. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2502 | std::string PlaceholderStr = FormatFunctionParameter(Policy, Param); |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2503 | if (Param->hasDefaultArg()) |
| 2504 | PlaceholderStr += GetDefaultValueString(Param, PP.getSourceManager(), PP.getLangOpts()); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2505 | |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2506 | if (Function->isVariadic() && P == N - 1) |
| 2507 | PlaceholderStr += ", ..."; |
| 2508 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2509 | // Add the placeholder string. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2510 | Result.AddPlaceholderChunk( |
| 2511 | Result.getAllocator().CopyString(PlaceholderStr)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2512 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2513 | |
| 2514 | if (const FunctionProtoType *Proto |
Douglas Gregor | ba44903 | 2009-09-22 21:42:17 +0000 | [diff] [blame] | 2515 | = Function->getType()->getAs<FunctionProtoType>()) |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2516 | if (Proto->isVariadic()) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2517 | if (Proto->getNumParams() == 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2518 | Result.AddPlaceholderChunk("..."); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2519 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2520 | MaybeAddSentinel(PP, Function, Result); |
Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2521 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2524 | /// Add template parameter chunks to the given code completion string. |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2525 | static void AddTemplateParameterChunks(ASTContext &Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2526 | const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2527 | const TemplateDecl *Template, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2528 | CodeCompletionBuilder &Result, |
| 2529 | unsigned MaxParameters = 0, |
| 2530 | unsigned Start = 0, |
| 2531 | bool InDefaultArg = false) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2532 | bool FirstParameter = true; |
Richard Smith | 6eece29 | 2015-01-15 02:27:20 +0000 | [diff] [blame] | 2533 | |
| 2534 | // Prefer to take the template parameter names from the first declaration of |
| 2535 | // the template. |
| 2536 | Template = cast<TemplateDecl>(Template->getCanonicalDecl()); |
| 2537 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2538 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2539 | TemplateParameterList::iterator PEnd = Params->end(); |
| 2540 | if (MaxParameters) |
| 2541 | PEnd = Params->begin() + MaxParameters; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2542 | for (TemplateParameterList::iterator P = Params->begin() + Start; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2543 | P != PEnd; ++P) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2544 | bool HasDefaultArg = false; |
| 2545 | std::string PlaceholderStr; |
| 2546 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 2547 | if (TTP->wasDeclaredWithTypename()) |
| 2548 | PlaceholderStr = "typename"; |
| 2549 | else |
| 2550 | PlaceholderStr = "class"; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2551 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2552 | if (TTP->getIdentifier()) { |
| 2553 | PlaceholderStr += ' '; |
| 2554 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2555 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2556 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2557 | HasDefaultArg = TTP->hasDefaultArgument(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2558 | } else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2559 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2560 | if (NTTP->getIdentifier()) |
| 2561 | PlaceholderStr = NTTP->getIdentifier()->getName(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2562 | NTTP->getType().getAsStringInternal(PlaceholderStr, Policy); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2563 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2564 | } else { |
| 2565 | assert(isa<TemplateTemplateParmDecl>(*P)); |
| 2566 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2567 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2568 | // Since putting the template argument list into the placeholder would |
| 2569 | // be very, very long, we just use an abbreviation. |
| 2570 | PlaceholderStr = "template<...> class"; |
| 2571 | if (TTP->getIdentifier()) { |
| 2572 | PlaceholderStr += ' '; |
| 2573 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2574 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2575 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2576 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2577 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2578 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2579 | if (HasDefaultArg && !InDefaultArg) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2580 | // When we see an optional default argument, put that argument and |
| 2581 | // the remaining default arguments into a new, optional string. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2582 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2583 | Result.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2584 | if (!FirstParameter) |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2585 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2586 | AddTemplateParameterChunks(Context, Policy, Template, Opt, MaxParameters, |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2587 | P - Params->begin(), true); |
| 2588 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2589 | break; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2590 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2591 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2592 | InDefaultArg = false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2593 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2594 | if (FirstParameter) |
| 2595 | FirstParameter = false; |
| 2596 | else |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2597 | Result.AddChunk(CodeCompletionString::CK_Comma); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2598 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2599 | // Add the placeholder string. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2600 | Result.AddPlaceholderChunk( |
| 2601 | Result.getAllocator().CopyString(PlaceholderStr)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2602 | } |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2605 | /// Add a qualifier to the given code-completion string, if the |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2606 | /// provided nested-name-specifier is non-NULL. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2607 | static void |
| 2608 | AddQualifierToCompletionString(CodeCompletionBuilder &Result, |
| 2609 | NestedNameSpecifier *Qualifier, |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2610 | bool QualifierIsInformative, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2611 | ASTContext &Context, |
| 2612 | const PrintingPolicy &Policy) { |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2613 | if (!Qualifier) |
| 2614 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2615 | |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2616 | std::string PrintedNNS; |
| 2617 | { |
| 2618 | llvm::raw_string_ostream OS(PrintedNNS); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2619 | Qualifier->print(OS, Policy); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2620 | } |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2621 | if (QualifierIsInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2622 | Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2623 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2624 | Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS)); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2627 | static void |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2628 | AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2629 | const FunctionDecl *Function) { |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2630 | const FunctionProtoType *Proto |
| 2631 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2632 | if (!Proto || !Proto->getTypeQuals()) |
| 2633 | return; |
| 2634 | |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2635 | // FIXME: Add ref-qualifier! |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2636 | |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2637 | // Handle single qualifiers without copying |
| 2638 | if (Proto->getTypeQuals() == Qualifiers::Const) { |
| 2639 | Result.AddInformativeChunk(" const"); |
| 2640 | return; |
| 2641 | } |
| 2642 | |
| 2643 | if (Proto->getTypeQuals() == Qualifiers::Volatile) { |
| 2644 | Result.AddInformativeChunk(" volatile"); |
| 2645 | return; |
| 2646 | } |
| 2647 | |
| 2648 | if (Proto->getTypeQuals() == Qualifiers::Restrict) { |
| 2649 | Result.AddInformativeChunk(" restrict"); |
| 2650 | return; |
| 2651 | } |
| 2652 | |
| 2653 | // Handle multiple qualifiers. |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2654 | std::string QualsStr; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2655 | if (Proto->isConst()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2656 | QualsStr += " const"; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2657 | if (Proto->isVolatile()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2658 | QualsStr += " volatile"; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2659 | if (Proto->isRestrict()) |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2660 | QualsStr += " restrict"; |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2661 | Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr)); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2664 | /// Add the name of the given declaration |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2665 | static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2666 | const NamedDecl *ND, |
| 2667 | CodeCompletionBuilder &Result) { |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2668 | DeclarationName Name = ND->getDeclName(); |
| 2669 | if (!Name) |
| 2670 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2671 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2672 | switch (Name.getNameKind()) { |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2673 | case DeclarationName::CXXOperatorName: { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2674 | const char *OperatorName = nullptr; |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2675 | switch (Name.getCXXOverloadedOperator()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2676 | case OO_None: |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2677 | case OO_Conditional: |
| 2678 | case NUM_OVERLOADED_OPERATORS: |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2679 | OperatorName = "operator"; |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2680 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2681 | |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2682 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 2683 | case OO_##Name: OperatorName = "operator" Spelling; break; |
| 2684 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) |
| 2685 | #include "clang/Basic/OperatorKinds.def" |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2686 | |
Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2687 | case OO_New: OperatorName = "operator new"; break; |
| 2688 | case OO_Delete: OperatorName = "operator delete"; break; |
| 2689 | case OO_Array_New: OperatorName = "operator new[]"; break; |
| 2690 | case OO_Array_Delete: OperatorName = "operator delete[]"; break; |
| 2691 | case OO_Call: OperatorName = "operator()"; break; |
| 2692 | case OO_Subscript: OperatorName = "operator[]"; break; |
| 2693 | } |
| 2694 | Result.AddTypedTextChunk(OperatorName); |
| 2695 | break; |
| 2696 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2697 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2698 | case DeclarationName::Identifier: |
| 2699 | case DeclarationName::CXXConversionFunctionName: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2700 | case DeclarationName::CXXDestructorName: |
| 2701 | case DeclarationName::CXXLiteralOperatorName: |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2702 | Result.AddTypedTextChunk( |
| 2703 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2704 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2705 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 2706 | case DeclarationName::CXXDeductionGuideName: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2707 | case DeclarationName::CXXUsingDirective: |
| 2708 | case DeclarationName::ObjCZeroArgSelector: |
| 2709 | case DeclarationName::ObjCOneArgSelector: |
| 2710 | case DeclarationName::ObjCMultiArgSelector: |
| 2711 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2712 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2713 | case DeclarationName::CXXConstructorName: { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2714 | CXXRecordDecl *Record = nullptr; |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2715 | QualType Ty = Name.getCXXNameType(); |
| 2716 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) |
| 2717 | Record = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 2718 | else if (const InjectedClassNameType *InjectedTy |
| 2719 | = Ty->getAs<InjectedClassNameType>()) |
| 2720 | Record = InjectedTy->getDecl(); |
| 2721 | else { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2722 | Result.AddTypedTextChunk( |
| 2723 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2724 | break; |
| 2725 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2726 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2727 | Result.AddTypedTextChunk( |
| 2728 | Result.getAllocator().CopyString(Record->getNameAsString())); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2729 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2730 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2731 | AddTemplateParameterChunks(Context, Policy, Template, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2732 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2733 | } |
| 2734 | break; |
| 2735 | } |
| 2736 | } |
| 2737 | } |
| 2738 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2739 | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString(Sema &S, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2740 | const CodeCompletionContext &CCContext, |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2741 | CodeCompletionAllocator &Allocator, |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2742 | CodeCompletionTUInfo &CCTUInfo, |
| 2743 | bool IncludeBriefComments) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2744 | return CreateCodeCompletionString(S.Context, S.PP, CCContext, Allocator, |
| 2745 | CCTUInfo, IncludeBriefComments); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
Eric Liu | 00f43c9 | 2018-07-06 09:43:57 +0000 | [diff] [blame^] | 2748 | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionStringForMacro( |
| 2749 | Preprocessor &PP, CodeCompletionAllocator &Allocator, |
| 2750 | CodeCompletionTUInfo &CCTUInfo) { |
| 2751 | assert(Kind == RK_Macro); |
| 2752 | CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability); |
| 2753 | const MacroInfo *MI = PP.getMacroInfo(Macro); |
| 2754 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Macro->getName())); |
| 2755 | |
| 2756 | if (!MI || !MI->isFunctionLike()) |
| 2757 | return Result.TakeString(); |
| 2758 | |
| 2759 | // Format a function-like macro with placeholders for the arguments. |
| 2760 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2761 | MacroInfo::param_iterator A = MI->param_begin(), AEnd = MI->param_end(); |
| 2762 | |
| 2763 | // C99 variadic macros add __VA_ARGS__ at the end. Skip it. |
| 2764 | if (MI->isC99Varargs()) { |
| 2765 | --AEnd; |
| 2766 | |
| 2767 | if (A == AEnd) { |
| 2768 | Result.AddPlaceholderChunk("..."); |
| 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | for (MacroInfo::param_iterator A = MI->param_begin(); A != AEnd; ++A) { |
| 2773 | if (A != MI->param_begin()) |
| 2774 | Result.AddChunk(CodeCompletionString::CK_Comma); |
| 2775 | |
| 2776 | if (MI->isVariadic() && (A + 1) == AEnd) { |
| 2777 | SmallString<32> Arg = (*A)->getName(); |
| 2778 | if (MI->isC99Varargs()) |
| 2779 | Arg += ", ..."; |
| 2780 | else |
| 2781 | Arg += "..."; |
| 2782 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
| 2783 | break; |
| 2784 | } |
| 2785 | |
| 2786 | // Non-variadic macros are simple. |
| 2787 | Result.AddPlaceholderChunk( |
| 2788 | Result.getAllocator().CopyString((*A)->getName())); |
| 2789 | } |
| 2790 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
| 2791 | return Result.TakeString(); |
| 2792 | } |
| 2793 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2794 | /// If possible, create a new code completion string for the given |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2795 | /// result. |
| 2796 | /// |
| 2797 | /// \returns Either a new, heap-allocated code completion string describing |
| 2798 | /// how to use this result, or NULL to indicate that the string or name of the |
| 2799 | /// result is all that is needed. |
| 2800 | CodeCompletionString * |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2801 | CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx, |
| 2802 | Preprocessor &PP, |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2803 | const CodeCompletionContext &CCContext, |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2804 | CodeCompletionAllocator &Allocator, |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2805 | CodeCompletionTUInfo &CCTUInfo, |
| 2806 | bool IncludeBriefComments) { |
Eric Liu | 00f43c9 | 2018-07-06 09:43:57 +0000 | [diff] [blame^] | 2807 | if (Kind == RK_Macro) |
| 2808 | return CreateCodeCompletionStringForMacro(PP, Allocator, CCTUInfo); |
| 2809 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2810 | CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2811 | |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2812 | PrintingPolicy Policy = getCompletionPrintingPolicy(Ctx, PP); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2813 | if (Kind == RK_Pattern) { |
| 2814 | Pattern->Priority = Priority; |
| 2815 | Pattern->Availability = Availability; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2816 | |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2817 | if (Declaration) { |
| 2818 | Result.addParentContext(Declaration->getDeclContext()); |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2819 | Pattern->ParentName = Result.getParentName(); |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 2820 | if (const RawComment *RC = |
| 2821 | getPatternCompletionComment(Ctx, Declaration)) { |
| 2822 | Result.addBriefComment(RC->getBriefText(Ctx)); |
| 2823 | Pattern->BriefComment = Result.getBriefComment(); |
| 2824 | } |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2825 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2826 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2827 | return Pattern; |
| 2828 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2829 | |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2830 | if (Kind == RK_Keyword) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2831 | Result.AddTypedTextChunk(Keyword); |
| 2832 | return Result.TakeString(); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2833 | } |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2834 | assert(Kind == RK_Declaration && "Missed a result kind?"); |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2835 | const NamedDecl *ND = Declaration; |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2836 | Result.addParentContext(ND->getDeclContext()); |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2837 | |
| 2838 | if (IncludeBriefComments) { |
| 2839 | // Add documentation comment, if it exists. |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 2840 | if (const RawComment *RC = getCompletionComment(Ctx, Declaration)) { |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2841 | Result.addBriefComment(RC->getBriefText(Ctx)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2842 | } |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2845 | if (StartsNestedNameSpecifier) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2846 | Result.AddTypedTextChunk( |
| 2847 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2848 | Result.AddTextChunk("::"); |
| 2849 | return Result.TakeString(); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2850 | } |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 2851 | |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2852 | for (const auto *I : ND->specific_attrs<AnnotateAttr>()) |
| 2853 | Result.AddAnnotation(Result.getAllocator().CopyString(I->getAnnotation())); |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 2854 | |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2855 | AddResultTypeChunk(Ctx, Policy, ND, CCContext.getBaseType(), Result); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2856 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2857 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2858 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2859 | Ctx, Policy); |
| 2860 | AddTypedNameChunk(Ctx, Policy, ND, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2861 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2862 | AddFunctionParameterChunks(PP, Policy, Function, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2863 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2864 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2865 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2866 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2867 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2868 | if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2869 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2870 | Ctx, Policy); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2871 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2872 | AddTypedNameChunk(Ctx, Policy, Function, Result); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2873 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2874 | // Figure out which template parameters are deduced (or have default |
| 2875 | // arguments). |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 2876 | llvm::SmallBitVector Deduced; |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2877 | Sema::MarkDeducedTemplateParameters(Ctx, FunTmpl, Deduced); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2878 | unsigned LastDeducibleArgument; |
| 2879 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
| 2880 | --LastDeducibleArgument) { |
| 2881 | if (!Deduced[LastDeducibleArgument - 1]) { |
| 2882 | // C++0x: Figure out if the template argument has a default. If so, |
| 2883 | // the user doesn't need to type this argument. |
| 2884 | // FIXME: We need to abstract template parameters better! |
| 2885 | bool HasDefaultArg = false; |
| 2886 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2887 | LastDeducibleArgument - 1); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2888 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 2889 | HasDefaultArg = TTP->hasDefaultArgument(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2890 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2891 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 2892 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2893 | else { |
| 2894 | assert(isa<TemplateTemplateParmDecl>(Param)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2895 | HasDefaultArg |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2896 | = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2897 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2898 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2899 | if (!HasDefaultArg) |
| 2900 | break; |
| 2901 | } |
| 2902 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2903 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2904 | if (LastDeducibleArgument) { |
| 2905 | // Some of the function template arguments cannot be deduced from a |
| 2906 | // function call, so we introduce an explicit template argument list |
| 2907 | // containing all of the arguments up to the first deducible argument. |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2908 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2909 | AddTemplateParameterChunks(Ctx, Policy, FunTmpl, Result, |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2910 | LastDeducibleArgument); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2911 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2912 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2913 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2914 | // Add the function parameters |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2915 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2916 | AddFunctionParameterChunks(PP, Policy, Function, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2917 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2918 | AddFunctionTypeQualsToCompletionString(Result, Function); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2919 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2920 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2921 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2922 | if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2923 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2924 | Ctx, Policy); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2925 | Result.AddTypedTextChunk( |
| 2926 | Result.getAllocator().CopyString(Template->getNameAsString())); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2927 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2928 | AddTemplateParameterChunks(Ctx, Policy, Template, Result); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2929 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2930 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2931 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2932 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2933 | if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2934 | Selector Sel = Method->getSelector(); |
| 2935 | if (Sel.isUnarySelector()) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2936 | Result.AddTypedTextChunk(Result.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2937 | Sel.getNameForSlot(0))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2938 | return Result.TakeString(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2941 | std::string SelName = Sel.getNameForSlot(0).str(); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2942 | SelName += ':'; |
| 2943 | if (StartParameter == 0) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2944 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName)); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2945 | else { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2946 | Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2947 | |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2948 | // If there is only one parameter, and we're past it, add an empty |
| 2949 | // typed-text chunk since there is nothing to type. |
| 2950 | if (Method->param_size() == 1) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2951 | Result.AddTypedTextChunk(""); |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2952 | } |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2953 | unsigned Idx = 0; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2954 | for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(), |
| 2955 | PEnd = Method->param_end(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2956 | P != PEnd; (void)++P, ++Idx) { |
| 2957 | if (Idx > 0) { |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2958 | std::string Keyword; |
| 2959 | if (Idx > StartParameter) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2960 | Result.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2961 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 2962 | Keyword += II->getName(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2963 | Keyword += ":"; |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2964 | if (Idx < StartParameter || AllParametersAreInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2965 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2966 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2967 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword)); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2968 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2969 | |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2970 | // If we're before the starting parameter, skip the placeholder. |
| 2971 | if (Idx < StartParameter) |
| 2972 | continue; |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2973 | |
| 2974 | std::string Arg; |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2975 | QualType ParamType = (*P)->getType(); |
| 2976 | Optional<ArrayRef<QualType>> ObjCSubsts; |
| 2977 | if (!CCContext.getBaseType().isNull()) |
| 2978 | ObjCSubsts = CCContext.getBaseType()->getObjCSubstitutions(Method); |
| 2979 | |
| 2980 | if (ParamType->isBlockPointerType() && !DeclaringEntity) |
| 2981 | Arg = FormatFunctionParameter(Policy, *P, true, |
| 2982 | /*SuppressBlock=*/false, |
| 2983 | ObjCSubsts); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2984 | else { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2985 | if (ObjCSubsts) |
| 2986 | ParamType = ParamType.substObjCTypeArgs(Ctx, *ObjCSubsts, |
| 2987 | ObjCSubstitutionContext::Parameter); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2988 | Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier(), |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2989 | ParamType); |
| 2990 | Arg += ParamType.getAsString(Policy) + ")"; |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2991 | if (IdentifierInfo *II = (*P)->getIdentifier()) |
Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2992 | if (DeclaringEntity || AllParametersAreInformative) |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 2993 | Arg += II->getName(); |
Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2994 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2995 | |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2996 | if (Method->isVariadic() && (P + 1) == PEnd) |
| 2997 | Arg += ", ..."; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2998 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2999 | if (DeclaringEntity) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3000 | Result.AddTextChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 3001 | else if (AllParametersAreInformative) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3002 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3003 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3004 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 3007 | if (Method->isVariadic()) { |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3008 | if (Method->param_size() == 0) { |
| 3009 | if (DeclaringEntity) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3010 | Result.AddTextChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3011 | else if (AllParametersAreInformative) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3012 | Result.AddInformativeChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3013 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3014 | Result.AddPlaceholderChunk(", ..."); |
Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3015 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3016 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 3017 | MaybeAddSentinel(PP, Method, Result); |
Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 3018 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3019 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3020 | return Result.TakeString(); |
Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3021 | } |
| 3022 | |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 3023 | if (Qualifier) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3024 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3025 | Ctx, Policy); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 3026 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3027 | Result.AddTypedTextChunk( |
| 3028 | Result.getAllocator().CopyString(ND->getNameAsString())); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3029 | return Result.TakeString(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3032 | const RawComment *clang::getCompletionComment(const ASTContext &Ctx, |
| 3033 | const NamedDecl *ND) { |
| 3034 | if (!ND) |
| 3035 | return nullptr; |
| 3036 | if (auto *RC = Ctx.getRawCommentForAnyRedecl(ND)) |
| 3037 | return RC; |
| 3038 | |
| 3039 | // Try to find comment from a property for ObjC methods. |
| 3040 | const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(ND); |
| 3041 | if (!M) |
| 3042 | return nullptr; |
| 3043 | const ObjCPropertyDecl *PDecl = M->findPropertyDecl(); |
| 3044 | if (!PDecl) |
| 3045 | return nullptr; |
| 3046 | |
| 3047 | return Ctx.getRawCommentForAnyRedecl(PDecl); |
| 3048 | } |
| 3049 | |
| 3050 | const RawComment *clang::getPatternCompletionComment(const ASTContext &Ctx, |
| 3051 | const NamedDecl *ND) { |
| 3052 | const ObjCMethodDecl *M = dyn_cast_or_null<ObjCMethodDecl>(ND); |
| 3053 | if (!M || !M->isPropertyAccessor()) |
| 3054 | return nullptr; |
| 3055 | |
| 3056 | // Provide code completion comment for self.GetterName where |
| 3057 | // GetterName is the getter method for a property with name |
| 3058 | // different from the property name (declared via a property |
| 3059 | // getter attribute. |
| 3060 | const ObjCPropertyDecl *PDecl = M->findPropertyDecl(); |
| 3061 | if (!PDecl) |
| 3062 | return nullptr; |
| 3063 | if (PDecl->getGetterName() == M->getSelector() && |
| 3064 | PDecl->getIdentifier() != M->getIdentifier()) { |
| 3065 | if (auto *RC = Ctx.getRawCommentForAnyRedecl(M)) |
| 3066 | return RC; |
| 3067 | if (auto *RC = Ctx.getRawCommentForAnyRedecl(PDecl)) |
| 3068 | return RC; |
| 3069 | } |
| 3070 | return nullptr; |
| 3071 | } |
| 3072 | |
| 3073 | const RawComment *clang::getParameterComment( |
| 3074 | const ASTContext &Ctx, |
| 3075 | const CodeCompleteConsumer::OverloadCandidate &Result, |
| 3076 | unsigned ArgIndex) { |
| 3077 | auto FDecl = Result.getFunction(); |
| 3078 | if (!FDecl) |
| 3079 | return nullptr; |
| 3080 | if (ArgIndex < FDecl->getNumParams()) |
| 3081 | return Ctx.getRawCommentForAnyRedecl(FDecl->getParamDecl(ArgIndex)); |
| 3082 | return nullptr; |
| 3083 | } |
| 3084 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3085 | /// Add function overload parameter chunks to the given code completion |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3086 | /// string. |
| 3087 | static void AddOverloadParameterChunks(ASTContext &Context, |
| 3088 | const PrintingPolicy &Policy, |
| 3089 | const FunctionDecl *Function, |
| 3090 | const FunctionProtoType *Prototype, |
| 3091 | CodeCompletionBuilder &Result, |
| 3092 | unsigned CurrentArg, |
| 3093 | unsigned Start = 0, |
| 3094 | bool InOptional = false) { |
| 3095 | bool FirstParameter = true; |
| 3096 | unsigned NumParams = Function ? Function->getNumParams() |
| 3097 | : Prototype->getNumParams(); |
| 3098 | |
| 3099 | for (unsigned P = Start; P != NumParams; ++P) { |
| 3100 | if (Function && Function->getParamDecl(P)->hasDefaultArg() && !InOptional) { |
| 3101 | // When we see an optional default argument, put that argument and |
| 3102 | // the remaining default arguments into a new, optional string. |
| 3103 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 3104 | Result.getCodeCompletionTUInfo()); |
| 3105 | if (!FirstParameter) |
| 3106 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| 3107 | // Optional sections are nested. |
| 3108 | AddOverloadParameterChunks(Context, Policy, Function, Prototype, Opt, |
| 3109 | CurrentArg, P, /*InOptional=*/true); |
| 3110 | Result.AddOptionalChunk(Opt.TakeString()); |
| 3111 | return; |
| 3112 | } |
| 3113 | |
| 3114 | if (FirstParameter) |
| 3115 | FirstParameter = false; |
| 3116 | else |
| 3117 | Result.AddChunk(CodeCompletionString::CK_Comma); |
| 3118 | |
| 3119 | InOptional = false; |
| 3120 | |
| 3121 | // Format the placeholder string. |
| 3122 | std::string Placeholder; |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 3123 | if (Function) { |
| 3124 | const ParmVarDecl *Param = Function->getParamDecl(P); |
| 3125 | Placeholder = FormatFunctionParameter(Policy, Param); |
| 3126 | if (Param->hasDefaultArg()) |
| 3127 | Placeholder += GetDefaultValueString(Param, Context.getSourceManager(), Context.getLangOpts()); |
| 3128 | } else { |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3129 | Placeholder = Prototype->getParamType(P).getAsString(Policy); |
Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 3130 | } |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3131 | |
| 3132 | if (P == CurrentArg) |
| 3133 | Result.AddCurrentParameterChunk( |
| 3134 | Result.getAllocator().CopyString(Placeholder)); |
| 3135 | else |
| 3136 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Placeholder)); |
| 3137 | } |
| 3138 | |
| 3139 | if (Prototype && Prototype->isVariadic()) { |
| 3140 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 3141 | Result.getCodeCompletionTUInfo()); |
| 3142 | if (!FirstParameter) |
| 3143 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| 3144 | |
| 3145 | if (CurrentArg < NumParams) |
| 3146 | Opt.AddPlaceholderChunk("..."); |
| 3147 | else |
| 3148 | Opt.AddCurrentParameterChunk("..."); |
| 3149 | |
| 3150 | Result.AddOptionalChunk(Opt.TakeString()); |
| 3151 | } |
| 3152 | } |
| 3153 | |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3154 | CodeCompletionString * |
| 3155 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3156 | unsigned CurrentArg, Sema &S, |
| 3157 | CodeCompletionAllocator &Allocator, |
| 3158 | CodeCompletionTUInfo &CCTUInfo, |
| 3159 | bool IncludeBriefComments) const { |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3160 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3161 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3162 | // FIXME: Set priority, availability appropriately. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3163 | CodeCompletionBuilder Result(Allocator,CCTUInfo, 1, CXAvailability_Available); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3164 | FunctionDecl *FDecl = getFunction(); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3165 | const FunctionProtoType *Proto |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3166 | = dyn_cast<FunctionProtoType>(getFunctionType()); |
| 3167 | if (!FDecl && !Proto) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3168 | // Function without a prototype. Just give the return type and a |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3169 | // highlighted ellipsis. |
| 3170 | const FunctionType *FT = getFunctionType(); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3171 | Result.AddResultTypeChunk(Result.getAllocator().CopyString( |
| 3172 | FT->getReturnType().getAsString(Policy))); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3173 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3174 | Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "..."); |
| 3175 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3176 | return Result.TakeString(); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3177 | } |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3178 | |
| 3179 | if (FDecl) { |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3180 | if (IncludeBriefComments) { |
| 3181 | if (auto RC = getParameterComment(S.getASTContext(), *this, CurrentArg)) |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3182 | Result.addBriefComment(RC->getBriefText(S.getASTContext())); |
Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3183 | } |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3184 | AddResultTypeChunk(S.Context, Policy, FDecl, QualType(), Result); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3185 | Result.AddTextChunk( |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3186 | Result.getAllocator().CopyString(FDecl->getNameAsString())); |
| 3187 | } else { |
| 3188 | Result.AddResultTypeChunk( |
| 3189 | Result.getAllocator().CopyString( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3190 | Proto->getReturnType().getAsString(Policy))); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3191 | } |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3192 | |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3193 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3194 | AddOverloadParameterChunks(S.getASTContext(), Policy, FDecl, Proto, Result, |
| 3195 | CurrentArg); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3196 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3197 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3198 | return Result.TakeString(); |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3201 | unsigned clang::getMacroUsagePriority(StringRef MacroName, |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3202 | const LangOptions &LangOpts, |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3203 | bool PreferredTypeIsPointer) { |
| 3204 | unsigned Priority = CCP_Macro; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3205 | |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3206 | // Treat the "nil", "Nil" and "NULL" macros as null pointer constants. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3207 | if (MacroName.equals("nil") || MacroName.equals("NULL") || |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3208 | MacroName.equals("Nil")) { |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3209 | Priority = CCP_Constant; |
| 3210 | if (PreferredTypeIsPointer) |
| 3211 | Priority = Priority / CCF_SimilarTypeMatch; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3212 | } |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3213 | // Treat "YES", "NO", "true", and "false" as constants. |
| 3214 | else if (MacroName.equals("YES") || MacroName.equals("NO") || |
| 3215 | MacroName.equals("true") || MacroName.equals("false")) |
| 3216 | Priority = CCP_Constant; |
| 3217 | // Treat "bool" as a type. |
| 3218 | else if (MacroName.equals("bool")) |
| 3219 | Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3220 | |
| 3221 | |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3222 | return Priority; |
| 3223 | } |
| 3224 | |
Dmitri Gribenko | bdc80de | 2013-01-11 20:32:41 +0000 | [diff] [blame] | 3225 | CXCursorKind clang::getCursorKindForDecl(const Decl *D) { |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3226 | if (!D) |
| 3227 | return CXCursor_UnexposedDecl; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3228 | |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3229 | switch (D->getKind()) { |
| 3230 | case Decl::Enum: return CXCursor_EnumDecl; |
| 3231 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 3232 | case Decl::Field: return CXCursor_FieldDecl; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3233 | case Decl::Function: |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3234 | return CXCursor_FunctionDecl; |
| 3235 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 3236 | case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryImplDecl; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3237 | case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl; |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 3238 | |
Argyrios Kyrtzidis | 3698cef | 2012-01-24 21:39:26 +0000 | [diff] [blame] | 3239 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3240 | case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3241 | case Decl::ObjCMethod: |
| 3242 | return cast<ObjCMethodDecl>(D)->isInstanceMethod() |
| 3243 | ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl; |
| 3244 | case Decl::CXXMethod: return CXCursor_CXXMethod; |
| 3245 | case Decl::CXXConstructor: return CXCursor_Constructor; |
| 3246 | case Decl::CXXDestructor: return CXCursor_Destructor; |
| 3247 | case Decl::CXXConversion: return CXCursor_ConversionFunction; |
| 3248 | case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl; |
Argyrios Kyrtzidis | 3698cef | 2012-01-24 21:39:26 +0000 | [diff] [blame] | 3249 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3250 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 3251 | case Decl::Typedef: return CXCursor_TypedefDecl; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3252 | case Decl::TypeAlias: return CXCursor_TypeAliasDecl; |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 3253 | case Decl::TypeAliasTemplate: return CXCursor_TypeAliasTemplateDecl; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3254 | case Decl::Var: return CXCursor_VarDecl; |
| 3255 | case Decl::Namespace: return CXCursor_Namespace; |
| 3256 | case Decl::NamespaceAlias: return CXCursor_NamespaceAlias; |
| 3257 | case Decl::TemplateTypeParm: return CXCursor_TemplateTypeParameter; |
| 3258 | case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter; |
| 3259 | case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter; |
| 3260 | case Decl::FunctionTemplate: return CXCursor_FunctionTemplate; |
| 3261 | case Decl::ClassTemplate: return CXCursor_ClassTemplate; |
Argyrios Kyrtzidis | 12afd70 | 2011-09-30 17:58:23 +0000 | [diff] [blame] | 3262 | case Decl::AccessSpec: return CXCursor_CXXAccessSpecifier; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3263 | case Decl::ClassTemplatePartialSpecialization: |
| 3264 | return CXCursor_ClassTemplatePartialSpecialization; |
| 3265 | case Decl::UsingDirective: return CXCursor_UsingDirective; |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 3266 | case Decl::StaticAssert: return CXCursor_StaticAssert; |
Olivier Goffart | d211c64 | 2016-11-04 06:29:27 +0000 | [diff] [blame] | 3267 | case Decl::Friend: return CXCursor_FriendDecl; |
Douglas Gregor | 3e653b3 | 2012-04-30 23:41:16 +0000 | [diff] [blame] | 3268 | case Decl::TranslationUnit: return CXCursor_TranslationUnit; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3269 | |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3270 | case Decl::Using: |
| 3271 | case Decl::UnresolvedUsingValue: |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3272 | case Decl::UnresolvedUsingTypename: |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3273 | return CXCursor_UsingDeclaration; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3274 | |
Douglas Gregor | 4cd6596 | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3275 | case Decl::ObjCPropertyImpl: |
| 3276 | switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) { |
| 3277 | case ObjCPropertyImplDecl::Dynamic: |
| 3278 | return CXCursor_ObjCDynamicDecl; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3279 | |
Douglas Gregor | 4cd6596 | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3280 | case ObjCPropertyImplDecl::Synthesize: |
| 3281 | return CXCursor_ObjCSynthesizeDecl; |
| 3282 | } |
Argyrios Kyrtzidis | 50e5b1d | 2012-10-05 00:22:24 +0000 | [diff] [blame] | 3283 | |
| 3284 | case Decl::Import: |
| 3285 | return CXCursor_ModuleImportDecl; |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3286 | |
| 3287 | case Decl::ObjCTypeParam: return CXCursor_TemplateTypeParameter; |
| 3288 | |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3289 | default: |
Dmitri Gribenko | bdc80de | 2013-01-11 20:32:41 +0000 | [diff] [blame] | 3290 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3291 | switch (TD->getTagKind()) { |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3292 | case TTK_Interface: // fall through |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3293 | case TTK_Struct: return CXCursor_StructDecl; |
| 3294 | case TTK_Class: return CXCursor_ClassDecl; |
| 3295 | case TTK_Union: return CXCursor_UnionDecl; |
| 3296 | case TTK_Enum: return CXCursor_EnumDecl; |
| 3297 | } |
| 3298 | } |
| 3299 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3300 | |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3301 | return CXCursor_UnexposedDecl; |
| 3302 | } |
| 3303 | |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3304 | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3305 | bool IncludeUndefined, |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3306 | bool TargetTypeIsPointer = false) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3307 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3308 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3309 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3310 | |
| 3311 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3312 | MEnd = PP.macro_end(); |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3313 | M != MEnd; ++M) { |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 3314 | auto MD = PP.getMacroDefinition(M->first); |
| 3315 | if (IncludeUndefined || MD) { |
| 3316 | if (MacroInfo *MI = MD.getMacroInfo()) |
Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 3317 | if (MI->isUsedForHeaderGuard()) |
| 3318 | continue; |
| 3319 | |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3320 | Results.AddResult(Result(M->first, |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3321 | getMacroUsagePriority(M->first->getName(), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3322 | PP.getLangOpts(), |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3323 | TargetTypeIsPointer))); |
Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 3324 | } |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3325 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3326 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3327 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3328 | |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3329 | } |
| 3330 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3331 | static void AddPrettyFunctionResults(const LangOptions &LangOpts, |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3332 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3333 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3334 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3335 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3336 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3337 | Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant)); |
| 3338 | Results.AddResult(Result("__FUNCTION__", CCP_Constant)); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3339 | if (LangOpts.C99 || LangOpts.CPlusPlus11) |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3340 | Results.AddResult(Result("__func__", CCP_Constant)); |
| 3341 | Results.ExitScope(); |
| 3342 | } |
| 3343 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3344 | static void HandleCodeCompleteResults(Sema *S, |
| 3345 | CodeCompleteConsumer *CodeCompleter, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3346 | CodeCompletionContext Context, |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3347 | CodeCompletionResult *Results, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3348 | unsigned NumResults) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3349 | if (CodeCompleter) |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3350 | CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3353 | static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3354 | Sema::ParserCompletionContext PCC) { |
| 3355 | switch (PCC) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3356 | case Sema::PCC_Namespace: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3357 | return CodeCompletionContext::CCC_TopLevel; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3358 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3359 | case Sema::PCC_Class: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3360 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 3361 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3362 | case Sema::PCC_ObjCInterface: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3363 | return CodeCompletionContext::CCC_ObjCInterface; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3364 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3365 | case Sema::PCC_ObjCImplementation: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3366 | return CodeCompletionContext::CCC_ObjCImplementation; |
| 3367 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3368 | case Sema::PCC_ObjCInstanceVariableList: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3369 | return CodeCompletionContext::CCC_ObjCIvarList; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3370 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3371 | case Sema::PCC_Template: |
| 3372 | case Sema::PCC_MemberTemplate: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3373 | if (S.CurContext->isFileContext()) |
| 3374 | return CodeCompletionContext::CCC_TopLevel; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3375 | if (S.CurContext->isRecord()) |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3376 | return CodeCompletionContext::CCC_ClassStructUnion; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3377 | return CodeCompletionContext::CCC_Other; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3378 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3379 | case Sema::PCC_RecoveryInFunction: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3380 | return CodeCompletionContext::CCC_Recovery; |
Douglas Gregor | c769d6e | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 3381 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3382 | case Sema::PCC_ForInit: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3383 | if (S.getLangOpts().CPlusPlus || S.getLangOpts().C99 || |
| 3384 | S.getLangOpts().ObjC1) |
Douglas Gregor | c769d6e | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 3385 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
| 3386 | else |
| 3387 | return CodeCompletionContext::CCC_Expression; |
| 3388 | |
| 3389 | case Sema::PCC_Expression: |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3390 | case Sema::PCC_Condition: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3391 | return CodeCompletionContext::CCC_Expression; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3392 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3393 | case Sema::PCC_Statement: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3394 | return CodeCompletionContext::CCC_Statement; |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3395 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3396 | case Sema::PCC_Type: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3397 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3398 | |
| 3399 | case Sema::PCC_ParenthesizedExpression: |
| 3400 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3401 | |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3402 | case Sema::PCC_LocalDeclarationSpecifiers: |
| 3403 | return CodeCompletionContext::CCC_Type; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3404 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3405 | |
| 3406 | llvm_unreachable("Invalid ParserCompletionContext!"); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3409 | /// If we're in a C++ virtual member function, add completion results |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3410 | /// that invoke the functions we override, since it's common to invoke the |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3411 | /// overridden function as well as adding new functionality. |
| 3412 | /// |
| 3413 | /// \param S The semantic analysis object for which we are generating results. |
| 3414 | /// |
| 3415 | /// \param InContext This context in which the nested-name-specifier preceding |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3416 | /// the code-completion point |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3417 | static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, |
| 3418 | ResultBuilder &Results) { |
| 3419 | // Look through blocks. |
| 3420 | DeclContext *CurContext = S.CurContext; |
| 3421 | while (isa<BlockDecl>(CurContext)) |
| 3422 | CurContext = CurContext->getParent(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3423 | |
| 3424 | |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3425 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext); |
| 3426 | if (!Method || !Method->isVirtual()) |
| 3427 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3428 | |
| 3429 | // We need to have names for all of the parameters, if we're going to |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3430 | // generate a forwarding call. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3431 | for (auto P : Method->parameters()) |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3432 | if (!P->getDeclName()) |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3433 | return; |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3434 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3435 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 3436 | for (const CXXMethodDecl *Overridden : Method->overridden_methods()) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3437 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3438 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3439 | if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl()) |
| 3440 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3441 | |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3442 | // If we need a nested-name-specifier, add one now. |
| 3443 | if (!InContext) { |
| 3444 | NestedNameSpecifier *NNS |
| 3445 | = getRequiredQualification(S.Context, CurContext, |
| 3446 | Overridden->getDeclContext()); |
| 3447 | if (NNS) { |
| 3448 | std::string Str; |
| 3449 | llvm::raw_string_ostream OS(Str); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3450 | NNS->print(OS, Policy); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3451 | Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str())); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3452 | } |
| 3453 | } else if (!InContext->Equals(Overridden->getDeclContext())) |
| 3454 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3455 | |
| 3456 | Builder.AddTypedTextChunk(Results.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3457 | Overridden->getNameAsString())); |
| 3458 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3459 | bool FirstParam = true; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3460 | for (auto P : Method->parameters()) { |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3461 | if (FirstParam) |
| 3462 | FirstParam = false; |
| 3463 | else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3464 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3465 | |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3466 | Builder.AddPlaceholderChunk( |
| 3467 | Results.getAllocator().CopyString(P->getIdentifier()->getName())); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3468 | } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3469 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3470 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3471 | CCP_SuperCompletion, |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3472 | CXCursor_CXXMethod, |
| 3473 | CXAvailability_Available, |
| 3474 | Overridden)); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3475 | Results.Ignore(Overridden); |
| 3476 | } |
| 3477 | } |
| 3478 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3479 | void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc, |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3480 | ModuleIdPath Path) { |
| 3481 | typedef CodeCompletionResult Result; |
| 3482 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3483 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3484 | CodeCompletionContext::CCC_Other); |
| 3485 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3486 | |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3487 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3488 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3489 | typedef CodeCompletionResult Result; |
| 3490 | if (Path.empty()) { |
| 3491 | // Enumerate all top-level modules. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3492 | SmallVector<Module *, 8> Modules; |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3493 | PP.getHeaderSearchInfo().collectAllModules(Modules); |
| 3494 | for (unsigned I = 0, N = Modules.size(); I != N; ++I) { |
| 3495 | Builder.AddTypedTextChunk( |
| 3496 | Builder.getAllocator().CopyString(Modules[I]->Name)); |
| 3497 | Results.AddResult(Result(Builder.TakeString(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3498 | CCP_Declaration, |
Argyrios Kyrtzidis | 345d05f | 2013-05-29 18:50:15 +0000 | [diff] [blame] | 3499 | CXCursor_ModuleImportDecl, |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3500 | Modules[I]->isAvailable() |
| 3501 | ? CXAvailability_Available |
| 3502 | : CXAvailability_NotAvailable)); |
| 3503 | } |
Daniel Jasper | 07e6c40 | 2013-08-05 20:26:17 +0000 | [diff] [blame] | 3504 | } else if (getLangOpts().Modules) { |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3505 | // Load the named module. |
| 3506 | Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path, |
| 3507 | Module::AllVisible, |
| 3508 | /*IsInclusionDirective=*/false); |
| 3509 | // Enumerate submodules. |
| 3510 | if (Mod) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3511 | for (Module::submodule_iterator Sub = Mod->submodule_begin(), |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3512 | SubEnd = Mod->submodule_end(); |
| 3513 | Sub != SubEnd; ++Sub) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3514 | |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3515 | Builder.AddTypedTextChunk( |
| 3516 | Builder.getAllocator().CopyString((*Sub)->Name)); |
| 3517 | Results.AddResult(Result(Builder.TakeString(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3518 | CCP_Declaration, |
Argyrios Kyrtzidis | 345d05f | 2013-05-29 18:50:15 +0000 | [diff] [blame] | 3519 | CXCursor_ModuleImportDecl, |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3520 | (*Sub)->isAvailable() |
| 3521 | ? CXAvailability_Available |
| 3522 | : CXAvailability_NotAvailable)); |
| 3523 | } |
| 3524 | } |
| 3525 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3526 | Results.ExitScope(); |
Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3527 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 3528 | Results.data(),Results.size()); |
| 3529 | } |
| 3530 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3531 | void Sema::CodeCompleteOrdinaryName(Scope *S, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3532 | ParserCompletionContext CompletionContext) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3533 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3534 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3535 | mapCodeCompletionContext(*this, CompletionContext)); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3536 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3537 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3538 | // Determine how to filter results, e.g., so that the names of |
| 3539 | // values (functions, enumerators, function templates, etc.) are |
| 3540 | // only allowed where we can have an expression. |
| 3541 | switch (CompletionContext) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3542 | case PCC_Namespace: |
| 3543 | case PCC_Class: |
| 3544 | case PCC_ObjCInterface: |
| 3545 | case PCC_ObjCImplementation: |
| 3546 | case PCC_ObjCInstanceVariableList: |
| 3547 | case PCC_Template: |
| 3548 | case PCC_MemberTemplate: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3549 | case PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3550 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3551 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 3552 | break; |
| 3553 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3554 | case PCC_Statement: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3555 | case PCC_ParenthesizedExpression: |
Douglas Gregor | 4d755e8 | 2010-08-24 23:58:17 +0000 | [diff] [blame] | 3556 | case PCC_Expression: |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3557 | case PCC_ForInit: |
| 3558 | case PCC_Condition: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3559 | if (WantTypesInContext(CompletionContext, getLangOpts())) |
Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 3560 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 3561 | else |
| 3562 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3563 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3564 | if (getLangOpts().CPlusPlus) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3565 | MaybeAddOverrideCalls(*this, /*InContext=*/nullptr, Results); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3566 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3567 | |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3568 | case PCC_RecoveryInFunction: |
Douglas Gregor | 6da3db4 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 3569 | // Unfiltered |
| 3570 | break; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3571 | } |
| 3572 | |
Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3573 | // If we are in a C++ non-static member function, check the qualifiers on |
| 3574 | // the member function to filter/prioritize the results list. |
| 3575 | if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext)) |
| 3576 | if (CurMethod->isInstance()) |
| 3577 | Results.setObjectTypeQualifiers( |
| 3578 | Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3579 | |
Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 3580 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3581 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 3582 | CodeCompleter->includeGlobals(), |
| 3583 | CodeCompleter->loadExternal()); |
Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 3584 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3585 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); |
Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 3586 | Results.ExitScope(); |
| 3587 | |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3588 | switch (CompletionContext) { |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3589 | case PCC_ParenthesizedExpression: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3590 | case PCC_Expression: |
| 3591 | case PCC_Statement: |
| 3592 | case PCC_RecoveryInFunction: |
| 3593 | if (S->getFnParent()) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 3594 | AddPrettyFunctionResults(getLangOpts(), Results); |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3595 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3596 | |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3597 | case PCC_Namespace: |
| 3598 | case PCC_Class: |
| 3599 | case PCC_ObjCInterface: |
| 3600 | case PCC_ObjCImplementation: |
| 3601 | case PCC_ObjCInstanceVariableList: |
| 3602 | case PCC_Template: |
| 3603 | case PCC_MemberTemplate: |
| 3604 | case PCC_ForInit: |
| 3605 | case PCC_Condition: |
| 3606 | case PCC_Type: |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3607 | case PCC_LocalDeclarationSpecifiers: |
Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3608 | break; |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3609 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3610 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3611 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3612 | AddMacroResults(PP, Results, false); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3613 | |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 3614 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3615 | Results.data(),Results.size()); |
Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 3616 | } |
| 3617 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3618 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3619 | ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 3620 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 3621 | bool AtArgumentExpression, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3622 | bool IsSuper, |
| 3623 | ResultBuilder &Results); |
| 3624 | |
| 3625 | void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, |
| 3626 | bool AllowNonIdentifiers, |
| 3627 | bool AllowNestedNameSpecifiers) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3628 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3629 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3630 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3631 | AllowNestedNameSpecifiers |
| 3632 | ? CodeCompletionContext::CCC_PotentiallyQualifiedName |
| 3633 | : CodeCompletionContext::CCC_Name); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3634 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3635 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3636 | // Type qualifiers can come after names. |
| 3637 | Results.AddResult(Result("const")); |
| 3638 | Results.AddResult(Result("volatile")); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3639 | if (getLangOpts().C99) |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3640 | Results.AddResult(Result("restrict")); |
| 3641 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3642 | if (getLangOpts().CPlusPlus) { |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 3643 | if (getLangOpts().CPlusPlus11 && |
| 3644 | (DS.getTypeSpecType() == DeclSpec::TST_class || |
| 3645 | DS.getTypeSpecType() == DeclSpec::TST_struct)) |
| 3646 | Results.AddResult("final"); |
| 3647 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3648 | if (AllowNonIdentifiers) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3649 | Results.AddResult(Result("operator")); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3650 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3651 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3652 | // Add nested-name-specifiers. |
| 3653 | if (AllowNestedNameSpecifiers) { |
| 3654 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3655 | Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3656 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3657 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 3658 | CodeCompleter->includeGlobals(), |
| 3659 | CodeCompleter->loadExternal()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3660 | Results.setFilter(nullptr); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3661 | } |
| 3662 | } |
| 3663 | Results.ExitScope(); |
| 3664 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3665 | // If we're in a context where we might have an expression (rather than a |
| 3666 | // declaration), and what we've seen so far is an Objective-C type that could |
| 3667 | // be a receiver of a class message, this may be a class message send with |
| 3668 | // the initial opening bracket '[' missing. Add appropriate completions. |
| 3669 | if (AllowNonIdentifiers && !AllowNestedNameSpecifiers && |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3670 | DS.getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3671 | DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3672 | DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified && |
| 3673 | DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3674 | !DS.isTypeAltiVecVector() && |
| 3675 | S && |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3676 | (S->getFlags() & Scope::DeclScope) != 0 && |
| 3677 | (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3678 | Scope::FunctionPrototypeScope | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3679 | Scope::AtCatchScope)) == 0) { |
| 3680 | ParsedType T = DS.getRepAsType(); |
| 3681 | if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType()) |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 3682 | AddClassMessageCompletions(*this, S, T, None, false, false, Results); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3683 | } |
| 3684 | |
Douglas Gregor | 56ccce0 | 2010-08-24 04:59:56 +0000 | [diff] [blame] | 3685 | // Note that we intentionally suppress macro results here, since we do not |
| 3686 | // encourage using macros to produce the names of entities. |
| 3687 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3688 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3689 | Results.getCompletionContext(), |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3690 | Results.data(), Results.size()); |
| 3691 | } |
| 3692 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3693 | struct Sema::CodeCompleteExpressionData { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3694 | CodeCompleteExpressionData(QualType PreferredType = QualType()) |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3695 | : PreferredType(PreferredType), IntegralConstantExpression(false), |
| 3696 | ObjCCollection(false) { } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3697 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3698 | QualType PreferredType; |
| 3699 | bool IntegralConstantExpression; |
| 3700 | bool ObjCCollection; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3701 | SmallVector<Decl *, 4> IgnoreDecls; |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3702 | }; |
| 3703 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3704 | /// Perform code-completion in an expression context when we know what |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3705 | /// type we're looking for. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3706 | void Sema::CodeCompleteExpression(Scope *S, |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3707 | const CodeCompleteExpressionData &Data) { |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 3708 | ResultBuilder Results( |
| 3709 | *this, CodeCompleter->getAllocator(), |
| 3710 | CodeCompleter->getCodeCompletionTUInfo(), |
| 3711 | CodeCompletionContext(CodeCompletionContext::CCC_Expression, |
| 3712 | Data.PreferredType)); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3713 | if (Data.ObjCCollection) |
| 3714 | Results.setFilter(&ResultBuilder::IsObjCCollection); |
| 3715 | else if (Data.IntegralConstantExpression) |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3716 | Results.setFilter(&ResultBuilder::IsIntegralConstantValue); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3717 | else if (WantTypesInContext(PCC_Expression, getLangOpts())) |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3718 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 3719 | else |
| 3720 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3721 | |
| 3722 | if (!Data.PreferredType.isNull()) |
| 3723 | Results.setPreferredType(Data.PreferredType.getNonReferenceType()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3724 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3725 | // Ignore any declarations that we were told that we don't care about. |
| 3726 | for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I) |
| 3727 | Results.Ignore(Data.IgnoreDecls[I]); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3728 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3729 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3730 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 3731 | CodeCompleter->includeGlobals(), |
| 3732 | CodeCompleter->loadExternal()); |
| 3733 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3734 | Results.EnterNewScope(); |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3735 | AddOrdinaryNameResults(PCC_Expression, S, *this, Results); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3736 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3737 | |
Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3738 | bool PreferredTypeIsPointer = false; |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3739 | if (!Data.PreferredType.isNull()) |
| 3740 | PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType() |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3741 | || Data.PreferredType->isMemberPointerType() |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3742 | || Data.PreferredType->isBlockPointerType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3743 | |
| 3744 | if (S->getFnParent() && |
| 3745 | !Data.ObjCCollection && |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3746 | !Data.IntegralConstantExpression) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 3747 | AddPrettyFunctionResults(getLangOpts(), Results); |
Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3748 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3749 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 3750 | AddMacroResults(PP, Results, false, PreferredTypeIsPointer); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 3751 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 3752 | Results.data(), Results.size()); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3753 | } |
| 3754 | |
Douglas Gregor | eda7e54 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 3755 | void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) { |
| 3756 | if (E.isInvalid()) |
| 3757 | CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3758 | else if (getLangOpts().ObjC1) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3759 | CodeCompleteObjCInstanceMessage(S, E.get(), None, false); |
Douglas Gregor | ed0b69d | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 3760 | } |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3761 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3762 | /// The set of properties that have already been added, referenced by |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3763 | /// property name. |
| 3764 | typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet; |
| 3765 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3766 | /// Retrieve the container definition, if any? |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3767 | static ObjCContainerDecl *getContainerDef(ObjCContainerDecl *Container) { |
| 3768 | if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 3769 | if (Interface->hasDefinition()) |
| 3770 | return Interface->getDefinition(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3771 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3772 | return Interface; |
| 3773 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3774 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3775 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 3776 | if (Protocol->hasDefinition()) |
| 3777 | return Protocol->getDefinition(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3778 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3779 | return Protocol; |
| 3780 | } |
| 3781 | return Container; |
| 3782 | } |
| 3783 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3784 | /// Adds a block invocation code completion result for the given block |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3785 | /// declaration \p BD. |
| 3786 | static void AddObjCBlockCall(ASTContext &Context, const PrintingPolicy &Policy, |
| 3787 | CodeCompletionBuilder &Builder, |
| 3788 | const NamedDecl *BD, |
| 3789 | const FunctionTypeLoc &BlockLoc, |
| 3790 | const FunctionProtoTypeLoc &BlockProtoLoc) { |
| 3791 | Builder.AddResultTypeChunk( |
| 3792 | GetCompletionTypeString(BlockLoc.getReturnLoc().getType(), Context, |
| 3793 | Policy, Builder.getAllocator())); |
| 3794 | |
| 3795 | AddTypedNameChunk(Context, Policy, BD, Builder); |
| 3796 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3797 | |
| 3798 | if (BlockProtoLoc && BlockProtoLoc.getTypePtr()->isVariadic()) { |
| 3799 | Builder.AddPlaceholderChunk("..."); |
| 3800 | } else { |
| 3801 | for (unsigned I = 0, N = BlockLoc.getNumParams(); I != N; ++I) { |
| 3802 | if (I) |
| 3803 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 3804 | |
| 3805 | // Format the placeholder string. |
| 3806 | std::string PlaceholderStr = |
| 3807 | FormatFunctionParameter(Policy, BlockLoc.getParam(I)); |
| 3808 | |
| 3809 | if (I == N - 1 && BlockProtoLoc && |
| 3810 | BlockProtoLoc.getTypePtr()->isVariadic()) |
| 3811 | PlaceholderStr += ", ..."; |
| 3812 | |
| 3813 | // Add the placeholder string. |
| 3814 | Builder.AddPlaceholderChunk( |
| 3815 | Builder.getAllocator().CopyString(PlaceholderStr)); |
| 3816 | } |
| 3817 | } |
| 3818 | |
| 3819 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3820 | } |
| 3821 | |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3822 | static void AddObjCProperties( |
| 3823 | const CodeCompletionContext &CCContext, ObjCContainerDecl *Container, |
| 3824 | bool AllowCategories, bool AllowNullaryMethods, DeclContext *CurContext, |
| 3825 | AddedPropertiesSet &AddedProperties, ResultBuilder &Results, |
| 3826 | bool IsBaseExprStatement = false, bool IsClassProperty = false) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3827 | typedef CodeCompletionResult Result; |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3828 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3829 | // Retrieve the definition. |
| 3830 | Container = getContainerDef(Container); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3831 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3832 | // Add properties in this container. |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3833 | const auto AddProperty = [&](const ObjCPropertyDecl *P) { |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3834 | if (!AddedProperties.insert(P->getIdentifier()).second) |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3835 | return; |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3836 | |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3837 | // FIXME: Provide block invocation completion for non-statement |
| 3838 | // expressions. |
| 3839 | if (!P->getType().getTypePtr()->isBlockPointerType() || |
| 3840 | !IsBaseExprStatement) { |
| 3841 | Results.MaybeAddResult(Result(P, Results.getBasePriority(P), nullptr), |
| 3842 | CurContext); |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3843 | return; |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3844 | } |
| 3845 | |
| 3846 | // Block setter and invocation completion is provided only when we are able |
| 3847 | // to find the FunctionProtoTypeLoc with parameter names for the block. |
| 3848 | FunctionTypeLoc BlockLoc; |
| 3849 | FunctionProtoTypeLoc BlockProtoLoc; |
| 3850 | findTypeLocationForBlockDecl(P->getTypeSourceInfo(), BlockLoc, |
| 3851 | BlockProtoLoc); |
| 3852 | if (!BlockLoc) { |
| 3853 | Results.MaybeAddResult(Result(P, Results.getBasePriority(P), nullptr), |
| 3854 | CurContext); |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3855 | return; |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3856 | } |
| 3857 | |
| 3858 | // The default completion result for block properties should be the block |
| 3859 | // invocation completion when the base expression is a statement. |
| 3860 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3861 | Results.getCodeCompletionTUInfo()); |
| 3862 | AddObjCBlockCall(Container->getASTContext(), |
| 3863 | getCompletionPrintingPolicy(Results.getSema()), Builder, P, |
| 3864 | BlockLoc, BlockProtoLoc); |
| 3865 | Results.MaybeAddResult( |
| 3866 | Result(Builder.TakeString(), P, Results.getBasePriority(P)), |
| 3867 | CurContext); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3868 | |
| 3869 | // Provide additional block setter completion iff the base expression is a |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3870 | // statement and the block property is mutable. |
| 3871 | if (!P->isReadOnly()) { |
| 3872 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3873 | Results.getCodeCompletionTUInfo()); |
| 3874 | AddResultTypeChunk(Container->getASTContext(), |
| 3875 | getCompletionPrintingPolicy(Results.getSema()), P, |
| 3876 | CCContext.getBaseType(), Builder); |
| 3877 | Builder.AddTypedTextChunk( |
| 3878 | Results.getAllocator().CopyString(P->getName())); |
| 3879 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3880 | |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3881 | std::string PlaceholderStr = formatBlockPlaceholder( |
| 3882 | getCompletionPrintingPolicy(Results.getSema()), P, BlockLoc, |
| 3883 | BlockProtoLoc, /*SuppressBlockName=*/true); |
| 3884 | // Add the placeholder string. |
| 3885 | Builder.AddPlaceholderChunk( |
| 3886 | Builder.getAllocator().CopyString(PlaceholderStr)); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3887 | |
Alex Lorenz | 6e0f393 | 2017-01-06 12:00:44 +0000 | [diff] [blame] | 3888 | // When completing blocks properties that return void the default |
| 3889 | // property completion result should show up before the setter, |
| 3890 | // otherwise the setter completion should show up before the default |
| 3891 | // property completion, as we normally want to use the result of the |
| 3892 | // call. |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3893 | Results.MaybeAddResult( |
| 3894 | Result(Builder.TakeString(), P, |
Alex Lorenz | 6e0f393 | 2017-01-06 12:00:44 +0000 | [diff] [blame] | 3895 | Results.getBasePriority(P) + |
| 3896 | (BlockLoc.getTypePtr()->getReturnType()->isVoidType() |
| 3897 | ? CCD_BlockPropertySetter |
| 3898 | : -CCD_BlockPropertySetter)), |
Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3899 | CurContext); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3900 | } |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3901 | }; |
| 3902 | |
| 3903 | if (IsClassProperty) { |
| 3904 | for (const auto *P : Container->class_properties()) |
| 3905 | AddProperty(P); |
| 3906 | } else { |
| 3907 | for (const auto *P : Container->instance_properties()) |
| 3908 | AddProperty(P); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3909 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3910 | |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3911 | // Add nullary methods or implicit class properties |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3912 | if (AllowNullaryMethods) { |
| 3913 | ASTContext &Context = Container->getASTContext(); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3914 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3915 | // Adds a method result |
| 3916 | const auto AddMethod = [&](const ObjCMethodDecl *M) { |
| 3917 | IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0); |
| 3918 | if (!Name) |
| 3919 | return; |
| 3920 | if (!AddedProperties.insert(Name).second) |
| 3921 | return; |
| 3922 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3923 | Results.getCodeCompletionTUInfo()); |
| 3924 | AddResultTypeChunk(Context, Policy, M, CCContext.getBaseType(), Builder); |
| 3925 | Builder.AddTypedTextChunk( |
| 3926 | Results.getAllocator().CopyString(Name->getName())); |
| 3927 | Results.MaybeAddResult( |
| 3928 | Result(Builder.TakeString(), M, |
| 3929 | CCP_MemberDeclaration + CCD_MethodAsProperty), |
| 3930 | CurContext); |
| 3931 | }; |
| 3932 | |
| 3933 | if (IsClassProperty) { |
| 3934 | for (const auto *M : Container->methods()) { |
| 3935 | // Gather the class method that can be used as implicit property |
| 3936 | // getters. Methods with arguments or methods that return void aren't |
| 3937 | // added to the results as they can't be used as a getter. |
| 3938 | if (!M->getSelector().isUnarySelector() || |
| 3939 | M->getReturnType()->isVoidType() || M->isInstanceMethod()) |
| 3940 | continue; |
| 3941 | AddMethod(M); |
| 3942 | } |
| 3943 | } else { |
| 3944 | for (auto *M : Container->methods()) { |
| 3945 | if (M->getSelector().isUnarySelector()) |
| 3946 | AddMethod(M); |
| 3947 | } |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3948 | } |
| 3949 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3950 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3951 | // Add properties in referenced protocols. |
| 3952 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 3953 | for (auto *P : Protocol->protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3954 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3955 | CurContext, AddedProperties, Results, |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3956 | IsBaseExprStatement, IsClassProperty); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3957 | } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){ |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3958 | if (AllowCategories) { |
| 3959 | // Look through categories. |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 3960 | for (auto *Cat : IFace->known_categories()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3961 | AddObjCProperties(CCContext, Cat, AllowCategories, AllowNullaryMethods, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3962 | CurContext, AddedProperties, Results, |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3963 | IsBaseExprStatement, IsClassProperty); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3964 | } |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 3965 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3966 | // Look through protocols. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 3967 | for (auto *I : IFace->all_referenced_protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3968 | AddObjCProperties(CCContext, I, AllowCategories, AllowNullaryMethods, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3969 | CurContext, AddedProperties, Results, |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3970 | IsBaseExprStatement, IsClassProperty); |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3971 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3972 | // Look in the superclass. |
| 3973 | if (IFace->getSuperClass()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3974 | AddObjCProperties(CCContext, IFace->getSuperClass(), AllowCategories, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3975 | AllowNullaryMethods, CurContext, AddedProperties, |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3976 | Results, IsBaseExprStatement, IsClassProperty); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3977 | } else if (const ObjCCategoryDecl *Category |
| 3978 | = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 3979 | // Look through protocols. |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 3980 | for (auto *P : Category->protocols()) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3981 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3982 | CurContext, AddedProperties, Results, |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3983 | IsBaseExprStatement, IsClassProperty); |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3984 | } |
| 3985 | } |
| 3986 | |
Alex Lorenz | 0fe0d98 | 2017-05-11 13:41:00 +0000 | [diff] [blame] | 3987 | static void AddRecordMembersCompletionResults(Sema &SemaRef, |
| 3988 | ResultBuilder &Results, Scope *S, |
| 3989 | QualType BaseType, |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 3990 | RecordDecl *RD, |
| 3991 | Optional<FixItHint> AccessOpFixIt) { |
Alex Lorenz | 0fe0d98 | 2017-05-11 13:41:00 +0000 | [diff] [blame] | 3992 | // Indicate that we are performing a member access, and the cv-qualifiers |
| 3993 | // for the base object type. |
| 3994 | Results.setObjectTypeQualifiers(BaseType.getQualifiers()); |
| 3995 | |
| 3996 | // Access to a C/C++ class, struct, or union. |
| 3997 | Results.allowNestedNameSpecifiers(); |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 3998 | std::vector<FixItHint> FixIts; |
| 3999 | if (AccessOpFixIt) |
| 4000 | FixIts.emplace_back(AccessOpFixIt.getValue()); |
| 4001 | CodeCompletionDeclConsumer Consumer(Results, SemaRef.CurContext, std::move(FixIts)); |
Alex Lorenz | 0fe0d98 | 2017-05-11 13:41:00 +0000 | [diff] [blame] | 4002 | SemaRef.LookupVisibleDecls(RD, Sema::LookupMemberName, Consumer, |
Alex Lorenz | e6afa39 | 2017-05-11 13:48:57 +0000 | [diff] [blame] | 4003 | SemaRef.CodeCompleter->includeGlobals(), |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4004 | /*IncludeDependentBases=*/true, |
| 4005 | SemaRef.CodeCompleter->loadExternal()); |
Alex Lorenz | 0fe0d98 | 2017-05-11 13:41:00 +0000 | [diff] [blame] | 4006 | |
| 4007 | if (SemaRef.getLangOpts().CPlusPlus) { |
| 4008 | if (!Results.empty()) { |
| 4009 | // The "template" keyword can follow "->" or "." in the grammar. |
| 4010 | // However, we only want to suggest the template keyword if something |
| 4011 | // is dependent. |
| 4012 | bool IsDependent = BaseType->isDependentType(); |
| 4013 | if (!IsDependent) { |
| 4014 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) |
| 4015 | if (DeclContext *Ctx = DepScope->getEntity()) { |
| 4016 | IsDependent = Ctx->isDependentContext(); |
| 4017 | break; |
| 4018 | } |
| 4019 | } |
| 4020 | |
| 4021 | if (IsDependent) |
| 4022 | Results.AddResult(CodeCompletionResult("template")); |
| 4023 | } |
| 4024 | } |
| 4025 | } |
| 4026 | |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 4027 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base, |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4028 | Expr *OtherOpBase, |
Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4029 | SourceLocation OpLoc, bool IsArrow, |
| 4030 | bool IsBaseExprStatement) { |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 4031 | if (!Base || !CodeCompleter) |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4032 | return; |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4033 | |
Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 4034 | ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow); |
| 4035 | if (ConvertedBase.isInvalid()) |
| 4036 | return; |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4037 | QualType ConvertedBaseType = ConvertedBase.get()->getType(); |
| 4038 | |
| 4039 | enum CodeCompletionContext::Kind contextKind; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4040 | |
| 4041 | if (IsArrow) { |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4042 | if (const PointerType *Ptr = ConvertedBaseType->getAs<PointerType>()) |
| 4043 | ConvertedBaseType = Ptr->getPointeeType(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4044 | } |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4045 | |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4046 | if (IsArrow) { |
| 4047 | contextKind = CodeCompletionContext::CCC_ArrowMemberAccess; |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4048 | } else { |
| 4049 | if (ConvertedBaseType->isObjCObjectPointerType() || |
| 4050 | ConvertedBaseType->isObjCObjectOrInterfaceType()) { |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4051 | contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess; |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4052 | } else { |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4053 | contextKind = CodeCompletionContext::CCC_DotMemberAccess; |
| 4054 | } |
| 4055 | } |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 4056 | |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4057 | CodeCompletionContext CCContext(contextKind, ConvertedBaseType); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4058 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4059 | CodeCompleter->getCodeCompletionTUInfo(), CCContext, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4060 | &ResultBuilder::IsMember); |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4061 | |
| 4062 | auto DoCompletion = [&](Expr *Base, bool IsArrow, Optional<FixItHint> AccessOpFixIt) -> bool { |
| 4063 | if (!Base) |
| 4064 | return false; |
| 4065 | |
| 4066 | ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow); |
| 4067 | if (ConvertedBase.isInvalid()) |
| 4068 | return false; |
| 4069 | Base = ConvertedBase.get(); |
| 4070 | |
| 4071 | QualType BaseType = Base->getType(); |
| 4072 | |
| 4073 | if (IsArrow) { |
| 4074 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 4075 | BaseType = Ptr->getPointeeType(); |
| 4076 | else if (BaseType->isObjCObjectPointerType()) |
| 4077 | /*Do nothing*/; |
| 4078 | else |
| 4079 | return false; |
| 4080 | } |
| 4081 | |
| 4082 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
| 4083 | AddRecordMembersCompletionResults(*this, Results, S, BaseType, |
| 4084 | Record->getDecl(), |
| 4085 | std::move(AccessOpFixIt)); |
| 4086 | } else if (const auto *TST = |
| 4087 | BaseType->getAs<TemplateSpecializationType>()) { |
| 4088 | TemplateName TN = TST->getTemplateName(); |
| 4089 | if (const auto *TD = |
| 4090 | dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl())) { |
| 4091 | CXXRecordDecl *RD = TD->getTemplatedDecl(); |
| 4092 | AddRecordMembersCompletionResults(*this, Results, S, BaseType, RD, |
| 4093 | std::move(AccessOpFixIt)); |
| 4094 | } |
| 4095 | } else if (const auto *ICNT = BaseType->getAs<InjectedClassNameType>()) { |
| 4096 | if (auto *RD = ICNT->getDecl()) |
| 4097 | AddRecordMembersCompletionResults(*this, Results, S, BaseType, RD, |
| 4098 | std::move(AccessOpFixIt)); |
| 4099 | } else if (!IsArrow && BaseType->isObjCObjectPointerType()) { |
| 4100 | // Objective-C property reference. |
| 4101 | AddedPropertiesSet AddedProperties; |
| 4102 | |
| 4103 | if (const ObjCObjectPointerType *ObjCPtr = |
| 4104 | BaseType->getAsObjCInterfacePointerType()) { |
| 4105 | // Add property results based on our interface. |
| 4106 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!"); |
| 4107 | AddObjCProperties(CCContext, ObjCPtr->getInterfaceDecl(), true, |
| 4108 | /*AllowNullaryMethods=*/true, CurContext, |
| 4109 | AddedProperties, Results, IsBaseExprStatement); |
| 4110 | } |
| 4111 | |
| 4112 | // Add properties from the protocols in a qualified interface. |
| 4113 | for (auto *I : BaseType->getAs<ObjCObjectPointerType>()->quals()) |
| 4114 | AddObjCProperties(CCContext, I, true, /*AllowNullaryMethods=*/true, |
| 4115 | CurContext, AddedProperties, Results, |
| 4116 | IsBaseExprStatement); |
| 4117 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
| 4118 | (!IsArrow && BaseType->isObjCObjectType())) { |
| 4119 | // Objective-C instance variable access. |
| 4120 | ObjCInterfaceDecl *Class = nullptr; |
| 4121 | if (const ObjCObjectPointerType *ObjCPtr = |
| 4122 | BaseType->getAs<ObjCObjectPointerType>()) |
| 4123 | Class = ObjCPtr->getInterfaceDecl(); |
| 4124 | else |
| 4125 | Class = BaseType->getAs<ObjCObjectType>()->getInterface(); |
| 4126 | |
| 4127 | // Add all ivars from this class and its superclasses. |
| 4128 | if (Class) { |
| 4129 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4130 | Results.setFilter(&ResultBuilder::IsObjCIvar); |
| 4131 | LookupVisibleDecls( |
| 4132 | Class, LookupMemberName, Consumer, CodeCompleter->includeGlobals(), |
| 4133 | /*IncludeDependentBases=*/false, CodeCompleter->loadExternal()); |
| 4134 | } |
| 4135 | } |
| 4136 | |
| 4137 | // FIXME: How do we cope with isa? |
| 4138 | return true; |
| 4139 | }; |
| 4140 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4141 | Results.EnterNewScope(); |
Alex Lorenz | 06cfa99 | 2016-10-12 11:40:15 +0000 | [diff] [blame] | 4142 | |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4143 | bool CompletionSucceded = DoCompletion(Base, IsArrow, None); |
| 4144 | if (CodeCompleter->includeFixIts()) { |
| 4145 | const CharSourceRange OpRange = |
| 4146 | CharSourceRange::getTokenRange(OpLoc, OpLoc); |
| 4147 | CompletionSucceded |= DoCompletion( |
| 4148 | OtherOpBase, !IsArrow, |
| 4149 | FixItHint::CreateReplacement(OpRange, IsArrow ? "." : "->")); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4150 | } |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4151 | |
Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4152 | Results.ExitScope(); |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 4153 | |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4154 | if (!CompletionSucceded) |
| 4155 | return; |
| 4156 | |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 4157 | // Hand off the results found for code completion. |
Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4158 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4159 | Results.data(), Results.size()); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4162 | void Sema::CodeCompleteObjCClassPropertyRefExpr(Scope *S, |
| 4163 | IdentifierInfo &ClassName, |
| 4164 | SourceLocation ClassNameLoc, |
| 4165 | bool IsBaseExprStatement) { |
| 4166 | IdentifierInfo *ClassNamePtr = &ClassName; |
| 4167 | ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(ClassNamePtr, ClassNameLoc); |
| 4168 | if (!IFace) |
| 4169 | return; |
| 4170 | CodeCompletionContext CCContext( |
| 4171 | CodeCompletionContext::CCC_ObjCPropertyAccess); |
| 4172 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4173 | CodeCompleter->getCodeCompletionTUInfo(), CCContext, |
| 4174 | &ResultBuilder::IsMember); |
| 4175 | Results.EnterNewScope(); |
| 4176 | AddedPropertiesSet AddedProperties; |
| 4177 | AddObjCProperties(CCContext, IFace, true, |
| 4178 | /*AllowNullaryMethods=*/true, CurContext, AddedProperties, |
| 4179 | Results, IsBaseExprStatement, |
| 4180 | /*IsClassProperty=*/true); |
| 4181 | Results.ExitScope(); |
| 4182 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4183 | Results.data(), Results.size()); |
| 4184 | } |
| 4185 | |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 4186 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4187 | if (!CodeCompleter) |
| 4188 | return; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4189 | |
| 4190 | ResultBuilder::LookupFilter Filter = nullptr; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4191 | enum CodeCompletionContext::Kind ContextKind |
| 4192 | = CodeCompletionContext::CCC_Other; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4193 | switch ((DeclSpec::TST)TagSpec) { |
| 4194 | case DeclSpec::TST_enum: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4195 | Filter = &ResultBuilder::IsEnum; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4196 | ContextKind = CodeCompletionContext::CCC_EnumTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4197 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4198 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4199 | case DeclSpec::TST_union: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4200 | Filter = &ResultBuilder::IsUnion; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4201 | ContextKind = CodeCompletionContext::CCC_UnionTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4202 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4203 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4204 | case DeclSpec::TST_struct: |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4205 | case DeclSpec::TST_class: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4206 | case DeclSpec::TST_interface: |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4207 | Filter = &ResultBuilder::IsClassOrStruct; |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4208 | ContextKind = CodeCompletionContext::CCC_ClassOrStructTag; |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4209 | break; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4210 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4211 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4212 | llvm_unreachable("Unknown type specifier kind in CodeCompleteTag"); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4213 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4214 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4215 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4216 | CodeCompleter->getCodeCompletionTUInfo(), ContextKind); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4217 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 4218 | |
| 4219 | // First pass: look for tags. |
| 4220 | Results.setFilter(Filter); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4221 | LookupVisibleDecls(S, LookupTagName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4222 | CodeCompleter->includeGlobals(), |
| 4223 | CodeCompleter->loadExternal()); |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 4224 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4225 | if (CodeCompleter->includeGlobals()) { |
| 4226 | // Second pass: look for nested name specifiers. |
| 4227 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4228 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
| 4229 | CodeCompleter->includeGlobals(), |
| 4230 | CodeCompleter->loadExternal()); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4231 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4232 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4233 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4234 | Results.data(),Results.size()); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4235 | } |
| 4236 | |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4237 | static void AddTypeQualifierResults(DeclSpec &DS, ResultBuilder &Results, |
| 4238 | const LangOptions &LangOpts) { |
| 4239 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const)) |
| 4240 | Results.AddResult("const"); |
| 4241 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile)) |
| 4242 | Results.AddResult("volatile"); |
| 4243 | if (LangOpts.C99 && !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict)) |
| 4244 | Results.AddResult("restrict"); |
| 4245 | if (LangOpts.C11 && !(DS.getTypeQualifiers() & DeclSpec::TQ_atomic)) |
| 4246 | Results.AddResult("_Atomic"); |
| 4247 | if (LangOpts.MSVCCompat && !(DS.getTypeQualifiers() & DeclSpec::TQ_unaligned)) |
| 4248 | Results.AddResult("__unaligned"); |
| 4249 | } |
| 4250 | |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4251 | void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4252 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4253 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4254 | CodeCompletionContext::CCC_TypeQualifiers); |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4255 | Results.EnterNewScope(); |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4256 | AddTypeQualifierResults(DS, Results, LangOpts); |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4257 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4258 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4259 | Results.getCompletionContext(), |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4260 | Results.data(), Results.size()); |
| 4261 | } |
| 4262 | |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4263 | void Sema::CodeCompleteFunctionQualifiers(DeclSpec &DS, Declarator &D, |
| 4264 | const VirtSpecifiers *VS) { |
| 4265 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4266 | CodeCompleter->getCodeCompletionTUInfo(), |
| 4267 | CodeCompletionContext::CCC_TypeQualifiers); |
| 4268 | Results.EnterNewScope(); |
| 4269 | AddTypeQualifierResults(DS, Results, LangOpts); |
| 4270 | if (LangOpts.CPlusPlus11) { |
| 4271 | Results.AddResult("noexcept"); |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 4272 | if (D.getContext() == DeclaratorContext::MemberContext && |
| 4273 | !D.isCtorOrDtor() && !D.isStaticMember()) { |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4274 | if (!VS || !VS->isFinalSpecified()) |
| 4275 | Results.AddResult("final"); |
| 4276 | if (!VS || !VS->isOverrideSpecified()) |
| 4277 | Results.AddResult("override"); |
| 4278 | } |
| 4279 | } |
| 4280 | Results.ExitScope(); |
| 4281 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4282 | Results.data(), Results.size()); |
| 4283 | } |
| 4284 | |
Benjamin Kramer | 72dae62 | 2016-02-18 15:30:24 +0000 | [diff] [blame] | 4285 | void Sema::CodeCompleteBracketDeclarator(Scope *S) { |
| 4286 | CodeCompleteExpression(S, QualType(getASTContext().getSizeType())); |
| 4287 | } |
| 4288 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4289 | void Sema::CodeCompleteCase(Scope *S) { |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 4290 | if (getCurFunction()->SwitchStack.empty() || !CodeCompleter) |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4291 | return; |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 4292 | |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 4293 | SwitchStmt *Switch = getCurFunction()->SwitchStack.back(); |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 4294 | QualType type = Switch->getCond()->IgnoreImplicit()->getType(); |
| 4295 | if (!type->isEnumeralType()) { |
| 4296 | CodeCompleteExpressionData Data(type); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4297 | Data.IntegralConstantExpression = true; |
| 4298 | CodeCompleteExpression(S, Data); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4299 | return; |
Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 4300 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4301 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4302 | // Code-complete the cases of a switch statement over an enumeration type |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4303 | // by providing the list of |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 4304 | EnumDecl *Enum = type->castAs<EnumType>()->getDecl(); |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 4305 | if (EnumDecl *Def = Enum->getDefinition()) |
| 4306 | Enum = Def; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4307 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4308 | // Determine which enumerators we have already seen in the switch statement. |
| 4309 | // FIXME: Ideally, we would also be able to look *past* the code-completion |
| 4310 | // token, in case we are code-completing in the middle of the switch and not |
| 4311 | // at the end. However, we aren't able to do so at the moment. |
| 4312 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4313 | NestedNameSpecifier *Qualifier = nullptr; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4314 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4315 | SC = SC->getNextSwitchCase()) { |
| 4316 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); |
| 4317 | if (!Case) |
| 4318 | continue; |
| 4319 | |
| 4320 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); |
| 4321 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal)) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4322 | if (EnumConstantDecl *Enumerator |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4323 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4324 | // We look into the AST of the case statement to determine which |
| 4325 | // enumerator was named. Alternatively, we could compute the value of |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4326 | // the integral constant expression, then compare it against the |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4327 | // values of each enumerator. However, value-based approach would not |
| 4328 | // work as well with C++ templates where enumerators declared within a |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4329 | // template are type- and value-dependent. |
| 4330 | EnumeratorsSeen.insert(Enumerator); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4331 | |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4332 | // If this is a qualified-id, keep track of the nested-name-specifier |
| 4333 | // 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] | 4334 | // |
| 4335 | // switch (TagD.getKind()) { |
| 4336 | // case TagDecl::TK_enum: |
| 4337 | // break; |
| 4338 | // case XXX |
| 4339 | // |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4340 | // At the XXX, our completions are TagDecl::TK_union, |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4341 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, |
| 4342 | // TK_struct, and TK_class. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4343 | Qualifier = DRE->getQualifier(); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4344 | } |
| 4345 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4346 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4347 | if (getLangOpts().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4348 | // If there are no prior enumerators in C++, check whether we have to |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4349 | // qualify the names of the enumerators that we suggest, because they |
| 4350 | // may not be visible in this scope. |
Douglas Gregor | d3cebdb | 2012-02-01 05:02:47 +0000 | [diff] [blame] | 4351 | Qualifier = getRequiredQualification(Context, CurContext, Enum); |
Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4352 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4353 | |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4354 | // Add any enumerators that have not yet been mentioned. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4355 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4356 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4357 | CodeCompletionContext::CCC_Expression); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4358 | Results.EnterNewScope(); |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 4359 | for (auto *E : Enum->enumerators()) { |
| 4360 | if (EnumeratorsSeen.count(E)) |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4361 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4362 | |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 4363 | CodeCompletionResult R(E, CCP_EnumInCase, Qualifier); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4364 | Results.AddResult(R, CurContext, nullptr, false); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4365 | } |
| 4366 | Results.ExitScope(); |
Douglas Gregor | 28556092 | 2010-04-06 20:02:15 +0000 | [diff] [blame] | 4367 | |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4368 | if (CodeCompleter->includeMacros()) { |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 4369 | AddMacroResults(PP, Results, false); |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4370 | } |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 4371 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4372 | Results.data(), Results.size()); |
Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 4375 | static bool anyNullArguments(ArrayRef<Expr *> Args) { |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4376 | if (Args.size() && !Args.data()) |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 4377 | return true; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4378 | |
| 4379 | for (unsigned I = 0; I != Args.size(); ++I) |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 4380 | if (!Args[I]) |
| 4381 | return true; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4382 | |
Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 4383 | return false; |
| 4384 | } |
| 4385 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4386 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; |
| 4387 | |
Francisco Lopes da Silva | 8cafefa | 2015-01-29 05:54:59 +0000 | [diff] [blame] | 4388 | static void mergeCandidatesWithResults(Sema &SemaRef, |
| 4389 | SmallVectorImpl<ResultCandidate> &Results, |
| 4390 | OverloadCandidateSet &CandidateSet, |
| 4391 | SourceLocation Loc) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4392 | if (!CandidateSet.empty()) { |
| 4393 | // Sort the overload candidate set by placing the best overloads first. |
| 4394 | std::stable_sort( |
| 4395 | CandidateSet.begin(), CandidateSet.end(), |
| 4396 | [&](const OverloadCandidate &X, const OverloadCandidate &Y) { |
Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4397 | return isBetterOverloadCandidate(SemaRef, X, Y, Loc, |
| 4398 | CandidateSet.getKind()); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4399 | }); |
| 4400 | |
| 4401 | // Add the remaining viable overload candidates as code-completion results. |
Erik Verbruggen | 7ec9107 | 2017-09-08 10:23:08 +0000 | [diff] [blame] | 4402 | for (auto &Candidate : CandidateSet) { |
| 4403 | if (Candidate.Function && Candidate.Function->isDeleted()) |
| 4404 | continue; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4405 | if (Candidate.Viable) |
| 4406 | Results.push_back(ResultCandidate(Candidate.Function)); |
Erik Verbruggen | 7ec9107 | 2017-09-08 10:23:08 +0000 | [diff] [blame] | 4407 | } |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4408 | } |
| 4409 | } |
| 4410 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4411 | /// Get the type of the Nth parameter from a given set of overload |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4412 | /// candidates. |
Francisco Lopes da Silva | 8cafefa | 2015-01-29 05:54:59 +0000 | [diff] [blame] | 4413 | static QualType getParamType(Sema &SemaRef, |
| 4414 | ArrayRef<ResultCandidate> Candidates, |
| 4415 | unsigned N) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4416 | |
| 4417 | // Given the overloads 'Candidates' for a function call matching all arguments |
| 4418 | // up to N, return the type of the Nth parameter if it is the same for all |
| 4419 | // overload candidates. |
| 4420 | QualType ParamType; |
| 4421 | for (auto &Candidate : Candidates) { |
| 4422 | if (auto FType = Candidate.getFunctionType()) |
| 4423 | if (auto Proto = dyn_cast<FunctionProtoType>(FType)) |
| 4424 | if (N < Proto->getNumParams()) { |
| 4425 | if (ParamType.isNull()) |
| 4426 | ParamType = Proto->getParamType(N); |
| 4427 | else if (!SemaRef.Context.hasSameUnqualifiedType( |
| 4428 | ParamType.getNonReferenceType(), |
| 4429 | Proto->getParamType(N).getNonReferenceType())) |
| 4430 | // Otherwise return a default-constructed QualType. |
| 4431 | return QualType(); |
| 4432 | } |
| 4433 | } |
| 4434 | |
| 4435 | return ParamType; |
| 4436 | } |
| 4437 | |
Francisco Lopes da Silva | 8cafefa | 2015-01-29 05:54:59 +0000 | [diff] [blame] | 4438 | static void CodeCompleteOverloadResults(Sema &SemaRef, Scope *S, |
| 4439 | MutableArrayRef<ResultCandidate> Candidates, |
| 4440 | unsigned CurrentArg, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4441 | bool CompleteExpressionWithCurrentArg = true) { |
| 4442 | QualType ParamType; |
| 4443 | if (CompleteExpressionWithCurrentArg) |
| 4444 | ParamType = getParamType(SemaRef, Candidates, CurrentArg); |
| 4445 | |
| 4446 | if (ParamType.isNull()) |
| 4447 | SemaRef.CodeCompleteOrdinaryName(S, Sema::PCC_Expression); |
| 4448 | else |
| 4449 | SemaRef.CodeCompleteExpression(S, ParamType); |
| 4450 | |
| 4451 | if (!Candidates.empty()) |
| 4452 | SemaRef.CodeCompleter->ProcessOverloadCandidates(SemaRef, CurrentArg, |
| 4453 | Candidates.data(), |
| 4454 | Candidates.size()); |
| 4455 | } |
| 4456 | |
| 4457 | void Sema::CodeCompleteCall(Scope *S, Expr *Fn, ArrayRef<Expr *> Args) { |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4458 | if (!CodeCompleter) |
| 4459 | return; |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4460 | |
| 4461 | // When we're code-completing for a call, we fall back to ordinary |
| 4462 | // name code-completion whenever we can't produce specific |
| 4463 | // results. We may want to revisit this strategy in the future, |
| 4464 | // e.g., by merging the two kinds of results. |
| 4465 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4466 | // FIXME: Provide support for variadic template functions. |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4467 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4468 | // Ignore type-dependent call expressions entirely. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4469 | if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args) || |
| 4470 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4471 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4472 | return; |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4473 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4474 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4475 | // Build an overload candidate set based on the functions we find. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4476 | SourceLocation Loc = Fn->getExprLoc(); |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 4477 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4478 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4479 | SmallVector<ResultCandidate, 8> Results; |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 4480 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4481 | Expr *NakedFn = Fn->IgnoreParenCasts(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4482 | if (auto ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4483 | AddOverloadedCallCandidates(ULE, Args, CandidateSet, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4484 | /*PartialOverloading=*/true); |
| 4485 | else if (auto UME = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 4486 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
| 4487 | if (UME->hasExplicitTemplateArgs()) { |
| 4488 | UME->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 4489 | TemplateArgs = &TemplateArgsBuffer; |
Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 4490 | } |
Erik Verbruggen | f1898cf | 2017-03-28 07:22:21 +0000 | [diff] [blame] | 4491 | |
| 4492 | // Add the base as first argument (use a nullptr if the base is implicit). |
| 4493 | SmallVector<Expr *, 12> ArgExprs( |
| 4494 | 1, UME->isImplicitAccess() ? nullptr : UME->getBase()); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4495 | ArgExprs.append(Args.begin(), Args.end()); |
| 4496 | UnresolvedSet<8> Decls; |
| 4497 | Decls.append(UME->decls_begin(), UME->decls_end()); |
Benjamin Kramer | e3962ae | 2017-10-26 08:41:28 +0000 | [diff] [blame] | 4498 | const bool FirstArgumentIsBase = !UME->isImplicitAccess() && UME->getBase(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4499 | AddFunctionCandidates(Decls, ArgExprs, CandidateSet, TemplateArgs, |
| 4500 | /*SuppressUsedConversions=*/false, |
Benjamin Kramer | e3962ae | 2017-10-26 08:41:28 +0000 | [diff] [blame] | 4501 | /*PartialOverloading=*/true, |
| 4502 | FirstArgumentIsBase); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4503 | } else { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4504 | FunctionDecl *FD = nullptr; |
| 4505 | if (auto MCE = dyn_cast<MemberExpr>(NakedFn)) |
| 4506 | FD = dyn_cast<FunctionDecl>(MCE->getMemberDecl()); |
| 4507 | else if (auto DRE = dyn_cast<DeclRefExpr>(NakedFn)) |
| 4508 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4509 | if (FD) { // We check whether it's a resolved function declaration. |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 4510 | if (!getLangOpts().CPlusPlus || |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4511 | !FD->getType()->getAs<FunctionProtoType>()) |
| 4512 | Results.push_back(ResultCandidate(FD)); |
| 4513 | else |
| 4514 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, FD->getAccess()), |
| 4515 | Args, CandidateSet, |
| 4516 | /*SuppressUsedConversions=*/false, |
| 4517 | /*PartialOverloading=*/true); |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4518 | |
| 4519 | } else if (auto DC = NakedFn->getType()->getAsCXXRecordDecl()) { |
| 4520 | // If expression's type is CXXRecordDecl, it may overload the function |
| 4521 | // call operator, so we check if it does and add them as candidates. |
Francisco Lopes da Silva | a349a8a | 2015-01-25 08:47:59 +0000 | [diff] [blame] | 4522 | // A complete type is needed to lookup for member function call operators. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4523 | if (isCompleteType(Loc, NakedFn->getType())) { |
Francisco Lopes da Silva | a349a8a | 2015-01-25 08:47:59 +0000 | [diff] [blame] | 4524 | DeclarationName OpName = Context.DeclarationNames |
| 4525 | .getCXXOperatorName(OO_Call); |
| 4526 | LookupResult R(*this, OpName, Loc, LookupOrdinaryName); |
| 4527 | LookupQualifiedName(R, DC); |
| 4528 | R.suppressDiagnostics(); |
| 4529 | SmallVector<Expr *, 12> ArgExprs(1, NakedFn); |
| 4530 | ArgExprs.append(Args.begin(), Args.end()); |
| 4531 | AddFunctionCandidates(R.asUnresolvedSet(), ArgExprs, CandidateSet, |
| 4532 | /*ExplicitArgs=*/nullptr, |
| 4533 | /*SuppressUsedConversions=*/false, |
| 4534 | /*PartialOverloading=*/true); |
| 4535 | } |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4536 | } else { |
| 4537 | // Lastly we check whether expression's type is function pointer or |
| 4538 | // function. |
| 4539 | QualType T = NakedFn->getType(); |
| 4540 | if (!T->getPointeeType().isNull()) |
| 4541 | T = T->getPointeeType(); |
| 4542 | |
| 4543 | if (auto FP = T->getAs<FunctionProtoType>()) { |
| 4544 | if (!TooManyArguments(FP->getNumParams(), Args.size(), |
Francisco Lopes da Silva | 62a9a4f | 2015-01-23 13:17:51 +0000 | [diff] [blame] | 4545 | /*PartialOverloading=*/true) || |
| 4546 | FP->isVariadic()) |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4547 | Results.push_back(ResultCandidate(FP)); |
| 4548 | } else if (auto FT = T->getAs<FunctionType>()) |
Francisco Lopes da Silva | 62a9a4f | 2015-01-23 13:17:51 +0000 | [diff] [blame] | 4549 | // No prototype and declaration, it may be a K & R style function. |
Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4550 | Results.push_back(ResultCandidate(FT)); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4551 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4552 | } |
Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4553 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4554 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); |
| 4555 | CodeCompleteOverloadResults(*this, S, Results, Args.size(), |
| 4556 | !CandidateSet.empty()); |
| 4557 | } |
| 4558 | |
| 4559 | void Sema::CodeCompleteConstructor(Scope *S, QualType Type, SourceLocation Loc, |
| 4560 | ArrayRef<Expr *> Args) { |
| 4561 | if (!CodeCompleter) |
| 4562 | return; |
| 4563 | |
| 4564 | // A complete type is needed to lookup for constructors. |
Ilya Biryukov | fb9dde7 | 2018-05-14 13:50:36 +0000 | [diff] [blame] | 4565 | CXXRecordDecl *RD = |
| 4566 | isCompleteType(Loc, Type) ? Type->getAsCXXRecordDecl() : nullptr; |
Argyrios Kyrtzidis | ee1d76f | 2015-03-13 07:39:30 +0000 | [diff] [blame] | 4567 | if (!RD) { |
| 4568 | CodeCompleteExpression(S, Type); |
| 4569 | return; |
| 4570 | } |
| 4571 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4572 | // FIXME: Provide support for member initializers. |
| 4573 | // FIXME: Provide support for variadic template constructors. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4574 | |
| 4575 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
| 4576 | |
Argyrios Kyrtzidis | ee1d76f | 2015-03-13 07:39:30 +0000 | [diff] [blame] | 4577 | for (auto C : LookupConstructors(RD)) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4578 | if (auto FD = dyn_cast<FunctionDecl>(C)) { |
| 4579 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, C->getAccess()), |
| 4580 | Args, CandidateSet, |
| 4581 | /*SuppressUsedConversions=*/false, |
| 4582 | /*PartialOverloading=*/true); |
| 4583 | } else if (auto FTD = dyn_cast<FunctionTemplateDecl>(C)) { |
| 4584 | AddTemplateOverloadCandidate(FTD, |
| 4585 | DeclAccessPair::make(FTD, C->getAccess()), |
| 4586 | /*ExplicitTemplateArgs=*/nullptr, |
| 4587 | Args, CandidateSet, |
| 4588 | /*SuppressUsedConversions=*/false, |
| 4589 | /*PartialOverloading=*/true); |
| 4590 | } |
| 4591 | } |
| 4592 | |
| 4593 | SmallVector<ResultCandidate, 8> Results; |
| 4594 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); |
| 4595 | CodeCompleteOverloadResults(*this, S, Results, Args.size()); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4598 | void Sema::CodeCompleteInitializer(Scope *S, Decl *D) { |
| 4599 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4600 | if (!VD) { |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4601 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4602 | return; |
| 4603 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4604 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4605 | CodeCompleteExpression(S, VD->getType()); |
| 4606 | } |
| 4607 | |
| 4608 | void Sema::CodeCompleteReturn(Scope *S) { |
| 4609 | QualType ResultType; |
| 4610 | if (isa<BlockDecl>(CurContext)) { |
| 4611 | if (BlockScopeInfo *BSI = getCurBlock()) |
| 4612 | ResultType = BSI->ReturnType; |
| 4613 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4614 | ResultType = Function->getReturnType(); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4615 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4616 | ResultType = Method->getReturnType(); |
| 4617 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4618 | if (ResultType.isNull()) |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4619 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4620 | else |
| 4621 | CodeCompleteExpression(S, ResultType); |
| 4622 | } |
| 4623 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4624 | void Sema::CodeCompleteAfterIf(Scope *S) { |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4625 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4626 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4627 | mapCodeCompletionContext(*this, PCC_Statement)); |
| 4628 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 4629 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4630 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4631 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4632 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4633 | CodeCompleter->includeGlobals(), |
| 4634 | CodeCompleter->loadExternal()); |
| 4635 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4636 | AddOrdinaryNameResults(PCC_Statement, S, *this, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4637 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4638 | // "else" block |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4639 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4640 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4641 | Builder.AddTypedTextChunk("else"); |
Douglas Gregor | 3a5d6c2 | 2012-02-16 17:49:04 +0000 | [diff] [blame] | 4642 | if (Results.includeCodePatterns()) { |
| 4643 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4644 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4645 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4646 | Builder.AddPlaceholderChunk("statements"); |
| 4647 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4648 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4649 | } |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4650 | Results.AddResult(Builder.TakeString()); |
| 4651 | |
| 4652 | // "else if" block |
| 4653 | Builder.AddTypedTextChunk("else"); |
| 4654 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4655 | Builder.AddTextChunk("if"); |
| 4656 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4657 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4658 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4659 | Builder.AddPlaceholderChunk("condition"); |
| 4660 | else |
| 4661 | Builder.AddPlaceholderChunk("expression"); |
| 4662 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | 3a5d6c2 | 2012-02-16 17:49:04 +0000 | [diff] [blame] | 4663 | if (Results.includeCodePatterns()) { |
| 4664 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4665 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4666 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4667 | Builder.AddPlaceholderChunk("statements"); |
| 4668 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4669 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4670 | } |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4671 | Results.AddResult(Builder.TakeString()); |
| 4672 | |
| 4673 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4674 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4675 | if (S->getFnParent()) |
Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 4676 | AddPrettyFunctionResults(getLangOpts(), Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4677 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4678 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 4679 | AddMacroResults(PP, Results, false); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4680 | |
Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4681 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4682 | Results.data(),Results.size()); |
| 4683 | } |
| 4684 | |
Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 4685 | void Sema::CodeCompleteAssignmentRHS(Scope *S, Expr *LHS) { |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4686 | if (LHS) |
| 4687 | CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType()); |
| 4688 | else |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4689 | CodeCompleteOrdinaryName(S, PCC_Expression); |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4690 | } |
| 4691 | |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4692 | void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4693 | bool EnteringContext) { |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 4694 | if (SS.isEmpty() || !CodeCompleter) |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4695 | return; |
Alex Lorenz | 8a7a4cf | 2017-06-15 21:40:54 +0000 | [diff] [blame] | 4696 | |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 4697 | // We want to keep the scope specifier even if it's invalid (e.g. the scope |
| 4698 | // "a::b::" is not corresponding to any context/namespace in the AST), since |
| 4699 | // it can be useful for global code completion which have information about |
| 4700 | // contexts/symbols that are not in the AST. |
| 4701 | if (SS.isInvalid()) { |
| 4702 | CodeCompletionContext CC(CodeCompletionContext::CCC_Name); |
| 4703 | CC.setCXXScopeSpecifier(SS); |
| 4704 | HandleCodeCompleteResults(this, CodeCompleter, CC, nullptr, 0); |
| 4705 | return; |
| 4706 | } |
Alex Lorenz | 8a7a4cf | 2017-06-15 21:40:54 +0000 | [diff] [blame] | 4707 | // Always pretend to enter a context to ensure that a dependent type |
| 4708 | // resolves to a dependent record. |
| 4709 | DeclContext *Ctx = computeDeclContext(SS, /*EnteringContext=*/true); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4710 | if (!Ctx) |
| 4711 | return; |
Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 4712 | |
| 4713 | // Try to instantiate any non-dependent declaration contexts before |
| 4714 | // we look in them. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4715 | if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx)) |
Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 4716 | return; |
| 4717 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4718 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4719 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4720 | CodeCompletionContext::CCC_Name); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4721 | Results.EnterNewScope(); |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 4722 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4723 | // The "template" keyword can follow "::" in the grammar, but only |
| 4724 | // put it into the grammar if the nested-name-specifier is dependent. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4725 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4726 | if (!Results.empty() && NNS->isDependent()) |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4727 | Results.AddResult("template"); |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4728 | |
| 4729 | // Add calls to overridden virtual functions, if there are any. |
| 4730 | // |
| 4731 | // FIXME: This isn't wonderful, because we don't know whether we're actually |
| 4732 | // in a context that permits expressions. This is a general issue with |
| 4733 | // qualified-id completions. |
| 4734 | if (!EnteringContext) |
| 4735 | MaybeAddOverrideCalls(*this, Ctx, Results); |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 4736 | Results.ExitScope(); |
| 4737 | |
Eric Liu | fead6ae | 2017-12-13 10:26:49 +0000 | [diff] [blame] | 4738 | if (CodeCompleter->includeNamespaceLevelDecls() || |
| 4739 | (!Ctx->isNamespace() && !Ctx->isTranslationUnit())) { |
| 4740 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4741 | LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer, |
| 4742 | /*IncludeGlobalScope=*/true, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4743 | /*IncludeDependentBases=*/true, |
| 4744 | CodeCompleter->loadExternal()); |
Eric Liu | fead6ae | 2017-12-13 10:26:49 +0000 | [diff] [blame] | 4745 | } |
Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4746 | |
Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 4747 | auto CC = Results.getCompletionContext(); |
| 4748 | CC.setCXXScopeSpecifier(SS); |
| 4749 | |
| 4750 | HandleCodeCompleteResults(this, CodeCompleter, CC, Results.data(), |
| 4751 | Results.size()); |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4752 | } |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4753 | |
| 4754 | void Sema::CodeCompleteUsing(Scope *S) { |
| 4755 | if (!CodeCompleter) |
| 4756 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4757 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4758 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4759 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4760 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
| 4761 | &ResultBuilder::IsNestedNameSpecifier); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4762 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4763 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4764 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 4765 | if (!S->isClassScope()) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4766 | Results.AddResult(CodeCompletionResult("namespace")); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4767 | |
| 4768 | // After "using", we can see anything that would start a |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4769 | // nested-name-specifier. |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4770 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4771 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4772 | CodeCompleter->includeGlobals(), |
| 4773 | CodeCompleter->loadExternal()); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4774 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 4775 | |
| 4776 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4777 | Results.data(), Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4778 | } |
| 4779 | |
| 4780 | void Sema::CodeCompleteUsingDirective(Scope *S) { |
| 4781 | if (!CodeCompleter) |
| 4782 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4783 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4784 | // After "using namespace", we expect to see a namespace name or namespace |
| 4785 | // alias. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4786 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4787 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4788 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4789 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4790 | Results.EnterNewScope(); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4791 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4792 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4793 | CodeCompleter->includeGlobals(), |
| 4794 | CodeCompleter->loadExternal()); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4795 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 4796 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4797 | Results.data(), Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4798 | } |
| 4799 | |
| 4800 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { |
| 4801 | if (!CodeCompleter) |
| 4802 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4803 | |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 4804 | DeclContext *Ctx = S->getEntity(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4805 | if (!S->getParent()) |
| 4806 | Ctx = Context.getTranslationUnitDecl(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4807 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4808 | bool SuppressedGlobalResults |
| 4809 | = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4810 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4811 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4812 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4813 | SuppressedGlobalResults |
| 4814 | ? CodeCompletionContext::CCC_Namespace |
| 4815 | : CodeCompletionContext::CCC_Other, |
| 4816 | &ResultBuilder::IsNamespace); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4817 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4818 | if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) { |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4819 | // We only want to see those namespaces that have already been defined |
| 4820 | // within this scope, because its likely that the user is creating an |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4821 | // extended namespace declaration. Keep track of the most recent |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4822 | // definition of each namespace. |
| 4823 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4824 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4825 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); |
| 4826 | NS != NSEnd; ++NS) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4827 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4828 | |
| 4829 | // Add the most recent definition (or extended definition) of each |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4830 | // namespace to the list of results. |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4831 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4832 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 4833 | NS = OrigToLatest.begin(), |
| 4834 | NSEnd = OrigToLatest.end(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4835 | NS != NSEnd; ++NS) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 4836 | Results.AddResult(CodeCompletionResult( |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4837 | NS->second, Results.getBasePriority(NS->second), |
| 4838 | nullptr), |
| 4839 | CurContext, nullptr, false); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4840 | Results.ExitScope(); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4841 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4842 | |
| 4843 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4844 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4845 | Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4846 | } |
| 4847 | |
| 4848 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { |
| 4849 | if (!CodeCompleter) |
| 4850 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4851 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4852 | // After "namespace", we expect to see a namespace or alias. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4853 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4854 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4855 | CodeCompletionContext::CCC_Namespace, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4856 | &ResultBuilder::IsNamespaceOrAlias); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4857 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4858 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4859 | CodeCompleter->includeGlobals(), |
| 4860 | CodeCompleter->loadExternal()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4861 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4862 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4863 | Results.data(),Results.size()); |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4864 | } |
| 4865 | |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 4866 | void Sema::CodeCompleteOperatorName(Scope *S) { |
| 4867 | if (!CodeCompleter) |
| 4868 | return; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4869 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4870 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4871 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4872 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4873 | CodeCompletionContext::CCC_Type, |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4874 | &ResultBuilder::IsType); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4875 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4876 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4877 | // Add the names of overloadable operators. |
| 4878 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 4879 | if (std::strcmp(Spelling, "?")) \ |
Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4880 | Results.AddResult(Result(Spelling)); |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4881 | #include "clang/Basic/OperatorKinds.def" |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4882 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4883 | // Add any type names visible from the current scope |
Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 4884 | Results.allowNestedNameSpecifiers(); |
Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4885 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4886 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4887 | CodeCompleter->includeGlobals(), |
| 4888 | CodeCompleter->loadExternal()); |
| 4889 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4890 | // Add any type specifiers |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4891 | AddTypeSpecifierResults(getLangOpts(), Results); |
Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4892 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 4893 | |
| 4894 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4895 | Results.data(), Results.size()); |
Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 4896 | } |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4897 | |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4898 | void Sema::CodeCompleteConstructorInitializer( |
| 4899 | Decl *ConstructorD, |
| 4900 | ArrayRef <CXXCtorInitializer *> Initializers) { |
Benjamin Kramer | a4f8df0 | 2015-07-09 15:31:10 +0000 | [diff] [blame] | 4901 | if (!ConstructorD) |
| 4902 | return; |
| 4903 | |
| 4904 | AdjustDeclIfTemplate(ConstructorD); |
| 4905 | |
| 4906 | CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4907 | if (!Constructor) |
| 4908 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4909 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4910 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4911 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4912 | CodeCompletionContext::CCC_PotentiallyQualifiedName); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4913 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4914 | |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4915 | // Fill in any already-initialized fields or base classes. |
| 4916 | llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields; |
| 4917 | llvm::SmallPtrSet<CanQualType, 4> InitializedBases; |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4918 | for (unsigned I = 0, E = Initializers.size(); I != E; ++I) { |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4919 | if (Initializers[I]->isBaseInitializer()) |
| 4920 | InitializedBases.insert( |
| 4921 | Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0))); |
| 4922 | else |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4923 | InitializedFields.insert(cast<FieldDecl>( |
| 4924 | Initializers[I]->getAnyMember())); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4925 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4926 | |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4927 | // Add completions for base classes. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4928 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4929 | Results.getCodeCompletionTUInfo()); |
Benjamin Kramer | a4f8df0 | 2015-07-09 15:31:10 +0000 | [diff] [blame] | 4930 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4931 | bool SawLastInitializer = Initializers.empty(); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4932 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4933 | for (const auto &Base : ClassDecl->bases()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4934 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) |
| 4935 | .second) { |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4936 | SawLastInitializer |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4937 | = !Initializers.empty() && |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4938 | Initializers.back()->isBaseInitializer() && |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4939 | Context.hasSameUnqualifiedType(Base.getType(), |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4940 | QualType(Initializers.back()->getBaseClass(), 0)); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4941 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4942 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4943 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4944 | Builder.AddTypedTextChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4945 | Results.getAllocator().CopyString( |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4946 | Base.getType().getAsString(Policy))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4947 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4948 | Builder.AddPlaceholderChunk("args"); |
| 4949 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4950 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4951 | SawLastInitializer? CCP_NextInitializer |
| 4952 | : CCP_MemberDeclaration)); |
| 4953 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4954 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4955 | |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4956 | // Add completions for virtual base classes. |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4957 | for (const auto &Base : ClassDecl->vbases()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4958 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) |
| 4959 | .second) { |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4960 | SawLastInitializer |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4961 | = !Initializers.empty() && |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4962 | Initializers.back()->isBaseInitializer() && |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4963 | Context.hasSameUnqualifiedType(Base.getType(), |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4964 | QualType(Initializers.back()->getBaseClass(), 0)); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4965 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4966 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4967 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4968 | Builder.AddTypedTextChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4969 | Builder.getAllocator().CopyString( |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4970 | Base.getType().getAsString(Policy))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4971 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4972 | Builder.AddPlaceholderChunk("args"); |
| 4973 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4974 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4975 | SawLastInitializer? CCP_NextInitializer |
| 4976 | : CCP_MemberDeclaration)); |
| 4977 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4978 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4979 | |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4980 | // Add completions for members. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4981 | for (auto *Field : ClassDecl->fields()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4982 | if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl())) |
| 4983 | .second) { |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4984 | SawLastInitializer |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4985 | = !Initializers.empty() && |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4986 | Initializers.back()->isAnyMemberInitializer() && |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4987 | Initializers.back()->getAnyMember() == Field; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4988 | continue; |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4989 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4990 | |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4991 | if (!Field->getDeclName()) |
| 4992 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4993 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4994 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4995 | Field->getIdentifier()->getName())); |
| 4996 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4997 | Builder.AddPlaceholderChunk("args"); |
| 4998 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4999 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5000 | SawLastInitializer? CCP_NextInitializer |
Douglas Gregor | f3af311 | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 5001 | : CCP_MemberDeclaration, |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 5002 | CXCursor_MemberRef, |
| 5003 | CXAvailability_Available, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5004 | Field)); |
Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5005 | SawLastInitializer = false; |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5006 | } |
| 5007 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5008 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5009 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5010 | Results.data(), Results.size()); |
| 5011 | } |
| 5012 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5013 | /// Determine whether this scope denotes a namespace. |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5014 | static bool isNamespaceScope(Scope *S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5015 | DeclContext *DC = S->getEntity(); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5016 | if (!DC) |
| 5017 | return false; |
| 5018 | |
| 5019 | return DC->isFileContext(); |
| 5020 | } |
| 5021 | |
| 5022 | void Sema::CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro, |
| 5023 | bool AfterAmpersand) { |
| 5024 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5025 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5026 | CodeCompletionContext::CCC_Other); |
| 5027 | Results.EnterNewScope(); |
| 5028 | |
| 5029 | // Note what has already been captured. |
| 5030 | llvm::SmallPtrSet<IdentifierInfo *, 4> Known; |
| 5031 | bool IncludedThis = false; |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 5032 | for (const auto &C : Intro.Captures) { |
| 5033 | if (C.Kind == LCK_This) { |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5034 | IncludedThis = true; |
| 5035 | continue; |
| 5036 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5037 | |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 5038 | Known.insert(C.Id); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5039 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5040 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5041 | // Look for other capturable variables. |
| 5042 | for (; S && !isNamespaceScope(S); S = S->getParent()) { |
Aaron Ballman | 35c5495 | 2014-03-17 16:55:25 +0000 | [diff] [blame] | 5043 | for (const auto *D : S->decls()) { |
| 5044 | const auto *Var = dyn_cast<VarDecl>(D); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5045 | if (!Var || |
| 5046 | !Var->hasLocalStorage() || |
| 5047 | Var->hasAttr<BlocksAttr>()) |
| 5048 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5049 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 5050 | if (Known.insert(Var->getIdentifier()).second) |
Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 5051 | Results.AddResult(CodeCompletionResult(Var, CCP_LocalDeclaration), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5052 | CurContext, nullptr, false); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5053 | } |
| 5054 | } |
| 5055 | |
| 5056 | // Add 'this', if it would be valid. |
| 5057 | if (!IncludedThis && !AfterAmpersand && Intro.Default != LCD_ByCopy) |
| 5058 | addThisCompletion(*this, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5059 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5060 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5061 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5062 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5063 | Results.data(), Results.size()); |
| 5064 | } |
| 5065 | |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5066 | /// Macro that optionally prepends an "@" to the string literal passed in via |
| 5067 | /// Keyword, depending on whether NeedAt is true or false. |
| 5068 | #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) ((NeedAt)? "@" Keyword : Keyword) |
| 5069 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5070 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5071 | ResultBuilder &Results, |
| 5072 | bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5073 | typedef CodeCompletionResult Result; |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5074 | // Since we have an implementation, we can end it. |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5075 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end"))); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5076 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5077 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5078 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5079 | if (LangOpts.ObjC2) { |
| 5080 | // @dynamic |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5081 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"dynamic")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5082 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5083 | Builder.AddPlaceholderChunk("property"); |
| 5084 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5085 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5086 | // @synthesize |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5087 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synthesize")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5088 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5089 | Builder.AddPlaceholderChunk("property"); |
| 5090 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5091 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5092 | } |
| 5093 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5094 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5095 | ResultBuilder &Results, |
| 5096 | bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5097 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5098 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5099 | // Since we have an interface or protocol, we can end it. |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5100 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end"))); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5101 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5102 | if (LangOpts.ObjC2) { |
| 5103 | // @property |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5104 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"property"))); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5105 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5106 | // @required |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5107 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"required"))); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5108 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5109 | // @optional |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5110 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"optional"))); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5111 | } |
| 5112 | } |
| 5113 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5114 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5115 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5116 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5117 | Results.getCodeCompletionTUInfo()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5118 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5119 | // @class name ; |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5120 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"class")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5121 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5122 | Builder.AddPlaceholderChunk("name"); |
| 5123 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5124 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5125 | if (Results.includeCodePatterns()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5126 | // @interface name |
| 5127 | // FIXME: Could introduce the whole pattern, including superclasses and |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5128 | // such. |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5129 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"interface")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5130 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5131 | Builder.AddPlaceholderChunk("class"); |
| 5132 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5133 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5134 | // @protocol name |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5135 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5136 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5137 | Builder.AddPlaceholderChunk("protocol"); |
| 5138 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5139 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5140 | // @implementation name |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5141 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"implementation")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5142 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5143 | Builder.AddPlaceholderChunk("class"); |
| 5144 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5145 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5146 | |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5147 | // @compatibility_alias name |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5148 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"compatibility_alias")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5149 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5150 | Builder.AddPlaceholderChunk("alias"); |
| 5151 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5152 | Builder.AddPlaceholderChunk("class"); |
| 5153 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | 61e3681 | 2013-03-07 23:26:24 +0000 | [diff] [blame] | 5154 | |
| 5155 | if (Results.getSema().getLangOpts().Modules) { |
| 5156 | // @import name |
| 5157 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "import")); |
| 5158 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5159 | Builder.AddPlaceholderChunk("module"); |
| 5160 | Results.AddResult(Result(Builder.TakeString())); |
| 5161 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5162 | } |
| 5163 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5164 | void Sema::CodeCompleteObjCAtDirective(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5165 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5166 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5167 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 5168 | Results.EnterNewScope(); |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5169 | if (isa<ObjCImplDecl>(CurContext)) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5170 | AddObjCImplementationResults(getLangOpts(), Results, false); |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5171 | else if (CurContext->isObjCContainer()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5172 | AddObjCInterfaceResults(getLangOpts(), Results, false); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5173 | else |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5174 | AddObjCTopLevelResults(Results, false); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 5175 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5176 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5177 | Results.data(), Results.size()); |
Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 5178 | } |
| 5179 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5180 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5181 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5182 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5183 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5184 | |
| 5185 | // @encode ( type-name ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5186 | const char *EncodeType = "char[]"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5187 | if (Results.getSema().getLangOpts().CPlusPlus || |
| 5188 | Results.getSema().getLangOpts().ConstStrings) |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5189 | EncodeType = "const char[]"; |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5190 | Builder.AddResultTypeChunk(EncodeType); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5191 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"encode")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5192 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5193 | Builder.AddPlaceholderChunk("type-name"); |
| 5194 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5195 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5196 | |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5197 | // @protocol ( protocol-name ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5198 | Builder.AddResultTypeChunk("Protocol *"); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5199 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5200 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5201 | Builder.AddPlaceholderChunk("protocol-name"); |
| 5202 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5203 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5204 | |
| 5205 | // @selector ( selector ) |
Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5206 | Builder.AddResultTypeChunk("SEL"); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5207 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"selector")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5208 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5209 | Builder.AddPlaceholderChunk("selector"); |
| 5210 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5211 | Results.AddResult(Result(Builder.TakeString())); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5212 | |
| 5213 | // @"string" |
| 5214 | Builder.AddResultTypeChunk("NSString *"); |
| 5215 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"\"")); |
| 5216 | Builder.AddPlaceholderChunk("string"); |
| 5217 | Builder.AddTextChunk("\""); |
| 5218 | Results.AddResult(Result(Builder.TakeString())); |
| 5219 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 5220 | // @[objects, ...] |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5221 | Builder.AddResultTypeChunk("NSArray *"); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5222 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"[")); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5223 | Builder.AddPlaceholderChunk("objects, ..."); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5224 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 5225 | Results.AddResult(Result(Builder.TakeString())); |
| 5226 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 5227 | // @{key : object, ...} |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5228 | Builder.AddResultTypeChunk("NSDictionary *"); |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5229 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"{")); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5230 | Builder.AddPlaceholderChunk("key"); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5231 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 5232 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5233 | Builder.AddPlaceholderChunk("object, ..."); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5234 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5235 | Results.AddResult(Result(Builder.TakeString())); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5236 | |
Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 5237 | // @(expression) |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5238 | Builder.AddResultTypeChunk("id"); |
| 5239 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "(")); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5240 | Builder.AddPlaceholderChunk("expression"); |
Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5241 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5242 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5243 | } |
| 5244 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5245 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5246 | typedef CodeCompletionResult Result; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5247 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5248 | Results.getCodeCompletionTUInfo()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5249 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5250 | if (Results.includeCodePatterns()) { |
| 5251 | // @try { statements } @catch ( declaration ) { statements } @finally |
| 5252 | // { statements } |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5253 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"try")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5254 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5255 | Builder.AddPlaceholderChunk("statements"); |
| 5256 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5257 | Builder.AddTextChunk("@catch"); |
| 5258 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5259 | Builder.AddPlaceholderChunk("parameter"); |
| 5260 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5261 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5262 | Builder.AddPlaceholderChunk("statements"); |
| 5263 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5264 | Builder.AddTextChunk("@finally"); |
| 5265 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5266 | Builder.AddPlaceholderChunk("statements"); |
| 5267 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5268 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5269 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5270 | |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5271 | // @throw |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5272 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"throw")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5273 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5274 | Builder.AddPlaceholderChunk("expression"); |
| 5275 | Results.AddResult(Result(Builder.TakeString())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5276 | |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5277 | if (Results.includeCodePatterns()) { |
| 5278 | // @synchronized ( expression ) { statements } |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5279 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synchronized")); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5280 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5281 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5282 | Builder.AddPlaceholderChunk("expression"); |
| 5283 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5284 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5285 | Builder.AddPlaceholderChunk("statements"); |
| 5286 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5287 | Results.AddResult(Result(Builder.TakeString())); |
Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5288 | } |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5289 | } |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5290 | |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5291 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5292 | ResultBuilder &Results, |
| 5293 | bool NeedAt) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5294 | typedef CodeCompletionResult Result; |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5295 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"private"))); |
| 5296 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"protected"))); |
| 5297 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"public"))); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5298 | if (LangOpts.ObjC2) |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5299 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"package"))); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5300 | } |
| 5301 | |
| 5302 | void Sema::CodeCompleteObjCAtVisibility(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5303 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5304 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5305 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5306 | Results.EnterNewScope(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5307 | AddObjCVisibilityResults(getLangOpts(), Results, false); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5308 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5309 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5310 | Results.data(), Results.size()); |
Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5311 | } |
| 5312 | |
| 5313 | void Sema::CodeCompleteObjCAtStatement(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5314 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5315 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5316 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5317 | Results.EnterNewScope(); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5318 | AddObjCStatementResults(Results, false); |
| 5319 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5320 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5321 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5322 | Results.data(), Results.size()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5323 | } |
| 5324 | |
| 5325 | void Sema::CodeCompleteObjCAtExpression(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5326 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5327 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5328 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5329 | Results.EnterNewScope(); |
Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5330 | AddObjCExpressionResults(Results, false); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5331 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5332 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5333 | Results.data(), Results.size()); |
Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5334 | } |
| 5335 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5336 | /// Determine whether the addition of the given flag to an Objective-C |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5337 | /// property's attributes will cause a conflict. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5338 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5339 | // Check if we've already added this flag. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5340 | if (Attributes & NewFlag) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5341 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5342 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5343 | Attributes |= NewFlag; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5344 | |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5345 | // Check for collisions with "readonly". |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5346 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 5347 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5348 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5349 | |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5350 | // Check for more than one of { assign, copy, retain, strong, weak }. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5351 | unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5352 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5353 | ObjCDeclSpec::DQ_PR_copy | |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5354 | ObjCDeclSpec::DQ_PR_retain | |
| 5355 | ObjCDeclSpec::DQ_PR_strong | |
| 5356 | ObjCDeclSpec::DQ_PR_weak); |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5357 | if (AssignCopyRetMask && |
| 5358 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5359 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained && |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5360 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5361 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain && |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5362 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong && |
| 5363 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_weak) |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5364 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5365 | |
Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5366 | return false; |
| 5367 | } |
| 5368 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5369 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5370 | if (!CodeCompleter) |
| 5371 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5372 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5373 | unsigned Attributes = ODS.getPropertyAttributes(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5374 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5375 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5376 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5377 | CodeCompletionContext::CCC_Other); |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5378 | Results.EnterNewScope(); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5379 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5380 | Results.AddResult(CodeCompletionResult("readonly")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5381 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5382 | Results.AddResult(CodeCompletionResult("assign")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5383 | if (!ObjCPropertyFlagConflicts(Attributes, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5384 | ObjCDeclSpec::DQ_PR_unsafe_unretained)) |
| 5385 | Results.AddResult(CodeCompletionResult("unsafe_unretained")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5386 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5387 | Results.AddResult(CodeCompletionResult("readwrite")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5388 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5389 | Results.AddResult(CodeCompletionResult("retain")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5390 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5391 | Results.AddResult(CodeCompletionResult("strong")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5392 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5393 | Results.AddResult(CodeCompletionResult("copy")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5394 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5395 | Results.AddResult(CodeCompletionResult("nonatomic")); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5396 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic)) |
Fariborz Jahanian | 1c2d29e | 2011-06-11 17:14:27 +0000 | [diff] [blame] | 5397 | Results.AddResult(CodeCompletionResult("atomic")); |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5398 | |
| 5399 | // Only suggest "weak" if we're compiling for ARC-with-weak-references or GC. |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 5400 | if (getLangOpts().ObjCWeak || getLangOpts().getGC() != LangOptions::NonGC) |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5401 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_weak)) |
Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5402 | Results.AddResult(CodeCompletionResult("weak")); |
| 5403 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5404 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5405 | CodeCompletionBuilder Setter(Results.getAllocator(), |
| 5406 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5407 | Setter.AddTypedTextChunk("setter"); |
Argyrios Kyrtzidis | 7bbb881 | 2014-02-20 07:55:15 +0000 | [diff] [blame] | 5408 | Setter.AddTextChunk("="); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5409 | Setter.AddPlaceholderChunk("method"); |
| 5410 | Results.AddResult(CodeCompletionResult(Setter.TakeString())); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 5411 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5412 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5413 | CodeCompletionBuilder Getter(Results.getAllocator(), |
| 5414 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5415 | Getter.AddTypedTextChunk("getter"); |
Argyrios Kyrtzidis | 7bbb881 | 2014-02-20 07:55:15 +0000 | [diff] [blame] | 5416 | Getter.AddTextChunk("="); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5417 | Getter.AddPlaceholderChunk("method"); |
| 5418 | Results.AddResult(CodeCompletionResult(Getter.TakeString())); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 5419 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 5420 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nullability)) { |
| 5421 | Results.AddResult(CodeCompletionResult("nonnull")); |
| 5422 | Results.AddResult(CodeCompletionResult("nullable")); |
| 5423 | Results.AddResult(CodeCompletionResult("null_unspecified")); |
| 5424 | Results.AddResult(CodeCompletionResult("null_resettable")); |
| 5425 | } |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5426 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5427 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5428 | Results.data(), Results.size()); |
Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5429 | } |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5430 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5431 | /// Describes the kind of Objective-C method that we want to find |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5432 | /// via code completion. |
| 5433 | enum ObjCMethodKind { |
Dmitri Gribenko | 4280e5c | 2012-06-08 23:13:42 +0000 | [diff] [blame] | 5434 | MK_Any, ///< Any kind of method, provided it means other specified criteria. |
| 5435 | MK_ZeroArgSelector, ///< Zero-argument (unary) selector. |
| 5436 | MK_OneArgSelector ///< One-argument selector. |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5437 | }; |
| 5438 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5439 | static bool isAcceptableObjCSelector(Selector Sel, |
| 5440 | ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5441 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5442 | bool AllowSameLength = true) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5443 | unsigned NumSelIdents = SelIdents.size(); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5444 | if (NumSelIdents > Sel.getNumArgs()) |
| 5445 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5446 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5447 | switch (WantKind) { |
| 5448 | case MK_Any: break; |
| 5449 | case MK_ZeroArgSelector: return Sel.isUnarySelector(); |
| 5450 | case MK_OneArgSelector: return Sel.getNumArgs() == 1; |
| 5451 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5452 | |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5453 | if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs()) |
| 5454 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5455 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5456 | for (unsigned I = 0; I != NumSelIdents; ++I) |
| 5457 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) |
| 5458 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5459 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5460 | return true; |
| 5461 | } |
| 5462 | |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5463 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, |
| 5464 | ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5465 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5466 | bool AllowSameLength = true) { |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5467 | return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5468 | AllowSameLength); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5469 | } |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5470 | |
| 5471 | namespace { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5472 | /// A set of selectors, which is used to avoid introducing multiple |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5473 | /// completions with the same selector into the result set. |
| 5474 | typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet; |
| 5475 | } |
| 5476 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5477 | /// Add all of the Objective-C methods in the given Objective-C |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5478 | /// container to the set of results. |
| 5479 | /// |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5480 | /// The container will be a class, protocol, category, or implementation of |
| 5481 | /// any of the above. This mether will recurse to include methods from |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5482 | /// the superclasses of classes along with their categories, protocols, and |
| 5483 | /// implementations. |
| 5484 | /// |
| 5485 | /// \param Container the container in which we'll look to find methods. |
| 5486 | /// |
James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5487 | /// \param WantInstanceMethods Whether to add instance methods (only); if |
| 5488 | /// false, this routine will add factory methods (only). |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5489 | /// |
| 5490 | /// \param CurContext the context in which we're performing the lookup that |
| 5491 | /// finds methods. |
| 5492 | /// |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5493 | /// \param AllowSameLength Whether we allow a method to be added to the list |
| 5494 | /// when it has the same number of parameters as we have selector identifiers. |
| 5495 | /// |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5496 | /// \param Results the structure into which we'll add results. |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5497 | static void AddObjCMethods(ObjCContainerDecl *Container, |
| 5498 | bool WantInstanceMethods, ObjCMethodKind WantKind, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5499 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5500 | DeclContext *CurContext, |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5501 | VisitedSelectorSet &Selectors, bool AllowSameLength, |
| 5502 | ResultBuilder &Results, bool InOriginalClass = true, |
| 5503 | bool IsRootClass = false) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5504 | typedef CodeCompletionResult Result; |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 5505 | Container = getContainerDef(Container); |
Douglas Gregor | 41778c3 | 2013-01-30 06:58:39 +0000 | [diff] [blame] | 5506 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5507 | IsRootClass = IsRootClass || (IFace && !IFace->getSuperClass()); |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 5508 | for (auto *M : Container->methods()) { |
Douglas Gregor | 41778c3 | 2013-01-30 06:58:39 +0000 | [diff] [blame] | 5509 | // The instance methods on the root class can be messaged via the |
| 5510 | // metaclass. |
| 5511 | if (M->isInstanceMethod() == WantInstanceMethods || |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5512 | (IsRootClass && !WantInstanceMethods)) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5513 | // Check whether the selector identifiers we've been given are a |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5514 | // subset of the identifiers for this particular method. |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 5515 | if (!isAcceptableObjCMethod(M, WantKind, SelIdents, AllowSameLength)) |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5516 | continue; |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5517 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 5518 | if (!Selectors.insert(M->getSelector()).second) |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5519 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5520 | |
| 5521 | Result R = Result(M, Results.getBasePriority(M), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5522 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5523 | R.AllParametersAreInformative = (WantKind != MK_Any); |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5524 | if (!InOriginalClass) |
| 5525 | R.Priority += CCD_InBaseClass; |
Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5526 | Results.MaybeAddResult(R, CurContext); |
| 5527 | } |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5528 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5529 | |
Douglas Gregor | f37c949 | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 5530 | // Visit the protocols of protocols. |
| 5531 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5532 | if (Protocol->hasDefinition()) { |
| 5533 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5534 | = Protocol->getReferencedProtocols(); |
| 5535 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5536 | E = Protocols.end(); |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5537 | I != E; ++I) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5538 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5539 | Selectors, AllowSameLength, Results, false, IsRootClass); |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5540 | } |
Douglas Gregor | f37c949 | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 5541 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5542 | |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 5543 | if (!IFace || !IFace->hasDefinition()) |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5544 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5545 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5546 | // Add methods in protocols. |
Aaron Ballman | a49c506 | 2014-03-13 20:29:09 +0000 | [diff] [blame] | 5547 | for (auto *I : IFace->protocols()) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5548 | AddObjCMethods(I, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5549 | Selectors, AllowSameLength, Results, false, IsRootClass); |
| 5550 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5551 | // Add methods in categories. |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 5552 | for (auto *CatDecl : IFace->known_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5553 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5554 | CurContext, Selectors, AllowSameLength, Results, |
| 5555 | InOriginalClass, IsRootClass); |
| 5556 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5557 | // Add a categories protocol methods. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5558 | const ObjCList<ObjCProtocolDecl> &Protocols |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5559 | = CatDecl->getReferencedProtocols(); |
| 5560 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 5561 | E = Protocols.end(); |
| 5562 | I != E; ++I) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5563 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5564 | Selectors, AllowSameLength, Results, false, IsRootClass); |
| 5565 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5566 | // Add methods in category implementations. |
| 5567 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5568 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5569 | Selectors, AllowSameLength, Results, InOriginalClass, |
| 5570 | IsRootClass); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5571 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5572 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5573 | // Add methods in superclass. |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5574 | // Avoid passing in IsRootClass since root classes won't have super classes. |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5575 | if (IFace->getSuperClass()) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5576 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, |
| 5577 | SelIdents, CurContext, Selectors, AllowSameLength, Results, |
| 5578 | /*IsRootClass=*/false); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5579 | |
| 5580 | // Add methods in our implementation, if any. |
| 5581 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5582 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5583 | Selectors, AllowSameLength, Results, InOriginalClass, |
| 5584 | IsRootClass); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5585 | } |
| 5586 | |
| 5587 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5588 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S) { |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5589 | // Try to find the interface where getters might live. |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5590 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5591 | if (!Class) { |
| 5592 | if (ObjCCategoryDecl *Category |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5593 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5594 | Class = Category->getClassInterface(); |
| 5595 | |
| 5596 | if (!Class) |
| 5597 | return; |
| 5598 | } |
| 5599 | |
| 5600 | // Find all of the potential getters. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5601 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5602 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5603 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5604 | Results.EnterNewScope(); |
| 5605 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5606 | VisitedSelectorSet Selectors; |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5607 | AddObjCMethods(Class, true, MK_ZeroArgSelector, None, CurContext, Selectors, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5608 | /*AllowSameLength=*/true, Results); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5609 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5610 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5611 | Results.data(), Results.size()); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5614 | void Sema::CodeCompleteObjCPropertySetter(Scope *S) { |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5615 | // Try to find the interface where setters might live. |
| 5616 | ObjCInterfaceDecl *Class |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5617 | = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5618 | if (!Class) { |
| 5619 | if (ObjCCategoryDecl *Category |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5620 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5621 | Class = Category->getClassInterface(); |
| 5622 | |
| 5623 | if (!Class) |
| 5624 | return; |
| 5625 | } |
| 5626 | |
| 5627 | // Find all of the potential getters. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5628 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5629 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5630 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5631 | Results.EnterNewScope(); |
| 5632 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5633 | VisitedSelectorSet Selectors; |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5634 | AddObjCMethods(Class, true, MK_OneArgSelector, None, CurContext, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5635 | Selectors, /*AllowSameLength=*/true, Results); |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5636 | |
| 5637 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5638 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5639 | Results.data(), Results.size()); |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5640 | } |
| 5641 | |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5642 | void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS, |
| 5643 | bool IsParameter) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5644 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5645 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5646 | CodeCompletionContext::CCC_Type); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5647 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5648 | |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5649 | // Add context-sensitive, Objective-C parameter-passing keywords. |
| 5650 | bool AddedInOut = false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5651 | if ((DS.getObjCDeclQualifier() & |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5652 | (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 5653 | Results.AddResult("in"); |
| 5654 | Results.AddResult("inout"); |
| 5655 | AddedInOut = true; |
| 5656 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5657 | if ((DS.getObjCDeclQualifier() & |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5658 | (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 5659 | Results.AddResult("out"); |
| 5660 | if (!AddedInOut) |
| 5661 | Results.AddResult("inout"); |
| 5662 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5663 | if ((DS.getObjCDeclQualifier() & |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5664 | (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref | |
| 5665 | ObjCDeclSpec::DQ_Oneway)) == 0) { |
| 5666 | Results.AddResult("bycopy"); |
| 5667 | Results.AddResult("byref"); |
| 5668 | Results.AddResult("oneway"); |
| 5669 | } |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 5670 | if ((DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability) == 0) { |
| 5671 | Results.AddResult("nonnull"); |
| 5672 | Results.AddResult("nullable"); |
| 5673 | Results.AddResult("null_unspecified"); |
| 5674 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5675 | |
| 5676 | // If we're completing the return type of an Objective-C method and the |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5677 | // identifier IBAction refers to a macro, provide a completion item for |
| 5678 | // an action, e.g., |
| 5679 | // IBAction)<#selector#>:(id)sender |
| 5680 | if (DS.getObjCDeclQualifier() == 0 && !IsParameter && |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 5681 | PP.isMacroDefined("IBAction")) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5682 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5683 | Results.getCodeCompletionTUInfo(), |
| 5684 | CCP_CodePattern, CXAvailability_Available); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5685 | Builder.AddTypedTextChunk("IBAction"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5686 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5687 | Builder.AddPlaceholderChunk("selector"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5688 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 5689 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5690 | Builder.AddTextChunk("id"); |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5691 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5692 | Builder.AddTextChunk("sender"); |
| 5693 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 5694 | } |
Douglas Gregor | ed1f597 | 2013-01-30 07:11:43 +0000 | [diff] [blame] | 5695 | |
| 5696 | // If we're completing the return type, provide 'instancetype'. |
| 5697 | if (!IsParameter) { |
| 5698 | Results.AddResult(CodeCompletionResult("instancetype")); |
| 5699 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5700 | |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5701 | // Add various builtin type names and specifiers. |
| 5702 | AddOrdinaryNameResults(PCC_Type, S, *this, Results); |
| 5703 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5704 | |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5705 | // Add the various type names |
| 5706 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 5707 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 5708 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 5709 | CodeCompleter->includeGlobals(), |
| 5710 | CodeCompleter->loadExternal()); |
| 5711 | |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5712 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 5713 | AddMacroResults(PP, Results, false); |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5714 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5715 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5716 | Results.data(), Results.size()); |
| 5717 | } |
| 5718 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5719 | /// When we have an expression with type "id", we may assume |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5720 | /// that it has some more-specific class type based on knowledge of |
| 5721 | /// common uses of Objective-C. This routine returns that class type, |
| 5722 | /// or NULL if no better result could be determined. |
| 5723 | static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) { |
Douglas Gregor | ed0b69d | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 5724 | ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5725 | if (!Msg) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5726 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5727 | |
| 5728 | Selector Sel = Msg->getSelector(); |
| 5729 | if (Sel.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5730 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5731 | |
| 5732 | IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0); |
| 5733 | if (!Id) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5734 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5735 | |
| 5736 | ObjCMethodDecl *Method = Msg->getMethodDecl(); |
| 5737 | if (!Method) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5738 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5739 | |
| 5740 | // Determine the class that we're sending the message to. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5741 | ObjCInterfaceDecl *IFace = nullptr; |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 5742 | switch (Msg->getReceiverKind()) { |
| 5743 | case ObjCMessageExpr::Class: |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5744 | if (const ObjCObjectType *ObjType |
| 5745 | = Msg->getClassReceiver()->getAs<ObjCObjectType>()) |
| 5746 | IFace = ObjType->getInterface(); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 5747 | break; |
| 5748 | |
| 5749 | case ObjCMessageExpr::Instance: { |
| 5750 | QualType T = Msg->getInstanceReceiver()->getType(); |
| 5751 | if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) |
| 5752 | IFace = Ptr->getInterfaceDecl(); |
| 5753 | break; |
| 5754 | } |
| 5755 | |
| 5756 | case ObjCMessageExpr::SuperInstance: |
| 5757 | case ObjCMessageExpr::SuperClass: |
| 5758 | break; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5759 | } |
| 5760 | |
| 5761 | if (!IFace) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5762 | return nullptr; |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5763 | |
| 5764 | ObjCInterfaceDecl *Super = IFace->getSuperClass(); |
| 5765 | if (Method->isInstanceMethod()) |
| 5766 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 5767 | .Case("retain", IFace) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5768 | .Case("strong", IFace) |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5769 | .Case("autorelease", IFace) |
| 5770 | .Case("copy", IFace) |
| 5771 | .Case("copyWithZone", IFace) |
| 5772 | .Case("mutableCopy", IFace) |
| 5773 | .Case("mutableCopyWithZone", IFace) |
| 5774 | .Case("awakeFromCoder", IFace) |
| 5775 | .Case("replacementObjectFromCoder", IFace) |
| 5776 | .Case("class", IFace) |
| 5777 | .Case("classForCoder", IFace) |
| 5778 | .Case("superclass", Super) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5779 | .Default(nullptr); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5780 | |
| 5781 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 5782 | .Case("new", IFace) |
| 5783 | .Case("alloc", IFace) |
| 5784 | .Case("allocWithZone", IFace) |
| 5785 | .Case("class", IFace) |
| 5786 | .Case("superclass", Super) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5787 | .Default(nullptr); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5788 | } |
| 5789 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5790 | // Add a special completion for a message send to "super", which fills in the |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5791 | // most likely case of forwarding all of our arguments to the superclass |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5792 | // function. |
| 5793 | /// |
| 5794 | /// \param S The semantic analysis object. |
| 5795 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 5796 | /// \param NeedSuperKeyword Whether we need to prefix this completion with |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5797 | /// the "super" keyword. Otherwise, we just need to provide the arguments. |
| 5798 | /// |
| 5799 | /// \param SelIdents The identifiers in the selector that have already been |
| 5800 | /// provided as arguments for a send to "super". |
| 5801 | /// |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5802 | /// \param Results The set of results to augment. |
| 5803 | /// |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5804 | /// \returns the Objective-C method declaration that would be invoked by |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5805 | /// this "super" completion. If NULL, no completion was added. |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5806 | static ObjCMethodDecl *AddSuperSendCompletion( |
| 5807 | Sema &S, bool NeedSuperKeyword, |
| 5808 | ArrayRef<IdentifierInfo *> SelIdents, |
| 5809 | ResultBuilder &Results) { |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5810 | ObjCMethodDecl *CurMethod = S.getCurMethodDecl(); |
| 5811 | if (!CurMethod) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5812 | return nullptr; |
| 5813 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5814 | ObjCInterfaceDecl *Class = CurMethod->getClassInterface(); |
| 5815 | if (!Class) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5816 | return nullptr; |
| 5817 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5818 | // Try to find a superclass method with the same selector. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5819 | ObjCMethodDecl *SuperMethod = nullptr; |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5820 | while ((Class = Class->getSuperClass()) && !SuperMethod) { |
| 5821 | // Check in the class |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5822 | SuperMethod = Class->getMethod(CurMethod->getSelector(), |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5823 | CurMethod->isInstanceMethod()); |
| 5824 | |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5825 | // Check in categories or class extensions. |
| 5826 | if (!SuperMethod) { |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 5827 | for (const auto *Cat : Class->known_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5828 | if ((SuperMethod = Cat->getMethod(CurMethod->getSelector(), |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5829 | CurMethod->isInstanceMethod()))) |
| 5830 | break; |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5831 | } |
Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5832 | } |
| 5833 | } |
| 5834 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5835 | if (!SuperMethod) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5836 | return nullptr; |
| 5837 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5838 | // Check whether the superclass method has the same signature. |
| 5839 | if (CurMethod->param_size() != SuperMethod->param_size() || |
| 5840 | CurMethod->isVariadic() != SuperMethod->isVariadic()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5841 | return nullptr; |
| 5842 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5843 | for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(), |
| 5844 | CurPEnd = CurMethod->param_end(), |
| 5845 | SuperP = SuperMethod->param_begin(); |
| 5846 | CurP != CurPEnd; ++CurP, ++SuperP) { |
| 5847 | // Make sure the parameter types are compatible. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5848 | if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(), |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5849 | (*SuperP)->getType())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5850 | return nullptr; |
| 5851 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5852 | // Make sure we have a parameter name to forward! |
| 5853 | if (!(*CurP)->getIdentifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5854 | return nullptr; |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5855 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5856 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5857 | // We have a superclass method. Now, form the send-to-super completion. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5858 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5859 | Results.getCodeCompletionTUInfo()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5860 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5861 | // Give this completion a return type. |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 5862 | AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod, |
| 5863 | Results.getCompletionContext().getBaseType(), |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 5864 | Builder); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5865 | |
| 5866 | // If we need the "super" keyword, add it (plus some spacing). |
| 5867 | if (NeedSuperKeyword) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5868 | Builder.AddTypedTextChunk("super"); |
| 5869 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5870 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5871 | |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5872 | Selector Sel = CurMethod->getSelector(); |
| 5873 | if (Sel.isUnarySelector()) { |
| 5874 | if (NeedSuperKeyword) |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5875 | Builder.AddTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5876 | Sel.getNameForSlot(0))); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5877 | else |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5878 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5879 | Sel.getNameForSlot(0))); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5880 | } else { |
| 5881 | ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(); |
| 5882 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5883 | if (I > SelIdents.size()) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5884 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5885 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5886 | if (I < SelIdents.size()) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5887 | Builder.AddInformativeChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5888 | Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5889 | Sel.getNameForSlot(I) + ":")); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5890 | else if (NeedSuperKeyword || I > SelIdents.size()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5891 | Builder.AddTextChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5892 | Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5893 | Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5894 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5895 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5896 | } else { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5897 | Builder.AddTypedTextChunk( |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5898 | Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5899 | Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5900 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5901 | (*CurP)->getIdentifier()->getName())); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5902 | } |
| 5903 | } |
| 5904 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5905 | |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 5906 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), SuperMethod, |
| 5907 | CCP_SuperCompletion)); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5908 | return SuperMethod; |
| 5909 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5910 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5911 | void Sema::CodeCompleteObjCMessageReceiver(Scope *S) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5912 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5913 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5914 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5915 | CodeCompletionContext::CCC_ObjCMessageReceiver, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5916 | getLangOpts().CPlusPlus11 |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5917 | ? &ResultBuilder::IsObjCMessageReceiverOrLambdaCapture |
| 5918 | : &ResultBuilder::IsObjCMessageReceiver); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5919 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5920 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 5921 | Results.EnterNewScope(); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5922 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 5923 | CodeCompleter->includeGlobals(), |
| 5924 | CodeCompleter->loadExternal()); |
| 5925 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5926 | // If we are in an Objective-C method inside a class that has a superclass, |
| 5927 | // add "super" as an option. |
| 5928 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 5929 | if (ObjCInterfaceDecl *Iface = Method->getClassInterface()) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5930 | if (Iface->getSuperClass()) { |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5931 | Results.AddResult(Result("super")); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5932 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5933 | AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, None, Results); |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5934 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5935 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5936 | if (getLangOpts().CPlusPlus11) |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5937 | addThisCompletion(*this, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5938 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5939 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5940 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5941 | if (CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 5942 | AddMacroResults(PP, Results, false); |
Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 5943 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5944 | Results.data(), Results.size()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5945 | |
Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5946 | } |
| 5947 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5948 | void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5949 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5950 | bool AtArgumentExpression) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5951 | ObjCInterfaceDecl *CDecl = nullptr; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5952 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 5953 | // Figure out which interface we're in. |
| 5954 | CDecl = CurMethod->getClassInterface(); |
| 5955 | if (!CDecl) |
| 5956 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5957 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5958 | // Find the superclass of this class. |
| 5959 | CDecl = CDecl->getSuperClass(); |
| 5960 | if (!CDecl) |
| 5961 | return; |
| 5962 | |
| 5963 | if (CurMethod->isInstanceMethod()) { |
| 5964 | // We are inside an instance method, which means that the message |
| 5965 | // send [super ...] is actually calling an instance method on the |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5966 | // current object. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5967 | return CodeCompleteObjCInstanceMessage(S, nullptr, SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5968 | AtArgumentExpression, |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5969 | CDecl); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5970 | } |
| 5971 | |
| 5972 | // Fall through to send to the superclass in CDecl. |
| 5973 | } else { |
| 5974 | // "super" may be the name of a type or variable. Figure out which |
| 5975 | // it is. |
Argyrios Kyrtzidis | 3e56dd4 | 2013-03-14 22:56:43 +0000 | [diff] [blame] | 5976 | IdentifierInfo *Super = getSuperIdentifier(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5977 | NamedDecl *ND = LookupSingleName(S, Super, SuperLoc, |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5978 | LookupOrdinaryName); |
| 5979 | if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) { |
| 5980 | // "super" names an interface. Use it. |
| 5981 | } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5982 | if (const ObjCObjectType *Iface |
| 5983 | = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>()) |
| 5984 | CDecl = Iface->getInterface(); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5985 | } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) { |
| 5986 | // "super" names an unresolved type; we can't be more specific. |
| 5987 | } else { |
| 5988 | // Assume that "super" names some kind of value and parse that way. |
| 5989 | CXXScopeSpec SS; |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 5990 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5991 | UnqualifiedId id; |
| 5992 | id.setIdentifier(Super, SuperLoc); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 5993 | ExprResult SuperExpr = ActOnIdExpression(S, SS, TemplateKWLoc, id, |
| 5994 | false, false); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5995 | return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(), |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5996 | SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5997 | AtArgumentExpression); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5998 | } |
| 5999 | |
| 6000 | // Fall through |
| 6001 | } |
| 6002 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6003 | ParsedType Receiver; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6004 | if (CDecl) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6005 | Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6006 | return CodeCompleteObjCClassMessage(S, Receiver, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6007 | AtArgumentExpression, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6008 | /*IsSuper=*/true); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6009 | } |
| 6010 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6011 | /// Given a set of code-completion results for the argument of a message |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6012 | /// send, determine the preferred type (if any) for that argument expression. |
| 6013 | static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results, |
| 6014 | unsigned NumSelIdents) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6015 | typedef CodeCompletionResult Result; |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6016 | ASTContext &Context = Results.getSema().Context; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6017 | |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6018 | QualType PreferredType; |
| 6019 | unsigned BestPriority = CCP_Unlikely * 2; |
| 6020 | Result *ResultsData = Results.data(); |
| 6021 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 6022 | Result &R = ResultsData[I]; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6023 | if (R.Kind == Result::RK_Declaration && |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6024 | isa<ObjCMethodDecl>(R.Declaration)) { |
| 6025 | if (R.Priority <= BestPriority) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 6026 | const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6027 | if (NumSelIdents <= Method->param_size()) { |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 6028 | QualType MyPreferredType = Method->parameters()[NumSelIdents - 1] |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6029 | ->getType(); |
| 6030 | if (R.Priority < BestPriority || PreferredType.isNull()) { |
| 6031 | BestPriority = R.Priority; |
| 6032 | PreferredType = MyPreferredType; |
| 6033 | } else if (!Context.hasSameUnqualifiedType(PreferredType, |
| 6034 | MyPreferredType)) { |
| 6035 | PreferredType = QualType(); |
| 6036 | } |
| 6037 | } |
| 6038 | } |
| 6039 | } |
| 6040 | } |
| 6041 | |
| 6042 | return PreferredType; |
| 6043 | } |
| 6044 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6045 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6046 | ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6047 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6048 | bool AtArgumentExpression, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6049 | bool IsSuper, |
| 6050 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6051 | typedef CodeCompletionResult Result; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6052 | ObjCInterfaceDecl *CDecl = nullptr; |
| 6053 | |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 6054 | // If the given name refers to an interface type, retrieve the |
| 6055 | // corresponding declaration. |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6056 | if (Receiver) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6057 | QualType T = SemaRef.GetTypeFromParser(Receiver, nullptr); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6058 | if (!T.isNull()) |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6059 | if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>()) |
| 6060 | CDecl = Interface->getInterface(); |
Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 6061 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6062 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6063 | // Add all of the factory methods in this Objective-C class, its protocols, |
| 6064 | // superclasses, categories, implementation, etc. |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6065 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6066 | |
| 6067 | // If this is a send-to-super, try to add the special "super" send |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6068 | // completion. |
| 6069 | if (IsSuper) { |
| 6070 | if (ObjCMethodDecl *SuperMethod |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6071 | = AddSuperSendCompletion(SemaRef, false, SelIdents, Results)) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6072 | Results.Ignore(SuperMethod); |
| 6073 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6074 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 6075 | // If we're inside an Objective-C method definition, prefer its selector to |
| 6076 | // others. |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6077 | if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl()) |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 6078 | Results.setPreferredSelector(CurMethod->getSelector()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6079 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6080 | VisitedSelectorSet Selectors; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6081 | if (CDecl) |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6082 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6083 | SemaRef.CurContext, Selectors, AtArgumentExpression, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6084 | Results); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6085 | else { |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6086 | // We're messaging "id" as a type; provide all class/factory methods. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6087 | |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6088 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 6089 | // pool from the AST file. |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 6090 | if (SemaRef.getExternalSource()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6091 | for (uint32_t I = 0, |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 6092 | N = SemaRef.getExternalSource()->GetNumExternalSelectors(); |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 6093 | I != N; ++I) { |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 6094 | Selector Sel = SemaRef.getExternalSource()->GetExternalSelector(I); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6095 | if (Sel.isNull() || SemaRef.MethodPool.count(Sel)) |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6096 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6097 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6098 | SemaRef.ReadMethodPool(Sel); |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6099 | } |
| 6100 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6101 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6102 | for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(), |
| 6103 | MEnd = SemaRef.MethodPool.end(); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6104 | M != MEnd; ++M) { |
| 6105 | for (ObjCMethodList *MethList = &M->second.second; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6106 | MethList && MethList->getMethod(); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 6107 | MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6108 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6109 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6110 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6111 | Result R(MethList->getMethod(), |
| 6112 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6113 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6114 | R.AllParametersAreInformative = false; |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6115 | Results.MaybeAddResult(R, SemaRef.CurContext); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6116 | } |
| 6117 | } |
| 6118 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6119 | |
| 6120 | Results.ExitScope(); |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6121 | } |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6122 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6123 | void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6124 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6125 | bool AtArgumentExpression, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6126 | bool IsSuper) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6127 | |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 6128 | QualType T = this->GetTypeFromParser(Receiver); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6129 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6130 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6131 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 6132 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6133 | T, SelIdents)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6134 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6135 | AddClassMessageCompletions(*this, S, Receiver, SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6136 | AtArgumentExpression, IsSuper, Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6137 | |
| 6138 | // If we're actually at the argument expression (rather than prior to the |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6139 | // selector), we're actually performing code completion for an expression. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6140 | // Determine whether we have a single, best method. If so, we can |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6141 | // code-complete the expression using the corresponding parameter type as |
| 6142 | // our preferred type, improving completion results. |
| 6143 | if (AtArgumentExpression) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6144 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6145 | SelIdents.size()); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6146 | if (PreferredType.isNull()) |
| 6147 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 6148 | else |
| 6149 | CodeCompleteExpression(S, PreferredType); |
| 6150 | return; |
| 6151 | } |
| 6152 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6153 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 6154 | Results.getCompletionContext(), |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6155 | Results.data(), Results.size()); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6156 | } |
| 6157 | |
Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 6158 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6159 | ArrayRef<IdentifierInfo *> SelIdents, |
Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6160 | bool AtArgumentExpression, |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 6161 | ObjCInterfaceDecl *Super) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6162 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6163 | |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6164 | Expr *RecExpr = static_cast<Expr *>(Receiver); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6165 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6166 | // If necessary, apply function/array conversion to the receiver. |
| 6167 | // C99 6.7.5.3p[7,8]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6168 | if (RecExpr) { |
| 6169 | ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr); |
| 6170 | if (Conv.isInvalid()) // conversion failed. bail. |
| 6171 | return; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6172 | RecExpr = Conv.get(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6173 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6174 | QualType ReceiverType = RecExpr? RecExpr->getType() |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 6175 | : Super? Context.getObjCObjectPointerType( |
| 6176 | Context.getObjCInterfaceType(Super)) |
| 6177 | : Context.getObjCIdType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6178 | |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6179 | // If we're messaging an expression with type "id" or "Class", check |
| 6180 | // whether we know something special about the receiver that allows |
| 6181 | // us to assume a more-specific receiver type. |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 6182 | if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) { |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6183 | if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) { |
| 6184 | if (ReceiverType->isObjCClassType()) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6185 | return CodeCompleteObjCClassMessage(S, |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6186 | ParsedType::make(Context.getObjCInterfaceType(IFace)), |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6187 | SelIdents, |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6188 | AtArgumentExpression, Super); |
| 6189 | |
| 6190 | ReceiverType = Context.getObjCObjectPointerType( |
| 6191 | Context.getObjCInterfaceType(IFace)); |
| 6192 | } |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 6193 | } else if (RecExpr && getLangOpts().CPlusPlus) { |
| 6194 | ExprResult Conv = PerformContextuallyConvertToObjCPointer(RecExpr); |
| 6195 | if (Conv.isUsable()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6196 | RecExpr = Conv.get(); |
Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 6197 | ReceiverType = RecExpr->getType(); |
| 6198 | } |
| 6199 | } |
Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6200 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6201 | // Build the set of methods we can see. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6202 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6203 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 6204 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6205 | ReceiverType, SelIdents)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6206 | |
Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6207 | Results.EnterNewScope(); |
Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6208 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6209 | // If this is a send-to-super, try to add the special "super" send |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6210 | // completion. |
Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 6211 | if (Super) { |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6212 | if (ObjCMethodDecl *SuperMethod |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6213 | = AddSuperSendCompletion(*this, false, SelIdents, Results)) |
Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6214 | Results.Ignore(SuperMethod); |
| 6215 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6216 | |
Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 6217 | // If we're inside an Objective-C method definition, prefer its selector to |
| 6218 | // others. |
| 6219 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 6220 | Results.setPreferredSelector(CurMethod->getSelector()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6221 | |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6222 | // Keep track of the selectors we've already added. |
| 6223 | VisitedSelectorSet Selectors; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6224 | |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6225 | // Handle messages to Class. This really isn't a message to an instance |
| 6226 | // method, so we treat it the same way we would treat a message send to a |
| 6227 | // class method. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6228 | if (ReceiverType->isObjCClassType() || |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6229 | ReceiverType->isObjCQualifiedClassType()) { |
| 6230 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 6231 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6232 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6233 | CurContext, Selectors, AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6234 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6235 | } |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6236 | // Handle messages to a qualified ID ("id<foo>"). |
| 6237 | else if (const ObjCObjectPointerType *QualID |
| 6238 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 6239 | // Search protocols for instance methods. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6240 | for (auto *I : QualID->quals()) |
| 6241 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6242 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6243 | } |
| 6244 | // Handle messages to a pointer to interface type. |
| 6245 | else if (const ObjCObjectPointerType *IFacePtr |
| 6246 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 6247 | // Search the class, its superclasses, etc., for instance methods. |
Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6248 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6249 | CurContext, Selectors, AtArgumentExpression, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6250 | Results); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6251 | |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6252 | // Search protocols for instance methods. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6253 | for (auto *I : IFacePtr->quals()) |
| 6254 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, |
Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6255 | Selectors, AtArgumentExpression, Results); |
Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6256 | } |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6257 | // Handle messages to "id". |
| 6258 | else if (ReceiverType->isObjCIdType()) { |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6259 | // We're messaging "id", so provide all instance methods we know |
| 6260 | // about as code-completion results. |
| 6261 | |
| 6262 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 6263 | // pool from the AST file. |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6264 | if (ExternalSource) { |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 6265 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 6266 | I != N; ++I) { |
| 6267 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6268 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6269 | continue; |
| 6270 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6271 | ReadMethodPool(Sel); |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6272 | } |
| 6273 | } |
| 6274 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6275 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 6276 | MEnd = MethodPool.end(); |
| 6277 | M != MEnd; ++M) { |
| 6278 | for (ObjCMethodList *MethList = &M->second.first; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6279 | MethList && MethList->getMethod(); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 6280 | MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6281 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6282 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6283 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6284 | if (!Selectors.insert(MethList->getMethod()->getSelector()).second) |
Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6285 | continue; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6286 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6287 | Result R(MethList->getMethod(), |
| 6288 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6289 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6290 | R.AllParametersAreInformative = false; |
| 6291 | Results.MaybeAddResult(R, CurContext); |
| 6292 | } |
| 6293 | } |
| 6294 | } |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6295 | Results.ExitScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6296 | |
| 6297 | |
| 6298 | // If we're actually at the argument expression (rather than prior to the |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6299 | // selector), we're actually performing code completion for an expression. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6300 | // Determine whether we have a single, best method. If so, we can |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6301 | // code-complete the expression using the corresponding parameter type as |
| 6302 | // our preferred type, improving completion results. |
| 6303 | if (AtArgumentExpression) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6304 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6305 | SelIdents.size()); |
Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6306 | if (PreferredType.isNull()) |
| 6307 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 6308 | else |
| 6309 | CodeCompleteExpression(S, PreferredType); |
| 6310 | return; |
| 6311 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6312 | |
| 6313 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 6314 | Results.getCompletionContext(), |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6315 | Results.data(),Results.size()); |
Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6316 | } |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6317 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6318 | void Sema::CodeCompleteObjCForCollection(Scope *S, |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6319 | DeclGroupPtrTy IterationVar) { |
| 6320 | CodeCompleteExpressionData Data; |
| 6321 | Data.ObjCCollection = true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6322 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6323 | if (IterationVar.getAsOpaquePtr()) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 6324 | DeclGroupRef DG = IterationVar.get(); |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6325 | for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) { |
| 6326 | if (*I) |
| 6327 | Data.IgnoreDecls.push_back(*I); |
| 6328 | } |
| 6329 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6330 | |
Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6331 | CodeCompleteExpression(S, Data); |
| 6332 | } |
| 6333 | |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6334 | void Sema::CodeCompleteObjCSelector(Scope *S, |
| 6335 | ArrayRef<IdentifierInfo *> SelIdents) { |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6336 | // If we have an external source, load the entire class method |
| 6337 | // pool from the AST file. |
| 6338 | if (ExternalSource) { |
| 6339 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 6340 | I != N; ++I) { |
| 6341 | Selector Sel = ExternalSource->GetExternalSelector(I); |
| 6342 | if (Sel.isNull() || MethodPool.count(Sel)) |
| 6343 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6344 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6345 | ReadMethodPool(Sel); |
| 6346 | } |
| 6347 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6348 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6349 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6350 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6351 | CodeCompletionContext::CCC_SelectorName); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6352 | Results.EnterNewScope(); |
| 6353 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 6354 | MEnd = MethodPool.end(); |
| 6355 | M != MEnd; ++M) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6356 | |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6357 | Selector Sel = M->first; |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6358 | if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents)) |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6359 | continue; |
| 6360 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6361 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 6362 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6363 | if (Sel.isUnarySelector()) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6364 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 6365 | Sel.getNameForSlot(0))); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6366 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6367 | continue; |
| 6368 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6369 | |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6370 | std::string Accumulator; |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6371 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) { |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6372 | if (I == SelIdents.size()) { |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6373 | if (!Accumulator.empty()) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6374 | Builder.AddInformativeChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6375 | Accumulator)); |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6376 | Accumulator.clear(); |
| 6377 | } |
| 6378 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6379 | |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 6380 | Accumulator += Sel.getNameForSlot(I); |
Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6381 | Accumulator += ':'; |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6382 | } |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6383 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator)); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6384 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6385 | } |
| 6386 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6387 | |
| 6388 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6389 | Results.data(), Results.size()); |
| 6390 | } |
| 6391 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6392 | /// Add all of the protocol declarations that we find in the given |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6393 | /// (translation unit) context. |
| 6394 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 6395 | bool OnlyForwardDeclarations, |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6396 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6397 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6398 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6399 | for (const auto *D : Ctx->decls()) { |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6400 | // Record any protocols we find. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6401 | if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(D)) |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 6402 | if (!OnlyForwardDeclarations || !Proto->hasDefinition()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6403 | Results.AddResult(Result(Proto, Results.getBasePriority(Proto),nullptr), |
| 6404 | CurContext, nullptr, false); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6405 | } |
| 6406 | } |
| 6407 | |
Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 6408 | void Sema::CodeCompleteObjCProtocolReferences( |
| 6409 | ArrayRef<IdentifierLocPair> Protocols) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6410 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6411 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6412 | CodeCompletionContext::CCC_ObjCProtocolName); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6413 | |
Chandler Carruth | ede1163 | 2016-11-04 06:06:50 +0000 | [diff] [blame] | 6414 | if (CodeCompleter->includeGlobals()) { |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6415 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6416 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6417 | // Tell the result set to ignore all of the protocols we have |
| 6418 | // already seen. |
| 6419 | // FIXME: This doesn't work when caching code-completion results. |
Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 6420 | for (const IdentifierLocPair &Pair : Protocols) |
| 6421 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Pair.first, |
| 6422 | Pair.second)) |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6423 | Results.Ignore(Protocol); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6424 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6425 | // Add all protocols. |
| 6426 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6427 | Results); |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 6428 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6429 | Results.ExitScope(); |
| 6430 | } |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6431 | |
| 6432 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6433 | Results.data(), Results.size()); |
Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 6434 | } |
| 6435 | |
| 6436 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6437 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6438 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6439 | CodeCompletionContext::CCC_ObjCProtocolName); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6440 | |
Chandler Carruth | ede1163 | 2016-11-04 06:06:50 +0000 | [diff] [blame] | 6441 | if (CodeCompleter->includeGlobals()) { |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6442 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6443 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6444 | // Add all protocols. |
| 6445 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 6446 | Results); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6447 | |
Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6448 | Results.ExitScope(); |
| 6449 | } |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6450 | |
| 6451 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6452 | Results.data(), Results.size()); |
Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6453 | } |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6454 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6455 | /// Add all of the Objective-C interface declarations that we find in |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6456 | /// the given (translation unit) context. |
| 6457 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, |
| 6458 | bool OnlyForwardDeclarations, |
| 6459 | bool OnlyUnimplemented, |
| 6460 | ResultBuilder &Results) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6461 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6462 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6463 | for (const auto *D : Ctx->decls()) { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 6464 | // Record any interfaces we find. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6465 | if (const auto *Class = dyn_cast<ObjCInterfaceDecl>(D)) |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 6466 | if ((!OnlyForwardDeclarations || !Class->hasDefinition()) && |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 6467 | (!OnlyUnimplemented || !Class->getImplementation())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6468 | Results.AddResult(Result(Class, Results.getBasePriority(Class),nullptr), |
| 6469 | CurContext, nullptr, false); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6470 | } |
| 6471 | } |
| 6472 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6473 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6474 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6475 | CodeCompleter->getCodeCompletionTUInfo(), |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6476 | CodeCompletionContext::CCC_ObjCInterfaceName); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6477 | Results.EnterNewScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6478 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6479 | if (CodeCompleter->includeGlobals()) { |
| 6480 | // Add all classes. |
| 6481 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6482 | false, Results); |
| 6483 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6484 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6485 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6486 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6487 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6488 | Results.data(), Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6489 | } |
| 6490 | |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6491 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6492 | SourceLocation ClassNameLoc) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6493 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6494 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6495 | CodeCompletionContext::CCC_ObjCInterfaceName); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6496 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6497 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6498 | // Make sure that we ignore the class we're currently defining. |
| 6499 | NamedDecl *CurClass |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6500 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6501 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6502 | Results.Ignore(CurClass); |
| 6503 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6504 | if (CodeCompleter->includeGlobals()) { |
| 6505 | // Add all classes. |
| 6506 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6507 | false, Results); |
| 6508 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6509 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6510 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6511 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6512 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6513 | Results.data(), Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6514 | } |
| 6515 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6516 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6517 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6518 | CodeCompleter->getCodeCompletionTUInfo(), |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6519 | CodeCompletionContext::CCC_ObjCImplementation); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6520 | Results.EnterNewScope(); |
| 6521 | |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6522 | if (CodeCompleter->includeGlobals()) { |
| 6523 | // Add all unimplemented classes. |
| 6524 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6525 | true, Results); |
| 6526 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6527 | |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6528 | Results.ExitScope(); |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6529 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6530 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6531 | Results.data(), Results.size()); |
Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6532 | } |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6533 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6534 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6535 | IdentifierInfo *ClassName, |
| 6536 | SourceLocation ClassNameLoc) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6537 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6538 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6539 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6540 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6541 | CodeCompletionContext::CCC_ObjCCategoryName); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6542 | |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6543 | // Ignore any categories we find that have already been implemented by this |
| 6544 | // interface. |
| 6545 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 6546 | NamedDecl *CurClass |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6547 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6548 | if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)){ |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 6549 | for (const auto *Cat : Class->visible_categories()) |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6550 | CategoryNames.insert(Cat->getIdentifier()); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6551 | } |
| 6552 | |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6553 | // Add all of the categories we know about. |
| 6554 | Results.EnterNewScope(); |
| 6555 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6556 | for (const auto *D : TU->decls()) |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6557 | if (const auto *Category = dyn_cast<ObjCCategoryDecl>(D)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6558 | if (CategoryNames.insert(Category->getIdentifier()).second) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6559 | Results.AddResult(Result(Category, Results.getBasePriority(Category), |
| 6560 | nullptr), |
| 6561 | CurContext, nullptr, false); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6562 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6563 | |
| 6564 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6565 | Results.data(), Results.size()); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6566 | } |
| 6567 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6568 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6569 | IdentifierInfo *ClassName, |
| 6570 | SourceLocation ClassNameLoc) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6571 | typedef CodeCompletionResult Result; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6572 | |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6573 | // Find the corresponding interface. If we couldn't find the interface, the |
| 6574 | // program itself is ill-formed. However, we'll try to be helpful still by |
| 6575 | // providing the list of all of the categories we know about. |
| 6576 | NamedDecl *CurClass |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6577 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6578 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); |
| 6579 | if (!Class) |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6580 | return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6581 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6582 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6583 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6584 | CodeCompletionContext::CCC_ObjCCategoryName); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6585 | |
| 6586 | // Add all of the categories that have have corresponding interface |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6587 | // declarations in this class and any of its superclasses, except for |
| 6588 | // already-implemented categories in the class itself. |
| 6589 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 6590 | Results.EnterNewScope(); |
| 6591 | bool IgnoreImplemented = true; |
| 6592 | while (Class) { |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 6593 | for (const auto *Cat : Class->visible_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6594 | if ((!IgnoreImplemented || !Cat->getImplementation()) && |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6595 | CategoryNames.insert(Cat->getIdentifier()).second) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6596 | Results.AddResult(Result(Cat, Results.getBasePriority(Cat), nullptr), |
| 6597 | CurContext, nullptr, false); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6598 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6599 | |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6600 | Class = Class->getSuperClass(); |
| 6601 | IgnoreImplemented = false; |
| 6602 | } |
| 6603 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6604 | |
| 6605 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6606 | Results.data(), Results.size()); |
Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6607 | } |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6608 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6609 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) { |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6610 | CodeCompletionContext CCContext(CodeCompletionContext::CCC_Other); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6611 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6612 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6613 | CCContext); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6614 | |
| 6615 | // Figure out where this @synthesize lives. |
| 6616 | ObjCContainerDecl *Container |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6617 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6618 | if (!Container || |
| 6619 | (!isa<ObjCImplementationDecl>(Container) && |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6620 | !isa<ObjCCategoryImplDecl>(Container))) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6621 | return; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6622 | |
| 6623 | // Ignore any properties that have already been implemented. |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6624 | Container = getContainerDef(Container); |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6625 | for (const auto *D : Container->decls()) |
| 6626 | if (const auto *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6627 | Results.Ignore(PropertyImpl->getPropertyDecl()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6628 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6629 | // Add any properties that we find. |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 6630 | AddedPropertiesSet AddedProperties; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6631 | Results.EnterNewScope(); |
| 6632 | if (ObjCImplementationDecl *ClassImpl |
| 6633 | = dyn_cast<ObjCImplementationDecl>(Container)) |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6634 | AddObjCProperties(CCContext, ClassImpl->getClassInterface(), false, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6635 | /*AllowNullaryMethods=*/false, CurContext, |
Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 6636 | AddedProperties, Results); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6637 | else |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6638 | AddObjCProperties(CCContext, |
| 6639 | cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6640 | false, /*AllowNullaryMethods=*/false, CurContext, |
Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 6641 | AddedProperties, Results); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6642 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6643 | |
| 6644 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6645 | Results.data(), Results.size()); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6646 | } |
| 6647 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6648 | void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S, |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6649 | IdentifierInfo *PropertyName) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6650 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6651 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6652 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6653 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6654 | |
| 6655 | // Figure out where this @synthesize lives. |
| 6656 | ObjCContainerDecl *Container |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6657 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6658 | if (!Container || |
| 6659 | (!isa<ObjCImplementationDecl>(Container) && |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6660 | !isa<ObjCCategoryImplDecl>(Container))) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6661 | return; |
| 6662 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6663 | // Figure out which interface we're looking into. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6664 | ObjCInterfaceDecl *Class = nullptr; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6665 | if (ObjCImplementationDecl *ClassImpl |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 6666 | = dyn_cast<ObjCImplementationDecl>(Container)) |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6667 | Class = ClassImpl->getClassInterface(); |
| 6668 | else |
| 6669 | Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl() |
| 6670 | ->getClassInterface(); |
| 6671 | |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6672 | // Determine the type of the property we're synthesizing. |
| 6673 | QualType PropertyType = Context.getObjCIdType(); |
| 6674 | if (Class) { |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 6675 | if (ObjCPropertyDecl *Property = Class->FindPropertyDeclaration( |
| 6676 | PropertyName, ObjCPropertyQueryKind::OBJC_PR_query_instance)) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6677 | PropertyType |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6678 | = Property->getType().getNonReferenceType().getUnqualifiedType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6679 | |
| 6680 | // Give preference to ivars |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6681 | Results.setPreferredType(PropertyType); |
| 6682 | } |
| 6683 | } |
| 6684 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6685 | // Add all of the instance variables in this class and its superclasses. |
| 6686 | Results.EnterNewScope(); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6687 | bool SawSimilarlyNamedIvar = false; |
| 6688 | std::string NameWithPrefix; |
| 6689 | NameWithPrefix += '_'; |
Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 6690 | NameWithPrefix += PropertyName->getName(); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6691 | std::string NameWithSuffix = PropertyName->getName().str(); |
| 6692 | NameWithSuffix += '_'; |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6693 | for(; Class; Class = Class->getSuperClass()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6694 | for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar; |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6695 | Ivar = Ivar->getNextIvar()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6696 | Results.AddResult(Result(Ivar, Results.getBasePriority(Ivar), nullptr), |
| 6697 | CurContext, nullptr, false); |
| 6698 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6699 | // Determine whether we've seen an ivar with a name similar to the |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6700 | // property. |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6701 | if ((PropertyName == Ivar->getIdentifier() || |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6702 | NameWithPrefix == Ivar->getName() || |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6703 | NameWithSuffix == Ivar->getName())) { |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6704 | SawSimilarlyNamedIvar = true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6705 | |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6706 | // Reduce the priority of this result by one, to give it a slight |
| 6707 | // advantage over other results whose names don't match so closely. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6708 | if (Results.size() && |
| 6709 | Results.data()[Results.size() - 1].Kind |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6710 | == CodeCompletionResult::RK_Declaration && |
| 6711 | Results.data()[Results.size() - 1].Declaration == Ivar) |
| 6712 | Results.data()[Results.size() - 1].Priority--; |
| 6713 | } |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6714 | } |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6715 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6716 | |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6717 | if (!SawSimilarlyNamedIvar) { |
| 6718 | // Create ivar result _propName, that the user can use to synthesize |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6719 | // an ivar of the appropriate type. |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6720 | unsigned Priority = CCP_MemberDeclaration + 1; |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6721 | typedef CodeCompletionResult Result; |
| 6722 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6723 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo(), |
| 6724 | Priority,CXAvailability_Available); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6725 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6726 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6727 | Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6728 | Policy, Allocator)); |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6729 | Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6730 | Results.AddResult(Result(Builder.TakeString(), Priority, |
Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6731 | CXCursor_ObjCIvarDecl)); |
| 6732 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6733 | |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6734 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6735 | |
| 6736 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6737 | Results.data(), Results.size()); |
Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6738 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6739 | |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6740 | // Mapping from selectors to the methods that implement that selector, along |
| 6741 | // with the "in original class" flag. |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 6742 | typedef llvm::DenseMap< |
| 6743 | Selector, llvm::PointerIntPair<ObjCMethodDecl *, 1, bool> > KnownMethodsMap; |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6744 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6745 | /// Find all of the methods that reside in the given container |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6746 | /// (and its superclasses, protocols, etc.) that meet the given |
| 6747 | /// criteria. Insert those methods into the map of known methods, |
| 6748 | /// indexed by selector so they can be easily found. |
| 6749 | static void FindImplementableMethods(ASTContext &Context, |
| 6750 | ObjCContainerDecl *Container, |
Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 6751 | Optional<bool> WantInstanceMethods, |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6752 | QualType ReturnType, |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6753 | KnownMethodsMap &KnownMethods, |
| 6754 | bool InOriginalClass = true) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6755 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) { |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6756 | // Make sure we have a definition; that's what we'll walk. |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 6757 | if (!IFace->hasDefinition()) |
| 6758 | return; |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6759 | |
| 6760 | IFace = IFace->getDefinition(); |
| 6761 | Container = IFace; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6762 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6763 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6764 | = IFace->getReferencedProtocols(); |
| 6765 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6766 | E = Protocols.end(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6767 | I != E; ++I) |
| 6768 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6769 | KnownMethods, InOriginalClass); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6770 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6771 | // Add methods from any class extensions and categories. |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 6772 | for (auto *Cat : IFace->visible_categories()) { |
| 6773 | FindImplementableMethods(Context, Cat, WantInstanceMethods, ReturnType, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6774 | KnownMethods, false); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6775 | } |
| 6776 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6777 | // Visit the superclass. |
| 6778 | if (IFace->getSuperClass()) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6779 | FindImplementableMethods(Context, IFace->getSuperClass(), |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6780 | WantInstanceMethods, ReturnType, |
| 6781 | KnownMethods, false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6782 | } |
| 6783 | |
| 6784 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 6785 | // Recurse into protocols. |
| 6786 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6787 | = Category->getReferencedProtocols(); |
| 6788 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6789 | E = Protocols.end(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6790 | I != E; ++I) |
| 6791 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6792 | KnownMethods, InOriginalClass); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6793 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6794 | // If this category is the original class, jump to the interface. |
| 6795 | if (InOriginalClass && Category->getClassInterface()) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6796 | FindImplementableMethods(Context, Category->getClassInterface(), |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6797 | WantInstanceMethods, ReturnType, KnownMethods, |
| 6798 | false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6799 | } |
| 6800 | |
| 6801 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6802 | // Make sure we have a definition; that's what we'll walk. |
| 6803 | if (!Protocol->hasDefinition()) |
| 6804 | return; |
| 6805 | Protocol = Protocol->getDefinition(); |
| 6806 | Container = Protocol; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6807 | |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6808 | // Recurse into protocols. |
| 6809 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6810 | = Protocol->getReferencedProtocols(); |
| 6811 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6812 | E = Protocols.end(); |
Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6813 | I != E; ++I) |
| 6814 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
| 6815 | KnownMethods, false); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6816 | } |
| 6817 | |
| 6818 | // Add methods in this container. This operation occurs last because |
| 6819 | // we want the methods from this container to override any methods |
| 6820 | // we've previously seen with the same selector. |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 6821 | for (auto *M : Container->methods()) { |
Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 6822 | if (!WantInstanceMethods || M->isInstanceMethod() == *WantInstanceMethods) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6823 | if (!ReturnType.isNull() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6824 | !Context.hasSameUnqualifiedType(ReturnType, M->getReturnType())) |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6825 | continue; |
| 6826 | |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 6827 | KnownMethods[M->getSelector()] = |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 6828 | KnownMethodsMap::mapped_type(M, InOriginalClass); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6829 | } |
| 6830 | } |
| 6831 | } |
| 6832 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6833 | /// Add the parenthesized return or parameter type chunk to a code |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6834 | /// completion string. |
| 6835 | static void AddObjCPassingTypeChunk(QualType Type, |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6836 | unsigned ObjCDeclQuals, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6837 | ASTContext &Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6838 | const PrintingPolicy &Policy, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6839 | CodeCompletionBuilder &Builder) { |
| 6840 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 6841 | std::string Quals = formatObjCParamQualifiers(ObjCDeclQuals, Type); |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6842 | if (!Quals.empty()) |
| 6843 | Builder.AddTextChunk(Builder.getAllocator().CopyString(Quals)); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6844 | Builder.AddTextChunk(GetCompletionTypeString(Type, Context, Policy, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6845 | Builder.getAllocator())); |
| 6846 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6847 | } |
| 6848 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6849 | /// Determine whether the given class is or inherits from a class by |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6850 | /// the given name. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6851 | static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6852 | StringRef Name) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6853 | if (!Class) |
| 6854 | return false; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6855 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6856 | if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name) |
| 6857 | return true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6858 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6859 | return InheritsFromClassNamed(Class->getSuperClass(), Name); |
| 6860 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6861 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6862 | /// Add code completions for Objective-C Key-Value Coding (KVC) and |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6863 | /// Key-Value Observing (KVO). |
| 6864 | static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property, |
| 6865 | bool IsInstanceMethod, |
| 6866 | QualType ReturnType, |
| 6867 | ASTContext &Context, |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6868 | VisitedSelectorSet &KnownSelectors, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6869 | ResultBuilder &Results) { |
| 6870 | IdentifierInfo *PropName = Property->getIdentifier(); |
| 6871 | if (!PropName || PropName->getLength() == 0) |
| 6872 | return; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6873 | |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6874 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
| 6875 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6876 | // Builder that will create each code completion. |
| 6877 | typedef CodeCompletionResult Result; |
| 6878 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6879 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6880 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6881 | // The selector table. |
| 6882 | SelectorTable &Selectors = Context.Selectors; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6883 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6884 | // The property name, copied into the code completion allocation region |
| 6885 | // on demand. |
| 6886 | struct KeyHolder { |
| 6887 | CodeCompletionAllocator &Allocator; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6888 | StringRef Key; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6889 | const char *CopiedKey; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6890 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6891 | KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6892 | : Allocator(Allocator), Key(Key), CopiedKey(nullptr) {} |
| 6893 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6894 | operator const char *() { |
| 6895 | if (CopiedKey) |
| 6896 | return CopiedKey; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6897 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6898 | return CopiedKey = Allocator.CopyString(Key); |
| 6899 | } |
| 6900 | } Key(Allocator, PropName->getName()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6901 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6902 | // The uppercased name of the property name. |
| 6903 | std::string UpperKey = PropName->getName(); |
| 6904 | if (!UpperKey.empty()) |
Jordan Rose | 4938f27 | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 6905 | UpperKey[0] = toUppercase(UpperKey[0]); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6906 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6907 | bool ReturnTypeMatchesProperty = ReturnType.isNull() || |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6908 | Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(), |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6909 | Property->getType()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6910 | bool ReturnTypeMatchesVoid |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6911 | = ReturnType.isNull() || ReturnType->isVoidType(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6912 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6913 | // Add the normal accessor -(type)key. |
| 6914 | if (IsInstanceMethod && |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6915 | KnownSelectors.insert(Selectors.getNullarySelector(PropName)).second && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6916 | ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) { |
| 6917 | if (ReturnType.isNull()) |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6918 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, |
| 6919 | Context, Policy, Builder); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6920 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6921 | Builder.AddTypedTextChunk(Key); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6922 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6923 | CXCursor_ObjCInstanceMethodDecl)); |
| 6924 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6925 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6926 | // If we have an integral or boolean property (or the user has provided |
| 6927 | // an integral or boolean return type), add the accessor -(type)isKey. |
| 6928 | if (IsInstanceMethod && |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6929 | ((!ReturnType.isNull() && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6930 | (ReturnType->isIntegerType() || ReturnType->isBooleanType())) || |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6931 | (ReturnType.isNull() && |
| 6932 | (Property->getType()->isIntegerType() || |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6933 | Property->getType()->isBooleanType())))) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6934 | std::string SelectorName = (Twine("is") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6935 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6936 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 6937 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6938 | if (ReturnType.isNull()) { |
| 6939 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6940 | Builder.AddTextChunk("BOOL"); |
| 6941 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6942 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6943 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6944 | Builder.AddTypedTextChunk( |
| 6945 | Allocator.CopyString(SelectorId->getName())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6946 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6947 | CXCursor_ObjCInstanceMethodDecl)); |
| 6948 | } |
| 6949 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6950 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6951 | // Add the normal mutator. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6952 | if (IsInstanceMethod && ReturnTypeMatchesVoid && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6953 | !Property->getSetterMethodDecl()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6954 | std::string SelectorName = (Twine("set") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6955 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6956 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6957 | if (ReturnType.isNull()) { |
| 6958 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6959 | Builder.AddTextChunk("void"); |
| 6960 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6961 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6962 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6963 | Builder.AddTypedTextChunk( |
| 6964 | Allocator.CopyString(SelectorId->getName())); |
| 6965 | Builder.AddTypedTextChunk(":"); |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6966 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, |
| 6967 | Context, Policy, Builder); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6968 | Builder.AddTextChunk(Key); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6969 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6970 | CXCursor_ObjCInstanceMethodDecl)); |
| 6971 | } |
| 6972 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6973 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6974 | // Indexed and unordered accessors |
| 6975 | unsigned IndexedGetterPriority = CCP_CodePattern; |
| 6976 | unsigned IndexedSetterPriority = CCP_CodePattern; |
| 6977 | unsigned UnorderedGetterPriority = CCP_CodePattern; |
| 6978 | unsigned UnorderedSetterPriority = CCP_CodePattern; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6979 | if (const ObjCObjectPointerType *ObjCPointer |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6980 | = Property->getType()->getAs<ObjCObjectPointerType>()) { |
| 6981 | if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) { |
| 6982 | // If this interface type is not provably derived from a known |
| 6983 | // collection, penalize the corresponding completions. |
| 6984 | if (!InheritsFromClassNamed(IFace, "NSMutableArray")) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6985 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6986 | if (!InheritsFromClassNamed(IFace, "NSArray")) |
| 6987 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6988 | } |
| 6989 | |
| 6990 | if (!InheritsFromClassNamed(IFace, "NSMutableSet")) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6991 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6992 | if (!InheritsFromClassNamed(IFace, "NSSet")) |
| 6993 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6994 | } |
| 6995 | } |
| 6996 | } else { |
| 6997 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6998 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 6999 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 7000 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 7001 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7002 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7003 | // Add -(NSUInteger)countOf<key> |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7004 | if (IsInstanceMethod && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7005 | (ReturnType.isNull() || ReturnType->isIntegerType())) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7006 | std::string SelectorName = (Twine("countOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7007 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7008 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7009 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7010 | if (ReturnType.isNull()) { |
| 7011 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7012 | Builder.AddTextChunk("NSUInteger"); |
| 7013 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7014 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7015 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7016 | Builder.AddTypedTextChunk( |
| 7017 | Allocator.CopyString(SelectorId->getName())); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7018 | Results.AddResult(Result(Builder.TakeString(), |
| 7019 | std::min(IndexedGetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7020 | UnorderedGetterPriority), |
| 7021 | CXCursor_ObjCInstanceMethodDecl)); |
| 7022 | } |
| 7023 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7024 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7025 | // Indexed getters |
| 7026 | // Add -(id)objectInKeyAtIndex:(NSUInteger)index |
| 7027 | if (IsInstanceMethod && |
| 7028 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7029 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7030 | = (Twine("objectIn") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7031 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7032 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7033 | if (ReturnType.isNull()) { |
| 7034 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7035 | Builder.AddTextChunk("id"); |
| 7036 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7037 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7038 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7039 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7040 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7041 | Builder.AddTextChunk("NSUInteger"); |
| 7042 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7043 | Builder.AddTextChunk("index"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7044 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7045 | CXCursor_ObjCInstanceMethodDecl)); |
| 7046 | } |
| 7047 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7048 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7049 | // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes |
| 7050 | if (IsInstanceMethod && |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7051 | (ReturnType.isNull() || |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7052 | (ReturnType->isObjCObjectPointerType() && |
| 7053 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 7054 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 7055 | ->getName() == "NSArray"))) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7056 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7057 | = (Twine(Property->getName()) + "AtIndexes").str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7058 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7059 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7060 | if (ReturnType.isNull()) { |
| 7061 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7062 | Builder.AddTextChunk("NSArray *"); |
| 7063 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7064 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7065 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7066 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7067 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7068 | Builder.AddTextChunk("NSIndexSet *"); |
| 7069 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7070 | Builder.AddTextChunk("indexes"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7071 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7072 | CXCursor_ObjCInstanceMethodDecl)); |
| 7073 | } |
| 7074 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7075 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7076 | // Add -(void)getKey:(type **)buffer range:(NSRange)inRange |
| 7077 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7078 | std::string SelectorName = (Twine("get") + UpperKey).str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7079 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7080 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7081 | &Context.Idents.get("range") |
| 7082 | }; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7083 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7084 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7085 | if (ReturnType.isNull()) { |
| 7086 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7087 | Builder.AddTextChunk("void"); |
| 7088 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7089 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7090 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7091 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7092 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7093 | Builder.AddPlaceholderChunk("object-type"); |
| 7094 | Builder.AddTextChunk(" **"); |
| 7095 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7096 | Builder.AddTextChunk("buffer"); |
| 7097 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7098 | Builder.AddTypedTextChunk("range:"); |
| 7099 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7100 | Builder.AddTextChunk("NSRange"); |
| 7101 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7102 | Builder.AddTextChunk("inRange"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7103 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7104 | CXCursor_ObjCInstanceMethodDecl)); |
| 7105 | } |
| 7106 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7107 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7108 | // Mutable indexed accessors |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7109 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7110 | // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index |
| 7111 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7112 | std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7113 | IdentifierInfo *SelectorIds[2] = { |
| 7114 | &Context.Idents.get("insertObject"), |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7115 | &Context.Idents.get(SelectorName) |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7116 | }; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7117 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7118 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7119 | if (ReturnType.isNull()) { |
| 7120 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7121 | Builder.AddTextChunk("void"); |
| 7122 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7123 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7124 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7125 | Builder.AddTypedTextChunk("insertObject:"); |
| 7126 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7127 | Builder.AddPlaceholderChunk("object-type"); |
| 7128 | Builder.AddTextChunk(" *"); |
| 7129 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7130 | Builder.AddTextChunk("object"); |
| 7131 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7132 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7133 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7134 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 7135 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7136 | Builder.AddTextChunk("index"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7137 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7138 | CXCursor_ObjCInstanceMethodDecl)); |
| 7139 | } |
| 7140 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7141 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7142 | // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes |
| 7143 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7144 | std::string SelectorName = (Twine("insert") + UpperKey).str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7145 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7146 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7147 | &Context.Idents.get("atIndexes") |
| 7148 | }; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7149 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7150 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7151 | if (ReturnType.isNull()) { |
| 7152 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7153 | Builder.AddTextChunk("void"); |
| 7154 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7155 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7156 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7157 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7158 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7159 | Builder.AddTextChunk("NSArray *"); |
| 7160 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7161 | Builder.AddTextChunk("array"); |
| 7162 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7163 | Builder.AddTypedTextChunk("atIndexes:"); |
| 7164 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7165 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 7166 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7167 | Builder.AddTextChunk("indexes"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7168 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7169 | CXCursor_ObjCInstanceMethodDecl)); |
| 7170 | } |
| 7171 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7172 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7173 | // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index |
| 7174 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7175 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7176 | = (Twine("removeObjectFrom") + UpperKey + "AtIndex").str(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7177 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7178 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7179 | if (ReturnType.isNull()) { |
| 7180 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7181 | Builder.AddTextChunk("void"); |
| 7182 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7183 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7184 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7185 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7186 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7187 | Builder.AddTextChunk("NSUInteger"); |
| 7188 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7189 | Builder.AddTextChunk("index"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7190 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7191 | CXCursor_ObjCInstanceMethodDecl)); |
| 7192 | } |
| 7193 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7194 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7195 | // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes |
| 7196 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7197 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7198 | = (Twine("remove") + UpperKey + "AtIndexes").str(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7199 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7200 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7201 | if (ReturnType.isNull()) { |
| 7202 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7203 | Builder.AddTextChunk("void"); |
| 7204 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7205 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7206 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7207 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7208 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7209 | Builder.AddTextChunk("NSIndexSet *"); |
| 7210 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7211 | Builder.AddTextChunk("indexes"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7212 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7213 | CXCursor_ObjCInstanceMethodDecl)); |
| 7214 | } |
| 7215 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7216 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7217 | // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object |
| 7218 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7219 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7220 | = (Twine("replaceObjectIn") + UpperKey + "AtIndex").str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7221 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7222 | &Context.Idents.get(SelectorName), |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7223 | &Context.Idents.get("withObject") |
| 7224 | }; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7225 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7226 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7227 | if (ReturnType.isNull()) { |
| 7228 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7229 | Builder.AddTextChunk("void"); |
| 7230 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7231 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7232 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7233 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7234 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7235 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 7236 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7237 | Builder.AddTextChunk("index"); |
| 7238 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7239 | Builder.AddTypedTextChunk("withObject:"); |
| 7240 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7241 | Builder.AddTextChunk("id"); |
| 7242 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7243 | Builder.AddTextChunk("object"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7244 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7245 | CXCursor_ObjCInstanceMethodDecl)); |
| 7246 | } |
| 7247 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7248 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7249 | // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array |
| 7250 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7251 | std::string SelectorName1 |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7252 | = (Twine("replace") + UpperKey + "AtIndexes").str(); |
| 7253 | std::string SelectorName2 = (Twine("with") + UpperKey).str(); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7254 | IdentifierInfo *SelectorIds[2] = { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7255 | &Context.Idents.get(SelectorName1), |
| 7256 | &Context.Idents.get(SelectorName2) |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7257 | }; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7258 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7259 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7260 | if (ReturnType.isNull()) { |
| 7261 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7262 | Builder.AddTextChunk("void"); |
| 7263 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7264 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7265 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7266 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":")); |
| 7267 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7268 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 7269 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7270 | Builder.AddTextChunk("indexes"); |
| 7271 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7272 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":")); |
| 7273 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7274 | Builder.AddTextChunk("NSArray *"); |
| 7275 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7276 | Builder.AddTextChunk("array"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7277 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7278 | CXCursor_ObjCInstanceMethodDecl)); |
| 7279 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7280 | } |
| 7281 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7282 | // Unordered getters |
| 7283 | // - (NSEnumerator *)enumeratorOfKey |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7284 | if (IsInstanceMethod && |
| 7285 | (ReturnType.isNull() || |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7286 | (ReturnType->isObjCObjectPointerType() && |
| 7287 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 7288 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 7289 | ->getName() == "NSEnumerator"))) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7290 | std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7291 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7292 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7293 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7294 | if (ReturnType.isNull()) { |
| 7295 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7296 | Builder.AddTextChunk("NSEnumerator *"); |
| 7297 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7298 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7299 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7300 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7301 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7302 | CXCursor_ObjCInstanceMethodDecl)); |
| 7303 | } |
| 7304 | } |
| 7305 | |
| 7306 | // - (type *)memberOfKey:(type *)object |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7307 | if (IsInstanceMethod && |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7308 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7309 | std::string SelectorName = (Twine("memberOf") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7310 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7311 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7312 | if (ReturnType.isNull()) { |
| 7313 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7314 | Builder.AddPlaceholderChunk("object-type"); |
| 7315 | Builder.AddTextChunk(" *"); |
| 7316 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7317 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7318 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7319 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7320 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7321 | if (ReturnType.isNull()) { |
| 7322 | Builder.AddPlaceholderChunk("object-type"); |
| 7323 | Builder.AddTextChunk(" *"); |
| 7324 | } else { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7325 | Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7326 | Policy, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7327 | Builder.getAllocator())); |
| 7328 | } |
| 7329 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7330 | Builder.AddTextChunk("object"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7331 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7332 | CXCursor_ObjCInstanceMethodDecl)); |
| 7333 | } |
| 7334 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7335 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7336 | // Mutable unordered accessors |
| 7337 | // - (void)addKeyObject:(type *)object |
| 7338 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7339 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7340 | = (Twine("add") + UpperKey + Twine("Object")).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7341 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7342 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7343 | if (ReturnType.isNull()) { |
| 7344 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7345 | Builder.AddTextChunk("void"); |
| 7346 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7347 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7348 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7349 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7350 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7351 | Builder.AddPlaceholderChunk("object-type"); |
| 7352 | Builder.AddTextChunk(" *"); |
| 7353 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7354 | Builder.AddTextChunk("object"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7355 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7356 | CXCursor_ObjCInstanceMethodDecl)); |
| 7357 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7358 | } |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7359 | |
| 7360 | // - (void)addKey:(NSSet *)objects |
| 7361 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7362 | std::string SelectorName = (Twine("add") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7363 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7364 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7365 | if (ReturnType.isNull()) { |
| 7366 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7367 | Builder.AddTextChunk("void"); |
| 7368 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7369 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7370 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7371 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7372 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7373 | Builder.AddTextChunk("NSSet *"); |
| 7374 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7375 | Builder.AddTextChunk("objects"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7376 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7377 | CXCursor_ObjCInstanceMethodDecl)); |
| 7378 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7379 | } |
| 7380 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7381 | // - (void)removeKeyObject:(type *)object |
| 7382 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7383 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7384 | = (Twine("remove") + UpperKey + Twine("Object")).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7385 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7386 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7387 | if (ReturnType.isNull()) { |
| 7388 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7389 | Builder.AddTextChunk("void"); |
| 7390 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7391 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7392 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7393 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7394 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7395 | Builder.AddPlaceholderChunk("object-type"); |
| 7396 | Builder.AddTextChunk(" *"); |
| 7397 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7398 | Builder.AddTextChunk("object"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7399 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7400 | CXCursor_ObjCInstanceMethodDecl)); |
| 7401 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7402 | } |
| 7403 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7404 | // - (void)removeKey:(NSSet *)objects |
| 7405 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7406 | std::string SelectorName = (Twine("remove") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7407 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7408 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7409 | if (ReturnType.isNull()) { |
| 7410 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7411 | Builder.AddTextChunk("void"); |
| 7412 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7413 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7414 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7415 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7416 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7417 | Builder.AddTextChunk("NSSet *"); |
| 7418 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7419 | Builder.AddTextChunk("objects"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7420 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7421 | CXCursor_ObjCInstanceMethodDecl)); |
| 7422 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7423 | } |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7424 | |
| 7425 | // - (void)intersectKey:(NSSet *)objects |
| 7426 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7427 | std::string SelectorName = (Twine("intersect") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7428 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7429 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7430 | if (ReturnType.isNull()) { |
| 7431 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7432 | Builder.AddTextChunk("void"); |
| 7433 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7434 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7435 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7436 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7437 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7438 | Builder.AddTextChunk("NSSet *"); |
| 7439 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7440 | Builder.AddTextChunk("objects"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7441 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7442 | CXCursor_ObjCInstanceMethodDecl)); |
| 7443 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7444 | } |
| 7445 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7446 | // Key-Value Observing |
| 7447 | // + (NSSet *)keyPathsForValuesAffectingKey |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7448 | if (!IsInstanceMethod && |
| 7449 | (ReturnType.isNull() || |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7450 | (ReturnType->isObjCObjectPointerType() && |
| 7451 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 7452 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 7453 | ->getName() == "NSSet"))) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7454 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7455 | = (Twine("keyPathsForValuesAffecting") + UpperKey).str(); |
Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7456 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7457 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7458 | .second) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7459 | if (ReturnType.isNull()) { |
| 7460 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
Alex Lorenz | 71ecb07 | 2016-12-08 16:49:05 +0000 | [diff] [blame] | 7461 | Builder.AddTextChunk("NSSet<NSString *> *"); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7462 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7463 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7464 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7465 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7466 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7467 | CXCursor_ObjCClassMethodDecl)); |
| 7468 | } |
| 7469 | } |
| 7470 | |
| 7471 | // + (BOOL)automaticallyNotifiesObserversForKey |
| 7472 | if (!IsInstanceMethod && |
| 7473 | (ReturnType.isNull() || |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7474 | ReturnType->isIntegerType() || |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7475 | ReturnType->isBooleanType())) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7476 | std::string SelectorName |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7477 | = (Twine("automaticallyNotifiesObserversOf") + UpperKey).str(); |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7478 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7479 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7480 | .second) { |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7481 | if (ReturnType.isNull()) { |
| 7482 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7483 | Builder.AddTextChunk("BOOL"); |
| 7484 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7485 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7486 | |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7487 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7488 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7489 | CXCursor_ObjCClassMethodDecl)); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7490 | } |
| 7491 | } |
| 7492 | } |
| 7493 | |
Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 7494 | void Sema::CodeCompleteObjCMethodDecl(Scope *S, Optional<bool> IsInstanceMethod, |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7495 | ParsedType ReturnTy) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7496 | // Determine the return type of the method we're declaring, if |
| 7497 | // provided. |
| 7498 | QualType ReturnType = GetTypeFromParser(ReturnTy); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7499 | Decl *IDecl = nullptr; |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7500 | if (CurContext->isObjCContainer()) { |
| 7501 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 7502 | IDecl = OCD; |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7503 | } |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7504 | // Determine where we should start searching for methods. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7505 | ObjCContainerDecl *SearchDecl = nullptr; |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7506 | bool IsInImplementation = false; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7507 | if (Decl *D = IDecl) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7508 | if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) { |
| 7509 | SearchDecl = Impl->getClassInterface(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7510 | IsInImplementation = true; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7511 | } else if (ObjCCategoryImplDecl *CatImpl |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7512 | = dyn_cast<ObjCCategoryImplDecl>(D)) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7513 | SearchDecl = CatImpl->getCategoryDecl(); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7514 | IsInImplementation = true; |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7515 | } else |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7516 | SearchDecl = dyn_cast<ObjCContainerDecl>(D); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7517 | } |
| 7518 | |
| 7519 | if (!SearchDecl && S) { |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 7520 | if (DeclContext *DC = S->getEntity()) |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7521 | SearchDecl = dyn_cast<ObjCContainerDecl>(DC); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7522 | } |
| 7523 | |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7524 | if (!SearchDecl) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7525 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 7526 | CodeCompletionContext::CCC_Other, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7527 | nullptr, 0); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7528 | return; |
| 7529 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7530 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7531 | // Find all of the methods that we could declare/implement here. |
| 7532 | KnownMethodsMap KnownMethods; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7533 | FindImplementableMethods(Context, SearchDecl, IsInstanceMethod, |
Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7534 | ReturnType, KnownMethods); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7535 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7536 | // Add declarations or definitions for each of the known methods. |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7537 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7538 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7539 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7540 | CodeCompletionContext::CCC_Other); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7541 | Results.EnterNewScope(); |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7542 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7543 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7544 | MEnd = KnownMethods.end(); |
| 7545 | M != MEnd; ++M) { |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 7546 | ObjCMethodDecl *Method = M->second.getPointer(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7547 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7548 | Results.getCodeCompletionTUInfo()); |
Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 7549 | |
| 7550 | // Add the '-'/'+' prefix if it wasn't provided yet. |
| 7551 | if (!IsInstanceMethod) { |
| 7552 | Builder.AddTextChunk(Method->isInstanceMethod() ? "-" : "+"); |
| 7553 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7554 | } |
| 7555 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7556 | // If the result type was not already provided, add it to the |
| 7557 | // pattern as (type). |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7558 | if (ReturnType.isNull()) { |
| 7559 | QualType ResTy = Method->getSendResultType().stripObjCKindOfType(Context); |
| 7560 | AttributedType::stripOuterNullability(ResTy); |
| 7561 | AddObjCPassingTypeChunk(ResTy, |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7562 | Method->getObjCDeclQualifier(), Context, Policy, |
| 7563 | Builder); |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7564 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7565 | |
| 7566 | Selector Sel = Method->getSelector(); |
| 7567 | |
| 7568 | // Add the first part of the selector to the pattern. |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7569 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 7570 | Sel.getNameForSlot(0))); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7571 | |
| 7572 | // Add parameters to the pattern. |
| 7573 | unsigned I = 0; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7574 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7575 | PEnd = Method->param_end(); |
| 7576 | P != PEnd; (void)++P, ++I) { |
| 7577 | // Add the part of the selector name. |
| 7578 | if (I == 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7579 | Builder.AddTypedTextChunk(":"); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7580 | else if (I < Sel.getNumArgs()) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7581 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7582 | Builder.AddTypedTextChunk( |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 7583 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7584 | } else |
| 7585 | break; |
| 7586 | |
| 7587 | // Add the parameter type. |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 7588 | QualType ParamType; |
| 7589 | if ((*P)->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 7590 | ParamType = (*P)->getType(); |
| 7591 | else |
| 7592 | ParamType = (*P)->getOriginalType(); |
Douglas Gregor | 9b7b3e9 | 2015-07-07 06:20:27 +0000 | [diff] [blame] | 7593 | ParamType = ParamType.substObjCTypeArgs(Context, {}, |
| 7594 | ObjCSubstitutionContext::Parameter); |
Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7595 | AttributedType::stripOuterNullability(ParamType); |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 7596 | AddObjCPassingTypeChunk(ParamType, |
Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 7597 | (*P)->getObjCDeclQualifier(), |
| 7598 | Context, Policy, |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7599 | Builder); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7600 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7601 | if (IdentifierInfo *Id = (*P)->getIdentifier()) |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7602 | Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName())); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7603 | } |
| 7604 | |
| 7605 | if (Method->isVariadic()) { |
| 7606 | if (Method->param_size() > 0) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7607 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 7608 | Builder.AddTextChunk("..."); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7609 | } |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7610 | |
Douglas Gregor | d37c59d | 2010-05-28 00:57:46 +0000 | [diff] [blame] | 7611 | if (IsInImplementation && Results.includeCodePatterns()) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7612 | // We will be defining the method here, so add a compound statement. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7613 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7614 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 7615 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7616 | if (!Method->getReturnType()->isVoidType()) { |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7617 | // If the result type is not void, add a return clause. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7618 | Builder.AddTextChunk("return"); |
| 7619 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7620 | Builder.AddPlaceholderChunk("expression"); |
| 7621 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7622 | } else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7623 | Builder.AddPlaceholderChunk("statements"); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7624 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7625 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 7626 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7627 | } |
| 7628 | |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 7629 | unsigned Priority = CCP_CodePattern; |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 7630 | if (!M->second.getInt()) |
Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 7631 | Priority += CCD_InBaseClass; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7632 | |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 7633 | Results.AddResult(Result(Builder.TakeString(), Method, Priority)); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7634 | } |
| 7635 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7636 | // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7637 | // the properties in this class and its categories. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7638 | if (Context.getLangOpts().ObjC2) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7639 | SmallVector<ObjCContainerDecl *, 4> Containers; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7640 | Containers.push_back(SearchDecl); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7641 | |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 7642 | VisitedSelectorSet KnownSelectors; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7643 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 7644 | MEnd = KnownMethods.end(); |
| 7645 | M != MEnd; ++M) |
| 7646 | KnownSelectors.insert(M->first); |
| 7647 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7648 | |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7649 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl); |
| 7650 | if (!IFace) |
| 7651 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl)) |
| 7652 | IFace = Category->getClassInterface(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7653 | |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 7654 | if (IFace) |
| 7655 | for (auto *Cat : IFace->visible_categories()) |
| 7656 | Containers.push_back(Cat); |
Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 7657 | |
| 7658 | if (IsInstanceMethod) { |
| 7659 | for (unsigned I = 0, N = Containers.size(); I != N; ++I) |
| 7660 | for (auto *P : Containers[I]->instance_properties()) |
| 7661 | AddObjCKeyValueCompletions(P, *IsInstanceMethod, ReturnType, Context, |
| 7662 | KnownSelectors, Results); |
| 7663 | } |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7664 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7665 | |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7666 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7667 | |
| 7668 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7669 | Results.data(), Results.size()); |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7670 | } |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7671 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7672 | void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S, |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7673 | bool IsInstanceMethod, |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7674 | bool AtParameterName, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7675 | ParsedType ReturnTy, |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7676 | ArrayRef<IdentifierInfo *> SelIdents) { |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7677 | // If we have an external source, load the entire class method |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 7678 | // pool from the AST file. |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7679 | if (ExternalSource) { |
| 7680 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 7681 | I != N; ++I) { |
| 7682 | Selector Sel = ExternalSource->GetExternalSelector(I); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7683 | if (Sel.isNull() || MethodPool.count(Sel)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7684 | continue; |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7685 | |
| 7686 | ReadMethodPool(Sel); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7687 | } |
| 7688 | } |
| 7689 | |
| 7690 | // Build the set of methods we can see. |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7691 | typedef CodeCompletionResult Result; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7692 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7693 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7694 | CodeCompletionContext::CCC_Other); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7695 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7696 | if (ReturnTy) |
| 7697 | Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType()); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7698 | |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7699 | Results.EnterNewScope(); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7700 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 7701 | MEnd = MethodPool.end(); |
| 7702 | M != MEnd; ++M) { |
| 7703 | for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first : |
| 7704 | &M->second.second; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7705 | MethList && MethList->getMethod(); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 7706 | MethList = MethList->getNext()) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7707 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7708 | continue; |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7709 | |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7710 | if (AtParameterName) { |
| 7711 | // Suggest parameter names we've seen before. |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7712 | unsigned NumSelIdents = SelIdents.size(); |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7713 | if (NumSelIdents && |
| 7714 | NumSelIdents <= MethList->getMethod()->param_size()) { |
| 7715 | ParmVarDecl *Param = |
| 7716 | MethList->getMethod()->parameters()[NumSelIdents - 1]; |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7717 | if (Param->getIdentifier()) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7718 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7719 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7720 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7721 | Param->getIdentifier()->getName())); |
| 7722 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7723 | } |
| 7724 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7725 | |
Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7726 | continue; |
| 7727 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7728 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7729 | Result R(MethList->getMethod(), |
| 7730 | Results.getBasePriority(MethList->getMethod()), nullptr); |
Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7731 | R.StartParameter = SelIdents.size(); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7732 | R.AllParametersAreInformative = false; |
| 7733 | R.DeclaringEntity = true; |
| 7734 | Results.MaybeAddResult(R, CurContext); |
| 7735 | } |
| 7736 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7737 | |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7738 | Results.ExitScope(); |
Alex Lorenz | 847fda1 | 2017-01-03 11:56:40 +0000 | [diff] [blame] | 7739 | |
| 7740 | if (!AtParameterName && !SelIdents.empty() && |
| 7741 | SelIdents.front()->getName().startswith("init")) { |
| 7742 | for (const auto &M : PP.macros()) { |
| 7743 | if (M.first->getName() != "NS_DESIGNATED_INITIALIZER") |
| 7744 | continue; |
| 7745 | Results.EnterNewScope(); |
| 7746 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7747 | Results.getCodeCompletionTUInfo()); |
| 7748 | Builder.AddTypedTextChunk( |
| 7749 | Builder.getAllocator().CopyString(M.first->getName())); |
| 7750 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), CCP_Macro, |
| 7751 | CXCursor_MacroDefinition)); |
| 7752 | Results.ExitScope(); |
| 7753 | } |
| 7754 | } |
| 7755 | |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7756 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7757 | Results.data(), Results.size()); |
Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7758 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 7759 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7760 | void Sema::CodeCompletePreprocessorDirective(bool InConditional) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7761 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7762 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7763 | CodeCompletionContext::CCC_PreprocessorDirective); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7764 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7765 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7766 | // #if <condition> |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7767 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7768 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7769 | Builder.AddTypedTextChunk("if"); |
| 7770 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7771 | Builder.AddPlaceholderChunk("condition"); |
| 7772 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7773 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7774 | // #ifdef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7775 | Builder.AddTypedTextChunk("ifdef"); |
| 7776 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7777 | Builder.AddPlaceholderChunk("macro"); |
| 7778 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7779 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7780 | // #ifndef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7781 | Builder.AddTypedTextChunk("ifndef"); |
| 7782 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7783 | Builder.AddPlaceholderChunk("macro"); |
| 7784 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7785 | |
| 7786 | if (InConditional) { |
| 7787 | // #elif <condition> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7788 | Builder.AddTypedTextChunk("elif"); |
| 7789 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7790 | Builder.AddPlaceholderChunk("condition"); |
| 7791 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7792 | |
| 7793 | // #else |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7794 | Builder.AddTypedTextChunk("else"); |
| 7795 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7796 | |
| 7797 | // #endif |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7798 | Builder.AddTypedTextChunk("endif"); |
| 7799 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7800 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7801 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7802 | // #include "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7803 | Builder.AddTypedTextChunk("include"); |
| 7804 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7805 | Builder.AddTextChunk("\""); |
| 7806 | Builder.AddPlaceholderChunk("header"); |
| 7807 | Builder.AddTextChunk("\""); |
| 7808 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7809 | |
| 7810 | // #include <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7811 | Builder.AddTypedTextChunk("include"); |
| 7812 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7813 | Builder.AddTextChunk("<"); |
| 7814 | Builder.AddPlaceholderChunk("header"); |
| 7815 | Builder.AddTextChunk(">"); |
| 7816 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7817 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7818 | // #define <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7819 | Builder.AddTypedTextChunk("define"); |
| 7820 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7821 | Builder.AddPlaceholderChunk("macro"); |
| 7822 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7823 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7824 | // #define <macro>(<args>) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7825 | Builder.AddTypedTextChunk("define"); |
| 7826 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7827 | Builder.AddPlaceholderChunk("macro"); |
| 7828 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7829 | Builder.AddPlaceholderChunk("args"); |
| 7830 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7831 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7832 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7833 | // #undef <macro> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7834 | Builder.AddTypedTextChunk("undef"); |
| 7835 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7836 | Builder.AddPlaceholderChunk("macro"); |
| 7837 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7838 | |
| 7839 | // #line <number> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7840 | Builder.AddTypedTextChunk("line"); |
| 7841 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7842 | Builder.AddPlaceholderChunk("number"); |
| 7843 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7844 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7845 | // #line <number> "filename" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7846 | Builder.AddTypedTextChunk("line"); |
| 7847 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7848 | Builder.AddPlaceholderChunk("number"); |
| 7849 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7850 | Builder.AddTextChunk("\""); |
| 7851 | Builder.AddPlaceholderChunk("filename"); |
| 7852 | Builder.AddTextChunk("\""); |
| 7853 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7854 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7855 | // #error <message> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7856 | Builder.AddTypedTextChunk("error"); |
| 7857 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7858 | Builder.AddPlaceholderChunk("message"); |
| 7859 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7860 | |
| 7861 | // #pragma <arguments> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7862 | Builder.AddTypedTextChunk("pragma"); |
| 7863 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7864 | Builder.AddPlaceholderChunk("arguments"); |
| 7865 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7866 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7867 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7868 | // #import "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7869 | Builder.AddTypedTextChunk("import"); |
| 7870 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7871 | Builder.AddTextChunk("\""); |
| 7872 | Builder.AddPlaceholderChunk("header"); |
| 7873 | Builder.AddTextChunk("\""); |
| 7874 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7875 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7876 | // #import <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7877 | Builder.AddTypedTextChunk("import"); |
| 7878 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7879 | Builder.AddTextChunk("<"); |
| 7880 | Builder.AddPlaceholderChunk("header"); |
| 7881 | Builder.AddTextChunk(">"); |
| 7882 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7883 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7884 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7885 | // #include_next "header" |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7886 | Builder.AddTypedTextChunk("include_next"); |
| 7887 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7888 | Builder.AddTextChunk("\""); |
| 7889 | Builder.AddPlaceholderChunk("header"); |
| 7890 | Builder.AddTextChunk("\""); |
| 7891 | Results.AddResult(Builder.TakeString()); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7892 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7893 | // #include_next <header> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7894 | Builder.AddTypedTextChunk("include_next"); |
| 7895 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7896 | Builder.AddTextChunk("<"); |
| 7897 | Builder.AddPlaceholderChunk("header"); |
| 7898 | Builder.AddTextChunk(">"); |
| 7899 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7900 | |
| 7901 | // #warning <message> |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7902 | Builder.AddTypedTextChunk("warning"); |
| 7903 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7904 | Builder.AddPlaceholderChunk("message"); |
| 7905 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7906 | |
| 7907 | // Note: #ident and #sccs are such crazy anachronisms that we don't provide |
| 7908 | // completions for them. And __include_macros is a Clang-internal extension |
| 7909 | // that we don't want to encourage anyone to use. |
| 7910 | |
| 7911 | // FIXME: we don't support #assert or #unassert, so don't suggest them. |
| 7912 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7913 | |
| 7914 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7915 | Results.data(), Results.size()); |
| 7916 | } |
| 7917 | |
| 7918 | void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) { |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7919 | CodeCompleteOrdinaryName(S, |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7920 | S->getFnParent()? Sema::PCC_RecoveryInFunction |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7921 | : Sema::PCC_Namespace); |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7922 | } |
| 7923 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7924 | void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7925 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7926 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7927 | IsDefinition? CodeCompletionContext::CCC_MacroName |
| 7928 | : CodeCompletionContext::CCC_MacroNameUse); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7929 | if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7930 | // Add just the names of macros, not their arguments. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7931 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7932 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7933 | Results.EnterNewScope(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7934 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7935 | MEnd = PP.macro_end(); |
| 7936 | M != MEnd; ++M) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7937 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7938 | M->first->getName())); |
Argyrios Kyrtzidis | 5c8b1cd | 2012-09-27 00:24:09 +0000 | [diff] [blame] | 7939 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
| 7940 | CCP_CodePattern, |
| 7941 | CXCursor_MacroDefinition)); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7942 | } |
| 7943 | Results.ExitScope(); |
| 7944 | } else if (IsDefinition) { |
| 7945 | // FIXME: Can we detect when the user just wrote an include guard above? |
| 7946 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7947 | |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7948 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7949 | Results.data(), Results.size()); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7950 | } |
| 7951 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7952 | void Sema::CodeCompletePreprocessorExpression() { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7953 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7954 | CodeCompleter->getCodeCompletionTUInfo(), |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7955 | CodeCompletionContext::CCC_PreprocessorExpression); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7956 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7957 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 7958 | AddMacroResults(PP, Results, true); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7959 | |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7960 | // defined (<macro>) |
| 7961 | Results.EnterNewScope(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7962 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7963 | Results.getCodeCompletionTUInfo()); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7964 | Builder.AddTypedTextChunk("defined"); |
| 7965 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7966 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7967 | Builder.AddPlaceholderChunk("macro"); |
| 7968 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7969 | Results.AddResult(Builder.TakeString()); |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7970 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7971 | |
| 7972 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7973 | Results.data(), Results.size()); |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7974 | } |
| 7975 | |
| 7976 | void Sema::CodeCompletePreprocessorMacroArgument(Scope *S, |
| 7977 | IdentifierInfo *Macro, |
| 7978 | MacroInfo *MacroInfo, |
| 7979 | unsigned Argument) { |
| 7980 | // FIXME: In the future, we could provide "overload" results, much like we |
| 7981 | // do for function calls. |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7982 | |
Argyrios Kyrtzidis | 75f6cd2 | 2011-08-18 19:41:28 +0000 | [diff] [blame] | 7983 | // Now just ignore this. There will be another code-completion callback |
| 7984 | // for the expanded tokens. |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7985 | } |
| 7986 | |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 7987 | void Sema::CodeCompleteNaturalLanguage() { |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 7988 | HandleCodeCompleteResults(this, CodeCompleter, |
Douglas Gregor | ea73637 | 2010-08-25 17:10:00 +0000 | [diff] [blame] | 7989 | CodeCompletionContext::CCC_NaturalLanguage, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7990 | nullptr, 0); |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 7991 | } |
| 7992 | |
Alex Lorenz | f7f6f82 | 2017-05-09 16:05:04 +0000 | [diff] [blame] | 7993 | void Sema::CodeCompleteAvailabilityPlatformName() { |
| 7994 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 7995 | CodeCompleter->getCodeCompletionTUInfo(), |
| 7996 | CodeCompletionContext::CCC_Other); |
| 7997 | Results.EnterNewScope(); |
| 7998 | static const char *Platforms[] = {"macOS", "iOS", "watchOS", "tvOS"}; |
| 7999 | for (const char *Platform : llvm::makeArrayRef(Platforms)) { |
| 8000 | Results.AddResult(CodeCompletionResult(Platform)); |
| 8001 | Results.AddResult(CodeCompletionResult(Results.getAllocator().CopyString( |
| 8002 | Twine(Platform) + "ApplicationExtension"))); |
| 8003 | } |
| 8004 | Results.ExitScope(); |
Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 8005 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 8006 | Results.data(), Results.size()); |
Alex Lorenz | f7f6f82 | 2017-05-09 16:05:04 +0000 | [diff] [blame] | 8007 | } |
| 8008 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 8009 | void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator, |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8010 | CodeCompletionTUInfo &CCTUInfo, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8011 | SmallVectorImpl<CodeCompletionResult> &Results) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8012 | ResultBuilder Builder(*this, Allocator, CCTUInfo, |
| 8013 | CodeCompletionContext::CCC_Recovery); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 8014 | if (!CodeCompleter || CodeCompleter->includeGlobals()) { |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8015 | CodeCompletionDeclConsumer Consumer(Builder, |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 8016 | Context.getTranslationUnitDecl()); |
Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 8017 | LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName, |
| 8018 | Consumer, |
| 8019 | !CodeCompleter || CodeCompleter->loadExternal()); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 8020 | } |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8021 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 8022 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
Douglas Gregor | 8cb1746 | 2012-10-09 16:01:50 +0000 | [diff] [blame] | 8023 | AddMacroResults(PP, Builder, true); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8024 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 8025 | Results.clear(); |
Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8026 | Results.insert(Results.end(), |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 8027 | Builder.data(), Builder.data() + Builder.size()); |
| 8028 | } |