forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftLangSupport.h
374 lines (300 loc) · 14.1 KB
/
SwiftLangSupport.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//===--- SwiftLangSupport.h - ------------------------------------*- C++ -*-==//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_SOURCEKIT_LIB_SWIFTLANG_SWIFTLANGSUPPORT_H
#define LLVM_SOURCEKIT_LIB_SWIFTLANG_SWIFTLANGSUPPORT_H
#include "CodeCompletion.h"
#include "SwiftInterfaceGenContext.h"
#include "SourceKit/Core/LangSupport.h"
#include "SourceKit/Support/Concurrency.h"
#include "SourceKit/Support/ThreadSafeRefCntPtr.h"
#include "SourceKit/Support/Tracing.h"
#include "swift/Basic/ThreadSafeRefCounted.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Mutex.h"
#include <map>
#include <string>
namespace swift {
class ASTContext;
class ClangModuleLoader;
class CompilerInstance;
class CompilerInvocation;
class Decl;
class AbstractStorageDecl;
class SourceFile;
class ValueDecl;
enum class AccessorKind;
namespace ide {
class CodeCompletionCache;
class OnDiskCodeCompletionCache;
enum class CodeCompletionDeclKind;
enum class SyntaxNodeKind : uint8_t;
enum class SyntaxStructureKind : uint8_t;
enum class SyntaxStructureElementKind : uint8_t;
class CodeCompletionConsumer;
}
}
namespace SourceKit {
class ImmutableTextSnapshot;
typedef RefPtr<ImmutableTextSnapshot> ImmutableTextSnapshotRef;
class SwiftASTManager;
class SwiftLangSupport;
class SwiftEditorDocument :
public ThreadSafeRefCountedBase<SwiftEditorDocument> {
struct Implementation;
Implementation &Impl;
size_t getLineOffset(unsigned LineIndex);
size_t getTrimmedLineOffset(unsigned LineIndex);
public:
struct CodeFormatOptions;
SwiftEditorDocument(StringRef FilePath, SwiftLangSupport &LangSupport);
~SwiftEditorDocument();
ImmutableTextSnapshotRef initializeText(llvm::MemoryBuffer *Buf,
ArrayRef<const char *> Args);
ImmutableTextSnapshotRef replaceText(unsigned Offset, unsigned Length,
llvm::MemoryBuffer *Buf,
bool ProvideSemanticInfo);
void updateSemaInfo();
void removeCachedAST();
ImmutableTextSnapshotRef getLatestSnapshot() const;
void parse(ImmutableTextSnapshotRef Snapshot, SwiftLangSupport &Lang);
void readSyntaxInfo(EditorConsumer& consumer);
void readSemanticInfo(ImmutableTextSnapshotRef Snapshot,
EditorConsumer& Consumer);
void applyFormatOptions(OptionsDictionary &FmtOptions);
void formatText(unsigned Line, unsigned Length, EditorConsumer &Consumer);
void expandPlaceholder(unsigned Offset, unsigned Length,
EditorConsumer &Consumer);
StringRef getTrimmedTextForLine(unsigned Line);
size_t getExpandedIndentForLine(unsigned LineIndex);
const CodeFormatOptions &getFormatOptions();
static void reportDocumentStructure(swift::SourceFile &SrcFile,
EditorConsumer &Consumer);
};
typedef IntrusiveRefCntPtr<SwiftEditorDocument> SwiftEditorDocumentRef;
class SwiftEditorDocumentFileMap {
WorkQueue Queue{ WorkQueue::Dequeuing::Concurrent,
"sourcekit.swift.EditorDocFileMap" };
struct DocInfo {
SwiftEditorDocumentRef DocRef;
std::string ResolvedPath;
};
llvm::StringMap<DocInfo> Docs;
public:
bool getOrUpdate(StringRef FilePath,
SwiftLangSupport &LangSupport,
SwiftEditorDocumentRef &EditorDoc);
/// Looks up the document only by the path name that was given initially.
SwiftEditorDocumentRef getByUnresolvedName(StringRef FilePath);
/// Looks up the document by resolving symlinks in the paths.
SwiftEditorDocumentRef findByPath(StringRef FilePath);
SwiftEditorDocumentRef remove(StringRef FilePath);
};
namespace CodeCompletion {
/// Provides a thread-safe cache for code completion results that remain valid
/// for the duration of a 'session' - for example, from the point that a user
/// invokes code completion until they accept a completion, or otherwise close
/// the list of completions.
///
/// The contents of the cache can be modified asynchronously during the session,
/// but the contained objects are immutable.
class SessionCache : public ThreadSafeRefCountedBase<SessionCache> {
std::unique_ptr<llvm::MemoryBuffer> buffer;
std::vector<std::string> args;
CompletionSink sink;
std::vector<Completion *> sortedCompletions;
CompletionKind completionKind;
llvm::sys::Mutex mtx;
public:
SessionCache(CompletionSink &&sink,
std::unique_ptr<llvm::MemoryBuffer> &&buffer,
std::vector<std::string> &&args, CompletionKind completionKind)
: buffer(std::move(buffer)), args(std::move(args)), sink(std::move(sink)),
completionKind(completionKind) {}
void setSortedCompletions(std::vector<Completion *> &&completions);
ArrayRef<Completion *> getSortedCompletions();
llvm::MemoryBuffer *getBuffer();
ArrayRef<std::string> getCompilerArgs();
CompletionKind getCompletionKind();
};
typedef RefPtr<SessionCache> SessionCacheRef;
/// A thread-safe map from (buffer, code complete offset) to \c SessionCache.
class SessionCacheMap {
mutable unsigned nextBufferID = 0;
mutable llvm::StringMap<unsigned> nameToBufferMap;
typedef std::pair<unsigned, unsigned> Key;
llvm::DenseMap<Key, SessionCacheRef> sessions;
mutable llvm::sys::Mutex mtx;
// Should only be called with Mtx locked.
unsigned getBufferID(StringRef name) const;
public:
SessionCacheRef get(StringRef name, unsigned offset) const;
bool set(StringRef name, unsigned offset, SessionCacheRef session);
bool remove(StringRef name, unsigned offset);
};
} // end namespace CodeCompletion
class SwiftInterfaceGenMap {
llvm::StringMap<SwiftInterfaceGenContextRef> IFaceGens;
mutable llvm::sys::Mutex Mtx;
public:
SwiftInterfaceGenContextRef get(StringRef Name) const;
void set(StringRef Name, SwiftInterfaceGenContextRef IFaceGen);
bool remove(StringRef Name);
SwiftInterfaceGenContextRef find(StringRef ModuleName,
const swift::CompilerInvocation &Invok);
};
struct SwiftCompletionCache
: public ThreadSafeRefCountedBase<SwiftCompletionCache> {
std::unique_ptr<swift::ide::CodeCompletionCache> inMemory;
std::unique_ptr<swift::ide::OnDiskCodeCompletionCache> onDisk;
swift::ide::CodeCompletionCache &getCache();
SwiftCompletionCache() = default;
~SwiftCompletionCache();
};
struct SwiftPopularAPI : public ThreadSafeRefCountedBase<SwiftPopularAPI> {
llvm::StringMap<CodeCompletion::PopularityFactor> nameToFactor;
};
struct SwiftCustomCompletions
: public ThreadSafeRefCountedBase<SwiftCustomCompletions> {
std::vector<CustomCompletionInfo> customCompletions;
};
class SwiftLangSupport : public LangSupport {
SourceKit::Context &SKCtx;
std::string RuntimeResourcePath;
std::unique_ptr<SwiftASTManager> ASTMgr;
SwiftEditorDocumentFileMap EditorDocuments;
SwiftInterfaceGenMap IFaceGenContexts;
ThreadSafeRefCntPtr<SwiftCompletionCache> CCCache;
ThreadSafeRefCntPtr<SwiftPopularAPI> PopularAPI;
CodeCompletion::SessionCacheMap CCSessions;
ThreadSafeRefCntPtr<SwiftCustomCompletions> CustomCompletions;
public:
explicit SwiftLangSupport(SourceKit::Context &SKCtx);
~SwiftLangSupport();
SourceKit::Context &getContext() { return SKCtx; }
StringRef getRuntimeResourcePath() const { return RuntimeResourcePath; }
SwiftASTManager &getASTManager() { return *ASTMgr; }
SwiftEditorDocumentFileMap &getEditorDocuments() { return EditorDocuments; }
SwiftInterfaceGenMap &getIFaceGenContexts() { return IFaceGenContexts; }
IntrusiveRefCntPtr<SwiftCompletionCache> getCodeCompletionCache() {
return CCCache;
}
static SourceKit::UIdent getUIDForDecl(const swift::Decl *D,
bool IsRef = false);
static SourceKit::UIdent getUIDForLocalVar(bool IsRef = false);
static SourceKit::UIdent getUIDForCodeCompletionDeclKind(
swift::ide::CodeCompletionDeclKind Kind, bool IsRef = false);
static SourceKit::UIdent getUIDForAccessor(const swift::ValueDecl *D,
swift::AccessorKind AccKind,
bool IsRef = false);
static SourceKit::UIdent getUIDForModuleRef();
static SourceKit::UIdent getUIDForSyntaxNodeKind(
swift::ide::SyntaxNodeKind Kind);
static SourceKit::UIdent getUIDForSyntaxStructureKind(
swift::ide::SyntaxStructureKind Kind);
static SourceKit::UIdent getUIDForSyntaxStructureElementKind(
swift::ide::SyntaxStructureElementKind Kind);
static bool printDisplayName(const swift::ValueDecl *D, llvm::raw_ostream &OS);
/// Generate a USR for a Decl, including the prefix.
/// \returns true if the results should be ignored, false otherwise.
static bool printUSR(const swift::ValueDecl *D, llvm::raw_ostream &OS);
/// Generate a USR for an accessor, including the prefix.
/// \returns true if the results should be ignored, false otherwise.
static bool printAccessorUSR(const swift::AbstractStorageDecl *D,
swift::AccessorKind AccKind,
llvm::raw_ostream &OS);
/// Tries to resolve the path to the real file-system path. If it fails it
/// returns the original path;
static std::string resolvePathSymlinks(StringRef FilePath);
//==========================================================================//
// LangSupport Interface
//==========================================================================//
void indexSource(StringRef Filename, IndexingConsumer &Consumer,
ArrayRef<const char *> Args, StringRef Hash) override;
void codeComplete(llvm::MemoryBuffer *InputBuf, unsigned Offset,
SourceKit::CodeCompletionConsumer &Consumer,
ArrayRef<const char *> Args) override;
void codeCompleteOpen(StringRef name, llvm::MemoryBuffer *inputBuf,
unsigned offset,
OptionsDictionary *options,
GroupedCodeCompletionConsumer &consumer,
ArrayRef<const char *> args) override;
void codeCompleteClose(StringRef name, unsigned offset,
GroupedCodeCompletionConsumer &consumer) override;
void codeCompleteUpdate(StringRef name, unsigned offset,
OptionsDictionary *options,
GroupedCodeCompletionConsumer &consumer) override;
void codeCompleteCacheOnDisk(StringRef path) override;
void codeCompleteSetPopularAPI(ArrayRef<const char *> popularAPI,
ArrayRef<const char *> unpopularAPI) override;
void
codeCompleteSetCustom(ArrayRef<CustomCompletionInfo> completions) override;
void editorOpen(StringRef Name, llvm::MemoryBuffer *Buf, bool EnableSyntaxMap,
EditorConsumer &Consumer,
ArrayRef<const char *> Args) override;
void editorOpenInterface(EditorConsumer &Consumer,
StringRef Name,
StringRef ModuleName,
ArrayRef<const char *> Args) override;
void editorOpenHeaderInterface(EditorConsumer &Consumer,
StringRef Name,
StringRef HeaderName,
ArrayRef<const char *> Args) override;
void editorOpenSwiftSourceInterface(StringRef Name,
StringRef SourceName,
ArrayRef<const char *> Args,
std::shared_ptr<EditorConsumer> Consumer) override;
void editorClose(StringRef Name, bool RemoveCache) override;
void editorReplaceText(StringRef Name, llvm::MemoryBuffer *Buf,
unsigned Offset, unsigned Length,
EditorConsumer &Consumer) override;
void editorApplyFormatOptions(StringRef Name,
OptionsDictionary &FmtOptions) override;
void editorFormatText(StringRef Name, unsigned Line, unsigned Length,
EditorConsumer &Consumer) override;
void editorExtractTextFromComment(StringRef Source,
EditorConsumer &Consumer) override;
void editorExpandPlaceholder(StringRef Name, unsigned Offset, unsigned Length,
EditorConsumer &Consumer) override;
void getCursorInfo(StringRef Filename, unsigned Offset,
ArrayRef<const char *> Args,
std::function<void(const CursorInfo &)> Receiver) override;
void findRelatedIdentifiersInFile(StringRef Filename, unsigned Offset,
ArrayRef<const char *> Args,
std::function<void(const RelatedIdentsInfo &)> Receiver) override;
void getDocInfo(llvm::MemoryBuffer *InputBuf,
StringRef ModuleName,
ArrayRef<const char *> Args,
DocInfoConsumer &Consumer) override;
llvm::Optional<std::pair<unsigned, unsigned>>
findUSRRange(StringRef DocumentName, StringRef USR) override;
void findInterfaceDocument(StringRef ModuleName, ArrayRef<const char *> Args,
std::function<void(const InterfaceDocInfo &)> Receiver) override;
};
namespace trace {
void initTraceInfo(trace::SwiftInvocation &SwiftArgs,
StringRef InputFile,
ArrayRef<const char *> Args);
void initTraceFiles(trace::SwiftInvocation &SwiftArgs,
swift::CompilerInstance &CI);
}
/// When we cannot build any more clang modules, close the .pcm / files to
/// prevent fd leaks in clients that cache the AST.
// FIXME: Remove this once rdar://problem/19720334 is complete.
class CloseClangModuleFiles {
swift::ClangModuleLoader &loader;
public:
CloseClangModuleFiles(swift::ClangModuleLoader &loader) : loader(loader) {}
~CloseClangModuleFiles();
};
} // namespace SourceKit.
#endif