File: IncludeTree.cpp

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 (1089 lines) | stat: -rw-r--r-- 37,652 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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
//===- IncludeTree.cpp - Include-tree CAS graph -----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "clang/CAS/IncludeTree.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallBitVector.h"
#include "llvm/CAS/ObjectStore.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/Error.h"
#include <utility>

using namespace clang;
using namespace clang::cas;

template <typename NodeT>
Expected<NodeT> IncludeTreeBase<NodeT>::create(ObjectStore &DB,
                                               ArrayRef<ObjectRef> Refs,
                                               ArrayRef<char> Data) {
  // Using 4 chars for less chance that it will randomly match a wrong node and
  // makes the buffer 4 bytes "aligned".
  static_assert(NodeT::getNodeKind().size() == 4,
                "getNodeKind() should return 4 characters");
  SmallString<256> Buf{NodeT::getNodeKind()};
  Buf.reserve(Data.size() + NodeT::getNodeKind().size());
  Buf.append(Data.begin(), Data.end());
  auto Node = DB.store(Refs, Buf);
  if (!Node)
    return Node.takeError();
  auto Proxy = DB.getProxy(*Node);
  if (!Proxy)
    return Proxy.takeError();
  return NodeT(*Proxy);
}

Expected<IncludeTree::SpuriousImport>
IncludeTree::SpuriousImport::create(ObjectStore &DB, ObjectRef ImportRef,
                                    ObjectRef TreeRef) {
  return IncludeTreeBase::create(DB, {ImportRef, TreeRef}, {});
}

Expected<IncludeTree::File> IncludeTree::File::create(ObjectStore &DB,
                                                      StringRef Filename,
                                                      ObjectRef Contents) {
  auto PathRef = DB.storeFromString({}, Filename);
  if (!PathRef)
    return PathRef.takeError();
  std::array<ObjectRef, 2> Refs{*PathRef, Contents};
  return IncludeTreeBase::create(DB, Refs, {});
}

Expected<IncludeTree::File> IncludeTree::getBaseFile() {
  auto Node = getCAS().getProxy(getBaseFileRef());
  if (!Node)
    return Node.takeError();
  return File(std::move(*Node));
}

Expected<IncludeTree::FileInfo> IncludeTree::getBaseFileInfo() {
  auto File = getBaseFile();
  if (!File)
    return File.takeError();
  return File->getFileInfo();
}

llvm::Error IncludeTree::forEachInclude(
    llvm::function_ref<llvm::Error(std::pair<Node, uint32_t>)> Callback) {
  size_t RefI = 0;
  const size_t IncludeEnd = getNumIncludes();
  return forEachReference([&](ObjectRef Ref) -> llvm::Error {
    if (RefI == 0) {
      ++RefI;
      return llvm::Error::success();
    }
    size_t IncludeI = RefI - 1;
    if (IncludeI >= IncludeEnd)
      return llvm::Error::success();
    ++RefI;
    auto Include = getIncludeNode(Ref, getIncludeKind(IncludeI));
    if (!Include)
      return Include.takeError();
    return Callback({*Include, getIncludeOffset(IncludeI)});
  });
}

/// Write the bitset \p Bits to \p Writer, filling the final byte with zeros for
/// any unused values. Note: this does not store the size of the bitset.
static void writeBitSet(llvm::support::endian::Writer &Writer,
                        const llvm::SmallBitVector &Bits) {
  uintptr_t Store;
  ArrayRef<uintptr_t> BitWords = Bits.getData(Store);
  size_t RemainingBitsCount = Bits.size();
  while (RemainingBitsCount > 0) {
    if (BitWords.size() > 1) {
      Writer.write(BitWords.front());
      BitWords = BitWords.drop_front();
      RemainingBitsCount -= sizeof(uintptr_t) * CHAR_BIT;
      continue;
    }
    assert(RemainingBitsCount <= sizeof(uintptr_t) * CHAR_BIT);
    uintptr_t LastWord = BitWords.front();
    unsigned BytesNum = RemainingBitsCount / CHAR_BIT;
    if (RemainingBitsCount % CHAR_BIT != 0)
      ++BytesNum;
    while (BytesNum--) {
      Writer.write(static_cast<uint8_t>(LastWord & 0xFF));
      LastWord >>= CHAR_BIT;
    }
    break;
  }
}

Expected<IncludeTree> IncludeTree::create(
    ObjectStore &DB, SrcMgr::CharacteristicKind FileCharacteristic,
    ObjectRef BaseFile, ArrayRef<IncludeInfo> Includes,
    std::optional<ObjectRef> SubmoduleName, llvm::SmallBitVector Checks) {
  // The data buffer is composed of
  // 1. 1 byte for `CharacteristicKind` and IsSubmodule
  // 2. `uint32_t` offset and `uint8_t` kind for each includes
  // 3. variable number of bitset bytes for `Checks`.

  char Kind = FileCharacteristic;
  assert(Kind == FileCharacteristic && (Kind & IsSubmoduleBit) == 0 &&
         "SrcMgr::CharacteristicKind too big!");
  if (SubmoduleName)
    Kind |= IsSubmoduleBit;

  assert(File::isValid(DB, BaseFile));
  SmallVector<ObjectRef, 16> Refs;
  Refs.reserve(Includes.size() + 2);
  Refs.push_back(BaseFile);
  SmallString<64> Buffer;
  Buffer.reserve(Includes.size() * sizeof(uint32_t) + 1);

  Buffer += Kind;

  llvm::raw_svector_ostream BufOS(Buffer);
  llvm::support::endian::Writer Writer(BufOS, llvm::support::little);

  for (const auto &Include : Includes) {
    assert((Include.Kind == NodeKind::Tree &&
            IncludeTree::isValid(DB, Include.Ref)) ||
           (Include.Kind == NodeKind::ModuleImport &&
            ModuleImport::isValid(DB, Include.Ref)) ||
           (Include.Kind == NodeKind::SpuriousImport &&
            SpuriousImport::isValid(DB, Include.Ref)));
    Refs.push_back(Include.Ref);
    Writer.write(Include.Offset);
    static_assert(sizeof(uint8_t) == sizeof(Kind));
    Writer.write(static_cast<uint8_t>(Include.Kind));
  }

  if (SubmoduleName)
    Refs.push_back(*SubmoduleName);

  writeBitSet(Writer, Checks);

  return IncludeTreeBase::create(DB, Refs, Buffer);
}

Expected<IncludeTree> IncludeTree::get(ObjectStore &DB, ObjectRef Ref) {
  auto Node = DB.getProxy(Ref);
  if (!Node)
    return Node.takeError();
  if (!isValid(*Node))
    return llvm::createStringError(llvm::inconvertibleErrorCode(),
                                   "not a IncludeTree node kind");
  return IncludeTree(std::move(*Node));
}

IncludeTree::NodeKind IncludeTree::getIncludeKind(size_t I) const {
  assert(I < getNumIncludes());
  StringRef Data = dataSkippingFlags();
  assert(Data.size() >= (I + 1) * (sizeof(uint32_t) + 1));
  uint8_t K = *(Data.data() + I * (sizeof(uint32_t) + 1) + sizeof(uint32_t));
  return NodeKind(K);
}

uint32_t IncludeTree::getIncludeOffset(size_t I) const {
  assert(I < getNumIncludes());
  StringRef Data = dataSkippingFlags();
  assert(Data.size() >= (I + 1) * sizeof(uint32_t));
  uint32_t Offset =
      llvm::support::endian::read<uint32_t, llvm::support::little>(
          Data.data() + I * (sizeof(uint32_t) + 1));
  return Offset;
}

bool IncludeTree::getCheckResult(size_t I) const {
  // Skip include offsets and CharacteristicKind.
  StringRef Data = dataSkippingIncludes();
  unsigned ByteIndex = I / CHAR_BIT;
  size_t RemainingIndex = I % CHAR_BIT;
  uint8_t Bits = Data[ByteIndex];
  return Bits & (1 << RemainingIndex);
}

Expected<IncludeTree::Node> IncludeTree::getIncludeNode(size_t I) {
  return getIncludeNode(getIncludeRef(I), getIncludeKind(I));
}

Expected<IncludeTree::Node> IncludeTree::getIncludeNode(ObjectRef Ref,
                                                        NodeKind K) {
  auto N = getCAS().getProxy(Ref);
  if (!N)
    return N.takeError();
  return Node(std::move(*N), K);
}

bool IncludeTree::isValid(const ObjectProxy &Node) {
  if (!IncludeTreeBase::isValid(Node))
    return false;
  IncludeTreeBase Base(Node);
  if (Base.getNumReferences() == 0 || Base.getData().empty())
    return false;
  unsigned NumIncludes = Base.getNumReferences() - 1;
  if (Base.getData().front() & IsSubmoduleBit)
    NumIncludes -= 1;
  return Base.getData().size() >= NumIncludes * (sizeof(uint32_t) + 1) + 1;
}

Expected<IncludeTree::ModuleImport>
IncludeTree::ModuleImport::create(ObjectStore &DB, StringRef ModuleName,
                                  bool VisibilityOnly) {
  SmallString<64> Buffer;
  Buffer.push_back((char)VisibilityOnly);
  Buffer.append(ModuleName);
  return IncludeTreeBase::create(DB, {}, Buffer);
}

size_t IncludeTree::FileList::getNumFilesCurrentList() const {
  return llvm::support::endian::read<uint32_t, llvm::support::little>(
      getData().data());
}

IncludeTree::FileList::FileSizeTy
IncludeTree::FileList::getFileSize(size_t I) const {
  assert(I < getNumFilesCurrentList());
  StringRef Data = getData().drop_front(sizeof(uint32_t));
  assert(Data.size() >= (I + 1) * sizeof(FileSizeTy));
  return llvm::support::endian::read<FileSizeTy, llvm::support::little>(
      Data.data() + I * sizeof(FileSizeTy));
}

llvm::Error IncludeTree::FileList::forEachFileImpl(
    llvm::DenseSet<ObjectRef> &Seen,
    llvm::function_ref<llvm::Error(File, FileSizeTy)> Callback) {
  size_t Next = 0;
  size_t FileCount = getNumFilesCurrentList();
  return forEachReference([&](ObjectRef Ref) -> llvm::Error {
    size_t Index = Next++;
    if (!Seen.insert(Ref).second)
      return llvm::Error::success();

    if (Index < FileCount) {
      auto Include = getFile(Ref);
      if (!Include)
        return Include.takeError();
      return Callback(std::move(*Include), getFileSize(Index));
    }

    // Otherwise, it's a chained FileList.
    auto Proxy = getCAS().getProxy(Ref);
    if (!Proxy)
      return Proxy.takeError();
    FileList FL(std::move(*Proxy));
    return FL.forEachFileImpl(Seen, Callback);
  });
}

llvm::Error IncludeTree::FileList::forEachFile(
    llvm::function_ref<llvm::Error(File, FileSizeTy)> Callback) {
  llvm::DenseSet<ObjectRef> Seen;
  return forEachFileImpl(Seen, Callback);
}

Expected<IncludeTree::FileList>
IncludeTree::FileList::create(ObjectStore &DB, ArrayRef<FileEntry> Files,
                              ArrayRef<ObjectRef> FileLists) {
  SmallVector<ObjectRef, 16> Refs;
  Refs.reserve(Files.size() + FileLists.size());
  SmallString<256> Buffer;
  Buffer.reserve(sizeof(uint32_t) + Files.size() * sizeof(FileSizeTy));

  llvm::raw_svector_ostream BufOS(Buffer);
  llvm::support::endian::Writer Writer(BufOS, llvm::support::little);
  Writer.write(static_cast<uint32_t>(Files.size()));

  for (const FileEntry &Entry : Files) {
    assert(File::isValid(DB, Entry.FileRef));
    Refs.push_back(Entry.FileRef);
    Writer.write(Entry.Size);
  }

  Refs.append(FileLists.begin(), FileLists.end());

  return IncludeTreeBase::create(DB, Refs, Buffer);
}

Expected<IncludeTree::FileList> IncludeTree::FileList::get(ObjectStore &DB,
                                                           ObjectRef Ref) {
  auto Node = DB.getProxy(Ref);
  if (!Node)
    return Node.takeError();
  if (!isValid(*Node))
    return llvm::createStringError(llvm::inconvertibleErrorCode(),
                                   "not a IncludeFileList node kind");
  return FileList(std::move(*Node));
}

bool IncludeTree::FileList::isValid(const ObjectProxy &Node) {
  if (!IncludeTreeBase::isValid(Node))
    return false;
  IncludeTreeBase Base(Node);
  StringRef Data = Base.getData();
  if (Data.size() < sizeof(uint32_t))
    return false;
  unsigned NumFiles =
      llvm::support::endian::read<uint32_t, llvm::support::little>(Data.data());
  return NumFiles != 0 && NumFiles <= Base.getNumReferences() &&
         Data.size() == sizeof(uint32_t) + NumFiles * sizeof(FileSizeTy);
}

static constexpr uint16_t ModuleFlagFramework = 1 << 0;
static constexpr uint16_t ModuleFlagExplicit = 1 << 1;
static constexpr uint16_t ModuleFlagExternC = 1 << 2;
static constexpr uint16_t ModuleFlagSystem = 1 << 3;
static constexpr uint16_t ModuleFlagInferSubmodules = 1 << 4;
static constexpr uint16_t ModuleFlagInferExplicitSubmodules = 1 << 5;
static constexpr uint16_t ModuleFlagInferInferExportWildcard = 1 << 6;
static constexpr uint16_t ModuleFlagHasExports = 1 << 7;
static constexpr uint16_t ModuleFlagHasLinkLibraries = 1 << 8;
static constexpr uint16_t ModuleFlagUseExportAsModuleLinkName = 1 << 9;

IncludeTree::Module::ModuleFlags IncludeTree::Module::getFlags() const {
  uint16_t Raw = rawFlags();
  ModuleFlags Flags;
  Flags.IsFramework = Raw & ModuleFlagFramework;
  Flags.IsExplicit = Raw & ModuleFlagExplicit;
  Flags.IsExternC = Raw & ModuleFlagExternC;
  Flags.IsSystem = Raw & ModuleFlagSystem;
  Flags.InferSubmodules = Raw & ModuleFlagInferSubmodules;
  Flags.InferExplicitSubmodules = Raw & ModuleFlagInferExplicitSubmodules;
  Flags.InferExportWildcard = Raw & ModuleFlagInferInferExportWildcard;
  Flags.UseExportAsModuleLinkName = Raw & ModuleFlagUseExportAsModuleLinkName;
  return Flags;
}

size_t IncludeTree::Module::getNumSubmodules() const {
  size_t Count = getNumReferences();
  if (hasExports())
    Count -= 1;
  if (hasLinkLibraries())
    Count -= 1;
  return Count;
}

llvm::Error IncludeTree::Module::forEachSubmodule(
    llvm::function_ref<llvm::Error(Module)> CB) {
  size_t Count = getNumSubmodules();
  return forEachReference([&](ObjectRef Ref) -> llvm::Error {
    if (Count == 0)
      return llvm::Error::success();
    Count -= 1;
    auto Node = getCAS().getProxy(Ref);
    if (!Node)
      return Node.takeError();
    return CB(Module(*Node));
  });
}

Expected<IncludeTree::Module>
IncludeTree::Module::create(ObjectStore &DB, StringRef ModuleName,
                            StringRef ExportAs, ModuleFlags Flags,
                            ArrayRef<ObjectRef> Submodules,
                            std::optional<ObjectRef> ExportList,
                            std::optional<ObjectRef> LinkLibraries) {
  // Data:
  // - 2 bytes for Flags
  // - ModuleName (String, null-terminated)
  // - (optional) ExportAsModule
  // Refs:
  // - Submodules (IncludeTreeModule)
  // - (optional) ExportList
  // - (optional) LinkLibaryList

  uint16_t RawFlags = 0;
  if (Flags.IsFramework)
    RawFlags |= ModuleFlagFramework;
  if (Flags.IsExplicit)
    RawFlags |= ModuleFlagExplicit;
  if (Flags.IsExternC)
    RawFlags |= ModuleFlagExternC;
  if (Flags.IsSystem)
    RawFlags |= ModuleFlagSystem;
  if (Flags.InferSubmodules)
    RawFlags |= ModuleFlagInferSubmodules;
  if (Flags.InferExplicitSubmodules)
    RawFlags |= ModuleFlagInferExplicitSubmodules;
  if (Flags.InferExportWildcard)
    RawFlags |= ModuleFlagInferInferExportWildcard;
  if (ExportList)
    RawFlags |= ModuleFlagHasExports;
  if (LinkLibraries)
    RawFlags |= ModuleFlagHasLinkLibraries;
  if (Flags.UseExportAsModuleLinkName)
    RawFlags |= ModuleFlagUseExportAsModuleLinkName;

  SmallString<64> Buffer;
  llvm::raw_svector_ostream BufOS(Buffer);
  llvm::support::endian::Writer Writer(BufOS, llvm::support::little);
  Writer.write(RawFlags);

  Buffer.append(ModuleName);
  Buffer.append(StringRef("\0", 1));
  Buffer.append(ExportAs);

  SmallVector<ObjectRef> Refs(Submodules);
  if (ExportList)
    Refs.push_back(*ExportList);
  if (LinkLibraries)
    Refs.push_back(*LinkLibraries);

  return IncludeTreeBase::create(DB, Refs, Buffer);
}

uint16_t IncludeTree::Module::rawFlags() const {
  return llvm::support::endian::read<uint16_t, llvm::support::little>(
      getData().data());
}

bool IncludeTree::Module::hasExports() const {
  return rawFlags() & ModuleFlagHasExports;
}
bool IncludeTree::Module::hasLinkLibraries() const {
  return rawFlags() & ModuleFlagHasLinkLibraries;
}

std::optional<unsigned> IncludeTree::Module::getExportsIndex() const {
  if (hasExports())
    return getNumReferences() - (hasLinkLibraries() ? 2 : 1);
  return std::nullopt;
}
std::optional<unsigned> IncludeTree::Module::getLinkLibrariesIndex() const {
  if (hasLinkLibraries())
    return getNumReferences() - 1;
  return std::nullopt;
}

Expected<std::optional<IncludeTree::Module::ExportList>>
IncludeTree::Module::getExports() {
  if (auto Ref = getExportsRef()) {
    auto N = getCAS().getProxy(*Ref);
    if (!N)
      return N.takeError();
    return ExportList(std::move(*N));
  }
  return std::nullopt;
}

/// The list of modules that this submodule re-exports.
Expected<std::optional<IncludeTree::Module::LinkLibraryList>>
IncludeTree::Module::getLinkLibraries() {
  if (auto Ref = getLinkLibrariesRef()) {
    auto N = getCAS().getProxy(*Ref);
    if (!N)
      return N.takeError();
    return LinkLibraryList(std::move(*N));
  }
  return std::nullopt;
}

bool IncludeTree::Module::ExportList::hasGlobalWildcard() const {
  // The bit after explicit exports is global.
  return exportHasWildcard(getNumExplicitExports());
}
bool IncludeTree::Module::ExportList::exportHasWildcard(size_t I) const {
  assert(I < getNumExplicitExports() + 1);
  unsigned ByteIndex = I / CHAR_BIT;
  size_t RemainingIndex = I % CHAR_BIT;
  uint8_t Bits = getData()[ByteIndex];
  return Bits & (1 << RemainingIndex);
}
Expected<IncludeTree::Module::ExportList::Export>
IncludeTree::Module::ExportList::getExplicitExport(size_t I) {
  Expected<ObjectProxy> Name = getCAS().getProxy(getReference(I));
  if (!Name)
    return Name.takeError();
  return Export{Name->getData(), exportHasWildcard(I)};
}
llvm::Error IncludeTree::Module::ExportList::forEachExplicitExport(
    llvm::function_ref<llvm::Error(Export)> CB) {
  size_t ExportI = 0;
  return forEachReference([&](ObjectRef Ref) {
    Expected<ObjectProxy> Name = getCAS().getProxy(Ref);
    if (!Name)
      return Name.takeError();
    return CB(Export{Name->getData(), exportHasWildcard(ExportI)});
  });
}
Expected<IncludeTree::Module::ExportList>
IncludeTree::Module::ExportList::create(ObjectStore &DB,
                                        ArrayRef<Export> Exports,
                                        bool GlobalWildcard) {
  // Data:
  // - 1 bit per explicit export for wildcard
  // - 1 bit for global wildcard
  // Refs: export names
  SmallString<64> Buffer;
  llvm::raw_svector_ostream BufOS(Buffer);
  llvm::support::endian::Writer Writer(BufOS, llvm::support::little);
  SmallVector<ObjectRef> Refs;
  llvm::SmallBitVector WildcardBits;
  for (Export E : Exports) {
    auto Ref = DB.storeFromString({}, E.ModuleName);
    if (!Ref)
      return Ref.takeError();
    Refs.push_back(*Ref);
    WildcardBits.push_back(E.Wildcard);
  }
  WildcardBits.push_back(GlobalWildcard);
  writeBitSet(Writer, WildcardBits);

  return IncludeTreeBase::create(DB, Refs, Buffer);
}

bool IncludeTree::Module::LinkLibraryList::isFramework(size_t I) const {
  assert(I < getNumLibraries());
  unsigned ByteIndex = I / CHAR_BIT;
  size_t RemainingIndex = I % CHAR_BIT;
  uint8_t Bits = getData()[ByteIndex];
  return Bits & (1 << RemainingIndex);
}
llvm::Error IncludeTree::Module::LinkLibraryList::forEachLinkLibrary(
    llvm::function_ref<llvm::Error(LinkLibrary)> CB) {
  size_t I = 0;
  return forEachReference([&](ObjectRef Ref) {
    auto Name = getCAS().getProxy(getLibraryNameRef(I));
    if (!Name)
      return Name.takeError();
    return CB({Name->getData(), isFramework(I++)});
  });
}
Expected<IncludeTree::Module::LinkLibraryList>
IncludeTree::Module::LinkLibraryList::create(ObjectStore &DB,
                                             ArrayRef<LinkLibrary> Libraries) {
  // Data:
  // - 1 bit per library for IsFramework
  // Refs: library names
  SmallString<64> Buffer;
  llvm::raw_svector_ostream BufOS(Buffer);
  llvm::support::endian::Writer Writer(BufOS, llvm::support::little);
  SmallVector<ObjectRef> Refs;
  llvm::SmallBitVector FrameworkBits;
  for (LinkLibrary L : Libraries) {
    auto Ref = DB.storeFromString({}, L.Library);
    if (!Ref)
      return Ref.takeError();
    Refs.push_back(*Ref);
    FrameworkBits.push_back(L.IsFramework);
  }
  writeBitSet(Writer, FrameworkBits);

  return IncludeTreeBase::create(DB, Refs, Buffer);
}

Expected<IncludeTree::ModuleMap>
IncludeTree::ModuleMap::create(ObjectStore &DB, ArrayRef<ObjectRef> Modules) {
  return IncludeTreeBase::create(DB, Modules, {});
}

llvm::Error IncludeTree::ModuleMap::forEachModule(
    llvm::function_ref<llvm::Error(Module)> CB) {
  return forEachReference([&](ObjectRef Ref) {
    auto N = getCAS().getProxy(Ref);
    if (!N)
      return N.takeError();
    return CB(Module(std::move(*N)));
  });
}

static constexpr char HasPCH = 1;
static constexpr char HasModuleMap = 1 << 1;
static constexpr char HasAPINotes = 1 << 2;

Expected<IncludeTreeRoot>
IncludeTreeRoot::create(ObjectStore &DB, ObjectRef MainFileTree,
                        ObjectRef FileList, std::optional<ObjectRef> PCHRef,
                        std::optional<ObjectRef> ModuleMapRef,
                        std::optional<ObjectRef> APINotesRef) {
  assert(IncludeTree::isValid(DB, MainFileTree));
  assert(IncludeTree::FileList::isValid(DB, FileList));
  assert(!ModuleMapRef || IncludeTree::ModuleMap::isValid(DB, *ModuleMapRef));

  std::array<char, 1> Data = {0};
  if (PCHRef)
    Data[0] |= HasPCH;
  if (ModuleMapRef)
    Data[0] |= HasModuleMap;
  if (APINotesRef)
    Data[0] |= HasAPINotes;

  SmallVector<ObjectRef> Refs = {MainFileTree, FileList};
  if (PCHRef)
    Refs.push_back(*PCHRef);
  if (ModuleMapRef)
    Refs.push_back(*ModuleMapRef);
  if (APINotesRef)
    Refs.push_back(*APINotesRef);

  return IncludeTreeBase::create(DB, Refs, Data);
}

Expected<IncludeTreeRoot> IncludeTreeRoot::get(ObjectStore &DB, ObjectRef Ref) {
  auto Node = DB.getProxy(Ref);
  if (!Node)
    return Node.takeError();
  if (!isValid(*Node))
    return llvm::createStringError(llvm::inconvertibleErrorCode(),
                                   "not a IncludeTreeRoot node kind");
  return IncludeTreeRoot(std::move(*Node));
}

std::optional<unsigned> IncludeTreeRoot::getPCHRefIndex() const {
  if (getData()[0] & HasPCH)
    return 2;
  return std::nullopt;
}
std::optional<unsigned> IncludeTreeRoot::getModuleMapRefIndex() const {
  if (getData()[0] & HasModuleMap)
    return (getData()[0] & HasPCH) ? 3u : 2u;
  return std::nullopt;
}
std::optional<unsigned> IncludeTreeRoot::getAPINotesRefIndex() const {
  if (getData()[0] & HasAPINotes)
    return 2 + (getPCHRefIndex() ? 1 : 0) + (getModuleMapRefIndex() ? 1 : 0);
  return std::nullopt;
}

llvm::Error IncludeTree::File::print(llvm::raw_ostream &OS, unsigned Indent) {
  auto Filename = getFilename();
  if (!Filename)
    return Filename.takeError();
  OS.indent(Indent) << Filename->getData() << ' ';
  getCAS().getID(getContentsRef()).print(OS);
  OS << '\n';
  return llvm::Error::success();
}

llvm::Error IncludeTree::print(llvm::raw_ostream &OS, unsigned Indent) {
  auto IncludeBy = getBaseFile();
  if (!IncludeBy)
    return IncludeBy.takeError();
  if (llvm::Error E = IncludeBy->print(OS))
    return E;

  auto Submodule = getSubmoduleName();
  if (!Submodule)
    return Submodule.takeError();
  if (*Submodule)
    OS.indent(Indent) << "Submodule: " << **Submodule << '\n';

  llvm::SourceMgr SM;
  auto Blob = IncludeBy->getContents();
  if (!Blob)
    return Blob.takeError();
  auto MemBuf = llvm::MemoryBuffer::getMemBuffer(Blob->getData());
  unsigned BufID = SM.AddNewSourceBuffer(std::move(MemBuf), llvm::SMLoc());

  return forEachInclude([&](std::pair<Node, uint32_t> Include) -> llvm::Error {
    llvm::SMLoc Loc = llvm::SMLoc::getFromPointer(
        SM.getMemoryBuffer(BufID)->getBufferStart() + Include.second);
    auto LineCol = SM.getLineAndColumn(Loc);
    OS.indent(Indent) << LineCol.first << ':' << LineCol.second << ' ';
    return Include.first.print(OS, Indent + 2);
  });
}

llvm::Error IncludeTree::FileList::print(llvm::raw_ostream &OS,
                                         unsigned Indent) {
  return forEachFile([&](File File, FileSizeTy) -> llvm::Error {
    return File.print(OS, Indent);
  });
}

llvm::Error IncludeTree::ModuleImport::print(llvm::raw_ostream &OS,
                                             unsigned Indent, char End) {
  if (visibilityOnly())
    OS << "(Module for visibility only) ";
  else
    OS << "(Module) ";
  OS << getModuleName() << End;
  return llvm::Error::success();
}

llvm::Error IncludeTree::SpuriousImport::print(llvm::raw_ostream &OS,
                                               unsigned Indent) {
  auto MI = getModuleImport();
  if (!MI)
    return MI.takeError();
  auto IT = getIncludeTree();
  if (!IT)
    return IT.takeError();
  OS << "(Spurious import) ";
  if (llvm::Error E = MI->print(OS, Indent, /*End=*/' '))
    return E;
  if (llvm::Error E = IT->print(OS, Indent))
    return E;
  return llvm::Error::success();
}

llvm::Error IncludeTree::Node::print(llvm::raw_ostream &OS, unsigned Indent) {
  switch (K) {
  case NodeKind::Tree:
    return getIncludeTree().print(OS, Indent);
  case NodeKind::ModuleImport:
    return getModuleImport().print(OS, Indent);
  case NodeKind::SpuriousImport:
    return getSpuriousImport().print(OS, Indent);
  }
}

llvm::Error IncludeTree::Module::print(llvm::raw_ostream &OS, unsigned Indent) {
  OS.indent(Indent) << getName();
  ModuleFlags Flags = getFlags();
  if (Flags.IsFramework)
    OS << " (framework)";
  if (Flags.IsExplicit)
    OS << " (explicit)";
  if (Flags.IsExternC)
    OS << " (extern_c)";
  if (Flags.IsSystem)
    OS << " (system)";
  OS << '\n';
  auto ExportAs = getExportAsModule();
  if (!ExportAs.empty())
    OS << " export_as " << ExportAs << "\n";
  if (Flags.InferSubmodules) {
    if (Flags.InferExplicitSubmodules)
      OS << "  explicit module *";
    else
      OS << "  module *";
    if (Flags.InferExportWildcard)
      OS << " { export * }";
    OS << '\n';
  }
  auto ExportList = getExports();
  if (!ExportList)
    return ExportList.takeError();
  if (*ExportList)
    if (llvm::Error E = (*ExportList)->print(OS, Indent + 2))
      return E;
  auto LinkLibraries = getLinkLibraries();
  if (!LinkLibraries)
    return LinkLibraries.takeError();
  if (*LinkLibraries)
    if (llvm::Error E = (*LinkLibraries)->print(OS, Indent + 2))
      return E;
  return forEachSubmodule(
      [&](Module Sub) { return Sub.print(OS, Indent + 2); });
}
llvm::Error IncludeTree::Module::ExportList::print(llvm::raw_ostream &OS,
                                                   unsigned Indent) {
  if (hasGlobalWildcard())
    OS.indent(Indent) << "export *\n";
  return forEachExplicitExport([&](Export E) {
    OS.indent(Indent) << "export " << E.ModuleName;
    if (E.Wildcard)
      OS << ".*";
    OS << '\n';
    return llvm::Error::success();
  });
}

llvm::Error IncludeTree::Module::LinkLibraryList::print(llvm::raw_ostream &OS,
                                                        unsigned Indent) {
  return forEachLinkLibrary([&](LinkLibrary E) {
    OS.indent(Indent) << "link " << E.Library;
    if (E.IsFramework)
      OS << " (framework)";
    OS << '\n';
    return llvm::Error::success();
  });
}

llvm::Error IncludeTree::ModuleMap::print(llvm::raw_ostream &OS,
                                          unsigned Indent) {
  return forEachModule([&](Module M) { return M.print(OS, Indent); });
}

llvm::Expected<IncludeTree::APINotes>
IncludeTree::APINotes::create(ObjectStore &DB,
                              ArrayRef<ObjectRef> APINoteList) {
  assert(APINoteList.size() < 2 && "Too many APINotes added");
  return IncludeTreeBase::create(DB, APINoteList, {});
}

llvm::Expected<IncludeTree::APINotes>
IncludeTree::APINotes::get(ObjectStore &DB, ObjectRef Ref) {
  auto Node = DB.getProxy(Ref);
  if (!Node)
    return Node.takeError();
  if (!isValid(*Node))
    return llvm::createStringError(llvm::inconvertibleErrorCode(),
                                   "not an APINodes node kind");
  return APINotes(std::move(*Node));
}

llvm::Error IncludeTree::APINotes::print(llvm::raw_ostream &OS,
                                         unsigned Indent) {
  return forEachReference([&](ObjectRef Ref) -> llvm::Error {
    auto Node = getCAS().getProxy(Ref);
    if (!Node)
      return Node.takeError();
    OS.indent(Indent) << Node->getID() << "\n";
    OS.indent(Indent) << Node->getData() << "\n";
    return llvm::Error::success();
  });
}

llvm::Error IncludeTree::APINotes::forEachAPINotes(
    llvm::function_ref<llvm::Error(StringRef)> CB) {
  return forEachReference([&](ObjectRef Ref) {
    auto N = getCAS().getProxy(Ref);
    if (!N)
      return N.takeError();
    return CB(N->getData());
  });
}

llvm::Error IncludeTreeRoot::print(llvm::raw_ostream &OS, unsigned Indent) {
  std::optional<IncludeTree::File> PCH;
  if (llvm::Error E = getPCH().moveInto(PCH))
    return E;
  if (PCH) {
    OS.indent(Indent) << "(PCH) ";
    if (llvm::Error E = PCH->print(OS))
      return E;
  }
  std::optional<cas::IncludeTree> MainTree;
  if (llvm::Error E = getMainFileTree().moveInto(MainTree))
    return E;
  if (llvm::Error E = MainTree->print(OS.indent(Indent), Indent))
    return E;
  std::optional<IncludeTree::ModuleMap> ModuleMap;
  if (llvm::Error E = getModuleMap().moveInto(ModuleMap))
    return E;
  if (ModuleMap) {
    OS.indent(Indent) << "Module Map:\n";
    if (llvm::Error E = ModuleMap->print(OS, Indent))
      return E;
  }
  OS.indent(Indent) << "Files:\n";
  std::optional<IncludeTree::FileList> List;
  if (llvm::Error E = getFileList().moveInto(List))
    return E;
  std::optional<IncludeTree::APINotes> APINotes;
  if (llvm::Error E = getAPINotes().moveInto(APINotes))
    return E;
  if (APINotes) {
    OS.indent(Indent) << "APINotes:\n";
    if (llvm::Error E = APINotes->print(OS, Indent))
      return E;
  }
  return List->print(OS, Indent);
}

namespace {
/// An implementation of a \p vfs::FileSystem that supports the simple queries
/// of the preprocessor, for creating \p FileEntries using a file path, while
/// "replaying" an \p IncludeTreeRoot. It is not intended to be a complete
/// implementation of a file system.
class IncludeTreeFileSystem : public llvm::vfs::FileSystem {
  llvm::cas::ObjectStore &CAS;

public:
  class IncludeTreeFile : public llvm::vfs::File {
    llvm::vfs::Status Stat;
    StringRef Contents;
    cas::ObjectRef ContentsRef;

  public:
    IncludeTreeFile(llvm::vfs::Status Stat, StringRef Contents,
                    cas::ObjectRef ContentsRef)
        : Stat(std::move(Stat)), Contents(Contents),
          ContentsRef(std::move(ContentsRef)) {}

    llvm::ErrorOr<llvm::vfs::Status> status() override { return Stat; }

    llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
    getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
              bool IsVolatile) override {
      SmallString<256> NameBuf;
      return llvm::MemoryBuffer::getMemBuffer(Contents,
                                              Name.toStringRef(NameBuf));
    }

    llvm::ErrorOr<std::optional<cas::ObjectRef>>
    getObjectRefForContent() override {
      return ContentsRef;
    }

    std::error_code close() override { return std::error_code(); }
  };

  explicit IncludeTreeFileSystem(llvm::cas::ObjectStore &CAS) : CAS(CAS) {}

  struct FileEntry {
    cas::ObjectRef ContentsRef;
    IncludeTree::FileList::FileSizeTy Size;
    llvm::sys::fs::UniqueID UniqueID;
  };

  struct MaterializedFile : FileEntry {
    StringRef Contents;

    MaterializedFile(StringRef Contents, FileEntry FE)
        : FileEntry(std::move(FE)), Contents(Contents) {}
  };

  llvm::BumpPtrAllocator Alloc;
  llvm::StringMap<FileEntry, llvm::BumpPtrAllocator &> Files{Alloc};
  llvm::StringMap<llvm::sys::fs::UniqueID, llvm::BumpPtrAllocator &>
      Directories{Alloc};

  llvm::ErrorOr<llvm::vfs::Status> status(const Twine &Path) override {
    SmallString<128> Filename;
    getPath(Path, Filename);
    auto FileEntry = Files.find(Filename);
    if (FileEntry != Files.end()) {
      return makeStatus(Filename, FileEntry->second.Size,
                        FileEntry->second.UniqueID,
                        llvm::sys::fs::file_type::regular_file);
    }

    // Also check whether this is a parent directory status query.
    auto DirEntry = Directories.find(Filename);
    if (DirEntry != Directories.end()) {
      return makeStatus(Filename, /*Size*/ 0, DirEntry->second,
                        llvm::sys::fs::file_type::directory_file);
    }

    return llvm::errorToErrorCode(fileError(Filename));
  }

  llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
  openFileForRead(const Twine &Path) override {
    SmallString<128> Filename;
    getPath(Path, Filename);
    auto MaterializedFile = materialize(Filename);
    if (!MaterializedFile)
      return llvm::errorToErrorCode(MaterializedFile.takeError());
    llvm::vfs::Status Stat = makeStatus(
        Filename, MaterializedFile->Contents.size(), MaterializedFile->UniqueID,
        llvm::sys::fs::file_type::regular_file);
    return std::make_unique<IncludeTreeFile>(
        std::move(Stat), MaterializedFile->Contents,
        std::move(MaterializedFile->ContentsRef));
  }

  Expected<MaterializedFile> materialize(StringRef Filename) {
    auto Entry = Files.find(Filename);
    if (Entry == Files.end())
      return fileError(Filename);
    auto ContentsBlob = CAS.getProxy(Entry->second.ContentsRef);
    if (!ContentsBlob)
      return ContentsBlob.takeError();

    return MaterializedFile{ContentsBlob->getData(), Entry->second};
  }

  /// Produce a filename compatible with our StringMaps. See comment in
  /// \c createIncludeTreeFileSystem.
  void getPath(const Twine &Path, SmallVectorImpl<char> &Out) {
    Path.toVector(Out);
    // Strip dots, but do not eliminate a path consisting only of '.'
    if (Out.size() != 1)
      llvm::sys::path::remove_dots(Out);
  }

  static llvm::vfs::Status makeStatus(StringRef Filename, uint64_t Size,
                                      llvm::sys::fs::UniqueID UniqueID,
                                      llvm::sys::fs::file_type Type) {
    const llvm::sys::fs::perms Permissions =
        llvm::sys::fs::perms::all_read | llvm::sys::fs::perms::owner_write;
    return llvm::vfs::Status(Filename, UniqueID, llvm::sys::TimePoint<>(),
                             /*User=*/0,
                             /*Group=*/0, Size, Type, Permissions);
  }

  static llvm::Error fileError(StringRef Filename) {
    return llvm::createFileError(
        Filename,
        llvm::createStringError(std::errc::no_such_file_or_directory,
                                "filename not part of include tree list"));
  }

  llvm::vfs::directory_iterator dir_begin(const Twine &Dir,
                                          std::error_code &EC) override {
    // Return no_such_file_or_directory so llvm::vfs::OverlayFileSystem can
    // ignore this layer when iterating directories.
    EC = llvm::errc::no_such_file_or_directory;
    return llvm::vfs::directory_iterator();
  }
  llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
    return llvm::errc::operation_not_permitted;
  }
  std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
    return llvm::errc::operation_not_permitted;
  }
};
} // namespace

static llvm::Error diagnoseFileChange(IncludeTree::File F, ObjectRef Content) {
  auto FilenameBlob = F.getFilename();
  if (!FilenameBlob)
    return FilenameBlob.takeError();
  cas::ObjectStore &DB = F.getCAS();
  std::string Filename(FilenameBlob->getData());
  std::string OldID = DB.getID(Content).toString();
  std::string NewID = DB.getID(F.getContentsRef()).toString();
  return llvm::createStringError(llvm::inconvertibleErrorCode(),
                                 "file '%s' changed during build; include-tree "
                                 "contents changed from %s to %s",
                                 Filename.c_str(), OldID.c_str(),
                                 NewID.c_str());
}

Expected<IntrusiveRefCntPtr<llvm::vfs::FileSystem>>
cas::createIncludeTreeFileSystem(IncludeTreeRoot &Root) {
  auto FileList = Root.getFileList();
  if (!FileList)
    return FileList.takeError();

  return createIncludeTreeFileSystem(Root.getCAS(), *FileList);
}

Expected<IntrusiveRefCntPtr<llvm::vfs::FileSystem>>
cas::createIncludeTreeFileSystem(llvm::cas::ObjectStore &CAS,
                                 IncludeTree::FileList &FileList) {
  // Map from FilenameRef to ContentsRef.
  llvm::DenseMap<ObjectRef, ObjectRef> SeenContents;

  IntrusiveRefCntPtr<IncludeTreeFileSystem> IncludeTreeFS =
      new IncludeTreeFileSystem(CAS);
  llvm::Error E = FileList.forEachFile(
      [&](IncludeTree::File File,
          IncludeTree::FileList::FileSizeTy Size) -> llvm::Error {
        auto InsertPair = SeenContents.insert(
            std::make_pair(File.getFilenameRef(), File.getContentsRef()));
        if (!InsertPair.second) {
          if (InsertPair.first->second != File.getContentsRef())
            return diagnoseFileChange(File, InsertPair.first->second);
          return llvm::Error::success();
        }

        auto FilenameBlob = File.getFilename();
        if (!FilenameBlob)
          return FilenameBlob.takeError();

        SmallString<128> Filename(FilenameBlob->getData());
        // Strip './' in the filename to match the behaviour of ASTWriter; we
        // also strip './' in IncludeTreeFileSystem::getPath.
        assert(Filename != ".");
        llvm::sys::path::remove_dots(Filename);

        StringRef DirName = llvm::sys::path::parent_path(Filename);
        if (DirName.empty())
          DirName = ".";
        auto &DirEntry = IncludeTreeFS->Directories[DirName];
        if (DirEntry == llvm::sys::fs::UniqueID()) {
          DirEntry = llvm::vfs::getNextVirtualUniqueID();
        }

        IncludeTreeFS->Files.insert(
            std::make_pair(Filename, IncludeTreeFileSystem::FileEntry{
                                         File.getContentsRef(), Size,
                                         llvm::vfs::getNextVirtualUniqueID()}));
        return llvm::Error::success();
      });
  if (E)
    return std::move(E);

  return IncludeTreeFS;
}