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
|
//===- TextStubV5.cpp -----------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Implements Text Stub JSON mappings.
//
//===----------------------------------------------------------------------===//
#include "TextStubCommon.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/JSON.h"
// clang-format off
/*
JSON Format specification.
All library level keys, accept target values and are defaulted if not specified.
{
"tapi_tbd_version": 5, # Required: TBD version for all documents in file
"main_library": { # Required: top level library
"target_info": [ # Required: target information
{
"target": "x86_64-macos",
"min_deployment": "10.14" # Required: minimum OS deployment version
},
{
"target": "arm64-macos",
"min_deployment": "10.14"
},
{
"target": "arm64-maccatalyst",
"min_deployment": "12.1"
}],
"flags":[{"attributes": ["flat_namespace"]}], # Optional:
"install_names":[{"name":"/S/L/F/Foo.fwk/Foo"}], # Required: library install name
"current_versions":[{"version": "1.2"}], # Optional: defaults to 1
"compatibility_versions":[{ "version": "1.1"}], # Optional: defaults to 1
"rpaths": [ # Optional:
{
"targets": ["x86_64-macos"], # Optional: defaults to targets in `target-info`
"paths": ["@executable_path/.../Frameworks"]
}],
"parent_umbrellas": [{"umbrella": "System"}],
"allowable_clients": [{"clients": ["ClientA"]}],
"reexported_libraries": [{"names": ["/u/l/l/foo.dylib"]}],
"exported_symbols": [{ # List of export symbols section
"targets": ["x86_64-macos", "arm64-macos"], # Optional: defaults to targets in `target-info`
"text": { # List of Text segment symbols
"global": [ "_func" ],
"weak": [],
"thread_local": []
},
"data": { ... }, # List of Data segment symbols
}],
"reexported_symbols": [{ ... }], # List of reexported symbols section
"undefined_symbols": [{ ... }] # List of undefined symbols section
},
"libraries": [ # Optional: Array of inlined libraries
{...}, {...}, {...}
]
}
*/
// clang-format on
using namespace llvm;
using namespace llvm::json;
using namespace llvm::MachO;
struct JSONSymbol {
SymbolKind Kind;
std::string Name;
SymbolFlags Flags;
};
using AttrToTargets = std::map<std::string, TargetList>;
using TargetsToSymbols =
SmallVector<std::pair<TargetList, std::vector<JSONSymbol>>>;
enum TBDKey : size_t {
TBDVersion = 0U,
MainLibrary,
Documents,
TargetInfo,
Targets,
Target,
Deployment,
Flags,
Attributes,
InstallName,
CurrentVersion,
CompatibilityVersion,
Version,
SwiftABI,
ABI,
ParentUmbrella,
Umbrella,
AllowableClients,
Clients,
ReexportLibs,
Names,
Name,
Exports,
Reexports,
Undefineds,
Data,
Text,
Weak,
ThreadLocal,
Globals,
ObjCClass,
ObjCEHType,
ObjCIvar,
RPath,
Paths,
};
std::array<StringRef, 64> Keys = {
"tapi_tbd_version",
"main_library",
"libraries",
"target_info",
"targets",
"target",
"min_deployment",
"flags",
"attributes",
"install_names",
"current_versions",
"compatibility_versions",
"version",
"swift_abi",
"abi",
"parent_umbrellas",
"umbrella",
"allowable_clients",
"clients",
"reexported_libraries",
"names",
"name",
"exported_symbols",
"reexported_symbols",
"undefined_symbols",
"data",
"text",
"weak",
"thread_local",
"global",
"objc_class",
"objc_eh_type",
"objc_ivar",
"rpaths",
"paths",
};
static llvm::SmallString<128> getParseErrorMsg(TBDKey Key) {
return {"invalid ", Keys[Key], " section"};
}
class JSONStubError : public llvm::ErrorInfo<llvm::json::ParseError> {
public:
JSONStubError(Twine ErrMsg) : Message(ErrMsg.str()) {}
void log(llvm::raw_ostream &OS) const override { OS << Message << "\n"; }
std::error_code convertToErrorCode() const override {
return llvm::inconvertibleErrorCode();
}
private:
std::string Message;
};
template <typename JsonT, typename StubT = JsonT>
Expected<StubT> getRequiredValue(
TBDKey Key, const Object *Obj,
std::function<std::optional<JsonT>(const Object *, StringRef)> GetValue,
std::function<std::optional<StubT>(JsonT)> Validate = nullptr) {
std::optional<JsonT> Val = GetValue(Obj, Keys[Key]);
if (!Val)
return make_error<JSONStubError>(getParseErrorMsg(Key));
if (Validate == nullptr)
return static_cast<StubT>(*Val);
std::optional<StubT> Result = Validate(*Val);
if (!Result.has_value())
return make_error<JSONStubError>(getParseErrorMsg(Key));
return Result.value();
}
template <typename JsonT, typename StubT = JsonT>
Expected<StubT> getRequiredValue(
TBDKey Key, const Object *Obj,
std::function<std::optional<JsonT>(const Object *, StringRef)> GetValue,
StubT DefaultValue, std::function<std::optional<StubT>(JsonT)> Validate) {
std::optional<JsonT> Val = GetValue(Obj, Keys[Key]);
if (!Val)
return DefaultValue;
std::optional<StubT> Result;
Result = Validate(*Val);
if (!Result.has_value())
return make_error<JSONStubError>(getParseErrorMsg(Key));
return Result.value();
}
Error collectFromArray(TBDKey Key, const Object *Obj,
std::function<void(StringRef)> Append,
bool IsRequired = false) {
const auto *Values = Obj->getArray(Keys[Key]);
if (!Values) {
if (IsRequired)
return make_error<JSONStubError>(getParseErrorMsg(Key));
return Error::success();
}
for (Value Val : *Values) {
auto ValStr = Val.getAsString();
if (!ValStr.has_value())
return make_error<JSONStubError>(getParseErrorMsg(Key));
Append(ValStr.value());
}
return Error::success();
}
namespace StubParser {
Expected<FileType> getVersion(const Object *File) {
auto VersionOrErr = getRequiredValue<int64_t, FileType>(
TBDKey::TBDVersion, File, &Object::getInteger,
[](int64_t Val) -> std::optional<FileType> {
unsigned Result = Val;
if (Result != 5)
return std::nullopt;
return FileType::TBD_V5;
});
if (!VersionOrErr)
return VersionOrErr.takeError();
return *VersionOrErr;
}
Expected<TargetList> getTargets(const Object *Section) {
const auto *Targets = Section->getArray(Keys[TBDKey::Targets]);
if (!Targets)
return make_error<JSONStubError>(getParseErrorMsg(TBDKey::Targets));
TargetList IFTargets;
for (Value JSONTarget : *Targets) {
auto TargetStr = JSONTarget.getAsString();
if (!TargetStr.has_value())
return make_error<JSONStubError>(getParseErrorMsg(TBDKey::Target));
auto TargetOrErr = Target::create(TargetStr.value());
if (!TargetOrErr)
return make_error<JSONStubError>(getParseErrorMsg(TBDKey::Target));
IFTargets.push_back(*TargetOrErr);
}
return std::move(IFTargets);
}
Expected<TargetList> getTargetsSection(const Object *Section) {
const Array *Targets = Section->getArray(Keys[TBDKey::TargetInfo]);
if (!Targets)
return make_error<JSONStubError>(getParseErrorMsg(TBDKey::Targets));
TargetList IFTargets;
for (const Value &JSONTarget : *Targets) {
const auto *Obj = JSONTarget.getAsObject();
if (!Obj)
return make_error<JSONStubError>(getParseErrorMsg(TBDKey::Target));
auto TargetStr =
getRequiredValue<StringRef>(TBDKey::Target, Obj, &Object::getString);
if (!TargetStr)
return make_error<JSONStubError>(getParseErrorMsg(TBDKey::Target));
auto VersionStr = getRequiredValue<StringRef>(TBDKey::Deployment, Obj,
&Object::getString);
if (!VersionStr)
return make_error<JSONStubError>(getParseErrorMsg(TBDKey::Deployment));
VersionTuple Version;
if (Version.tryParse(*VersionStr))
return make_error<JSONStubError>(getParseErrorMsg(TBDKey::Deployment));
auto TargetOrErr = Target::create(*TargetStr);
if (!TargetOrErr)
return make_error<JSONStubError>(getParseErrorMsg(TBDKey::Target));
TargetOrErr->MinDeployment = Version;
IFTargets.push_back(*TargetOrErr);
}
return std::move(IFTargets);
}
Error collectSymbolsFromSegment(const Object *Segment, TargetsToSymbols &Result,
SymbolFlags SectionFlag) {
auto Err = collectFromArray(
TBDKey::Globals, Segment, [&Result, &SectionFlag](StringRef Name) {
JSONSymbol Sym = {SymbolKind::GlobalSymbol, Name.str(), SectionFlag};
Result.back().second.emplace_back(Sym);
});
if (Err)
return Err;
Err = collectFromArray(
TBDKey::ObjCClass, Segment, [&Result, &SectionFlag](StringRef Name) {
JSONSymbol Sym = {SymbolKind::ObjectiveCClass, Name.str(), SectionFlag};
Result.back().second.emplace_back(Sym);
});
if (Err)
return Err;
Err = collectFromArray(TBDKey::ObjCEHType, Segment,
[&Result, &SectionFlag](StringRef Name) {
JSONSymbol Sym = {SymbolKind::ObjectiveCClassEHType,
Name.str(), SectionFlag};
Result.back().second.emplace_back(Sym);
});
if (Err)
return Err;
Err = collectFromArray(
TBDKey::ObjCIvar, Segment, [&Result, &SectionFlag](StringRef Name) {
JSONSymbol Sym = {SymbolKind::ObjectiveCInstanceVariable, Name.str(),
SectionFlag};
Result.back().second.emplace_back(Sym);
});
if (Err)
return Err;
SymbolFlags WeakFlag = SectionFlag | (SectionFlag == SymbolFlags::Undefined
? SymbolFlags::WeakReferenced
: SymbolFlags::WeakDefined);
Err = collectFromArray(
TBDKey::Weak, Segment, [&Result, WeakFlag](StringRef Name) {
JSONSymbol Sym = {SymbolKind::GlobalSymbol, Name.str(), WeakFlag};
Result.back().second.emplace_back(Sym);
});
if (Err)
return Err;
Err = collectFromArray(
TBDKey::ThreadLocal, Segment, [&Result, SectionFlag](StringRef Name) {
JSONSymbol Sym = {SymbolKind::GlobalSymbol, Name.str(),
SymbolFlags::ThreadLocalValue | SectionFlag};
Result.back().second.emplace_back(Sym);
});
if (Err)
return Err;
return Error::success();
}
Expected<StringRef> getNameSection(const Object *File) {
const Array *Section = File->getArray(Keys[TBDKey::InstallName]);
if (!Section)
return make_error<JSONStubError>(getParseErrorMsg(TBDKey::InstallName));
assert(!Section->empty() && "unexpected missing install name");
// TODO: Just take first for now.
const auto *Obj = Section->front().getAsObject();
if (!Obj)
return make_error<JSONStubError>(getParseErrorMsg(TBDKey::InstallName));
return getRequiredValue<StringRef>(TBDKey::Name, Obj, &Object::getString);
}
Expected<TargetsToSymbols> getSymbolSection(const Object *File, TBDKey Key,
TargetList &Targets) {
const Array *Section = File->getArray(Keys[Key]);
if (!Section)
return TargetsToSymbols();
SymbolFlags SectionFlag;
switch (Key) {
case TBDKey::Reexports:
SectionFlag = SymbolFlags::Rexported;
break;
case TBDKey::Undefineds:
SectionFlag = SymbolFlags::Undefined;
break;
default:
SectionFlag = SymbolFlags::None;
break;
};
TargetsToSymbols Result;
TargetList MappedTargets;
for (auto Val : *Section) {
auto *Obj = Val.getAsObject();
if (!Obj)
continue;
auto TargetsOrErr = getTargets(Obj);
if (!TargetsOrErr) {
MappedTargets = Targets;
consumeError(TargetsOrErr.takeError());
} else {
MappedTargets = *TargetsOrErr;
}
Result.emplace_back(std::make_pair(Targets, std::vector<JSONSymbol>()));
auto *DataSection = Obj->getObject(Keys[TBDKey::Data]);
auto *TextSection = Obj->getObject(Keys[TBDKey::Text]);
// There should be at least one valid section.
if (!DataSection && !TextSection)
return make_error<JSONStubError>(getParseErrorMsg(Key));
if (DataSection) {
auto Err = collectSymbolsFromSegment(DataSection, Result,
SectionFlag | SymbolFlags::Data);
if (Err)
return std::move(Err);
}
if (TextSection) {
auto Err = collectSymbolsFromSegment(TextSection, Result,
SectionFlag | SymbolFlags::Text);
if (Err)
return std::move(Err);
}
}
return std::move(Result);
}
Expected<AttrToTargets> getLibSection(const Object *File, TBDKey Key,
TBDKey SubKey,
const TargetList &Targets) {
auto *Section = File->getArray(Keys[Key]);
if (!Section)
return AttrToTargets();
AttrToTargets Result;
TargetList MappedTargets;
for (auto Val : *Section) {
auto *Obj = Val.getAsObject();
if (!Obj)
continue;
auto TargetsOrErr = getTargets(Obj);
if (!TargetsOrErr) {
MappedTargets = Targets;
consumeError(TargetsOrErr.takeError());
} else {
MappedTargets = *TargetsOrErr;
}
auto Err =
collectFromArray(SubKey, Obj, [&Result, &MappedTargets](StringRef Key) {
Result[Key.str()] = MappedTargets;
});
if (Err)
return std::move(Err);
}
return std::move(Result);
}
Expected<AttrToTargets> getUmbrellaSection(const Object *File,
const TargetList &Targets) {
const auto *Umbrella = File->getArray(Keys[TBDKey::ParentUmbrella]);
if (!Umbrella)
return AttrToTargets();
AttrToTargets Result;
TargetList MappedTargets;
for (auto Val : *Umbrella) {
auto *Obj = Val.getAsObject();
if (!Obj)
return make_error<JSONStubError>(
getParseErrorMsg(TBDKey::ParentUmbrella));
// Get Targets section.
auto TargetsOrErr = getTargets(Obj);
if (!TargetsOrErr) {
MappedTargets = Targets;
consumeError(TargetsOrErr.takeError());
} else {
MappedTargets = *TargetsOrErr;
}
auto UmbrellaOrErr =
getRequiredValue<StringRef>(TBDKey::Umbrella, Obj, &Object::getString);
if (!UmbrellaOrErr)
return UmbrellaOrErr.takeError();
Result[UmbrellaOrErr->str()] = Targets;
}
return std::move(Result);
}
Expected<uint8_t> getSwiftVersion(const Object *File) {
const Array *Versions = File->getArray(Keys[TBDKey::SwiftABI]);
if (!Versions)
return 0;
for (const auto &Val : *Versions) {
const auto *Obj = Val.getAsObject();
if (!Obj)
return make_error<JSONStubError>(getParseErrorMsg(TBDKey::SwiftABI));
// TODO: Take first for now.
return getRequiredValue<int64_t, uint8_t>(TBDKey::ABI, Obj,
&Object::getInteger);
}
return 0;
}
Expected<PackedVersion> getPackedVersion(const Object *File, TBDKey Key) {
const Array *Versions = File->getArray(Keys[Key]);
if (!Versions)
return PackedVersion(1, 0, 0);
for (const auto &Val : *Versions) {
const auto *Obj = Val.getAsObject();
if (!Obj)
return make_error<JSONStubError>(getParseErrorMsg(Key));
auto ValidatePV = [](StringRef Version) -> std::optional<PackedVersion> {
PackedVersion PV;
auto [success, truncated] = PV.parse64(Version);
if (!success || truncated)
return std::nullopt;
return PV;
};
// TODO: Take first for now.
return getRequiredValue<StringRef, PackedVersion>(
TBDKey::Version, Obj, &Object::getString, PackedVersion(1, 0, 0),
ValidatePV);
}
return PackedVersion(1, 0, 0);
}
Expected<TBDFlags> getFlags(const Object *File) {
TBDFlags Flags = TBDFlags::None;
const Array *Section = File->getArray(Keys[TBDKey::Flags]);
if (!Section)
return Flags;
for (auto &Val : *Section) {
// TODO: Just take first for now.
const auto *Obj = Val.getAsObject();
if (!Obj)
return make_error<JSONStubError>(getParseErrorMsg(TBDKey::Flags));
auto FlagsOrErr =
collectFromArray(TBDKey::Attributes, Obj, [&Flags](StringRef Flag) {
TBDFlags TBDFlag =
StringSwitch<TBDFlags>(Flag)
.Case("flat_namespace", TBDFlags::FlatNamespace)
.Case("not_app_extension_safe",
TBDFlags::NotApplicationExtensionSafe)
.Default(TBDFlags::None);
Flags |= TBDFlag;
});
if (FlagsOrErr)
return std::move(FlagsOrErr);
return Flags;
}
return Flags;
}
using IFPtr = std::unique_ptr<InterfaceFile>;
Expected<IFPtr> parseToInterfaceFile(const Object *File) {
auto TargetsOrErr = getTargetsSection(File);
if (!TargetsOrErr)
return TargetsOrErr.takeError();
TargetList Targets = *TargetsOrErr;
auto NameOrErr = getNameSection(File);
if (!NameOrErr)
return NameOrErr.takeError();
StringRef Name = *NameOrErr;
auto CurrVersionOrErr = getPackedVersion(File, TBDKey::CurrentVersion);
if (!CurrVersionOrErr)
return CurrVersionOrErr.takeError();
PackedVersion CurrVersion = *CurrVersionOrErr;
auto CompVersionOrErr = getPackedVersion(File, TBDKey::CompatibilityVersion);
if (!CompVersionOrErr)
return CompVersionOrErr.takeError();
PackedVersion CompVersion = *CompVersionOrErr;
auto SwiftABIOrErr = getSwiftVersion(File);
if (!SwiftABIOrErr)
return SwiftABIOrErr.takeError();
uint8_t SwiftABI = *SwiftABIOrErr;
auto FlagsOrErr = getFlags(File);
if (!FlagsOrErr)
return FlagsOrErr.takeError();
TBDFlags Flags = *FlagsOrErr;
auto UmbrellasOrErr = getUmbrellaSection(File, Targets);
if (!UmbrellasOrErr)
return UmbrellasOrErr.takeError();
AttrToTargets Umbrellas = *UmbrellasOrErr;
auto ClientsOrErr =
getLibSection(File, TBDKey::AllowableClients, TBDKey::Clients, Targets);
if (!ClientsOrErr)
return ClientsOrErr.takeError();
AttrToTargets Clients = *ClientsOrErr;
auto RLOrErr =
getLibSection(File, TBDKey::ReexportLibs, TBDKey::Names, Targets);
if (!RLOrErr)
return RLOrErr.takeError();
AttrToTargets ReexportLibs = std::move(*RLOrErr);
auto RPathsOrErr = getLibSection(File, TBDKey::RPath, TBDKey::Paths, Targets);
if (!RPathsOrErr)
return RPathsOrErr.takeError();
AttrToTargets RPaths = std::move(*RPathsOrErr);
auto ExportsOrErr = getSymbolSection(File, TBDKey::Exports, Targets);
if (!ExportsOrErr)
return ExportsOrErr.takeError();
TargetsToSymbols Exports = std::move(*ExportsOrErr);
auto ReexportsOrErr = getSymbolSection(File, TBDKey::Reexports, Targets);
if (!ReexportsOrErr)
return ReexportsOrErr.takeError();
TargetsToSymbols Reexports = std::move(*ReexportsOrErr);
auto UndefinedsOrErr = getSymbolSection(File, TBDKey::Undefineds, Targets);
if (!UndefinedsOrErr)
return UndefinedsOrErr.takeError();
TargetsToSymbols Undefineds = std::move(*UndefinedsOrErr);
IFPtr F(new InterfaceFile);
F->setInstallName(Name);
F->setCurrentVersion(CurrVersion);
F->setCompatibilityVersion(CompVersion);
F->setSwiftABIVersion(SwiftABI);
F->setTwoLevelNamespace(!(Flags & TBDFlags::FlatNamespace));
F->setApplicationExtensionSafe(
!(Flags & TBDFlags::NotApplicationExtensionSafe));
for (auto &T : Targets)
F->addTarget(T);
for (auto &[Lib, Targets] : Clients)
for (auto Target : Targets)
F->addAllowableClient(Lib, Target);
for (auto &[Lib, Targets] : ReexportLibs)
for (auto Target : Targets)
F->addReexportedLibrary(Lib, Target);
for (auto &[Lib, Targets] : Umbrellas)
for (auto Target : Targets)
F->addParentUmbrella(Target, Lib);
for (auto &[Path, Targets] : RPaths)
for (auto Target : Targets)
F->addRPath(Target, Path);
for (auto &[Targets, Symbols] : Exports)
for (auto &Sym : Symbols)
F->addSymbol(Sym.Kind, Sym.Name, Targets, Sym.Flags);
for (auto &[Targets, Symbols] : Reexports)
for (auto &Sym : Symbols)
F->addSymbol(Sym.Kind, Sym.Name, Targets, Sym.Flags);
for (auto &[Targets, Symbols] : Undefineds)
for (auto &Sym : Symbols)
F->addSymbol(Sym.Kind, Sym.Name, Targets, Sym.Flags);
return std::move(F);
}
Expected<std::vector<IFPtr>> getInlinedLibs(const Object *File) {
std::vector<IFPtr> IFs;
const Array *Files = File->getArray(Keys[TBDKey::Documents]);
if (!Files)
return std::move(IFs);
for (auto Lib : *Files) {
auto IFOrErr = parseToInterfaceFile(Lib.getAsObject());
if (!IFOrErr)
return IFOrErr.takeError();
auto IF = std::move(*IFOrErr);
IFs.emplace_back(std::move(IF));
}
return std::move(IFs);
}
} // namespace StubParser
Expected<std::unique_ptr<InterfaceFile>>
MachO::getInterfaceFileFromJSON(StringRef JSON) {
auto ValOrErr = parse(JSON);
if (!ValOrErr)
return ValOrErr.takeError();
auto *Root = ValOrErr->getAsObject();
auto VersionOrErr = StubParser::getVersion(Root);
if (!VersionOrErr)
return VersionOrErr.takeError();
FileType Version = *VersionOrErr;
Object *MainLib = Root->getObject(Keys[TBDKey::MainLibrary]);
auto IFOrErr = StubParser::parseToInterfaceFile(MainLib);
if (!IFOrErr)
return IFOrErr.takeError();
(*IFOrErr)->setFileType(Version);
std::unique_ptr<InterfaceFile> IF(std::move(*IFOrErr));
auto IFsOrErr = StubParser::getInlinedLibs(Root);
if (!IFsOrErr)
return IFsOrErr.takeError();
for (auto &File : *IFsOrErr) {
File->setFileType(Version);
IF->addDocument(std::shared_ptr<InterfaceFile>(std::move(File)));
}
return std::move(IF);
}
|