| 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" |
| Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/iterator_range.h" |
| 36 | #include "llvm/Support/Path.h" |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 37 | #include <list> |
| 38 | #include <map> |
| 39 | #include <vector> |
| Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 40 | |
| 41 | using namespace clang; |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 42 | using namespace sema; |
| Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 43 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 44 | namespace { |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 45 | /// A container of code-completion results. |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 46 | class ResultBuilder { |
| 47 | public: |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 48 | /// 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] | 49 | /// name-lookup routines to specify which declarations should be included in |
| 50 | /// the result set (when it returns true) and which declarations should be |
| 51 | /// filtered out (returns false). |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 52 | typedef bool (ResultBuilder::*LookupFilter)(const NamedDecl *) const; |
| Ivan Donchevskii | 13d9054 | 2017-10-27 11:05:40 +0000 | [diff] [blame] | 53 | |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 54 | typedef CodeCompletionResult Result; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 55 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 56 | private: |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 57 | /// The actual results we have found. |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 58 | std::vector<Result> Results; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 59 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 60 | /// A record of all of the declarations we have found and placed |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 61 | /// into the result set, used to ensure that no declaration ever gets into |
| 62 | /// the result set twice. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 63 | llvm::SmallPtrSet<const Decl*, 16> AllDeclsFound; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 64 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 65 | typedef std::pair<const NamedDecl *, unsigned> DeclIndexPair; |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 66 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 67 | /// An entry in the shadow map, which is optimized to store |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 68 | /// a single (declaration, index) mapping (the common case) but |
| 69 | /// can also store a list of (declaration, index) mappings. |
| 70 | class ShadowMapEntry { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 71 | typedef SmallVector<DeclIndexPair, 4> DeclIndexPairVector; |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 72 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 73 | /// Contains either the solitary NamedDecl * or a vector |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 74 | /// of (declaration, index) pairs. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 75 | llvm::PointerUnion<const NamedDecl *, DeclIndexPairVector*> DeclOrVector; |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 76 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 77 | /// When the entry contains a single declaration, this is |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 78 | /// the index associated with that entry. |
| 79 | unsigned SingleDeclIndex; |
| 80 | |
| 81 | public: |
| 82 | ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { } |
| 83 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 84 | void Add(const NamedDecl *ND, unsigned Index) { |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 85 | if (DeclOrVector.isNull()) { |
| 86 | // 0 - > 1 elements: just set the single element information. |
| 87 | DeclOrVector = ND; |
| 88 | SingleDeclIndex = Index; |
| 89 | return; |
| 90 | } |
| 91 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 92 | if (const NamedDecl *PrevND = |
| 93 | DeclOrVector.dyn_cast<const NamedDecl *>()) { |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 94 | // 1 -> 2 elements: create the vector of results and push in the |
| 95 | // existing declaration. |
| 96 | DeclIndexPairVector *Vec = new DeclIndexPairVector; |
| 97 | Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex)); |
| 98 | DeclOrVector = Vec; |
| 99 | } |
| 100 | |
| 101 | // Add the new element to the end of the vector. |
| 102 | DeclOrVector.get<DeclIndexPairVector*>()->push_back( |
| 103 | DeclIndexPair(ND, Index)); |
| 104 | } |
| 105 | |
| 106 | void Destroy() { |
| 107 | if (DeclIndexPairVector *Vec |
| 108 | = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) { |
| 109 | delete Vec; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 110 | DeclOrVector = ((NamedDecl *)nullptr); |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | // Iteration. |
| 115 | class iterator; |
| 116 | iterator begin() const; |
| 117 | iterator end() const; |
| 118 | }; |
| 119 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 120 | /// A mapping from declaration names to the declarations that have |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 121 | /// this name within a particular scope and their index within the list of |
| 122 | /// results. |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 123 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 124 | |
| 125 | /// The semantic analysis object for which results are being |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 126 | /// produced. |
| 127 | Sema &SemaRef; |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 128 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 129 | /// The allocator used to allocate new code-completion strings. |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 130 | CodeCompletionAllocator &Allocator; |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 131 | |
| 132 | CodeCompletionTUInfo &CCTUInfo; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 133 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 134 | /// If non-NULL, a filter function used to remove any code-completion |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 135 | /// results that are not desirable. |
| 136 | LookupFilter Filter; |
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 137 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 138 | /// Whether we should allow declarations as |
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 139 | /// nested-name-specifiers that would otherwise be filtered out. |
| 140 | bool AllowNestedNameSpecifiers; |
| 141 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 142 | /// If set, the type that we would prefer our resulting value |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 143 | /// declarations to have. |
| 144 | /// |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 145 | /// Closely matching the preferred type gives a boost to a result's |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 146 | /// priority. |
| 147 | CanQualType PreferredType; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 148 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 149 | /// 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] | 150 | /// different levels of, e.g., the inheritance hierarchy. |
| 151 | std::list<ShadowMap> ShadowMaps; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 152 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 153 | /// If we're potentially referring to a C++ member function, the set |
| Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 154 | /// of qualifiers applied to the object type. |
| 155 | Qualifiers ObjectTypeQualifiers; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 156 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 157 | /// Whether the \p ObjectTypeQualifiers field is active. |
| Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 158 | bool HasObjectTypeQualifiers; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 159 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 160 | /// The selector that we prefer. |
| Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 161 | Selector PreferredSelector; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 162 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 163 | /// The completion context in which we are gathering results. |
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 164 | CodeCompletionContext CompletionContext; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 165 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 166 | /// If we are in an instance method definition, the \@implementation |
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 167 | /// object. |
| 168 | ObjCImplementationDecl *ObjCImplementation; |
| Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 169 | |
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 170 | void AdjustResultPriorityForDecl(Result &R); |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 171 | |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 172 | void MaybeAddConstructorResults(Result R); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 173 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 174 | public: |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 175 | explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator, |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 176 | CodeCompletionTUInfo &CCTUInfo, |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 177 | const CodeCompletionContext &CompletionContext, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 178 | LookupFilter Filter = nullptr) |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 179 | : SemaRef(SemaRef), Allocator(Allocator), CCTUInfo(CCTUInfo), |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 180 | Filter(Filter), |
| 181 | AllowNestedNameSpecifiers(false), HasObjectTypeQualifiers(false), |
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 182 | CompletionContext(CompletionContext), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 183 | ObjCImplementation(nullptr) |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 184 | { |
| 185 | // If this is an Objective-C instance method definition, dig out the |
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 186 | // corresponding implementation. |
| 187 | switch (CompletionContext.getKind()) { |
| 188 | case CodeCompletionContext::CCC_Expression: |
| 189 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
| 190 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
| 191 | case CodeCompletionContext::CCC_Statement: |
| 192 | case CodeCompletionContext::CCC_Recovery: |
| 193 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) |
| 194 | if (Method->isInstanceMethod()) |
| 195 | if (ObjCInterfaceDecl *Interface = Method->getClassInterface()) |
| 196 | ObjCImplementation = Interface->getImplementation(); |
| 197 | break; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 198 | |
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 199 | default: |
| 200 | break; |
| 201 | } |
| 202 | } |
| Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 203 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 204 | /// Determine the priority for a reference to the given declaration. |
| Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 205 | unsigned getBasePriority(const NamedDecl *D); |
| 206 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 207 | /// Whether we should include code patterns in the completion |
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 208 | /// results. |
| 209 | bool includeCodePatterns() const { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 210 | return SemaRef.CodeCompleter && |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 211 | SemaRef.CodeCompleter->includeCodePatterns(); |
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 212 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 213 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 214 | /// Set the filter used for code-completion results. |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 215 | void setFilter(LookupFilter Filter) { |
| 216 | this->Filter = Filter; |
| 217 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 218 | |
| 219 | Result *data() { return Results.empty()? nullptr : &Results.front(); } |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 220 | unsigned size() const { return Results.size(); } |
| 221 | bool empty() const { return Results.empty(); } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 222 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 223 | /// Specify the preferred type. |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 224 | void setPreferredType(QualType T) { |
| 225 | PreferredType = SemaRef.Context.getCanonicalType(T); |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 226 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 227 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 228 | /// Set the cv-qualifiers on the object type, for us in filtering |
| Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 229 | /// calls to member functions. |
| 230 | /// |
| 231 | /// 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] | 232 | /// out member functions that aren't available (because there will be a |
| Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 233 | /// cv-qualifier mismatch) or prefer functions with an exact qualifier |
| 234 | /// match. |
| 235 | void setObjectTypeQualifiers(Qualifiers Quals) { |
| 236 | ObjectTypeQualifiers = Quals; |
| 237 | HasObjectTypeQualifiers = true; |
| 238 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 239 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 240 | /// Set the preferred selector. |
| Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 241 | /// |
| 242 | /// When an Objective-C method declaration result is added, and that |
| 243 | /// method's selector matches this preferred selector, we give that method |
| 244 | /// a slight priority boost. |
| 245 | void setPreferredSelector(Selector Sel) { |
| 246 | PreferredSelector = Sel; |
| 247 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 248 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 249 | /// Retrieve the code-completion context for which results are |
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 250 | /// being collected. |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 251 | const CodeCompletionContext &getCompletionContext() const { |
| 252 | return CompletionContext; |
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 253 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 254 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 255 | /// Specify whether nested-name-specifiers are allowed. |
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 256 | void allowNestedNameSpecifiers(bool Allow = true) { |
| 257 | AllowNestedNameSpecifiers = Allow; |
| 258 | } |
| 259 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 260 | /// Return the semantic analysis object for which we are collecting |
| Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 261 | /// code completion results. |
| 262 | Sema &getSema() const { return SemaRef; } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 263 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 264 | /// Retrieve the allocator used to allocate code completion strings. |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 265 | CodeCompletionAllocator &getAllocator() const { return Allocator; } |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 266 | |
| 267 | CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 268 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 269 | /// Determine whether the given declaration is at all interesting |
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 270 | /// as a code-completion result. |
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 271 | /// |
| 272 | /// \param ND the declaration that we are inspecting. |
| 273 | /// |
| 274 | /// \param AsNestedNameSpecifier will be set true if this declaration is |
| 275 | /// only interesting when it is a nested-name-specifier. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 276 | bool isInterestingDecl(const NamedDecl *ND, |
| 277 | bool &AsNestedNameSpecifier) const; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 278 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 279 | /// Check whether the result is hidden by the Hiding declaration. |
| Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 280 | /// |
| 281 | /// \returns true if the result is hidden and cannot be found, false if |
| 282 | /// the hidden result could still be found. When false, \p R may be |
| 283 | /// modified to describe how the result can be found (e.g., via extra |
| 284 | /// qualification). |
| 285 | bool CheckHiddenResult(Result &R, DeclContext *CurContext, |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 286 | const NamedDecl *Hiding); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 287 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 288 | /// 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] | 289 | /// 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] | 290 | /// redeclaration). |
| 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 R the result to add (if it is unique). |
| Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 293 | /// |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 294 | /// \param CurContext the context in which this result will be named. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 295 | void MaybeAddResult(Result R, DeclContext *CurContext = nullptr); |
| 296 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 297 | /// Add a new result to this result set, where we already know |
| Yaron Keren | 8fbe4398 | 2014-11-14 18:33:42 +0000 | [diff] [blame] | 298 | /// the hiding declaration (if any). |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 299 | /// |
| 300 | /// \param R the result to add (if it is unique). |
| 301 | /// |
| 302 | /// \param CurContext the context in which this result will be named. |
| 303 | /// |
| 304 | /// \param Hiding the declaration that hides the result. |
| Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 305 | /// |
| 306 | /// \param InBaseClass whether the result was found in a base |
| 307 | /// class of the searched context. |
| 308 | void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding, |
| 309 | bool InBaseClass); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 310 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 311 | /// Add a new non-declaration result to this result set. |
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 312 | void AddResult(Result R); |
| 313 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 314 | /// Enter into a new scope. |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 315 | void EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 316 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 317 | /// Exit from the current scope. |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 318 | void ExitScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 319 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 320 | /// Ignore this declaration, if it is seen again. |
| Dmitri Gribenko | 6cfb153 | 2013-02-14 13:53:30 +0000 | [diff] [blame] | 321 | void Ignore(const Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); } |
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 322 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 323 | /// Add a visited context. |
| Haojian Wu | 10d95c5 | 2018-01-17 14:29:25 +0000 | [diff] [blame] | 324 | void addVisitedContext(DeclContext *Ctx) { |
| 325 | CompletionContext.addVisitedContext(Ctx); |
| 326 | } |
| 327 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 328 | /// \name Name lookup predicates |
| 329 | /// |
| 330 | /// These predicates can be passed to the name lookup functions to filter the |
| 331 | /// 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] | 332 | /// |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 333 | //@{ |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 334 | bool IsOrdinaryName(const NamedDecl *ND) const; |
| 335 | bool IsOrdinaryNonTypeName(const NamedDecl *ND) const; |
| 336 | bool IsIntegralConstantValue(const NamedDecl *ND) const; |
| 337 | bool IsOrdinaryNonValueName(const NamedDecl *ND) const; |
| 338 | bool IsNestedNameSpecifier(const NamedDecl *ND) const; |
| 339 | bool IsEnum(const NamedDecl *ND) const; |
| 340 | bool IsClassOrStruct(const NamedDecl *ND) const; |
| 341 | bool IsUnion(const NamedDecl *ND) const; |
| 342 | bool IsNamespace(const NamedDecl *ND) const; |
| 343 | bool IsNamespaceOrAlias(const NamedDecl *ND) const; |
| 344 | bool IsType(const NamedDecl *ND) const; |
| 345 | bool IsMember(const NamedDecl *ND) const; |
| 346 | bool IsObjCIvar(const NamedDecl *ND) const; |
| 347 | bool IsObjCMessageReceiver(const NamedDecl *ND) const; |
| 348 | bool IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const; |
| 349 | bool IsObjCCollection(const NamedDecl *ND) const; |
| 350 | bool IsImpossibleToSatisfy(const NamedDecl *ND) const; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 351 | //@} |
| 352 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 353 | } |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 354 | |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 355 | class ResultBuilder::ShadowMapEntry::iterator { |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 356 | llvm::PointerUnion<const NamedDecl *, const DeclIndexPair *> DeclOrIterator; |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 357 | unsigned SingleDeclIndex; |
| 358 | |
| 359 | public: |
| 360 | typedef DeclIndexPair value_type; |
| 361 | typedef value_type reference; |
| 362 | typedef std::ptrdiff_t difference_type; |
| 363 | typedef std::input_iterator_tag iterator_category; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 364 | |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 365 | class pointer { |
| 366 | DeclIndexPair Value; |
| 367 | |
| 368 | public: |
| 369 | pointer(const DeclIndexPair &Value) : Value(Value) { } |
| 370 | |
| 371 | const DeclIndexPair *operator->() const { |
| 372 | return &Value; |
| 373 | } |
| 374 | }; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 375 | |
| 376 | iterator() : DeclOrIterator((NamedDecl *)nullptr), SingleDeclIndex(0) {} |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 377 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 378 | iterator(const NamedDecl *SingleDecl, unsigned Index) |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 379 | : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { } |
| 380 | |
| 381 | iterator(const DeclIndexPair *Iterator) |
| 382 | : DeclOrIterator(Iterator), SingleDeclIndex(0) { } |
| 383 | |
| 384 | iterator &operator++() { |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 385 | if (DeclOrIterator.is<const NamedDecl *>()) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 386 | DeclOrIterator = (NamedDecl *)nullptr; |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 387 | SingleDeclIndex = 0; |
| 388 | return *this; |
| 389 | } |
| 390 | |
| 391 | const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>(); |
| 392 | ++I; |
| 393 | DeclOrIterator = I; |
| 394 | return *this; |
| 395 | } |
| 396 | |
| Chris Lattner | 9795b39 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 397 | /*iterator operator++(int) { |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 398 | iterator tmp(*this); |
| 399 | ++(*this); |
| 400 | return tmp; |
| Chris Lattner | 9795b39 | 2010-09-04 18:12:20 +0000 | [diff] [blame] | 401 | }*/ |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 402 | |
| 403 | reference operator*() const { |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 404 | if (const NamedDecl *ND = DeclOrIterator.dyn_cast<const NamedDecl *>()) |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 405 | return reference(ND, SingleDeclIndex); |
| 406 | |
| Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 407 | return *DeclOrIterator.get<const DeclIndexPair*>(); |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | pointer operator->() const { |
| 411 | return pointer(**this); |
| 412 | } |
| 413 | |
| 414 | friend bool operator==(const iterator &X, const iterator &Y) { |
| Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 415 | return X.DeclOrIterator.getOpaqueValue() |
| 416 | == Y.DeclOrIterator.getOpaqueValue() && |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 417 | X.SingleDeclIndex == Y.SingleDeclIndex; |
| 418 | } |
| 419 | |
| 420 | friend bool operator!=(const iterator &X, const iterator &Y) { |
| Douglas Gregor | 94bb5e8 | 2009-12-06 21:27:58 +0000 | [diff] [blame] | 421 | return !(X == Y); |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 422 | } |
| 423 | }; |
| 424 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 425 | ResultBuilder::ShadowMapEntry::iterator |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 426 | ResultBuilder::ShadowMapEntry::begin() const { |
| 427 | if (DeclOrVector.isNull()) |
| 428 | return iterator(); |
| 429 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 430 | if (const NamedDecl *ND = DeclOrVector.dyn_cast<const NamedDecl *>()) |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 431 | return iterator(ND, SingleDeclIndex); |
| 432 | |
| 433 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin()); |
| 434 | } |
| 435 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 436 | ResultBuilder::ShadowMapEntry::iterator |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 437 | ResultBuilder::ShadowMapEntry::end() const { |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 438 | if (DeclOrVector.is<const NamedDecl *>() || DeclOrVector.isNull()) |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 439 | return iterator(); |
| 440 | |
| 441 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end()); |
| 442 | } |
| 443 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 444 | /// Compute the qualification required to get from the current context |
| Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 445 | /// (\p CurContext) to the target context (\p TargetContext). |
| 446 | /// |
| 447 | /// \param Context the AST context in which the qualification will be used. |
| 448 | /// |
| 449 | /// \param CurContext the context where an entity is being named, which is |
| 450 | /// typically based on the current scope. |
| 451 | /// |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 452 | /// \param TargetContext the context in which the named entity actually |
| Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 453 | /// resides. |
| 454 | /// |
| 455 | /// \returns a nested name specifier that refers into the target context, or |
| 456 | /// NULL if no qualification is needed. |
| 457 | static NestedNameSpecifier * |
| 458 | getRequiredQualification(ASTContext &Context, |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 459 | const DeclContext *CurContext, |
| 460 | const DeclContext *TargetContext) { |
| 461 | SmallVector<const DeclContext *, 4> TargetParents; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 462 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 463 | for (const DeclContext *CommonAncestor = TargetContext; |
| Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 464 | CommonAncestor && !CommonAncestor->Encloses(CurContext); |
| 465 | CommonAncestor = CommonAncestor->getLookupParent()) { |
| 466 | if (CommonAncestor->isTransparentContext() || |
| 467 | CommonAncestor->isFunctionOrMethod()) |
| 468 | continue; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 469 | |
| Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 470 | TargetParents.push_back(CommonAncestor); |
| 471 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 472 | |
| 473 | NestedNameSpecifier *Result = nullptr; |
| Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 474 | while (!TargetParents.empty()) { |
| Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 475 | const DeclContext *Parent = TargetParents.pop_back_val(); |
| 476 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 477 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) { |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 478 | if (!Namespace->getIdentifier()) |
| 479 | continue; |
| 480 | |
| Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 481 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 482 | } |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 483 | else if (const TagDecl *TD = dyn_cast<TagDecl>(Parent)) |
| Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 484 | Result = NestedNameSpecifier::Create(Context, Result, |
| 485 | false, |
| 486 | Context.getTypeDeclType(TD).getTypePtr()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 487 | } |
| Douglas Gregor | 2af2f67 | 2009-09-21 20:12:40 +0000 | [diff] [blame] | 488 | return Result; |
| 489 | } |
| 490 | |
| Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 491 | /// Determine whether \p Id is a name reserved for the implementation (C99 |
| 492 | /// 7.1.3, C++ [lib.global.names]). |
| Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 493 | static bool isReservedName(const IdentifierInfo *Id, |
| 494 | bool doubleUnderscoreOnly = false) { |
| Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 495 | if (Id->getLength() < 2) |
| 496 | return false; |
| 497 | const char *Name = Id->getNameStart(); |
| 498 | return Name[0] == '_' && |
| Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 499 | (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z' && |
| 500 | !doubleUnderscoreOnly)); |
| 501 | } |
| 502 | |
| 503 | // Some declarations have reserved names that we don't want to ever show. |
| 504 | // Filter out names reserved for the implementation if they come from a |
| 505 | // system header. |
| 506 | static bool shouldIgnoreDueToReservedName(const NamedDecl *ND, Sema &SemaRef) { |
| 507 | const IdentifierInfo *Id = ND->getIdentifier(); |
| 508 | if (!Id) |
| 509 | return false; |
| 510 | |
| 511 | // Ignore reserved names for compiler provided decls. |
| 512 | if (isReservedName(Id) && ND->getLocation().isInvalid()) |
| 513 | return true; |
| 514 | |
| 515 | // For system headers ignore only double-underscore names. |
| 516 | // This allows for system headers providing private symbols with a single |
| 517 | // underscore. |
| 518 | if (isReservedName(Id, /*doubleUnderscoreOnly=*/true) && |
| 519 | SemaRef.SourceMgr.isInSystemHeader( |
| 520 | SemaRef.SourceMgr.getSpellingLoc(ND->getLocation()))) |
| 521 | return true; |
| 522 | |
| 523 | return false; |
| Alp Toker | 034bbd5 | 2014-06-30 01:33:53 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 526 | bool ResultBuilder::isInterestingDecl(const NamedDecl *ND, |
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 527 | bool &AsNestedNameSpecifier) const { |
| 528 | AsNestedNameSpecifier = false; |
| 529 | |
| Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 530 | auto *Named = ND; |
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 531 | ND = ND->getUnderlyingDecl(); |
| Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 532 | |
| 533 | // Skip unnamed entities. |
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 534 | if (!ND->getDeclName()) |
| 535 | return false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 536 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 537 | // Friend declarations and declarations introduced due to friends are never |
| 538 | // added as results. |
| Richard Smith | 6eece29 | 2015-01-15 02:27:20 +0000 | [diff] [blame] | 539 | if (ND->getFriendObjectKind() == Decl::FOK_Undeclared) |
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 540 | return false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 541 | |
| Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 542 | // Class template (partial) specializations are never added as results. |
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 543 | if (isa<ClassTemplateSpecializationDecl>(ND) || |
| 544 | isa<ClassTemplatePartialSpecializationDecl>(ND)) |
| 545 | return false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 546 | |
| Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 547 | // Using declarations themselves are never added as results. |
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 548 | if (isa<UsingDecl>(ND)) |
| 549 | return false; |
| Argyrios Kyrtzidis | 5d8006d | 2016-07-01 01:17:02 +0000 | [diff] [blame] | 550 | |
| 551 | if (shouldIgnoreDueToReservedName(ND, SemaRef)) |
| 552 | return false; |
| Douglas Gregor | 2927c0c | 2010-11-09 03:59:40 +0000 | [diff] [blame] | 553 | |
| Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 554 | if (Filter == &ResultBuilder::IsNestedNameSpecifier || |
| Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 555 | (isa<NamespaceDecl>(ND) && |
| Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 556 | Filter != &ResultBuilder::IsNamespace && |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 557 | Filter != &ResultBuilder::IsNamespaceOrAlias && |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 558 | Filter != nullptr)) |
| Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 559 | AsNestedNameSpecifier = true; |
| 560 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 561 | // Filter out any unwanted results. |
| Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 562 | if (Filter && !(this->*Filter)(Named)) { |
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 563 | // Check whether it is interesting as a nested-name-specifier. |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 564 | if (AllowNestedNameSpecifiers && SemaRef.getLangOpts().CPlusPlus && |
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 565 | IsNestedNameSpecifier(ND) && |
| 566 | (Filter != &ResultBuilder::IsMember || |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 567 | (isa<CXXRecordDecl>(ND) && |
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 568 | cast<CXXRecordDecl>(ND)->isInjectedClassName()))) { |
| 569 | AsNestedNameSpecifier = true; |
| 570 | return true; |
| 571 | } |
| 572 | |
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 573 | return false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 574 | } |
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 575 | // ... then it must be interesting! |
| 576 | return true; |
| 577 | } |
| 578 | |
| Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 579 | bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext, |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 580 | const NamedDecl *Hiding) { |
| Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 581 | // In C, there is no way to refer to a hidden name. |
| 582 | // FIXME: This isn't true; we can find a tag name hidden by an ordinary |
| 583 | // name if we introduce the tag type. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 584 | if (!SemaRef.getLangOpts().CPlusPlus) |
| Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 585 | return true; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 586 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 587 | const DeclContext *HiddenCtx = |
| 588 | R.Declaration->getDeclContext()->getRedeclContext(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 589 | |
| Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 590 | // There is no way to qualify a name declared in a function or method. |
| 591 | if (HiddenCtx->isFunctionOrMethod()) |
| 592 | return true; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 593 | |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 594 | if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext()) |
| Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 595 | return true; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 596 | |
| Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 597 | // We can refer to the result with the appropriate qualification. Do it. |
| 598 | R.Hidden = true; |
| 599 | R.QualifierIsInformative = false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 600 | |
| Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 601 | if (!R.Qualifier) |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 602 | R.Qualifier = getRequiredQualification(SemaRef.Context, |
| 603 | CurContext, |
| Douglas Gregor | e0717ab | 2010-01-14 00:41:07 +0000 | [diff] [blame] | 604 | R.Declaration->getDeclContext()); |
| 605 | return false; |
| 606 | } |
| 607 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 608 | /// A simplified classification of types used to determine whether two |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 609 | /// types are "similar enough" when adjusting priorities. |
| Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 610 | SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) { |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 611 | switch (T->getTypeClass()) { |
| 612 | case Type::Builtin: |
| 613 | switch (cast<BuiltinType>(T)->getKind()) { |
| 614 | case BuiltinType::Void: |
| 615 | return STC_Void; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 616 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 617 | case BuiltinType::NullPtr: |
| 618 | return STC_Pointer; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 619 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 620 | case BuiltinType::Overload: |
| 621 | case BuiltinType::Dependent: |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 622 | return STC_Other; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 623 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 624 | case BuiltinType::ObjCId: |
| 625 | case BuiltinType::ObjCClass: |
| 626 | case BuiltinType::ObjCSel: |
| 627 | return STC_ObjectiveC; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 628 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 629 | default: |
| 630 | return STC_Arithmetic; |
| 631 | } |
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 632 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 633 | case Type::Complex: |
| 634 | return STC_Arithmetic; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 635 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 636 | case Type::Pointer: |
| 637 | return STC_Pointer; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 638 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 639 | case Type::BlockPointer: |
| 640 | return STC_Block; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 641 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 642 | case Type::LValueReference: |
| 643 | case Type::RValueReference: |
| 644 | return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 645 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 646 | case Type::ConstantArray: |
| 647 | case Type::IncompleteArray: |
| 648 | case Type::VariableArray: |
| 649 | case Type::DependentSizedArray: |
| 650 | return STC_Array; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 651 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 652 | case Type::DependentSizedExtVector: |
| 653 | case Type::Vector: |
| 654 | case Type::ExtVector: |
| 655 | return STC_Arithmetic; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 656 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 657 | case Type::FunctionProto: |
| 658 | case Type::FunctionNoProto: |
| 659 | return STC_Function; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 660 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 661 | case Type::Record: |
| 662 | return STC_Record; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 663 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 664 | case Type::Enum: |
| 665 | return STC_Arithmetic; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 666 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 667 | case Type::ObjCObject: |
| 668 | case Type::ObjCInterface: |
| 669 | case Type::ObjCObjectPointer: |
| 670 | return STC_ObjectiveC; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 671 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 672 | default: |
| 673 | return STC_Other; |
| 674 | } |
| 675 | } |
| 676 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 677 | /// Get the type that a given expression will have if this declaration |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 678 | /// is used as an expression in its "typical" code-completion form. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 679 | QualType clang::getDeclUsageType(ASTContext &C, const NamedDecl *ND) { |
| George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 680 | ND = ND->getUnderlyingDecl(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 681 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 682 | if (const TypeDecl *Type = dyn_cast<TypeDecl>(ND)) |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 683 | return C.getTypeDeclType(Type); |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 684 | if (const ObjCInterfaceDecl *Iface = dyn_cast<ObjCInterfaceDecl>(ND)) |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 685 | return C.getObjCInterfaceType(Iface); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 686 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 687 | QualType T; |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 688 | if (const FunctionDecl *Function = ND->getAsFunction()) |
| Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 689 | T = Function->getCallResultType(); |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 690 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) |
| Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 691 | T = Method->getSendResultType(); |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 692 | else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 693 | T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext())); |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 694 | else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 695 | T = Property->getType(); |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 696 | else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND)) |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 697 | T = Value->getType(); |
| 698 | else |
| 699 | return QualType(); |
| Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 700 | |
| 701 | // Dig through references, function pointers, and block pointers to |
| 702 | // get down to the likely type of an expression when the entity is |
| 703 | // used. |
| 704 | do { |
| 705 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) { |
| 706 | T = Ref->getPointeeType(); |
| 707 | continue; |
| 708 | } |
| 709 | |
| 710 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 711 | if (Pointer->getPointeeType()->isFunctionType()) { |
| 712 | T = Pointer->getPointeeType(); |
| 713 | continue; |
| 714 | } |
| 715 | |
| 716 | break; |
| 717 | } |
| 718 | |
| 719 | if (const BlockPointerType *Block = T->getAs<BlockPointerType>()) { |
| 720 | T = Block->getPointeeType(); |
| 721 | continue; |
| 722 | } |
| 723 | |
| 724 | if (const FunctionType *Function = T->getAs<FunctionType>()) { |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 725 | T = Function->getReturnType(); |
| Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 726 | continue; |
| 727 | } |
| 728 | |
| 729 | break; |
| 730 | } while (true); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 731 | |
| Douglas Gregor | af670a8 | 2011-04-14 20:33:34 +0000 | [diff] [blame] | 732 | return T; |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 735 | unsigned ResultBuilder::getBasePriority(const NamedDecl *ND) { |
| 736 | if (!ND) |
| 737 | return CCP_Unlikely; |
| 738 | |
| 739 | // Context-based decisions. |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 740 | const DeclContext *LexicalDC = ND->getLexicalDeclContext(); |
| 741 | if (LexicalDC->isFunctionOrMethod()) { |
| Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 742 | // _cmd is relatively rare |
| 743 | if (const ImplicitParamDecl *ImplicitParam = |
| 744 | dyn_cast<ImplicitParamDecl>(ND)) |
| 745 | if (ImplicitParam->getIdentifier() && |
| 746 | ImplicitParam->getIdentifier()->isStr("_cmd")) |
| 747 | return CCP_ObjC_cmd; |
| 748 | |
| 749 | return CCP_LocalDeclaration; |
| 750 | } |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 751 | |
| 752 | const DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
| Ilya Biryukov | 4e7a6fe | 2017-09-22 19:07:37 +0000 | [diff] [blame] | 753 | if (DC->isRecord() || isa<ObjCContainerDecl>(DC)) { |
| 754 | // Explicit destructor calls are very rare. |
| 755 | if (isa<CXXDestructorDecl>(ND)) |
| 756 | return CCP_Unlikely; |
| 757 | // Explicit operator and conversion function calls are also very rare. |
| 758 | auto DeclNameKind = ND->getDeclName().getNameKind(); |
| 759 | if (DeclNameKind == DeclarationName::CXXOperatorName || |
| 760 | DeclNameKind == DeclarationName::CXXLiteralOperatorName || |
| 761 | DeclNameKind == DeclarationName::CXXConversionFunctionName) |
| 762 | return CCP_Unlikely; |
| Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 763 | return CCP_MemberDeclaration; |
| Ilya Biryukov | 4e7a6fe | 2017-09-22 19:07:37 +0000 | [diff] [blame] | 764 | } |
| Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 765 | |
| 766 | // Content-based decisions. |
| 767 | if (isa<EnumConstantDecl>(ND)) |
| 768 | return CCP_Constant; |
| 769 | |
| Douglas Gregor | 52e0de4 | 2013-01-31 05:03:46 +0000 | [diff] [blame] | 770 | // Use CCP_Type for type declarations unless we're in a statement, Objective-C |
| 771 | // message receiver, or parenthesized expression context. There, it's as |
| 772 | // likely that the user will want to write a type as other declarations. |
| 773 | if ((isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) && |
| 774 | !(CompletionContext.getKind() == CodeCompletionContext::CCC_Statement || |
| 775 | CompletionContext.getKind() |
| 776 | == CodeCompletionContext::CCC_ObjCMessageReceiver || |
| 777 | CompletionContext.getKind() |
| 778 | == CodeCompletionContext::CCC_ParenthesizedExpression)) |
| Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 779 | return CCP_Type; |
| 780 | |
| 781 | return CCP_Declaration; |
| 782 | } |
| 783 | |
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 784 | void ResultBuilder::AdjustResultPriorityForDecl(Result &R) { |
| 785 | // If this is an Objective-C method declaration whose selector matches our |
| 786 | // preferred selector, give it a priority boost. |
| 787 | if (!PreferredSelector.isNull()) |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 788 | if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration)) |
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 789 | if (PreferredSelector == Method->getSelector()) |
| 790 | R.Priority += CCD_SelectorMatch; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 791 | |
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 792 | // If we have a preferred type, adjust the priority for results with exactly- |
| 793 | // matching or nearly-matching types. |
| 794 | if (!PreferredType.isNull()) { |
| 795 | QualType T = getDeclUsageType(SemaRef.Context, R.Declaration); |
| 796 | if (!T.isNull()) { |
| 797 | CanQualType TC = SemaRef.Context.getCanonicalType(T); |
| 798 | // Check for exactly-matching types (modulo qualifiers). |
| 799 | if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) |
| 800 | R.Priority /= CCF_ExactTypeMatch; |
| 801 | // Check for nearly-matching types, based on classification of each. |
| 802 | else if ((getSimplifiedTypeClass(PreferredType) |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 803 | == getSimplifiedTypeClass(TC)) && |
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 804 | !(PreferredType->isEnumeralType() && TC->isEnumeralType())) |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 805 | R.Priority /= CCF_SimilarTypeMatch; |
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 806 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 807 | } |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 810 | void ResultBuilder::MaybeAddConstructorResults(Result R) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 811 | if (!SemaRef.getLangOpts().CPlusPlus || !R.Declaration || |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 812 | !CompletionContext.wantConstructorResults()) |
| 813 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 814 | |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 815 | ASTContext &Context = SemaRef.Context; |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 816 | const NamedDecl *D = R.Declaration; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 817 | const CXXRecordDecl *Record = nullptr; |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 818 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 819 | Record = ClassTemplate->getTemplatedDecl(); |
| 820 | else if ((Record = dyn_cast<CXXRecordDecl>(D))) { |
| 821 | // Skip specializations and partial specializations. |
| 822 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 823 | return; |
| 824 | } else { |
| 825 | // There are no constructors here. |
| 826 | return; |
| 827 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 828 | |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 829 | Record = Record->getDefinition(); |
| 830 | if (!Record) |
| 831 | return; |
| 832 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 833 | |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 834 | QualType RecordTy = Context.getTypeDeclType(Record); |
| 835 | DeclarationName ConstructorName |
| 836 | = Context.DeclarationNames.getCXXConstructorName( |
| 837 | Context.getCanonicalType(RecordTy)); |
| Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 838 | DeclContext::lookup_result Ctors = Record->lookup(ConstructorName); |
| 839 | for (DeclContext::lookup_iterator I = Ctors.begin(), |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 840 | E = Ctors.end(); |
| 841 | I != E; ++I) { |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 842 | R.Declaration = *I; |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 843 | R.CursorKind = getCursorKindForDecl(R.Declaration); |
| 844 | Results.push_back(R); |
| 845 | } |
| 846 | } |
| 847 | |
| Sam McCall | 63c5972 | 2018-01-22 20:44:47 +0000 | [diff] [blame] | 848 | static bool isConstructor(const Decl *ND) { |
| 849 | if (const auto *Tmpl = dyn_cast<FunctionTemplateDecl>(ND)) |
| 850 | ND = Tmpl->getTemplatedDecl(); |
| 851 | return isa<CXXConstructorDecl>(ND); |
| 852 | } |
| 853 | |
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 854 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { |
| 855 | assert(!ShadowMaps.empty() && "Must enter into a results scope"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 856 | |
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 857 | if (R.Kind != Result::RK_Declaration) { |
| 858 | // For non-declaration results, just add the result. |
| 859 | Results.push_back(R); |
| 860 | return; |
| 861 | } |
| 862 | |
| 863 | // Look through using declarations. |
| Kirill Bobyrev | 47d7f52 | 2018-07-11 14:49:49 +0000 | [diff] [blame] | 864 | if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
| 865 | CodeCompletionResult Result(Using->getTargetDecl(), |
| 866 | getBasePriority(Using->getTargetDecl()), |
| 867 | R.Qualifier); |
| 868 | Result.ShadowDecl = Using; |
| 869 | MaybeAddResult(Result, CurContext); |
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 870 | return; |
| 871 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 872 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 873 | const Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 874 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
| 875 | |
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 876 | bool AsNestedNameSpecifier = false; |
| 877 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
| Douglas Gregor | 7c20861 | 2010-01-14 00:20:49 +0000 | [diff] [blame] | 878 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 879 | |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 880 | // C++ constructors are never found by name lookup. |
| Sam McCall | 63c5972 | 2018-01-22 20:44:47 +0000 | [diff] [blame] | 881 | if (isConstructor(R.Declaration)) |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 882 | return; |
| 883 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 884 | ShadowMap &SMap = ShadowMaps.back(); |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 885 | ShadowMapEntry::iterator I, IEnd; |
| 886 | ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName()); |
| 887 | if (NamePos != SMap.end()) { |
| 888 | I = NamePos->second.begin(); |
| 889 | IEnd = NamePos->second.end(); |
| 890 | } |
| 891 | |
| 892 | for (; I != IEnd; ++I) { |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 893 | const NamedDecl *ND = I->first; |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 894 | unsigned Index = I->second; |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 895 | if (ND->getCanonicalDecl() == CanonDecl) { |
| 896 | // This is a redeclaration. Always pick the newer declaration. |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 897 | Results[Index].Declaration = R.Declaration; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 898 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 899 | // We're done. |
| 900 | return; |
| 901 | } |
| 902 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 903 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 904 | // This is a new declaration in this scope. However, check whether this |
| 905 | // declaration name is hidden by a similarly-named declaration in an outer |
| 906 | // scope. |
| 907 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
| 908 | --SMEnd; |
| 909 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 910 | ShadowMapEntry::iterator I, IEnd; |
| 911 | ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName()); |
| 912 | if (NamePos != SM->end()) { |
| 913 | I = NamePos->second.begin(); |
| 914 | IEnd = NamePos->second.end(); |
| 915 | } |
| 916 | for (; I != IEnd; ++I) { |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 917 | // A tag declaration does not hide a non-tag declaration. |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 918 | if (I->first->hasTagIdentifierNamespace() && |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 919 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
| 920 | Decl::IDNS_LocalExtern | Decl::IDNS_ObjCProtocol))) |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 921 | continue; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 922 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 923 | // Protocols are in distinct namespaces from everything else. |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 924 | if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 925 | || (IDNS & Decl::IDNS_ObjCProtocol)) && |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 926 | I->first->getIdentifierNamespace() != IDNS) |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 927 | continue; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 928 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 929 | // 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] | 930 | if (CheckHiddenResult(R, CurContext, I->first)) |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 931 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 932 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 933 | break; |
| 934 | } |
| 935 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 936 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 937 | // 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] | 938 | if (!AllDeclsFound.insert(CanonDecl).second) |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 939 | return; |
| Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 940 | |
| Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 941 | // If the filter is for nested-name-specifiers, then this result starts a |
| 942 | // nested-name-specifier. |
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 943 | if (AsNestedNameSpecifier) { |
| Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 944 | R.StartsNestedNameSpecifier = true; |
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 945 | R.Priority = CCP_NestedNameSpecifier; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 946 | } else |
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 947 | AdjustResultPriorityForDecl(R); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 948 | |
| Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 949 | // If this result is supposed to have an informative qualifier, add one. |
| Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 950 | if (R.QualifierIsInformative && !R.Qualifier && |
| 951 | !R.StartsNestedNameSpecifier) { |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 952 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 953 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 954 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 955 | Namespace); |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 956 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 957 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 958 | false, SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
| Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 959 | else |
| 960 | R.QualifierIsInformative = false; |
| 961 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 962 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 963 | // Insert this result into the set of results and into the current shadow |
| 964 | // map. |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 965 | SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size()); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 966 | Results.push_back(R); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 967 | |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 968 | if (!AsNestedNameSpecifier) |
| 969 | MaybeAddConstructorResults(R); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 972 | void ResultBuilder::AddResult(Result R, DeclContext *CurContext, |
| Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 973 | NamedDecl *Hiding, bool InBaseClass = false) { |
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 974 | if (R.Kind != Result::RK_Declaration) { |
| 975 | // For non-declaration results, just add the result. |
| 976 | Results.push_back(R); |
| 977 | return; |
| 978 | } |
| 979 | |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 980 | // Look through using declarations. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 981 | if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
| Kirill Bobyrev | 47d7f52 | 2018-07-11 14:49:49 +0000 | [diff] [blame] | 982 | CodeCompletionResult Result(Using->getTargetDecl(), |
| 983 | getBasePriority(Using->getTargetDecl()), |
| 984 | R.Qualifier); |
| 985 | Result.ShadowDecl = Using; |
| 986 | AddResult(Result, CurContext, Hiding); |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 987 | return; |
| 988 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 989 | |
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 990 | bool AsNestedNameSpecifier = false; |
| 991 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 992 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 993 | |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 994 | // C++ constructors are never found by name lookup. |
| Sam McCall | 63c5972 | 2018-01-22 20:44:47 +0000 | [diff] [blame] | 995 | if (isConstructor(R.Declaration)) |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 996 | return; |
| 997 | |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 998 | if (Hiding && CheckHiddenResult(R, CurContext, Hiding)) |
| 999 | return; |
| Nick Lewycky | c392148 | 2012-04-03 21:44:08 +0000 | [diff] [blame] | 1000 | |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1001 | // 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] | 1002 | if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()).second) |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1003 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1004 | |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1005 | // If the filter is for nested-name-specifiers, then this result starts a |
| 1006 | // nested-name-specifier. |
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1007 | if (AsNestedNameSpecifier) { |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1008 | R.StartsNestedNameSpecifier = true; |
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1009 | R.Priority = CCP_NestedNameSpecifier; |
| Kirill Bobyrev | 47d7f52 | 2018-07-11 14:49:49 +0000 | [diff] [blame] | 1010 | } else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && |
| 1011 | InBaseClass && |
| 1012 | isa<CXXRecordDecl>( |
| 1013 | R.Declaration->getDeclContext()->getRedeclContext())) |
| Douglas Gregor | 09bbc65 | 2010-01-14 15:47:35 +0000 | [diff] [blame] | 1014 | R.QualifierIsInformative = true; |
| 1015 | |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1016 | // If this result is supposed to have an informative qualifier, add one. |
| 1017 | if (R.QualifierIsInformative && !R.Qualifier && |
| 1018 | !R.StartsNestedNameSpecifier) { |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1019 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
| 1020 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1021 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, |
| 1022 | Namespace); |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1023 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1024 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, false, |
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 1025 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1026 | else |
| 1027 | R.QualifierIsInformative = false; |
| 1028 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1029 | |
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1030 | // Adjust the priority if this result comes from a base class. |
| 1031 | if (InBaseClass) |
| 1032 | R.Priority += CCD_InBaseClass; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1033 | |
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 1034 | AdjustResultPriorityForDecl(R); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1035 | |
| Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1036 | if (HasObjectTypeQualifiers) |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1037 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(R.Declaration)) |
| Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1038 | if (Method->isInstance()) { |
| 1039 | Qualifiers MethodQuals |
| 1040 | = Qualifiers::fromCVRMask(Method->getTypeQualifiers()); |
| 1041 | if (ObjectTypeQualifiers == MethodQuals) |
| 1042 | R.Priority += CCD_ObjectQualifierMatch; |
| 1043 | else if (ObjectTypeQualifiers - MethodQuals) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1044 | // The method cannot be invoked, because doing so would drop |
| Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 1045 | // qualifiers. |
| 1046 | return; |
| 1047 | } |
| 1048 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1049 | |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1050 | // Insert this result into the set of results. |
| 1051 | Results.push_back(R); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1052 | |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 1053 | if (!AsNestedNameSpecifier) |
| 1054 | MaybeAddConstructorResults(R); |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1057 | void ResultBuilder::AddResult(Result R) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1058 | assert(R.Kind != Result::RK_Declaration && |
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1059 | "Declaration results need more context"); |
| 1060 | Results.push_back(R); |
| 1061 | } |
| 1062 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1063 | /// Enter into a new scope. |
| Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1064 | void ResultBuilder::EnterNewScope() { ShadowMaps.emplace_back(); } |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1065 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1066 | /// Exit from the current scope. |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1067 | void ResultBuilder::ExitScope() { |
| Douglas Gregor | 05e7ca3 | 2009-12-06 20:23:50 +0000 | [diff] [blame] | 1068 | for (ShadowMap::iterator E = ShadowMaps.back().begin(), |
| 1069 | EEnd = ShadowMaps.back().end(); |
| 1070 | E != EEnd; |
| 1071 | ++E) |
| 1072 | E->second.Destroy(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1073 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1074 | ShadowMaps.pop_back(); |
| 1075 | } |
| 1076 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1077 | /// Determines whether this given declaration will be found by |
| Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1078 | /// ordinary name lookup. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1079 | bool ResultBuilder::IsOrdinaryName(const NamedDecl *ND) const { |
| George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1080 | ND = ND->getUnderlyingDecl(); |
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1081 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1082 | // If name lookup finds a local extern declaration, then we are in a |
| 1083 | // context where it behaves like an ordinary name. |
| 1084 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1085 | if (SemaRef.getLangOpts().CPlusPlus) |
| Douglas Gregor | 9858ed5 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1086 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1087 | else if (SemaRef.getLangOpts().ObjC1) { |
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1088 | if (isa<ObjCIvarDecl>(ND)) |
| 1089 | return true; |
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1090 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1091 | |
| Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 1092 | return ND->getIdentifierNamespace() & IDNS; |
| 1093 | } |
| 1094 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1095 | /// Determines whether this given declaration will be found by |
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1096 | /// ordinary name lookup but is not a type name. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1097 | bool ResultBuilder::IsOrdinaryNonTypeName(const NamedDecl *ND) const { |
| George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1098 | ND = ND->getUnderlyingDecl(); |
| Alex Lorenz | f3df1f7 | 2017-11-14 01:46:24 +0000 | [diff] [blame] | 1099 | if (isa<TypeDecl>(ND)) |
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1100 | return false; |
| Alex Lorenz | f3df1f7 | 2017-11-14 01:46:24 +0000 | [diff] [blame] | 1101 | // Objective-C interfaces names are not filtered by this method because they |
| 1102 | // can be used in a class property expression. We can still filter out |
| 1103 | // @class declarations though. |
| 1104 | if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND)) { |
| 1105 | if (!ID->getDefinition()) |
| 1106 | return false; |
| 1107 | } |
| 1108 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1109 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1110 | if (SemaRef.getLangOpts().CPlusPlus) |
| Douglas Gregor | 9858ed5 | 2010-06-15 20:26:51 +0000 | [diff] [blame] | 1111 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1112 | else if (SemaRef.getLangOpts().ObjC1) { |
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1113 | if (isa<ObjCIvarDecl>(ND)) |
| 1114 | return true; |
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1115 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1116 | |
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1117 | return ND->getIdentifierNamespace() & IDNS; |
| 1118 | } |
| 1119 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1120 | bool ResultBuilder::IsIntegralConstantValue(const NamedDecl *ND) const { |
| Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1121 | if (!IsOrdinaryNonTypeName(ND)) |
| 1122 | return 0; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1123 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1124 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl())) |
| Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1125 | if (VD->getType()->isIntegralOrEnumerationType()) |
| 1126 | return true; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1127 | |
| Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 1128 | return false; |
| 1129 | } |
| 1130 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1131 | /// Determines whether this given declaration will be found by |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1132 | /// ordinary name lookup. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1133 | bool ResultBuilder::IsOrdinaryNonValueName(const NamedDecl *ND) const { |
| George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1134 | ND = ND->getUnderlyingDecl(); |
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1135 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1136 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1137 | if (SemaRef.getLangOpts().CPlusPlus) |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 1138 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1139 | |
| 1140 | return (ND->getIdentifierNamespace() & IDNS) && |
| 1141 | !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) && |
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1142 | !isa<ObjCPropertyDecl>(ND); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1145 | /// Determines whether the given declaration is suitable as the |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1146 | /// 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] | 1147 | bool ResultBuilder::IsNestedNameSpecifier(const NamedDecl *ND) const { |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1148 | // Allow us to find class templates, too. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1149 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1150 | ND = ClassTemplate->getTemplatedDecl(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1151 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1152 | return SemaRef.isAcceptableNestedNameSpecifier(ND); |
| 1153 | } |
| 1154 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1155 | /// Determines whether the given declaration is an enumeration. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1156 | bool ResultBuilder::IsEnum(const NamedDecl *ND) const { |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1157 | return isa<EnumDecl>(ND); |
| 1158 | } |
| 1159 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1160 | /// Determines whether the given declaration is a class or struct. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1161 | bool ResultBuilder::IsClassOrStruct(const NamedDecl *ND) const { |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1162 | // Allow us to find class templates, too. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1163 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1164 | ND = ClassTemplate->getTemplatedDecl(); |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1165 | |
| 1166 | // For purposes of this check, interfaces match too. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1167 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1168 | return RD->getTagKind() == TTK_Class || |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1169 | RD->getTagKind() == TTK_Struct || |
| 1170 | RD->getTagKind() == TTK_Interface; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1171 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1172 | return false; |
| 1173 | } |
| 1174 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1175 | /// Determines whether the given declaration is a union. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1176 | bool ResultBuilder::IsUnion(const NamedDecl *ND) const { |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1177 | // Allow us to find class templates, too. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1178 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1179 | ND = ClassTemplate->getTemplatedDecl(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1180 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1181 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND)) |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1182 | return RD->getTagKind() == TTK_Union; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1183 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1184 | return false; |
| 1185 | } |
| 1186 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1187 | /// Determines whether the given declaration is a namespace. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1188 | bool ResultBuilder::IsNamespace(const NamedDecl *ND) const { |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1189 | return isa<NamespaceDecl>(ND); |
| 1190 | } |
| 1191 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1192 | /// Determines whether the given declaration is a namespace or |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1193 | /// namespace alias. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1194 | bool ResultBuilder::IsNamespaceOrAlias(const NamedDecl *ND) const { |
| Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1195 | return isa<NamespaceDecl>(ND->getUnderlyingDecl()); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1198 | /// Determines whether the given declaration is a type. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1199 | bool ResultBuilder::IsType(const NamedDecl *ND) const { |
| Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1200 | ND = ND->getUnderlyingDecl(); |
| Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 1201 | return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1204 | /// Determines which members of a class should be visible via |
| Douglas Gregor | 99fe2ad | 2009-12-11 17:31:05 +0000 | [diff] [blame] | 1205 | /// "." or "->". Only value declarations, nested name specifiers, and |
| 1206 | /// using declarations thereof should show up. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1207 | bool ResultBuilder::IsMember(const NamedDecl *ND) const { |
| Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1208 | ND = ND->getUnderlyingDecl(); |
| Douglas Gregor | 7078839 | 2009-12-11 18:14:22 +0000 | [diff] [blame] | 1209 | return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) || |
| Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1210 | isa<ObjCPropertyDecl>(ND); |
| Douglas Gregor | e412a5a | 2009-09-23 22:26:46 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1213 | static bool isObjCReceiverType(ASTContext &C, QualType T) { |
| 1214 | T = C.getCanonicalType(T); |
| 1215 | switch (T->getTypeClass()) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1216 | case Type::ObjCObject: |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1217 | case Type::ObjCInterface: |
| 1218 | case Type::ObjCObjectPointer: |
| 1219 | return true; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1220 | |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1221 | case Type::Builtin: |
| 1222 | switch (cast<BuiltinType>(T)->getKind()) { |
| 1223 | case BuiltinType::ObjCId: |
| 1224 | case BuiltinType::ObjCClass: |
| 1225 | case BuiltinType::ObjCSel: |
| 1226 | return true; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1227 | |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1228 | default: |
| 1229 | break; |
| 1230 | } |
| 1231 | return false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1232 | |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1233 | default: |
| 1234 | break; |
| 1235 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1236 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1237 | if (!C.getLangOpts().CPlusPlus) |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1238 | return false; |
| 1239 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1240 | // FIXME: We could perform more analysis here to determine whether a |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1241 | // particular class type has any conversions to Objective-C types. For now, |
| 1242 | // just accept all class types. |
| 1243 | return T->isDependentType() || T->isRecordType(); |
| 1244 | } |
| 1245 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1246 | bool ResultBuilder::IsObjCMessageReceiver(const NamedDecl *ND) const { |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1247 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1248 | if (T.isNull()) |
| 1249 | return false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1250 | |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1251 | T = SemaRef.Context.getBaseElementType(T); |
| 1252 | return isObjCReceiverType(SemaRef.Context, T); |
| 1253 | } |
| 1254 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1255 | bool ResultBuilder::IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const { |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1256 | if (IsObjCMessageReceiver(ND)) |
| 1257 | return true; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1258 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1259 | const VarDecl *Var = dyn_cast<VarDecl>(ND); |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1260 | if (!Var) |
| 1261 | return false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1262 | |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1263 | return Var->hasLocalStorage() && !Var->hasAttr<BlocksAttr>(); |
| 1264 | } |
| 1265 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1266 | bool ResultBuilder::IsObjCCollection(const NamedDecl *ND) const { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1267 | if ((SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryName(ND)) || |
| 1268 | (!SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryNonTypeName(ND))) |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1269 | return false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1270 | |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1271 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
| 1272 | if (T.isNull()) |
| 1273 | return false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1274 | |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1275 | T = SemaRef.Context.getBaseElementType(T); |
| 1276 | return T->isObjCObjectType() || T->isObjCObjectPointerType() || |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1277 | T->isObjCIdType() || |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1278 | (SemaRef.getLangOpts().CPlusPlus && T->isRecordType()); |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 1279 | } |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 1280 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1281 | bool ResultBuilder::IsImpossibleToSatisfy(const NamedDecl *ND) const { |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1282 | return false; |
| 1283 | } |
| 1284 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1285 | /// Determines whether the given declaration is an Objective-C |
| Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1286 | /// instance variable. |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 1287 | bool ResultBuilder::IsObjCIvar(const NamedDecl *ND) const { |
| Douglas Gregor | 2b8162b | 2010-01-14 16:08:12 +0000 | [diff] [blame] | 1288 | return isa<ObjCIvarDecl>(ND); |
| 1289 | } |
| 1290 | |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1291 | namespace { |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1292 | /// Visible declaration consumer that adds a code-completion result |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1293 | /// for each visible declaration. |
| 1294 | class CodeCompletionDeclConsumer : public VisibleDeclConsumer { |
| 1295 | ResultBuilder &Results; |
| 1296 | DeclContext *CurContext; |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 1297 | std::vector<FixItHint> FixIts; |
| Haojian Wu | 10d95c5 | 2018-01-17 14:29:25 +0000 | [diff] [blame] | 1298 | |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1299 | public: |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 1300 | CodeCompletionDeclConsumer( |
| 1301 | ResultBuilder &Results, DeclContext *CurContext, |
| 1302 | std::vector<FixItHint> FixIts = std::vector<FixItHint>()) |
| 1303 | : Results(Results), CurContext(CurContext), FixIts(std::move(FixIts)) {} |
| Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1304 | |
| 1305 | void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx, |
| 1306 | bool InBaseClass) override { |
| Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1307 | bool Accessible = true; |
| Ilya Biryukov | 55b1b15 | 2018-07-30 15:19:05 +0000 | [diff] [blame] | 1308 | if (Ctx) |
| 1309 | Accessible = Results.getSema().IsSimplyAccessible(ND, Ctx); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1310 | ResultBuilder::Result Result(ND, Results.getBasePriority(ND), nullptr, |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 1311 | false, Accessible, FixIts); |
| Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1312 | Results.AddResult(Result, CurContext, Hiding, InBaseClass); |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1313 | } |
| Haojian Wu | 10d95c5 | 2018-01-17 14:29:25 +0000 | [diff] [blame] | 1314 | |
| 1315 | void EnteredContext(DeclContext* Ctx) override { |
| 1316 | Results.addVisitedContext(Ctx); |
| 1317 | } |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1318 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1319 | } |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 1320 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1321 | /// Add type specifiers for the current language as keyword results. |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1322 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1323 | ResultBuilder &Results) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1324 | typedef CodeCompletionResult Result; |
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1325 | Results.AddResult(Result("short", CCP_Type)); |
| 1326 | Results.AddResult(Result("long", CCP_Type)); |
| 1327 | Results.AddResult(Result("signed", CCP_Type)); |
| 1328 | Results.AddResult(Result("unsigned", CCP_Type)); |
| 1329 | Results.AddResult(Result("void", CCP_Type)); |
| 1330 | Results.AddResult(Result("char", CCP_Type)); |
| 1331 | Results.AddResult(Result("int", CCP_Type)); |
| 1332 | Results.AddResult(Result("float", CCP_Type)); |
| 1333 | Results.AddResult(Result("double", CCP_Type)); |
| 1334 | Results.AddResult(Result("enum", CCP_Type)); |
| 1335 | Results.AddResult(Result("struct", CCP_Type)); |
| 1336 | Results.AddResult(Result("union", CCP_Type)); |
| 1337 | Results.AddResult(Result("const", CCP_Type)); |
| 1338 | Results.AddResult(Result("volatile", CCP_Type)); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1339 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1340 | if (LangOpts.C99) { |
| 1341 | // C99-specific |
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1342 | Results.AddResult(Result("_Complex", CCP_Type)); |
| 1343 | Results.AddResult(Result("_Imaginary", CCP_Type)); |
| 1344 | Results.AddResult(Result("_Bool", CCP_Type)); |
| 1345 | Results.AddResult(Result("restrict", CCP_Type)); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1346 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1347 | |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1348 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 1349 | Results.getCodeCompletionTUInfo()); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1350 | if (LangOpts.CPlusPlus) { |
| 1351 | // C++-specific |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1352 | Results.AddResult(Result("bool", CCP_Type + |
| Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 1353 | (LangOpts.ObjC1? CCD_bool_in_ObjC : 0))); |
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1354 | Results.AddResult(Result("class", CCP_Type)); |
| 1355 | Results.AddResult(Result("wchar_t", CCP_Type)); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1356 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1357 | // typename qualified-id |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1358 | Builder.AddTypedTextChunk("typename"); |
| 1359 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1360 | Builder.AddPlaceholderChunk("qualifier"); |
| 1361 | Builder.AddTextChunk("::"); |
| 1362 | Builder.AddPlaceholderChunk("name"); |
| 1363 | Results.AddResult(Result(Builder.TakeString())); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1364 | |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1365 | if (LangOpts.CPlusPlus11) { |
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1366 | Results.AddResult(Result("auto", CCP_Type)); |
| 1367 | Results.AddResult(Result("char16_t", CCP_Type)); |
| 1368 | Results.AddResult(Result("char32_t", CCP_Type)); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1369 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1370 | Builder.AddTypedTextChunk("decltype"); |
| 1371 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1372 | Builder.AddPlaceholderChunk("expression"); |
| 1373 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1374 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1375 | } |
| Alex Lorenz | 46eed9d | 2017-02-13 23:35:59 +0000 | [diff] [blame] | 1376 | } else |
| 1377 | Results.AddResult(Result("__auto_type", CCP_Type)); |
| 1378 | |
| Richard Smith | 7b301e2 | 2018-05-24 21:51:52 +0000 | [diff] [blame] | 1379 | // GNU keywords |
| 1380 | if (LangOpts.GNUKeywords) { |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1381 | // FIXME: Enable when we actually support decimal floating point. |
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1382 | // Results.AddResult(Result("_Decimal32")); |
| 1383 | // Results.AddResult(Result("_Decimal64")); |
| 1384 | // Results.AddResult(Result("_Decimal128")); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1385 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1386 | Builder.AddTypedTextChunk("typeof"); |
| 1387 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1388 | Builder.AddPlaceholderChunk("expression"); |
| 1389 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1390 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1391 | Builder.AddTypedTextChunk("typeof"); |
| 1392 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1393 | Builder.AddPlaceholderChunk("type"); |
| 1394 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1395 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1396 | } |
| Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 1397 | |
| 1398 | // Nullability |
| Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 1399 | Results.AddResult(Result("_Nonnull", CCP_Type)); |
| 1400 | Results.AddResult(Result("_Null_unspecified", CCP_Type)); |
| 1401 | Results.AddResult(Result("_Nullable", CCP_Type)); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1404 | static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC, |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1405 | const LangOptions &LangOpts, |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1406 | ResultBuilder &Results) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1407 | typedef CodeCompletionResult Result; |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1408 | // Note: we don't suggest either "auto" or "register", because both |
| 1409 | // are pointless as storage specifiers. Elsewhere, we suggest "auto" |
| 1410 | // in C++0x as a type specifier. |
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1411 | Results.AddResult(Result("extern")); |
| 1412 | Results.AddResult(Result("static")); |
| Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1413 | |
| 1414 | if (LangOpts.CPlusPlus11) { |
| 1415 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
| 1416 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
| 1417 | |
| 1418 | // alignas |
| 1419 | Builder.AddTypedTextChunk("alignas"); |
| 1420 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1421 | Builder.AddPlaceholderChunk("expression"); |
| 1422 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1423 | Results.AddResult(Result(Builder.TakeString())); |
| 1424 | |
| 1425 | Results.AddResult(Result("constexpr")); |
| 1426 | Results.AddResult(Result("thread_local")); |
| 1427 | } |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1430 | static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC, |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1431 | const LangOptions &LangOpts, |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1432 | ResultBuilder &Results) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1433 | typedef CodeCompletionResult Result; |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1434 | switch (CCC) { |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1435 | case Sema::PCC_Class: |
| 1436 | case Sema::PCC_MemberTemplate: |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1437 | if (LangOpts.CPlusPlus) { |
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1438 | Results.AddResult(Result("explicit")); |
| 1439 | Results.AddResult(Result("friend")); |
| 1440 | Results.AddResult(Result("mutable")); |
| 1441 | Results.AddResult(Result("virtual")); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1442 | } |
| Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 1443 | LLVM_FALLTHROUGH; |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1444 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1445 | case Sema::PCC_ObjCInterface: |
| 1446 | case Sema::PCC_ObjCImplementation: |
| 1447 | case Sema::PCC_Namespace: |
| 1448 | case Sema::PCC_Template: |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1449 | if (LangOpts.CPlusPlus || LangOpts.C99) |
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 1450 | Results.AddResult(Result("inline")); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1451 | break; |
| 1452 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1453 | case Sema::PCC_ObjCInstanceVariableList: |
| 1454 | case Sema::PCC_Expression: |
| 1455 | case Sema::PCC_Statement: |
| 1456 | case Sema::PCC_ForInit: |
| 1457 | case Sema::PCC_Condition: |
| 1458 | case Sema::PCC_RecoveryInFunction: |
| 1459 | case Sema::PCC_Type: |
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1460 | case Sema::PCC_ParenthesizedExpression: |
| Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1461 | case Sema::PCC_LocalDeclarationSpecifiers: |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1462 | break; |
| 1463 | } |
| 1464 | } |
| 1465 | |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1466 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); |
| 1467 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); |
| 1468 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
| Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1469 | ResultBuilder &Results, |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1470 | bool NeedAt); |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1471 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1472 | ResultBuilder &Results, |
| 1473 | bool NeedAt); |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1474 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1475 | ResultBuilder &Results, |
| 1476 | bool NeedAt); |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1477 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1478 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1479 | static void AddTypedefResult(ResultBuilder &Results) { |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1480 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 1481 | Results.getCodeCompletionTUInfo()); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1482 | Builder.AddTypedTextChunk("typedef"); |
| 1483 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1484 | Builder.AddPlaceholderChunk("type"); |
| 1485 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1486 | Builder.AddPlaceholderChunk("name"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1487 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1490 | static bool WantTypesInContext(Sema::ParserCompletionContext CCC, |
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1491 | const LangOptions &LangOpts) { |
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1492 | switch (CCC) { |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1493 | case Sema::PCC_Namespace: |
| 1494 | case Sema::PCC_Class: |
| 1495 | case Sema::PCC_ObjCInstanceVariableList: |
| 1496 | case Sema::PCC_Template: |
| 1497 | case Sema::PCC_MemberTemplate: |
| 1498 | case Sema::PCC_Statement: |
| 1499 | case Sema::PCC_RecoveryInFunction: |
| 1500 | case Sema::PCC_Type: |
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1501 | case Sema::PCC_ParenthesizedExpression: |
| Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1502 | case Sema::PCC_LocalDeclarationSpecifiers: |
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1503 | return true; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1504 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1505 | case Sema::PCC_Expression: |
| 1506 | case Sema::PCC_Condition: |
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1507 | return LangOpts.CPlusPlus; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1508 | |
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1509 | case Sema::PCC_ObjCInterface: |
| 1510 | case Sema::PCC_ObjCImplementation: |
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1511 | return false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1512 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1513 | case Sema::PCC_ForInit: |
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1514 | return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99; |
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1515 | } |
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1516 | |
| 1517 | llvm_unreachable("Invalid ParserCompletionContext!"); |
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 1520 | static PrintingPolicy getCompletionPrintingPolicy(const ASTContext &Context, |
| 1521 | const Preprocessor &PP) { |
| 1522 | PrintingPolicy Policy = Sema::getPrintingPolicy(Context, PP); |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1523 | Policy.AnonymousTagLocations = false; |
| 1524 | Policy.SuppressStrongLifetime = true; |
| Douglas Gregor | 2e10cf9 | 2011-11-03 00:16:13 +0000 | [diff] [blame] | 1525 | Policy.SuppressUnwrittenScope = true; |
| Ilya Biryukov | b5da91c | 2017-11-08 10:39:09 +0000 | [diff] [blame] | 1526 | Policy.SuppressScope = true; |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1527 | return Policy; |
| 1528 | } |
| 1529 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1530 | /// Retrieve a printing policy suitable for code completion. |
| Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 1531 | static PrintingPolicy getCompletionPrintingPolicy(Sema &S) { |
| 1532 | return getCompletionPrintingPolicy(S.Context, S.PP); |
| 1533 | } |
| 1534 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1535 | /// Retrieve the string representation of the given type as a string |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1536 | /// that has the appropriate lifetime for code completion. |
| 1537 | /// |
| 1538 | /// This routine provides a fast path where we provide constant strings for |
| 1539 | /// common type names. |
| 1540 | static const char *GetCompletionTypeString(QualType T, |
| 1541 | ASTContext &Context, |
| 1542 | const PrintingPolicy &Policy, |
| 1543 | CodeCompletionAllocator &Allocator) { |
| 1544 | if (!T.getLocalQualifiers()) { |
| 1545 | // Built-in type names are constant strings. |
| 1546 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) |
| Argyrios Kyrtzidis | bbff3da | 2012-05-05 04:20:28 +0000 | [diff] [blame] | 1547 | return BT->getNameAsCString(Policy); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1548 | |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1549 | // Anonymous tag types are constant strings. |
| 1550 | if (const TagType *TagT = dyn_cast<TagType>(T)) |
| 1551 | if (TagDecl *Tag = TagT->getDecl()) |
| John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 1552 | if (!Tag->hasNameForLinkage()) { |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1553 | switch (Tag->getTagKind()) { |
| 1554 | case TTK_Struct: return "struct <anonymous>"; |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1555 | case TTK_Interface: return "__interface <anonymous>"; |
| 1556 | case TTK_Class: return "class <anonymous>"; |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1557 | case TTK_Union: return "union <anonymous>"; |
| 1558 | case TTK_Enum: return "enum <anonymous>"; |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1562 | |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1563 | // Slow path: format the type as a string. |
| 1564 | std::string Result; |
| 1565 | T.getAsStringInternal(Result, Policy); |
| 1566 | return Allocator.CopyString(Result); |
| 1567 | } |
| 1568 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1569 | /// Add a completion for "this", if we're in a member function. |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1570 | static void addThisCompletion(Sema &S, ResultBuilder &Results) { |
| 1571 | QualType ThisTy = S.getCurrentThisType(); |
| 1572 | if (ThisTy.isNull()) |
| 1573 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1574 | |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1575 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1576 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1577 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1578 | Builder.AddResultTypeChunk(GetCompletionTypeString(ThisTy, |
| 1579 | S.Context, |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1580 | Policy, |
| 1581 | Allocator)); |
| 1582 | Builder.AddTypedTextChunk("this"); |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1583 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
| Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1586 | static void AddStaticAssertResult(CodeCompletionBuilder &Builder, |
| 1587 | ResultBuilder &Results, |
| 1588 | const LangOptions &LangOpts) { |
| 1589 | if (!LangOpts.CPlusPlus11) |
| 1590 | return; |
| 1591 | |
| 1592 | Builder.AddTypedTextChunk("static_assert"); |
| 1593 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1594 | Builder.AddPlaceholderChunk("expression"); |
| 1595 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 1596 | Builder.AddPlaceholderChunk("message"); |
| 1597 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1598 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 1599 | } |
| 1600 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1601 | /// Add language constructs that show up for "ordinary" names. |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1602 | static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1603 | Scope *S, |
| 1604 | Sema &SemaRef, |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1605 | ResultBuilder &Results) { |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1606 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1607 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1608 | |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1609 | typedef CodeCompletionResult Result; |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1610 | switch (CCC) { |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1611 | case Sema::PCC_Namespace: |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1612 | if (SemaRef.getLangOpts().CPlusPlus) { |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1613 | if (Results.includeCodePatterns()) { |
| 1614 | // namespace <identifier> { declarations } |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1615 | Builder.AddTypedTextChunk("namespace"); |
| 1616 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1617 | Builder.AddPlaceholderChunk("identifier"); |
| 1618 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1619 | Builder.AddPlaceholderChunk("declarations"); |
| 1620 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1621 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1622 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1623 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1624 | |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1625 | // namespace identifier = identifier ; |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1626 | Builder.AddTypedTextChunk("namespace"); |
| 1627 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1628 | Builder.AddPlaceholderChunk("name"); |
| 1629 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
| 1630 | Builder.AddPlaceholderChunk("namespace"); |
| 1631 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1632 | |
| 1633 | // Using directives |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1634 | Builder.AddTypedTextChunk("using"); |
| 1635 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1636 | Builder.AddTextChunk("namespace"); |
| 1637 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1638 | Builder.AddPlaceholderChunk("identifier"); |
| 1639 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1640 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1641 | // asm(string-literal) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1642 | Builder.AddTypedTextChunk("asm"); |
| 1643 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1644 | Builder.AddPlaceholderChunk("string-literal"); |
| 1645 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1646 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1647 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1648 | if (Results.includeCodePatterns()) { |
| 1649 | // Explicit template instantiation |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1650 | Builder.AddTypedTextChunk("template"); |
| 1651 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1652 | Builder.AddPlaceholderChunk("declaration"); |
| 1653 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1654 | } |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1655 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1656 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1657 | if (SemaRef.getLangOpts().ObjC1) |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1658 | AddObjCTopLevelResults(Results, true); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1659 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1660 | AddTypedefResult(Results); |
| Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 1661 | LLVM_FALLTHROUGH; |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1662 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1663 | case Sema::PCC_Class: |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1664 | if (SemaRef.getLangOpts().CPlusPlus) { |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1665 | // Using declaration |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1666 | Builder.AddTypedTextChunk("using"); |
| 1667 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1668 | Builder.AddPlaceholderChunk("qualifier"); |
| 1669 | Builder.AddTextChunk("::"); |
| 1670 | Builder.AddPlaceholderChunk("name"); |
| 1671 | Results.AddResult(Result(Builder.TakeString())); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1672 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1673 | // using typename qualifier::name (only in a dependent context) |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1674 | if (SemaRef.CurContext->isDependentContext()) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1675 | Builder.AddTypedTextChunk("using"); |
| 1676 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1677 | Builder.AddTextChunk("typename"); |
| 1678 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1679 | Builder.AddPlaceholderChunk("qualifier"); |
| 1680 | Builder.AddTextChunk("::"); |
| 1681 | Builder.AddPlaceholderChunk("name"); |
| 1682 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
| Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1685 | AddStaticAssertResult(Builder, Results, SemaRef.getLangOpts()); |
| 1686 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1687 | if (CCC == Sema::PCC_Class) { |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1688 | AddTypedefResult(Results); |
| 1689 | |
| Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 1690 | bool IsNotInheritanceScope = |
| 1691 | !(S->getFlags() & Scope::ClassInheritanceScope); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1692 | // public: |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1693 | Builder.AddTypedTextChunk("public"); |
| Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 1694 | if (IsNotInheritanceScope && Results.includeCodePatterns()) |
| Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1695 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1696 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1697 | |
| 1698 | // protected: |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1699 | Builder.AddTypedTextChunk("protected"); |
| Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 1700 | if (IsNotInheritanceScope && Results.includeCodePatterns()) |
| Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1701 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1702 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1703 | |
| 1704 | // private: |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1705 | Builder.AddTypedTextChunk("private"); |
| Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 1706 | if (IsNotInheritanceScope && Results.includeCodePatterns()) |
| Douglas Gregor | 9489cdf | 2012-04-10 17:56:28 +0000 | [diff] [blame] | 1707 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1708 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1709 | } |
| 1710 | } |
| Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 1711 | LLVM_FALLTHROUGH; |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1712 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1713 | case Sema::PCC_Template: |
| 1714 | case Sema::PCC_MemberTemplate: |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1715 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns()) { |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1716 | // template < parameters > |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1717 | Builder.AddTypedTextChunk("template"); |
| 1718 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1719 | Builder.AddPlaceholderChunk("parameters"); |
| 1720 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1721 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1724 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1725 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1726 | break; |
| 1727 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1728 | case Sema::PCC_ObjCInterface: |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1729 | AddObjCInterfaceResults(SemaRef.getLangOpts(), Results, true); |
| 1730 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1731 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1732 | break; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1733 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1734 | case Sema::PCC_ObjCImplementation: |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1735 | AddObjCImplementationResults(SemaRef.getLangOpts(), Results, true); |
| 1736 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| 1737 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 1738 | break; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1739 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1740 | case Sema::PCC_ObjCInstanceVariableList: |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1741 | AddObjCVisibilityResults(SemaRef.getLangOpts(), Results, true); |
| Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1742 | break; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1743 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1744 | case Sema::PCC_RecoveryInFunction: |
| 1745 | case Sema::PCC_Statement: { |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1746 | AddTypedefResult(Results); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1747 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1748 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns() && |
| 1749 | SemaRef.getLangOpts().CXXExceptions) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1750 | Builder.AddTypedTextChunk("try"); |
| 1751 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1752 | Builder.AddPlaceholderChunk("statements"); |
| 1753 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1754 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1755 | Builder.AddTextChunk("catch"); |
| 1756 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1757 | Builder.AddPlaceholderChunk("declaration"); |
| 1758 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1759 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1760 | Builder.AddPlaceholderChunk("statements"); |
| 1761 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1762 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1763 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1764 | } |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1765 | if (SemaRef.getLangOpts().ObjC1) |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 1766 | AddObjCStatementResults(Results, true); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1767 | |
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1768 | if (Results.includeCodePatterns()) { |
| 1769 | // if (condition) { statements } |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1770 | Builder.AddTypedTextChunk("if"); |
| 1771 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1772 | if (SemaRef.getLangOpts().CPlusPlus) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1773 | Builder.AddPlaceholderChunk("condition"); |
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1774 | else |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1775 | Builder.AddPlaceholderChunk("expression"); |
| 1776 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1777 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1778 | Builder.AddPlaceholderChunk("statements"); |
| 1779 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1780 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1781 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1782 | |
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1783 | // switch (condition) { } |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1784 | Builder.AddTypedTextChunk("switch"); |
| 1785 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1786 | if (SemaRef.getLangOpts().CPlusPlus) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1787 | Builder.AddPlaceholderChunk("condition"); |
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1788 | else |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1789 | Builder.AddPlaceholderChunk("expression"); |
| 1790 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1791 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1792 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1793 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1794 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1795 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1796 | |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1797 | // Switch-specific statements. |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 1798 | if (!SemaRef.getCurFunction()->SwitchStack.empty()) { |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1799 | // case expression: |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1800 | Builder.AddTypedTextChunk("case"); |
| 1801 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1802 | Builder.AddPlaceholderChunk("expression"); |
| 1803 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1804 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1805 | |
| 1806 | // default: |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1807 | Builder.AddTypedTextChunk("default"); |
| 1808 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 1809 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1812 | if (Results.includeCodePatterns()) { |
| 1813 | /// while (condition) { statements } |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1814 | Builder.AddTypedTextChunk("while"); |
| 1815 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1816 | if (SemaRef.getLangOpts().CPlusPlus) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1817 | Builder.AddPlaceholderChunk("condition"); |
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1818 | else |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1819 | Builder.AddPlaceholderChunk("expression"); |
| 1820 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1821 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1822 | Builder.AddPlaceholderChunk("statements"); |
| 1823 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1824 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1825 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1826 | |
| 1827 | // do { statements } while ( expression ); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1828 | Builder.AddTypedTextChunk("do"); |
| 1829 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1830 | Builder.AddPlaceholderChunk("statements"); |
| 1831 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1832 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1833 | Builder.AddTextChunk("while"); |
| 1834 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1835 | Builder.AddPlaceholderChunk("expression"); |
| 1836 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1837 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1838 | |
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1839 | // for ( for-init-statement ; condition ; expression ) { statements } |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1840 | Builder.AddTypedTextChunk("for"); |
| 1841 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1842 | if (SemaRef.getLangOpts().CPlusPlus || SemaRef.getLangOpts().C99) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1843 | Builder.AddPlaceholderChunk("init-statement"); |
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1844 | else |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1845 | Builder.AddPlaceholderChunk("init-expression"); |
| 1846 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1847 | Builder.AddPlaceholderChunk("condition"); |
| 1848 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| 1849 | Builder.AddPlaceholderChunk("inc-expression"); |
| 1850 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1851 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 1852 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1853 | Builder.AddPlaceholderChunk("statements"); |
| 1854 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 1855 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 1856 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 1857 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1858 | |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1859 | if (S->getContinueParent()) { |
| 1860 | // continue ; |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1861 | Builder.AddTypedTextChunk("continue"); |
| 1862 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | if (S->getBreakParent()) { |
| 1866 | // break ; |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1867 | Builder.AddTypedTextChunk("break"); |
| 1868 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | // "return expression ;" or "return ;", depending on whether we |
| 1872 | // know the function is void or not. |
| 1873 | bool isVoid = false; |
| 1874 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1875 | isVoid = Function->getReturnType()->isVoidType(); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1876 | else if (ObjCMethodDecl *Method |
| 1877 | = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1878 | isVoid = Method->getReturnType()->isVoidType(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1879 | else if (SemaRef.getCurBlock() && |
| Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1880 | !SemaRef.getCurBlock()->ReturnType.isNull()) |
| 1881 | isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType(); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1882 | Builder.AddTypedTextChunk("return"); |
| Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1883 | if (!isVoid) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1884 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1885 | Builder.AddPlaceholderChunk("expression"); |
| Douglas Gregor | 44272ca | 2010-02-18 04:06:48 +0000 | [diff] [blame] | 1886 | } |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1887 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1888 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1889 | // goto identifier ; |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1890 | Builder.AddTypedTextChunk("goto"); |
| 1891 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1892 | Builder.AddPlaceholderChunk("label"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1893 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1894 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1895 | // Using directives |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1896 | Builder.AddTypedTextChunk("using"); |
| 1897 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1898 | Builder.AddTextChunk("namespace"); |
| 1899 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1900 | Builder.AddPlaceholderChunk("identifier"); |
| 1901 | Results.AddResult(Result(Builder.TakeString())); |
| Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 1902 | |
| 1903 | AddStaticAssertResult(Builder, Results, SemaRef.getLangOpts()); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1904 | } |
| Galina Kistanova | be3ba9da | 2017-06-07 06:31:55 +0000 | [diff] [blame] | 1905 | LLVM_FALLTHROUGH; |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1906 | |
| 1907 | // Fall through (for statement expressions). |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1908 | case Sema::PCC_ForInit: |
| 1909 | case Sema::PCC_Condition: |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1910 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1911 | // Fall through: conditions and statements can have expressions. |
| Galina Kistanova | 3339911 | 2017-06-03 06:35:06 +0000 | [diff] [blame] | 1912 | LLVM_FALLTHROUGH; |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1913 | |
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1914 | case Sema::PCC_ParenthesizedExpression: |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1915 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1916 | CCC == Sema::PCC_ParenthesizedExpression) { |
| 1917 | // (__bridge <type>)<expression> |
| 1918 | Builder.AddTypedTextChunk("__bridge"); |
| 1919 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1920 | Builder.AddPlaceholderChunk("type"); |
| 1921 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1922 | Builder.AddPlaceholderChunk("expression"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1923 | Results.AddResult(Result(Builder.TakeString())); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1924 | |
| 1925 | // (__bridge_transfer <Objective-C type>)<expression> |
| 1926 | Builder.AddTypedTextChunk("__bridge_transfer"); |
| 1927 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1928 | Builder.AddPlaceholderChunk("Objective-C type"); |
| 1929 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1930 | Builder.AddPlaceholderChunk("expression"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1931 | Results.AddResult(Result(Builder.TakeString())); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1932 | |
| 1933 | // (__bridge_retained <CF type>)<expression> |
| 1934 | Builder.AddTypedTextChunk("__bridge_retained"); |
| 1935 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 1936 | Builder.AddPlaceholderChunk("CF type"); |
| 1937 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 1938 | Builder.AddPlaceholderChunk("expression"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1939 | Results.AddResult(Result(Builder.TakeString())); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1940 | } |
| 1941 | // Fall through |
| Galina Kistanova | 3339911 | 2017-06-03 06:35:06 +0000 | [diff] [blame] | 1942 | LLVM_FALLTHROUGH; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1943 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1944 | case Sema::PCC_Expression: { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1945 | if (SemaRef.getLangOpts().CPlusPlus) { |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1946 | // 'this', if we're in a non-static member function. |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1947 | addThisCompletion(SemaRef, Results); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1948 | |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1949 | // true |
| 1950 | Builder.AddResultTypeChunk("bool"); |
| 1951 | Builder.AddTypedTextChunk("true"); |
| 1952 | Results.AddResult(Result(Builder.TakeString())); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1953 | |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 1954 | // false |
| 1955 | Builder.AddResultTypeChunk("bool"); |
| 1956 | Builder.AddTypedTextChunk("false"); |
| 1957 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1958 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1959 | if (SemaRef.getLangOpts().RTTI) { |
| Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1960 | // dynamic_cast < type-id > ( expression ) |
| 1961 | Builder.AddTypedTextChunk("dynamic_cast"); |
| 1962 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1963 | Builder.AddPlaceholderChunk("type"); |
| 1964 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1965 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1966 | Builder.AddPlaceholderChunk("expression"); |
| 1967 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1968 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 1969 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1970 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1971 | // static_cast < type-id > ( expression ) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1972 | Builder.AddTypedTextChunk("static_cast"); |
| 1973 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1974 | Builder.AddPlaceholderChunk("type"); |
| 1975 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1976 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1977 | Builder.AddPlaceholderChunk("expression"); |
| 1978 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1979 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1980 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1981 | // reinterpret_cast < type-id > ( expression ) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1982 | Builder.AddTypedTextChunk("reinterpret_cast"); |
| 1983 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1984 | Builder.AddPlaceholderChunk("type"); |
| 1985 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1986 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1987 | Builder.AddPlaceholderChunk("expression"); |
| 1988 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1989 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1990 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 1991 | // const_cast < type-id > ( expression ) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1992 | Builder.AddTypedTextChunk("const_cast"); |
| 1993 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
| 1994 | Builder.AddPlaceholderChunk("type"); |
| 1995 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
| 1996 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 1997 | Builder.AddPlaceholderChunk("expression"); |
| 1998 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 1999 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2000 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2001 | if (SemaRef.getLangOpts().RTTI) { |
| Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2002 | // typeid ( expression-or-type ) |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2003 | Builder.AddResultTypeChunk("std::type_info"); |
| Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2004 | Builder.AddTypedTextChunk("typeid"); |
| 2005 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2006 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 2007 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2008 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2009 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2010 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2011 | // new T ( ... ) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2012 | Builder.AddTypedTextChunk("new"); |
| 2013 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2014 | Builder.AddPlaceholderChunk("type"); |
| 2015 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2016 | Builder.AddPlaceholderChunk("expressions"); |
| 2017 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2018 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2019 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2020 | // new T [ ] ( ... ) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2021 | Builder.AddTypedTextChunk("new"); |
| 2022 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2023 | Builder.AddPlaceholderChunk("type"); |
| 2024 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 2025 | Builder.AddPlaceholderChunk("size"); |
| 2026 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 2027 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2028 | Builder.AddPlaceholderChunk("expressions"); |
| 2029 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2030 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2031 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2032 | // delete expression |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2033 | Builder.AddResultTypeChunk("void"); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2034 | Builder.AddTypedTextChunk("delete"); |
| 2035 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2036 | Builder.AddPlaceholderChunk("expression"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2037 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2038 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2039 | // delete [] expression |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2040 | Builder.AddResultTypeChunk("void"); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2041 | Builder.AddTypedTextChunk("delete"); |
| 2042 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2043 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
| 2044 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 2045 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2046 | Builder.AddPlaceholderChunk("expression"); |
| 2047 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2048 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2049 | if (SemaRef.getLangOpts().CXXExceptions) { |
| Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2050 | // throw expression |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2051 | Builder.AddResultTypeChunk("void"); |
| Douglas Gregor | c05f657 | 2011-04-12 02:47:21 +0000 | [diff] [blame] | 2052 | Builder.AddTypedTextChunk("throw"); |
| 2053 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 2054 | Builder.AddPlaceholderChunk("expression"); |
| 2055 | Results.AddResult(Result(Builder.TakeString())); |
| 2056 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2057 | |
| Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 2058 | // FIXME: Rethrow? |
| Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2059 | |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2060 | if (SemaRef.getLangOpts().CPlusPlus11) { |
| Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2061 | // nullptr |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2062 | Builder.AddResultTypeChunk("std::nullptr_t"); |
| Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2063 | Builder.AddTypedTextChunk("nullptr"); |
| 2064 | Results.AddResult(Result(Builder.TakeString())); |
| 2065 | |
| 2066 | // alignof |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2067 | Builder.AddResultTypeChunk("size_t"); |
| Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2068 | Builder.AddTypedTextChunk("alignof"); |
| 2069 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2070 | Builder.AddPlaceholderChunk("type"); |
| 2071 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2072 | Results.AddResult(Result(Builder.TakeString())); |
| 2073 | |
| 2074 | // noexcept |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2075 | Builder.AddResultTypeChunk("bool"); |
| Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2076 | Builder.AddTypedTextChunk("noexcept"); |
| 2077 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2078 | Builder.AddPlaceholderChunk("expression"); |
| 2079 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2080 | Results.AddResult(Result(Builder.TakeString())); |
| 2081 | |
| 2082 | // sizeof... expression |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2083 | Builder.AddResultTypeChunk("size_t"); |
| Douglas Gregor | 4205fef | 2011-10-18 16:29:03 +0000 | [diff] [blame] | 2084 | Builder.AddTypedTextChunk("sizeof..."); |
| 2085 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2086 | Builder.AddPlaceholderChunk("parameter-pack"); |
| 2087 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2088 | Results.AddResult(Result(Builder.TakeString())); |
| 2089 | } |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2092 | if (SemaRef.getLangOpts().ObjC1) { |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2093 | // 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] | 2094 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
| 2095 | // The interface can be NULL. |
| 2096 | if (ObjCInterfaceDecl *ID = Method->getClassInterface()) |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2097 | if (ID->getSuperClass()) { |
| 2098 | std::string SuperType; |
| 2099 | SuperType = ID->getSuperClass()->getNameAsString(); |
| 2100 | if (Method->isInstanceMethod()) |
| 2101 | SuperType += " *"; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2102 | |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2103 | Builder.AddResultTypeChunk(Allocator.CopyString(SuperType)); |
| 2104 | Builder.AddTypedTextChunk("super"); |
| 2105 | Results.AddResult(Result(Builder.TakeString())); |
| 2106 | } |
| Ted Kremenek | 305a0a7 | 2010-05-31 21:43:10 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 2109 | AddObjCExpressionResults(Results, true); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
| Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2112 | if (SemaRef.getLangOpts().C11) { |
| 2113 | // _Alignof |
| 2114 | Builder.AddResultTypeChunk("size_t"); |
| Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2115 | if (SemaRef.PP.isMacroDefined("alignof")) |
| Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2116 | Builder.AddTypedTextChunk("alignof"); |
| 2117 | else |
| 2118 | Builder.AddTypedTextChunk("_Alignof"); |
| 2119 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2120 | Builder.AddPlaceholderChunk("type"); |
| 2121 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2122 | Results.AddResult(Result(Builder.TakeString())); |
| 2123 | } |
| 2124 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 2125 | // sizeof expression |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 2126 | Builder.AddResultTypeChunk("size_t"); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2127 | Builder.AddTypedTextChunk("sizeof"); |
| 2128 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2129 | Builder.AddPlaceholderChunk("expression-or-type"); |
| 2130 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 2131 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2132 | break; |
| 2133 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2134 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2135 | case Sema::PCC_Type: |
| Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2136 | case Sema::PCC_LocalDeclarationSpecifiers: |
| Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 2137 | break; |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2140 | if (WantTypesInContext(CCC, SemaRef.getLangOpts())) |
| 2141 | AddTypeSpecifierResults(SemaRef.getLangOpts(), Results); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2142 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2143 | if (SemaRef.getLangOpts().CPlusPlus && CCC != Sema::PCC_Type) |
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 2144 | Results.AddResult(Result("operator")); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2147 | /// 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] | 2148 | /// type chunk. |
| 2149 | static void AddResultTypeChunk(ASTContext &Context, |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2150 | const PrintingPolicy &Policy, |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2151 | const NamedDecl *ND, |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2152 | QualType BaseType, |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2153 | CodeCompletionBuilder &Result) { |
| Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2154 | if (!ND) |
| 2155 | return; |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2156 | |
| 2157 | // Skip constructors and conversion functions, which have their return types |
| 2158 | // built into their names. |
| Sam McCall | 63c5972 | 2018-01-22 20:44:47 +0000 | [diff] [blame] | 2159 | if (isConstructor(ND) || isa<CXXConversionDecl>(ND)) |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2160 | return; |
| 2161 | |
| Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2162 | // Determine the type of the declaration (if it has a type). |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 2163 | QualType T; |
| 2164 | if (const FunctionDecl *Function = ND->getAsFunction()) |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2165 | T = Function->getReturnType(); |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2166 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
| 2167 | if (!BaseType.isNull()) |
| 2168 | T = Method->getSendResultType(BaseType); |
| 2169 | else |
| 2170 | T = Method->getReturnType(); |
| Ilya Biryukov | b5da91c | 2017-11-08 10:39:09 +0000 | [diff] [blame] | 2171 | } else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) { |
| Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2172 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); |
| Ilya Biryukov | b5da91c | 2017-11-08 10:39:09 +0000 | [diff] [blame] | 2173 | T = clang::TypeName::getFullyQualifiedType(T, Context); |
| 2174 | } else if (isa<UnresolvedUsingValueDecl>(ND)) { |
| Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2175 | /* Do nothing: ignore unresolved using declarations*/ |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2176 | } else if (const ObjCIvarDecl *Ivar = dyn_cast<ObjCIvarDecl>(ND)) { |
| 2177 | if (!BaseType.isNull()) |
| 2178 | T = Ivar->getUsageType(BaseType); |
| 2179 | else |
| 2180 | T = Ivar->getType(); |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2181 | } else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND)) { |
| Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2182 | T = Value->getType(); |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2183 | } else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) { |
| 2184 | if (!BaseType.isNull()) |
| 2185 | T = Property->getUsageType(BaseType); |
| 2186 | else |
| 2187 | T = Property->getType(); |
| 2188 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2189 | |
| Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2190 | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) |
| 2191 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2192 | |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2193 | Result.AddResultTypeChunk(GetCompletionTypeString(T, Context, Policy, |
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2194 | Result.getAllocator())); |
| Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 2195 | } |
| 2196 | |
| Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2197 | static void MaybeAddSentinel(Preprocessor &PP, |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2198 | const NamedDecl *FunctionOrMethod, |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2199 | CodeCompletionBuilder &Result) { |
| Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2200 | if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) |
| 2201 | if (Sentinel->getSentinel() == 0) { |
| Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2202 | if (PP.getLangOpts().ObjC1 && PP.isMacroDefined("nil")) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2203 | Result.AddTextChunk(", nil"); |
| Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2204 | else if (PP.isMacroDefined("NULL")) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2205 | Result.AddTextChunk(", NULL"); |
| Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2206 | else |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2207 | Result.AddTextChunk(", (void*)0"); |
| Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2208 | } |
| 2209 | } |
| 2210 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2211 | static std::string formatObjCParamQualifiers(unsigned ObjCQuals, |
| Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2212 | QualType &Type) { |
| Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2213 | std::string Result; |
| 2214 | if (ObjCQuals & Decl::OBJC_TQ_In) |
| Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2215 | Result += "in "; |
| Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2216 | else if (ObjCQuals & Decl::OBJC_TQ_Inout) |
| Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2217 | Result += "inout "; |
| Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2218 | else if (ObjCQuals & Decl::OBJC_TQ_Out) |
| Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2219 | Result += "out "; |
| Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2220 | if (ObjCQuals & Decl::OBJC_TQ_Bycopy) |
| Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2221 | Result += "bycopy "; |
| Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2222 | else if (ObjCQuals & Decl::OBJC_TQ_Byref) |
| Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2223 | Result += "byref "; |
| Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2224 | if (ObjCQuals & Decl::OBJC_TQ_Oneway) |
| Douglas Gregor | 407d1f9 | 2011-11-09 02:13:45 +0000 | [diff] [blame] | 2225 | Result += "oneway "; |
| Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2226 | if (ObjCQuals & Decl::OBJC_TQ_CSNullability) { |
| 2227 | if (auto nullability = AttributedType::stripOuterNullability(Type)) { |
| 2228 | switch (*nullability) { |
| 2229 | case NullabilityKind::NonNull: |
| 2230 | Result += "nonnull "; |
| 2231 | break; |
| 2232 | |
| 2233 | case NullabilityKind::Nullable: |
| 2234 | Result += "nullable "; |
| 2235 | break; |
| 2236 | |
| 2237 | case NullabilityKind::Unspecified: |
| 2238 | Result += "null_unspecified "; |
| 2239 | break; |
| 2240 | } |
| 2241 | } |
| 2242 | } |
| Douglas Gregor | 8f08d74 | 2011-07-30 07:55:26 +0000 | [diff] [blame] | 2243 | return Result; |
| 2244 | } |
| 2245 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2246 | /// Tries to find the most appropriate type location for an Objective-C |
| Alex Lorenz | a195120 | 2016-10-18 10:35:27 +0000 | [diff] [blame] | 2247 | /// block placeholder. |
| 2248 | /// |
| 2249 | /// This function ignores things like typedefs and qualifiers in order to |
| 2250 | /// present the most relevant and accurate block placeholders in code completion |
| 2251 | /// results. |
| 2252 | static void findTypeLocationForBlockDecl(const TypeSourceInfo *TSInfo, |
| 2253 | FunctionTypeLoc &Block, |
| 2254 | FunctionProtoTypeLoc &BlockProto, |
| 2255 | bool SuppressBlock = false) { |
| 2256 | if (!TSInfo) |
| 2257 | return; |
| 2258 | TypeLoc TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 2259 | while (true) { |
| 2260 | // Look through typedefs. |
| 2261 | if (!SuppressBlock) { |
| 2262 | if (TypedefTypeLoc TypedefTL = TL.getAs<TypedefTypeLoc>()) { |
| 2263 | if (TypeSourceInfo *InnerTSInfo = |
| 2264 | TypedefTL.getTypedefNameDecl()->getTypeSourceInfo()) { |
| 2265 | TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); |
| 2266 | continue; |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | // Look through qualified types |
| 2271 | if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>()) { |
| 2272 | TL = QualifiedTL.getUnqualifiedLoc(); |
| 2273 | continue; |
| 2274 | } |
| 2275 | |
| 2276 | if (AttributedTypeLoc AttrTL = TL.getAs<AttributedTypeLoc>()) { |
| 2277 | TL = AttrTL.getModifiedLoc(); |
| 2278 | continue; |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | // Try to get the function prototype behind the block pointer type, |
| 2283 | // then we're done. |
| 2284 | if (BlockPointerTypeLoc BlockPtr = TL.getAs<BlockPointerTypeLoc>()) { |
| 2285 | TL = BlockPtr.getPointeeLoc().IgnoreParens(); |
| 2286 | Block = TL.getAs<FunctionTypeLoc>(); |
| 2287 | BlockProto = TL.getAs<FunctionProtoTypeLoc>(); |
| 2288 | } |
| 2289 | break; |
| 2290 | } |
| 2291 | } |
| 2292 | |
| Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2293 | static std::string |
| 2294 | formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, |
| 2295 | FunctionTypeLoc &Block, FunctionProtoTypeLoc &BlockProto, |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2296 | bool SuppressBlockName = false, |
| Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2297 | bool SuppressBlock = false, |
| 2298 | Optional<ArrayRef<QualType>> ObjCSubsts = None); |
| 2299 | |
| Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2300 | static std::string FormatFunctionParameter(const PrintingPolicy &Policy, |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2301 | const ParmVarDecl *Param, |
| Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2302 | bool SuppressName = false, |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2303 | bool SuppressBlock = false, |
| 2304 | Optional<ArrayRef<QualType>> ObjCSubsts = None) { |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2305 | bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext()); |
| 2306 | if (Param->getType()->isDependentType() || |
| 2307 | !Param->getType()->isBlockPointerType()) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2308 | // The argument for a dependent or non-block parameter is a placeholder |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2309 | // containing that parameter's type. |
| 2310 | std::string Result; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2311 | |
| Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2312 | if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName) |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2313 | Result = Param->getIdentifier()->getName(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2314 | |
| Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2315 | QualType Type = Param->getType(); |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2316 | if (ObjCSubsts) |
| 2317 | Type = Type.substObjCTypeArgs(Param->getASTContext(), *ObjCSubsts, |
| 2318 | ObjCSubstitutionContext::Parameter); |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2319 | if (ObjCMethodParam) { |
| Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2320 | Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier(), |
| 2321 | Type); |
| 2322 | Result += Type.getAsString(Policy) + ")"; |
| Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2323 | if (Param->getIdentifier() && !SuppressName) |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2324 | Result += Param->getIdentifier()->getName(); |
| Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2325 | } else { |
| 2326 | Type.getAsStringInternal(Result, Policy); |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2327 | } |
| 2328 | return Result; |
| 2329 | } |
| Alex Lorenz | a195120 | 2016-10-18 10:35:27 +0000 | [diff] [blame] | 2330 | |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2331 | // The argument for a block pointer parameter is a block literal with |
| 2332 | // the appropriate type. |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2333 | FunctionTypeLoc Block; |
| 2334 | FunctionProtoTypeLoc BlockProto; |
| Alex Lorenz | a195120 | 2016-10-18 10:35:27 +0000 | [diff] [blame] | 2335 | findTypeLocationForBlockDecl(Param->getTypeSourceInfo(), Block, BlockProto, |
| 2336 | SuppressBlock); |
| Alex Lorenz | 6bf4a58 | 2017-03-13 15:43:42 +0000 | [diff] [blame] | 2337 | // Try to retrieve the block type information from the property if this is a |
| 2338 | // parameter in a setter. |
| 2339 | if (!Block && ObjCMethodParam && |
| 2340 | cast<ObjCMethodDecl>(Param->getDeclContext())->isPropertyAccessor()) { |
| 2341 | if (const auto *PD = cast<ObjCMethodDecl>(Param->getDeclContext()) |
| 2342 | ->findPropertyDecl(/*CheckOverrides=*/false)) |
| 2343 | findTypeLocationForBlockDecl(PD->getTypeSourceInfo(), Block, BlockProto, |
| 2344 | SuppressBlock); |
| 2345 | } |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2346 | |
| 2347 | if (!Block) { |
| 2348 | // We were unable to find a FunctionProtoTypeLoc with parameter names |
| 2349 | // for the block; just use the parameter type as a placeholder. |
| 2350 | std::string Result; |
| Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2351 | if (!ObjCMethodParam && Param->getIdentifier()) |
| 2352 | Result = Param->getIdentifier()->getName(); |
| 2353 | |
| Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2354 | QualType Type = Param->getType().getUnqualifiedType(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2355 | |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2356 | if (ObjCMethodParam) { |
| Alex Lorenz | 01bcfc1 | 2016-11-23 16:28:34 +0000 | [diff] [blame] | 2357 | Result = Type.getAsString(Policy); |
| 2358 | std::string Quals = |
| 2359 | formatObjCParamQualifiers(Param->getObjCDeclQualifier(), Type); |
| 2360 | if (!Quals.empty()) |
| 2361 | Result = "(" + Quals + " " + Result + ")"; |
| 2362 | if (Result.back() != ')') |
| 2363 | Result += " "; |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2364 | if (Param->getIdentifier()) |
| 2365 | Result += Param->getIdentifier()->getName(); |
| Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2366 | } else { |
| 2367 | Type.getAsStringInternal(Result, Policy); |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2368 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2369 | |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2370 | return Result; |
| 2371 | } |
| Alex Lorenz | 01bcfc1 | 2016-11-23 16:28:34 +0000 | [diff] [blame] | 2372 | |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2373 | // We have the function prototype behind the block pointer type, as it was |
| 2374 | // written in the source. |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2375 | return formatBlockPlaceholder(Policy, Param, Block, BlockProto, |
| 2376 | /*SuppressBlockName=*/false, SuppressBlock, |
| Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2377 | ObjCSubsts); |
| 2378 | } |
| 2379 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2380 | /// Returns a placeholder string that corresponds to an Objective-C block |
| Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2381 | /// declaration. |
| 2382 | /// |
| 2383 | /// \param BlockDecl A declaration with an Objective-C block type. |
| 2384 | /// |
| 2385 | /// \param Block The most relevant type location for that block type. |
| 2386 | /// |
| Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 2387 | /// \param SuppressBlockName Determines whether or not the name of the block |
| Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2388 | /// declaration is included in the resulting string. |
| 2389 | static std::string |
| 2390 | formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, |
| 2391 | FunctionTypeLoc &Block, FunctionProtoTypeLoc &BlockProto, |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2392 | bool SuppressBlockName, bool SuppressBlock, |
| Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2393 | Optional<ArrayRef<QualType>> ObjCSubsts) { |
| Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2394 | std::string Result; |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2395 | QualType ResultType = Block.getTypePtr()->getReturnType(); |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2396 | if (ObjCSubsts) |
| Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2397 | ResultType = |
| 2398 | ResultType.substObjCTypeArgs(BlockDecl->getASTContext(), *ObjCSubsts, |
| 2399 | ObjCSubstitutionContext::Result); |
| Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2400 | if (!ResultType->isVoidType() || SuppressBlock) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2401 | ResultType.getAsStringInternal(Result, Policy); |
| Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2402 | |
| 2403 | // Format the parameter list. |
| 2404 | std::string Params; |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2405 | if (!BlockProto || Block.getNumParams() == 0) { |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2406 | if (BlockProto && BlockProto.getTypePtr()->isVariadic()) |
| Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2407 | Params = "(...)"; |
| Douglas Gregor | af25cfa | 2010-10-02 23:49:58 +0000 | [diff] [blame] | 2408 | else |
| Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2409 | Params = "(void)"; |
| Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2410 | } else { |
| Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2411 | Params += "("; |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2412 | for (unsigned I = 0, N = Block.getNumParams(); I != N; ++I) { |
| Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2413 | if (I) |
| Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2414 | Params += ", "; |
| Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2415 | Params += FormatFunctionParameter(Policy, Block.getParam(I), |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2416 | /*SuppressName=*/false, |
| Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2417 | /*SuppressBlock=*/true, ObjCSubsts); |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2418 | |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2419 | if (I == N - 1 && BlockProto.getTypePtr()->isVariadic()) |
| Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2420 | Params += ", ..."; |
| Douglas Gregor | 67da50e | 2010-09-08 22:47:51 +0000 | [diff] [blame] | 2421 | } |
| Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2422 | Params += ")"; |
| Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2423 | } |
| Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2424 | |
| Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2425 | if (SuppressBlock) { |
| 2426 | // Format as a parameter. |
| 2427 | Result = Result + " (^"; |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2428 | if (!SuppressBlockName && BlockDecl->getIdentifier()) |
| Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2429 | Result += BlockDecl->getIdentifier()->getName(); |
| Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2430 | Result += ")"; |
| 2431 | Result += Params; |
| 2432 | } else { |
| 2433 | // Format as a block literal argument. |
| 2434 | Result = '^' + Result; |
| 2435 | Result += Params; |
| Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2436 | |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 2437 | if (!SuppressBlockName && BlockDecl->getIdentifier()) |
| Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2438 | Result += BlockDecl->getIdentifier()->getName(); |
| Douglas Gregor | d793e7c | 2011-10-18 04:23:19 +0000 | [diff] [blame] | 2439 | } |
| Alex Lorenz | 920ae14 | 2016-10-18 10:38:58 +0000 | [diff] [blame] | 2440 | |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2441 | return Result; |
| 2442 | } |
| 2443 | |
| Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2444 | static std::string GetDefaultValueString(const ParmVarDecl *Param, |
| 2445 | const SourceManager &SM, |
| 2446 | const LangOptions &LangOpts) { |
| Ilya Biryukov | b6d1ec8 | 2017-07-21 09:24:00 +0000 | [diff] [blame] | 2447 | const SourceRange SrcRange = Param->getDefaultArgRange(); |
| Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2448 | CharSourceRange CharSrcRange = CharSourceRange::getTokenRange(SrcRange); |
| 2449 | bool Invalid = CharSrcRange.isInvalid(); |
| 2450 | if (Invalid) |
| 2451 | return ""; |
| 2452 | StringRef srcText = Lexer::getSourceText(CharSrcRange, SM, LangOpts, &Invalid); |
| 2453 | if (Invalid) |
| 2454 | return ""; |
| 2455 | |
| 2456 | if (srcText.empty() || srcText == "=") { |
| 2457 | // Lexer can't determine the value. |
| 2458 | // This happens if the code is incorrect (for example class is forward declared). |
| 2459 | return ""; |
| 2460 | } |
| Erik Verbruggen | 797980e | 2017-07-19 11:15:36 +0000 | [diff] [blame] | 2461 | std::string DefValue(srcText.str()); |
| Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2462 | // FIXME: remove this check if the Lexer::getSourceText value is fixed and |
| 2463 | // this value always has (or always does not have) '=' in front of it |
| 2464 | if (DefValue.at(0) != '=') { |
| 2465 | // If we don't have '=' in front of value. |
| 2466 | // Lexer returns built-in types values without '=' and user-defined types values with it. |
| 2467 | return " = " + DefValue; |
| 2468 | } |
| 2469 | return " " + DefValue; |
| 2470 | } |
| 2471 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2472 | /// Add function parameter chunks to the given code completion string. |
| Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2473 | static void AddFunctionParameterChunks(Preprocessor &PP, |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2474 | const PrintingPolicy &Policy, |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2475 | const FunctionDecl *Function, |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2476 | CodeCompletionBuilder &Result, |
| 2477 | unsigned Start = 0, |
| 2478 | bool InOptional = false) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2479 | bool FirstParameter = true; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2480 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2481 | for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) { |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2482 | const ParmVarDecl *Param = Function->getParamDecl(P); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2483 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2484 | if (Param->hasDefaultArg() && !InOptional) { |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2485 | // When we see an optional default argument, put that argument and |
| 2486 | // the remaining default arguments into a new, optional string. |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2487 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2488 | Result.getCodeCompletionTUInfo()); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2489 | if (!FirstParameter) |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2490 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2491 | AddFunctionParameterChunks(PP, Policy, Function, Opt, P, true); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2492 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2493 | break; |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2494 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2495 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2496 | if (FirstParameter) |
| 2497 | FirstParameter = false; |
| 2498 | else |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2499 | Result.AddChunk(CodeCompletionString::CK_Comma); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2500 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2501 | InOptional = false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2502 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2503 | // Format the placeholder string. |
| Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2504 | std::string PlaceholderStr = FormatFunctionParameter(Policy, Param); |
| Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 2505 | if (Param->hasDefaultArg()) |
| 2506 | PlaceholderStr += GetDefaultValueString(Param, PP.getSourceManager(), PP.getLangOpts()); |
| Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2507 | |
| Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2508 | if (Function->isVariadic() && P == N - 1) |
| 2509 | PlaceholderStr += ", ..."; |
| 2510 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2511 | // Add the placeholder string. |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2512 | Result.AddPlaceholderChunk( |
| 2513 | Result.getAllocator().CopyString(PlaceholderStr)); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2514 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2515 | |
| 2516 | if (const FunctionProtoType *Proto |
| Douglas Gregor | ba44903 | 2009-09-22 21:42:17 +0000 | [diff] [blame] | 2517 | = Function->getType()->getAs<FunctionProtoType>()) |
| Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2518 | if (Proto->isVariadic()) { |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2519 | if (Proto->getNumParams() == 0) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2520 | Result.AddPlaceholderChunk("..."); |
| Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2521 | |
| Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2522 | MaybeAddSentinel(PP, Function, Result); |
| Douglas Gregor | dbb71db | 2010-08-23 23:51:41 +0000 | [diff] [blame] | 2523 | } |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2526 | /// Add template parameter chunks to the given code completion string. |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2527 | static void AddTemplateParameterChunks(ASTContext &Context, |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2528 | const PrintingPolicy &Policy, |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2529 | const TemplateDecl *Template, |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2530 | CodeCompletionBuilder &Result, |
| 2531 | unsigned MaxParameters = 0, |
| 2532 | unsigned Start = 0, |
| 2533 | bool InDefaultArg = false) { |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2534 | bool FirstParameter = true; |
| Richard Smith | 6eece29 | 2015-01-15 02:27:20 +0000 | [diff] [blame] | 2535 | |
| 2536 | // Prefer to take the template parameter names from the first declaration of |
| 2537 | // the template. |
| 2538 | Template = cast<TemplateDecl>(Template->getCanonicalDecl()); |
| 2539 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2540 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2541 | TemplateParameterList::iterator PEnd = Params->end(); |
| 2542 | if (MaxParameters) |
| 2543 | PEnd = Params->begin() + MaxParameters; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2544 | for (TemplateParameterList::iterator P = Params->begin() + Start; |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2545 | P != PEnd; ++P) { |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2546 | bool HasDefaultArg = false; |
| 2547 | std::string PlaceholderStr; |
| 2548 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 2549 | if (TTP->wasDeclaredWithTypename()) |
| 2550 | PlaceholderStr = "typename"; |
| 2551 | else |
| 2552 | PlaceholderStr = "class"; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2553 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2554 | if (TTP->getIdentifier()) { |
| 2555 | PlaceholderStr += ' '; |
| 2556 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2557 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2558 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2559 | HasDefaultArg = TTP->hasDefaultArgument(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2560 | } else if (NonTypeTemplateParmDecl *NTTP |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2561 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2562 | if (NTTP->getIdentifier()) |
| 2563 | PlaceholderStr = NTTP->getIdentifier()->getName(); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2564 | NTTP->getType().getAsStringInternal(PlaceholderStr, Policy); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2565 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2566 | } else { |
| 2567 | assert(isa<TemplateTemplateParmDecl>(*P)); |
| 2568 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2569 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2570 | // Since putting the template argument list into the placeholder would |
| 2571 | // be very, very long, we just use an abbreviation. |
| 2572 | PlaceholderStr = "template<...> class"; |
| 2573 | if (TTP->getIdentifier()) { |
| 2574 | PlaceholderStr += ' '; |
| 2575 | PlaceholderStr += TTP->getIdentifier()->getName(); |
| 2576 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2577 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2578 | HasDefaultArg = TTP->hasDefaultArgument(); |
| 2579 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2580 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2581 | if (HasDefaultArg && !InDefaultArg) { |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2582 | // When we see an optional default argument, put that argument and |
| 2583 | // the remaining default arguments into a new, optional string. |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2584 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 2585 | Result.getCodeCompletionTUInfo()); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2586 | if (!FirstParameter) |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2587 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2588 | AddTemplateParameterChunks(Context, Policy, Template, Opt, MaxParameters, |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2589 | P - Params->begin(), true); |
| 2590 | Result.AddOptionalChunk(Opt.TakeString()); |
| 2591 | break; |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2592 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2593 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2594 | InDefaultArg = false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2595 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2596 | if (FirstParameter) |
| 2597 | FirstParameter = false; |
| 2598 | else |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2599 | Result.AddChunk(CodeCompletionString::CK_Comma); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2600 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2601 | // Add the placeholder string. |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2602 | Result.AddPlaceholderChunk( |
| 2603 | Result.getAllocator().CopyString(PlaceholderStr)); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2604 | } |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2607 | /// Add a qualifier to the given code-completion string, if the |
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2608 | /// provided nested-name-specifier is non-NULL. |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2609 | static void |
| 2610 | AddQualifierToCompletionString(CodeCompletionBuilder &Result, |
| 2611 | NestedNameSpecifier *Qualifier, |
| Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2612 | bool QualifierIsInformative, |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2613 | ASTContext &Context, |
| 2614 | const PrintingPolicy &Policy) { |
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2615 | if (!Qualifier) |
| 2616 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2617 | |
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2618 | std::string PrintedNNS; |
| 2619 | { |
| 2620 | llvm::raw_string_ostream OS(PrintedNNS); |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2621 | Qualifier->print(OS, Policy); |
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2622 | } |
| Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2623 | if (QualifierIsInformative) |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2624 | Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS)); |
| Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 2625 | else |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2626 | Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS)); |
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 2627 | } |
| 2628 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2629 | static void |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2630 | AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result, |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2631 | const FunctionDecl *Function) { |
| Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2632 | const FunctionProtoType *Proto |
| 2633 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2634 | if (!Proto || !Proto->getTypeQuals()) |
| 2635 | return; |
| 2636 | |
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2637 | // FIXME: Add ref-qualifier! |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2638 | |
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2639 | // Handle single qualifiers without copying |
| 2640 | if (Proto->getTypeQuals() == Qualifiers::Const) { |
| 2641 | Result.AddInformativeChunk(" const"); |
| 2642 | return; |
| 2643 | } |
| 2644 | |
| 2645 | if (Proto->getTypeQuals() == Qualifiers::Volatile) { |
| 2646 | Result.AddInformativeChunk(" volatile"); |
| 2647 | return; |
| 2648 | } |
| 2649 | |
| 2650 | if (Proto->getTypeQuals() == Qualifiers::Restrict) { |
| 2651 | Result.AddInformativeChunk(" restrict"); |
| 2652 | return; |
| 2653 | } |
| 2654 | |
| 2655 | // Handle multiple qualifiers. |
| Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2656 | std::string QualsStr; |
| David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2657 | if (Proto->isConst()) |
| Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2658 | QualsStr += " const"; |
| David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2659 | if (Proto->isVolatile()) |
| Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2660 | QualsStr += " volatile"; |
| David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 2661 | if (Proto->isRestrict()) |
| Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2662 | QualsStr += " restrict"; |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2663 | Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr)); |
| Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2666 | /// Add the name of the given declaration |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2667 | static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy, |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2668 | const NamedDecl *ND, |
| 2669 | CodeCompletionBuilder &Result) { |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2670 | DeclarationName Name = ND->getDeclName(); |
| 2671 | if (!Name) |
| 2672 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2673 | |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2674 | switch (Name.getNameKind()) { |
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2675 | case DeclarationName::CXXOperatorName: { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2676 | const char *OperatorName = nullptr; |
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2677 | switch (Name.getCXXOverloadedOperator()) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2678 | case OO_None: |
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2679 | case OO_Conditional: |
| 2680 | case NUM_OVERLOADED_OPERATORS: |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2681 | OperatorName = "operator"; |
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2682 | break; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2683 | |
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2684 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 2685 | case OO_##Name: OperatorName = "operator" Spelling; break; |
| 2686 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) |
| 2687 | #include "clang/Basic/OperatorKinds.def" |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2688 | |
| Douglas Gregor | 304f9b0 | 2011-02-01 21:15:40 +0000 | [diff] [blame] | 2689 | case OO_New: OperatorName = "operator new"; break; |
| 2690 | case OO_Delete: OperatorName = "operator delete"; break; |
| 2691 | case OO_Array_New: OperatorName = "operator new[]"; break; |
| 2692 | case OO_Array_Delete: OperatorName = "operator delete[]"; break; |
| 2693 | case OO_Call: OperatorName = "operator()"; break; |
| 2694 | case OO_Subscript: OperatorName = "operator[]"; break; |
| 2695 | } |
| 2696 | Result.AddTypedTextChunk(OperatorName); |
| 2697 | break; |
| 2698 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2699 | |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2700 | case DeclarationName::Identifier: |
| 2701 | case DeclarationName::CXXConversionFunctionName: |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2702 | case DeclarationName::CXXDestructorName: |
| 2703 | case DeclarationName::CXXLiteralOperatorName: |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2704 | Result.AddTypedTextChunk( |
| 2705 | Result.getAllocator().CopyString(ND->getNameAsString())); |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2706 | break; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2707 | |
| Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 2708 | case DeclarationName::CXXDeductionGuideName: |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2709 | case DeclarationName::CXXUsingDirective: |
| 2710 | case DeclarationName::ObjCZeroArgSelector: |
| 2711 | case DeclarationName::ObjCOneArgSelector: |
| 2712 | case DeclarationName::ObjCMultiArgSelector: |
| 2713 | break; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2714 | |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2715 | case DeclarationName::CXXConstructorName: { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2716 | CXXRecordDecl *Record = nullptr; |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2717 | QualType Ty = Name.getCXXNameType(); |
| 2718 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) |
| 2719 | Record = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 2720 | else if (const InjectedClassNameType *InjectedTy |
| 2721 | = Ty->getAs<InjectedClassNameType>()) |
| 2722 | Record = InjectedTy->getDecl(); |
| 2723 | else { |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2724 | Result.AddTypedTextChunk( |
| 2725 | Result.getAllocator().CopyString(ND->getNameAsString())); |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2726 | break; |
| 2727 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2728 | |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2729 | Result.AddTypedTextChunk( |
| 2730 | Result.getAllocator().CopyString(Record->getNameAsString())); |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2731 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2732 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2733 | AddTemplateParameterChunks(Context, Policy, Template, Result); |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2734 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2735 | } |
| 2736 | break; |
| 2737 | } |
| 2738 | } |
| 2739 | } |
| 2740 | |
| Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2741 | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString(Sema &S, |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2742 | const CodeCompletionContext &CCContext, |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2743 | CodeCompletionAllocator &Allocator, |
| Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2744 | CodeCompletionTUInfo &CCTUInfo, |
| 2745 | bool IncludeBriefComments) { |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2746 | return CreateCodeCompletionString(S.Context, S.PP, CCContext, Allocator, |
| 2747 | CCTUInfo, IncludeBriefComments); |
| Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2748 | } |
| 2749 | |
| Eric Liu | 00f43c9 | 2018-07-06 09:43:57 +0000 | [diff] [blame] | 2750 | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionStringForMacro( |
| 2751 | Preprocessor &PP, CodeCompletionAllocator &Allocator, |
| 2752 | CodeCompletionTUInfo &CCTUInfo) { |
| 2753 | assert(Kind == RK_Macro); |
| 2754 | CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability); |
| 2755 | const MacroInfo *MI = PP.getMacroInfo(Macro); |
| 2756 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Macro->getName())); |
| 2757 | |
| 2758 | if (!MI || !MI->isFunctionLike()) |
| 2759 | return Result.TakeString(); |
| 2760 | |
| 2761 | // Format a function-like macro with placeholders for the arguments. |
| 2762 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
| 2763 | MacroInfo::param_iterator A = MI->param_begin(), AEnd = MI->param_end(); |
| 2764 | |
| 2765 | // C99 variadic macros add __VA_ARGS__ at the end. Skip it. |
| 2766 | if (MI->isC99Varargs()) { |
| 2767 | --AEnd; |
| 2768 | |
| 2769 | if (A == AEnd) { |
| 2770 | Result.AddPlaceholderChunk("..."); |
| 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | for (MacroInfo::param_iterator A = MI->param_begin(); A != AEnd; ++A) { |
| 2775 | if (A != MI->param_begin()) |
| 2776 | Result.AddChunk(CodeCompletionString::CK_Comma); |
| 2777 | |
| 2778 | if (MI->isVariadic() && (A + 1) == AEnd) { |
| 2779 | SmallString<32> Arg = (*A)->getName(); |
| 2780 | if (MI->isC99Varargs()) |
| 2781 | Arg += ", ..."; |
| 2782 | else |
| 2783 | Arg += "..."; |
| 2784 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
| 2785 | break; |
| 2786 | } |
| 2787 | |
| 2788 | // Non-variadic macros are simple. |
| 2789 | Result.AddPlaceholderChunk( |
| 2790 | Result.getAllocator().CopyString((*A)->getName())); |
| 2791 | } |
| 2792 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
| 2793 | return Result.TakeString(); |
| 2794 | } |
| 2795 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2796 | /// If possible, create a new code completion string for the given |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2797 | /// result. |
| 2798 | /// |
| 2799 | /// \returns Either a new, heap-allocated code completion string describing |
| 2800 | /// how to use this result, or NULL to indicate that the string or name of the |
| 2801 | /// result is all that is needed. |
| 2802 | CodeCompletionString * |
| Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2803 | CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx, |
| 2804 | Preprocessor &PP, |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2805 | const CodeCompletionContext &CCContext, |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2806 | CodeCompletionAllocator &Allocator, |
| Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2807 | CodeCompletionTUInfo &CCTUInfo, |
| 2808 | bool IncludeBriefComments) { |
| Eric Liu | 00f43c9 | 2018-07-06 09:43:57 +0000 | [diff] [blame] | 2809 | if (Kind == RK_Macro) |
| 2810 | return CreateCodeCompletionStringForMacro(PP, Allocator, CCTUInfo); |
| 2811 | |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2812 | CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2813 | |
| Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2814 | PrintingPolicy Policy = getCompletionPrintingPolicy(Ctx, PP); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2815 | if (Kind == RK_Pattern) { |
| 2816 | Pattern->Priority = Priority; |
| 2817 | Pattern->Availability = Availability; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2818 | |
| Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2819 | if (Declaration) { |
| 2820 | Result.addParentContext(Declaration->getDeclContext()); |
| Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2821 | Pattern->ParentName = Result.getParentName(); |
| Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 2822 | if (const RawComment *RC = |
| 2823 | getPatternCompletionComment(Ctx, Declaration)) { |
| 2824 | Result.addBriefComment(RC->getBriefText(Ctx)); |
| 2825 | Pattern->BriefComment = Result.getBriefComment(); |
| 2826 | } |
| Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2827 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2828 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2829 | return Pattern; |
| 2830 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2831 | |
| Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2832 | if (Kind == RK_Keyword) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2833 | Result.AddTypedTextChunk(Keyword); |
| 2834 | return Result.TakeString(); |
| Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 2835 | } |
| Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 2836 | assert(Kind == RK_Declaration && "Missed a result kind?"); |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2837 | const NamedDecl *ND = Declaration; |
| Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 2838 | Result.addParentContext(ND->getDeclContext()); |
| Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2839 | |
| 2840 | if (IncludeBriefComments) { |
| 2841 | // Add documentation comment, if it exists. |
| Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 2842 | if (const RawComment *RC = getCompletionComment(Ctx, Declaration)) { |
| Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2843 | Result.addBriefComment(RC->getBriefText(Ctx)); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2844 | } |
| Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
| Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2847 | if (StartsNestedNameSpecifier) { |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2848 | Result.AddTypedTextChunk( |
| 2849 | Result.getAllocator().CopyString(ND->getNameAsString())); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2850 | Result.AddTextChunk("::"); |
| 2851 | return Result.TakeString(); |
| Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2852 | } |
| Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 2853 | |
| Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2854 | for (const auto *I : ND->specific_attrs<AnnotateAttr>()) |
| 2855 | Result.AddAnnotation(Result.getAllocator().CopyString(I->getAnnotation())); |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 2856 | |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2857 | AddResultTypeChunk(Ctx, Policy, ND, CCContext.getBaseType(), Result); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2858 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2859 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2860 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2861 | Ctx, Policy); |
| 2862 | AddTypedNameChunk(Ctx, Policy, ND, Result); |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2863 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
| Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2864 | AddFunctionParameterChunks(PP, Policy, Function, Result); |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2865 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
| Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2866 | AddFunctionTypeQualsToCompletionString(Result, Function); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2867 | return Result.TakeString(); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2868 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2869 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2870 | if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2871 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2872 | Ctx, Policy); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2873 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
| Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2874 | AddTypedNameChunk(Ctx, Policy, Function, Result); |
| Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 2875 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2876 | // Figure out which template parameters are deduced (or have default |
| 2877 | // arguments). |
| Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 2878 | llvm::SmallBitVector Deduced; |
| Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2879 | Sema::MarkDeducedTemplateParameters(Ctx, FunTmpl, Deduced); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2880 | unsigned LastDeducibleArgument; |
| 2881 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
| 2882 | --LastDeducibleArgument) { |
| 2883 | if (!Deduced[LastDeducibleArgument - 1]) { |
| 2884 | // C++0x: Figure out if the template argument has a default. If so, |
| 2885 | // the user doesn't need to type this argument. |
| 2886 | // FIXME: We need to abstract template parameters better! |
| 2887 | bool HasDefaultArg = false; |
| 2888 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2889 | LastDeducibleArgument - 1); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2890 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 2891 | HasDefaultArg = TTP->hasDefaultArgument(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2892 | else if (NonTypeTemplateParmDecl *NTTP |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2893 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 2894 | HasDefaultArg = NTTP->hasDefaultArgument(); |
| 2895 | else { |
| 2896 | assert(isa<TemplateTemplateParmDecl>(Param)); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2897 | HasDefaultArg |
| Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2898 | = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2899 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2900 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2901 | if (!HasDefaultArg) |
| 2902 | break; |
| 2903 | } |
| 2904 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2905 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2906 | if (LastDeducibleArgument) { |
| 2907 | // Some of the function template arguments cannot be deduced from a |
| 2908 | // function call, so we introduce an explicit template argument list |
| 2909 | // containing all of the arguments up to the first deducible argument. |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2910 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2911 | AddTemplateParameterChunks(Ctx, Policy, FunTmpl, Result, |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2912 | LastDeducibleArgument); |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2913 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2914 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2915 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2916 | // Add the function parameters |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2917 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
| Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 2918 | AddFunctionParameterChunks(PP, Policy, Function, Result); |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2919 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
| Douglas Gregor | 0f62236 | 2009-12-11 18:44:16 +0000 | [diff] [blame] | 2920 | AddFunctionTypeQualsToCompletionString(Result, Function); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2921 | return Result.TakeString(); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2922 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2923 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2924 | if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2925 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2926 | Ctx, Policy); |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2927 | Result.AddTypedTextChunk( |
| 2928 | Result.getAllocator().CopyString(Template->getNameAsString())); |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2929 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
| Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 2930 | AddTemplateParameterChunks(Ctx, Policy, Template, Result); |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 2931 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2932 | return Result.TakeString(); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 2933 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2934 | |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2935 | if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2936 | Selector Sel = Method->getSelector(); |
| 2937 | if (Sel.isUnarySelector()) { |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2938 | Result.AddTypedTextChunk(Result.getAllocator().CopyString( |
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2939 | Sel.getNameForSlot(0))); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2940 | return Result.TakeString(); |
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 2943 | std::string SelName = Sel.getNameForSlot(0).str(); |
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2944 | SelName += ':'; |
| 2945 | if (StartParameter == 0) |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2946 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName)); |
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2947 | else { |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2948 | Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName)); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2949 | |
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2950 | // If there is only one parameter, and we're past it, add an empty |
| 2951 | // typed-text chunk since there is nothing to type. |
| 2952 | if (Method->param_size() == 1) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2953 | Result.AddTypedTextChunk(""); |
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2954 | } |
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2955 | unsigned Idx = 0; |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 2956 | for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(), |
| 2957 | PEnd = Method->param_end(); |
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2958 | P != PEnd; (void)++P, ++Idx) { |
| 2959 | if (Idx > 0) { |
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2960 | std::string Keyword; |
| 2961 | if (Idx > StartParameter) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2962 | Result.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2963 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) |
| Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 2964 | Keyword += II->getName(); |
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2965 | Keyword += ":"; |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 2966 | if (Idx < StartParameter || AllParametersAreInformative) |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2967 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword)); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2968 | else |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 2969 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword)); |
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2970 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2971 | |
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2972 | // If we're before the starting parameter, skip the placeholder. |
| 2973 | if (Idx < StartParameter) |
| 2974 | continue; |
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 2975 | |
| 2976 | std::string Arg; |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2977 | QualType ParamType = (*P)->getType(); |
| 2978 | Optional<ArrayRef<QualType>> ObjCSubsts; |
| 2979 | if (!CCContext.getBaseType().isNull()) |
| 2980 | ObjCSubsts = CCContext.getBaseType()->getObjCSubstitutions(Method); |
| 2981 | |
| 2982 | if (ParamType->isBlockPointerType() && !DeclaringEntity) |
| 2983 | Arg = FormatFunctionParameter(Policy, *P, true, |
| 2984 | /*SuppressBlock=*/false, |
| 2985 | ObjCSubsts); |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2986 | else { |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2987 | if (ObjCSubsts) |
| 2988 | ParamType = ParamType.substObjCTypeArgs(Ctx, *ObjCSubsts, |
| 2989 | ObjCSubstitutionContext::Parameter); |
| Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 2990 | Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier(), |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 2991 | ParamType); |
| 2992 | Arg += ParamType.getAsString(Policy) + ")"; |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2993 | if (IdentifierInfo *II = (*P)->getIdentifier()) |
| Douglas Gregor | 981a0c4 | 2010-08-29 19:47:46 +0000 | [diff] [blame] | 2994 | if (DeclaringEntity || AllParametersAreInformative) |
| Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 2995 | Arg += II->getName(); |
| Douglas Gregor | e90dd00 | 2010-08-24 16:15:59 +0000 | [diff] [blame] | 2996 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 2997 | |
| Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 2998 | if (Method->isVariadic() && (P + 1) == PEnd) |
| 2999 | Arg += ", ..."; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3000 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 3001 | if (DeclaringEntity) |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3002 | Result.AddTextChunk(Result.getAllocator().CopyString(Arg)); |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 3003 | else if (AllParametersAreInformative) |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3004 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg)); |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 3005 | else |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3006 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
| Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 3009 | if (Method->isVariadic()) { |
| Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3010 | if (Method->param_size() == 0) { |
| 3011 | if (DeclaringEntity) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3012 | Result.AddTextChunk(", ..."); |
| Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3013 | else if (AllParametersAreInformative) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3014 | Result.AddInformativeChunk(", ..."); |
| Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3015 | else |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3016 | Result.AddPlaceholderChunk(", ..."); |
| Douglas Gregor | 400f597 | 2010-08-31 05:13:43 +0000 | [diff] [blame] | 3017 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3018 | |
| Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 3019 | MaybeAddSentinel(PP, Method, Result); |
| Douglas Gregor | 04c5f97 | 2009-12-23 00:21:46 +0000 | [diff] [blame] | 3020 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3021 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3022 | return Result.TakeString(); |
| Douglas Gregor | d3c5d79 | 2009-11-17 16:44:22 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
| Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 3025 | if (Qualifier) |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3026 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
| Argyrios Kyrtzidis | 8d05ca7 | 2012-01-17 02:15:51 +0000 | [diff] [blame] | 3027 | Ctx, Policy); |
| Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 3028 | |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3029 | Result.AddTypedTextChunk( |
| 3030 | Result.getAllocator().CopyString(ND->getNameAsString())); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3031 | return Result.TakeString(); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
| Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3034 | const RawComment *clang::getCompletionComment(const ASTContext &Ctx, |
| 3035 | const NamedDecl *ND) { |
| 3036 | if (!ND) |
| 3037 | return nullptr; |
| 3038 | if (auto *RC = Ctx.getRawCommentForAnyRedecl(ND)) |
| 3039 | return RC; |
| 3040 | |
| 3041 | // Try to find comment from a property for ObjC methods. |
| 3042 | const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(ND); |
| 3043 | if (!M) |
| 3044 | return nullptr; |
| 3045 | const ObjCPropertyDecl *PDecl = M->findPropertyDecl(); |
| 3046 | if (!PDecl) |
| 3047 | return nullptr; |
| 3048 | |
| 3049 | return Ctx.getRawCommentForAnyRedecl(PDecl); |
| 3050 | } |
| 3051 | |
| 3052 | const RawComment *clang::getPatternCompletionComment(const ASTContext &Ctx, |
| 3053 | const NamedDecl *ND) { |
| 3054 | const ObjCMethodDecl *M = dyn_cast_or_null<ObjCMethodDecl>(ND); |
| 3055 | if (!M || !M->isPropertyAccessor()) |
| 3056 | return nullptr; |
| 3057 | |
| 3058 | // Provide code completion comment for self.GetterName where |
| 3059 | // GetterName is the getter method for a property with name |
| 3060 | // different from the property name (declared via a property |
| 3061 | // getter attribute. |
| 3062 | const ObjCPropertyDecl *PDecl = M->findPropertyDecl(); |
| 3063 | if (!PDecl) |
| 3064 | return nullptr; |
| 3065 | if (PDecl->getGetterName() == M->getSelector() && |
| 3066 | PDecl->getIdentifier() != M->getIdentifier()) { |
| 3067 | if (auto *RC = Ctx.getRawCommentForAnyRedecl(M)) |
| 3068 | return RC; |
| 3069 | if (auto *RC = Ctx.getRawCommentForAnyRedecl(PDecl)) |
| 3070 | return RC; |
| 3071 | } |
| 3072 | return nullptr; |
| 3073 | } |
| 3074 | |
| 3075 | const RawComment *clang::getParameterComment( |
| 3076 | const ASTContext &Ctx, |
| 3077 | const CodeCompleteConsumer::OverloadCandidate &Result, |
| 3078 | unsigned ArgIndex) { |
| 3079 | auto FDecl = Result.getFunction(); |
| 3080 | if (!FDecl) |
| 3081 | return nullptr; |
| 3082 | if (ArgIndex < FDecl->getNumParams()) |
| 3083 | return Ctx.getRawCommentForAnyRedecl(FDecl->getParamDecl(ArgIndex)); |
| 3084 | return nullptr; |
| 3085 | } |
| 3086 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3087 | /// Add function overload parameter chunks to the given code completion |
| Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3088 | /// string. |
| 3089 | static void AddOverloadParameterChunks(ASTContext &Context, |
| 3090 | const PrintingPolicy &Policy, |
| 3091 | const FunctionDecl *Function, |
| 3092 | const FunctionProtoType *Prototype, |
| 3093 | CodeCompletionBuilder &Result, |
| 3094 | unsigned CurrentArg, |
| 3095 | unsigned Start = 0, |
| 3096 | bool InOptional = false) { |
| 3097 | bool FirstParameter = true; |
| 3098 | unsigned NumParams = Function ? Function->getNumParams() |
| 3099 | : Prototype->getNumParams(); |
| 3100 | |
| 3101 | for (unsigned P = Start; P != NumParams; ++P) { |
| 3102 | if (Function && Function->getParamDecl(P)->hasDefaultArg() && !InOptional) { |
| 3103 | // When we see an optional default argument, put that argument and |
| 3104 | // the remaining default arguments into a new, optional string. |
| 3105 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 3106 | Result.getCodeCompletionTUInfo()); |
| 3107 | if (!FirstParameter) |
| 3108 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| 3109 | // Optional sections are nested. |
| 3110 | AddOverloadParameterChunks(Context, Policy, Function, Prototype, Opt, |
| 3111 | CurrentArg, P, /*InOptional=*/true); |
| 3112 | Result.AddOptionalChunk(Opt.TakeString()); |
| 3113 | return; |
| 3114 | } |
| 3115 | |
| 3116 | if (FirstParameter) |
| 3117 | FirstParameter = false; |
| 3118 | else |
| 3119 | Result.AddChunk(CodeCompletionString::CK_Comma); |
| 3120 | |
| 3121 | InOptional = false; |
| 3122 | |
| 3123 | // Format the placeholder string. |
| 3124 | std::string Placeholder; |
| Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 3125 | if (Function) { |
| 3126 | const ParmVarDecl *Param = Function->getParamDecl(P); |
| 3127 | Placeholder = FormatFunctionParameter(Policy, Param); |
| 3128 | if (Param->hasDefaultArg()) |
| 3129 | Placeholder += GetDefaultValueString(Param, Context.getSourceManager(), Context.getLangOpts()); |
| 3130 | } else { |
| Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3131 | Placeholder = Prototype->getParamType(P).getAsString(Policy); |
| Erik Verbruggen | 11338c5 | 2017-07-19 10:45:40 +0000 | [diff] [blame] | 3132 | } |
| Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3133 | |
| 3134 | if (P == CurrentArg) |
| 3135 | Result.AddCurrentParameterChunk( |
| 3136 | Result.getAllocator().CopyString(Placeholder)); |
| 3137 | else |
| 3138 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Placeholder)); |
| 3139 | } |
| 3140 | |
| 3141 | if (Prototype && Prototype->isVariadic()) { |
| 3142 | CodeCompletionBuilder Opt(Result.getAllocator(), |
| 3143 | Result.getCodeCompletionTUInfo()); |
| 3144 | if (!FirstParameter) |
| 3145 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
| 3146 | |
| 3147 | if (CurrentArg < NumParams) |
| 3148 | Opt.AddPlaceholderChunk("..."); |
| 3149 | else |
| 3150 | Opt.AddCurrentParameterChunk("..."); |
| 3151 | |
| 3152 | Result.AddOptionalChunk(Opt.TakeString()); |
| 3153 | } |
| 3154 | } |
| 3155 | |
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3156 | CodeCompletionString * |
| 3157 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( |
| Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3158 | unsigned CurrentArg, Sema &S, |
| 3159 | CodeCompletionAllocator &Allocator, |
| 3160 | CodeCompletionTUInfo &CCTUInfo, |
| 3161 | bool IncludeBriefComments) const { |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3162 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3163 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3164 | // FIXME: Set priority, availability appropriately. |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3165 | CodeCompletionBuilder Result(Allocator,CCTUInfo, 1, CXAvailability_Available); |
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3166 | FunctionDecl *FDecl = getFunction(); |
| Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3167 | const FunctionProtoType *Proto |
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3168 | = dyn_cast<FunctionProtoType>(getFunctionType()); |
| 3169 | if (!FDecl && !Proto) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3170 | // Function without a prototype. Just give the return type and a |
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3171 | // highlighted ellipsis. |
| 3172 | const FunctionType *FT = getFunctionType(); |
| Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3173 | Result.AddResultTypeChunk(Result.getAllocator().CopyString( |
| 3174 | FT->getReturnType().getAsString(Policy))); |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3175 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3176 | Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "..."); |
| 3177 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3178 | return Result.TakeString(); |
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3179 | } |
| Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3180 | |
| 3181 | if (FDecl) { |
| Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3182 | if (IncludeBriefComments) { |
| 3183 | if (auto RC = getParameterComment(S.getASTContext(), *this, CurrentArg)) |
| Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3184 | Result.addBriefComment(RC->getBriefText(S.getASTContext())); |
| Ilya Biryukov | a3f955b | 2018-05-16 12:30:01 +0000 | [diff] [blame] | 3185 | } |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3186 | AddResultTypeChunk(S.Context, Policy, FDecl, QualType(), Result); |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3187 | Result.AddTextChunk( |
| Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3188 | Result.getAllocator().CopyString(FDecl->getNameAsString())); |
| 3189 | } else { |
| 3190 | Result.AddResultTypeChunk( |
| 3191 | Result.getAllocator().CopyString( |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3192 | Proto->getReturnType().getAsString(Policy))); |
| Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3193 | } |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3194 | |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3195 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
| Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3196 | AddOverloadParameterChunks(S.getASTContext(), Policy, FDecl, Proto, Result, |
| 3197 | CurrentArg); |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 3198 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
| Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 3199 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3200 | return Result.TakeString(); |
| Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3203 | unsigned clang::getMacroUsagePriority(StringRef MacroName, |
| Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3204 | const LangOptions &LangOpts, |
| Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3205 | bool PreferredTypeIsPointer) { |
| 3206 | unsigned Priority = CCP_Macro; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3207 | |
| Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3208 | // Treat the "nil", "Nil" and "NULL" macros as null pointer constants. |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3209 | if (MacroName.equals("nil") || MacroName.equals("NULL") || |
| Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3210 | MacroName.equals("Nil")) { |
| Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3211 | Priority = CCP_Constant; |
| 3212 | if (PreferredTypeIsPointer) |
| 3213 | Priority = Priority / CCF_SimilarTypeMatch; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3214 | } |
| Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 3215 | // Treat "YES", "NO", "true", and "false" as constants. |
| 3216 | else if (MacroName.equals("YES") || MacroName.equals("NO") || |
| 3217 | MacroName.equals("true") || MacroName.equals("false")) |
| 3218 | Priority = CCP_Constant; |
| 3219 | // Treat "bool" as a type. |
| 3220 | else if (MacroName.equals("bool")) |
| 3221 | Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3222 | |
| 3223 | |
| Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 3224 | return Priority; |
| 3225 | } |
| 3226 | |
| Dmitri Gribenko | bdc80de | 2013-01-11 20:32:41 +0000 | [diff] [blame] | 3227 | CXCursorKind clang::getCursorKindForDecl(const Decl *D) { |
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3228 | if (!D) |
| 3229 | return CXCursor_UnexposedDecl; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3230 | |
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3231 | switch (D->getKind()) { |
| 3232 | case Decl::Enum: return CXCursor_EnumDecl; |
| 3233 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 3234 | case Decl::Field: return CXCursor_FieldDecl; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3235 | case Decl::Function: |
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3236 | return CXCursor_FunctionDecl; |
| 3237 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 3238 | case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryImplDecl; |
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3239 | case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl; |
| Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 3240 | |
| Argyrios Kyrtzidis | 3698cef | 2012-01-24 21:39:26 +0000 | [diff] [blame] | 3241 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3242 | case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl; |
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3243 | case Decl::ObjCMethod: |
| 3244 | return cast<ObjCMethodDecl>(D)->isInstanceMethod() |
| 3245 | ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl; |
| 3246 | case Decl::CXXMethod: return CXCursor_CXXMethod; |
| 3247 | case Decl::CXXConstructor: return CXCursor_Constructor; |
| 3248 | case Decl::CXXDestructor: return CXCursor_Destructor; |
| 3249 | case Decl::CXXConversion: return CXCursor_ConversionFunction; |
| 3250 | case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl; |
| Argyrios Kyrtzidis | 3698cef | 2012-01-24 21:39:26 +0000 | [diff] [blame] | 3251 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3252 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 3253 | case Decl::Typedef: return CXCursor_TypedefDecl; |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3254 | case Decl::TypeAlias: return CXCursor_TypeAliasDecl; |
| Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 3255 | case Decl::TypeAliasTemplate: return CXCursor_TypeAliasTemplateDecl; |
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3256 | case Decl::Var: return CXCursor_VarDecl; |
| 3257 | case Decl::Namespace: return CXCursor_Namespace; |
| 3258 | case Decl::NamespaceAlias: return CXCursor_NamespaceAlias; |
| 3259 | case Decl::TemplateTypeParm: return CXCursor_TemplateTypeParameter; |
| 3260 | case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter; |
| 3261 | case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter; |
| 3262 | case Decl::FunctionTemplate: return CXCursor_FunctionTemplate; |
| 3263 | case Decl::ClassTemplate: return CXCursor_ClassTemplate; |
| Argyrios Kyrtzidis | 12afd70 | 2011-09-30 17:58:23 +0000 | [diff] [blame] | 3264 | case Decl::AccessSpec: return CXCursor_CXXAccessSpecifier; |
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3265 | case Decl::ClassTemplatePartialSpecialization: |
| 3266 | return CXCursor_ClassTemplatePartialSpecialization; |
| 3267 | case Decl::UsingDirective: return CXCursor_UsingDirective; |
| Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 3268 | case Decl::StaticAssert: return CXCursor_StaticAssert; |
| Olivier Goffart | d211c64 | 2016-11-04 06:29:27 +0000 | [diff] [blame] | 3269 | case Decl::Friend: return CXCursor_FriendDecl; |
| Douglas Gregor | 3e653b3 | 2012-04-30 23:41:16 +0000 | [diff] [blame] | 3270 | case Decl::TranslationUnit: return CXCursor_TranslationUnit; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3271 | |
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3272 | case Decl::Using: |
| 3273 | case Decl::UnresolvedUsingValue: |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3274 | case Decl::UnresolvedUsingTypename: |
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3275 | return CXCursor_UsingDeclaration; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3276 | |
| Douglas Gregor | 4cd6596 | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3277 | case Decl::ObjCPropertyImpl: |
| 3278 | switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) { |
| 3279 | case ObjCPropertyImplDecl::Dynamic: |
| 3280 | return CXCursor_ObjCDynamicDecl; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3281 | |
| Douglas Gregor | 4cd6596 | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3282 | case ObjCPropertyImplDecl::Synthesize: |
| 3283 | return CXCursor_ObjCSynthesizeDecl; |
| 3284 | } |
| Argyrios Kyrtzidis | 50e5b1d | 2012-10-05 00:22:24 +0000 | [diff] [blame] | 3285 | |
| 3286 | case Decl::Import: |
| 3287 | return CXCursor_ModuleImportDecl; |
| Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3288 | |
| 3289 | case Decl::ObjCTypeParam: return CXCursor_TemplateTypeParameter; |
| 3290 | |
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3291 | default: |
| Dmitri Gribenko | bdc80de | 2013-01-11 20:32:41 +0000 | [diff] [blame] | 3292 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3293 | switch (TD->getTagKind()) { |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3294 | case TTK_Interface: // fall through |
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3295 | case TTK_Struct: return CXCursor_StructDecl; |
| 3296 | case TTK_Class: return CXCursor_ClassDecl; |
| 3297 | case TTK_Union: return CXCursor_UnionDecl; |
| 3298 | case TTK_Enum: return CXCursor_EnumDecl; |
| 3299 | } |
| 3300 | } |
| 3301 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3302 | |
| Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 3303 | return CXCursor_UnexposedDecl; |
| 3304 | } |
| 3305 | |
| Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3306 | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, |
| Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 3307 | bool LoadExternal, bool IncludeUndefined, |
| Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3308 | bool TargetTypeIsPointer = false) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3309 | typedef CodeCompletionResult Result; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3310 | |
| Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 3311 | Results.EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3312 | |
| Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 3313 | for (Preprocessor::macro_iterator M = PP.macro_begin(LoadExternal), |
| 3314 | MEnd = PP.macro_end(LoadExternal); |
| Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3315 | M != MEnd; ++M) { |
| Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 3316 | auto MD = PP.getMacroDefinition(M->first); |
| 3317 | if (IncludeUndefined || MD) { |
| Eric Liu | d485df1 | 2018-09-05 14:59:17 +0000 | [diff] [blame] | 3318 | MacroInfo *MI = MD.getMacroInfo(); |
| 3319 | if (MI && MI->isUsedForHeaderGuard()) |
| 3320 | continue; |
| Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 3321 | |
| Eric Liu | d485df1 | 2018-09-05 14:59:17 +0000 | [diff] [blame] | 3322 | Results.AddResult( |
| 3323 | Result(M->first, MI, |
| 3324 | getMacroUsagePriority(M->first->getName(), PP.getLangOpts(), |
| 3325 | TargetTypeIsPointer))); |
| Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 3326 | } |
| Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3327 | } |
| 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 | Results.ExitScope(); |
| 3330 | } |
| 3331 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3332 | static void AddPrettyFunctionResults(const LangOptions &LangOpts, |
| Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3333 | ResultBuilder &Results) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3334 | typedef CodeCompletionResult Result; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3335 | |
| Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3336 | Results.EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3337 | |
| Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3338 | Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant)); |
| 3339 | Results.AddResult(Result("__FUNCTION__", CCP_Constant)); |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3340 | if (LangOpts.C99 || LangOpts.CPlusPlus11) |
| Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3341 | Results.AddResult(Result("__func__", CCP_Constant)); |
| 3342 | Results.ExitScope(); |
| 3343 | } |
| 3344 | |
| Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 3345 | static void HandleCodeCompleteResults(Sema *S, |
| 3346 | CodeCompleteConsumer *CodeCompleter, |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3347 | CodeCompletionContext Context, |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3348 | CodeCompletionResult *Results, |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3349 | unsigned NumResults) { |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3350 | if (CodeCompleter) |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3351 | CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 3352 | } |
| 3353 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3354 | static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S, |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3355 | Sema::ParserCompletionContext PCC) { |
| 3356 | switch (PCC) { |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3357 | case Sema::PCC_Namespace: |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3358 | return CodeCompletionContext::CCC_TopLevel; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3359 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3360 | case Sema::PCC_Class: |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3361 | return CodeCompletionContext::CCC_ClassStructUnion; |
| 3362 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3363 | case Sema::PCC_ObjCInterface: |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3364 | return CodeCompletionContext::CCC_ObjCInterface; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3365 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3366 | case Sema::PCC_ObjCImplementation: |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3367 | return CodeCompletionContext::CCC_ObjCImplementation; |
| 3368 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3369 | case Sema::PCC_ObjCInstanceVariableList: |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3370 | return CodeCompletionContext::CCC_ObjCIvarList; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3371 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3372 | case Sema::PCC_Template: |
| 3373 | case Sema::PCC_MemberTemplate: |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3374 | if (S.CurContext->isFileContext()) |
| 3375 | return CodeCompletionContext::CCC_TopLevel; |
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3376 | if (S.CurContext->isRecord()) |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3377 | return CodeCompletionContext::CCC_ClassStructUnion; |
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3378 | return CodeCompletionContext::CCC_Other; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3379 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3380 | case Sema::PCC_RecoveryInFunction: |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3381 | return CodeCompletionContext::CCC_Recovery; |
| Douglas Gregor | c769d6e | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 3382 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3383 | case Sema::PCC_ForInit: |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3384 | if (S.getLangOpts().CPlusPlus || S.getLangOpts().C99 || |
| 3385 | S.getLangOpts().ObjC1) |
| Douglas Gregor | c769d6e | 2010-10-18 22:01:46 +0000 | [diff] [blame] | 3386 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
| 3387 | else |
| 3388 | return CodeCompletionContext::CCC_Expression; |
| 3389 | |
| 3390 | case Sema::PCC_Expression: |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3391 | case Sema::PCC_Condition: |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3392 | return CodeCompletionContext::CCC_Expression; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3393 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3394 | case Sema::PCC_Statement: |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3395 | return CodeCompletionContext::CCC_Statement; |
| Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3396 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3397 | case Sema::PCC_Type: |
| Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3398 | return CodeCompletionContext::CCC_Type; |
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3399 | |
| 3400 | case Sema::PCC_ParenthesizedExpression: |
| 3401 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3402 | |
| Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3403 | case Sema::PCC_LocalDeclarationSpecifiers: |
| 3404 | return CodeCompletionContext::CCC_Type; |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3405 | } |
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3406 | |
| 3407 | llvm_unreachable("Invalid ParserCompletionContext!"); |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3410 | /// If we're in a C++ virtual member function, add completion results |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3411 | /// 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] | 3412 | /// overridden function as well as adding new functionality. |
| 3413 | /// |
| 3414 | /// \param S The semantic analysis object for which we are generating results. |
| 3415 | /// |
| 3416 | /// \param InContext This context in which the nested-name-specifier preceding |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3417 | /// the code-completion point |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3418 | static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, |
| 3419 | ResultBuilder &Results) { |
| 3420 | // Look through blocks. |
| 3421 | DeclContext *CurContext = S.CurContext; |
| 3422 | while (isa<BlockDecl>(CurContext)) |
| 3423 | CurContext = CurContext->getParent(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3424 | |
| 3425 | |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3426 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext); |
| 3427 | if (!Method || !Method->isVirtual()) |
| 3428 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3429 | |
| 3430 | // 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] | 3431 | // generate a forwarding call. |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3432 | for (auto P : Method->parameters()) |
| Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3433 | if (!P->getDeclName()) |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3434 | return; |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3435 | |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3436 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
| Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 3437 | for (const CXXMethodDecl *Overridden : Method->overridden_methods()) { |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3438 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3439 | Results.getCodeCompletionTUInfo()); |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3440 | if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl()) |
| 3441 | continue; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3442 | |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3443 | // If we need a nested-name-specifier, add one now. |
| 3444 | if (!InContext) { |
| 3445 | NestedNameSpecifier *NNS |
| 3446 | = getRequiredQualification(S.Context, CurContext, |
| 3447 | Overridden->getDeclContext()); |
| 3448 | if (NNS) { |
| 3449 | std::string Str; |
| 3450 | llvm::raw_string_ostream OS(Str); |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3451 | NNS->print(OS, Policy); |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 3452 | Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str())); |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3453 | } |
| 3454 | } else if (!InContext->Equals(Overridden->getDeclContext())) |
| 3455 | continue; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3456 | |
| 3457 | Builder.AddTypedTextChunk(Results.getAllocator().CopyString( |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3458 | Overridden->getNameAsString())); |
| 3459 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3460 | bool FirstParam = true; |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3461 | for (auto P : Method->parameters()) { |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3462 | if (FirstParam) |
| 3463 | FirstParam = false; |
| 3464 | else |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3465 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3466 | |
| Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3467 | Builder.AddPlaceholderChunk( |
| 3468 | Results.getAllocator().CopyString(P->getIdentifier()->getName())); |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3469 | } |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3470 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3471 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3472 | CCP_SuperCompletion, |
| Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 3473 | CXCursor_CXXMethod, |
| 3474 | CXAvailability_Available, |
| 3475 | Overridden)); |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3476 | Results.Ignore(Overridden); |
| 3477 | } |
| 3478 | } |
| 3479 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3480 | void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc, |
| Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3481 | ModuleIdPath Path) { |
| 3482 | typedef CodeCompletionResult Result; |
| 3483 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3484 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3485 | CodeCompletionContext::CCC_Other); |
| 3486 | Results.EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3487 | |
| Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3488 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3489 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
| Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3490 | typedef CodeCompletionResult Result; |
| 3491 | if (Path.empty()) { |
| 3492 | // Enumerate all top-level modules. |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3493 | SmallVector<Module *, 8> Modules; |
| Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3494 | PP.getHeaderSearchInfo().collectAllModules(Modules); |
| 3495 | for (unsigned I = 0, N = Modules.size(); I != N; ++I) { |
| 3496 | Builder.AddTypedTextChunk( |
| 3497 | Builder.getAllocator().CopyString(Modules[I]->Name)); |
| 3498 | Results.AddResult(Result(Builder.TakeString(), |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3499 | CCP_Declaration, |
| Argyrios Kyrtzidis | 345d05f | 2013-05-29 18:50:15 +0000 | [diff] [blame] | 3500 | CXCursor_ModuleImportDecl, |
| Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3501 | Modules[I]->isAvailable() |
| 3502 | ? CXAvailability_Available |
| 3503 | : CXAvailability_NotAvailable)); |
| 3504 | } |
| Daniel Jasper | 07e6c40 | 2013-08-05 20:26:17 +0000 | [diff] [blame] | 3505 | } else if (getLangOpts().Modules) { |
| Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3506 | // Load the named module. |
| 3507 | Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path, |
| 3508 | Module::AllVisible, |
| 3509 | /*IsInclusionDirective=*/false); |
| 3510 | // Enumerate submodules. |
| 3511 | if (Mod) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3512 | for (Module::submodule_iterator Sub = Mod->submodule_begin(), |
| Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3513 | SubEnd = Mod->submodule_end(); |
| 3514 | Sub != SubEnd; ++Sub) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3515 | |
| Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3516 | Builder.AddTypedTextChunk( |
| 3517 | Builder.getAllocator().CopyString((*Sub)->Name)); |
| 3518 | Results.AddResult(Result(Builder.TakeString(), |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3519 | CCP_Declaration, |
| Argyrios Kyrtzidis | 345d05f | 2013-05-29 18:50:15 +0000 | [diff] [blame] | 3520 | CXCursor_ModuleImportDecl, |
| Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3521 | (*Sub)->isAvailable() |
| 3522 | ? CXAvailability_Available |
| 3523 | : CXAvailability_NotAvailable)); |
| 3524 | } |
| 3525 | } |
| 3526 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3527 | Results.ExitScope(); |
| Douglas Gregor | 07f4357 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 3528 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 3529 | Results.data(),Results.size()); |
| 3530 | } |
| 3531 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3532 | void Sema::CodeCompleteOrdinaryName(Scope *S, |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3533 | ParserCompletionContext CompletionContext) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3534 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3535 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3536 | mapCodeCompletionContext(*this, CompletionContext)); |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 3537 | Results.EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3538 | |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3539 | // Determine how to filter results, e.g., so that the names of |
| 3540 | // values (functions, enumerators, function templates, etc.) are |
| 3541 | // only allowed where we can have an expression. |
| 3542 | switch (CompletionContext) { |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3543 | case PCC_Namespace: |
| 3544 | case PCC_Class: |
| 3545 | case PCC_ObjCInterface: |
| 3546 | case PCC_ObjCImplementation: |
| 3547 | case PCC_ObjCInstanceVariableList: |
| 3548 | case PCC_Template: |
| 3549 | case PCC_MemberTemplate: |
| Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3550 | case PCC_Type: |
| Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3551 | case PCC_LocalDeclarationSpecifiers: |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3552 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 3553 | break; |
| 3554 | |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3555 | case PCC_Statement: |
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3556 | case PCC_ParenthesizedExpression: |
| Douglas Gregor | 4d755e8 | 2010-08-24 23:58:17 +0000 | [diff] [blame] | 3557 | case PCC_Expression: |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3558 | case PCC_ForInit: |
| 3559 | case PCC_Condition: |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3560 | if (WantTypesInContext(CompletionContext, getLangOpts())) |
| Douglas Gregor | 70febae | 2010-05-28 00:49:12 +0000 | [diff] [blame] | 3561 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 3562 | else |
| 3563 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3564 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3565 | if (getLangOpts().CPlusPlus) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3566 | MaybeAddOverrideCalls(*this, /*InContext=*/nullptr, Results); |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3567 | break; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3568 | |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3569 | case PCC_RecoveryInFunction: |
| Douglas Gregor | 6da3db4 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 3570 | // Unfiltered |
| 3571 | break; |
| Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 3572 | } |
| 3573 | |
| Douglas Gregor | 9be0ed4 | 2010-08-26 16:36:48 +0000 | [diff] [blame] | 3574 | // If we are in a C++ non-static member function, check the qualifiers on |
| 3575 | // the member function to filter/prioritize the results list. |
| 3576 | if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext)) |
| 3577 | if (CurMethod->isInstance()) |
| 3578 | Results.setObjectTypeQualifiers( |
| 3579 | Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers())); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3580 | |
| Douglas Gregor | c580c52 | 2010-01-14 01:09:38 +0000 | [diff] [blame] | 3581 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3582 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 3583 | CodeCompleter->includeGlobals(), |
| 3584 | CodeCompleter->loadExternal()); |
| Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 3585 | |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 3586 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); |
| Douglas Gregor | 9225369 | 2009-12-07 09:54:55 +0000 | [diff] [blame] | 3587 | Results.ExitScope(); |
| 3588 | |
| Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3589 | switch (CompletionContext) { |
| Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 3590 | case PCC_ParenthesizedExpression: |
| Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3591 | case PCC_Expression: |
| 3592 | case PCC_Statement: |
| 3593 | case PCC_RecoveryInFunction: |
| 3594 | if (S->getFnParent()) |
| Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 3595 | AddPrettyFunctionResults(getLangOpts(), Results); |
| Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3596 | break; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3597 | |
| Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3598 | case PCC_Namespace: |
| 3599 | case PCC_Class: |
| 3600 | case PCC_ObjCInterface: |
| 3601 | case PCC_ObjCImplementation: |
| 3602 | case PCC_ObjCInstanceVariableList: |
| 3603 | case PCC_Template: |
| 3604 | case PCC_MemberTemplate: |
| 3605 | case PCC_ForInit: |
| 3606 | case PCC_Condition: |
| 3607 | case PCC_Type: |
| Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3608 | case PCC_LocalDeclarationSpecifiers: |
| Douglas Gregor | f02e5f3 | 2010-08-24 01:11:00 +0000 | [diff] [blame] | 3609 | break; |
| Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3610 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3611 | |
| Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 3612 | if (CodeCompleter->includeMacros()) |
| Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 3613 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3614 | |
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 3615 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3616 | Results.data(),Results.size()); |
| Douglas Gregor | 9d64c5e | 2009-09-21 20:51:25 +0000 | [diff] [blame] | 3617 | } |
| 3618 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3619 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3620 | ParsedType Receiver, |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 3621 | ArrayRef<IdentifierInfo *> SelIdents, |
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 3622 | bool AtArgumentExpression, |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3623 | bool IsSuper, |
| 3624 | ResultBuilder &Results); |
| 3625 | |
| 3626 | void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, |
| 3627 | bool AllowNonIdentifiers, |
| 3628 | bool AllowNestedNameSpecifiers) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3629 | typedef CodeCompletionResult Result; |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 3630 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 3631 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3632 | AllowNestedNameSpecifiers |
| 3633 | ? CodeCompletionContext::CCC_PotentiallyQualifiedName |
| 3634 | : CodeCompletionContext::CCC_Name); |
| Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3635 | Results.EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3636 | |
| Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3637 | // Type qualifiers can come after names. |
| 3638 | Results.AddResult(Result("const")); |
| 3639 | Results.AddResult(Result("volatile")); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3640 | if (getLangOpts().C99) |
| Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3641 | Results.AddResult(Result("restrict")); |
| 3642 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3643 | if (getLangOpts().CPlusPlus) { |
| Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 3644 | if (getLangOpts().CPlusPlus11 && |
| 3645 | (DS.getTypeSpecType() == DeclSpec::TST_class || |
| 3646 | DS.getTypeSpecType() == DeclSpec::TST_struct)) |
| 3647 | Results.AddResult("final"); |
| 3648 | |
| Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3649 | if (AllowNonIdentifiers) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3650 | Results.AddResult(Result("operator")); |
| Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3651 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3652 | |
| Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3653 | // Add nested-name-specifiers. |
| 3654 | if (AllowNestedNameSpecifiers) { |
| 3655 | Results.allowNestedNameSpecifiers(); |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3656 | Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy); |
| Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3657 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 3658 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
| Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 3659 | CodeCompleter->includeGlobals(), |
| 3660 | CodeCompleter->loadExternal()); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3661 | Results.setFilter(nullptr); |
| Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3662 | } |
| 3663 | } |
| 3664 | Results.ExitScope(); |
| 3665 | |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3666 | // If we're in a context where we might have an expression (rather than a |
| 3667 | // declaration), and what we've seen so far is an Objective-C type that could |
| 3668 | // be a receiver of a class message, this may be a class message send with |
| 3669 | // the initial opening bracket '[' missing. Add appropriate completions. |
| 3670 | if (AllowNonIdentifiers && !AllowNestedNameSpecifiers && |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3671 | DS.getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier && |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3672 | DS.getTypeSpecType() == DeclSpec::TST_typename && |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3673 | DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified && |
| 3674 | DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3675 | !DS.isTypeAltiVecVector() && |
| 3676 | S && |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3677 | (S->getFlags() & Scope::DeclScope) != 0 && |
| 3678 | (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3679 | Scope::FunctionPrototypeScope | |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3680 | Scope::AtCatchScope)) == 0) { |
| 3681 | ParsedType T = DS.getRepAsType(); |
| 3682 | if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType()) |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 3683 | AddClassMessageCompletions(*this, S, T, None, false, false, Results); |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3684 | } |
| 3685 | |
| Douglas Gregor | 56ccce0 | 2010-08-24 04:59:56 +0000 | [diff] [blame] | 3686 | // Note that we intentionally suppress macro results here, since we do not |
| 3687 | // encourage using macros to produce the names of entities. |
| 3688 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3689 | HandleCodeCompleteResults(this, CodeCompleter, |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 3690 | Results.getCompletionContext(), |
| Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3691 | Results.data(), Results.size()); |
| 3692 | } |
| 3693 | |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3694 | struct Sema::CodeCompleteExpressionData { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3695 | CodeCompleteExpressionData(QualType PreferredType = QualType()) |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3696 | : PreferredType(PreferredType), IntegralConstantExpression(false), |
| 3697 | ObjCCollection(false) { } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3698 | |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3699 | QualType PreferredType; |
| 3700 | bool IntegralConstantExpression; |
| 3701 | bool ObjCCollection; |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3702 | SmallVector<Decl *, 4> IgnoreDecls; |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3703 | }; |
| 3704 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3705 | /// Perform code-completion in an expression context when we know what |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3706 | /// type we're looking for. |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3707 | void Sema::CodeCompleteExpression(Scope *S, |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3708 | const CodeCompleteExpressionData &Data) { |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 3709 | ResultBuilder Results( |
| 3710 | *this, CodeCompleter->getAllocator(), |
| 3711 | CodeCompleter->getCodeCompletionTUInfo(), |
| 3712 | CodeCompletionContext(CodeCompletionContext::CCC_Expression, |
| 3713 | Data.PreferredType)); |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3714 | if (Data.ObjCCollection) |
| 3715 | Results.setFilter(&ResultBuilder::IsObjCCollection); |
| 3716 | else if (Data.IntegralConstantExpression) |
| Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 3717 | Results.setFilter(&ResultBuilder::IsIntegralConstantValue); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3718 | else if (WantTypesInContext(PCC_Expression, getLangOpts())) |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3719 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 3720 | else |
| 3721 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3722 | |
| 3723 | if (!Data.PreferredType.isNull()) |
| 3724 | Results.setPreferredType(Data.PreferredType.getNonReferenceType()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3725 | |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3726 | // Ignore any declarations that we were told that we don't care about. |
| 3727 | for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I) |
| 3728 | Results.Ignore(Data.IgnoreDecls[I]); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3729 | |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3730 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 3731 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 3732 | CodeCompleter->includeGlobals(), |
| 3733 | CodeCompleter->loadExternal()); |
| 3734 | |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3735 | Results.EnterNewScope(); |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 3736 | AddOrdinaryNameResults(PCC_Expression, S, *this, Results); |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3737 | Results.ExitScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3738 | |
| Douglas Gregor | 55b037b | 2010-07-08 20:55:51 +0000 | [diff] [blame] | 3739 | bool PreferredTypeIsPointer = false; |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3740 | if (!Data.PreferredType.isNull()) |
| 3741 | PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType() |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3742 | || Data.PreferredType->isMemberPointerType() |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 3743 | || Data.PreferredType->isBlockPointerType(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3744 | |
| 3745 | if (S->getFnParent() && |
| 3746 | !Data.ObjCCollection && |
| Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3747 | !Data.IntegralConstantExpression) |
| Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 3748 | AddPrettyFunctionResults(getLangOpts(), Results); |
| Douglas Gregor | ce0e856 | 2010-08-23 21:54:33 +0000 | [diff] [blame] | 3749 | |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3750 | if (CodeCompleter->includeMacros()) |
| Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 3751 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false, |
| 3752 | PreferredTypeIsPointer); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 3753 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 3754 | Results.data(), Results.size()); |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3755 | } |
| 3756 | |
| Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 3757 | void Sema::CodeCompleteExpression(Scope *S, QualType PreferredType) { |
| 3758 | return CodeCompleteExpression(S, CodeCompleteExpressionData(PreferredType)); |
| 3759 | } |
| 3760 | |
| Douglas Gregor | eda7e54 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 3761 | void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) { |
| 3762 | if (E.isInvalid()) |
| 3763 | CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3764 | else if (getLangOpts().ObjC1) |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3765 | CodeCompleteObjCInstanceMessage(S, E.get(), None, false); |
| Douglas Gregor | ed0b69d | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 3766 | } |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 3767 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3768 | /// The set of properties that have already been added, referenced by |
| Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 3769 | /// property name. |
| 3770 | typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet; |
| 3771 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3772 | /// Retrieve the container definition, if any? |
| Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3773 | static ObjCContainerDecl *getContainerDef(ObjCContainerDecl *Container) { |
| 3774 | if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 3775 | if (Interface->hasDefinition()) |
| 3776 | return Interface->getDefinition(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3777 | |
| Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3778 | return Interface; |
| 3779 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3780 | |
| Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3781 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 3782 | if (Protocol->hasDefinition()) |
| 3783 | return Protocol->getDefinition(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3784 | |
| Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3785 | return Protocol; |
| 3786 | } |
| 3787 | return Container; |
| 3788 | } |
| 3789 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3790 | /// Adds a block invocation code completion result for the given block |
| Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3791 | /// declaration \p BD. |
| 3792 | static void AddObjCBlockCall(ASTContext &Context, const PrintingPolicy &Policy, |
| 3793 | CodeCompletionBuilder &Builder, |
| 3794 | const NamedDecl *BD, |
| 3795 | const FunctionTypeLoc &BlockLoc, |
| 3796 | const FunctionProtoTypeLoc &BlockProtoLoc) { |
| 3797 | Builder.AddResultTypeChunk( |
| 3798 | GetCompletionTypeString(BlockLoc.getReturnLoc().getType(), Context, |
| 3799 | Policy, Builder.getAllocator())); |
| 3800 | |
| 3801 | AddTypedNameChunk(Context, Policy, BD, Builder); |
| 3802 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 3803 | |
| 3804 | if (BlockProtoLoc && BlockProtoLoc.getTypePtr()->isVariadic()) { |
| 3805 | Builder.AddPlaceholderChunk("..."); |
| 3806 | } else { |
| 3807 | for (unsigned I = 0, N = BlockLoc.getNumParams(); I != N; ++I) { |
| 3808 | if (I) |
| 3809 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 3810 | |
| 3811 | // Format the placeholder string. |
| 3812 | std::string PlaceholderStr = |
| 3813 | FormatFunctionParameter(Policy, BlockLoc.getParam(I)); |
| 3814 | |
| 3815 | if (I == N - 1 && BlockProtoLoc && |
| 3816 | BlockProtoLoc.getTypePtr()->isVariadic()) |
| 3817 | PlaceholderStr += ", ..."; |
| 3818 | |
| 3819 | // Add the placeholder string. |
| 3820 | Builder.AddPlaceholderChunk( |
| 3821 | Builder.getAllocator().CopyString(PlaceholderStr)); |
| 3822 | } |
| 3823 | } |
| 3824 | |
| 3825 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 3826 | } |
| 3827 | |
| Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3828 | static void AddObjCProperties( |
| 3829 | const CodeCompletionContext &CCContext, ObjCContainerDecl *Container, |
| 3830 | bool AllowCategories, bool AllowNullaryMethods, DeclContext *CurContext, |
| 3831 | AddedPropertiesSet &AddedProperties, ResultBuilder &Results, |
| 3832 | bool IsBaseExprStatement = false, bool IsClassProperty = false) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 3833 | typedef CodeCompletionResult Result; |
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3834 | |
| Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 3835 | // Retrieve the definition. |
| 3836 | Container = getContainerDef(Container); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3837 | |
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3838 | // Add properties in this container. |
| Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3839 | const auto AddProperty = [&](const ObjCPropertyDecl *P) { |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3840 | if (!AddedProperties.insert(P->getIdentifier()).second) |
| Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3841 | return; |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3842 | |
| Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3843 | // FIXME: Provide block invocation completion for non-statement |
| 3844 | // expressions. |
| 3845 | if (!P->getType().getTypePtr()->isBlockPointerType() || |
| 3846 | !IsBaseExprStatement) { |
| 3847 | Results.MaybeAddResult(Result(P, Results.getBasePriority(P), nullptr), |
| 3848 | CurContext); |
| Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3849 | return; |
| Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
| 3852 | // Block setter and invocation completion is provided only when we are able |
| 3853 | // to find the FunctionProtoTypeLoc with parameter names for the block. |
| 3854 | FunctionTypeLoc BlockLoc; |
| 3855 | FunctionProtoTypeLoc BlockProtoLoc; |
| 3856 | findTypeLocationForBlockDecl(P->getTypeSourceInfo(), BlockLoc, |
| 3857 | BlockProtoLoc); |
| 3858 | if (!BlockLoc) { |
| 3859 | Results.MaybeAddResult(Result(P, Results.getBasePriority(P), nullptr), |
| 3860 | CurContext); |
| Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3861 | return; |
| Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3862 | } |
| 3863 | |
| 3864 | // The default completion result for block properties should be the block |
| 3865 | // invocation completion when the base expression is a statement. |
| 3866 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3867 | Results.getCodeCompletionTUInfo()); |
| 3868 | AddObjCBlockCall(Container->getASTContext(), |
| 3869 | getCompletionPrintingPolicy(Results.getSema()), Builder, P, |
| 3870 | BlockLoc, BlockProtoLoc); |
| 3871 | Results.MaybeAddResult( |
| 3872 | Result(Builder.TakeString(), P, Results.getBasePriority(P)), |
| 3873 | CurContext); |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3874 | |
| 3875 | // Provide additional block setter completion iff the base expression is a |
| Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3876 | // statement and the block property is mutable. |
| 3877 | if (!P->isReadOnly()) { |
| 3878 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3879 | Results.getCodeCompletionTUInfo()); |
| 3880 | AddResultTypeChunk(Container->getASTContext(), |
| 3881 | getCompletionPrintingPolicy(Results.getSema()), P, |
| 3882 | CCContext.getBaseType(), Builder); |
| 3883 | Builder.AddTypedTextChunk( |
| 3884 | Results.getAllocator().CopyString(P->getName())); |
| 3885 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3886 | |
| Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3887 | std::string PlaceholderStr = formatBlockPlaceholder( |
| 3888 | getCompletionPrintingPolicy(Results.getSema()), P, BlockLoc, |
| 3889 | BlockProtoLoc, /*SuppressBlockName=*/true); |
| 3890 | // Add the placeholder string. |
| 3891 | Builder.AddPlaceholderChunk( |
| 3892 | Builder.getAllocator().CopyString(PlaceholderStr)); |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3893 | |
| Alex Lorenz | 6e0f393 | 2017-01-06 12:00:44 +0000 | [diff] [blame] | 3894 | // When completing blocks properties that return void the default |
| 3895 | // property completion result should show up before the setter, |
| 3896 | // otherwise the setter completion should show up before the default |
| 3897 | // property completion, as we normally want to use the result of the |
| 3898 | // call. |
| Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3899 | Results.MaybeAddResult( |
| 3900 | Result(Builder.TakeString(), P, |
| Alex Lorenz | 6e0f393 | 2017-01-06 12:00:44 +0000 | [diff] [blame] | 3901 | Results.getBasePriority(P) + |
| 3902 | (BlockLoc.getTypePtr()->getReturnType()->isVoidType() |
| 3903 | ? CCD_BlockPropertySetter |
| 3904 | : -CCD_BlockPropertySetter)), |
| Alex Lorenz | baef802 | 2016-11-09 13:43:18 +0000 | [diff] [blame] | 3905 | CurContext); |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3906 | } |
| Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3907 | }; |
| 3908 | |
| 3909 | if (IsClassProperty) { |
| 3910 | for (const auto *P : Container->class_properties()) |
| 3911 | AddProperty(P); |
| 3912 | } else { |
| 3913 | for (const auto *P : Container->instance_properties()) |
| 3914 | AddProperty(P); |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3915 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3916 | |
| Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3917 | // Add nullary methods or implicit class properties |
| Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3918 | if (AllowNullaryMethods) { |
| 3919 | ASTContext &Context = Container->getASTContext(); |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 3920 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
| Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3921 | // Adds a method result |
| 3922 | const auto AddMethod = [&](const ObjCMethodDecl *M) { |
| 3923 | IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0); |
| 3924 | if (!Name) |
| 3925 | return; |
| 3926 | if (!AddedProperties.insert(Name).second) |
| 3927 | return; |
| 3928 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 3929 | Results.getCodeCompletionTUInfo()); |
| 3930 | AddResultTypeChunk(Context, Policy, M, CCContext.getBaseType(), Builder); |
| 3931 | Builder.AddTypedTextChunk( |
| 3932 | Results.getAllocator().CopyString(Name->getName())); |
| 3933 | Results.MaybeAddResult( |
| 3934 | Result(Builder.TakeString(), M, |
| 3935 | CCP_MemberDeclaration + CCD_MethodAsProperty), |
| 3936 | CurContext); |
| 3937 | }; |
| 3938 | |
| 3939 | if (IsClassProperty) { |
| 3940 | for (const auto *M : Container->methods()) { |
| 3941 | // Gather the class method that can be used as implicit property |
| 3942 | // getters. Methods with arguments or methods that return void aren't |
| 3943 | // added to the results as they can't be used as a getter. |
| 3944 | if (!M->getSelector().isUnarySelector() || |
| 3945 | M->getReturnType()->isVoidType() || M->isInstanceMethod()) |
| 3946 | continue; |
| 3947 | AddMethod(M); |
| 3948 | } |
| 3949 | } else { |
| 3950 | for (auto *M : Container->methods()) { |
| 3951 | if (M->getSelector().isUnarySelector()) |
| 3952 | AddMethod(M); |
| 3953 | } |
| Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 3954 | } |
| 3955 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 3956 | |
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3957 | // Add properties in referenced protocols. |
| 3958 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 3959 | for (auto *P : Protocol->protocols()) |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3960 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3961 | CurContext, AddedProperties, Results, |
| Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3962 | IsBaseExprStatement, IsClassProperty); |
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3963 | } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){ |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3964 | if (AllowCategories) { |
| 3965 | // Look through categories. |
| Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 3966 | for (auto *Cat : IFace->known_categories()) |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3967 | AddObjCProperties(CCContext, Cat, AllowCategories, AllowNullaryMethods, |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3968 | CurContext, AddedProperties, Results, |
| Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3969 | IsBaseExprStatement, IsClassProperty); |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 3970 | } |
| Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 3971 | |
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3972 | // Look through protocols. |
| Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 3973 | for (auto *I : IFace->all_referenced_protocols()) |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3974 | AddObjCProperties(CCContext, I, AllowCategories, AllowNullaryMethods, |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3975 | CurContext, AddedProperties, Results, |
| Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3976 | IsBaseExprStatement, IsClassProperty); |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3977 | |
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3978 | // Look in the superclass. |
| 3979 | if (IFace->getSuperClass()) |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3980 | AddObjCProperties(CCContext, IFace->getSuperClass(), AllowCategories, |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3981 | AllowNullaryMethods, CurContext, AddedProperties, |
| Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3982 | Results, IsBaseExprStatement, IsClassProperty); |
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3983 | } else if (const ObjCCategoryDecl *Category |
| 3984 | = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 3985 | // Look through protocols. |
| Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 3986 | for (auto *P : Category->protocols()) |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 3987 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 3988 | CurContext, AddedProperties, Results, |
| Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 3989 | IsBaseExprStatement, IsClassProperty); |
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 3990 | } |
| 3991 | } |
| 3992 | |
| Alex Lorenz | 0fe0d98 | 2017-05-11 13:41:00 +0000 | [diff] [blame] | 3993 | static void AddRecordMembersCompletionResults(Sema &SemaRef, |
| 3994 | ResultBuilder &Results, Scope *S, |
| 3995 | QualType BaseType, |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 3996 | RecordDecl *RD, |
| 3997 | Optional<FixItHint> AccessOpFixIt) { |
| Alex Lorenz | 0fe0d98 | 2017-05-11 13:41:00 +0000 | [diff] [blame] | 3998 | // Indicate that we are performing a member access, and the cv-qualifiers |
| 3999 | // for the base object type. |
| 4000 | Results.setObjectTypeQualifiers(BaseType.getQualifiers()); |
| 4001 | |
| 4002 | // Access to a C/C++ class, struct, or union. |
| 4003 | Results.allowNestedNameSpecifiers(); |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4004 | std::vector<FixItHint> FixIts; |
| 4005 | if (AccessOpFixIt) |
| 4006 | FixIts.emplace_back(AccessOpFixIt.getValue()); |
| 4007 | CodeCompletionDeclConsumer Consumer(Results, SemaRef.CurContext, std::move(FixIts)); |
| Alex Lorenz | 0fe0d98 | 2017-05-11 13:41:00 +0000 | [diff] [blame] | 4008 | SemaRef.LookupVisibleDecls(RD, Sema::LookupMemberName, Consumer, |
| Alex Lorenz | e6afa39 | 2017-05-11 13:48:57 +0000 | [diff] [blame] | 4009 | SemaRef.CodeCompleter->includeGlobals(), |
| Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4010 | /*IncludeDependentBases=*/true, |
| 4011 | SemaRef.CodeCompleter->loadExternal()); |
| Alex Lorenz | 0fe0d98 | 2017-05-11 13:41:00 +0000 | [diff] [blame] | 4012 | |
| 4013 | if (SemaRef.getLangOpts().CPlusPlus) { |
| 4014 | if (!Results.empty()) { |
| 4015 | // The "template" keyword can follow "->" or "." in the grammar. |
| 4016 | // However, we only want to suggest the template keyword if something |
| 4017 | // is dependent. |
| 4018 | bool IsDependent = BaseType->isDependentType(); |
| 4019 | if (!IsDependent) { |
| 4020 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) |
| 4021 | if (DeclContext *Ctx = DepScope->getEntity()) { |
| 4022 | IsDependent = Ctx->isDependentContext(); |
| 4023 | break; |
| 4024 | } |
| 4025 | } |
| 4026 | |
| 4027 | if (IsDependent) |
| 4028 | Results.AddResult(CodeCompletionResult("template")); |
| 4029 | } |
| 4030 | } |
| 4031 | } |
| 4032 | |
| Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 4033 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base, |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4034 | Expr *OtherOpBase, |
| Alex Lorenz | f0b4e5d | 2016-10-18 10:55:01 +0000 | [diff] [blame] | 4035 | SourceLocation OpLoc, bool IsArrow, |
| 4036 | bool IsBaseExprStatement) { |
| Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 4037 | if (!Base || !CodeCompleter) |
| Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4038 | return; |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4039 | |
| Douglas Gregor | 1cc88a9 | 2012-01-23 15:59:30 +0000 | [diff] [blame] | 4040 | ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow); |
| 4041 | if (ConvertedBase.isInvalid()) |
| 4042 | return; |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4043 | QualType ConvertedBaseType = ConvertedBase.get()->getType(); |
| 4044 | |
| 4045 | enum CodeCompletionContext::Kind contextKind; |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4046 | |
| 4047 | if (IsArrow) { |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4048 | if (const PointerType *Ptr = ConvertedBaseType->getAs<PointerType>()) |
| 4049 | ConvertedBaseType = Ptr->getPointeeType(); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4050 | } |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4051 | |
| Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4052 | if (IsArrow) { |
| 4053 | contextKind = CodeCompletionContext::CCC_ArrowMemberAccess; |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4054 | } else { |
| 4055 | if (ConvertedBaseType->isObjCObjectPointerType() || |
| 4056 | ConvertedBaseType->isObjCObjectOrInterfaceType()) { |
| Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4057 | contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess; |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4058 | } else { |
| Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4059 | contextKind = CodeCompletionContext::CCC_DotMemberAccess; |
| 4060 | } |
| 4061 | } |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 4062 | |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4063 | CodeCompletionContext CCContext(contextKind, ConvertedBaseType); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4064 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4065 | CodeCompleter->getCodeCompletionTUInfo(), CCContext, |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4066 | &ResultBuilder::IsMember); |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4067 | |
| 4068 | auto DoCompletion = [&](Expr *Base, bool IsArrow, Optional<FixItHint> AccessOpFixIt) -> bool { |
| 4069 | if (!Base) |
| 4070 | return false; |
| 4071 | |
| 4072 | ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow); |
| 4073 | if (ConvertedBase.isInvalid()) |
| 4074 | return false; |
| 4075 | Base = ConvertedBase.get(); |
| 4076 | |
| 4077 | QualType BaseType = Base->getType(); |
| 4078 | |
| 4079 | if (IsArrow) { |
| 4080 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 4081 | BaseType = Ptr->getPointeeType(); |
| 4082 | else if (BaseType->isObjCObjectPointerType()) |
| 4083 | /*Do nothing*/; |
| 4084 | else |
| 4085 | return false; |
| 4086 | } |
| 4087 | |
| 4088 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { |
| 4089 | AddRecordMembersCompletionResults(*this, Results, S, BaseType, |
| 4090 | Record->getDecl(), |
| 4091 | std::move(AccessOpFixIt)); |
| 4092 | } else if (const auto *TST = |
| 4093 | BaseType->getAs<TemplateSpecializationType>()) { |
| 4094 | TemplateName TN = TST->getTemplateName(); |
| 4095 | if (const auto *TD = |
| 4096 | dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl())) { |
| 4097 | CXXRecordDecl *RD = TD->getTemplatedDecl(); |
| 4098 | AddRecordMembersCompletionResults(*this, Results, S, BaseType, RD, |
| 4099 | std::move(AccessOpFixIt)); |
| 4100 | } |
| 4101 | } else if (const auto *ICNT = BaseType->getAs<InjectedClassNameType>()) { |
| 4102 | if (auto *RD = ICNT->getDecl()) |
| 4103 | AddRecordMembersCompletionResults(*this, Results, S, BaseType, RD, |
| 4104 | std::move(AccessOpFixIt)); |
| 4105 | } else if (!IsArrow && BaseType->isObjCObjectPointerType()) { |
| 4106 | // Objective-C property reference. |
| 4107 | AddedPropertiesSet AddedProperties; |
| 4108 | |
| 4109 | if (const ObjCObjectPointerType *ObjCPtr = |
| 4110 | BaseType->getAsObjCInterfacePointerType()) { |
| 4111 | // Add property results based on our interface. |
| 4112 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!"); |
| 4113 | AddObjCProperties(CCContext, ObjCPtr->getInterfaceDecl(), true, |
| 4114 | /*AllowNullaryMethods=*/true, CurContext, |
| 4115 | AddedProperties, Results, IsBaseExprStatement); |
| 4116 | } |
| 4117 | |
| 4118 | // Add properties from the protocols in a qualified interface. |
| 4119 | for (auto *I : BaseType->getAs<ObjCObjectPointerType>()->quals()) |
| 4120 | AddObjCProperties(CCContext, I, true, /*AllowNullaryMethods=*/true, |
| 4121 | CurContext, AddedProperties, Results, |
| 4122 | IsBaseExprStatement); |
| 4123 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
| 4124 | (!IsArrow && BaseType->isObjCObjectType())) { |
| 4125 | // Objective-C instance variable access. |
| 4126 | ObjCInterfaceDecl *Class = nullptr; |
| 4127 | if (const ObjCObjectPointerType *ObjCPtr = |
| 4128 | BaseType->getAs<ObjCObjectPointerType>()) |
| 4129 | Class = ObjCPtr->getInterfaceDecl(); |
| 4130 | else |
| 4131 | Class = BaseType->getAs<ObjCObjectType>()->getInterface(); |
| 4132 | |
| 4133 | // Add all ivars from this class and its superclasses. |
| 4134 | if (Class) { |
| 4135 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4136 | Results.setFilter(&ResultBuilder::IsObjCIvar); |
| 4137 | LookupVisibleDecls( |
| 4138 | Class, LookupMemberName, Consumer, CodeCompleter->includeGlobals(), |
| 4139 | /*IncludeDependentBases=*/false, CodeCompleter->loadExternal()); |
| 4140 | } |
| 4141 | } |
| 4142 | |
| 4143 | // FIXME: How do we cope with isa? |
| 4144 | return true; |
| 4145 | }; |
| 4146 | |
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4147 | Results.EnterNewScope(); |
| Alex Lorenz | 06cfa99 | 2016-10-12 11:40:15 +0000 | [diff] [blame] | 4148 | |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4149 | bool CompletionSucceded = DoCompletion(Base, IsArrow, None); |
| 4150 | if (CodeCompleter->includeFixIts()) { |
| 4151 | const CharSourceRange OpRange = |
| 4152 | CharSourceRange::getTokenRange(OpLoc, OpLoc); |
| 4153 | CompletionSucceded |= DoCompletion( |
| 4154 | OtherOpBase, !IsArrow, |
| 4155 | FixItHint::CreateReplacement(OpRange, IsArrow ? "." : "->")); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4156 | } |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4157 | |
| Douglas Gregor | 9291bad | 2009-11-18 01:29:26 +0000 | [diff] [blame] | 4158 | Results.ExitScope(); |
| Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 4159 | |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4160 | if (!CompletionSucceded) |
| 4161 | return; |
| 4162 | |
| Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 4163 | // Hand off the results found for code completion. |
| Ivan Donchevskii | b4670fc | 2018-05-25 12:56:26 +0000 | [diff] [blame] | 4164 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4165 | Results.data(), Results.size()); |
| Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4166 | } |
| 4167 | |
| Alex Lorenz | feafdf6 | 2016-12-08 15:09:40 +0000 | [diff] [blame] | 4168 | void Sema::CodeCompleteObjCClassPropertyRefExpr(Scope *S, |
| 4169 | IdentifierInfo &ClassName, |
| 4170 | SourceLocation ClassNameLoc, |
| 4171 | bool IsBaseExprStatement) { |
| 4172 | IdentifierInfo *ClassNamePtr = &ClassName; |
| 4173 | ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(ClassNamePtr, ClassNameLoc); |
| 4174 | if (!IFace) |
| 4175 | return; |
| 4176 | CodeCompletionContext CCContext( |
| 4177 | CodeCompletionContext::CCC_ObjCPropertyAccess); |
| 4178 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4179 | CodeCompleter->getCodeCompletionTUInfo(), CCContext, |
| 4180 | &ResultBuilder::IsMember); |
| 4181 | Results.EnterNewScope(); |
| 4182 | AddedPropertiesSet AddedProperties; |
| 4183 | AddObjCProperties(CCContext, IFace, true, |
| 4184 | /*AllowNullaryMethods=*/true, CurContext, AddedProperties, |
| 4185 | Results, IsBaseExprStatement, |
| 4186 | /*IsClassProperty=*/true); |
| 4187 | Results.ExitScope(); |
| 4188 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4189 | Results.data(), Results.size()); |
| 4190 | } |
| 4191 | |
| Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 4192 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { |
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4193 | if (!CodeCompleter) |
| 4194 | return; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4195 | |
| 4196 | ResultBuilder::LookupFilter Filter = nullptr; |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4197 | enum CodeCompletionContext::Kind ContextKind |
| 4198 | = CodeCompletionContext::CCC_Other; |
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4199 | switch ((DeclSpec::TST)TagSpec) { |
| 4200 | case DeclSpec::TST_enum: |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4201 | Filter = &ResultBuilder::IsEnum; |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4202 | ContextKind = CodeCompletionContext::CCC_EnumTag; |
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4203 | break; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4204 | |
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4205 | case DeclSpec::TST_union: |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4206 | Filter = &ResultBuilder::IsUnion; |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4207 | ContextKind = CodeCompletionContext::CCC_UnionTag; |
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4208 | break; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4209 | |
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4210 | case DeclSpec::TST_struct: |
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4211 | case DeclSpec::TST_class: |
| Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4212 | case DeclSpec::TST_interface: |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4213 | Filter = &ResultBuilder::IsClassOrStruct; |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4214 | ContextKind = CodeCompletionContext::CCC_ClassOrStructTag; |
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4215 | break; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4216 | |
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4217 | default: |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4218 | llvm_unreachable("Unknown type specifier kind in CodeCompleteTag"); |
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4219 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4220 | |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4221 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4222 | CodeCompleter->getCodeCompletionTUInfo(), ContextKind); |
| Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4223 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 4224 | |
| 4225 | // First pass: look for tags. |
| 4226 | Results.setFilter(Filter); |
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4227 | LookupVisibleDecls(S, LookupTagName, Consumer, |
| Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4228 | CodeCompleter->includeGlobals(), |
| 4229 | CodeCompleter->loadExternal()); |
| John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 4230 | |
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4231 | if (CodeCompleter->includeGlobals()) { |
| 4232 | // Second pass: look for nested name specifiers. |
| 4233 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); |
| Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4234 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
| 4235 | CodeCompleter->includeGlobals(), |
| 4236 | CodeCompleter->loadExternal()); |
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4237 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4238 | |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4239 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4240 | Results.data(),Results.size()); |
| Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
| Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4243 | static void AddTypeQualifierResults(DeclSpec &DS, ResultBuilder &Results, |
| 4244 | const LangOptions &LangOpts) { |
| 4245 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const)) |
| 4246 | Results.AddResult("const"); |
| 4247 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile)) |
| 4248 | Results.AddResult("volatile"); |
| 4249 | if (LangOpts.C99 && !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict)) |
| 4250 | Results.AddResult("restrict"); |
| 4251 | if (LangOpts.C11 && !(DS.getTypeQualifiers() & DeclSpec::TQ_atomic)) |
| 4252 | Results.AddResult("_Atomic"); |
| 4253 | if (LangOpts.MSVCCompat && !(DS.getTypeQualifiers() & DeclSpec::TQ_unaligned)) |
| 4254 | Results.AddResult("__unaligned"); |
| 4255 | } |
| 4256 | |
| Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4257 | void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4258 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4259 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4260 | CodeCompletionContext::CCC_TypeQualifiers); |
| Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4261 | Results.EnterNewScope(); |
| Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4262 | AddTypeQualifierResults(DS, Results, LangOpts); |
| Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4263 | Results.ExitScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4264 | HandleCodeCompleteResults(this, CodeCompleter, |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4265 | Results.getCompletionContext(), |
| Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4266 | Results.data(), Results.size()); |
| 4267 | } |
| 4268 | |
| Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4269 | void Sema::CodeCompleteFunctionQualifiers(DeclSpec &DS, Declarator &D, |
| 4270 | const VirtSpecifiers *VS) { |
| 4271 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 4272 | CodeCompleter->getCodeCompletionTUInfo(), |
| 4273 | CodeCompletionContext::CCC_TypeQualifiers); |
| 4274 | Results.EnterNewScope(); |
| 4275 | AddTypeQualifierResults(DS, Results, LangOpts); |
| 4276 | if (LangOpts.CPlusPlus11) { |
| 4277 | Results.AddResult("noexcept"); |
| Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 4278 | if (D.getContext() == DeclaratorContext::MemberContext && |
| 4279 | !D.isCtorOrDtor() && !D.isStaticMember()) { |
| Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4280 | if (!VS || !VS->isFinalSpecified()) |
| 4281 | Results.AddResult("final"); |
| 4282 | if (!VS || !VS->isOverrideSpecified()) |
| 4283 | Results.AddResult("override"); |
| 4284 | } |
| 4285 | } |
| 4286 | Results.ExitScope(); |
| 4287 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4288 | Results.data(), Results.size()); |
| 4289 | } |
| 4290 | |
| Benjamin Kramer | 72dae62 | 2016-02-18 15:30:24 +0000 | [diff] [blame] | 4291 | void Sema::CodeCompleteBracketDeclarator(Scope *S) { |
| 4292 | CodeCompleteExpression(S, QualType(getASTContext().getSizeType())); |
| 4293 | } |
| 4294 | |
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4295 | void Sema::CodeCompleteCase(Scope *S) { |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 4296 | if (getCurFunction()->SwitchStack.empty() || !CodeCompleter) |
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4297 | return; |
| John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 4298 | |
| Richard Smith | ef6c43d | 2018-07-26 18:41:30 +0000 | [diff] [blame] | 4299 | SwitchStmt *Switch = getCurFunction()->SwitchStack.back().getPointer(); |
| John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 4300 | QualType type = Switch->getCond()->IgnoreImplicit()->getType(); |
| 4301 | if (!type->isEnumeralType()) { |
| 4302 | CodeCompleteExpressionData Data(type); |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 4303 | Data.IntegralConstantExpression = true; |
| 4304 | CodeCompleteExpression(S, Data); |
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4305 | return; |
| Douglas Gregor | 85b5063 | 2010-07-28 21:50:18 +0000 | [diff] [blame] | 4306 | } |
| 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 | // Code-complete the cases of a switch statement over an enumeration type |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4309 | // by providing the list of |
| John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 4310 | EnumDecl *Enum = type->castAs<EnumType>()->getDecl(); |
| Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 4311 | if (EnumDecl *Def = Enum->getDefinition()) |
| 4312 | Enum = Def; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4313 | |
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4314 | // Determine which enumerators we have already seen in the switch statement. |
| 4315 | // FIXME: Ideally, we would also be able to look *past* the code-completion |
| 4316 | // token, in case we are code-completing in the middle of the switch and not |
| 4317 | // at the end. However, we aren't able to do so at the moment. |
| 4318 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4319 | NestedNameSpecifier *Qualifier = nullptr; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4320 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; |
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4321 | SC = SC->getNextSwitchCase()) { |
| 4322 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); |
| 4323 | if (!Case) |
| 4324 | continue; |
| 4325 | |
| 4326 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); |
| 4327 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal)) |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4328 | if (EnumConstantDecl *Enumerator |
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4329 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4330 | // We look into the AST of the case statement to determine which |
| 4331 | // enumerator was named. Alternatively, we could compute the value of |
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4332 | // the integral constant expression, then compare it against the |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4333 | // values of each enumerator. However, value-based approach would not |
| 4334 | // work as well with C++ templates where enumerators declared within a |
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4335 | // template are type- and value-dependent. |
| 4336 | EnumeratorsSeen.insert(Enumerator); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4337 | |
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4338 | // If this is a qualified-id, keep track of the nested-name-specifier |
| 4339 | // 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] | 4340 | // |
| 4341 | // switch (TagD.getKind()) { |
| 4342 | // case TagDecl::TK_enum: |
| 4343 | // break; |
| 4344 | // case XXX |
| 4345 | // |
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4346 | // At the XXX, our completions are TagDecl::TK_union, |
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4347 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, |
| 4348 | // TK_struct, and TK_class. |
| Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4349 | Qualifier = DRE->getQualifier(); |
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4350 | } |
| 4351 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4352 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4353 | if (getLangOpts().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4354 | // 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] | 4355 | // qualify the names of the enumerators that we suggest, because they |
| 4356 | // may not be visible in this scope. |
| Douglas Gregor | d3cebdb | 2012-02-01 05:02:47 +0000 | [diff] [blame] | 4357 | Qualifier = getRequiredQualification(Context, CurContext, Enum); |
| Douglas Gregor | f251067 | 2009-09-21 19:57:38 +0000 | [diff] [blame] | 4358 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4359 | |
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4360 | // Add any enumerators that have not yet been mentioned. |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4361 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4362 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4363 | CodeCompletionContext::CCC_Expression); |
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4364 | Results.EnterNewScope(); |
| Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 4365 | for (auto *E : Enum->enumerators()) { |
| 4366 | if (EnumeratorsSeen.count(E)) |
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4367 | continue; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4368 | |
| Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 4369 | CodeCompletionResult R(E, CCP_EnumInCase, Qualifier); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4370 | Results.AddResult(R, CurContext, nullptr, false); |
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4371 | } |
| 4372 | Results.ExitScope(); |
| Douglas Gregor | 28556092 | 2010-04-06 20:02:15 +0000 | [diff] [blame] | 4373 | |
| Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4374 | if (CodeCompleter->includeMacros()) { |
| Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 4375 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); |
| Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 4376 | } |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 4377 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4378 | Results.data(), Results.size()); |
| Douglas Gregor | d328d57 | 2009-09-21 18:10:23 +0000 | [diff] [blame] | 4379 | } |
| 4380 | |
| Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 4381 | static bool anyNullArguments(ArrayRef<Expr *> Args) { |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4382 | if (Args.size() && !Args.data()) |
| Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 4383 | return true; |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4384 | |
| 4385 | for (unsigned I = 0; I != Args.size(); ++I) |
| Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 4386 | if (!Args[I]) |
| 4387 | return true; |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4388 | |
| Douglas Gregor | 6ed3eb8 | 2010-05-30 06:10:08 +0000 | [diff] [blame] | 4389 | return false; |
| 4390 | } |
| 4391 | |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4392 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; |
| 4393 | |
| Francisco Lopes da Silva | 8cafefa | 2015-01-29 05:54:59 +0000 | [diff] [blame] | 4394 | static void mergeCandidatesWithResults(Sema &SemaRef, |
| 4395 | SmallVectorImpl<ResultCandidate> &Results, |
| 4396 | OverloadCandidateSet &CandidateSet, |
| 4397 | SourceLocation Loc) { |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4398 | if (!CandidateSet.empty()) { |
| 4399 | // Sort the overload candidate set by placing the best overloads first. |
| 4400 | std::stable_sort( |
| 4401 | CandidateSet.begin(), CandidateSet.end(), |
| 4402 | [&](const OverloadCandidate &X, const OverloadCandidate &Y) { |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4403 | return isBetterOverloadCandidate(SemaRef, X, Y, Loc, |
| 4404 | CandidateSet.getKind()); |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4405 | }); |
| 4406 | |
| 4407 | // Add the remaining viable overload candidates as code-completion results. |
| Erik Verbruggen | 7ec9107 | 2017-09-08 10:23:08 +0000 | [diff] [blame] | 4408 | for (auto &Candidate : CandidateSet) { |
| 4409 | if (Candidate.Function && Candidate.Function->isDeleted()) |
| 4410 | continue; |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4411 | if (Candidate.Viable) |
| 4412 | Results.push_back(ResultCandidate(Candidate.Function)); |
| Erik Verbruggen | 7ec9107 | 2017-09-08 10:23:08 +0000 | [diff] [blame] | 4413 | } |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4414 | } |
| 4415 | } |
| 4416 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4417 | /// 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] | 4418 | /// candidates. |
| Francisco Lopes da Silva | 8cafefa | 2015-01-29 05:54:59 +0000 | [diff] [blame] | 4419 | static QualType getParamType(Sema &SemaRef, |
| 4420 | ArrayRef<ResultCandidate> Candidates, |
| 4421 | unsigned N) { |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4422 | |
| 4423 | // Given the overloads 'Candidates' for a function call matching all arguments |
| 4424 | // up to N, return the type of the Nth parameter if it is the same for all |
| 4425 | // overload candidates. |
| 4426 | QualType ParamType; |
| 4427 | for (auto &Candidate : Candidates) { |
| 4428 | if (auto FType = Candidate.getFunctionType()) |
| 4429 | if (auto Proto = dyn_cast<FunctionProtoType>(FType)) |
| 4430 | if (N < Proto->getNumParams()) { |
| 4431 | if (ParamType.isNull()) |
| 4432 | ParamType = Proto->getParamType(N); |
| 4433 | else if (!SemaRef.Context.hasSameUnqualifiedType( |
| 4434 | ParamType.getNonReferenceType(), |
| 4435 | Proto->getParamType(N).getNonReferenceType())) |
| 4436 | // Otherwise return a default-constructed QualType. |
| 4437 | return QualType(); |
| 4438 | } |
| 4439 | } |
| 4440 | |
| 4441 | return ParamType; |
| 4442 | } |
| 4443 | |
| Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4444 | static QualType |
| 4445 | ProduceSignatureHelp(Sema &SemaRef, Scope *S, |
| 4446 | MutableArrayRef<ResultCandidate> Candidates, |
| 4447 | unsigned CurrentArg, SourceLocation OpenParLoc) { |
| 4448 | if (Candidates.empty()) |
| 4449 | return QualType(); |
| 4450 | SemaRef.CodeCompleter->ProcessOverloadCandidates( |
| 4451 | SemaRef, CurrentArg, Candidates.data(), Candidates.size(), OpenParLoc); |
| 4452 | return getParamType(SemaRef, Candidates, CurrentArg); |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4453 | } |
| 4454 | |
| Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4455 | QualType Sema::ProduceCallSignatureHelp(Scope *S, Expr *Fn, |
| 4456 | ArrayRef<Expr *> Args, |
| 4457 | SourceLocation OpenParLoc) { |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4458 | if (!CodeCompleter) |
| Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4459 | return QualType(); |
| Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4460 | |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4461 | // FIXME: Provide support for variadic template functions. |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4462 | // Ignore type-dependent call expressions entirely. |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4463 | if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args) || |
| 4464 | Expr::hasAnyTypeDependentArguments(Args)) { |
| Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4465 | return QualType(); |
| Douglas Gregor | 3ef5952 | 2009-12-11 19:06:04 +0000 | [diff] [blame] | 4466 | } |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4467 | |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4468 | // Build an overload candidate set based on the functions we find. |
| John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4469 | SourceLocation Loc = Fn->getExprLoc(); |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 4470 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4471 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4472 | SmallVector<ResultCandidate, 8> Results; |
| Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 4473 | |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4474 | Expr *NakedFn = Fn->IgnoreParenCasts(); |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4475 | if (auto ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4476 | AddOverloadedCallCandidates(ULE, Args, CandidateSet, |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4477 | /*PartialOverloading=*/true); |
| 4478 | else if (auto UME = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 4479 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
| 4480 | if (UME->hasExplicitTemplateArgs()) { |
| 4481 | UME->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 4482 | TemplateArgs = &TemplateArgsBuffer; |
| Douglas Gregor | ff59f67 | 2010-01-21 15:46:19 +0000 | [diff] [blame] | 4483 | } |
| Erik Verbruggen | f1898cf | 2017-03-28 07:22:21 +0000 | [diff] [blame] | 4484 | |
| 4485 | // Add the base as first argument (use a nullptr if the base is implicit). |
| 4486 | SmallVector<Expr *, 12> ArgExprs( |
| 4487 | 1, UME->isImplicitAccess() ? nullptr : UME->getBase()); |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4488 | ArgExprs.append(Args.begin(), Args.end()); |
| 4489 | UnresolvedSet<8> Decls; |
| 4490 | Decls.append(UME->decls_begin(), UME->decls_end()); |
| Benjamin Kramer | e3962ae | 2017-10-26 08:41:28 +0000 | [diff] [blame] | 4491 | const bool FirstArgumentIsBase = !UME->isImplicitAccess() && UME->getBase(); |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4492 | AddFunctionCandidates(Decls, ArgExprs, CandidateSet, TemplateArgs, |
| 4493 | /*SuppressUsedConversions=*/false, |
| Benjamin Kramer | e3962ae | 2017-10-26 08:41:28 +0000 | [diff] [blame] | 4494 | /*PartialOverloading=*/true, |
| 4495 | FirstArgumentIsBase); |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4496 | } else { |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4497 | FunctionDecl *FD = nullptr; |
| 4498 | if (auto MCE = dyn_cast<MemberExpr>(NakedFn)) |
| 4499 | FD = dyn_cast<FunctionDecl>(MCE->getMemberDecl()); |
| 4500 | else if (auto DRE = dyn_cast<DeclRefExpr>(NakedFn)) |
| 4501 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4502 | 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] | 4503 | if (!getLangOpts().CPlusPlus || |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4504 | !FD->getType()->getAs<FunctionProtoType>()) |
| 4505 | Results.push_back(ResultCandidate(FD)); |
| 4506 | else |
| 4507 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, FD->getAccess()), |
| 4508 | Args, CandidateSet, |
| 4509 | /*SuppressUsedConversions=*/false, |
| 4510 | /*PartialOverloading=*/true); |
| Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4511 | |
| 4512 | } else if (auto DC = NakedFn->getType()->getAsCXXRecordDecl()) { |
| 4513 | // If expression's type is CXXRecordDecl, it may overload the function |
| 4514 | // 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] | 4515 | // A complete type is needed to lookup for member function call operators. |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4516 | if (isCompleteType(Loc, NakedFn->getType())) { |
| Francisco Lopes da Silva | a349a8a | 2015-01-25 08:47:59 +0000 | [diff] [blame] | 4517 | DeclarationName OpName = Context.DeclarationNames |
| 4518 | .getCXXOperatorName(OO_Call); |
| 4519 | LookupResult R(*this, OpName, Loc, LookupOrdinaryName); |
| 4520 | LookupQualifiedName(R, DC); |
| 4521 | R.suppressDiagnostics(); |
| 4522 | SmallVector<Expr *, 12> ArgExprs(1, NakedFn); |
| 4523 | ArgExprs.append(Args.begin(), Args.end()); |
| 4524 | AddFunctionCandidates(R.asUnresolvedSet(), ArgExprs, CandidateSet, |
| 4525 | /*ExplicitArgs=*/nullptr, |
| 4526 | /*SuppressUsedConversions=*/false, |
| 4527 | /*PartialOverloading=*/true); |
| 4528 | } |
| Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4529 | } else { |
| 4530 | // Lastly we check whether expression's type is function pointer or |
| 4531 | // function. |
| 4532 | QualType T = NakedFn->getType(); |
| 4533 | if (!T->getPointeeType().isNull()) |
| 4534 | T = T->getPointeeType(); |
| 4535 | |
| 4536 | if (auto FP = T->getAs<FunctionProtoType>()) { |
| 4537 | if (!TooManyArguments(FP->getNumParams(), Args.size(), |
| Francisco Lopes da Silva | 62a9a4f | 2015-01-23 13:17:51 +0000 | [diff] [blame] | 4538 | /*PartialOverloading=*/true) || |
| 4539 | FP->isVariadic()) |
| Francisco Lopes da Silva | c6ccc4f | 2015-01-22 21:14:08 +0000 | [diff] [blame] | 4540 | Results.push_back(ResultCandidate(FP)); |
| 4541 | } else if (auto FT = T->getAs<FunctionType>()) |
| Francisco Lopes da Silva | 62a9a4f | 2015-01-23 13:17:51 +0000 | [diff] [blame] | 4542 | // 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] | 4543 | Results.push_back(ResultCandidate(FT)); |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4544 | } |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4545 | } |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4546 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); |
| Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4547 | QualType ParamType = |
| 4548 | ProduceSignatureHelp(*this, S, Results, Args.size(), OpenParLoc); |
| 4549 | return !CandidateSet.empty() ? ParamType : QualType(); |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4550 | } |
| 4551 | |
| Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4552 | QualType Sema::ProduceConstructorSignatureHelp(Scope *S, QualType Type, |
| 4553 | SourceLocation Loc, |
| 4554 | ArrayRef<Expr *> Args, |
| 4555 | SourceLocation OpenParLoc) { |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4556 | if (!CodeCompleter) |
| Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4557 | return QualType(); |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4558 | |
| 4559 | // A complete type is needed to lookup for constructors. |
| Ilya Biryukov | fb9dde7 | 2018-05-14 13:50:36 +0000 | [diff] [blame] | 4560 | CXXRecordDecl *RD = |
| 4561 | isCompleteType(Loc, Type) ? Type->getAsCXXRecordDecl() : nullptr; |
| Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4562 | if (!RD) |
| 4563 | return Type; |
| Argyrios Kyrtzidis | ee1d76f | 2015-03-13 07:39:30 +0000 | [diff] [blame] | 4564 | |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4565 | // FIXME: Provide support for member initializers. |
| 4566 | // FIXME: Provide support for variadic template constructors. |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4567 | |
| 4568 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
| 4569 | |
| Argyrios Kyrtzidis | ee1d76f | 2015-03-13 07:39:30 +0000 | [diff] [blame] | 4570 | for (auto C : LookupConstructors(RD)) { |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4571 | if (auto FD = dyn_cast<FunctionDecl>(C)) { |
| 4572 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, C->getAccess()), |
| 4573 | Args, CandidateSet, |
| 4574 | /*SuppressUsedConversions=*/false, |
| 4575 | /*PartialOverloading=*/true); |
| 4576 | } else if (auto FTD = dyn_cast<FunctionTemplateDecl>(C)) { |
| 4577 | AddTemplateOverloadCandidate(FTD, |
| 4578 | DeclAccessPair::make(FTD, C->getAccess()), |
| 4579 | /*ExplicitTemplateArgs=*/nullptr, |
| 4580 | Args, CandidateSet, |
| 4581 | /*SuppressUsedConversions=*/false, |
| 4582 | /*PartialOverloading=*/true); |
| 4583 | } |
| 4584 | } |
| 4585 | |
| 4586 | SmallVector<ResultCandidate, 8> Results; |
| 4587 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); |
| Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 4588 | return ProduceSignatureHelp(*this, S, Results, Args.size(), OpenParLoc); |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4589 | } |
| 4590 | |
| Kadir Cetinkaya | 84774c3 | 2018-09-11 15:02:18 +0000 | [diff] [blame] | 4591 | QualType Sema::ProduceCtorInitMemberSignatureHelp( |
| 4592 | Scope *S, Decl *ConstructorDecl, CXXScopeSpec SS, ParsedType TemplateTypeTy, |
| 4593 | ArrayRef<Expr *> ArgExprs, IdentifierInfo *II, SourceLocation OpenParLoc) { |
| 4594 | if (!CodeCompleter) |
| 4595 | return QualType(); |
| 4596 | |
| 4597 | CXXConstructorDecl *Constructor = |
| 4598 | dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
| 4599 | if (!Constructor) |
| 4600 | return QualType(); |
| 4601 | // FIXME: Add support for Base class constructors as well. |
| 4602 | if (ValueDecl *MemberDecl = tryLookupCtorInitMemberDecl( |
| 4603 | Constructor->getParent(), SS, TemplateTypeTy, II)) |
| 4604 | return ProduceConstructorSignatureHelp(getCurScope(), MemberDecl->getType(), |
| 4605 | MemberDecl->getLocation(), ArgExprs, |
| 4606 | OpenParLoc); |
| 4607 | return QualType(); |
| 4608 | } |
| 4609 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4610 | void Sema::CodeCompleteInitializer(Scope *S, Decl *D) { |
| 4611 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4612 | if (!VD) { |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4613 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4614 | return; |
| 4615 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4616 | |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4617 | CodeCompleteExpression(S, VD->getType()); |
| 4618 | } |
| 4619 | |
| 4620 | void Sema::CodeCompleteReturn(Scope *S) { |
| 4621 | QualType ResultType; |
| 4622 | if (isa<BlockDecl>(CurContext)) { |
| 4623 | if (BlockScopeInfo *BSI = getCurBlock()) |
| 4624 | ResultType = BSI->ReturnType; |
| 4625 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext)) |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4626 | ResultType = Function->getReturnType(); |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4627 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext)) |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4628 | ResultType = Method->getReturnType(); |
| 4629 | |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4630 | if (ResultType.isNull()) |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4631 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4632 | else |
| 4633 | CodeCompleteExpression(S, ResultType); |
| 4634 | } |
| 4635 | |
| Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4636 | void Sema::CodeCompleteAfterIf(Scope *S) { |
| Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4637 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4638 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4639 | mapCodeCompletionContext(*this, PCC_Statement)); |
| 4640 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
| 4641 | Results.EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4642 | |
| Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4643 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4644 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4645 | CodeCompleter->includeGlobals(), |
| 4646 | CodeCompleter->loadExternal()); |
| 4647 | |
| Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4648 | AddOrdinaryNameResults(PCC_Statement, S, *this, Results); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4649 | |
| Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4650 | // "else" block |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4651 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4652 | Results.getCodeCompletionTUInfo()); |
| Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4653 | Builder.AddTypedTextChunk("else"); |
| Douglas Gregor | 3a5d6c2 | 2012-02-16 17:49:04 +0000 | [diff] [blame] | 4654 | if (Results.includeCodePatterns()) { |
| 4655 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4656 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4657 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4658 | Builder.AddPlaceholderChunk("statements"); |
| 4659 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4660 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4661 | } |
| Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4662 | Results.AddResult(Builder.TakeString()); |
| 4663 | |
| 4664 | // "else if" block |
| 4665 | Builder.AddTypedTextChunk("else"); |
| 4666 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4667 | Builder.AddTextChunk("if"); |
| 4668 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4669 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4670 | if (getLangOpts().CPlusPlus) |
| Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4671 | Builder.AddPlaceholderChunk("condition"); |
| 4672 | else |
| 4673 | Builder.AddPlaceholderChunk("expression"); |
| 4674 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| Douglas Gregor | 3a5d6c2 | 2012-02-16 17:49:04 +0000 | [diff] [blame] | 4675 | if (Results.includeCodePatterns()) { |
| 4676 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 4677 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 4678 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4679 | Builder.AddPlaceholderChunk("statements"); |
| 4680 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 4681 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 4682 | } |
| Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4683 | Results.AddResult(Builder.TakeString()); |
| 4684 | |
| 4685 | Results.ExitScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4686 | |
| Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4687 | if (S->getFnParent()) |
| Craig Topper | 1212626 | 2015-11-15 17:27:57 +0000 | [diff] [blame] | 4688 | AddPrettyFunctionResults(getLangOpts(), Results); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4689 | |
| Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4690 | if (CodeCompleter->includeMacros()) |
| Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 4691 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4692 | |
| Douglas Gregor | 4ecb720 | 2011-07-30 08:36:53 +0000 | [diff] [blame] | 4693 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4694 | Results.data(),Results.size()); |
| 4695 | } |
| 4696 | |
| Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 4697 | void Sema::CodeCompleteAssignmentRHS(Scope *S, Expr *LHS) { |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4698 | if (LHS) |
| 4699 | CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType()); |
| 4700 | else |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4701 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 4702 | } |
| 4703 | |
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4704 | void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, |
| Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4705 | bool EnteringContext) { |
| Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 4706 | if (SS.isEmpty() || !CodeCompleter) |
| Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4707 | return; |
| Alex Lorenz | 8a7a4cf | 2017-06-15 21:40:54 +0000 | [diff] [blame] | 4708 | |
| Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 4709 | // We want to keep the scope specifier even if it's invalid (e.g. the scope |
| 4710 | // "a::b::" is not corresponding to any context/namespace in the AST), since |
| 4711 | // it can be useful for global code completion which have information about |
| 4712 | // contexts/symbols that are not in the AST. |
| 4713 | if (SS.isInvalid()) { |
| 4714 | CodeCompletionContext CC(CodeCompletionContext::CCC_Name); |
| 4715 | CC.setCXXScopeSpecifier(SS); |
| 4716 | HandleCodeCompleteResults(this, CodeCompleter, CC, nullptr, 0); |
| 4717 | return; |
| 4718 | } |
| Alex Lorenz | 8a7a4cf | 2017-06-15 21:40:54 +0000 | [diff] [blame] | 4719 | // Always pretend to enter a context to ensure that a dependent type |
| 4720 | // resolves to a dependent record. |
| 4721 | DeclContext *Ctx = computeDeclContext(SS, /*EnteringContext=*/true); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4722 | if (!Ctx) |
| 4723 | return; |
| Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 4724 | |
| 4725 | // Try to instantiate any non-dependent declaration contexts before |
| 4726 | // we look in them. |
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4727 | if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx)) |
| Douglas Gregor | 800f2f0 | 2009-12-11 18:28:39 +0000 | [diff] [blame] | 4728 | return; |
| 4729 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4730 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4731 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4732 | CodeCompletionContext::CCC_Name); |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4733 | Results.EnterNewScope(); |
| Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 4734 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4735 | // The "template" keyword can follow "::" in the grammar, but only |
| 4736 | // put it into the grammar if the nested-name-specifier is dependent. |
| Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4737 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4738 | if (!Results.empty() && NNS->isDependent()) |
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4739 | Results.AddResult("template"); |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4740 | |
| 4741 | // Add calls to overridden virtual functions, if there are any. |
| 4742 | // |
| 4743 | // FIXME: This isn't wonderful, because we don't know whether we're actually |
| 4744 | // in a context that permits expressions. This is a general issue with |
| 4745 | // qualified-id completions. |
| 4746 | if (!EnteringContext) |
| 4747 | MaybeAddOverrideCalls(*this, Ctx, Results); |
| Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 4748 | Results.ExitScope(); |
| 4749 | |
| Eric Liu | fead6ae | 2017-12-13 10:26:49 +0000 | [diff] [blame] | 4750 | if (CodeCompleter->includeNamespaceLevelDecls() || |
| 4751 | (!Ctx->isNamespace() && !Ctx->isTranslationUnit())) { |
| 4752 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 4753 | LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer, |
| 4754 | /*IncludeGlobalScope=*/true, |
| Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4755 | /*IncludeDependentBases=*/true, |
| 4756 | CodeCompleter->loadExternal()); |
| Eric Liu | fead6ae | 2017-12-13 10:26:49 +0000 | [diff] [blame] | 4757 | } |
| Douglas Gregor | ac322ec | 2010-08-27 21:18:54 +0000 | [diff] [blame] | 4758 | |
| Eric Liu | 06d3402 | 2017-12-12 11:35:46 +0000 | [diff] [blame] | 4759 | auto CC = Results.getCompletionContext(); |
| 4760 | CC.setCXXScopeSpecifier(SS); |
| 4761 | |
| 4762 | HandleCodeCompleteResults(this, CodeCompleter, CC, Results.data(), |
| 4763 | Results.size()); |
| Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 4764 | } |
| Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4765 | |
| 4766 | void Sema::CodeCompleteUsing(Scope *S) { |
| 4767 | if (!CodeCompleter) |
| 4768 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4769 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4770 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4771 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4772 | CodeCompletionContext::CCC_PotentiallyQualifiedName, |
| 4773 | &ResultBuilder::IsNestedNameSpecifier); |
| Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4774 | Results.EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4775 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4776 | // If we aren't in class scope, we could see the "namespace" keyword. |
| 4777 | if (!S->isClassScope()) |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4778 | Results.AddResult(CodeCompletionResult("namespace")); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4779 | |
| 4780 | // After "using", we can see anything that would start a |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4781 | // nested-name-specifier. |
| Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4782 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4783 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4784 | CodeCompleter->includeGlobals(), |
| 4785 | CodeCompleter->loadExternal()); |
| Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4786 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 4787 | |
| 4788 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4789 | Results.data(), Results.size()); |
| Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
| 4792 | void Sema::CodeCompleteUsingDirective(Scope *S) { |
| 4793 | if (!CodeCompleter) |
| 4794 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4795 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4796 | // After "using namespace", we expect to see a namespace name or namespace |
| 4797 | // alias. |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4798 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4799 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4800 | CodeCompletionContext::CCC_Namespace, |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4801 | &ResultBuilder::IsNamespaceOrAlias); |
| Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4802 | Results.EnterNewScope(); |
| Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4803 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4804 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4805 | CodeCompleter->includeGlobals(), |
| 4806 | CodeCompleter->loadExternal()); |
| Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4807 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 4808 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4809 | Results.data(), Results.size()); |
| Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { |
| 4813 | if (!CodeCompleter) |
| 4814 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4815 | |
| Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 4816 | DeclContext *Ctx = S->getEntity(); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4817 | if (!S->getParent()) |
| 4818 | Ctx = Context.getTranslationUnitDecl(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4819 | |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4820 | bool SuppressedGlobalResults |
| 4821 | = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4822 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4823 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4824 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4825 | SuppressedGlobalResults |
| 4826 | ? CodeCompletionContext::CCC_Namespace |
| 4827 | : CodeCompletionContext::CCC_Other, |
| 4828 | &ResultBuilder::IsNamespace); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4829 | |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4830 | if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) { |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4831 | // We only want to see those namespaces that have already been defined |
| 4832 | // within this scope, because its likely that the user is creating an |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4833 | // extended namespace declaration. Keep track of the most recent |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4834 | // definition of each namespace. |
| 4835 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4836 | for (DeclContext::specific_decl_iterator<NamespaceDecl> |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4837 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); |
| 4838 | NS != NSEnd; ++NS) |
| David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4839 | OrigToLatest[NS->getOriginalNamespace()] = *NS; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4840 | |
| 4841 | // Add the most recent definition (or extended definition) of each |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4842 | // namespace to the list of results. |
| Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4843 | Results.EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4844 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator |
| Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 4845 | NS = OrigToLatest.begin(), |
| 4846 | NSEnd = OrigToLatest.end(); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4847 | NS != NSEnd; ++NS) |
| Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 4848 | Results.AddResult(CodeCompletionResult( |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4849 | NS->second, Results.getBasePriority(NS->second), |
| 4850 | nullptr), |
| 4851 | CurContext, nullptr, false); |
| Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4852 | Results.ExitScope(); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4853 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4854 | |
| 4855 | HandleCodeCompleteResults(this, CodeCompleter, |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4856 | Results.getCompletionContext(), |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4857 | Results.data(),Results.size()); |
| Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4858 | } |
| 4859 | |
| 4860 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { |
| 4861 | if (!CodeCompleter) |
| 4862 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4863 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4864 | // After "namespace", we expect to see a namespace or alias. |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4865 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4866 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4867 | CodeCompletionContext::CCC_Namespace, |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4868 | &ResultBuilder::IsNamespaceOrAlias); |
| Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4869 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4870 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4871 | CodeCompleter->includeGlobals(), |
| 4872 | CodeCompleter->loadExternal()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4873 | HandleCodeCompleteResults(this, CodeCompleter, |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4874 | Results.getCompletionContext(), |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 4875 | Results.data(),Results.size()); |
| Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4876 | } |
| 4877 | |
| Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 4878 | void Sema::CodeCompleteOperatorName(Scope *S) { |
| 4879 | if (!CodeCompleter) |
| 4880 | return; |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4881 | |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 4882 | typedef CodeCompletionResult Result; |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4883 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4884 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4885 | CodeCompletionContext::CCC_Type, |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4886 | &ResultBuilder::IsType); |
| Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4887 | Results.EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4888 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4889 | // Add the names of overloadable operators. |
| 4890 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 4891 | if (std::strcmp(Spelling, "?")) \ |
| Douglas Gregor | 78a2101 | 2010-01-14 16:01:26 +0000 | [diff] [blame] | 4892 | Results.AddResult(Result(Spelling)); |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4893 | #include "clang/Basic/OperatorKinds.def" |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4894 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4895 | // Add any type names visible from the current scope |
| Douglas Gregor | 6ae4c52 | 2010-01-14 03:21:49 +0000 | [diff] [blame] | 4896 | Results.allowNestedNameSpecifiers(); |
| Douglas Gregor | a6e2edc | 2010-01-14 03:27:13 +0000 | [diff] [blame] | 4897 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 4898 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 4899 | CodeCompleter->includeGlobals(), |
| 4900 | CodeCompleter->loadExternal()); |
| 4901 | |
| Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 4902 | // Add any type specifiers |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4903 | AddTypeSpecifierResults(getLangOpts(), Results); |
| Douglas Gregor | 64b12b5 | 2009-09-22 23:31:26 +0000 | [diff] [blame] | 4904 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 4905 | |
| 4906 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 4907 | Results.data(), Results.size()); |
| Douglas Gregor | c811ede | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 4908 | } |
| Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 4909 | |
| Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4910 | void Sema::CodeCompleteConstructorInitializer( |
| 4911 | Decl *ConstructorD, |
| 4912 | ArrayRef <CXXCtorInitializer *> Initializers) { |
| Benjamin Kramer | a4f8df0 | 2015-07-09 15:31:10 +0000 | [diff] [blame] | 4913 | if (!ConstructorD) |
| 4914 | return; |
| 4915 | |
| 4916 | AdjustDeclIfTemplate(ConstructorD); |
| 4917 | |
| 4918 | CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ConstructorD); |
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4919 | if (!Constructor) |
| 4920 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4921 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4922 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4923 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 4924 | CodeCompletionContext::CCC_PotentiallyQualifiedName); |
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4925 | Results.EnterNewScope(); |
| 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 | // Fill in any already-initialized fields or base classes. |
| 4928 | llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields; |
| 4929 | llvm::SmallPtrSet<CanQualType, 4> InitializedBases; |
| Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4930 | for (unsigned I = 0, E = Initializers.size(); I != E; ++I) { |
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4931 | if (Initializers[I]->isBaseInitializer()) |
| 4932 | InitializedBases.insert( |
| 4933 | Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0))); |
| 4934 | else |
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4935 | InitializedFields.insert(cast<FieldDecl>( |
| 4936 | Initializers[I]->getAnyMember())); |
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4937 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4938 | |
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4939 | // Add completions for base classes. |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 4940 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 4941 | Results.getCodeCompletionTUInfo()); |
| Benjamin Kramer | a4f8df0 | 2015-07-09 15:31:10 +0000 | [diff] [blame] | 4942 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
| Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4943 | bool SawLastInitializer = Initializers.empty(); |
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4944 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4945 | for (const auto &Base : ClassDecl->bases()) { |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4946 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) |
| 4947 | .second) { |
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4948 | SawLastInitializer |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4949 | = !Initializers.empty() && |
| Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4950 | Initializers.back()->isBaseInitializer() && |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4951 | Context.hasSameUnqualifiedType(Base.getType(), |
| Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4952 | QualType(Initializers.back()->getBaseClass(), 0)); |
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4953 | continue; |
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4954 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4955 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4956 | Builder.AddTypedTextChunk( |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4957 | Results.getAllocator().CopyString( |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4958 | Base.getType().getAsString(Policy))); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4959 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4960 | Builder.AddPlaceholderChunk("args"); |
| 4961 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4962 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4963 | SawLastInitializer? CCP_NextInitializer |
| 4964 | : CCP_MemberDeclaration)); |
| 4965 | SawLastInitializer = false; |
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4966 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4967 | |
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4968 | // Add completions for virtual base classes. |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4969 | for (const auto &Base : ClassDecl->vbases()) { |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4970 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) |
| 4971 | .second) { |
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4972 | SawLastInitializer |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4973 | = !Initializers.empty() && |
| Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4974 | Initializers.back()->isBaseInitializer() && |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4975 | Context.hasSameUnqualifiedType(Base.getType(), |
| Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4976 | QualType(Initializers.back()->getBaseClass(), 0)); |
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4977 | continue; |
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4978 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4979 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4980 | Builder.AddTypedTextChunk( |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 4981 | Builder.getAllocator().CopyString( |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4982 | Base.getType().getAsString(Policy))); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 4983 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 4984 | Builder.AddPlaceholderChunk("args"); |
| 4985 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4986 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4987 | SawLastInitializer? CCP_NextInitializer |
| 4988 | : CCP_MemberDeclaration)); |
| 4989 | SawLastInitializer = false; |
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4990 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4991 | |
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 4992 | // Add completions for members. |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4993 | for (auto *Field : ClassDecl->fields()) { |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 4994 | if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl())) |
| 4995 | .second) { |
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 4996 | SawLastInitializer |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 4997 | = !Initializers.empty() && |
| Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 4998 | Initializers.back()->isAnyMemberInitializer() && |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4999 | Initializers.back()->getAnyMember() == Field; |
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5000 | continue; |
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5001 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5002 | |
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5003 | if (!Field->getDeclName()) |
| 5004 | continue; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5005 | |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5006 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5007 | Field->getIdentifier()->getName())); |
| 5008 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5009 | Builder.AddPlaceholderChunk("args"); |
| 5010 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5011 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5012 | SawLastInitializer? CCP_NextInitializer |
| Douglas Gregor | f3af311 | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 5013 | : CCP_MemberDeclaration, |
| Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 5014 | CXCursor_MemberRef, |
| 5015 | CXAvailability_Available, |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5016 | Field)); |
| Douglas Gregor | 99129ef | 2010-08-29 19:27:27 +0000 | [diff] [blame] | 5017 | SawLastInitializer = false; |
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5018 | } |
| 5019 | Results.ExitScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5020 | |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 5021 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 5022 | Results.data(), Results.size()); |
| 5023 | } |
| 5024 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5025 | /// Determine whether this scope denotes a namespace. |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5026 | static bool isNamespaceScope(Scope *S) { |
| Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5027 | DeclContext *DC = S->getEntity(); |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5028 | if (!DC) |
| 5029 | return false; |
| 5030 | |
| 5031 | return DC->isFileContext(); |
| 5032 | } |
| 5033 | |
| 5034 | void Sema::CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro, |
| 5035 | bool AfterAmpersand) { |
| 5036 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5037 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5038 | CodeCompletionContext::CCC_Other); |
| 5039 | Results.EnterNewScope(); |
| 5040 | |
| 5041 | // Note what has already been captured. |
| 5042 | llvm::SmallPtrSet<IdentifierInfo *, 4> Known; |
| 5043 | bool IncludedThis = false; |
| Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 5044 | for (const auto &C : Intro.Captures) { |
| 5045 | if (C.Kind == LCK_This) { |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5046 | IncludedThis = true; |
| 5047 | continue; |
| 5048 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5049 | |
| Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 5050 | Known.insert(C.Id); |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5051 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5052 | |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5053 | // Look for other capturable variables. |
| 5054 | for (; S && !isNamespaceScope(S); S = S->getParent()) { |
| Aaron Ballman | 35c5495 | 2014-03-17 16:55:25 +0000 | [diff] [blame] | 5055 | for (const auto *D : S->decls()) { |
| 5056 | const auto *Var = dyn_cast<VarDecl>(D); |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5057 | if (!Var || |
| 5058 | !Var->hasLocalStorage() || |
| 5059 | Var->hasAttr<BlocksAttr>()) |
| 5060 | continue; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5061 | |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 5062 | if (Known.insert(Var->getIdentifier()).second) |
| Douglas Gregor | 0a0e2b3 | 2013-01-31 04:52:16 +0000 | [diff] [blame] | 5063 | Results.AddResult(CodeCompletionResult(Var, CCP_LocalDeclaration), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5064 | CurContext, nullptr, false); |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5065 | } |
| 5066 | } |
| 5067 | |
| 5068 | // Add 'this', if it would be valid. |
| 5069 | if (!IncludedThis && !AfterAmpersand && Intro.Default != LCD_ByCopy) |
| 5070 | addThisCompletion(*this, Results); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5071 | |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5072 | Results.ExitScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5073 | |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5074 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5075 | Results.data(), Results.size()); |
| 5076 | } |
| 5077 | |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5078 | /// Macro that optionally prepends an "@" to the string literal passed in via |
| 5079 | /// Keyword, depending on whether NeedAt is true or false. |
| 5080 | #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) ((NeedAt)? "@" Keyword : Keyword) |
| 5081 | |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5082 | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5083 | ResultBuilder &Results, |
| 5084 | bool NeedAt) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5085 | typedef CodeCompletionResult Result; |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5086 | // Since we have an implementation, we can end it. |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5087 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end"))); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5088 | |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5089 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5090 | Results.getCodeCompletionTUInfo()); |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5091 | if (LangOpts.ObjC2) { |
| 5092 | // @dynamic |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5093 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"dynamic")); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5094 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5095 | Builder.AddPlaceholderChunk("property"); |
| 5096 | Results.AddResult(Result(Builder.TakeString())); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5097 | |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5098 | // @synthesize |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5099 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synthesize")); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5100 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5101 | Builder.AddPlaceholderChunk("property"); |
| 5102 | Results.AddResult(Result(Builder.TakeString())); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5103 | } |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5104 | } |
| 5105 | |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5106 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5107 | ResultBuilder &Results, |
| 5108 | bool NeedAt) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5109 | typedef CodeCompletionResult Result; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5110 | |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5111 | // Since we have an interface or protocol, we can end it. |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5112 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end"))); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5113 | |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5114 | if (LangOpts.ObjC2) { |
| 5115 | // @property |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5116 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"property"))); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5117 | |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5118 | // @required |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5119 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"required"))); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5120 | |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5121 | // @optional |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5122 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"optional"))); |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5123 | } |
| 5124 | } |
| 5125 | |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5126 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5127 | typedef CodeCompletionResult Result; |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5128 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5129 | Results.getCodeCompletionTUInfo()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5130 | |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5131 | // @class name ; |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5132 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"class")); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5133 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5134 | Builder.AddPlaceholderChunk("name"); |
| 5135 | Results.AddResult(Result(Builder.TakeString())); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5136 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5137 | if (Results.includeCodePatterns()) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5138 | // @interface name |
| 5139 | // FIXME: Could introduce the whole pattern, including superclasses and |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5140 | // such. |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5141 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"interface")); |
| 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())); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5145 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5146 | // @protocol name |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5147 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol")); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5148 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5149 | Builder.AddPlaceholderChunk("protocol"); |
| 5150 | Results.AddResult(Result(Builder.TakeString())); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5151 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5152 | // @implementation name |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5153 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"implementation")); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5154 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5155 | Builder.AddPlaceholderChunk("class"); |
| 5156 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5157 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5158 | |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5159 | // @compatibility_alias name |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5160 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"compatibility_alias")); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5161 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5162 | Builder.AddPlaceholderChunk("alias"); |
| 5163 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5164 | Builder.AddPlaceholderChunk("class"); |
| 5165 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | 61e3681 | 2013-03-07 23:26:24 +0000 | [diff] [blame] | 5166 | |
| 5167 | if (Results.getSema().getLangOpts().Modules) { |
| 5168 | // @import name |
| 5169 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "import")); |
| 5170 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5171 | Builder.AddPlaceholderChunk("module"); |
| 5172 | Results.AddResult(Result(Builder.TakeString())); |
| 5173 | } |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5174 | } |
| 5175 | |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5176 | void Sema::CodeCompleteObjCAtDirective(Scope *S) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5177 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5178 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5179 | CodeCompletionContext::CCC_Other); |
| Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 5180 | Results.EnterNewScope(); |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5181 | if (isa<ObjCImplDecl>(CurContext)) |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5182 | AddObjCImplementationResults(getLangOpts(), Results, false); |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5183 | else if (CurContext->isObjCContainer()) |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5184 | AddObjCInterfaceResults(getLangOpts(), Results, false); |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5185 | else |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5186 | AddObjCTopLevelResults(Results, false); |
| Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 5187 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5188 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5189 | Results.data(), Results.size()); |
| Douglas Gregor | f48706c | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 5190 | } |
| 5191 | |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5192 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5193 | typedef CodeCompletionResult Result; |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5194 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5195 | Results.getCodeCompletionTUInfo()); |
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5196 | |
| 5197 | // @encode ( type-name ) |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5198 | const char *EncodeType = "char[]"; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5199 | if (Results.getSema().getLangOpts().CPlusPlus || |
| 5200 | Results.getSema().getLangOpts().ConstStrings) |
| Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5201 | EncodeType = "const char[]"; |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5202 | Builder.AddResultTypeChunk(EncodeType); |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5203 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"encode")); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5204 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5205 | Builder.AddPlaceholderChunk("type-name"); |
| 5206 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5207 | Results.AddResult(Result(Builder.TakeString())); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5208 | |
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5209 | // @protocol ( protocol-name ) |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5210 | Builder.AddResultTypeChunk("Protocol *"); |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5211 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol")); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5212 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5213 | Builder.AddPlaceholderChunk("protocol-name"); |
| 5214 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5215 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5216 | |
| 5217 | // @selector ( selector ) |
| Douglas Gregor | e5c79d5 | 2011-10-18 21:20:17 +0000 | [diff] [blame] | 5218 | Builder.AddResultTypeChunk("SEL"); |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5219 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"selector")); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5220 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5221 | Builder.AddPlaceholderChunk("selector"); |
| 5222 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5223 | Results.AddResult(Result(Builder.TakeString())); |
| Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5224 | |
| 5225 | // @"string" |
| 5226 | Builder.AddResultTypeChunk("NSString *"); |
| 5227 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"\"")); |
| 5228 | Builder.AddPlaceholderChunk("string"); |
| 5229 | Builder.AddTextChunk("\""); |
| 5230 | Results.AddResult(Result(Builder.TakeString())); |
| 5231 | |
| Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 5232 | // @[objects, ...] |
| Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5233 | Builder.AddResultTypeChunk("NSArray *"); |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5234 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"[")); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5235 | Builder.AddPlaceholderChunk("objects, ..."); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5236 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
| 5237 | Results.AddResult(Result(Builder.TakeString())); |
| 5238 | |
| Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 5239 | // @{key : object, ...} |
| Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5240 | Builder.AddResultTypeChunk("NSDictionary *"); |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5241 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"{")); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5242 | Builder.AddPlaceholderChunk("key"); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5243 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 5244 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5245 | Builder.AddPlaceholderChunk("object, ..."); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5246 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5247 | Results.AddResult(Result(Builder.TakeString())); |
| Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5248 | |
| Douglas Gregor | 951de30 | 2012-07-17 23:24:47 +0000 | [diff] [blame] | 5249 | // @(expression) |
| Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5250 | Builder.AddResultTypeChunk("id"); |
| 5251 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "(")); |
| Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5252 | Builder.AddPlaceholderChunk("expression"); |
| Jordan Rose | 9da0585 | 2012-06-15 18:19:56 +0000 | [diff] [blame] | 5253 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5254 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5257 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5258 | typedef CodeCompletionResult Result; |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5259 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5260 | Results.getCodeCompletionTUInfo()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5261 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5262 | if (Results.includeCodePatterns()) { |
| 5263 | // @try { statements } @catch ( declaration ) { statements } @finally |
| 5264 | // { statements } |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5265 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"try")); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5266 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5267 | Builder.AddPlaceholderChunk("statements"); |
| 5268 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5269 | Builder.AddTextChunk("@catch"); |
| 5270 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5271 | Builder.AddPlaceholderChunk("parameter"); |
| 5272 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5273 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5274 | Builder.AddPlaceholderChunk("statements"); |
| 5275 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5276 | Builder.AddTextChunk("@finally"); |
| 5277 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5278 | Builder.AddPlaceholderChunk("statements"); |
| 5279 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5280 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5281 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5282 | |
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5283 | // @throw |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5284 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"throw")); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5285 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5286 | Builder.AddPlaceholderChunk("expression"); |
| 5287 | Results.AddResult(Result(Builder.TakeString())); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5288 | |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5289 | if (Results.includeCodePatterns()) { |
| 5290 | // @synchronized ( expression ) { statements } |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5291 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synchronized")); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5292 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 5293 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 5294 | Builder.AddPlaceholderChunk("expression"); |
| 5295 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 5296 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 5297 | Builder.AddPlaceholderChunk("statements"); |
| 5298 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| 5299 | Results.AddResult(Result(Builder.TakeString())); |
| Douglas Gregor | f4c3334 | 2010-05-28 00:22:41 +0000 | [diff] [blame] | 5300 | } |
| Douglas Gregor | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5301 | } |
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5302 | |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5303 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
| Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5304 | ResultBuilder &Results, |
| 5305 | bool NeedAt) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5306 | typedef CodeCompletionResult Result; |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5307 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"private"))); |
| 5308 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"protected"))); |
| 5309 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"public"))); |
| Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5310 | if (LangOpts.ObjC2) |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5311 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"package"))); |
| Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5312 | } |
| 5313 | |
| 5314 | void Sema::CodeCompleteObjCAtVisibility(Scope *S) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5315 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5316 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5317 | CodeCompletionContext::CCC_Other); |
| Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5318 | Results.EnterNewScope(); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5319 | AddObjCVisibilityResults(getLangOpts(), Results, false); |
| Douglas Gregor | 48d4625 | 2010-01-13 21:54:15 +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 | 48d4625 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 5323 | } |
| 5324 | |
| 5325 | void Sema::CodeCompleteObjCAtStatement(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 | f193416 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 5329 | Results.EnterNewScope(); |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5330 | AddObjCStatementResults(Results, false); |
| 5331 | AddObjCExpressionResults(Results, false); |
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5332 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5333 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5334 | Results.data(), Results.size()); |
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5335 | } |
| 5336 | |
| 5337 | void Sema::CodeCompleteObjCAtExpression(Scope *S) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5338 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5339 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5340 | CodeCompletionContext::CCC_Other); |
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5341 | Results.EnterNewScope(); |
| Douglas Gregor | f98e6a2 | 2010-01-13 23:51:12 +0000 | [diff] [blame] | 5342 | AddObjCExpressionResults(Results, false); |
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5343 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5344 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5345 | Results.data(), Results.size()); |
| Douglas Gregor | bc7c5e4 | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 5346 | } |
| 5347 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5348 | /// Determine whether the addition of the given flag to an Objective-C |
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5349 | /// property's attributes will cause a conflict. |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5350 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { |
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5351 | // Check if we've already added this flag. |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5352 | if (Attributes & NewFlag) |
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5353 | return true; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5354 | |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5355 | Attributes |= NewFlag; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5356 | |
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5357 | // Check for collisions with "readonly". |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5358 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 5359 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) |
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5360 | return true; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5361 | |
| Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5362 | // Check for more than one of { assign, copy, retain, strong, weak }. |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5363 | unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5364 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5365 | ObjCDeclSpec::DQ_PR_copy | |
| Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5366 | ObjCDeclSpec::DQ_PR_retain | |
| 5367 | ObjCDeclSpec::DQ_PR_strong | |
| 5368 | ObjCDeclSpec::DQ_PR_weak); |
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5369 | if (AssignCopyRetMask && |
| 5370 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5371 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained && |
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5372 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5373 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain && |
| Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5374 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong && |
| 5375 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_weak) |
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5376 | return true; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5377 | |
| Douglas Gregor | e6078da | 2009-11-19 00:14:45 +0000 | [diff] [blame] | 5378 | return false; |
| 5379 | } |
| 5380 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5381 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { |
| Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5382 | if (!CodeCompleter) |
| 5383 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5384 | |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5385 | unsigned Attributes = ODS.getPropertyAttributes(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5386 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5387 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5388 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5389 | CodeCompletionContext::CCC_Other); |
| Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5390 | Results.EnterNewScope(); |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5391 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5392 | Results.AddResult(CodeCompletionResult("readonly")); |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5393 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5394 | Results.AddResult(CodeCompletionResult("assign")); |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5395 | if (!ObjCPropertyFlagConflicts(Attributes, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5396 | ObjCDeclSpec::DQ_PR_unsafe_unretained)) |
| 5397 | Results.AddResult(CodeCompletionResult("unsafe_unretained")); |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5398 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5399 | Results.AddResult(CodeCompletionResult("readwrite")); |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5400 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5401 | Results.AddResult(CodeCompletionResult("retain")); |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5402 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong)) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5403 | Results.AddResult(CodeCompletionResult("strong")); |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5404 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5405 | Results.AddResult(CodeCompletionResult("copy")); |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5406 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5407 | Results.AddResult(CodeCompletionResult("nonatomic")); |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5408 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic)) |
| Fariborz Jahanian | 1c2d29e | 2011-06-11 17:14:27 +0000 | [diff] [blame] | 5409 | Results.AddResult(CodeCompletionResult("atomic")); |
| Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5410 | |
| 5411 | // 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] | 5412 | if (getLangOpts().ObjCWeak || getLangOpts().getGC() != LangOptions::NonGC) |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5413 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_weak)) |
| Jordan Rose | 53cb2f3 | 2012-08-20 20:01:13 +0000 | [diff] [blame] | 5414 | Results.AddResult(CodeCompletionResult("weak")); |
| 5415 | |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5416 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5417 | CodeCompletionBuilder Setter(Results.getAllocator(), |
| 5418 | Results.getCodeCompletionTUInfo()); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5419 | Setter.AddTypedTextChunk("setter"); |
| Argyrios Kyrtzidis | 7bbb881 | 2014-02-20 07:55:15 +0000 | [diff] [blame] | 5420 | Setter.AddTextChunk("="); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5421 | Setter.AddPlaceholderChunk("method"); |
| 5422 | Results.AddResult(CodeCompletionResult(Setter.TakeString())); |
| Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 5423 | } |
| Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 5424 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5425 | CodeCompletionBuilder Getter(Results.getAllocator(), |
| 5426 | Results.getCodeCompletionTUInfo()); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5427 | Getter.AddTypedTextChunk("getter"); |
| Argyrios Kyrtzidis | 7bbb881 | 2014-02-20 07:55:15 +0000 | [diff] [blame] | 5428 | Getter.AddTextChunk("="); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5429 | Getter.AddPlaceholderChunk("method"); |
| 5430 | Results.AddResult(CodeCompletionResult(Getter.TakeString())); |
| Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 5431 | } |
| Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 5432 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nullability)) { |
| 5433 | Results.AddResult(CodeCompletionResult("nonnull")); |
| 5434 | Results.AddResult(CodeCompletionResult("nullable")); |
| 5435 | Results.AddResult(CodeCompletionResult("null_unspecified")); |
| 5436 | Results.AddResult(CodeCompletionResult("null_resettable")); |
| 5437 | } |
| Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5438 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5439 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5440 | Results.data(), Results.size()); |
| Steve Naroff | 936354c | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 5441 | } |
| Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 5442 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5443 | /// Describes the kind of Objective-C method that we want to find |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5444 | /// via code completion. |
| 5445 | enum ObjCMethodKind { |
| Dmitri Gribenko | 4280e5c | 2012-06-08 23:13:42 +0000 | [diff] [blame] | 5446 | MK_Any, ///< Any kind of method, provided it means other specified criteria. |
| 5447 | MK_ZeroArgSelector, ///< Zero-argument (unary) selector. |
| 5448 | MK_OneArgSelector ///< One-argument selector. |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5449 | }; |
| 5450 | |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5451 | static bool isAcceptableObjCSelector(Selector Sel, |
| 5452 | ObjCMethodKind WantKind, |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5453 | ArrayRef<IdentifierInfo *> SelIdents, |
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5454 | bool AllowSameLength = true) { |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5455 | unsigned NumSelIdents = SelIdents.size(); |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5456 | if (NumSelIdents > Sel.getNumArgs()) |
| 5457 | return false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5458 | |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5459 | switch (WantKind) { |
| 5460 | case MK_Any: break; |
| 5461 | case MK_ZeroArgSelector: return Sel.isUnarySelector(); |
| 5462 | case MK_OneArgSelector: return Sel.getNumArgs() == 1; |
| 5463 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5464 | |
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5465 | if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs()) |
| 5466 | return false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5467 | |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5468 | for (unsigned I = 0; I != NumSelIdents; ++I) |
| 5469 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) |
| 5470 | return false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5471 | |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5472 | return true; |
| 5473 | } |
| 5474 | |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5475 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, |
| 5476 | ObjCMethodKind WantKind, |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5477 | ArrayRef<IdentifierInfo *> SelIdents, |
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5478 | bool AllowSameLength = true) { |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 5479 | return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents, |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5480 | AllowSameLength); |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5481 | } |
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5482 | |
| 5483 | namespace { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5484 | /// A set of selectors, which is used to avoid introducing multiple |
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5485 | /// completions with the same selector into the result set. |
| 5486 | typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet; |
| 5487 | } |
| 5488 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5489 | /// Add all of the Objective-C methods in the given Objective-C |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5490 | /// container to the set of results. |
| 5491 | /// |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5492 | /// The container will be a class, protocol, category, or implementation of |
| 5493 | /// any of the above. This mether will recurse to include methods from |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5494 | /// the superclasses of classes along with their categories, protocols, and |
| 5495 | /// implementations. |
| 5496 | /// |
| 5497 | /// \param Container the container in which we'll look to find methods. |
| 5498 | /// |
| James Dennett | 596e475 | 2012-06-14 03:11:41 +0000 | [diff] [blame] | 5499 | /// \param WantInstanceMethods Whether to add instance methods (only); if |
| 5500 | /// false, this routine will add factory methods (only). |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5501 | /// |
| 5502 | /// \param CurContext the context in which we're performing the lookup that |
| 5503 | /// finds methods. |
| 5504 | /// |
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5505 | /// \param AllowSameLength Whether we allow a method to be added to the list |
| 5506 | /// when it has the same number of parameters as we have selector identifiers. |
| 5507 | /// |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5508 | /// \param Results the structure into which we'll add results. |
| Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5509 | static void AddObjCMethods(ObjCContainerDecl *Container, |
| 5510 | bool WantInstanceMethods, ObjCMethodKind WantKind, |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5511 | ArrayRef<IdentifierInfo *> SelIdents, |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5512 | DeclContext *CurContext, |
| Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5513 | VisitedSelectorSet &Selectors, bool AllowSameLength, |
| 5514 | ResultBuilder &Results, bool InOriginalClass = true, |
| 5515 | bool IsRootClass = false) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5516 | typedef CodeCompletionResult Result; |
| Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 5517 | Container = getContainerDef(Container); |
| Douglas Gregor | 41778c3 | 2013-01-30 06:58:39 +0000 | [diff] [blame] | 5518 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); |
| Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5519 | IsRootClass = IsRootClass || (IFace && !IFace->getSuperClass()); |
| Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 5520 | for (auto *M : Container->methods()) { |
| Douglas Gregor | 41778c3 | 2013-01-30 06:58:39 +0000 | [diff] [blame] | 5521 | // The instance methods on the root class can be messaged via the |
| 5522 | // metaclass. |
| 5523 | if (M->isInstanceMethod() == WantInstanceMethods || |
| Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5524 | (IsRootClass && !WantInstanceMethods)) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5525 | // Check whether the selector identifiers we've been given are a |
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5526 | // subset of the identifiers for this particular method. |
| Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 5527 | if (!isAcceptableObjCMethod(M, WantKind, SelIdents, AllowSameLength)) |
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5528 | continue; |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5529 | |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 5530 | if (!Selectors.insert(M->getSelector()).second) |
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5531 | continue; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5532 | |
| 5533 | Result R = Result(M, Results.getBasePriority(M), nullptr); |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5534 | R.StartParameter = SelIdents.size(); |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5535 | R.AllParametersAreInformative = (WantKind != MK_Any); |
| Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 5536 | if (!InOriginalClass) |
| 5537 | R.Priority += CCD_InBaseClass; |
| Douglas Gregor | 1b605f7 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 5538 | Results.MaybeAddResult(R, CurContext); |
| 5539 | } |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5540 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5541 | |
| Douglas Gregor | f37c949 | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 5542 | // Visit the protocols of protocols. |
| 5543 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5544 | if (Protocol->hasDefinition()) { |
| 5545 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 5546 | = Protocol->getReferencedProtocols(); |
| 5547 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5548 | E = Protocols.end(); |
| Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5549 | I != E; ++I) |
| Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5550 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5551 | Selectors, AllowSameLength, Results, false, IsRootClass); |
| Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 5552 | } |
| Douglas Gregor | f37c949 | 2010-09-16 15:34:59 +0000 | [diff] [blame] | 5553 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5554 | |
| Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 5555 | if (!IFace || !IFace->hasDefinition()) |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5556 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5557 | |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5558 | // Add methods in protocols. |
| Aaron Ballman | a49c506 | 2014-03-13 20:29:09 +0000 | [diff] [blame] | 5559 | for (auto *I : IFace->protocols()) |
| Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5560 | AddObjCMethods(I, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5561 | Selectors, AllowSameLength, Results, false, IsRootClass); |
| 5562 | |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5563 | // Add methods in categories. |
| Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 5564 | for (auto *CatDecl : IFace->known_categories()) { |
| Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5565 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, |
| Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5566 | CurContext, Selectors, AllowSameLength, Results, |
| 5567 | InOriginalClass, IsRootClass); |
| 5568 | |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5569 | // Add a categories protocol methods. |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5570 | const ObjCList<ObjCProtocolDecl> &Protocols |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5571 | = CatDecl->getReferencedProtocols(); |
| 5572 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 5573 | E = Protocols.end(); |
| 5574 | I != E; ++I) |
| Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5575 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5576 | Selectors, AllowSameLength, Results, false, IsRootClass); |
| 5577 | |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5578 | // Add methods in category implementations. |
| 5579 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) |
| Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5580 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5581 | Selectors, AllowSameLength, Results, InOriginalClass, |
| 5582 | IsRootClass); |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5583 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5584 | |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5585 | // Add methods in superclass. |
| Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5586 | // Avoid passing in IsRootClass since root classes won't have super classes. |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5587 | if (IFace->getSuperClass()) |
| Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5588 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, |
| 5589 | SelIdents, CurContext, Selectors, AllowSameLength, Results, |
| 5590 | /*IsRootClass=*/false); |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5591 | |
| 5592 | // Add methods in our implementation, if any. |
| 5593 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
| Alex Lorenz | 638dbc3 | 2017-01-24 14:15:08 +0000 | [diff] [blame] | 5594 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, CurContext, |
| 5595 | Selectors, AllowSameLength, Results, InOriginalClass, |
| 5596 | IsRootClass); |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5597 | } |
| 5598 | |
| 5599 | |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5600 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S) { |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5601 | // Try to find the interface where getters might live. |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5602 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5603 | if (!Class) { |
| 5604 | if (ObjCCategoryDecl *Category |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5605 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5606 | Class = Category->getClassInterface(); |
| 5607 | |
| 5608 | if (!Class) |
| 5609 | return; |
| 5610 | } |
| 5611 | |
| 5612 | // Find all of the potential getters. |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5613 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5614 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5615 | CodeCompletionContext::CCC_Other); |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5616 | Results.EnterNewScope(); |
| 5617 | |
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5618 | VisitedSelectorSet Selectors; |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5619 | AddObjCMethods(Class, true, MK_ZeroArgSelector, None, CurContext, Selectors, |
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5620 | /*AllowSameLength=*/true, Results); |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5621 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5622 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5623 | Results.data(), Results.size()); |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5624 | } |
| 5625 | |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5626 | void Sema::CodeCompleteObjCPropertySetter(Scope *S) { |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5627 | // Try to find the interface where setters might live. |
| 5628 | ObjCInterfaceDecl *Class |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5629 | = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5630 | if (!Class) { |
| 5631 | if (ObjCCategoryDecl *Category |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 5632 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5633 | Class = Category->getClassInterface(); |
| 5634 | |
| 5635 | if (!Class) |
| 5636 | return; |
| 5637 | } |
| 5638 | |
| 5639 | // Find all of the potential getters. |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5640 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5641 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5642 | CodeCompletionContext::CCC_Other); |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5643 | Results.EnterNewScope(); |
| 5644 | |
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 5645 | VisitedSelectorSet Selectors; |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5646 | AddObjCMethods(Class, true, MK_OneArgSelector, None, CurContext, |
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 5647 | Selectors, /*AllowSameLength=*/true, Results); |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 5648 | |
| 5649 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5650 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 5651 | Results.data(), Results.size()); |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 5652 | } |
| 5653 | |
| Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5654 | void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS, |
| 5655 | bool IsParameter) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5656 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5657 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5658 | CodeCompletionContext::CCC_Type); |
| Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5659 | Results.EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5660 | |
| Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5661 | // Add context-sensitive, Objective-C parameter-passing keywords. |
| 5662 | bool AddedInOut = false; |
| 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_In | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 5665 | Results.AddResult("in"); |
| 5666 | Results.AddResult("inout"); |
| 5667 | AddedInOut = true; |
| 5668 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5669 | if ((DS.getObjCDeclQualifier() & |
| Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5670 | (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) { |
| 5671 | Results.AddResult("out"); |
| 5672 | if (!AddedInOut) |
| 5673 | Results.AddResult("inout"); |
| 5674 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5675 | if ((DS.getObjCDeclQualifier() & |
| Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5676 | (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref | |
| 5677 | ObjCDeclSpec::DQ_Oneway)) == 0) { |
| 5678 | Results.AddResult("bycopy"); |
| 5679 | Results.AddResult("byref"); |
| 5680 | Results.AddResult("oneway"); |
| 5681 | } |
| Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 5682 | if ((DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability) == 0) { |
| 5683 | Results.AddResult("nonnull"); |
| 5684 | Results.AddResult("nullable"); |
| 5685 | Results.AddResult("null_unspecified"); |
| 5686 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5687 | |
| 5688 | // 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] | 5689 | // identifier IBAction refers to a macro, provide a completion item for |
| 5690 | // an action, e.g., |
| 5691 | // IBAction)<#selector#>:(id)sender |
| 5692 | if (DS.getObjCDeclQualifier() == 0 && !IsParameter && |
| Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 5693 | PP.isMacroDefined("IBAction")) { |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5694 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5695 | Results.getCodeCompletionTUInfo(), |
| 5696 | CCP_CodePattern, CXAvailability_Available); |
| Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5697 | Builder.AddTypedTextChunk("IBAction"); |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5698 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5699 | Builder.AddPlaceholderChunk("selector"); |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5700 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
| 5701 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5702 | Builder.AddTextChunk("id"); |
| Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 5703 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| Douglas Gregor | f34a6f0 | 2011-02-15 22:19:42 +0000 | [diff] [blame] | 5704 | Builder.AddTextChunk("sender"); |
| 5705 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 5706 | } |
| Douglas Gregor | ed1f597 | 2013-01-30 07:11:43 +0000 | [diff] [blame] | 5707 | |
| 5708 | // If we're completing the return type, provide 'instancetype'. |
| 5709 | if (!IsParameter) { |
| 5710 | Results.AddResult(CodeCompletionResult("instancetype")); |
| 5711 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5712 | |
| Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5713 | // Add various builtin type names and specifiers. |
| 5714 | AddOrdinaryNameResults(PCC_Type, S, *this, Results); |
| 5715 | Results.ExitScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5716 | |
| Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5717 | // Add the various type names |
| 5718 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
| 5719 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 5720 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 5721 | CodeCompleter->includeGlobals(), |
| 5722 | CodeCompleter->loadExternal()); |
| 5723 | |
| Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5724 | if (CodeCompleter->includeMacros()) |
| Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 5725 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); |
| Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5726 | |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 5727 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| Douglas Gregor | 99fa264 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 5728 | Results.data(), Results.size()); |
| 5729 | } |
| 5730 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5731 | /// When we have an expression with type "id", we may assume |
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5732 | /// that it has some more-specific class type based on knowledge of |
| 5733 | /// common uses of Objective-C. This routine returns that class type, |
| 5734 | /// or NULL if no better result could be determined. |
| 5735 | static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) { |
| Douglas Gregor | ed0b69d | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 5736 | ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E); |
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5737 | if (!Msg) |
| 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 | Selector Sel = Msg->getSelector(); |
| 5741 | if (Sel.isNull()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5742 | return nullptr; |
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5743 | |
| 5744 | IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0); |
| 5745 | if (!Id) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5746 | return nullptr; |
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5747 | |
| 5748 | ObjCMethodDecl *Method = Msg->getMethodDecl(); |
| 5749 | if (!Method) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5750 | return nullptr; |
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5751 | |
| 5752 | // Determine the class that we're sending the message to. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5753 | ObjCInterfaceDecl *IFace = nullptr; |
| Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 5754 | switch (Msg->getReceiverKind()) { |
| 5755 | case ObjCMessageExpr::Class: |
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5756 | if (const ObjCObjectType *ObjType |
| 5757 | = Msg->getClassReceiver()->getAs<ObjCObjectType>()) |
| 5758 | IFace = ObjType->getInterface(); |
| Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 5759 | break; |
| 5760 | |
| 5761 | case ObjCMessageExpr::Instance: { |
| 5762 | QualType T = Msg->getInstanceReceiver()->getType(); |
| 5763 | if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) |
| 5764 | IFace = Ptr->getInterfaceDecl(); |
| 5765 | break; |
| 5766 | } |
| 5767 | |
| 5768 | case ObjCMessageExpr::SuperInstance: |
| 5769 | case ObjCMessageExpr::SuperClass: |
| 5770 | break; |
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5771 | } |
| 5772 | |
| 5773 | if (!IFace) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5774 | return nullptr; |
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5775 | |
| 5776 | ObjCInterfaceDecl *Super = IFace->getSuperClass(); |
| 5777 | if (Method->isInstanceMethod()) |
| 5778 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 5779 | .Case("retain", IFace) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5780 | .Case("strong", IFace) |
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5781 | .Case("autorelease", IFace) |
| 5782 | .Case("copy", IFace) |
| 5783 | .Case("copyWithZone", IFace) |
| 5784 | .Case("mutableCopy", IFace) |
| 5785 | .Case("mutableCopyWithZone", IFace) |
| 5786 | .Case("awakeFromCoder", IFace) |
| 5787 | .Case("replacementObjectFromCoder", IFace) |
| 5788 | .Case("class", IFace) |
| 5789 | .Case("classForCoder", IFace) |
| 5790 | .Case("superclass", Super) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5791 | .Default(nullptr); |
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5792 | |
| 5793 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) |
| 5794 | .Case("new", IFace) |
| 5795 | .Case("alloc", IFace) |
| 5796 | .Case("allocWithZone", IFace) |
| 5797 | .Case("class", IFace) |
| 5798 | .Case("superclass", Super) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5799 | .Default(nullptr); |
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 5800 | } |
| 5801 | |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5802 | // 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] | 5803 | // most likely case of forwarding all of our arguments to the superclass |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5804 | // function. |
| 5805 | /// |
| 5806 | /// \param S The semantic analysis object. |
| 5807 | /// |
| Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 5808 | /// \param NeedSuperKeyword Whether we need to prefix this completion with |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5809 | /// the "super" keyword. Otherwise, we just need to provide the arguments. |
| 5810 | /// |
| 5811 | /// \param SelIdents The identifiers in the selector that have already been |
| 5812 | /// provided as arguments for a send to "super". |
| 5813 | /// |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5814 | /// \param Results The set of results to augment. |
| 5815 | /// |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5816 | /// \returns the Objective-C method declaration that would be invoked by |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5817 | /// this "super" completion. If NULL, no completion was added. |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5818 | static ObjCMethodDecl *AddSuperSendCompletion( |
| 5819 | Sema &S, bool NeedSuperKeyword, |
| 5820 | ArrayRef<IdentifierInfo *> SelIdents, |
| 5821 | ResultBuilder &Results) { |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5822 | ObjCMethodDecl *CurMethod = S.getCurMethodDecl(); |
| 5823 | if (!CurMethod) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5824 | return nullptr; |
| 5825 | |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5826 | ObjCInterfaceDecl *Class = CurMethod->getClassInterface(); |
| 5827 | if (!Class) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5828 | return nullptr; |
| 5829 | |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5830 | // Try to find a superclass method with the same selector. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5831 | ObjCMethodDecl *SuperMethod = nullptr; |
| Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5832 | while ((Class = Class->getSuperClass()) && !SuperMethod) { |
| 5833 | // Check in the class |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5834 | SuperMethod = Class->getMethod(CurMethod->getSelector(), |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5835 | CurMethod->isInstanceMethod()); |
| 5836 | |
| Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5837 | // Check in categories or class extensions. |
| 5838 | if (!SuperMethod) { |
| Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 5839 | for (const auto *Cat : Class->known_categories()) { |
| Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5840 | if ((SuperMethod = Cat->getMethod(CurMethod->getSelector(), |
| Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5841 | CurMethod->isInstanceMethod()))) |
| 5842 | break; |
| Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 5843 | } |
| Douglas Gregor | b5f1e46 | 2011-02-16 00:51:18 +0000 | [diff] [blame] | 5844 | } |
| 5845 | } |
| 5846 | |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5847 | if (!SuperMethod) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5848 | return nullptr; |
| 5849 | |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5850 | // Check whether the superclass method has the same signature. |
| 5851 | if (CurMethod->param_size() != SuperMethod->param_size() || |
| 5852 | CurMethod->isVariadic() != SuperMethod->isVariadic()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5853 | return nullptr; |
| 5854 | |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5855 | for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(), |
| 5856 | CurPEnd = CurMethod->param_end(), |
| 5857 | SuperP = SuperMethod->param_begin(); |
| 5858 | CurP != CurPEnd; ++CurP, ++SuperP) { |
| 5859 | // Make sure the parameter types are compatible. |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5860 | if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(), |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5861 | (*SuperP)->getType())) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5862 | return nullptr; |
| 5863 | |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5864 | // Make sure we have a parameter name to forward! |
| 5865 | if (!(*CurP)->getIdentifier()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5866 | return nullptr; |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5867 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5868 | |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5869 | // We have a superclass method. Now, form the send-to-super completion. |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5870 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 5871 | Results.getCodeCompletionTUInfo()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5872 | |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5873 | // Give this completion a return type. |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 5874 | AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod, |
| 5875 | Results.getCompletionContext().getBaseType(), |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 5876 | Builder); |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5877 | |
| 5878 | // If we need the "super" keyword, add it (plus some spacing). |
| 5879 | if (NeedSuperKeyword) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5880 | Builder.AddTypedTextChunk("super"); |
| 5881 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5882 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5883 | |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5884 | Selector Sel = CurMethod->getSelector(); |
| 5885 | if (Sel.isUnarySelector()) { |
| 5886 | if (NeedSuperKeyword) |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5887 | Builder.AddTextChunk(Builder.getAllocator().CopyString( |
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5888 | Sel.getNameForSlot(0))); |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5889 | else |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5890 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5891 | Sel.getNameForSlot(0))); |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5892 | } else { |
| 5893 | ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(); |
| 5894 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) { |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5895 | if (I > SelIdents.size()) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5896 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5897 | |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5898 | if (I < SelIdents.size()) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5899 | Builder.AddInformativeChunk( |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5900 | Builder.getAllocator().CopyString( |
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5901 | Sel.getNameForSlot(I) + ":")); |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5902 | else if (NeedSuperKeyword || I > SelIdents.size()) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5903 | Builder.AddTextChunk( |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5904 | Builder.getAllocator().CopyString( |
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5905 | Sel.getNameForSlot(I) + ":")); |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5906 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5907 | (*CurP)->getIdentifier()->getName())); |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5908 | } else { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5909 | Builder.AddTypedTextChunk( |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5910 | Builder.getAllocator().CopyString( |
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 5911 | Sel.getNameForSlot(I) + ":")); |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 5912 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5913 | (*CurP)->getIdentifier()->getName())); |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5914 | } |
| 5915 | } |
| 5916 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5917 | |
| Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 5918 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), SuperMethod, |
| 5919 | CCP_SuperCompletion)); |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5920 | return SuperMethod; |
| 5921 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5922 | |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5923 | void Sema::CodeCompleteObjCMessageReceiver(Scope *S) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 5924 | typedef CodeCompletionResult Result; |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5925 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 5926 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 5927 | CodeCompletionContext::CCC_ObjCMessageReceiver, |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5928 | getLangOpts().CPlusPlus11 |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5929 | ? &ResultBuilder::IsObjCMessageReceiverOrLambdaCapture |
| 5930 | : &ResultBuilder::IsObjCMessageReceiver); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5931 | |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5932 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
| 5933 | Results.EnterNewScope(); |
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 5934 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
| Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 5935 | CodeCompleter->includeGlobals(), |
| 5936 | CodeCompleter->loadExternal()); |
| 5937 | |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5938 | // If we are in an Objective-C method inside a class that has a superclass, |
| 5939 | // add "super" as an option. |
| 5940 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 5941 | if (ObjCInterfaceDecl *Iface = Method->getClassInterface()) |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5942 | if (Iface->getSuperClass()) { |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5943 | Results.AddResult(Result("super")); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5944 | |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5945 | AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, None, Results); |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 5946 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5947 | |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5948 | if (getLangOpts().CPlusPlus11) |
| Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 5949 | addThisCompletion(*this, Results); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5950 | |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5951 | Results.ExitScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5952 | |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5953 | if (CodeCompleter->includeMacros()) |
| Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 5954 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); |
| Douglas Gregor | 50832e0 | 2010-09-20 22:39:41 +0000 | [diff] [blame] | 5955 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 5956 | Results.data(), Results.size()); |
| Douglas Gregor | a817a19 | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 5957 | } |
| 5958 | |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5959 | void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 5960 | ArrayRef<IdentifierInfo *> SelIdents, |
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5961 | bool AtArgumentExpression) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5962 | ObjCInterfaceDecl *CDecl = nullptr; |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5963 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 5964 | // Figure out which interface we're in. |
| 5965 | CDecl = CurMethod->getClassInterface(); |
| 5966 | if (!CDecl) |
| 5967 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5968 | |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5969 | // Find the superclass of this class. |
| 5970 | CDecl = CDecl->getSuperClass(); |
| 5971 | if (!CDecl) |
| 5972 | return; |
| 5973 | |
| 5974 | if (CurMethod->isInstanceMethod()) { |
| 5975 | // We are inside an instance method, which means that the message |
| 5976 | // send [super ...] is actually calling an instance method on the |
| Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5977 | // current object. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5978 | return CodeCompleteObjCInstanceMessage(S, nullptr, SelIdents, |
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 5979 | AtArgumentExpression, |
| Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 5980 | CDecl); |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5981 | } |
| 5982 | |
| 5983 | // Fall through to send to the superclass in CDecl. |
| 5984 | } else { |
| 5985 | // "super" may be the name of a type or variable. Figure out which |
| 5986 | // it is. |
| Argyrios Kyrtzidis | 3e56dd4 | 2013-03-14 22:56:43 +0000 | [diff] [blame] | 5987 | IdentifierInfo *Super = getSuperIdentifier(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 5988 | NamedDecl *ND = LookupSingleName(S, Super, SuperLoc, |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5989 | LookupOrdinaryName); |
| 5990 | if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) { |
| 5991 | // "super" names an interface. Use it. |
| 5992 | } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) { |
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5993 | if (const ObjCObjectType *Iface |
| 5994 | = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>()) |
| 5995 | CDecl = Iface->getInterface(); |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 5996 | } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) { |
| 5997 | // "super" names an unresolved type; we can't be more specific. |
| 5998 | } else { |
| 5999 | // Assume that "super" names some kind of value and parse that way. |
| 6000 | CXXScopeSpec SS; |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 6001 | SourceLocation TemplateKWLoc; |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6002 | UnqualifiedId id; |
| 6003 | id.setIdentifier(Super, SuperLoc); |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 6004 | ExprResult SuperExpr = ActOnIdExpression(S, SS, TemplateKWLoc, id, |
| 6005 | false, false); |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6006 | return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(), |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6007 | SelIdents, |
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6008 | AtArgumentExpression); |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6009 | } |
| 6010 | |
| 6011 | // Fall through |
| 6012 | } |
| 6013 | |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6014 | ParsedType Receiver; |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6015 | if (CDecl) |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6016 | Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl)); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6017 | return CodeCompleteObjCClassMessage(S, Receiver, SelIdents, |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6018 | AtArgumentExpression, |
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6019 | /*IsSuper=*/true); |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6020 | } |
| 6021 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6022 | /// 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] | 6023 | /// send, determine the preferred type (if any) for that argument expression. |
| 6024 | static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results, |
| 6025 | unsigned NumSelIdents) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6026 | typedef CodeCompletionResult Result; |
| Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6027 | ASTContext &Context = Results.getSema().Context; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6028 | |
| Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6029 | QualType PreferredType; |
| 6030 | unsigned BestPriority = CCP_Unlikely * 2; |
| 6031 | Result *ResultsData = Results.data(); |
| 6032 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 6033 | Result &R = ResultsData[I]; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6034 | if (R.Kind == Result::RK_Declaration && |
| Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6035 | isa<ObjCMethodDecl>(R.Declaration)) { |
| 6036 | if (R.Priority <= BestPriority) { |
| Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 6037 | const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration); |
| Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6038 | if (NumSelIdents <= Method->param_size()) { |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 6039 | QualType MyPreferredType = Method->parameters()[NumSelIdents - 1] |
| Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6040 | ->getType(); |
| 6041 | if (R.Priority < BestPriority || PreferredType.isNull()) { |
| 6042 | BestPriority = R.Priority; |
| 6043 | PreferredType = MyPreferredType; |
| 6044 | } else if (!Context.hasSameUnqualifiedType(PreferredType, |
| 6045 | MyPreferredType)) { |
| 6046 | PreferredType = QualType(); |
| 6047 | } |
| 6048 | } |
| 6049 | } |
| 6050 | } |
| 6051 | } |
| 6052 | |
| 6053 | return PreferredType; |
| 6054 | } |
| 6055 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6056 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6057 | ParsedType Receiver, |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6058 | ArrayRef<IdentifierInfo *> SelIdents, |
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6059 | bool AtArgumentExpression, |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6060 | bool IsSuper, |
| 6061 | ResultBuilder &Results) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6062 | typedef CodeCompletionResult Result; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6063 | ObjCInterfaceDecl *CDecl = nullptr; |
| 6064 | |
| Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 6065 | // If the given name refers to an interface type, retrieve the |
| 6066 | // corresponding declaration. |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6067 | if (Receiver) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6068 | QualType T = SemaRef.GetTypeFromParser(Receiver, nullptr); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6069 | if (!T.isNull()) |
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6070 | if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>()) |
| 6071 | CDecl = Interface->getInterface(); |
| Douglas Gregor | 8ce3321 | 2009-11-17 17:59:40 +0000 | [diff] [blame] | 6072 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6073 | |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6074 | // Add all of the factory methods in this Objective-C class, its protocols, |
| 6075 | // superclasses, categories, implementation, etc. |
| Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6076 | Results.EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6077 | |
| 6078 | // 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] | 6079 | // completion. |
| 6080 | if (IsSuper) { |
| 6081 | if (ObjCMethodDecl *SuperMethod |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6082 | = AddSuperSendCompletion(SemaRef, false, SelIdents, Results)) |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6083 | Results.Ignore(SuperMethod); |
| 6084 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6085 | |
| Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 6086 | // If we're inside an Objective-C method definition, prefer its selector to |
| 6087 | // others. |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6088 | if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl()) |
| Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 6089 | Results.setPreferredSelector(CurMethod->getSelector()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6090 | |
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6091 | VisitedSelectorSet Selectors; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6092 | if (CDecl) |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6093 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, |
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6094 | SemaRef.CurContext, Selectors, AtArgumentExpression, |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6095 | Results); |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 6096 | else { |
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6097 | // We're messaging "id" as a type; provide all class/factory methods. |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6098 | |
| Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6099 | // If we have an external source, load the entire class method |
| Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 6100 | // pool from the AST file. |
| Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 6101 | if (SemaRef.getExternalSource()) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6102 | for (uint32_t I = 0, |
| Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 6103 | N = SemaRef.getExternalSource()->GetNumExternalSelectors(); |
| John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 6104 | I != N; ++I) { |
| Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 6105 | Selector Sel = SemaRef.getExternalSource()->GetExternalSelector(I); |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6106 | if (Sel.isNull() || SemaRef.MethodPool.count(Sel)) |
| Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6107 | continue; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6108 | |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6109 | SemaRef.ReadMethodPool(Sel); |
| Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6110 | } |
| 6111 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6112 | |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6113 | for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(), |
| 6114 | MEnd = SemaRef.MethodPool.end(); |
| Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6115 | M != MEnd; ++M) { |
| 6116 | for (ObjCMethodList *MethList = &M->second.second; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6117 | MethList && MethList->getMethod(); |
| Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 6118 | MethList = MethList->getNext()) { |
| Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6119 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6120 | continue; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6121 | |
| Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6122 | Result R(MethList->getMethod(), |
| 6123 | Results.getBasePriority(MethList->getMethod()), nullptr); |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6124 | R.StartParameter = SelIdents.size(); |
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6125 | R.AllParametersAreInformative = false; |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6126 | Results.MaybeAddResult(R, SemaRef.CurContext); |
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6127 | } |
| 6128 | } |
| 6129 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6130 | |
| 6131 | Results.ExitScope(); |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6132 | } |
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6133 | |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6134 | void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6135 | ArrayRef<IdentifierInfo *> SelIdents, |
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6136 | bool AtArgumentExpression, |
| Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 6137 | bool IsSuper) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6138 | |
| Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 6139 | QualType T = this->GetTypeFromParser(Receiver); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6140 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6141 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6142 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 6143 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage, |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6144 | T, SelIdents)); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6145 | |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6146 | AddClassMessageCompletions(*this, S, Receiver, SelIdents, |
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6147 | AtArgumentExpression, IsSuper, Results); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6148 | |
| 6149 | // 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] | 6150 | // selector), we're actually performing code completion for an expression. |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6151 | // Determine whether we have a single, best method. If so, we can |
| Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6152 | // code-complete the expression using the corresponding parameter type as |
| 6153 | // our preferred type, improving completion results. |
| 6154 | if (AtArgumentExpression) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6155 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6156 | SelIdents.size()); |
| Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6157 | if (PreferredType.isNull()) |
| 6158 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 6159 | else |
| 6160 | CodeCompleteExpression(S, PreferredType); |
| 6161 | return; |
| 6162 | } |
| 6163 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6164 | HandleCodeCompleteResults(this, CodeCompleter, |
| Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 6165 | Results.getCompletionContext(), |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6166 | Results.data(), Results.size()); |
| Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6167 | } |
| 6168 | |
| Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 6169 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver, |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6170 | ArrayRef<IdentifierInfo *> SelIdents, |
| Douglas Gregor | f86e4da | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 6171 | bool AtArgumentExpression, |
| Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 6172 | ObjCInterfaceDecl *Super) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6173 | typedef CodeCompletionResult Result; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6174 | |
| Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6175 | Expr *RecExpr = static_cast<Expr *>(Receiver); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6176 | |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6177 | // If necessary, apply function/array conversion to the receiver. |
| 6178 | // C99 6.7.5.3p[7,8]. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6179 | if (RecExpr) { |
| 6180 | ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr); |
| 6181 | if (Conv.isInvalid()) // conversion failed. bail. |
| 6182 | return; |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6183 | RecExpr = Conv.get(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6184 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6185 | QualType ReceiverType = RecExpr? RecExpr->getType() |
| Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 6186 | : Super? Context.getObjCObjectPointerType( |
| 6187 | Context.getObjCInterfaceType(Super)) |
| 6188 | : Context.getObjCIdType(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6189 | |
| Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6190 | // If we're messaging an expression with type "id" or "Class", check |
| 6191 | // whether we know something special about the receiver that allows |
| 6192 | // us to assume a more-specific receiver type. |
| Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 6193 | if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) { |
| Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6194 | if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) { |
| 6195 | if (ReceiverType->isObjCClassType()) |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6196 | return CodeCompleteObjCClassMessage(S, |
| Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6197 | ParsedType::make(Context.getObjCInterfaceType(IFace)), |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6198 | SelIdents, |
| Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6199 | AtArgumentExpression, Super); |
| 6200 | |
| 6201 | ReceiverType = Context.getObjCObjectPointerType( |
| 6202 | Context.getObjCInterfaceType(IFace)); |
| 6203 | } |
| Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 6204 | } else if (RecExpr && getLangOpts().CPlusPlus) { |
| 6205 | ExprResult Conv = PerformContextuallyConvertToObjCPointer(RecExpr); |
| 6206 | if (Conv.isUsable()) { |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6207 | RecExpr = Conv.get(); |
| Anders Carlsson | 382ba41 | 2014-02-28 19:07:22 +0000 | [diff] [blame] | 6208 | ReceiverType = RecExpr->getType(); |
| 6209 | } |
| 6210 | } |
| Douglas Gregor | dc520b0 | 2010-11-08 21:12:30 +0000 | [diff] [blame] | 6211 | |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6212 | // Build the set of methods we can see. |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6213 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6214 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 6215 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage, |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6216 | ReceiverType, SelIdents)); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6217 | |
| Douglas Gregor | bab2b3c | 2009-11-17 23:22:23 +0000 | [diff] [blame] | 6218 | Results.EnterNewScope(); |
| Douglas Gregor | 9d2ddb2 | 2010-04-06 19:22:33 +0000 | [diff] [blame] | 6219 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6220 | // 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] | 6221 | // completion. |
| Douglas Gregor | 392a84b | 2010-10-13 21:24:53 +0000 | [diff] [blame] | 6222 | if (Super) { |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6223 | if (ObjCMethodDecl *SuperMethod |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6224 | = AddSuperSendCompletion(*this, false, SelIdents, Results)) |
| Douglas Gregor | 6fc0413 | 2010-08-27 15:10:57 +0000 | [diff] [blame] | 6225 | Results.Ignore(SuperMethod); |
| 6226 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6227 | |
| Douglas Gregor | c2cb2e2 | 2010-08-27 15:29:55 +0000 | [diff] [blame] | 6228 | // If we're inside an Objective-C method definition, prefer its selector to |
| 6229 | // others. |
| 6230 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 6231 | Results.setPreferredSelector(CurMethod->getSelector()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6232 | |
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6233 | // Keep track of the selectors we've already added. |
| 6234 | VisitedSelectorSet Selectors; |
| 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 Class. This really isn't a message to an instance |
| 6237 | // method, so we treat it the same way we would treat a message send to a |
| 6238 | // class method. |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6239 | if (ReceiverType->isObjCClassType() || |
| Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6240 | ReceiverType->isObjCQualifiedClassType()) { |
| 6241 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { |
| 6242 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6243 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, |
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6244 | CurContext, Selectors, AtArgumentExpression, Results); |
| Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6245 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6246 | } |
| Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6247 | // Handle messages to a qualified ID ("id<foo>"). |
| 6248 | else if (const ObjCObjectPointerType *QualID |
| 6249 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 6250 | // Search protocols for instance methods. |
| Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6251 | for (auto *I : QualID->quals()) |
| 6252 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, |
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6253 | Selectors, AtArgumentExpression, Results); |
| Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6254 | } |
| 6255 | // Handle messages to a pointer to interface type. |
| 6256 | else if (const ObjCObjectPointerType *IFacePtr |
| 6257 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 6258 | // Search the class, its superclasses, etc., for instance methods. |
| Douglas Gregor | c8537c5 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 6259 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6260 | CurContext, Selectors, AtArgumentExpression, |
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6261 | Results); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6262 | |
| Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6263 | // Search protocols for instance methods. |
| Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6264 | for (auto *I : IFacePtr->quals()) |
| 6265 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, |
| Douglas Gregor | b4a7c03 | 2010-11-17 21:36:08 +0000 | [diff] [blame] | 6266 | Selectors, AtArgumentExpression, Results); |
| Douglas Gregor | a3329fa | 2009-11-18 00:06:18 +0000 | [diff] [blame] | 6267 | } |
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6268 | // Handle messages to "id". |
| 6269 | else if (ReceiverType->isObjCIdType()) { |
| Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6270 | // We're messaging "id", so provide all instance methods we know |
| 6271 | // about as code-completion results. |
| 6272 | |
| 6273 | // If we have an external source, load the entire class method |
| Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 6274 | // pool from the AST file. |
| Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6275 | if (ExternalSource) { |
| John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 6276 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 6277 | I != N; ++I) { |
| 6278 | Selector Sel = ExternalSource->GetExternalSelector(I); |
| Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6279 | if (Sel.isNull() || MethodPool.count(Sel)) |
| Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6280 | continue; |
| 6281 | |
| Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6282 | ReadMethodPool(Sel); |
| Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 6283 | } |
| 6284 | } |
| 6285 | |
| Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 6286 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 6287 | MEnd = MethodPool.end(); |
| 6288 | M != MEnd; ++M) { |
| 6289 | for (ObjCMethodList *MethList = &M->second.first; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6290 | MethList && MethList->getMethod(); |
| Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 6291 | MethList = MethList->getNext()) { |
| Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6292 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6293 | continue; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6294 | |
| Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6295 | if (!Selectors.insert(MethList->getMethod()->getSelector()).second) |
| Douglas Gregor | 1154e27 | 2010-09-16 16:06:31 +0000 | [diff] [blame] | 6296 | continue; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6297 | |
| Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 6298 | Result R(MethList->getMethod(), |
| 6299 | Results.getBasePriority(MethList->getMethod()), nullptr); |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6300 | R.StartParameter = SelIdents.size(); |
| Douglas Gregor | 6285f75 | 2010-04-06 16:40:00 +0000 | [diff] [blame] | 6301 | R.AllParametersAreInformative = false; |
| 6302 | Results.MaybeAddResult(R, CurContext); |
| 6303 | } |
| 6304 | } |
| 6305 | } |
| Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6306 | Results.ExitScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6307 | |
| 6308 | |
| 6309 | // 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] | 6310 | // selector), we're actually performing code completion for an expression. |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6311 | // Determine whether we have a single, best method. If so, we can |
| Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6312 | // code-complete the expression using the corresponding parameter type as |
| 6313 | // our preferred type, improving completion results. |
| 6314 | if (AtArgumentExpression) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6315 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6316 | SelIdents.size()); |
| Douglas Gregor | 7466127 | 2010-09-21 00:03:25 +0000 | [diff] [blame] | 6317 | if (PreferredType.isNull()) |
| 6318 | CodeCompleteOrdinaryName(S, PCC_Expression); |
| 6319 | else |
| 6320 | CodeCompleteExpression(S, PreferredType); |
| 6321 | return; |
| 6322 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6323 | |
| 6324 | HandleCodeCompleteResults(this, CodeCompleter, |
| Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 6325 | Results.getCompletionContext(), |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 6326 | Results.data(),Results.size()); |
| Steve Naroff | eae6503 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 6327 | } |
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6328 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6329 | void Sema::CodeCompleteObjCForCollection(Scope *S, |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6330 | DeclGroupPtrTy IterationVar) { |
| 6331 | CodeCompleteExpressionData Data; |
| 6332 | Data.ObjCCollection = true; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6333 | |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6334 | if (IterationVar.getAsOpaquePtr()) { |
| Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 6335 | DeclGroupRef DG = IterationVar.get(); |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6336 | for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) { |
| 6337 | if (*I) |
| 6338 | Data.IgnoreDecls.push_back(*I); |
| 6339 | } |
| 6340 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6341 | |
| Douglas Gregor | 68762e7 | 2010-08-23 21:17:50 +0000 | [diff] [blame] | 6342 | CodeCompleteExpression(S, Data); |
| 6343 | } |
| 6344 | |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6345 | void Sema::CodeCompleteObjCSelector(Scope *S, |
| 6346 | ArrayRef<IdentifierInfo *> SelIdents) { |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6347 | // If we have an external source, load the entire class method |
| 6348 | // pool from the AST file. |
| 6349 | if (ExternalSource) { |
| 6350 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 6351 | I != N; ++I) { |
| 6352 | Selector Sel = ExternalSource->GetExternalSelector(I); |
| 6353 | if (Sel.isNull() || MethodPool.count(Sel)) |
| 6354 | continue; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6355 | |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6356 | ReadMethodPool(Sel); |
| 6357 | } |
| 6358 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6359 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6360 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6361 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6362 | CodeCompletionContext::CCC_SelectorName); |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6363 | Results.EnterNewScope(); |
| 6364 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 6365 | MEnd = MethodPool.end(); |
| 6366 | M != MEnd; ++M) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6367 | |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6368 | Selector Sel = M->first; |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6369 | if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents)) |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6370 | continue; |
| 6371 | |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6372 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 6373 | Results.getCodeCompletionTUInfo()); |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6374 | if (Sel.isUnarySelector()) { |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6375 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 6376 | Sel.getNameForSlot(0))); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6377 | Results.AddResult(Builder.TakeString()); |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6378 | continue; |
| 6379 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6380 | |
| Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6381 | std::string Accumulator; |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6382 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) { |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 6383 | if (I == SelIdents.size()) { |
| Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6384 | if (!Accumulator.empty()) { |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6385 | Builder.AddInformativeChunk(Builder.getAllocator().CopyString( |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6386 | Accumulator)); |
| Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6387 | Accumulator.clear(); |
| 6388 | } |
| 6389 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6390 | |
| Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 6391 | Accumulator += Sel.getNameForSlot(I); |
| Douglas Gregor | 9ac1ad1 | 2010-08-26 16:46:39 +0000 | [diff] [blame] | 6392 | Accumulator += ':'; |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6393 | } |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 6394 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator)); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6395 | Results.AddResult(Builder.TakeString()); |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6396 | } |
| 6397 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6398 | |
| 6399 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 6400 | Results.data(), Results.size()); |
| 6401 | } |
| 6402 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6403 | /// Add all of the protocol declarations that we find in the given |
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6404 | /// (translation unit) context. |
| 6405 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, |
| Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 6406 | bool OnlyForwardDeclarations, |
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6407 | ResultBuilder &Results) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6408 | typedef CodeCompletionResult Result; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6409 | |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6410 | for (const auto *D : Ctx->decls()) { |
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6411 | // Record any protocols we find. |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6412 | if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(D)) |
| Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 6413 | if (!OnlyForwardDeclarations || !Proto->hasDefinition()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6414 | Results.AddResult(Result(Proto, Results.getBasePriority(Proto),nullptr), |
| 6415 | CurContext, nullptr, false); |
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6416 | } |
| 6417 | } |
| 6418 | |
| Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 6419 | void Sema::CodeCompleteObjCProtocolReferences( |
| 6420 | ArrayRef<IdentifierLocPair> Protocols) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6421 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6422 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6423 | CodeCompletionContext::CCC_ObjCProtocolName); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6424 | |
| Chandler Carruth | ede1163 | 2016-11-04 06:06:50 +0000 | [diff] [blame] | 6425 | if (CodeCompleter->includeGlobals()) { |
| Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6426 | Results.EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6427 | |
| Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6428 | // Tell the result set to ignore all of the protocols we have |
| 6429 | // already seen. |
| 6430 | // FIXME: This doesn't work when caching code-completion results. |
| Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 6431 | for (const IdentifierLocPair &Pair : Protocols) |
| 6432 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Pair.first, |
| 6433 | Pair.second)) |
| Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6434 | Results.Ignore(Protocol); |
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6435 | |
| Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6436 | // Add all protocols. |
| 6437 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6438 | Results); |
| Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 6439 | |
| Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6440 | Results.ExitScope(); |
| 6441 | } |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6442 | |
| 6443 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6444 | Results.data(), Results.size()); |
| Douglas Gregor | 5b4671c | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 6445 | } |
| 6446 | |
| 6447 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6448 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6449 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6450 | CodeCompletionContext::CCC_ObjCProtocolName); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6451 | |
| Chandler Carruth | ede1163 | 2016-11-04 06:06:50 +0000 | [diff] [blame] | 6452 | if (CodeCompleter->includeGlobals()) { |
| Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6453 | Results.EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6454 | |
| Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6455 | // Add all protocols. |
| 6456 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, |
| 6457 | Results); |
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6458 | |
| Douglas Gregor | a3b23b0 | 2010-12-09 21:44:02 +0000 | [diff] [blame] | 6459 | Results.ExitScope(); |
| 6460 | } |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6461 | |
| 6462 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6463 | Results.data(), Results.size()); |
| Douglas Gregor | baf6961 | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 6464 | } |
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6465 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6466 | /// Add all of the Objective-C interface declarations that we find in |
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6467 | /// the given (translation unit) context. |
| 6468 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, |
| 6469 | bool OnlyForwardDeclarations, |
| 6470 | bool OnlyUnimplemented, |
| 6471 | ResultBuilder &Results) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6472 | typedef CodeCompletionResult Result; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6473 | |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6474 | for (const auto *D : Ctx->decls()) { |
| Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 6475 | // Record any interfaces we find. |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6476 | if (const auto *Class = dyn_cast<ObjCInterfaceDecl>(D)) |
| Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 6477 | if ((!OnlyForwardDeclarations || !Class->hasDefinition()) && |
| Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 6478 | (!OnlyUnimplemented || !Class->getImplementation())) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6479 | Results.AddResult(Result(Class, Results.getBasePriority(Class),nullptr), |
| 6480 | CurContext, nullptr, false); |
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6481 | } |
| 6482 | } |
| 6483 | |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6484 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6485 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6486 | CodeCompleter->getCodeCompletionTUInfo(), |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6487 | CodeCompletionContext::CCC_ObjCInterfaceName); |
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6488 | Results.EnterNewScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6489 | |
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6490 | if (CodeCompleter->includeGlobals()) { |
| 6491 | // Add all classes. |
| 6492 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6493 | false, Results); |
| 6494 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6495 | |
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6496 | Results.ExitScope(); |
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6497 | |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6498 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6499 | Results.data(), Results.size()); |
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6500 | } |
| 6501 | |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6502 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName, |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6503 | SourceLocation ClassNameLoc) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6504 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6505 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6506 | CodeCompletionContext::CCC_ObjCInterfaceName); |
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6507 | Results.EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6508 | |
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6509 | // Make sure that we ignore the class we're currently defining. |
| 6510 | NamedDecl *CurClass |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6511 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6512 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) |
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6513 | Results.Ignore(CurClass); |
| 6514 | |
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6515 | if (CodeCompleter->includeGlobals()) { |
| 6516 | // Add all classes. |
| 6517 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6518 | false, Results); |
| 6519 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6520 | |
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6521 | Results.ExitScope(); |
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6522 | |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6523 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6524 | Results.data(), Results.size()); |
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6525 | } |
| 6526 | |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6527 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6528 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6529 | CodeCompleter->getCodeCompletionTUInfo(), |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6530 | CodeCompletionContext::CCC_ObjCImplementation); |
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6531 | Results.EnterNewScope(); |
| 6532 | |
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6533 | if (CodeCompleter->includeGlobals()) { |
| 6534 | // Add all unimplemented classes. |
| 6535 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, |
| 6536 | true, Results); |
| 6537 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6538 | |
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6539 | Results.ExitScope(); |
| Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 6540 | |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6541 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6542 | Results.data(), Results.size()); |
| Douglas Gregor | 49c22a7 | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 6543 | } |
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6544 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6545 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6546 | IdentifierInfo *ClassName, |
| 6547 | SourceLocation ClassNameLoc) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6548 | typedef CodeCompletionResult Result; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6549 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6550 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6551 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6552 | CodeCompletionContext::CCC_ObjCCategoryName); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6553 | |
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6554 | // Ignore any categories we find that have already been implemented by this |
| 6555 | // interface. |
| 6556 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 6557 | NamedDecl *CurClass |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6558 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
| Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6559 | if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)){ |
| Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 6560 | for (const auto *Cat : Class->visible_categories()) |
| Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6561 | CategoryNames.insert(Cat->getIdentifier()); |
| Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6562 | } |
| 6563 | |
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6564 | // Add all of the categories we know about. |
| 6565 | Results.EnterNewScope(); |
| 6566 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6567 | for (const auto *D : TU->decls()) |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6568 | if (const auto *Category = dyn_cast<ObjCCategoryDecl>(D)) |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6569 | if (CategoryNames.insert(Category->getIdentifier()).second) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6570 | Results.AddResult(Result(Category, Results.getBasePriority(Category), |
| 6571 | nullptr), |
| 6572 | CurContext, nullptr, false); |
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6573 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6574 | |
| 6575 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6576 | Results.data(), Results.size()); |
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6579 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6580 | IdentifierInfo *ClassName, |
| 6581 | SourceLocation ClassNameLoc) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6582 | typedef CodeCompletionResult Result; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6583 | |
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6584 | // Find the corresponding interface. If we couldn't find the interface, the |
| 6585 | // program itself is ill-formed. However, we'll try to be helpful still by |
| 6586 | // providing the list of all of the categories we know about. |
| 6587 | NamedDecl *CurClass |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6588 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); |
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6589 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); |
| 6590 | if (!Class) |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6591 | return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6592 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6593 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6594 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 6595 | CodeCompletionContext::CCC_ObjCCategoryName); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6596 | |
| 6597 | // Add all of the categories that have have corresponding interface |
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6598 | // declarations in this class and any of its superclasses, except for |
| 6599 | // already-implemented categories in the class itself. |
| 6600 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; |
| 6601 | Results.EnterNewScope(); |
| 6602 | bool IgnoreImplemented = true; |
| 6603 | while (Class) { |
| Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 6604 | for (const auto *Cat : Class->visible_categories()) { |
| Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6605 | if ((!IgnoreImplemented || !Cat->getImplementation()) && |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6606 | CategoryNames.insert(Cat->getIdentifier()).second) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6607 | Results.AddResult(Result(Cat, Results.getBasePriority(Cat), nullptr), |
| 6608 | CurContext, nullptr, false); |
| Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6609 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6610 | |
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6611 | Class = Class->getSuperClass(); |
| 6612 | IgnoreImplemented = false; |
| 6613 | } |
| 6614 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6615 | |
| 6616 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6617 | Results.data(), Results.size()); |
| Douglas Gregor | 5d34fd3 | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 6618 | } |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6619 | |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6620 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) { |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6621 | CodeCompletionContext CCContext(CodeCompletionContext::CCC_Other); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6622 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6623 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6624 | CCContext); |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6625 | |
| 6626 | // Figure out where this @synthesize lives. |
| 6627 | ObjCContainerDecl *Container |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6628 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6629 | if (!Container || |
| 6630 | (!isa<ObjCImplementationDecl>(Container) && |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6631 | !isa<ObjCCategoryImplDecl>(Container))) |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6632 | return; |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6633 | |
| 6634 | // Ignore any properties that have already been implemented. |
| Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6635 | Container = getContainerDef(Container); |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6636 | for (const auto *D : Container->decls()) |
| 6637 | if (const auto *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(D)) |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6638 | Results.Ignore(PropertyImpl->getPropertyDecl()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6639 | |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6640 | // Add any properties that we find. |
| Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 6641 | AddedPropertiesSet AddedProperties; |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6642 | Results.EnterNewScope(); |
| 6643 | if (ObjCImplementationDecl *ClassImpl |
| 6644 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6645 | AddObjCProperties(CCContext, ClassImpl->getClassInterface(), false, |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6646 | /*AllowNullaryMethods=*/false, CurContext, |
| Douglas Gregor | b888acf | 2010-12-09 23:01:55 +0000 | [diff] [blame] | 6647 | AddedProperties, Results); |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6648 | else |
| Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 6649 | AddObjCProperties(CCContext, |
| 6650 | cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6651 | false, /*AllowNullaryMethods=*/false, CurContext, |
| Douglas Gregor | 9514714 | 2011-05-05 15:50:42 +0000 | [diff] [blame] | 6652 | AddedProperties, Results); |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6653 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6654 | |
| 6655 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6656 | Results.data(), Results.size()); |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6657 | } |
| 6658 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6659 | void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S, |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6660 | IdentifierInfo *PropertyName) { |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 6661 | typedef CodeCompletionResult Result; |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6662 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6663 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 6664 | CodeCompletionContext::CCC_Other); |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6665 | |
| 6666 | // Figure out where this @synthesize lives. |
| 6667 | ObjCContainerDecl *Container |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 6668 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6669 | if (!Container || |
| 6670 | (!isa<ObjCImplementationDecl>(Container) && |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6671 | !isa<ObjCCategoryImplDecl>(Container))) |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6672 | return; |
| 6673 | |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6674 | // Figure out which interface we're looking into. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6675 | ObjCInterfaceDecl *Class = nullptr; |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6676 | if (ObjCImplementationDecl *ClassImpl |
| Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 6677 | = dyn_cast<ObjCImplementationDecl>(Container)) |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6678 | Class = ClassImpl->getClassInterface(); |
| 6679 | else |
| 6680 | Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl() |
| 6681 | ->getClassInterface(); |
| 6682 | |
| Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6683 | // Determine the type of the property we're synthesizing. |
| 6684 | QualType PropertyType = Context.getObjCIdType(); |
| 6685 | if (Class) { |
| Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 6686 | if (ObjCPropertyDecl *Property = Class->FindPropertyDeclaration( |
| 6687 | PropertyName, ObjCPropertyQueryKind::OBJC_PR_query_instance)) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6688 | PropertyType |
| Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6689 | = Property->getType().getNonReferenceType().getUnqualifiedType(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6690 | |
| 6691 | // Give preference to ivars |
| Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6692 | Results.setPreferredType(PropertyType); |
| 6693 | } |
| 6694 | } |
| 6695 | |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6696 | // Add all of the instance variables in this class and its superclasses. |
| 6697 | Results.EnterNewScope(); |
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6698 | bool SawSimilarlyNamedIvar = false; |
| 6699 | std::string NameWithPrefix; |
| 6700 | NameWithPrefix += '_'; |
| Benjamin Kramer | 632500c | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 6701 | NameWithPrefix += PropertyName->getName(); |
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6702 | std::string NameWithSuffix = PropertyName->getName().str(); |
| 6703 | NameWithSuffix += '_'; |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6704 | for(; Class; Class = Class->getSuperClass()) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6705 | for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar; |
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6706 | Ivar = Ivar->getNextIvar()) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6707 | Results.AddResult(Result(Ivar, Results.getBasePriority(Ivar), nullptr), |
| 6708 | CurContext, nullptr, false); |
| 6709 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6710 | // 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] | 6711 | // property. |
| Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6712 | if ((PropertyName == Ivar->getIdentifier() || |
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6713 | NameWithPrefix == Ivar->getName() || |
| Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6714 | NameWithSuffix == Ivar->getName())) { |
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6715 | SawSimilarlyNamedIvar = true; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6716 | |
| Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6717 | // Reduce the priority of this result by one, to give it a slight |
| 6718 | // advantage over other results whose names don't match so closely. |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6719 | if (Results.size() && |
| 6720 | Results.data()[Results.size() - 1].Kind |
| Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6721 | == CodeCompletionResult::RK_Declaration && |
| 6722 | Results.data()[Results.size() - 1].Declaration == Ivar) |
| 6723 | Results.data()[Results.size() - 1].Priority--; |
| 6724 | } |
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6725 | } |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6726 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6727 | |
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6728 | if (!SawSimilarlyNamedIvar) { |
| 6729 | // Create ivar result _propName, that the user can use to synthesize |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6730 | // an ivar of the appropriate type. |
| Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6731 | unsigned Priority = CCP_MemberDeclaration + 1; |
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6732 | typedef CodeCompletionResult Result; |
| 6733 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6734 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo(), |
| 6735 | Priority,CXAvailability_Available); |
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6736 | |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6737 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
| Douglas Gregor | 6c7a9ee | 2011-04-18 14:40:46 +0000 | [diff] [blame] | 6738 | Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context, |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6739 | Policy, Allocator)); |
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6740 | Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix)); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6741 | Results.AddResult(Result(Builder.TakeString(), Priority, |
| Douglas Gregor | 331faa0 | 2011-04-18 14:13:53 +0000 | [diff] [blame] | 6742 | CXCursor_ObjCIvarDecl)); |
| 6743 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6744 | |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6745 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 6746 | |
| 6747 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 6748 | Results.data(), Results.size()); |
| Douglas Gregor | 5d64988 | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 6749 | } |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6750 | |
| Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6751 | // Mapping from selectors to the methods that implement that selector, along |
| 6752 | // with the "in original class" flag. |
| Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 6753 | typedef llvm::DenseMap< |
| 6754 | Selector, llvm::PointerIntPair<ObjCMethodDecl *, 1, bool> > KnownMethodsMap; |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6755 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6756 | /// Find all of the methods that reside in the given container |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6757 | /// (and its superclasses, protocols, etc.) that meet the given |
| 6758 | /// criteria. Insert those methods into the map of known methods, |
| 6759 | /// indexed by selector so they can be easily found. |
| 6760 | static void FindImplementableMethods(ASTContext &Context, |
| 6761 | ObjCContainerDecl *Container, |
| Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 6762 | Optional<bool> WantInstanceMethods, |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6763 | QualType ReturnType, |
| Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 6764 | KnownMethodsMap &KnownMethods, |
| 6765 | bool InOriginalClass = true) { |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6766 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6767 | // Make sure we have a definition; that's what we'll walk. |
| Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 6768 | if (!IFace->hasDefinition()) |
| 6769 | return; |
| Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6770 | |
| 6771 | IFace = IFace->getDefinition(); |
| 6772 | Container = IFace; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6773 | |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6774 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6775 | = IFace->getReferencedProtocols(); |
| 6776 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6777 | E = Protocols.end(); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6778 | I != E; ++I) |
| 6779 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6780 | KnownMethods, InOriginalClass); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6781 | |
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6782 | // Add methods from any class extensions and categories. |
| Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 6783 | for (auto *Cat : IFace->visible_categories()) { |
| 6784 | FindImplementableMethods(Context, Cat, WantInstanceMethods, ReturnType, |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6785 | KnownMethods, false); |
| Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 6786 | } |
| 6787 | |
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6788 | // Visit the superclass. |
| 6789 | if (IFace->getSuperClass()) |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6790 | FindImplementableMethods(Context, IFace->getSuperClass(), |
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6791 | WantInstanceMethods, ReturnType, |
| 6792 | KnownMethods, false); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6793 | } |
| 6794 | |
| 6795 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { |
| 6796 | // Recurse into protocols. |
| 6797 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6798 | = Category->getReferencedProtocols(); |
| 6799 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6800 | E = Protocols.end(); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6801 | I != E; ++I) |
| 6802 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6803 | KnownMethods, InOriginalClass); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6804 | |
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6805 | // If this category is the original class, jump to the interface. |
| 6806 | if (InOriginalClass && Category->getClassInterface()) |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6807 | FindImplementableMethods(Context, Category->getClassInterface(), |
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 6808 | WantInstanceMethods, ReturnType, KnownMethods, |
| 6809 | false); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6810 | } |
| 6811 | |
| 6812 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6813 | // Make sure we have a definition; that's what we'll walk. |
| 6814 | if (!Protocol->hasDefinition()) |
| 6815 | return; |
| 6816 | Protocol = Protocol->getDefinition(); |
| 6817 | Container = Protocol; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6818 | |
| Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6819 | // Recurse into protocols. |
| 6820 | const ObjCList<ObjCProtocolDecl> &Protocols |
| 6821 | = Protocol->getReferencedProtocols(); |
| 6822 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6823 | E = Protocols.end(); |
| Douglas Gregor | 9b4f370 | 2012-06-12 13:44:08 +0000 | [diff] [blame] | 6824 | I != E; ++I) |
| 6825 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, |
| 6826 | KnownMethods, false); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6827 | } |
| 6828 | |
| 6829 | // Add methods in this container. This operation occurs last because |
| 6830 | // we want the methods from this container to override any methods |
| 6831 | // we've previously seen with the same selector. |
| Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 6832 | for (auto *M : Container->methods()) { |
| Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 6833 | if (!WantInstanceMethods || M->isInstanceMethod() == *WantInstanceMethods) { |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6834 | if (!ReturnType.isNull() && |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6835 | !Context.hasSameUnqualifiedType(ReturnType, M->getReturnType())) |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6836 | continue; |
| 6837 | |
| Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 6838 | KnownMethods[M->getSelector()] = |
| Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 6839 | KnownMethodsMap::mapped_type(M, InOriginalClass); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 6840 | } |
| 6841 | } |
| 6842 | } |
| 6843 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6844 | /// Add the parenthesized return or parameter type chunk to a code |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6845 | /// completion string. |
| 6846 | static void AddObjCPassingTypeChunk(QualType Type, |
| Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6847 | unsigned ObjCDeclQuals, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6848 | ASTContext &Context, |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6849 | const PrintingPolicy &Policy, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6850 | CodeCompletionBuilder &Builder) { |
| 6851 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 6852 | std::string Quals = formatObjCParamQualifiers(ObjCDeclQuals, Type); |
| Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6853 | if (!Quals.empty()) |
| 6854 | Builder.AddTextChunk(Builder.getAllocator().CopyString(Quals)); |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6855 | Builder.AddTextChunk(GetCompletionTypeString(Type, Context, Policy, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6856 | Builder.getAllocator())); |
| 6857 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6858 | } |
| 6859 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6860 | /// Determine whether the given class is or inherits from a class by |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6861 | /// the given name. |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6862 | static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class, |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6863 | StringRef Name) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6864 | if (!Class) |
| 6865 | return false; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6866 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6867 | if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name) |
| 6868 | return true; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6869 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6870 | return InheritsFromClassNamed(Class->getSuperClass(), Name); |
| 6871 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6872 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6873 | /// Add code completions for Objective-C Key-Value Coding (KVC) and |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6874 | /// Key-Value Observing (KVO). |
| 6875 | static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property, |
| 6876 | bool IsInstanceMethod, |
| 6877 | QualType ReturnType, |
| 6878 | ASTContext &Context, |
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 6879 | VisitedSelectorSet &KnownSelectors, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6880 | ResultBuilder &Results) { |
| 6881 | IdentifierInfo *PropName = Property->getIdentifier(); |
| 6882 | if (!PropName || PropName->getLength() == 0) |
| 6883 | return; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6884 | |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6885 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
| 6886 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6887 | // Builder that will create each code completion. |
| 6888 | typedef CodeCompletionResult Result; |
| 6889 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 6890 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6891 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6892 | // The selector table. |
| 6893 | SelectorTable &Selectors = Context.Selectors; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6894 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6895 | // The property name, copied into the code completion allocation region |
| 6896 | // on demand. |
| 6897 | struct KeyHolder { |
| 6898 | CodeCompletionAllocator &Allocator; |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6899 | StringRef Key; |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6900 | const char *CopiedKey; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6901 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6902 | KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6903 | : Allocator(Allocator), Key(Key), CopiedKey(nullptr) {} |
| 6904 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6905 | operator const char *() { |
| 6906 | if (CopiedKey) |
| 6907 | return CopiedKey; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6908 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6909 | return CopiedKey = Allocator.CopyString(Key); |
| 6910 | } |
| 6911 | } Key(Allocator, PropName->getName()); |
| 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 | // The uppercased name of the property name. |
| 6914 | std::string UpperKey = PropName->getName(); |
| 6915 | if (!UpperKey.empty()) |
| Jordan Rose | 4938f27 | 2013-02-09 10:09:43 +0000 | [diff] [blame] | 6916 | UpperKey[0] = toUppercase(UpperKey[0]); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6917 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6918 | bool ReturnTypeMatchesProperty = ReturnType.isNull() || |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6919 | Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(), |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6920 | Property->getType()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6921 | bool ReturnTypeMatchesVoid |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6922 | = ReturnType.isNull() || ReturnType->isVoidType(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6923 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6924 | // Add the normal accessor -(type)key. |
| 6925 | if (IsInstanceMethod && |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6926 | KnownSelectors.insert(Selectors.getNullarySelector(PropName)).second && |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6927 | ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) { |
| 6928 | if (ReturnType.isNull()) |
| Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6929 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, |
| 6930 | Context, Policy, Builder); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6931 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6932 | Builder.AddTypedTextChunk(Key); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6933 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6934 | CXCursor_ObjCInstanceMethodDecl)); |
| 6935 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6936 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6937 | // If we have an integral or boolean property (or the user has provided |
| 6938 | // an integral or boolean return type), add the accessor -(type)isKey. |
| 6939 | if (IsInstanceMethod && |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6940 | ((!ReturnType.isNull() && |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6941 | (ReturnType->isIntegerType() || ReturnType->isBooleanType())) || |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6942 | (ReturnType.isNull() && |
| 6943 | (Property->getType()->isIntegerType() || |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6944 | Property->getType()->isBooleanType())))) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6945 | std::string SelectorName = (Twine("is") + UpperKey).str(); |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6946 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6947 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 6948 | .second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6949 | if (ReturnType.isNull()) { |
| 6950 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6951 | Builder.AddTextChunk("BOOL"); |
| 6952 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 6953 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6954 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6955 | Builder.AddTypedTextChunk( |
| 6956 | Allocator.CopyString(SelectorId->getName())); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6957 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6958 | CXCursor_ObjCInstanceMethodDecl)); |
| 6959 | } |
| 6960 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6961 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6962 | // Add the normal mutator. |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6963 | if (IsInstanceMethod && ReturnTypeMatchesVoid && |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6964 | !Property->getSetterMethodDecl()) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6965 | std::string SelectorName = (Twine("set") + UpperKey).str(); |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 6966 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6967 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6968 | if (ReturnType.isNull()) { |
| 6969 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 6970 | Builder.AddTextChunk("void"); |
| 6971 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 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 | Builder.AddTypedTextChunk( |
| 6975 | Allocator.CopyString(SelectorId->getName())); |
| 6976 | Builder.AddTypedTextChunk(":"); |
| Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 6977 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, |
| 6978 | Context, Policy, Builder); |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6979 | Builder.AddTextChunk(Key); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6980 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6981 | CXCursor_ObjCInstanceMethodDecl)); |
| 6982 | } |
| 6983 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6984 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6985 | // Indexed and unordered accessors |
| 6986 | unsigned IndexedGetterPriority = CCP_CodePattern; |
| 6987 | unsigned IndexedSetterPriority = CCP_CodePattern; |
| 6988 | unsigned UnorderedGetterPriority = CCP_CodePattern; |
| 6989 | unsigned UnorderedSetterPriority = CCP_CodePattern; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6990 | if (const ObjCObjectPointerType *ObjCPointer |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6991 | = Property->getType()->getAs<ObjCObjectPointerType>()) { |
| 6992 | if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) { |
| 6993 | // If this interface type is not provably derived from a known |
| 6994 | // collection, penalize the corresponding completions. |
| 6995 | if (!InheritsFromClassNamed(IFace, "NSMutableArray")) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 6996 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 6997 | if (!InheritsFromClassNamed(IFace, "NSArray")) |
| 6998 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 6999 | } |
| 7000 | |
| 7001 | if (!InheritsFromClassNamed(IFace, "NSMutableSet")) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7002 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7003 | if (!InheritsFromClassNamed(IFace, "NSSet")) |
| 7004 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 7005 | } |
| 7006 | } |
| 7007 | } else { |
| 7008 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 7009 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 7010 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; |
| 7011 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; |
| 7012 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7013 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7014 | // Add -(NSUInteger)countOf<key> |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7015 | if (IsInstanceMethod && |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7016 | (ReturnType.isNull() || ReturnType->isIntegerType())) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7017 | std::string SelectorName = (Twine("countOf") + UpperKey).str(); |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7018 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7019 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7020 | .second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7021 | if (ReturnType.isNull()) { |
| 7022 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7023 | Builder.AddTextChunk("NSUInteger"); |
| 7024 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7025 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7026 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7027 | Builder.AddTypedTextChunk( |
| 7028 | Allocator.CopyString(SelectorId->getName())); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7029 | Results.AddResult(Result(Builder.TakeString(), |
| 7030 | std::min(IndexedGetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7031 | UnorderedGetterPriority), |
| 7032 | CXCursor_ObjCInstanceMethodDecl)); |
| 7033 | } |
| 7034 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7035 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7036 | // Indexed getters |
| 7037 | // Add -(id)objectInKeyAtIndex:(NSUInteger)index |
| 7038 | if (IsInstanceMethod && |
| 7039 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7040 | std::string SelectorName |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7041 | = (Twine("objectIn") + UpperKey + "AtIndex").str(); |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7042 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7043 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7044 | if (ReturnType.isNull()) { |
| 7045 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7046 | Builder.AddTextChunk("id"); |
| 7047 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7048 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7049 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7050 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7051 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7052 | Builder.AddTextChunk("NSUInteger"); |
| 7053 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7054 | Builder.AddTextChunk("index"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7055 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7056 | CXCursor_ObjCInstanceMethodDecl)); |
| 7057 | } |
| 7058 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7059 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7060 | // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes |
| 7061 | if (IsInstanceMethod && |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7062 | (ReturnType.isNull() || |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7063 | (ReturnType->isObjCObjectPointerType() && |
| 7064 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 7065 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 7066 | ->getName() == "NSArray"))) { |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7067 | std::string SelectorName |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7068 | = (Twine(Property->getName()) + "AtIndexes").str(); |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7069 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7070 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7071 | if (ReturnType.isNull()) { |
| 7072 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7073 | Builder.AddTextChunk("NSArray *"); |
| 7074 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7075 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7076 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7077 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7078 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7079 | Builder.AddTextChunk("NSIndexSet *"); |
| 7080 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7081 | Builder.AddTextChunk("indexes"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7082 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7083 | CXCursor_ObjCInstanceMethodDecl)); |
| 7084 | } |
| 7085 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7086 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7087 | // Add -(void)getKey:(type **)buffer range:(NSRange)inRange |
| 7088 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7089 | std::string SelectorName = (Twine("get") + UpperKey).str(); |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7090 | IdentifierInfo *SelectorIds[2] = { |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7091 | &Context.Idents.get(SelectorName), |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7092 | &Context.Idents.get("range") |
| 7093 | }; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7094 | |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7095 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7096 | if (ReturnType.isNull()) { |
| 7097 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7098 | Builder.AddTextChunk("void"); |
| 7099 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7100 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7101 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7102 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7103 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7104 | Builder.AddPlaceholderChunk("object-type"); |
| 7105 | Builder.AddTextChunk(" **"); |
| 7106 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7107 | Builder.AddTextChunk("buffer"); |
| 7108 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7109 | Builder.AddTypedTextChunk("range:"); |
| 7110 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7111 | Builder.AddTextChunk("NSRange"); |
| 7112 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7113 | Builder.AddTextChunk("inRange"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7114 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7115 | CXCursor_ObjCInstanceMethodDecl)); |
| 7116 | } |
| 7117 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7118 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7119 | // Mutable indexed accessors |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7120 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7121 | // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index |
| 7122 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7123 | std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str(); |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7124 | IdentifierInfo *SelectorIds[2] = { |
| 7125 | &Context.Idents.get("insertObject"), |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7126 | &Context.Idents.get(SelectorName) |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7127 | }; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7128 | |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7129 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7130 | if (ReturnType.isNull()) { |
| 7131 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7132 | Builder.AddTextChunk("void"); |
| 7133 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7134 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7135 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7136 | Builder.AddTypedTextChunk("insertObject:"); |
| 7137 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7138 | Builder.AddPlaceholderChunk("object-type"); |
| 7139 | Builder.AddTextChunk(" *"); |
| 7140 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7141 | Builder.AddTextChunk("object"); |
| 7142 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7143 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7144 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7145 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 7146 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7147 | Builder.AddTextChunk("index"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7148 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7149 | CXCursor_ObjCInstanceMethodDecl)); |
| 7150 | } |
| 7151 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7152 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7153 | // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes |
| 7154 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7155 | std::string SelectorName = (Twine("insert") + UpperKey).str(); |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7156 | IdentifierInfo *SelectorIds[2] = { |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7157 | &Context.Idents.get(SelectorName), |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7158 | &Context.Idents.get("atIndexes") |
| 7159 | }; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7160 | |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7161 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7162 | if (ReturnType.isNull()) { |
| 7163 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7164 | Builder.AddTextChunk("void"); |
| 7165 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7166 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7167 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7168 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7169 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7170 | Builder.AddTextChunk("NSArray *"); |
| 7171 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7172 | Builder.AddTextChunk("array"); |
| 7173 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7174 | Builder.AddTypedTextChunk("atIndexes:"); |
| 7175 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7176 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 7177 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7178 | Builder.AddTextChunk("indexes"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7179 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7180 | CXCursor_ObjCInstanceMethodDecl)); |
| 7181 | } |
| 7182 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7183 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7184 | // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index |
| 7185 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7186 | std::string SelectorName |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7187 | = (Twine("removeObjectFrom") + UpperKey + "AtIndex").str(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7188 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7189 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7190 | if (ReturnType.isNull()) { |
| 7191 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7192 | Builder.AddTextChunk("void"); |
| 7193 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7194 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7195 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7196 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7197 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7198 | Builder.AddTextChunk("NSUInteger"); |
| 7199 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7200 | Builder.AddTextChunk("index"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7201 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7202 | CXCursor_ObjCInstanceMethodDecl)); |
| 7203 | } |
| 7204 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7205 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7206 | // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes |
| 7207 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7208 | std::string SelectorName |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7209 | = (Twine("remove") + UpperKey + "AtIndexes").str(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7210 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7211 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7212 | if (ReturnType.isNull()) { |
| 7213 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7214 | Builder.AddTextChunk("void"); |
| 7215 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7216 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7217 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7218 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7219 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7220 | Builder.AddTextChunk("NSIndexSet *"); |
| 7221 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7222 | Builder.AddTextChunk("indexes"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7223 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7224 | CXCursor_ObjCInstanceMethodDecl)); |
| 7225 | } |
| 7226 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7227 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7228 | // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object |
| 7229 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7230 | std::string SelectorName |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7231 | = (Twine("replaceObjectIn") + UpperKey + "AtIndex").str(); |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7232 | IdentifierInfo *SelectorIds[2] = { |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7233 | &Context.Idents.get(SelectorName), |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7234 | &Context.Idents.get("withObject") |
| 7235 | }; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7236 | |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7237 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7238 | if (ReturnType.isNull()) { |
| 7239 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7240 | Builder.AddTextChunk("void"); |
| 7241 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7242 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7243 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7244 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7245 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7246 | Builder.AddPlaceholderChunk("NSUInteger"); |
| 7247 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7248 | Builder.AddTextChunk("index"); |
| 7249 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7250 | Builder.AddTypedTextChunk("withObject:"); |
| 7251 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7252 | Builder.AddTextChunk("id"); |
| 7253 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7254 | Builder.AddTextChunk("object"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7255 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7256 | CXCursor_ObjCInstanceMethodDecl)); |
| 7257 | } |
| 7258 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7259 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7260 | // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array |
| 7261 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7262 | std::string SelectorName1 |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7263 | = (Twine("replace") + UpperKey + "AtIndexes").str(); |
| 7264 | std::string SelectorName2 = (Twine("with") + UpperKey).str(); |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7265 | IdentifierInfo *SelectorIds[2] = { |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7266 | &Context.Idents.get(SelectorName1), |
| 7267 | &Context.Idents.get(SelectorName2) |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7268 | }; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7269 | |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7270 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7271 | if (ReturnType.isNull()) { |
| 7272 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7273 | Builder.AddTextChunk("void"); |
| 7274 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7275 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7276 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7277 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":")); |
| 7278 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7279 | Builder.AddPlaceholderChunk("NSIndexSet *"); |
| 7280 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7281 | Builder.AddTextChunk("indexes"); |
| 7282 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7283 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":")); |
| 7284 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7285 | Builder.AddTextChunk("NSArray *"); |
| 7286 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7287 | Builder.AddTextChunk("array"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7288 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7289 | CXCursor_ObjCInstanceMethodDecl)); |
| 7290 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7291 | } |
| 7292 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7293 | // Unordered getters |
| 7294 | // - (NSEnumerator *)enumeratorOfKey |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7295 | if (IsInstanceMethod && |
| 7296 | (ReturnType.isNull() || |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7297 | (ReturnType->isObjCObjectPointerType() && |
| 7298 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 7299 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 7300 | ->getName() == "NSEnumerator"))) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7301 | std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str(); |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7302 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7303 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7304 | .second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7305 | if (ReturnType.isNull()) { |
| 7306 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7307 | Builder.AddTextChunk("NSEnumerator *"); |
| 7308 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7309 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7310 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7311 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7312 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7313 | CXCursor_ObjCInstanceMethodDecl)); |
| 7314 | } |
| 7315 | } |
| 7316 | |
| 7317 | // - (type *)memberOfKey:(type *)object |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7318 | if (IsInstanceMethod && |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7319 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7320 | std::string SelectorName = (Twine("memberOf") + UpperKey).str(); |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7321 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7322 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7323 | if (ReturnType.isNull()) { |
| 7324 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7325 | Builder.AddPlaceholderChunk("object-type"); |
| 7326 | Builder.AddTextChunk(" *"); |
| 7327 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7328 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7329 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7330 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7331 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7332 | if (ReturnType.isNull()) { |
| 7333 | Builder.AddPlaceholderChunk("object-type"); |
| 7334 | Builder.AddTextChunk(" *"); |
| 7335 | } else { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7336 | Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context, |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7337 | Policy, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7338 | Builder.getAllocator())); |
| 7339 | } |
| 7340 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7341 | Builder.AddTextChunk("object"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7342 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7343 | CXCursor_ObjCInstanceMethodDecl)); |
| 7344 | } |
| 7345 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7346 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7347 | // Mutable unordered accessors |
| 7348 | // - (void)addKeyObject:(type *)object |
| 7349 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7350 | std::string SelectorName |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7351 | = (Twine("add") + UpperKey + Twine("Object")).str(); |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7352 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7353 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7354 | if (ReturnType.isNull()) { |
| 7355 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7356 | Builder.AddTextChunk("void"); |
| 7357 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7358 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7359 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7360 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7361 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7362 | Builder.AddPlaceholderChunk("object-type"); |
| 7363 | Builder.AddTextChunk(" *"); |
| 7364 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7365 | Builder.AddTextChunk("object"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7366 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7367 | CXCursor_ObjCInstanceMethodDecl)); |
| 7368 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7369 | } |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7370 | |
| 7371 | // - (void)addKey:(NSSet *)objects |
| 7372 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7373 | std::string SelectorName = (Twine("add") + UpperKey).str(); |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7374 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7375 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7376 | if (ReturnType.isNull()) { |
| 7377 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7378 | Builder.AddTextChunk("void"); |
| 7379 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7380 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7381 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7382 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7383 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7384 | Builder.AddTextChunk("NSSet *"); |
| 7385 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7386 | Builder.AddTextChunk("objects"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7387 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7388 | CXCursor_ObjCInstanceMethodDecl)); |
| 7389 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7390 | } |
| 7391 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7392 | // - (void)removeKeyObject:(type *)object |
| 7393 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7394 | std::string SelectorName |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7395 | = (Twine("remove") + UpperKey + Twine("Object")).str(); |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7396 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7397 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7398 | if (ReturnType.isNull()) { |
| 7399 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7400 | Builder.AddTextChunk("void"); |
| 7401 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7402 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7403 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7404 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7405 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7406 | Builder.AddPlaceholderChunk("object-type"); |
| 7407 | Builder.AddTextChunk(" *"); |
| 7408 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7409 | Builder.AddTextChunk("object"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7410 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7411 | CXCursor_ObjCInstanceMethodDecl)); |
| 7412 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7413 | } |
| 7414 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7415 | // - (void)removeKey:(NSSet *)objects |
| 7416 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7417 | std::string SelectorName = (Twine("remove") + UpperKey).str(); |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7418 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7419 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7420 | if (ReturnType.isNull()) { |
| 7421 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7422 | Builder.AddTextChunk("void"); |
| 7423 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7424 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7425 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7426 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7427 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7428 | Builder.AddTextChunk("NSSet *"); |
| 7429 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7430 | Builder.AddTextChunk("objects"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7431 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7432 | CXCursor_ObjCInstanceMethodDecl)); |
| 7433 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7434 | } |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7435 | |
| 7436 | // - (void)intersectKey:(NSSet *)objects |
| 7437 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7438 | std::string SelectorName = (Twine("intersect") + UpperKey).str(); |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7439 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7440 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7441 | if (ReturnType.isNull()) { |
| 7442 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7443 | Builder.AddTextChunk("void"); |
| 7444 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7445 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7446 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7447 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); |
| 7448 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7449 | Builder.AddTextChunk("NSSet *"); |
| 7450 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7451 | Builder.AddTextChunk("objects"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7452 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7453 | CXCursor_ObjCInstanceMethodDecl)); |
| 7454 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7455 | } |
| 7456 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7457 | // Key-Value Observing |
| 7458 | // + (NSSet *)keyPathsForValuesAffectingKey |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7459 | if (!IsInstanceMethod && |
| 7460 | (ReturnType.isNull() || |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7461 | (ReturnType->isObjCObjectPointerType() && |
| 7462 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && |
| 7463 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() |
| 7464 | ->getName() == "NSSet"))) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7465 | std::string SelectorName |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7466 | = (Twine("keyPathsForValuesAffecting") + UpperKey).str(); |
| Douglas Gregor | 0e5d72f | 2011-02-17 03:19:26 +0000 | [diff] [blame] | 7467 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7468 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7469 | .second) { |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7470 | if (ReturnType.isNull()) { |
| 7471 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| Alex Lorenz | 71ecb07 | 2016-12-08 16:49:05 +0000 | [diff] [blame] | 7472 | Builder.AddTextChunk("NSSet<NSString *> *"); |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7473 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7474 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7475 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7476 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7477 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7478 | CXCursor_ObjCClassMethodDecl)); |
| 7479 | } |
| 7480 | } |
| 7481 | |
| 7482 | // + (BOOL)automaticallyNotifiesObserversForKey |
| 7483 | if (!IsInstanceMethod && |
| 7484 | (ReturnType.isNull() || |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7485 | ReturnType->isIntegerType() || |
| Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7486 | ReturnType->isBooleanType())) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7487 | std::string SelectorName |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7488 | = (Twine("automaticallyNotifiesObserversOf") + UpperKey).str(); |
| Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7489 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7490 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) |
| 7491 | .second) { |
| Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7492 | if (ReturnType.isNull()) { |
| 7493 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7494 | Builder.AddTextChunk("BOOL"); |
| 7495 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7496 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7497 | |
| Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7498 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7499 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, |
| Douglas Gregor | 857bcda | 2011-06-02 04:02:27 +0000 | [diff] [blame] | 7500 | CXCursor_ObjCClassMethodDecl)); |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7501 | } |
| 7502 | } |
| 7503 | } |
| 7504 | |
| Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 7505 | void Sema::CodeCompleteObjCMethodDecl(Scope *S, Optional<bool> IsInstanceMethod, |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7506 | ParsedType ReturnTy) { |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7507 | // Determine the return type of the method we're declaring, if |
| 7508 | // provided. |
| 7509 | QualType ReturnType = GetTypeFromParser(ReturnTy); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7510 | Decl *IDecl = nullptr; |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7511 | if (CurContext->isObjCContainer()) { |
| 7512 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
| George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 7513 | IDecl = OCD; |
| Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 7514 | } |
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7515 | // Determine where we should start searching for methods. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7516 | ObjCContainerDecl *SearchDecl = nullptr; |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7517 | bool IsInImplementation = false; |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7518 | if (Decl *D = IDecl) { |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7519 | if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) { |
| 7520 | SearchDecl = Impl->getClassInterface(); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7521 | IsInImplementation = true; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7522 | } else if (ObjCCategoryImplDecl *CatImpl |
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7523 | = dyn_cast<ObjCCategoryImplDecl>(D)) { |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7524 | SearchDecl = CatImpl->getCategoryDecl(); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7525 | IsInImplementation = true; |
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7526 | } else |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7527 | SearchDecl = dyn_cast<ObjCContainerDecl>(D); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7528 | } |
| 7529 | |
| 7530 | if (!SearchDecl && S) { |
| Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 7531 | if (DeclContext *DC = S->getEntity()) |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7532 | SearchDecl = dyn_cast<ObjCContainerDecl>(DC); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7533 | } |
| 7534 | |
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7535 | if (!SearchDecl) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7536 | HandleCodeCompleteResults(this, CodeCompleter, |
| Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 7537 | CodeCompletionContext::CCC_Other, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7538 | nullptr, 0); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7539 | return; |
| 7540 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7541 | |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7542 | // Find all of the methods that we could declare/implement here. |
| 7543 | KnownMethodsMap KnownMethods; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7544 | FindImplementableMethods(Context, SearchDecl, IsInstanceMethod, |
| Douglas Gregor | 1b035bb | 2010-10-18 18:21:28 +0000 | [diff] [blame] | 7545 | ReturnType, KnownMethods); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7546 | |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7547 | // Add declarations or definitions for each of the known methods. |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7548 | typedef CodeCompletionResult Result; |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7549 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7550 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7551 | CodeCompletionContext::CCC_Other); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7552 | Results.EnterNewScope(); |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7553 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7554 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7555 | MEnd = KnownMethods.end(); |
| 7556 | M != MEnd; ++M) { |
| Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 7557 | ObjCMethodDecl *Method = M->second.getPointer(); |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7558 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7559 | Results.getCodeCompletionTUInfo()); |
| Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 7560 | |
| 7561 | // Add the '-'/'+' prefix if it wasn't provided yet. |
| 7562 | if (!IsInstanceMethod) { |
| 7563 | Builder.AddTextChunk(Method->isInstanceMethod() ? "-" : "+"); |
| 7564 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7565 | } |
| 7566 | |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7567 | // If the result type was not already provided, add it to the |
| 7568 | // pattern as (type). |
| Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7569 | if (ReturnType.isNull()) { |
| 7570 | QualType ResTy = Method->getSendResultType().stripObjCKindOfType(Context); |
| 7571 | AttributedType::stripOuterNullability(ResTy); |
| 7572 | AddObjCPassingTypeChunk(ResTy, |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7573 | Method->getObjCDeclQualifier(), Context, Policy, |
| 7574 | Builder); |
| Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7575 | } |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7576 | |
| 7577 | Selector Sel = Method->getSelector(); |
| 7578 | |
| 7579 | // Add the first part of the selector to the pattern. |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7580 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 7581 | Sel.getNameForSlot(0))); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7582 | |
| 7583 | // Add parameters to the pattern. |
| 7584 | unsigned I = 0; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7585 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7586 | PEnd = Method->param_end(); |
| 7587 | P != PEnd; (void)++P, ++I) { |
| 7588 | // Add the part of the selector name. |
| 7589 | if (I == 0) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7590 | Builder.AddTypedTextChunk(":"); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7591 | else if (I < Sel.getNumArgs()) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7592 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7593 | Builder.AddTypedTextChunk( |
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 7594 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7595 | } else |
| 7596 | break; |
| 7597 | |
| 7598 | // Add the parameter type. |
| Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 7599 | QualType ParamType; |
| 7600 | if ((*P)->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 7601 | ParamType = (*P)->getType(); |
| 7602 | else |
| 7603 | ParamType = (*P)->getOriginalType(); |
| Douglas Gregor | 9b7b3e9 | 2015-07-07 06:20:27 +0000 | [diff] [blame] | 7604 | ParamType = ParamType.substObjCTypeArgs(Context, {}, |
| 7605 | ObjCSubstitutionContext::Parameter); |
| Argyrios Kyrtzidis | f0917ab | 2015-07-24 17:00:19 +0000 | [diff] [blame] | 7606 | AttributedType::stripOuterNullability(ParamType); |
| Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 7607 | AddObjCPassingTypeChunk(ParamType, |
| Douglas Gregor | 2997914 | 2012-04-10 18:35:07 +0000 | [diff] [blame] | 7608 | (*P)->getObjCDeclQualifier(), |
| 7609 | Context, Policy, |
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7610 | Builder); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7611 | |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7612 | if (IdentifierInfo *Id = (*P)->getIdentifier()) |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7613 | Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName())); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7614 | } |
| 7615 | |
| 7616 | if (Method->isVariadic()) { |
| 7617 | if (Method->param_size() > 0) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7618 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
| 7619 | Builder.AddTextChunk("..."); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7620 | } |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7621 | |
| Douglas Gregor | d37c59d | 2010-05-28 00:57:46 +0000 | [diff] [blame] | 7622 | if (IsInImplementation && Results.includeCodePatterns()) { |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7623 | // We will be defining the method here, so add a compound statement. |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7624 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7625 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
| 7626 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7627 | if (!Method->getReturnType()->isVoidType()) { |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7628 | // If the result type is not void, add a return clause. |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7629 | Builder.AddTextChunk("return"); |
| 7630 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7631 | Builder.AddPlaceholderChunk("expression"); |
| 7632 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7633 | } else |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7634 | Builder.AddPlaceholderChunk("statements"); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7635 | |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7636 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
| 7637 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7638 | } |
| 7639 | |
| Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 7640 | unsigned Priority = CCP_CodePattern; |
| Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 7641 | if (!M->second.getInt()) |
| Douglas Gregor | 416b575 | 2010-08-25 01:08:01 +0000 | [diff] [blame] | 7642 | Priority += CCD_InBaseClass; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7643 | |
| Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 7644 | Results.AddResult(Result(Builder.TakeString(), Method, Priority)); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7645 | } |
| 7646 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7647 | // 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] | 7648 | // the properties in this class and its categories. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7649 | if (Context.getLangOpts().ObjC2) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7650 | SmallVector<ObjCContainerDecl *, 4> Containers; |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7651 | Containers.push_back(SearchDecl); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7652 | |
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 7653 | VisitedSelectorSet KnownSelectors; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7654 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), |
| Douglas Gregor | d4a8ced | 2011-05-04 23:50:46 +0000 | [diff] [blame] | 7655 | MEnd = KnownMethods.end(); |
| 7656 | M != MEnd; ++M) |
| 7657 | KnownSelectors.insert(M->first); |
| 7658 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7659 | |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7660 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl); |
| 7661 | if (!IFace) |
| 7662 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl)) |
| 7663 | IFace = Category->getClassInterface(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7664 | |
| Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 7665 | if (IFace) |
| 7666 | for (auto *Cat : IFace->visible_categories()) |
| 7667 | Containers.push_back(Cat); |
| Alex Lorenz | b874042 | 2017-10-24 16:39:37 +0000 | [diff] [blame] | 7668 | |
| 7669 | if (IsInstanceMethod) { |
| 7670 | for (unsigned I = 0, N = Containers.size(); I != N; ++I) |
| 7671 | for (auto *P : Containers[I]->instance_properties()) |
| 7672 | AddObjCKeyValueCompletions(P, *IsInstanceMethod, ReturnType, Context, |
| 7673 | KnownSelectors, Results); |
| 7674 | } |
| Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 7675 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7676 | |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7677 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7678 | |
| 7679 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7680 | Results.data(), Results.size()); |
| Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 7681 | } |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7682 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7683 | void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S, |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7684 | bool IsInstanceMethod, |
| Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7685 | bool AtParameterName, |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7686 | ParsedType ReturnTy, |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7687 | ArrayRef<IdentifierInfo *> SelIdents) { |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7688 | // If we have an external source, load the entire class method |
| Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 7689 | // pool from the AST file. |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7690 | if (ExternalSource) { |
| 7691 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); |
| 7692 | I != N; ++I) { |
| 7693 | Selector Sel = ExternalSource->GetExternalSelector(I); |
| Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7694 | if (Sel.isNull() || MethodPool.count(Sel)) |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7695 | continue; |
| Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7696 | |
| 7697 | ReadMethodPool(Sel); |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7698 | } |
| 7699 | } |
| 7700 | |
| 7701 | // Build the set of methods we can see. |
| John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 7702 | typedef CodeCompletionResult Result; |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7703 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7704 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7705 | CodeCompletionContext::CCC_Other); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7706 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7707 | if (ReturnTy) |
| 7708 | Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType()); |
| Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7709 | |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7710 | Results.EnterNewScope(); |
| Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 7711 | for (GlobalMethodPool::iterator M = MethodPool.begin(), |
| 7712 | MEnd = MethodPool.end(); |
| 7713 | M != MEnd; ++M) { |
| 7714 | for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first : |
| 7715 | &M->second.second; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7716 | MethList && MethList->getMethod(); |
| Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 7717 | MethList = MethList->getNext()) { |
| Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7718 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7719 | continue; |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7720 | |
| Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7721 | if (AtParameterName) { |
| 7722 | // Suggest parameter names we've seen before. |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7723 | unsigned NumSelIdents = SelIdents.size(); |
| Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7724 | if (NumSelIdents && |
| 7725 | NumSelIdents <= MethList->getMethod()->param_size()) { |
| 7726 | ParmVarDecl *Param = |
| 7727 | MethList->getMethod()->parameters()[NumSelIdents - 1]; |
| Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7728 | if (Param->getIdentifier()) { |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7729 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7730 | Results.getCodeCompletionTUInfo()); |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7731 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7732 | Param->getIdentifier()->getName())); |
| 7733 | Results.AddResult(Builder.TakeString()); |
| Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7734 | } |
| 7735 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7736 | |
| Douglas Gregor | 4587969 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 7737 | continue; |
| 7738 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7739 | |
| Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 7740 | Result R(MethList->getMethod(), |
| 7741 | Results.getBasePriority(MethList->getMethod()), nullptr); |
| Dmitri Gribenko | 070a10e | 2013-06-16 03:47:57 +0000 | [diff] [blame] | 7742 | R.StartParameter = SelIdents.size(); |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7743 | R.AllParametersAreInformative = false; |
| 7744 | R.DeclaringEntity = true; |
| 7745 | Results.MaybeAddResult(R, CurContext); |
| 7746 | } |
| 7747 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7748 | |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7749 | Results.ExitScope(); |
| Alex Lorenz | 847fda1 | 2017-01-03 11:56:40 +0000 | [diff] [blame] | 7750 | |
| 7751 | if (!AtParameterName && !SelIdents.empty() && |
| 7752 | SelIdents.front()->getName().startswith("init")) { |
| 7753 | for (const auto &M : PP.macros()) { |
| 7754 | if (M.first->getName() != "NS_DESIGNATED_INITIALIZER") |
| 7755 | continue; |
| 7756 | Results.EnterNewScope(); |
| 7757 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7758 | Results.getCodeCompletionTUInfo()); |
| 7759 | Builder.AddTypedTextChunk( |
| 7760 | Builder.getAllocator().CopyString(M.first->getName())); |
| 7761 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), CCP_Macro, |
| 7762 | CXCursor_MacroDefinition)); |
| 7763 | Results.ExitScope(); |
| 7764 | } |
| 7765 | } |
| 7766 | |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7767 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7768 | Results.data(), Results.size()); |
| Douglas Gregor | 95887f9 | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 7769 | } |
| Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 7770 | |
| Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7771 | void Sema::CodeCompletePreprocessorDirective(bool InConditional) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7772 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7773 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7774 | CodeCompletionContext::CCC_PreprocessorDirective); |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7775 | Results.EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7776 | |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7777 | // #if <condition> |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7778 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7779 | Results.getCodeCompletionTUInfo()); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7780 | Builder.AddTypedTextChunk("if"); |
| 7781 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7782 | Builder.AddPlaceholderChunk("condition"); |
| 7783 | Results.AddResult(Builder.TakeString()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7784 | |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7785 | // #ifdef <macro> |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7786 | Builder.AddTypedTextChunk("ifdef"); |
| 7787 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7788 | Builder.AddPlaceholderChunk("macro"); |
| 7789 | Results.AddResult(Builder.TakeString()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7790 | |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7791 | // #ifndef <macro> |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7792 | Builder.AddTypedTextChunk("ifndef"); |
| 7793 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7794 | Builder.AddPlaceholderChunk("macro"); |
| 7795 | Results.AddResult(Builder.TakeString()); |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7796 | |
| 7797 | if (InConditional) { |
| 7798 | // #elif <condition> |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7799 | Builder.AddTypedTextChunk("elif"); |
| 7800 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7801 | Builder.AddPlaceholderChunk("condition"); |
| 7802 | Results.AddResult(Builder.TakeString()); |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7803 | |
| 7804 | // #else |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7805 | Builder.AddTypedTextChunk("else"); |
| 7806 | Results.AddResult(Builder.TakeString()); |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7807 | |
| 7808 | // #endif |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7809 | Builder.AddTypedTextChunk("endif"); |
| 7810 | Results.AddResult(Builder.TakeString()); |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7811 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7812 | |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7813 | // #include "header" |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7814 | Builder.AddTypedTextChunk("include"); |
| 7815 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7816 | Builder.AddTextChunk("\""); |
| 7817 | Builder.AddPlaceholderChunk("header"); |
| 7818 | Builder.AddTextChunk("\""); |
| 7819 | Results.AddResult(Builder.TakeString()); |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7820 | |
| 7821 | // #include <header> |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7822 | Builder.AddTypedTextChunk("include"); |
| 7823 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7824 | Builder.AddTextChunk("<"); |
| 7825 | Builder.AddPlaceholderChunk("header"); |
| 7826 | Builder.AddTextChunk(">"); |
| 7827 | Results.AddResult(Builder.TakeString()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7828 | |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7829 | // #define <macro> |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7830 | Builder.AddTypedTextChunk("define"); |
| 7831 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7832 | Builder.AddPlaceholderChunk("macro"); |
| 7833 | Results.AddResult(Builder.TakeString()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7834 | |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7835 | // #define <macro>(<args>) |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7836 | Builder.AddTypedTextChunk("define"); |
| 7837 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7838 | Builder.AddPlaceholderChunk("macro"); |
| 7839 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7840 | Builder.AddPlaceholderChunk("args"); |
| 7841 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7842 | Results.AddResult(Builder.TakeString()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7843 | |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7844 | // #undef <macro> |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7845 | Builder.AddTypedTextChunk("undef"); |
| 7846 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7847 | Builder.AddPlaceholderChunk("macro"); |
| 7848 | Results.AddResult(Builder.TakeString()); |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7849 | |
| 7850 | // #line <number> |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7851 | Builder.AddTypedTextChunk("line"); |
| 7852 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7853 | Builder.AddPlaceholderChunk("number"); |
| 7854 | Results.AddResult(Builder.TakeString()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7855 | |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7856 | // #line <number> "filename" |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7857 | Builder.AddTypedTextChunk("line"); |
| 7858 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7859 | Builder.AddPlaceholderChunk("number"); |
| 7860 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7861 | Builder.AddTextChunk("\""); |
| 7862 | Builder.AddPlaceholderChunk("filename"); |
| 7863 | Builder.AddTextChunk("\""); |
| 7864 | Results.AddResult(Builder.TakeString()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7865 | |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7866 | // #error <message> |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7867 | Builder.AddTypedTextChunk("error"); |
| 7868 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7869 | Builder.AddPlaceholderChunk("message"); |
| 7870 | Results.AddResult(Builder.TakeString()); |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7871 | |
| 7872 | // #pragma <arguments> |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7873 | Builder.AddTypedTextChunk("pragma"); |
| 7874 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7875 | Builder.AddPlaceholderChunk("arguments"); |
| 7876 | Results.AddResult(Builder.TakeString()); |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7877 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7878 | if (getLangOpts().ObjC1) { |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7879 | // #import "header" |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7880 | Builder.AddTypedTextChunk("import"); |
| 7881 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7882 | Builder.AddTextChunk("\""); |
| 7883 | Builder.AddPlaceholderChunk("header"); |
| 7884 | Builder.AddTextChunk("\""); |
| 7885 | Results.AddResult(Builder.TakeString()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7886 | |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7887 | // #import <header> |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7888 | Builder.AddTypedTextChunk("import"); |
| 7889 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7890 | Builder.AddTextChunk("<"); |
| 7891 | Builder.AddPlaceholderChunk("header"); |
| 7892 | Builder.AddTextChunk(">"); |
| 7893 | Results.AddResult(Builder.TakeString()); |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7894 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7895 | |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7896 | // #include_next "header" |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7897 | Builder.AddTypedTextChunk("include_next"); |
| 7898 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7899 | Builder.AddTextChunk("\""); |
| 7900 | Builder.AddPlaceholderChunk("header"); |
| 7901 | Builder.AddTextChunk("\""); |
| 7902 | Results.AddResult(Builder.TakeString()); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7903 | |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7904 | // #include_next <header> |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7905 | Builder.AddTypedTextChunk("include_next"); |
| 7906 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7907 | Builder.AddTextChunk("<"); |
| 7908 | Builder.AddPlaceholderChunk("header"); |
| 7909 | Builder.AddTextChunk(">"); |
| 7910 | Results.AddResult(Builder.TakeString()); |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7911 | |
| 7912 | // #warning <message> |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7913 | Builder.AddTypedTextChunk("warning"); |
| 7914 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7915 | Builder.AddPlaceholderChunk("message"); |
| 7916 | Results.AddResult(Builder.TakeString()); |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7917 | |
| 7918 | // Note: #ident and #sccs are such crazy anachronisms that we don't provide |
| 7919 | // completions for them. And __include_macros is a Clang-internal extension |
| 7920 | // that we don't want to encourage anyone to use. |
| 7921 | |
| 7922 | // FIXME: we don't support #assert or #unassert, so don't suggest them. |
| 7923 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7924 | |
| 7925 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7926 | Results.data(), Results.size()); |
| 7927 | } |
| 7928 | |
| 7929 | void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) { |
| Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7930 | CodeCompleteOrdinaryName(S, |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7931 | S->getFnParent()? Sema::PCC_RecoveryInFunction |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7932 | : Sema::PCC_Namespace); |
| Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 7933 | } |
| 7934 | |
| Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7935 | void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7936 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7937 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7938 | IsDefinition? CodeCompletionContext::CCC_MacroName |
| 7939 | : CodeCompletionContext::CCC_MacroNameUse); |
| Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7940 | if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7941 | // Add just the names of macros, not their arguments. |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7942 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7943 | Results.getCodeCompletionTUInfo()); |
| Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7944 | Results.EnterNewScope(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7945 | for (Preprocessor::macro_iterator M = PP.macro_begin(), |
| Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7946 | MEnd = PP.macro_end(); |
| 7947 | M != MEnd; ++M) { |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 7948 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7949 | M->first->getName())); |
| Argyrios Kyrtzidis | 5c8b1cd | 2012-09-27 00:24:09 +0000 | [diff] [blame] | 7950 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), |
| 7951 | CCP_CodePattern, |
| 7952 | CXCursor_MacroDefinition)); |
| Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7953 | } |
| 7954 | Results.ExitScope(); |
| 7955 | } else if (IsDefinition) { |
| 7956 | // FIXME: Can we detect when the user just wrote an include guard above? |
| 7957 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7958 | |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7959 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7960 | Results.data(), Results.size()); |
| Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 7961 | } |
| 7962 | |
| Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7963 | void Sema::CodeCompletePreprocessorExpression() { |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7964 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7965 | CodeCompleter->getCodeCompletionTUInfo(), |
| Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 7966 | CodeCompletionContext::CCC_PreprocessorExpression); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7967 | |
| Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7968 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
| Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 7969 | AddMacroResults(PP, Results, |
| 7970 | CodeCompleter ? CodeCompleter->loadExternal() : false, |
| 7971 | true); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7972 | |
| Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7973 | // defined (<macro>) |
| 7974 | Results.EnterNewScope(); |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 7975 | CodeCompletionBuilder Builder(Results.getAllocator(), |
| 7976 | Results.getCodeCompletionTUInfo()); |
| Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 7977 | Builder.AddTypedTextChunk("defined"); |
| 7978 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
| 7979 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
| 7980 | Builder.AddPlaceholderChunk("macro"); |
| 7981 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
| 7982 | Results.AddResult(Builder.TakeString()); |
| Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7983 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 7984 | |
| 7985 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 7986 | Results.data(), Results.size()); |
| Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7987 | } |
| 7988 | |
| 7989 | void Sema::CodeCompletePreprocessorMacroArgument(Scope *S, |
| 7990 | IdentifierInfo *Macro, |
| 7991 | MacroInfo *MacroInfo, |
| 7992 | unsigned Argument) { |
| 7993 | // FIXME: In the future, we could provide "overload" results, much like we |
| 7994 | // do for function calls. |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 7995 | |
| Argyrios Kyrtzidis | 75f6cd2 | 2011-08-18 19:41:28 +0000 | [diff] [blame] | 7996 | // Now just ignore this. There will be another code-completion callback |
| 7997 | // for the expanded tokens. |
| Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 7998 | } |
| 7999 | |
| Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 8000 | // This handles completion inside an #include filename, e.g. #include <foo/ba |
| 8001 | // We look for the directory "foo" under each directory on the include path, |
| 8002 | // list its files, and reassemble the appropriate #include. |
| 8003 | void Sema::CodeCompleteIncludedFile(llvm::StringRef Dir, bool Angled) { |
| 8004 | // RelDir should use /, but unescaped \ is possible on windows! |
| 8005 | // Our completions will normalize to / for simplicity, this case is rare. |
| 8006 | std::string RelDir = llvm::sys::path::convert_to_slash(Dir); |
| 8007 | // We need the native slashes for the actual file system interactions. |
| 8008 | SmallString<128> NativeRelDir = StringRef(RelDir); |
| 8009 | llvm::sys::path::native(NativeRelDir); |
| 8010 | auto FS = getSourceManager().getFileManager().getVirtualFileSystem(); |
| 8011 | |
| 8012 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 8013 | CodeCompleter->getCodeCompletionTUInfo(), |
| 8014 | CodeCompletionContext::CCC_IncludedFile); |
| 8015 | llvm::DenseSet<StringRef> SeenResults; // To deduplicate results. |
| 8016 | |
| 8017 | // Helper: adds one file or directory completion result. |
| 8018 | auto AddCompletion = [&](StringRef Filename, bool IsDirectory) { |
| 8019 | SmallString<64> TypedChunk = Filename; |
| 8020 | // Directory completion is up to the slash, e.g. <sys/ |
| 8021 | TypedChunk.push_back(IsDirectory ? '/' : Angled ? '>' : '"'); |
| 8022 | auto R = SeenResults.insert(TypedChunk); |
| 8023 | if (R.second) { // New completion |
| 8024 | const char *InternedTyped = Results.getAllocator().CopyString(TypedChunk); |
| 8025 | *R.first = InternedTyped; // Avoid dangling StringRef. |
| 8026 | CodeCompletionBuilder Builder(CodeCompleter->getAllocator(), |
| 8027 | CodeCompleter->getCodeCompletionTUInfo()); |
| 8028 | Builder.AddTypedTextChunk(InternedTyped); |
| 8029 | // The result is a "Pattern", which is pretty opaque. |
| 8030 | // We may want to include the real filename to allow smart ranking. |
| 8031 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
| 8032 | } |
| 8033 | }; |
| 8034 | |
| 8035 | // Helper: scans IncludeDir for nice files, and adds results for each. |
| 8036 | auto AddFilesFromIncludeDir = [&](StringRef IncludeDir, bool IsSystem) { |
| 8037 | llvm::SmallString<128> Dir = IncludeDir; |
| 8038 | if (!NativeRelDir.empty()) |
| 8039 | llvm::sys::path::append(Dir, NativeRelDir); |
| 8040 | |
| 8041 | std::error_code EC; |
| 8042 | unsigned Count = 0; |
| 8043 | for (auto It = FS->dir_begin(Dir, EC); |
| 8044 | !EC && It != vfs::directory_iterator(); It.increment(EC)) { |
| 8045 | if (++Count == 2500) // If we happen to hit a huge directory, |
| 8046 | break; // bail out early so we're not too slow. |
| 8047 | StringRef Filename = llvm::sys::path::filename(It->path()); |
| 8048 | switch (It->type()) { |
| 8049 | case llvm::sys::fs::file_type::directory_file: |
| 8050 | AddCompletion(Filename, /*IsDirectory=*/true); |
| 8051 | break; |
| 8052 | case llvm::sys::fs::file_type::regular_file: |
| 8053 | // Only files that really look like headers. (Except in system dirs). |
| 8054 | if (!IsSystem) { |
| 8055 | // Header extensions from Types.def, which we can't depend on here. |
| 8056 | if (!(Filename.endswith_lower(".h") || |
| 8057 | Filename.endswith_lower(".hh") || |
| 8058 | Filename.endswith_lower(".hpp") || |
| 8059 | Filename.endswith_lower(".inc"))) |
| 8060 | break; |
| 8061 | } |
| 8062 | AddCompletion(Filename, /*IsDirectory=*/false); |
| 8063 | break; |
| 8064 | default: |
| 8065 | break; |
| 8066 | } |
| 8067 | } |
| 8068 | }; |
| 8069 | |
| 8070 | // Helper: adds results relative to IncludeDir, if possible. |
| 8071 | auto AddFilesFromDirLookup = [&](const DirectoryLookup &IncludeDir, |
| 8072 | bool IsSystem) { |
| 8073 | llvm::SmallString<128> Dir; |
| 8074 | switch (IncludeDir.getLookupType()) { |
| 8075 | case DirectoryLookup::LT_HeaderMap: |
| 8076 | // header maps are not (currently) enumerable. |
| 8077 | break; |
| 8078 | case DirectoryLookup::LT_NormalDir: |
| 8079 | AddFilesFromIncludeDir(IncludeDir.getDir()->getName(), IsSystem); |
| 8080 | break; |
| 8081 | case DirectoryLookup::LT_Framework: |
| 8082 | AddFilesFromIncludeDir(IncludeDir.getFrameworkDir()->getName(), IsSystem); |
| 8083 | break; |
| 8084 | } |
| 8085 | }; |
| 8086 | |
| 8087 | // Finally with all our helpers, we can scan the include path. |
| 8088 | // Do this in standard order so deduplication keeps the right file. |
| 8089 | // (In case we decide to add more details to the results later). |
| 8090 | const auto &S = PP.getHeaderSearchInfo(); |
| 8091 | using llvm::make_range; |
| 8092 | if (!Angled) { |
| 8093 | // The current directory is on the include path for "quoted" includes. |
| 8094 | auto *CurFile = PP.getCurrentFileLexer()->getFileEntry(); |
| 8095 | if (CurFile && CurFile->getDir()) |
| 8096 | AddFilesFromIncludeDir(CurFile->getDir()->getName(), false); |
| 8097 | for (const auto &D : make_range(S.quoted_dir_begin(), S.quoted_dir_end())) |
| 8098 | AddFilesFromDirLookup(D, false); |
| 8099 | } |
| 8100 | for (const auto &D : make_range(S.angled_dir_begin(), S.angled_dir_end())) |
| 8101 | AddFilesFromDirLookup(D, true); |
| 8102 | for (const auto &D : make_range(S.system_dir_begin(), S.system_dir_end())) |
| 8103 | AddFilesFromDirLookup(D, true); |
| 8104 | |
| 8105 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 8106 | Results.data(), Results.size()); |
| 8107 | } |
| 8108 | |
| Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 8109 | void Sema::CodeCompleteNaturalLanguage() { |
| Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 8110 | HandleCodeCompleteResults(this, CodeCompleter, |
| Douglas Gregor | ea73637 | 2010-08-25 17:10:00 +0000 | [diff] [blame] | 8111 | CodeCompletionContext::CCC_NaturalLanguage, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8112 | nullptr, 0); |
| Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 8113 | } |
| 8114 | |
| Alex Lorenz | f7f6f82 | 2017-05-09 16:05:04 +0000 | [diff] [blame] | 8115 | void Sema::CodeCompleteAvailabilityPlatformName() { |
| 8116 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
| 8117 | CodeCompleter->getCodeCompletionTUInfo(), |
| 8118 | CodeCompletionContext::CCC_Other); |
| 8119 | Results.EnterNewScope(); |
| 8120 | static const char *Platforms[] = {"macOS", "iOS", "watchOS", "tvOS"}; |
| 8121 | for (const char *Platform : llvm::makeArrayRef(Platforms)) { |
| 8122 | Results.AddResult(CodeCompletionResult(Platform)); |
| 8123 | Results.AddResult(CodeCompletionResult(Results.getAllocator().CopyString( |
| 8124 | Twine(Platform) + "ApplicationExtension"))); |
| 8125 | } |
| 8126 | Results.ExitScope(); |
| Eric Liu | f5ba09f | 2018-07-04 10:01:18 +0000 | [diff] [blame] | 8127 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
| 8128 | Results.data(), Results.size()); |
| Alex Lorenz | f7f6f82 | 2017-05-09 16:05:04 +0000 | [diff] [blame] | 8129 | } |
| 8130 | |
| Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 8131 | void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator, |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8132 | CodeCompletionTUInfo &CCTUInfo, |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8133 | SmallVectorImpl<CodeCompletionResult> &Results) { |
| Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 8134 | ResultBuilder Builder(*this, Allocator, CCTUInfo, |
| 8135 | CodeCompletionContext::CCC_Recovery); |
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 8136 | if (!CodeCompleter || CodeCompleter->includeGlobals()) { |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8137 | CodeCompletionDeclConsumer Consumer(Builder, |
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 8138 | Context.getTranslationUnitDecl()); |
| Sam McCall | bb2cf63 | 2018-01-12 14:51:47 +0000 | [diff] [blame] | 8139 | LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName, |
| 8140 | Consumer, |
| 8141 | !CodeCompleter || CodeCompleter->loadExternal()); |
| Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 8142 | } |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8143 | |
| Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 8144 | if (!CodeCompleter || CodeCompleter->includeMacros()) |
| Eric Liu | 88de9f6 | 2018-09-19 09:34:55 +0000 | [diff] [blame] | 8145 | AddMacroResults(PP, Builder, |
| 8146 | CodeCompleter ? CodeCompleter->loadExternal() : false, |
| 8147 | true); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8148 | |
| Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 8149 | Results.clear(); |
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 8150 | Results.insert(Results.end(), |
| Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 8151 | Builder.data(), Builder.data() + Builder.size()); |
| 8152 | } |