File: SwiftLangSupport.h

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (811 lines) | stat: -rw-r--r-- 34,319 bytes parent folder | download
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
//===--- SwiftLangSupport.h - -----------------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://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 "SourceKit/Core/Context.h"
#include "SourceKit/Core/LangSupport.h"
#include "SourceKit/Support/Concurrency.h"
#include "SourceKit/Support/Statistic.h"
#include "SourceKit/Support/ThreadSafeRefCntPtr.h"
#include "SourceKit/Support/Tracing.h"
#include "SwiftInterfaceGenContext.h"
#include "swift/AST/DiagnosticConsumer.h"
#include "swift/AST/PluginRegistry.h"
#include "swift/Basic/ThreadSafeRefCounted.h"
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
#include "swift/IDE/CancellableResult.h"
#include "swift/IDE/Indenting.h"
#include "swift/IDETool/CompileInstance.h"
#include "swift/IDETool/IDEInspectionInstance.h"
#include "swift/Index/IndexSymbol.h"
#include "swift/Refactoring/Refactoring.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 Type;
  class AbstractStorageDecl;
  class SourceFile;
  class SILOptions;
  class ValueDecl;
  class GenericSignature;
  enum class AccessorKind;

namespace ide {
  class CodeCompletionCache;
  class IDEInspectionInstance;
  class OnDiskCodeCompletionCache;
  class SourceEditConsumer;
  class SyntacticMacroExpansion;
  enum class CodeCompletionDeclKind : uint8_t;
  enum class SyntaxNodeKind : uint8_t;
  enum class SyntaxStructureKind : uint8_t;
  enum class SyntaxStructureElementKind : uint8_t;
  enum class RangeKind : int8_t;
  class CodeCompletionConsumer;

  enum class NameKind {
    ObjC,
    Swift,
  };
}
}

namespace SourceKit {
  class FileSystemProvider;
  class ImmutableTextSnapshot;
  typedef RefPtr<ImmutableTextSnapshot> ImmutableTextSnapshotRef;
  class SwiftASTManager;
  class SwiftLangSupport;
  class Context;
  class NotificationCenter;

  using TypeContextKind = swift::ide::CodeCompletionContext::TypeContextKind;

class SwiftEditorDocument :
    public ThreadSafeRefCountedBase<SwiftEditorDocument> {

  struct Implementation;
  Implementation &Impl;

public:

  SwiftEditorDocument(StringRef FilePath, SwiftLangSupport &LangSupport,
       llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fileSystem,
       swift::ide::CodeFormatOptions Options = swift::ide::CodeFormatOptions());
  ~SwiftEditorDocument();

  ImmutableTextSnapshotRef
  initializeText(llvm::MemoryBuffer *Buf, ArrayRef<const char *> Args,
                 bool ProvideSemanticInfo,
                 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fileSystem);
  ImmutableTextSnapshotRef replaceText(unsigned Offset, unsigned Length,
                                       llvm::MemoryBuffer *Buf,
                                       bool ProvideSemanticInfo,
                                       std::string &error);

  void updateSemaInfo(SourceKitCancellationToken CancellationToken);

  void removeCachedAST();
  void cancelBuildsForCachedAST();

  ImmutableTextSnapshotRef getLatestSnapshot() const;

  void resetSyntaxInfo(ImmutableTextSnapshotRef Snapshot,
                       SwiftLangSupport &Lang);
  void readSyntaxInfo(EditorConsumer &consumer, bool ReportDiags);
  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);
  const swift::ide::CodeFormatOptions &getFormatOptions();

  static void reportDocumentStructure(swift::SourceFile &SrcFile,
                                      EditorConsumer &Consumer);

  std::string getFilePath() const;
      
  /// Returns the virtual filesystem associated with this document.
  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> getFileSystem() const;
};

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.
  /// If \p IsRealpath is \c true, then \p FilePath must already be
  /// canonicalized to a realpath.
  SwiftEditorDocumentRef findByPath(StringRef FilePath,
                                    bool IsRealpath = false);
  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;
  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fileSystem;
  CompletionSink sink;
  std::vector<Completion *> sortedCompletions;
  CompletionKind completionKind;
  TypeContextKind typeContextKind;
  bool completionMayUseImplicitMemberExpr;
  FilterRules filterRules;
  llvm::sys::Mutex mtx;

public:
  SessionCache(CompletionSink &&sink,
               std::unique_ptr<llvm::MemoryBuffer> &&buffer,
               std::vector<std::string> &&args,
               llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fileSystem,
               CompletionKind completionKind,
               TypeContextKind typeContextKind, bool mayUseImplicitMemberExpr,
               FilterRules filterRules)
      : buffer(std::move(buffer)), args(std::move(args)),
        fileSystem(std::move(fileSystem)), sink(std::move(sink)),
        completionKind(completionKind), typeContextKind(typeContextKind),
        completionMayUseImplicitMemberExpr(mayUseImplicitMemberExpr),
        filterRules(std::move(filterRules)) {}
  void setSortedCompletions(std::vector<Completion *> &&completions);
  ArrayRef<Completion *> getSortedCompletions();
  llvm::MemoryBuffer *getBuffer();
  ArrayRef<std::string> getCompilerArgs();
  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> getFileSystem();
  const FilterRules &getFilterRules();
  CompletionKind getCompletionKind();
  TypeContextKind getCompletionTypeContextKind();
  bool getCompletionMayUseImplicitMemberExpr();
};
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

namespace TypeContextInfo {
struct Options {
  // TypeContextInfo doesn't receive any options at this point.
};
} // namespace TypeContextInfo

namespace ConformingMethodList {
struct Options {
  // ConformingMethodList doesn't receive any options at this point.
};
} // namespace ConformingMethodList

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 RequestRefactoringEditConsumer: public swift::ide::SourceEditConsumer,
                                      public swift::DiagnosticConsumer {
  class Implementation;
  Implementation &Impl;
public:
  RequestRefactoringEditConsumer(CategorizedEditsReceiver Receiver);
  ~RequestRefactoringEditConsumer();
  void accept(swift::SourceManager &SM, swift::ide::RegionType RegionType,
              ArrayRef<swift::ide::Replacement> Replacements) override;
  void handleDiagnostic(swift::SourceManager &SM,
                        const swift::DiagnosticInfo &Info) override;
};

namespace compile {
class Session {
  swift::ide::CompileInstance Compiler;

public:
  Session(const std::string &SwiftExecutablePath,
          const std::string &RuntimeResourcePath,
          const std::string &DiagnosticDocumentationPath,
          std::shared_ptr<swift::PluginRegistry> Plugins)
      : Compiler(SwiftExecutablePath, RuntimeResourcePath,
                 DiagnosticDocumentationPath, Plugins) {}

  bool
  performCompile(llvm::ArrayRef<const char *> Args,
                 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
                 swift::DiagnosticConsumer *DiagC,
                 std::shared_ptr<std::atomic<bool>> CancellationFlag) {
    return Compiler.performCompile(Args, FileSystem, DiagC, CancellationFlag);
  }
};

class SessionManager {
  const std::string &SwiftExecutablePath;
  const std::string &RuntimeResourcePath;
  const std::string &DiagnosticDocumentationPath;
  const std::shared_ptr<swift::PluginRegistry> Plugins;

  llvm::StringMap<std::shared_ptr<Session>> sessions;
  WorkQueue compileQueue{WorkQueue::Dequeuing::Concurrent,
                         "sourcekit.swift.Compile"};
  mutable llvm::sys::Mutex mtx;

public:
  SessionManager(const std::string &SwiftExecutablePath,
                 const std::string &RuntimeResourcePath,
                 const std::string &DiagnosticDocumentationPath,
                 const std::shared_ptr<swift::PluginRegistry> Plugins)
      : SwiftExecutablePath(SwiftExecutablePath),
        RuntimeResourcePath(RuntimeResourcePath),
        DiagnosticDocumentationPath(DiagnosticDocumentationPath),
        Plugins(Plugins) {}

  std::shared_ptr<Session> getSession(StringRef name);

  void clearSession(StringRef name);

  void performCompileAsync(
      StringRef Name, ArrayRef<const char *> Args,
      llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fileSystem,
      std::shared_ptr<std::atomic<bool>> CancellationFlag,
      std::function<void(const RequestResult<CompilationResult> &)> Receiver);
};
} // namespace compile

struct SwiftStatistics {
#define SWIFT_STATISTIC(VAR, UID, DESC)                                        \
  Statistic VAR{UIdent{"source.statistic." #UID}, DESC};
#include "SwiftStatistics.def"
};

class SwiftLangSupport : public LangSupport {
  std::shared_ptr<NotificationCenter> NotificationCtr;
  /// The path of the swift-frontend executable.
  /// Used to find clang relative to it.
  std::string SwiftExecutablePath;
  std::string RuntimeResourcePath;
  std::string DiagnosticDocumentationPath;
  std::shared_ptr<SwiftASTManager> ASTMgr;
  std::shared_ptr<SwiftEditorDocumentFileMap> EditorDocuments;
  std::shared_ptr<RequestTracker> ReqTracker;
  SwiftInterfaceGenMap IFaceGenContexts;
  ThreadSafeRefCntPtr<SwiftCompletionCache> CCCache;
  ThreadSafeRefCntPtr<SwiftPopularAPI> PopularAPI;
  CodeCompletion::SessionCacheMap CCSessions;
  ThreadSafeRefCntPtr<SwiftCustomCompletions> CustomCompletions;
  std::shared_ptr<SwiftStatistics> Stats;
  llvm::StringMap<std::unique_ptr<FileSystemProvider>> FileSystemProviders;
  std::shared_ptr<swift::ide::IDEInspectionInstance> IDEInspectionInst;
  std::shared_ptr<compile::SessionManager> CompileManager;
  std::shared_ptr<swift::ide::SyntacticMacroExpansion> SyntacticMacroExpansions;

public:
  explicit SwiftLangSupport(SourceKit::Context &SKCtx);
  ~SwiftLangSupport();

  std::shared_ptr<NotificationCenter> getNotificationCenter() const {
    return NotificationCtr;
  }

  StringRef getRuntimeResourcePath() const { return RuntimeResourcePath; }
  StringRef getDiagnosticDocumentationPath() const {
    return DiagnosticDocumentationPath;
  }

  std::shared_ptr<SwiftASTManager> getASTManager() { return ASTMgr; }

  std::shared_ptr<SwiftEditorDocumentFileMap> getEditorDocuments() { return EditorDocuments; }
  SwiftInterfaceGenMap &getIFaceGenContexts() { return IFaceGenContexts; }
  IntrusiveRefCntPtr<SwiftCompletionCache> getCodeCompletionCache() {
    return CCCache;
  }

  std::shared_ptr<swift::ide::IDEInspectionInstance>
  getIDEInspectionInstance() {
    return IDEInspectionInst;
  }

  /// Returns the FileSystemProvider registered under Name, or nullptr if not
  /// found.
  FileSystemProvider *getFileSystemProvider(StringRef Name);

  /// Registers the given FileSystemProvider under Name. The caller is
  /// responsible for keeping FileSystemProvider alive at least as long as
  /// this Context.
  /// This should only be called during setup because it is not synchronized.
  /// \param FileSystemProvider must be non-null
  void setFileSystemProvider(StringRef Name, std::unique_ptr<FileSystemProvider> FileSystemProvider);

  /// Returns the filesystem appropriate to use in a request with the given
  /// \p vfsOptions and \p primaryFile.
  ///
  /// If \p vfsOptions is provided, returns a virtual filesystem created from
  /// a registered FileSystemProvider, or nullptr if there is an error.
  ///
  /// If \p vfsOptions is None and \p primaryFile is provided, returns the
  /// virtual filesystem associated with that editor document, if any.
  ///
  /// Otherwise, returns the real filesystem.
  ///
  /// \param vfsOptions Options to select and initialize the VFS, or None to
  ///                   get the real file system.
  /// \param error Set to a description of the error, if appropriate.
  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
  getFileSystem(const std::optional<VFSOptions> &vfsOptions,
                std::optional<StringRef> primaryFile, std::string &error);

  static SourceKit::UIdent getUIDForDeclLanguage(const swift::Decl *D);
  static SourceKit::UIdent getUIDForDecl(const swift::Decl *D,
                                         bool IsRef = false);
  static SourceKit::UIdent getUIDForExtensionOfDecl(const swift::Decl *D);
  static SourceKit::UIdent getUIDForLocalVar(bool IsRef = false);
  static SourceKit::UIdent getUIDForRefactoringKind(
      swift::ide::RefactoringKind Kind);
  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 getUIDForObjCAttr();
  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 SourceKit::UIdent getUIDForSymbol(swift::index::SymbolInfo sym,
                                           bool isRef);

  static SourceKit::UIdent getUIDForRangeKind(swift::ide::RangeKind Kind);

  static SourceKit::UIdent getUIDForRegionType(swift::ide::RegionType Type);

  static SourceKit::UIdent getUIDForRefactoringRangeKind(swift::ide::RefactoringRangeKind Kind);

  static std::optional<UIdent>
  getUIDForDeclAttribute(const swift::DeclAttribute *Attr);

  static SourceKit::UIdent getUIDForFormalAccessScope(const swift::AccessScope Scope);

  static std::vector<UIdent> UIDsFromDeclAttributes(const swift::DeclAttributes &Attrs);

  static SourceKit::UIdent getUIDForNameKind(swift::ide::NameKind Kind);

  static swift::ide::NameKind getNameKindForUID(SourceKit::UIdent Id);

  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 the Type of a given decl.
  /// \returns true if the results should be ignored, false otherwise.
  static bool printDeclTypeUSR(const swift::ValueDecl *D, llvm::raw_ostream &OS);

  /// Generate a USR for of a given type.
  /// \returns true if the results should be ignored, false otherwise.
  static bool printTypeUSR(swift::Type Ty, 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);

  /// Annotates a declaration with XML tags that describe the key substructure
  /// of the declaration for CursorInfo/DocInfo.
  ///
  /// Prints declarations with decl- and type-specific tags derived from the
  /// UIDs used for decl/refs.
  ///
  /// FIXME: This move to libIDE, but currently depends on the UIdentVisitor.
  static void printFullyAnnotatedDeclaration(const swift::ValueDecl *VD,
                                             swift::Type BaseTy,
                                             llvm::raw_ostream &OS);

  static void printFullyAnnotatedDeclaration(const swift::ExtensionDecl *VD,
                                             llvm::raw_ostream &OS);

  static void
  printFullyAnnotatedSynthesizedDeclaration(const swift::ValueDecl *VD,
                                            swift::TypeOrExtensionDecl Target,
                                            llvm::raw_ostream &OS);

  static void
  printFullyAnnotatedSynthesizedDeclaration(const swift::ExtensionDecl *ED,
                                            swift::TypeOrExtensionDecl Target,
                                            llvm::raw_ostream &OS);

  /// Print 'description' or 'sourcetext' the given \p VD to \p OS. If
  /// \p usePlaceholder is \c true, call argument positions are substituted with
  /// a typed editor placeholders which is suitable for 'sourcetext'.
  static void
  printMemberDeclDescription(const swift::ValueDecl *VD, swift::Type baseTy,
                             bool usePlaceholder, 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);

  /// The result returned from \c performWithParamsToCompletionLikeOperation.
  struct CompletionLikeOperationParams {
    swift::CompilerInvocation &Invocation;
    llvm::MemoryBuffer *completionBuffer;
    swift::DiagnosticConsumer *DiagC;
    std::shared_ptr<std::atomic<bool>> CancellationFlag;
  };

  /// Execute \p PerformOperation synchronously with the parameters necessary to
  /// invoke a completion-like operation on \c IDEInspectionInstance.
  /// If \p InsertCodeCompletionToken is \c true, a code completion token will
  /// be inserted into the source buffer, if \p InsertCodeCompletionToken is \c
  /// false, the buffer is left as-is.
  void performWithParamsToCompletionLikeOperation(
      llvm::MemoryBuffer *UnresolvedInputFile, unsigned Offset,
      bool InsertCodeCompletionToken, ArrayRef<const char *> Args,
      llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
      SourceKitCancellationToken CancellationToken,
      llvm::function_ref<
          void(swift::ide::CancellableResult<CompletionLikeOperationParams>)>
          PerformOperation);

  //==========================================================================//
  // LangSupport Interface
  //==========================================================================//

  void globalConfigurationUpdated(std::shared_ptr<GlobalConfig> Config) override;

  void dependencyUpdated() override;

  void demangleNames(
      ArrayRef<const char *> MangledNames, bool Simplified,
      std::function<void(const RequestResult<ArrayRef<std::string>> &)>
          Receiver) override;

  void mangleSimpleClassNames(
      ArrayRef<std::pair<StringRef, StringRef>> ModuleClassPairs,
      std::function<void(const RequestResult<ArrayRef<std::string>> &)>
          Receiver) override;

  void indexSource(StringRef Filename, IndexingConsumer &Consumer,
                   ArrayRef<const char *> Args) override;

  void indexToStore(StringRef InputFile,
                    ArrayRef<const char *> Args,
                    IndexStoreOptions Opts,
                    SourceKitCancellationToken CancellationToken,
                    IndexToStoreReceiver Receiver) override;

  void codeComplete(llvm::MemoryBuffer *InputBuf, unsigned Offset,
                    OptionsDictionary *options,
                    SourceKit::CodeCompletionConsumer &Consumer,
                    ArrayRef<const char *> Args,
                    std::optional<VFSOptions> vfsOptions,
                    SourceKitCancellationToken CancellationToken) override;

  void codeCompleteOpen(StringRef name, llvm::MemoryBuffer *inputBuf,
                        unsigned offset, OptionsDictionary *options,
                        ArrayRef<FilterRule> rawFilterRules,
                        GroupedCodeCompletionConsumer &consumer,
                        ArrayRef<const char *> args,
                        std::optional<VFSOptions> vfsOptions,
                        SourceKitCancellationToken CancellationToken) override;

  void codeCompleteClose(StringRef name, unsigned offset,
                         GroupedCodeCompletionConsumer &consumer) override;

  void codeCompleteUpdate(StringRef name, unsigned offset,
                          OptionsDictionary *options,
                          SourceKitCancellationToken CancellationToken,
                          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,
                  EditorConsumer &Consumer, ArrayRef<const char *> Args,
                  std::optional<VFSOptions> vfsOptions) override;

  void editorOpenInterface(EditorConsumer &Consumer, StringRef Name,
                           StringRef ModuleName, std::optional<StringRef> Group,
                           ArrayRef<const char *> Args,
                           bool SynthesizedExtensions,
                           std::optional<StringRef> InterestedUSR) override;

  void editorOpenTypeInterface(EditorConsumer &Consumer,
                               ArrayRef<const char *> Args,
                               StringRef TypeUSR) override;

  void editorOpenHeaderInterface(EditorConsumer &Consumer,
                                 StringRef Name,
                                 StringRef HeaderName,
                                 ArrayRef<const char *> Args,
                                 bool UsingSwiftArgs,
                                 bool SynthesizedExtensions,
                                 StringRef swiftVersion) override;

  void editorOpenSwiftSourceInterface(
      StringRef Name, StringRef SourceName, ArrayRef<const char *> Args,
      SourceKitCancellationToken CancellationToken,
      std::shared_ptr<EditorConsumer> Consumer) override;

  void editorClose(StringRef Name, bool CancelBuilds,
                   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 editorConvertMarkupToXML(StringRef Source,
                                EditorConsumer &Consumer) override;

  void editorExpandPlaceholder(StringRef Name, unsigned Offset, unsigned Length,
                               EditorConsumer &Consumer) override;

  void getCursorInfo(StringRef PrimaryFilePath, StringRef InputBufferName,
                     unsigned Offset, unsigned Length, bool Actionables,
                     bool SymbolGraph, bool CancelOnSubsequentRequest,
                     ArrayRef<const char *> Args,
                     std::optional<VFSOptions> vfsOptions,
                     SourceKitCancellationToken CancellationToken,
                     std::function<void(const RequestResult<CursorInfoData> &)>
                         Receiver) override;

  void
  getDiagnostics(StringRef PrimaryFilePath, ArrayRef<const char *> Args,
                 std::optional<VFSOptions> VfsOptions,
                 SourceKitCancellationToken CancellationToken,
                 std::function<void(const RequestResult<DiagnosticsResult> &)>
                     Receiver) override;

  void getSemanticTokens(
      StringRef PrimaryFilePath, StringRef InputBufferName,
      ArrayRef<const char *> Args, std::optional<VFSOptions> VfsOptions,
      SourceKitCancellationToken CancellationToken,
      std::function<void(const RequestResult<SemanticTokensResult> &)> Receiver)
      override;

  void getNameInfo(
      StringRef PrimaryFilePath, StringRef InputBufferName, unsigned Offset,
      NameTranslatingInfo &Input, ArrayRef<const char *> Args,
      SourceKitCancellationToken CancellationToken,
      std::function<void(const RequestResult<NameTranslatingInfo> &)> Receiver)
      override;

  void getRangeInfo(
      StringRef PrimaryFilePath, StringRef InputBufferName, unsigned Offset,
      unsigned Length, bool CancelOnSubsequentRequest,
      ArrayRef<const char *> Args, SourceKitCancellationToken CancellationToken,
      std::function<void(const RequestResult<RangeInfo> &)> Receiver) override;

  void getCursorInfoFromUSR(
      StringRef PrimaryFilePath, StringRef InputBufferName, StringRef USR,
      bool CancelOnSubsequentRequest, ArrayRef<const char *> Args,
      std::optional<VFSOptions> vfsOptions,
      SourceKitCancellationToken CancellationToken,
      std::function<void(const RequestResult<CursorInfoData> &)> Receiver)
      override;

  void findRelatedIdentifiersInFile(
      StringRef PrimaryFilePath, StringRef InputBufferName, unsigned Offset,
      bool IncludeNonEditableBaseNames, bool CancelOnSubsequentRequest,
      ArrayRef<const char *> Args, SourceKitCancellationToken CancellationToken,
      std::function<void(const RequestResult<RelatedIdentsResult> &)> Receiver)
      override;

  void findActiveRegionsInFile(
      StringRef PrimaryFilePath, StringRef InputBufferName,
      ArrayRef<const char *> Args, SourceKitCancellationToken CancellationToken,
      std::function<void(const RequestResult<ActiveRegionsInfo> &)> Receiver)
      override;

  CancellableResult<std::vector<CategorizedRenameRanges>>
  findRenameRanges(llvm::MemoryBuffer *InputBuf,
                   ArrayRef<RenameLoc> RenameLocations,
                   ArrayRef<const char *> Args) override;

  void findLocalRenameRanges(StringRef Filename, unsigned Line, unsigned Column,
                             unsigned Length, ArrayRef<const char *> Args,
                             SourceKitCancellationToken CancellationToken,
                             CategorizedRenameRangesReceiver Receiver) override;

  void collectExpressionTypes(
      StringRef PrimaryFilePath, StringRef InputBufferName,
      ArrayRef<const char *> Args, ArrayRef<const char *> ExpectedProtocols,
      bool FullyQualified, bool CanonicalType,
      SourceKitCancellationToken CancellationToken,
      std::function<void(const RequestResult<ExpressionTypesInFile> &)>
          Receiver) override;

  void collectVariableTypes(
      StringRef PrimaryFilePath, StringRef InputBufferName,
      ArrayRef<const char *> Args, std::optional<unsigned> Offset,
      std::optional<unsigned> Length, bool FullyQualified,
      SourceKitCancellationToken CancellationToken,
      std::function<void(const RequestResult<VariableTypesInFile> &)> Receiver)
      override;

  void semanticRefactoring(StringRef PrimaryFilePath,
                           SemanticRefactoringInfo Info,
                           ArrayRef<const char *> Args,
                           SourceKitCancellationToken CancellationToken,
                           CategorizedEditsReceiver Receiver) override;

  void getDocInfo(llvm::MemoryBuffer *InputBuf,
                  StringRef ModuleName,
                  ArrayRef<const char *> Args,
                  DocInfoConsumer &Consumer) override;

  std::optional<std::pair<unsigned, unsigned>>
  findUSRRange(StringRef DocumentName, StringRef USR) override;

  void findInterfaceDocument(StringRef ModuleName, ArrayRef<const char *> Args,
               std::function<void(const RequestResult<InterfaceDocInfo> &)> Receiver) override;

  void findModuleGroups(StringRef ModuleName, ArrayRef<const char *> Args,
               std::function<void(const RequestResult<ArrayRef<StringRef>> &)> Receiver) override;

  void getExpressionContextInfo(llvm::MemoryBuffer *inputBuf, unsigned Offset,
                                OptionsDictionary *options,
                                ArrayRef<const char *> Args,
                                SourceKitCancellationToken CancellationToken,
                                TypeContextInfoConsumer &Consumer,
                                std::optional<VFSOptions> vfsOptions) override;

  void getConformingMethodList(llvm::MemoryBuffer *inputBuf, unsigned Offset,
                               OptionsDictionary *options,
                               ArrayRef<const char *> Args,
                               ArrayRef<const char *> ExpectedTypes,
                               SourceKitCancellationToken CancellationToken,
                               ConformingMethodListConsumer &Consumer,
                               std::optional<VFSOptions> vfsOptions) override;

  void expandMacroSyntactically(llvm::MemoryBuffer *inputBuf,
                                ArrayRef<const char *> args,
                                ArrayRef<MacroExpansionInfo> expansions,
                                CategorizedEditsReceiver receiver) override;

  void
  performCompile(StringRef Name, ArrayRef<const char *> Args,
                 std::optional<VFSOptions> vfsOptions,
                 SourceKitCancellationToken CancellationToken,
                 std::function<void(const RequestResult<CompilationResult> &)>
                     Receiver) override;

  void closeCompile(StringRef Name) override;

  void getStatistics(StatisticsReceiver) override;

private:
  swift::SourceFile *getSyntacticSourceFile(llvm::MemoryBuffer *InputBuf,
                                            ArrayRef<const char *> Args,
                                            swift::CompilerInstance &ParseCI,
                                            std::string &Error);
};

namespace trace {
  void initTraceInfo(trace::SwiftInvocation &SwiftArgs,
                     StringRef InputFile,
                     ArrayRef<const char *> Args);
  void initTraceInfo(trace::SwiftInvocation &SwiftArgs,
                     StringRef InputFile,
                     ArrayRef<std::string> Args);
}

/// 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();
};


/// Disable expensive SIL options which do not affect indexing or diagnostics.
void disableExpensiveSILOptions(swift::SILOptions &Opts);

swift::SourceFile *retrieveInputFile(StringRef inputBufferName,
                                     const swift::CompilerInstance &CI,
                                     bool haveRealPath = false);

} // namespace SourceKit

#endif