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 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
|
//===-- APINotesWriter.cpp - API Notes Writer -------------------*- 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/APINotes/APINotesWriter.h"
#include "APINotesFormat.h"
#include "clang/APINotes/Types.h"
#include "clang/Basic/FileManager.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Bitstream/BitstreamWriter.h"
#include "llvm/Support/DJB.h"
#include "llvm/Support/OnDiskHashTable.h"
#include "llvm/Support/VersionTuple.h"
namespace clang {
namespace api_notes {
class APINotesWriter::Implementation {
friend class APINotesWriter;
template <typename T>
using VersionedSmallVector =
llvm::SmallVector<std::pair<llvm::VersionTuple, T>, 1>;
std::string ModuleName;
const FileEntry *SourceFile;
/// Scratch space for bitstream writing.
llvm::SmallVector<uint64_t, 64> Scratch;
/// Mapping from strings to identifier IDs.
llvm::StringMap<IdentifierID> IdentifierIDs;
/// Information about contexts (Objective-C classes or protocols or C++
/// namespaces).
///
/// Indexed by the parent context ID, context kind and the identifier ID of
/// this context and provides both the context ID and information describing
/// the context within that module.
llvm::DenseMap<ContextTableKey,
std::pair<unsigned, VersionedSmallVector<ContextInfo>>>
Contexts;
/// Information about parent contexts for each context.
///
/// Indexed by context ID, provides the parent context ID.
llvm::DenseMap<uint32_t, uint32_t> ParentContexts;
/// Mapping from context IDs to the identifier ID holding the name.
llvm::DenseMap<unsigned, unsigned> ContextNames;
/// Information about Objective-C properties.
///
/// Indexed by the context ID, property name, and whether this is an
/// instance property.
llvm::DenseMap<
std::tuple<unsigned, unsigned, char>,
llvm::SmallVector<std::pair<VersionTuple, ObjCPropertyInfo>, 1>>
ObjCProperties;
/// Information about Objective-C methods.
///
/// Indexed by the context ID, selector ID, and Boolean (stored as a char)
/// indicating whether this is a class or instance method.
llvm::DenseMap<std::tuple<unsigned, unsigned, char>,
llvm::SmallVector<std::pair<VersionTuple, ObjCMethodInfo>, 1>>
ObjCMethods;
/// Information about C++ methods.
///
/// Indexed by the context ID and name ID.
llvm::DenseMap<SingleDeclTableKey,
llvm::SmallVector<std::pair<VersionTuple, CXXMethodInfo>, 1>>
CXXMethods;
/// Mapping from selectors to selector ID.
llvm::DenseMap<StoredObjCSelector, SelectorID> SelectorIDs;
/// Information about global variables.
///
/// Indexed by the context ID, identifier ID.
llvm::DenseMap<
SingleDeclTableKey,
llvm::SmallVector<std::pair<VersionTuple, GlobalVariableInfo>, 1>>
GlobalVariables;
/// Information about global functions.
///
/// Indexed by the context ID, identifier ID.
llvm::DenseMap<
SingleDeclTableKey,
llvm::SmallVector<std::pair<VersionTuple, GlobalFunctionInfo>, 1>>
GlobalFunctions;
/// Information about enumerators.
///
/// Indexed by the identifier ID.
llvm::DenseMap<
unsigned, llvm::SmallVector<std::pair<VersionTuple, EnumConstantInfo>, 1>>
EnumConstants;
/// Information about tags.
///
/// Indexed by the context ID, identifier ID.
llvm::DenseMap<SingleDeclTableKey,
llvm::SmallVector<std::pair<VersionTuple, TagInfo>, 1>>
Tags;
/// Information about typedefs.
///
/// Indexed by the context ID, identifier ID.
llvm::DenseMap<SingleDeclTableKey,
llvm::SmallVector<std::pair<VersionTuple, TypedefInfo>, 1>>
Typedefs;
/// Retrieve the ID for the given identifier.
IdentifierID getIdentifier(StringRef Identifier) {
if (Identifier.empty())
return 0;
auto Known = IdentifierIDs.find(Identifier);
if (Known != IdentifierIDs.end())
return Known->second;
// Add to the identifier table.
Known = IdentifierIDs.insert({Identifier, IdentifierIDs.size() + 1}).first;
return Known->second;
}
/// Retrieve the ID for the given selector.
SelectorID getSelector(ObjCSelectorRef SelectorRef) {
// Translate the selector reference into a stored selector.
StoredObjCSelector Selector;
Selector.NumArgs = SelectorRef.NumArgs;
Selector.Identifiers.reserve(SelectorRef.Identifiers.size());
for (auto piece : SelectorRef.Identifiers)
Selector.Identifiers.push_back(getIdentifier(piece));
// Look for the stored selector.
auto Known = SelectorIDs.find(Selector);
if (Known != SelectorIDs.end())
return Known->second;
// Add to the selector table.
Known = SelectorIDs.insert({Selector, SelectorIDs.size()}).first;
return Known->second;
}
private:
void writeBlockInfoBlock(llvm::BitstreamWriter &Stream);
void writeControlBlock(llvm::BitstreamWriter &Stream);
void writeIdentifierBlock(llvm::BitstreamWriter &Stream);
void writeContextBlock(llvm::BitstreamWriter &Stream);
void writeObjCPropertyBlock(llvm::BitstreamWriter &Stream);
void writeObjCMethodBlock(llvm::BitstreamWriter &Stream);
void writeCXXMethodBlock(llvm::BitstreamWriter &Stream);
void writeObjCSelectorBlock(llvm::BitstreamWriter &Stream);
void writeGlobalVariableBlock(llvm::BitstreamWriter &Stream);
void writeGlobalFunctionBlock(llvm::BitstreamWriter &Stream);
void writeEnumConstantBlock(llvm::BitstreamWriter &Stream);
void writeTagBlock(llvm::BitstreamWriter &Stream);
void writeTypedefBlock(llvm::BitstreamWriter &Stream);
public:
Implementation(llvm::StringRef ModuleName, const FileEntry *SF)
: ModuleName(std::string(ModuleName)), SourceFile(SF) {}
void writeToStream(llvm::raw_ostream &OS);
};
void APINotesWriter::Implementation::writeToStream(llvm::raw_ostream &OS) {
llvm::SmallVector<char, 0> Buffer;
{
llvm::BitstreamWriter Stream(Buffer);
// Emit the signature.
for (unsigned char Byte : API_NOTES_SIGNATURE)
Stream.Emit(Byte, 8);
// Emit the blocks.
writeBlockInfoBlock(Stream);
writeControlBlock(Stream);
writeIdentifierBlock(Stream);
writeContextBlock(Stream);
writeObjCPropertyBlock(Stream);
writeObjCMethodBlock(Stream);
writeCXXMethodBlock(Stream);
writeObjCSelectorBlock(Stream);
writeGlobalVariableBlock(Stream);
writeGlobalFunctionBlock(Stream);
writeEnumConstantBlock(Stream);
writeTagBlock(Stream);
writeTypedefBlock(Stream);
}
OS.write(Buffer.data(), Buffer.size());
OS.flush();
}
namespace {
/// Record the name of a block.
void emitBlockID(llvm::BitstreamWriter &Stream, unsigned ID,
llvm::StringRef Name) {
Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID,
llvm::ArrayRef<unsigned>{ID});
// Emit the block name if present.
if (Name.empty())
return;
Stream.EmitRecord(
llvm::bitc::BLOCKINFO_CODE_BLOCKNAME,
llvm::ArrayRef<unsigned char>(
const_cast<unsigned char *>(
reinterpret_cast<const unsigned char *>(Name.data())),
Name.size()));
}
/// Record the name of a record within a block.
void emitRecordID(llvm::BitstreamWriter &Stream, unsigned ID,
llvm::StringRef Name) {
assert(ID < 256 && "can't fit record ID in next to name");
llvm::SmallVector<unsigned char, 64> Buffer;
Buffer.resize(Name.size() + 1);
Buffer[0] = ID;
memcpy(Buffer.data() + 1, Name.data(), Name.size());
Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Buffer);
}
} // namespace
void APINotesWriter::Implementation::writeBlockInfoBlock(
llvm::BitstreamWriter &Stream) {
llvm::BCBlockRAII Scope(Stream, llvm::bitc::BLOCKINFO_BLOCK_ID, 2);
#define BLOCK(Block) emitBlockID(Stream, Block##_ID, #Block)
#define BLOCK_RECORD(NameSpace, Block) \
emitRecordID(Stream, NameSpace::Block, #Block)
BLOCK(CONTROL_BLOCK);
BLOCK_RECORD(control_block, METADATA);
BLOCK_RECORD(control_block, MODULE_NAME);
BLOCK(IDENTIFIER_BLOCK);
BLOCK_RECORD(identifier_block, IDENTIFIER_DATA);
BLOCK(OBJC_CONTEXT_BLOCK);
BLOCK_RECORD(context_block, CONTEXT_ID_DATA);
BLOCK(OBJC_PROPERTY_BLOCK);
BLOCK_RECORD(objc_property_block, OBJC_PROPERTY_DATA);
BLOCK(OBJC_METHOD_BLOCK);
BLOCK_RECORD(objc_method_block, OBJC_METHOD_DATA);
BLOCK(OBJC_SELECTOR_BLOCK);
BLOCK_RECORD(objc_selector_block, OBJC_SELECTOR_DATA);
BLOCK(GLOBAL_VARIABLE_BLOCK);
BLOCK_RECORD(global_variable_block, GLOBAL_VARIABLE_DATA);
BLOCK(GLOBAL_FUNCTION_BLOCK);
BLOCK_RECORD(global_function_block, GLOBAL_FUNCTION_DATA);
#undef BLOCK_RECORD
#undef BLOCK
}
void APINotesWriter::Implementation::writeControlBlock(
llvm::BitstreamWriter &Stream) {
llvm::BCBlockRAII Scope(Stream, CONTROL_BLOCK_ID, 3);
control_block::MetadataLayout Metadata(Stream);
Metadata.emit(Scratch, VERSION_MAJOR, VERSION_MINOR);
control_block::ModuleNameLayout ModuleName(Stream);
ModuleName.emit(Scratch, this->ModuleName);
if (SourceFile) {
control_block::SourceFileLayout SourceFile(Stream);
SourceFile.emit(Scratch, this->SourceFile->getSize(),
this->SourceFile->getModificationTime());
}
}
namespace {
/// Used to serialize the on-disk identifier table.
class IdentifierTableInfo {
public:
using key_type = StringRef;
using key_type_ref = key_type;
using data_type = IdentifierID;
using data_type_ref = const data_type &;
using hash_value_type = uint32_t;
using offset_type = unsigned;
hash_value_type ComputeHash(key_type_ref Key) { return llvm::djbHash(Key); }
std::pair<unsigned, unsigned>
EmitKeyDataLength(raw_ostream &OS, key_type_ref Key, data_type_ref) {
uint32_t KeyLength = Key.size();
uint32_t DataLength = sizeof(uint32_t);
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint16_t>(KeyLength);
writer.write<uint16_t>(DataLength);
return {KeyLength, DataLength};
}
void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) { OS << Key; }
void EmitData(raw_ostream &OS, key_type_ref, data_type_ref Data, unsigned) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint32_t>(Data);
}
};
} // namespace
void APINotesWriter::Implementation::writeIdentifierBlock(
llvm::BitstreamWriter &Stream) {
llvm::BCBlockRAII restoreBlock(Stream, IDENTIFIER_BLOCK_ID, 3);
if (IdentifierIDs.empty())
return;
llvm::SmallString<4096> HashTableBlob;
uint32_t Offset;
{
llvm::OnDiskChainedHashTableGenerator<IdentifierTableInfo> Generator;
for (auto &II : IdentifierIDs)
Generator.insert(II.first(), II.second);
llvm::raw_svector_ostream BlobStream(HashTableBlob);
// Make sure that no bucket is at offset 0
llvm::support::endian::write<uint32_t>(BlobStream, 0,
llvm::endianness::little);
Offset = Generator.Emit(BlobStream);
}
identifier_block::IdentifierDataLayout IdentifierData(Stream);
IdentifierData.emit(Scratch, Offset, HashTableBlob);
}
namespace {
/// Used to serialize the on-disk Objective-C context table.
class ContextIDTableInfo {
public:
using key_type = ContextTableKey;
using key_type_ref = key_type;
using data_type = unsigned;
using data_type_ref = const data_type &;
using hash_value_type = size_t;
using offset_type = unsigned;
hash_value_type ComputeHash(key_type_ref Key) {
return static_cast<size_t>(Key.hashValue());
}
std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &OS, key_type_ref,
data_type_ref) {
uint32_t KeyLength = sizeof(uint32_t) + sizeof(uint8_t) + sizeof(uint32_t);
uint32_t DataLength = sizeof(uint32_t);
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint16_t>(KeyLength);
writer.write<uint16_t>(DataLength);
return {KeyLength, DataLength};
}
void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint32_t>(Key.parentContextID);
writer.write<uint8_t>(Key.contextKind);
writer.write<uint32_t>(Key.contextID);
}
void EmitData(raw_ostream &OS, key_type_ref, data_type_ref Data, unsigned) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint32_t>(Data);
}
};
/// Localized helper to make a type dependent, thwarting template argument
/// deduction.
template <typename T> struct MakeDependent { typedef T Type; };
/// Retrieve the serialized size of the given VersionTuple, for use in
/// on-disk hash tables.
unsigned getVersionTupleSize(const VersionTuple &VT) {
unsigned size = sizeof(uint8_t) + /*major*/ sizeof(uint32_t);
if (VT.getMinor())
size += sizeof(uint32_t);
if (VT.getSubminor())
size += sizeof(uint32_t);
if (VT.getBuild())
size += sizeof(uint32_t);
return size;
}
/// Determine the size of an array of versioned information,
template <typename T>
unsigned getVersionedInfoSize(
const llvm::SmallVectorImpl<std::pair<llvm::VersionTuple, T>> &VI,
llvm::function_ref<unsigned(const typename MakeDependent<T>::Type &)>
getInfoSize) {
unsigned result = sizeof(uint16_t); // # of elements
for (const auto &E : VI) {
result += getVersionTupleSize(E.first);
result += getInfoSize(E.second);
}
return result;
}
/// Emit a serialized representation of a version tuple.
void emitVersionTuple(raw_ostream &OS, const VersionTuple &VT) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
// First byte contains the number of components beyond the 'major' component.
uint8_t descriptor;
if (VT.getBuild())
descriptor = 3;
else if (VT.getSubminor())
descriptor = 2;
else if (VT.getMinor())
descriptor = 1;
else
descriptor = 0;
writer.write<uint8_t>(descriptor);
// Write the components.
writer.write<uint32_t>(VT.getMajor());
if (auto minor = VT.getMinor())
writer.write<uint32_t>(*minor);
if (auto subminor = VT.getSubminor())
writer.write<uint32_t>(*subminor);
if (auto build = VT.getBuild())
writer.write<uint32_t>(*build);
}
/// Emit versioned information.
template <typename T>
void emitVersionedInfo(
raw_ostream &OS, llvm::SmallVectorImpl<std::pair<VersionTuple, T>> &VI,
llvm::function_ref<void(raw_ostream &,
const typename MakeDependent<T>::Type &)>
emitInfo) {
std::sort(VI.begin(), VI.end(),
[](const std::pair<VersionTuple, T> &LHS,
const std::pair<VersionTuple, T> &RHS) -> bool {
assert((&LHS == &RHS || LHS.first != RHS.first) &&
"two entries for the same version");
return LHS.first < RHS.first;
});
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint16_t>(VI.size());
for (const auto &E : VI) {
emitVersionTuple(OS, E.first);
emitInfo(OS, E.second);
}
}
/// On-disk hash table info key base for handling versioned data.
template <typename Derived, typename KeyType, typename UnversionedDataType>
class VersionedTableInfo {
Derived &asDerived() { return *static_cast<Derived *>(this); }
const Derived &asDerived() const {
return *static_cast<const Derived *>(this);
}
public:
using key_type = KeyType;
using key_type_ref = key_type;
using data_type =
llvm::SmallVector<std::pair<llvm::VersionTuple, UnversionedDataType>, 1>;
using data_type_ref = data_type &;
using hash_value_type = size_t;
using offset_type = unsigned;
std::pair<unsigned, unsigned>
EmitKeyDataLength(raw_ostream &OS, key_type_ref Key, data_type_ref Data) {
uint32_t KeyLength = asDerived().getKeyLength(Key);
uint32_t DataLength =
getVersionedInfoSize(Data, [this](const UnversionedDataType &UI) {
return asDerived().getUnversionedInfoSize(UI);
});
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint16_t>(KeyLength);
writer.write<uint16_t>(DataLength);
return {KeyLength, DataLength};
}
void EmitData(raw_ostream &OS, key_type_ref, data_type_ref Data, unsigned) {
emitVersionedInfo(
OS, Data, [this](llvm::raw_ostream &OS, const UnversionedDataType &UI) {
asDerived().emitUnversionedInfo(OS, UI);
});
}
};
/// Emit a serialized representation of the common entity information.
void emitCommonEntityInfo(raw_ostream &OS, const CommonEntityInfo &CEI) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
uint8_t payload = 0;
if (auto swiftPrivate = CEI.isSwiftPrivate()) {
payload |= 0x01;
if (*swiftPrivate)
payload |= 0x02;
}
payload <<= 1;
payload |= CEI.Unavailable;
payload <<= 1;
payload |= CEI.UnavailableInSwift;
writer.write<uint8_t>(payload);
writer.write<uint16_t>(CEI.UnavailableMsg.size());
OS.write(CEI.UnavailableMsg.c_str(), CEI.UnavailableMsg.size());
writer.write<uint16_t>(CEI.SwiftName.size());
OS.write(CEI.SwiftName.c_str(), CEI.SwiftName.size());
}
/// Retrieve the serialized size of the given CommonEntityInfo, for use in
/// on-disk hash tables.
unsigned getCommonEntityInfoSize(const CommonEntityInfo &CEI) {
return 5 + CEI.UnavailableMsg.size() + CEI.SwiftName.size();
}
// Retrieve the serialized size of the given CommonTypeInfo, for use
// in on-disk hash tables.
unsigned getCommonTypeInfoSize(const CommonTypeInfo &CTI) {
return 2 + (CTI.getSwiftBridge() ? CTI.getSwiftBridge()->size() : 0) + 2 +
(CTI.getNSErrorDomain() ? CTI.getNSErrorDomain()->size() : 0) +
getCommonEntityInfoSize(CTI);
}
/// Emit a serialized representation of the common type information.
void emitCommonTypeInfo(raw_ostream &OS, const CommonTypeInfo &CTI) {
emitCommonEntityInfo(OS, CTI);
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
if (auto swiftBridge = CTI.getSwiftBridge()) {
writer.write<uint16_t>(swiftBridge->size() + 1);
OS.write(swiftBridge->c_str(), swiftBridge->size());
} else {
writer.write<uint16_t>(0);
}
if (auto nsErrorDomain = CTI.getNSErrorDomain()) {
writer.write<uint16_t>(nsErrorDomain->size() + 1);
OS.write(nsErrorDomain->c_str(), CTI.getNSErrorDomain()->size());
} else {
writer.write<uint16_t>(0);
}
}
/// Used to serialize the on-disk Objective-C property table.
class ContextInfoTableInfo
: public VersionedTableInfo<ContextInfoTableInfo, unsigned, ContextInfo> {
public:
unsigned getKeyLength(key_type_ref) { return sizeof(uint32_t); }
void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint32_t>(Key);
}
hash_value_type ComputeHash(key_type_ref Key) {
return static_cast<size_t>(llvm::hash_value(Key));
}
unsigned getUnversionedInfoSize(const ContextInfo &OCI) {
return getCommonTypeInfoSize(OCI) + 1;
}
void emitUnversionedInfo(raw_ostream &OS, const ContextInfo &OCI) {
emitCommonTypeInfo(OS, OCI);
uint8_t payload = 0;
if (auto swiftImportAsNonGeneric = OCI.getSwiftImportAsNonGeneric())
payload |= (0x01 << 1) | (uint8_t)swiftImportAsNonGeneric.value();
payload <<= 2;
if (auto swiftObjCMembers = OCI.getSwiftObjCMembers())
payload |= (0x01 << 1) | (uint8_t)swiftObjCMembers.value();
payload <<= 3;
if (auto nullable = OCI.getDefaultNullability())
payload |= (0x01 << 2) | static_cast<uint8_t>(*nullable);
payload = (payload << 1) | (OCI.hasDesignatedInits() ? 1 : 0);
OS << payload;
}
};
} // namespace
void APINotesWriter::Implementation::writeContextBlock(
llvm::BitstreamWriter &Stream) {
llvm::BCBlockRAII restoreBlock(Stream, OBJC_CONTEXT_BLOCK_ID, 3);
if (Contexts.empty())
return;
{
llvm::SmallString<4096> HashTableBlob;
uint32_t Offset;
{
llvm::OnDiskChainedHashTableGenerator<ContextIDTableInfo> Generator;
for (auto &OC : Contexts)
Generator.insert(OC.first, OC.second.first);
llvm::raw_svector_ostream BlobStream(HashTableBlob);
// Make sure that no bucket is at offset 0
llvm::support::endian::write<uint32_t>(BlobStream, 0,
llvm::endianness::little);
Offset = Generator.Emit(BlobStream);
}
context_block::ContextIDLayout ContextID(Stream);
ContextID.emit(Scratch, Offset, HashTableBlob);
}
{
llvm::SmallString<4096> HashTableBlob;
uint32_t Offset;
{
llvm::OnDiskChainedHashTableGenerator<ContextInfoTableInfo> Generator;
for (auto &OC : Contexts)
Generator.insert(OC.second.first, OC.second.second);
llvm::raw_svector_ostream BlobStream(HashTableBlob);
// Make sure that no bucket is at offset 0
llvm::support::endian::write<uint32_t>(BlobStream, 0,
llvm::endianness::little);
Offset = Generator.Emit(BlobStream);
}
context_block::ContextInfoLayout ContextInfo(Stream);
ContextInfo.emit(Scratch, Offset, HashTableBlob);
}
}
namespace {
/// Retrieve the serialized size of the given VariableInfo, for use in
/// on-disk hash tables.
unsigned getVariableInfoSize(const VariableInfo &VI) {
return 2 + getCommonEntityInfoSize(VI) + 2 + VI.getType().size();
}
/// Emit a serialized representation of the variable information.
void emitVariableInfo(raw_ostream &OS, const VariableInfo &VI) {
emitCommonEntityInfo(OS, VI);
uint8_t bytes[2] = {0, 0};
if (auto nullable = VI.getNullability()) {
bytes[0] = 1;
bytes[1] = static_cast<uint8_t>(*nullable);
} else {
// Nothing to do.
}
OS.write(reinterpret_cast<const char *>(bytes), 2);
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint16_t>(VI.getType().size());
OS.write(VI.getType().data(), VI.getType().size());
}
/// Used to serialize the on-disk Objective-C property table.
class ObjCPropertyTableInfo
: public VersionedTableInfo<ObjCPropertyTableInfo,
std::tuple<unsigned, unsigned, char>,
ObjCPropertyInfo> {
public:
unsigned getKeyLength(key_type_ref) {
return sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t);
}
void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint32_t>(std::get<0>(Key));
writer.write<uint32_t>(std::get<1>(Key));
writer.write<uint8_t>(std::get<2>(Key));
}
hash_value_type ComputeHash(key_type_ref Key) {
return static_cast<size_t>(llvm::hash_value(Key));
}
unsigned getUnversionedInfoSize(const ObjCPropertyInfo &OPI) {
return getVariableInfoSize(OPI) + 1;
}
void emitUnversionedInfo(raw_ostream &OS, const ObjCPropertyInfo &OPI) {
emitVariableInfo(OS, OPI);
uint8_t flags = 0;
if (auto value = OPI.getSwiftImportAsAccessors()) {
flags |= 1 << 0;
flags |= value.value() << 1;
}
OS << flags;
}
};
} // namespace
void APINotesWriter::Implementation::writeObjCPropertyBlock(
llvm::BitstreamWriter &Stream) {
llvm::BCBlockRAII Scope(Stream, OBJC_PROPERTY_BLOCK_ID, 3);
if (ObjCProperties.empty())
return;
{
llvm::SmallString<4096> HashTableBlob;
uint32_t Offset;
{
llvm::OnDiskChainedHashTableGenerator<ObjCPropertyTableInfo> Generator;
for (auto &OP : ObjCProperties)
Generator.insert(OP.first, OP.second);
llvm::raw_svector_ostream BlobStream(HashTableBlob);
// Make sure that no bucket is at offset 0
llvm::support::endian::write<uint32_t>(BlobStream, 0,
llvm::endianness::little);
Offset = Generator.Emit(BlobStream);
}
objc_property_block::ObjCPropertyDataLayout ObjCPropertyData(Stream);
ObjCPropertyData.emit(Scratch, Offset, HashTableBlob);
}
}
namespace {
unsigned getFunctionInfoSize(const FunctionInfo &);
void emitFunctionInfo(llvm::raw_ostream &, const FunctionInfo &);
/// Used to serialize the on-disk Objective-C method table.
class ObjCMethodTableInfo
: public VersionedTableInfo<ObjCMethodTableInfo,
std::tuple<unsigned, unsigned, char>,
ObjCMethodInfo> {
public:
unsigned getKeyLength(key_type_ref) {
return sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t);
}
void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint32_t>(std::get<0>(Key));
writer.write<uint32_t>(std::get<1>(Key));
writer.write<uint8_t>(std::get<2>(Key));
}
hash_value_type ComputeHash(key_type_ref key) {
return static_cast<size_t>(llvm::hash_value(key));
}
unsigned getUnversionedInfoSize(const ObjCMethodInfo &OMI) {
return getFunctionInfoSize(OMI) + 1;
}
void emitUnversionedInfo(raw_ostream &OS, const ObjCMethodInfo &OMI) {
uint8_t flags = 0;
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
flags = (flags << 1) | OMI.DesignatedInit;
flags = (flags << 1) | OMI.RequiredInit;
writer.write<uint8_t>(flags);
emitFunctionInfo(OS, OMI);
}
};
/// Used to serialize the on-disk C++ method table.
class CXXMethodTableInfo
: public VersionedTableInfo<CXXMethodTableInfo, SingleDeclTableKey,
CXXMethodInfo> {
public:
unsigned getKeyLength(key_type_ref) {
return sizeof(uint32_t) + sizeof(uint32_t);
}
void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint32_t>(Key.parentContextID);
writer.write<uint32_t>(Key.nameID);
}
hash_value_type ComputeHash(key_type_ref key) {
return static_cast<size_t>(key.hashValue());
}
unsigned getUnversionedInfoSize(const CXXMethodInfo &OMI) {
return getFunctionInfoSize(OMI);
}
void emitUnversionedInfo(raw_ostream &OS, const CXXMethodInfo &OMI) {
emitFunctionInfo(OS, OMI);
}
};
} // namespace
void APINotesWriter::Implementation::writeObjCMethodBlock(
llvm::BitstreamWriter &Stream) {
llvm::BCBlockRAII Scope(Stream, OBJC_METHOD_BLOCK_ID, 3);
if (ObjCMethods.empty())
return;
{
llvm::SmallString<4096> HashTableBlob;
uint32_t Offset;
{
llvm::OnDiskChainedHashTableGenerator<ObjCMethodTableInfo> Generator;
for (auto &OM : ObjCMethods)
Generator.insert(OM.first, OM.second);
llvm::raw_svector_ostream BlobStream(HashTableBlob);
// Make sure that no bucket is at offset 0
llvm::support::endian::write<uint32_t>(BlobStream, 0,
llvm::endianness::little);
Offset = Generator.Emit(BlobStream);
}
objc_method_block::ObjCMethodDataLayout ObjCMethodData(Stream);
ObjCMethodData.emit(Scratch, Offset, HashTableBlob);
}
}
void APINotesWriter::Implementation::writeCXXMethodBlock(
llvm::BitstreamWriter &Stream) {
llvm::BCBlockRAII Scope(Stream, CXX_METHOD_BLOCK_ID, 3);
if (CXXMethods.empty())
return;
{
llvm::SmallString<4096> HashTableBlob;
uint32_t Offset;
{
llvm::OnDiskChainedHashTableGenerator<CXXMethodTableInfo> Generator;
for (auto &MD : CXXMethods)
Generator.insert(MD.first, MD.second);
llvm::raw_svector_ostream BlobStream(HashTableBlob);
// Make sure that no bucket is at offset 0
llvm::support::endian::write<uint32_t>(BlobStream, 0,
llvm::endianness::little);
Offset = Generator.Emit(BlobStream);
}
cxx_method_block::CXXMethodDataLayout CXXMethodData(Stream);
CXXMethodData.emit(Scratch, Offset, HashTableBlob);
}
}
namespace {
/// Used to serialize the on-disk Objective-C selector table.
class ObjCSelectorTableInfo {
public:
using key_type = StoredObjCSelector;
using key_type_ref = const key_type &;
using data_type = SelectorID;
using data_type_ref = data_type;
using hash_value_type = unsigned;
using offset_type = unsigned;
hash_value_type ComputeHash(key_type_ref Key) {
return llvm::DenseMapInfo<StoredObjCSelector>::getHashValue(Key);
}
std::pair<unsigned, unsigned>
EmitKeyDataLength(raw_ostream &OS, key_type_ref Key, data_type_ref) {
uint32_t KeyLength =
sizeof(uint16_t) + sizeof(uint32_t) * Key.Identifiers.size();
uint32_t DataLength = sizeof(uint32_t);
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint16_t>(KeyLength);
writer.write<uint16_t>(DataLength);
return {KeyLength, DataLength};
}
void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint16_t>(Key.NumArgs);
for (auto Identifier : Key.Identifiers)
writer.write<uint32_t>(Identifier);
}
void EmitData(raw_ostream &OS, key_type_ref, data_type_ref Data, unsigned) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint32_t>(Data);
}
};
} // namespace
void APINotesWriter::Implementation::writeObjCSelectorBlock(
llvm::BitstreamWriter &Stream) {
llvm::BCBlockRAII Scope(Stream, OBJC_SELECTOR_BLOCK_ID, 3);
if (SelectorIDs.empty())
return;
{
llvm::SmallString<4096> HashTableBlob;
uint32_t Offset;
{
llvm::OnDiskChainedHashTableGenerator<ObjCSelectorTableInfo> Generator;
for (auto &S : SelectorIDs)
Generator.insert(S.first, S.second);
llvm::raw_svector_ostream BlobStream(HashTableBlob);
// Make sure that no bucket is at offset 0
llvm::support::endian::write<uint32_t>(BlobStream, 0,
llvm::endianness::little);
Offset = Generator.Emit(BlobStream);
}
objc_selector_block::ObjCSelectorDataLayout ObjCSelectorData(Stream);
ObjCSelectorData.emit(Scratch, Offset, HashTableBlob);
}
}
namespace {
/// Used to serialize the on-disk global variable table.
class GlobalVariableTableInfo
: public VersionedTableInfo<GlobalVariableTableInfo, SingleDeclTableKey,
GlobalVariableInfo> {
public:
unsigned getKeyLength(key_type_ref) {
return sizeof(uint32_t) + sizeof(uint32_t);
}
void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint32_t>(Key.parentContextID);
writer.write<uint32_t>(Key.nameID);
}
hash_value_type ComputeHash(key_type_ref Key) {
return static_cast<size_t>(Key.hashValue());
}
unsigned getUnversionedInfoSize(const GlobalVariableInfo &GVI) {
return getVariableInfoSize(GVI);
}
void emitUnversionedInfo(raw_ostream &OS, const GlobalVariableInfo &GVI) {
emitVariableInfo(OS, GVI);
}
};
} // namespace
void APINotesWriter::Implementation::writeGlobalVariableBlock(
llvm::BitstreamWriter &Stream) {
llvm::BCBlockRAII Scope(Stream, GLOBAL_VARIABLE_BLOCK_ID, 3);
if (GlobalVariables.empty())
return;
{
llvm::SmallString<4096> HashTableBlob;
uint32_t Offset;
{
llvm::OnDiskChainedHashTableGenerator<GlobalVariableTableInfo> Generator;
for (auto &GV : GlobalVariables)
Generator.insert(GV.first, GV.second);
llvm::raw_svector_ostream BlobStream(HashTableBlob);
// Make sure that no bucket is at offset 0
llvm::support::endian::write<uint32_t>(BlobStream, 0,
llvm::endianness::little);
Offset = Generator.Emit(BlobStream);
}
global_variable_block::GlobalVariableDataLayout GlobalVariableData(Stream);
GlobalVariableData.emit(Scratch, Offset, HashTableBlob);
}
}
namespace {
unsigned getParamInfoSize(const ParamInfo &PI) {
return getVariableInfoSize(PI) + 1;
}
void emitParamInfo(raw_ostream &OS, const ParamInfo &PI) {
emitVariableInfo(OS, PI);
uint8_t flags = 0;
if (auto noescape = PI.isNoEscape()) {
flags |= 0x01;
if (*noescape)
flags |= 0x02;
}
flags <<= 3;
if (auto RCC = PI.getRetainCountConvention())
flags |= static_cast<uint8_t>(RCC.value()) + 1;
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint8_t>(flags);
}
/// Retrieve the serialized size of the given FunctionInfo, for use in on-disk
/// hash tables.
unsigned getFunctionInfoSize(const FunctionInfo &FI) {
unsigned size = getCommonEntityInfoSize(FI) + 2 + sizeof(uint64_t);
size += sizeof(uint16_t);
for (const auto &P : FI.Params)
size += getParamInfoSize(P);
size += sizeof(uint16_t) + FI.ResultType.size();
return size;
}
/// Emit a serialized representation of the function information.
void emitFunctionInfo(raw_ostream &OS, const FunctionInfo &FI) {
emitCommonEntityInfo(OS, FI);
uint8_t flags = 0;
flags |= FI.NullabilityAudited;
flags <<= 3;
if (auto RCC = FI.getRetainCountConvention())
flags |= static_cast<uint8_t>(RCC.value()) + 1;
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint8_t>(flags);
writer.write<uint8_t>(FI.NumAdjustedNullable);
writer.write<uint64_t>(FI.NullabilityPayload);
writer.write<uint16_t>(FI.Params.size());
for (const auto &PI : FI.Params)
emitParamInfo(OS, PI);
writer.write<uint16_t>(FI.ResultType.size());
writer.write(ArrayRef<char>{FI.ResultType.data(), FI.ResultType.size()});
}
/// Used to serialize the on-disk global function table.
class GlobalFunctionTableInfo
: public VersionedTableInfo<GlobalFunctionTableInfo, SingleDeclTableKey,
GlobalFunctionInfo> {
public:
unsigned getKeyLength(key_type_ref) {
return sizeof(uint32_t) + sizeof(uint32_t);
}
void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint32_t>(Key.parentContextID);
writer.write<uint32_t>(Key.nameID);
}
hash_value_type ComputeHash(key_type_ref Key) {
return static_cast<size_t>(Key.hashValue());
}
unsigned getUnversionedInfoSize(const GlobalFunctionInfo &GFI) {
return getFunctionInfoSize(GFI);
}
void emitUnversionedInfo(raw_ostream &OS, const GlobalFunctionInfo &GFI) {
emitFunctionInfo(OS, GFI);
}
};
} // namespace
void APINotesWriter::Implementation::writeGlobalFunctionBlock(
llvm::BitstreamWriter &Stream) {
llvm::BCBlockRAII Scope(Stream, GLOBAL_FUNCTION_BLOCK_ID, 3);
if (GlobalFunctions.empty())
return;
{
llvm::SmallString<4096> HashTableBlob;
uint32_t Offset;
{
llvm::OnDiskChainedHashTableGenerator<GlobalFunctionTableInfo> Generator;
for (auto &F : GlobalFunctions)
Generator.insert(F.first, F.second);
llvm::raw_svector_ostream BlobStream(HashTableBlob);
// Make sure that no bucket is at offset 0
llvm::support::endian::write<uint32_t>(BlobStream, 0,
llvm::endianness::little);
Offset = Generator.Emit(BlobStream);
}
global_function_block::GlobalFunctionDataLayout GlobalFunctionData(Stream);
GlobalFunctionData.emit(Scratch, Offset, HashTableBlob);
}
}
namespace {
/// Used to serialize the on-disk global enum constant.
class EnumConstantTableInfo
: public VersionedTableInfo<EnumConstantTableInfo, unsigned,
EnumConstantInfo> {
public:
unsigned getKeyLength(key_type_ref) { return sizeof(uint32_t); }
void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint32_t>(Key);
}
hash_value_type ComputeHash(key_type_ref Key) {
return static_cast<size_t>(llvm::hash_value(Key));
}
unsigned getUnversionedInfoSize(const EnumConstantInfo &ECI) {
return getCommonEntityInfoSize(ECI);
}
void emitUnversionedInfo(raw_ostream &OS, const EnumConstantInfo &ECI) {
emitCommonEntityInfo(OS, ECI);
}
};
} // namespace
void APINotesWriter::Implementation::writeEnumConstantBlock(
llvm::BitstreamWriter &Stream) {
llvm::BCBlockRAII Scope(Stream, ENUM_CONSTANT_BLOCK_ID, 3);
if (EnumConstants.empty())
return;
{
llvm::SmallString<4096> HashTableBlob;
uint32_t Offset;
{
llvm::OnDiskChainedHashTableGenerator<EnumConstantTableInfo> Generator;
for (auto &EC : EnumConstants)
Generator.insert(EC.first, EC.second);
llvm::raw_svector_ostream BlobStream(HashTableBlob);
// Make sure that no bucket is at offset 0
llvm::support::endian::write<uint32_t>(BlobStream, 0,
llvm::endianness::little);
Offset = Generator.Emit(BlobStream);
}
enum_constant_block::EnumConstantDataLayout EnumConstantData(Stream);
EnumConstantData.emit(Scratch, Offset, HashTableBlob);
}
}
namespace {
template <typename Derived, typename UnversionedDataType>
class CommonTypeTableInfo
: public VersionedTableInfo<Derived, SingleDeclTableKey,
UnversionedDataType> {
public:
using key_type_ref = typename CommonTypeTableInfo::key_type_ref;
using hash_value_type = typename CommonTypeTableInfo::hash_value_type;
unsigned getKeyLength(key_type_ref) {
return sizeof(uint32_t) + sizeof(IdentifierID);
}
void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
writer.write<uint32_t>(Key.parentContextID);
writer.write<IdentifierID>(Key.nameID);
}
hash_value_type ComputeHash(key_type_ref Key) {
return static_cast<size_t>(Key.hashValue());
}
unsigned getUnversionedInfoSize(const UnversionedDataType &UDT) {
return getCommonTypeInfoSize(UDT);
}
void emitUnversionedInfo(raw_ostream &OS, const UnversionedDataType &UDT) {
emitCommonTypeInfo(OS, UDT);
}
};
/// Used to serialize the on-disk tag table.
class TagTableInfo : public CommonTypeTableInfo<TagTableInfo, TagInfo> {
public:
unsigned getUnversionedInfoSize(const TagInfo &TI) {
return 2 + (TI.SwiftImportAs ? TI.SwiftImportAs->size() : 0) +
2 + (TI.SwiftRetainOp ? TI.SwiftRetainOp->size() : 0) +
2 + (TI.SwiftReleaseOp ? TI.SwiftReleaseOp->size() : 0) +
2 + getCommonTypeInfoSize(TI);
}
void emitUnversionedInfo(raw_ostream &OS, const TagInfo &TI) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
uint8_t Flags = 0;
if (auto extensibility = TI.EnumExtensibility) {
Flags |= static_cast<uint8_t>(extensibility.value()) + 1;
assert((Flags < (1 << 2)) && "must fit in two bits");
}
Flags <<= 2;
if (auto value = TI.isFlagEnum())
Flags |= (value.value() << 1 | 1 << 0);
writer.write<uint8_t>(Flags);
if (auto Copyable = TI.isSwiftCopyable())
writer.write<uint8_t>(*Copyable ? kSwiftCopyable : kSwiftNonCopyable);
else
writer.write<uint8_t>(0);
if (auto ImportAs = TI.SwiftImportAs) {
writer.write<uint16_t>(ImportAs->size() + 1);
OS.write(ImportAs->c_str(), ImportAs->size());
} else {
writer.write<uint16_t>(0);
}
if (auto RetainOp = TI.SwiftRetainOp) {
writer.write<uint16_t>(RetainOp->size() + 1);
OS.write(RetainOp->c_str(), RetainOp->size());
} else {
writer.write<uint16_t>(0);
}
if (auto ReleaseOp = TI.SwiftReleaseOp) {
writer.write<uint16_t>(ReleaseOp->size() + 1);
OS.write(ReleaseOp->c_str(), ReleaseOp->size());
} else {
writer.write<uint16_t>(0);
}
emitCommonTypeInfo(OS, TI);
}
};
} // namespace
void APINotesWriter::Implementation::writeTagBlock(
llvm::BitstreamWriter &Stream) {
llvm::BCBlockRAII Scope(Stream, TAG_BLOCK_ID, 3);
if (Tags.empty())
return;
{
llvm::SmallString<4096> HashTableBlob;
uint32_t Offset;
{
llvm::OnDiskChainedHashTableGenerator<TagTableInfo> Generator;
for (auto &T : Tags)
Generator.insert(T.first, T.second);
llvm::raw_svector_ostream BlobStream(HashTableBlob);
// Make sure that no bucket is at offset 0
llvm::support::endian::write<uint32_t>(BlobStream, 0,
llvm::endianness::little);
Offset = Generator.Emit(BlobStream);
}
tag_block::TagDataLayout TagData(Stream);
TagData.emit(Scratch, Offset, HashTableBlob);
}
}
namespace {
/// Used to serialize the on-disk typedef table.
class TypedefTableInfo
: public CommonTypeTableInfo<TypedefTableInfo, TypedefInfo> {
public:
unsigned getUnversionedInfoSize(const TypedefInfo &TI) {
return 1 + getCommonTypeInfoSize(TI);
}
void emitUnversionedInfo(raw_ostream &OS, const TypedefInfo &TI) {
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
uint8_t Flags = 0;
if (auto swiftWrapper = TI.SwiftWrapper)
Flags |= static_cast<uint8_t>(*swiftWrapper) + 1;
writer.write<uint8_t>(Flags);
emitCommonTypeInfo(OS, TI);
}
};
} // namespace
void APINotesWriter::Implementation::writeTypedefBlock(
llvm::BitstreamWriter &Stream) {
llvm::BCBlockRAII Scope(Stream, TYPEDEF_BLOCK_ID, 3);
if (Typedefs.empty())
return;
{
llvm::SmallString<4096> HashTableBlob;
uint32_t Offset;
{
llvm::OnDiskChainedHashTableGenerator<TypedefTableInfo> Generator;
for (auto &T : Typedefs)
Generator.insert(T.first, T.second);
llvm::raw_svector_ostream BlobStream(HashTableBlob);
// Make sure that no bucket is at offset 0
llvm::support::endian::write<uint32_t>(BlobStream, 0,
llvm::endianness::little);
Offset = Generator.Emit(BlobStream);
}
typedef_block::TypedefDataLayout TypedefData(Stream);
TypedefData.emit(Scratch, Offset, HashTableBlob);
}
}
// APINotesWriter
APINotesWriter::APINotesWriter(llvm::StringRef ModuleName, const FileEntry *SF)
: Implementation(new class Implementation(ModuleName, SF)) {}
APINotesWriter::~APINotesWriter() = default;
void APINotesWriter::writeToStream(llvm::raw_ostream &OS) {
Implementation->writeToStream(OS);
}
ContextID APINotesWriter::addContext(std::optional<ContextID> ParentCtxID,
llvm::StringRef Name, ContextKind Kind,
const ContextInfo &Info,
llvm::VersionTuple SwiftVersion) {
IdentifierID NameID = Implementation->getIdentifier(Name);
uint32_t RawParentCtxID = ParentCtxID ? ParentCtxID->Value : -1;
ContextTableKey Key(RawParentCtxID, static_cast<uint8_t>(Kind), NameID);
auto Known = Implementation->Contexts.find(Key);
if (Known == Implementation->Contexts.end()) {
unsigned NextID = Implementation->Contexts.size() + 1;
Implementation::VersionedSmallVector<ContextInfo> EmptyVersionedInfo;
Known = Implementation->Contexts
.insert(std::make_pair(
Key, std::make_pair(NextID, EmptyVersionedInfo)))
.first;
Implementation->ContextNames[NextID] = NameID;
Implementation->ParentContexts[NextID] = RawParentCtxID;
}
// Add this version information.
auto &VersionedVec = Known->second.second;
bool Found = false;
for (auto &Versioned : VersionedVec) {
if (Versioned.first == SwiftVersion) {
Versioned.second |= Info;
Found = true;
break;
}
}
if (!Found)
VersionedVec.push_back({SwiftVersion, Info});
return ContextID(Known->second.first);
}
void APINotesWriter::addObjCProperty(ContextID CtxID, StringRef Name,
bool IsInstanceProperty,
const ObjCPropertyInfo &Info,
VersionTuple SwiftVersion) {
IdentifierID NameID = Implementation->getIdentifier(Name);
Implementation
->ObjCProperties[std::make_tuple(CtxID.Value, NameID, IsInstanceProperty)]
.push_back({SwiftVersion, Info});
}
void APINotesWriter::addObjCMethod(ContextID CtxID, ObjCSelectorRef Selector,
bool IsInstanceMethod,
const ObjCMethodInfo &Info,
VersionTuple SwiftVersion) {
SelectorID SelID = Implementation->getSelector(Selector);
auto Key = std::tuple<unsigned, unsigned, char>{CtxID.Value, SelID,
IsInstanceMethod};
Implementation->ObjCMethods[Key].push_back({SwiftVersion, Info});
// If this method is a designated initializer, update the class to note that
// it has designated initializers.
if (Info.DesignatedInit) {
assert(Implementation->ParentContexts.contains(CtxID.Value));
uint32_t ParentCtxID = Implementation->ParentContexts[CtxID.Value];
ContextTableKey CtxKey(ParentCtxID,
static_cast<uint8_t>(ContextKind::ObjCClass),
Implementation->ContextNames[CtxID.Value]);
assert(Implementation->Contexts.contains(CtxKey));
auto &VersionedVec = Implementation->Contexts[CtxKey].second;
bool Found = false;
for (auto &Versioned : VersionedVec) {
if (Versioned.first == SwiftVersion) {
Versioned.second.setHasDesignatedInits(true);
Found = true;
break;
}
}
if (!Found) {
VersionedVec.push_back({SwiftVersion, ContextInfo()});
VersionedVec.back().second.setHasDesignatedInits(true);
}
}
}
void APINotesWriter::addCXXMethod(ContextID CtxID, llvm::StringRef Name,
const CXXMethodInfo &Info,
VersionTuple SwiftVersion) {
IdentifierID NameID = Implementation->getIdentifier(Name);
SingleDeclTableKey Key(CtxID.Value, NameID);
Implementation->CXXMethods[Key].push_back({SwiftVersion, Info});
}
void APINotesWriter::addGlobalVariable(std::optional<Context> Ctx,
llvm::StringRef Name,
const GlobalVariableInfo &Info,
VersionTuple SwiftVersion) {
IdentifierID VariableID = Implementation->getIdentifier(Name);
SingleDeclTableKey Key(Ctx, VariableID);
Implementation->GlobalVariables[Key].push_back({SwiftVersion, Info});
}
void APINotesWriter::addGlobalFunction(std::optional<Context> Ctx,
llvm::StringRef Name,
const GlobalFunctionInfo &Info,
VersionTuple SwiftVersion) {
IdentifierID NameID = Implementation->getIdentifier(Name);
SingleDeclTableKey Key(Ctx, NameID);
Implementation->GlobalFunctions[Key].push_back({SwiftVersion, Info});
}
void APINotesWriter::addEnumConstant(llvm::StringRef Name,
const EnumConstantInfo &Info,
VersionTuple SwiftVersion) {
IdentifierID EnumConstantID = Implementation->getIdentifier(Name);
Implementation->EnumConstants[EnumConstantID].push_back({SwiftVersion, Info});
}
void APINotesWriter::addTag(std::optional<Context> Ctx, llvm::StringRef Name,
const TagInfo &Info, VersionTuple SwiftVersion) {
IdentifierID TagID = Implementation->getIdentifier(Name);
SingleDeclTableKey Key(Ctx, TagID);
Implementation->Tags[Key].push_back({SwiftVersion, Info});
}
void APINotesWriter::addTypedef(std::optional<Context> Ctx,
llvm::StringRef Name, const TypedefInfo &Info,
VersionTuple SwiftVersion) {
IdentifierID TypedefID = Implementation->getIdentifier(Name);
SingleDeclTableKey Key(Ctx, TypedefID);
Implementation->Typedefs[Key].push_back({SwiftVersion, Info});
}
} // namespace api_notes
} // namespace clang
|