| 1 | //===-- APINotesWriter.cpp - API Notes Writer -------------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "clang/APINotes/APINotesWriter.h" |
| 10 | #include "APINotesFormat.h" |
| 11 | #include "clang/APINotes/Types.h" |
| 12 | #include "clang/Basic/FileManager.h" |
| 13 | #include "llvm/ADT/DenseMap.h" |
| 14 | #include "llvm/ADT/StringMap.h" |
| 15 | #include "llvm/Bitstream/BitstreamWriter.h" |
| 16 | #include "llvm/Support/DJB.h" |
| 17 | #include "llvm/Support/OnDiskHashTable.h" |
| 18 | #include "llvm/Support/VersionTuple.h" |
| 19 | |
| 20 | namespace clang { |
| 21 | namespace api_notes { |
| 22 | class APINotesWriter::Implementation { |
| 23 | friend class APINotesWriter; |
| 24 | |
| 25 | template <typename T> |
| 26 | using VersionedSmallVector = |
| 27 | llvm::SmallVector<std::pair<llvm::VersionTuple, T>, 1>; |
| 28 | |
| 29 | std::string ModuleName; |
| 30 | const FileEntry *SourceFile; |
| 31 | |
| 32 | /// Scratch space for bitstream writing. |
| 33 | llvm::SmallVector<uint64_t, 64> Scratch; |
| 34 | |
| 35 | /// Mapping from strings to identifier IDs. |
| 36 | llvm::StringMap<IdentifierID> IdentifierIDs; |
| 37 | |
| 38 | /// Information about contexts (Objective-C classes or protocols or C++ |
| 39 | /// namespaces). |
| 40 | /// |
| 41 | /// Indexed by the parent context ID, context kind and the identifier ID of |
| 42 | /// this context and provides both the context ID and information describing |
| 43 | /// the context within that module. |
| 44 | llvm::DenseMap<ContextTableKey, |
| 45 | std::pair<unsigned, VersionedSmallVector<ContextInfo>>> |
| 46 | Contexts; |
| 47 | |
| 48 | /// Information about parent contexts for each context. |
| 49 | /// |
| 50 | /// Indexed by context ID, provides the parent context ID. |
| 51 | llvm::DenseMap<uint32_t, uint32_t> ParentContexts; |
| 52 | |
| 53 | /// Mapping from context IDs to the kind of context. |
| 54 | llvm::DenseMap<unsigned, uint8_t> ContextKinds; |
| 55 | |
| 56 | /// Mapping from context IDs to the identifier ID holding the name. |
| 57 | llvm::DenseMap<unsigned, unsigned> ContextNames; |
| 58 | |
| 59 | /// Information about Objective-C properties. |
| 60 | /// |
| 61 | /// Indexed by the context ID, property name, and whether this is an |
| 62 | /// instance property. |
| 63 | llvm::DenseMap< |
| 64 | std::tuple<unsigned, unsigned, char>, |
| 65 | llvm::SmallVector<std::pair<VersionTuple, ObjCPropertyInfo>, 1>> |
| 66 | ObjCProperties; |
| 67 | |
| 68 | /// Information about C record fields. |
| 69 | /// |
| 70 | /// Indexed by the context ID and name ID. |
| 71 | llvm::DenseMap<SingleDeclTableKey, |
| 72 | llvm::SmallVector<std::pair<VersionTuple, FieldInfo>, 1>> |
| 73 | Fields; |
| 74 | |
| 75 | /// Information about Objective-C methods. |
| 76 | /// |
| 77 | /// Indexed by the context ID, selector ID, and Boolean (stored as a char) |
| 78 | /// indicating whether this is a class or instance method. |
| 79 | llvm::DenseMap<std::tuple<unsigned, unsigned, char>, |
| 80 | llvm::SmallVector<std::pair<VersionTuple, ObjCMethodInfo>, 1>> |
| 81 | ObjCMethods; |
| 82 | |
| 83 | /// Information about C++ methods. |
| 84 | /// |
| 85 | /// Indexed by the context ID and name ID. |
| 86 | llvm::DenseMap<SingleDeclTableKey, |
| 87 | llvm::SmallVector<std::pair<VersionTuple, CXXMethodInfo>, 1>> |
| 88 | CXXMethods; |
| 89 | |
| 90 | /// Mapping from selectors to selector ID. |
| 91 | llvm::DenseMap<StoredObjCSelector, SelectorID> SelectorIDs; |
| 92 | |
| 93 | /// Information about global variables. |
| 94 | /// |
| 95 | /// Indexed by the context ID, identifier ID. |
| 96 | llvm::DenseMap< |
| 97 | SingleDeclTableKey, |
| 98 | llvm::SmallVector<std::pair<VersionTuple, GlobalVariableInfo>, 1>> |
| 99 | GlobalVariables; |
| 100 | |
| 101 | /// Information about global functions. |
| 102 | /// |
| 103 | /// Indexed by the context ID, identifier ID. |
| 104 | llvm::DenseMap< |
| 105 | SingleDeclTableKey, |
| 106 | llvm::SmallVector<std::pair<VersionTuple, GlobalFunctionInfo>, 1>> |
| 107 | GlobalFunctions; |
| 108 | |
| 109 | /// Information about enumerators. |
| 110 | /// |
| 111 | /// Indexed by the identifier ID. |
| 112 | llvm::DenseMap< |
| 113 | unsigned, llvm::SmallVector<std::pair<VersionTuple, EnumConstantInfo>, 1>> |
| 114 | EnumConstants; |
| 115 | |
| 116 | /// Information about tags. |
| 117 | /// |
| 118 | /// Indexed by the context ID, identifier ID. |
| 119 | llvm::DenseMap<SingleDeclTableKey, |
| 120 | llvm::SmallVector<std::pair<VersionTuple, TagInfo>, 1>> |
| 121 | Tags; |
| 122 | |
| 123 | /// Information about typedefs. |
| 124 | /// |
| 125 | /// Indexed by the context ID, identifier ID. |
| 126 | llvm::DenseMap<SingleDeclTableKey, |
| 127 | llvm::SmallVector<std::pair<VersionTuple, TypedefInfo>, 1>> |
| 128 | Typedefs; |
| 129 | |
| 130 | /// Retrieve the ID for the given identifier. |
| 131 | IdentifierID getIdentifier(StringRef Identifier) { |
| 132 | if (Identifier.empty()) |
| 133 | return 0; |
| 134 | |
| 135 | // Add to the identifier table if missing. |
| 136 | return IdentifierIDs.try_emplace(Key: Identifier, Args: IdentifierIDs.size() + 1) |
| 137 | .first->second; |
| 138 | } |
| 139 | |
| 140 | /// Retrieve the ID for the given selector. |
| 141 | SelectorID getSelector(ObjCSelectorRef SelectorRef) { |
| 142 | // Translate the selector reference into a stored selector. |
| 143 | StoredObjCSelector Selector; |
| 144 | Selector.NumArgs = SelectorRef.NumArgs; |
| 145 | Selector.Identifiers.reserve(N: SelectorRef.Identifiers.size()); |
| 146 | for (auto piece : SelectorRef.Identifiers) |
| 147 | Selector.Identifiers.push_back(Elt: getIdentifier(Identifier: piece)); |
| 148 | |
| 149 | // Look for the stored selector. Add to the selector table if missing. |
| 150 | return SelectorIDs.try_emplace(Key: Selector, Args: SelectorIDs.size()).first->second; |
| 151 | } |
| 152 | |
| 153 | private: |
| 154 | void writeBlockInfoBlock(llvm::BitstreamWriter &Stream); |
| 155 | void writeControlBlock(llvm::BitstreamWriter &Stream); |
| 156 | void writeIdentifierBlock(llvm::BitstreamWriter &Stream); |
| 157 | void writeContextBlock(llvm::BitstreamWriter &Stream); |
| 158 | void writeObjCPropertyBlock(llvm::BitstreamWriter &Stream); |
| 159 | void writeObjCMethodBlock(llvm::BitstreamWriter &Stream); |
| 160 | void writeCXXMethodBlock(llvm::BitstreamWriter &Stream); |
| 161 | void writeFieldBlock(llvm::BitstreamWriter &Stream); |
| 162 | void writeObjCSelectorBlock(llvm::BitstreamWriter &Stream); |
| 163 | void writeGlobalVariableBlock(llvm::BitstreamWriter &Stream); |
| 164 | void writeGlobalFunctionBlock(llvm::BitstreamWriter &Stream); |
| 165 | void writeEnumConstantBlock(llvm::BitstreamWriter &Stream); |
| 166 | void writeTagBlock(llvm::BitstreamWriter &Stream); |
| 167 | void writeTypedefBlock(llvm::BitstreamWriter &Stream); |
| 168 | |
| 169 | public: |
| 170 | Implementation(llvm::StringRef ModuleName, const FileEntry *SF) |
| 171 | : ModuleName(std::string(ModuleName)), SourceFile(SF) {} |
| 172 | |
| 173 | void writeToStream(llvm::raw_ostream &OS); |
| 174 | }; |
| 175 | |
| 176 | void APINotesWriter::Implementation::writeToStream(llvm::raw_ostream &OS) { |
| 177 | llvm::SmallVector<char, 0> Buffer; |
| 178 | |
| 179 | { |
| 180 | llvm::BitstreamWriter Stream(Buffer); |
| 181 | |
| 182 | // Emit the signature. |
| 183 | for (unsigned char Byte : API_NOTES_SIGNATURE) |
| 184 | Stream.Emit(Val: Byte, NumBits: 8); |
| 185 | |
| 186 | // Emit the blocks. |
| 187 | writeBlockInfoBlock(Stream); |
| 188 | writeControlBlock(Stream); |
| 189 | writeIdentifierBlock(Stream); |
| 190 | writeContextBlock(Stream); |
| 191 | writeObjCPropertyBlock(Stream); |
| 192 | writeObjCMethodBlock(Stream); |
| 193 | writeCXXMethodBlock(Stream); |
| 194 | writeFieldBlock(Stream); |
| 195 | writeObjCSelectorBlock(Stream); |
| 196 | writeGlobalVariableBlock(Stream); |
| 197 | writeGlobalFunctionBlock(Stream); |
| 198 | writeEnumConstantBlock(Stream); |
| 199 | writeTagBlock(Stream); |
| 200 | writeTypedefBlock(Stream); |
| 201 | } |
| 202 | |
| 203 | OS.write(Ptr: Buffer.data(), Size: Buffer.size()); |
| 204 | OS.flush(); |
| 205 | } |
| 206 | |
| 207 | namespace { |
| 208 | /// Record the name of a block. |
| 209 | void emitBlockID(llvm::BitstreamWriter &Stream, unsigned ID, |
| 210 | llvm::StringRef Name) { |
| 211 | Stream.EmitRecord(Code: llvm::bitc::BLOCKINFO_CODE_SETBID, |
| 212 | Vals: llvm::ArrayRef<unsigned>{ID}); |
| 213 | |
| 214 | // Emit the block name if present. |
| 215 | if (Name.empty()) |
| 216 | return; |
| 217 | Stream.EmitRecord( |
| 218 | Code: llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, |
| 219 | Vals: llvm::ArrayRef<unsigned char>( |
| 220 | const_cast<unsigned char *>( |
| 221 | reinterpret_cast<const unsigned char *>(Name.data())), |
| 222 | Name.size())); |
| 223 | } |
| 224 | |
| 225 | /// Record the name of a record within a block. |
| 226 | void emitRecordID(llvm::BitstreamWriter &Stream, unsigned ID, |
| 227 | llvm::StringRef Name) { |
| 228 | assert(ID < 256 && "can't fit record ID in next to name" ); |
| 229 | |
| 230 | llvm::SmallVector<unsigned char, 64> Buffer; |
| 231 | Buffer.resize(N: Name.size() + 1); |
| 232 | Buffer[0] = ID; |
| 233 | memcpy(dest: Buffer.data() + 1, src: Name.data(), n: Name.size()); |
| 234 | |
| 235 | Stream.EmitRecord(Code: llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Vals: Buffer); |
| 236 | } |
| 237 | } // namespace |
| 238 | |
| 239 | void APINotesWriter::Implementation::writeBlockInfoBlock( |
| 240 | llvm::BitstreamWriter &Stream) { |
| 241 | llvm::BCBlockRAII Scope(Stream, llvm::bitc::BLOCKINFO_BLOCK_ID, 2); |
| 242 | |
| 243 | #define BLOCK(Block) emitBlockID(Stream, Block##_ID, #Block) |
| 244 | #define BLOCK_RECORD(NameSpace, Block) \ |
| 245 | emitRecordID(Stream, NameSpace::Block, #Block) |
| 246 | BLOCK(CONTROL_BLOCK); |
| 247 | BLOCK_RECORD(control_block, METADATA); |
| 248 | BLOCK_RECORD(control_block, MODULE_NAME); |
| 249 | |
| 250 | BLOCK(IDENTIFIER_BLOCK); |
| 251 | BLOCK_RECORD(identifier_block, IDENTIFIER_DATA); |
| 252 | |
| 253 | BLOCK(OBJC_CONTEXT_BLOCK); |
| 254 | BLOCK_RECORD(context_block, CONTEXT_ID_DATA); |
| 255 | |
| 256 | BLOCK(OBJC_PROPERTY_BLOCK); |
| 257 | BLOCK_RECORD(objc_property_block, OBJC_PROPERTY_DATA); |
| 258 | |
| 259 | BLOCK(OBJC_METHOD_BLOCK); |
| 260 | BLOCK_RECORD(objc_method_block, OBJC_METHOD_DATA); |
| 261 | |
| 262 | BLOCK(OBJC_SELECTOR_BLOCK); |
| 263 | BLOCK_RECORD(objc_selector_block, OBJC_SELECTOR_DATA); |
| 264 | |
| 265 | BLOCK(GLOBAL_VARIABLE_BLOCK); |
| 266 | BLOCK_RECORD(global_variable_block, GLOBAL_VARIABLE_DATA); |
| 267 | |
| 268 | BLOCK(GLOBAL_FUNCTION_BLOCK); |
| 269 | BLOCK_RECORD(global_function_block, GLOBAL_FUNCTION_DATA); |
| 270 | #undef BLOCK_RECORD |
| 271 | #undef BLOCK |
| 272 | } |
| 273 | |
| 274 | void APINotesWriter::Implementation::writeControlBlock( |
| 275 | llvm::BitstreamWriter &Stream) { |
| 276 | llvm::BCBlockRAII Scope(Stream, CONTROL_BLOCK_ID, 3); |
| 277 | |
| 278 | control_block::MetadataLayout Metadata(Stream); |
| 279 | Metadata.emit(buffer&: Scratch, data: VERSION_MAJOR, data: VERSION_MINOR); |
| 280 | |
| 281 | control_block::ModuleNameLayout ModuleName(Stream); |
| 282 | ModuleName.emit(buffer&: Scratch, data&: this->ModuleName); |
| 283 | |
| 284 | if (SourceFile) { |
| 285 | control_block::SourceFileLayout SourceFile(Stream); |
| 286 | SourceFile.emit(buffer&: Scratch, data: this->SourceFile->getSize(), |
| 287 | data: this->SourceFile->getModificationTime()); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | namespace { |
| 292 | /// Used to serialize the on-disk identifier table. |
| 293 | class IdentifierTableInfo { |
| 294 | public: |
| 295 | using key_type = StringRef; |
| 296 | using key_type_ref = key_type; |
| 297 | using data_type = IdentifierID; |
| 298 | using data_type_ref = const data_type &; |
| 299 | using hash_value_type = uint32_t; |
| 300 | using offset_type = unsigned; |
| 301 | |
| 302 | hash_value_type ComputeHash(key_type_ref Key) { return llvm::djbHash(Buffer: Key); } |
| 303 | |
| 304 | std::pair<unsigned, unsigned> |
| 305 | EmitKeyDataLength(raw_ostream &OS, key_type_ref Key, data_type_ref) { |
| 306 | uint32_t KeyLength = Key.size(); |
| 307 | uint32_t DataLength = sizeof(uint32_t); |
| 308 | |
| 309 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 310 | writer.write<uint16_t>(Val: KeyLength); |
| 311 | writer.write<uint16_t>(Val: DataLength); |
| 312 | return {KeyLength, DataLength}; |
| 313 | } |
| 314 | |
| 315 | void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) { OS << Key; } |
| 316 | |
| 317 | void EmitData(raw_ostream &OS, key_type_ref, data_type_ref Data, unsigned) { |
| 318 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 319 | writer.write<uint32_t>(Val: Data); |
| 320 | } |
| 321 | }; |
| 322 | } // namespace |
| 323 | |
| 324 | void APINotesWriter::Implementation::writeIdentifierBlock( |
| 325 | llvm::BitstreamWriter &Stream) { |
| 326 | llvm::BCBlockRAII restoreBlock(Stream, IDENTIFIER_BLOCK_ID, 3); |
| 327 | |
| 328 | if (IdentifierIDs.empty()) |
| 329 | return; |
| 330 | |
| 331 | llvm::SmallString<4096> HashTableBlob; |
| 332 | uint32_t Offset; |
| 333 | { |
| 334 | llvm::OnDiskChainedHashTableGenerator<IdentifierTableInfo> Generator; |
| 335 | for (auto &II : IdentifierIDs) |
| 336 | Generator.insert(Key: II.first(), Data: II.second); |
| 337 | |
| 338 | llvm::raw_svector_ostream BlobStream(HashTableBlob); |
| 339 | // Make sure that no bucket is at offset 0 |
| 340 | llvm::support::endian::write<uint32_t>(os&: BlobStream, value: 0, |
| 341 | endian: llvm::endianness::little); |
| 342 | Offset = Generator.Emit(Out&: BlobStream); |
| 343 | } |
| 344 | |
| 345 | identifier_block::IdentifierDataLayout IdentifierData(Stream); |
| 346 | IdentifierData.emit(buffer&: Scratch, data&: Offset, data&: HashTableBlob); |
| 347 | } |
| 348 | |
| 349 | namespace { |
| 350 | /// Used to serialize the on-disk Objective-C context table. |
| 351 | class ContextIDTableInfo { |
| 352 | public: |
| 353 | using key_type = ContextTableKey; |
| 354 | using key_type_ref = key_type; |
| 355 | using data_type = unsigned; |
| 356 | using data_type_ref = const data_type &; |
| 357 | using hash_value_type = size_t; |
| 358 | using offset_type = unsigned; |
| 359 | |
| 360 | hash_value_type ComputeHash(key_type_ref Key) { |
| 361 | return static_cast<size_t>(Key.hashValue()); |
| 362 | } |
| 363 | |
| 364 | std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &OS, key_type_ref, |
| 365 | data_type_ref) { |
| 366 | uint32_t KeyLength = sizeof(uint32_t) + sizeof(uint8_t) + sizeof(uint32_t); |
| 367 | uint32_t DataLength = sizeof(uint32_t); |
| 368 | |
| 369 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 370 | writer.write<uint16_t>(Val: KeyLength); |
| 371 | writer.write<uint16_t>(Val: DataLength); |
| 372 | return {KeyLength, DataLength}; |
| 373 | } |
| 374 | |
| 375 | void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) { |
| 376 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 377 | writer.write<uint32_t>(Val: Key.parentContextID); |
| 378 | writer.write<uint8_t>(Val: Key.contextKind); |
| 379 | writer.write<uint32_t>(Val: Key.contextID); |
| 380 | } |
| 381 | |
| 382 | void EmitData(raw_ostream &OS, key_type_ref, data_type_ref Data, unsigned) { |
| 383 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 384 | writer.write<uint32_t>(Val: Data); |
| 385 | } |
| 386 | }; |
| 387 | |
| 388 | /// Localized helper to make a type dependent, thwarting template argument |
| 389 | /// deduction. |
| 390 | template <typename T> struct MakeDependent { typedef T Type; }; |
| 391 | |
| 392 | /// Retrieve the serialized size of the given VersionTuple, for use in |
| 393 | /// on-disk hash tables. |
| 394 | unsigned getVersionTupleSize(const VersionTuple &VT) { |
| 395 | unsigned size = sizeof(uint8_t) + /*major*/ sizeof(uint32_t); |
| 396 | if (VT.getMinor()) |
| 397 | size += sizeof(uint32_t); |
| 398 | if (VT.getSubminor()) |
| 399 | size += sizeof(uint32_t); |
| 400 | if (VT.getBuild()) |
| 401 | size += sizeof(uint32_t); |
| 402 | return size; |
| 403 | } |
| 404 | |
| 405 | /// Determine the size of an array of versioned information, |
| 406 | template <typename T> |
| 407 | unsigned getVersionedInfoSize( |
| 408 | const llvm::SmallVectorImpl<std::pair<llvm::VersionTuple, T>> &VI, |
| 409 | llvm::function_ref<unsigned(const typename MakeDependent<T>::Type &)> |
| 410 | getInfoSize) { |
| 411 | unsigned result = sizeof(uint16_t); // # of elements |
| 412 | for (const auto &E : VI) { |
| 413 | result += getVersionTupleSize(E.first); |
| 414 | result += getInfoSize(E.second); |
| 415 | } |
| 416 | return result; |
| 417 | } |
| 418 | |
| 419 | /// Emit a serialized representation of a version tuple. |
| 420 | void emitVersionTuple(raw_ostream &OS, const VersionTuple &VT) { |
| 421 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 422 | |
| 423 | // First byte contains the number of components beyond the 'major' component. |
| 424 | uint8_t descriptor; |
| 425 | if (VT.getBuild()) |
| 426 | descriptor = 3; |
| 427 | else if (VT.getSubminor()) |
| 428 | descriptor = 2; |
| 429 | else if (VT.getMinor()) |
| 430 | descriptor = 1; |
| 431 | else |
| 432 | descriptor = 0; |
| 433 | writer.write<uint8_t>(Val: descriptor); |
| 434 | |
| 435 | // Write the components. |
| 436 | writer.write<uint32_t>(Val: VT.getMajor()); |
| 437 | if (auto minor = VT.getMinor()) |
| 438 | writer.write<uint32_t>(Val: *minor); |
| 439 | if (auto subminor = VT.getSubminor()) |
| 440 | writer.write<uint32_t>(Val: *subminor); |
| 441 | if (auto build = VT.getBuild()) |
| 442 | writer.write<uint32_t>(Val: *build); |
| 443 | } |
| 444 | |
| 445 | /// Emit versioned information. |
| 446 | template <typename T> |
| 447 | void emitVersionedInfo( |
| 448 | raw_ostream &OS, llvm::SmallVectorImpl<std::pair<VersionTuple, T>> &VI, |
| 449 | llvm::function_ref<void(raw_ostream &, |
| 450 | const typename MakeDependent<T>::Type &)> |
| 451 | emitInfo) { |
| 452 | std::sort(VI.begin(), VI.end(), |
| 453 | [](const std::pair<VersionTuple, T> &LHS, |
| 454 | const std::pair<VersionTuple, T> &RHS) -> bool { |
| 455 | assert((&LHS == &RHS || LHS.first != RHS.first) && |
| 456 | "two entries for the same version" ); |
| 457 | return LHS.first < RHS.first; |
| 458 | }); |
| 459 | |
| 460 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 461 | writer.write<uint16_t>(VI.size()); |
| 462 | for (const auto &E : VI) { |
| 463 | emitVersionTuple(OS, E.first); |
| 464 | emitInfo(OS, E.second); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | /// On-disk hash table info key base for handling versioned data. |
| 469 | template <typename Derived, typename KeyType, typename UnversionedDataType> |
| 470 | class VersionedTableInfo { |
| 471 | Derived &asDerived() { return *static_cast<Derived *>(this); } |
| 472 | |
| 473 | const Derived &asDerived() const { |
| 474 | return *static_cast<const Derived *>(this); |
| 475 | } |
| 476 | |
| 477 | public: |
| 478 | using key_type = KeyType; |
| 479 | using key_type_ref = key_type; |
| 480 | using data_type = |
| 481 | llvm::SmallVector<std::pair<llvm::VersionTuple, UnversionedDataType>, 1>; |
| 482 | using data_type_ref = data_type &; |
| 483 | using hash_value_type = size_t; |
| 484 | using offset_type = unsigned; |
| 485 | |
| 486 | std::pair<unsigned, unsigned> |
| 487 | EmitKeyDataLength(raw_ostream &OS, key_type_ref Key, data_type_ref Data) { |
| 488 | uint32_t KeyLength = asDerived().getKeyLength(Key); |
| 489 | uint32_t DataLength = |
| 490 | getVersionedInfoSize(Data, [this](const UnversionedDataType &UI) { |
| 491 | return asDerived().getUnversionedInfoSize(UI); |
| 492 | }); |
| 493 | |
| 494 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 495 | writer.write<uint16_t>(Val: KeyLength); |
| 496 | writer.write<uint16_t>(Val: DataLength); |
| 497 | return {KeyLength, DataLength}; |
| 498 | } |
| 499 | |
| 500 | void EmitData(raw_ostream &OS, key_type_ref, data_type_ref Data, unsigned) { |
| 501 | emitVersionedInfo( |
| 502 | OS, Data, [this](llvm::raw_ostream &OS, const UnversionedDataType &UI) { |
| 503 | asDerived().emitUnversionedInfo(OS, UI); |
| 504 | }); |
| 505 | } |
| 506 | }; |
| 507 | |
| 508 | /// Emit a serialized representation of the common entity information. |
| 509 | void emitCommonEntityInfo(raw_ostream &OS, const CommonEntityInfo &CEI) { |
| 510 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 511 | |
| 512 | uint8_t payload = 0; |
| 513 | if (auto safety = CEI.getSwiftSafety()) { |
| 514 | payload = static_cast<unsigned>(*safety); |
| 515 | payload <<= 1; |
| 516 | payload |= 0x01; |
| 517 | } |
| 518 | payload <<= 2; |
| 519 | if (auto swiftPrivate = CEI.isSwiftPrivate()) { |
| 520 | payload |= 0x01; |
| 521 | if (*swiftPrivate) |
| 522 | payload |= 0x02; |
| 523 | } |
| 524 | payload <<= 1; |
| 525 | payload |= CEI.Unavailable; |
| 526 | payload <<= 1; |
| 527 | payload |= CEI.UnavailableInSwift; |
| 528 | |
| 529 | writer.write<uint8_t>(Val: payload); |
| 530 | |
| 531 | writer.write<uint16_t>(Val: CEI.UnavailableMsg.size()); |
| 532 | OS.write(Ptr: CEI.UnavailableMsg.c_str(), Size: CEI.UnavailableMsg.size()); |
| 533 | |
| 534 | writer.write<uint16_t>(Val: CEI.SwiftName.size()); |
| 535 | OS.write(Ptr: CEI.SwiftName.c_str(), Size: CEI.SwiftName.size()); |
| 536 | } |
| 537 | |
| 538 | /// Retrieve the serialized size of the given CommonEntityInfo, for use in |
| 539 | /// on-disk hash tables. |
| 540 | unsigned getCommonEntityInfoSize(const CommonEntityInfo &CEI) { |
| 541 | return 5 + CEI.UnavailableMsg.size() + CEI.SwiftName.size(); |
| 542 | } |
| 543 | |
| 544 | // Retrieve the serialized size of the given CommonTypeInfo, for use |
| 545 | // in on-disk hash tables. |
| 546 | unsigned getCommonTypeInfoSize(const CommonTypeInfo &CTI) { |
| 547 | return 2 + (CTI.getSwiftBridge() ? CTI.getSwiftBridge()->size() : 0) + 2 + |
| 548 | (CTI.getNSErrorDomain() ? CTI.getNSErrorDomain()->size() : 0) + 2 + |
| 549 | (CTI.getSwiftConformance() ? CTI.getSwiftConformance()->size() : 0) + |
| 550 | getCommonEntityInfoSize(CEI: CTI); |
| 551 | } |
| 552 | |
| 553 | /// Emit a serialized representation of the common type information. |
| 554 | void emitCommonTypeInfo(raw_ostream &OS, const CommonTypeInfo &CTI) { |
| 555 | emitCommonEntityInfo(OS, CEI: CTI); |
| 556 | |
| 557 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 558 | if (auto swiftBridge = CTI.getSwiftBridge()) { |
| 559 | writer.write<uint16_t>(Val: swiftBridge->size() + 1); |
| 560 | OS.write(Ptr: swiftBridge->c_str(), Size: swiftBridge->size()); |
| 561 | } else { |
| 562 | writer.write<uint16_t>(Val: 0); |
| 563 | } |
| 564 | if (auto nsErrorDomain = CTI.getNSErrorDomain()) { |
| 565 | writer.write<uint16_t>(Val: nsErrorDomain->size() + 1); |
| 566 | OS.write(Ptr: nsErrorDomain->c_str(), Size: CTI.getNSErrorDomain()->size()); |
| 567 | } else { |
| 568 | writer.write<uint16_t>(Val: 0); |
| 569 | } |
| 570 | if (auto conformance = CTI.getSwiftConformance()) { |
| 571 | writer.write<uint16_t>(Val: conformance->size() + 1); |
| 572 | OS.write(Ptr: conformance->c_str(), Size: conformance->size()); |
| 573 | } else { |
| 574 | writer.write<uint16_t>(Val: 0); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | /// Used to serialize the on-disk Objective-C property table. |
| 579 | class ContextInfoTableInfo |
| 580 | : public VersionedTableInfo<ContextInfoTableInfo, unsigned, ContextInfo> { |
| 581 | public: |
| 582 | unsigned getKeyLength(key_type_ref) { return sizeof(uint32_t); } |
| 583 | |
| 584 | void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) { |
| 585 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 586 | writer.write<uint32_t>(Val: Key); |
| 587 | } |
| 588 | |
| 589 | hash_value_type ComputeHash(key_type_ref Key) { |
| 590 | return static_cast<size_t>(llvm::hash_value(value: Key)); |
| 591 | } |
| 592 | |
| 593 | unsigned getUnversionedInfoSize(const ContextInfo &OCI) { |
| 594 | return getCommonTypeInfoSize(CTI: OCI) + 1; |
| 595 | } |
| 596 | |
| 597 | void emitUnversionedInfo(raw_ostream &OS, const ContextInfo &OCI) { |
| 598 | emitCommonTypeInfo(OS, CTI: OCI); |
| 599 | |
| 600 | uint8_t payload = 0; |
| 601 | if (auto swiftImportAsNonGeneric = OCI.getSwiftImportAsNonGeneric()) |
| 602 | payload |= (0x01 << 1) | (uint8_t)swiftImportAsNonGeneric.value(); |
| 603 | payload <<= 2; |
| 604 | if (auto swiftObjCMembers = OCI.getSwiftObjCMembers()) |
| 605 | payload |= (0x01 << 1) | (uint8_t)swiftObjCMembers.value(); |
| 606 | payload <<= 3; |
| 607 | if (auto nullable = OCI.getDefaultNullability()) |
| 608 | payload |= (0x01 << 2) | static_cast<uint8_t>(*nullable); |
| 609 | payload = (payload << 1) | (OCI.hasDesignatedInits() ? 1 : 0); |
| 610 | |
| 611 | OS << payload; |
| 612 | } |
| 613 | }; |
| 614 | } // namespace |
| 615 | |
| 616 | void APINotesWriter::Implementation::writeContextBlock( |
| 617 | llvm::BitstreamWriter &Stream) { |
| 618 | llvm::BCBlockRAII restoreBlock(Stream, OBJC_CONTEXT_BLOCK_ID, 3); |
| 619 | |
| 620 | if (Contexts.empty()) |
| 621 | return; |
| 622 | |
| 623 | { |
| 624 | llvm::SmallString<4096> HashTableBlob; |
| 625 | uint32_t Offset; |
| 626 | { |
| 627 | llvm::OnDiskChainedHashTableGenerator<ContextIDTableInfo> Generator; |
| 628 | for (auto &OC : Contexts) |
| 629 | Generator.insert(Key: OC.first, Data: OC.second.first); |
| 630 | |
| 631 | llvm::raw_svector_ostream BlobStream(HashTableBlob); |
| 632 | // Make sure that no bucket is at offset 0 |
| 633 | llvm::support::endian::write<uint32_t>(os&: BlobStream, value: 0, |
| 634 | endian: llvm::endianness::little); |
| 635 | Offset = Generator.Emit(Out&: BlobStream); |
| 636 | } |
| 637 | |
| 638 | context_block::ContextIDLayout ContextID(Stream); |
| 639 | ContextID.emit(buffer&: Scratch, data&: Offset, data&: HashTableBlob); |
| 640 | } |
| 641 | |
| 642 | { |
| 643 | llvm::SmallString<4096> HashTableBlob; |
| 644 | uint32_t Offset; |
| 645 | { |
| 646 | llvm::OnDiskChainedHashTableGenerator<ContextInfoTableInfo> Generator; |
| 647 | for (auto &OC : Contexts) |
| 648 | Generator.insert(Key: OC.second.first, Data&: OC.second.second); |
| 649 | |
| 650 | llvm::raw_svector_ostream BlobStream(HashTableBlob); |
| 651 | // Make sure that no bucket is at offset 0 |
| 652 | llvm::support::endian::write<uint32_t>(os&: BlobStream, value: 0, |
| 653 | endian: llvm::endianness::little); |
| 654 | Offset = Generator.Emit(Out&: BlobStream); |
| 655 | } |
| 656 | |
| 657 | context_block::ContextInfoLayout ContextInfo(Stream); |
| 658 | ContextInfo.emit(buffer&: Scratch, data&: Offset, data&: HashTableBlob); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | namespace { |
| 663 | /// Retrieve the serialized size of the given VariableInfo, for use in |
| 664 | /// on-disk hash tables. |
| 665 | unsigned getVariableInfoSize(const VariableInfo &VI) { |
| 666 | return 2 + getCommonEntityInfoSize(CEI: VI) + 2 + VI.getType().size(); |
| 667 | } |
| 668 | unsigned getParamInfoSize(const ParamInfo &PI); |
| 669 | |
| 670 | /// Emit a serialized representation of the variable information. |
| 671 | void emitVariableInfo(raw_ostream &OS, const VariableInfo &VI) { |
| 672 | emitCommonEntityInfo(OS, CEI: VI); |
| 673 | |
| 674 | uint8_t bytes[2] = {0, 0}; |
| 675 | if (auto nullable = VI.getNullability()) { |
| 676 | bytes[0] = 1; |
| 677 | bytes[1] = static_cast<uint8_t>(*nullable); |
| 678 | } else { |
| 679 | // Nothing to do. |
| 680 | } |
| 681 | |
| 682 | OS.write(Ptr: reinterpret_cast<const char *>(bytes), Size: 2); |
| 683 | |
| 684 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 685 | writer.write<uint16_t>(Val: VI.getType().size()); |
| 686 | OS.write(Ptr: VI.getType().data(), Size: VI.getType().size()); |
| 687 | } |
| 688 | |
| 689 | /// Used to serialize the on-disk Objective-C property table. |
| 690 | class ObjCPropertyTableInfo |
| 691 | : public VersionedTableInfo<ObjCPropertyTableInfo, |
| 692 | std::tuple<unsigned, unsigned, char>, |
| 693 | ObjCPropertyInfo> { |
| 694 | public: |
| 695 | unsigned getKeyLength(key_type_ref) { |
| 696 | return sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t); |
| 697 | } |
| 698 | |
| 699 | void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) { |
| 700 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 701 | writer.write<uint32_t>(Val: std::get<0>(t&: Key)); |
| 702 | writer.write<uint32_t>(Val: std::get<1>(t&: Key)); |
| 703 | writer.write<uint8_t>(Val: std::get<2>(t&: Key)); |
| 704 | } |
| 705 | |
| 706 | hash_value_type ComputeHash(key_type_ref Key) { |
| 707 | return static_cast<size_t>(llvm::hash_value(arg: Key)); |
| 708 | } |
| 709 | |
| 710 | unsigned getUnversionedInfoSize(const ObjCPropertyInfo &OPI) { |
| 711 | return getVariableInfoSize(VI: OPI) + 1; |
| 712 | } |
| 713 | |
| 714 | void emitUnversionedInfo(raw_ostream &OS, const ObjCPropertyInfo &OPI) { |
| 715 | emitVariableInfo(OS, VI: OPI); |
| 716 | |
| 717 | uint8_t flags = 0; |
| 718 | if (auto value = OPI.getSwiftImportAsAccessors()) { |
| 719 | flags |= 1 << 0; |
| 720 | flags |= value.value() << 1; |
| 721 | } |
| 722 | OS << flags; |
| 723 | } |
| 724 | }; |
| 725 | } // namespace |
| 726 | |
| 727 | void APINotesWriter::Implementation::writeObjCPropertyBlock( |
| 728 | llvm::BitstreamWriter &Stream) { |
| 729 | llvm::BCBlockRAII Scope(Stream, OBJC_PROPERTY_BLOCK_ID, 3); |
| 730 | |
| 731 | if (ObjCProperties.empty()) |
| 732 | return; |
| 733 | |
| 734 | { |
| 735 | llvm::SmallString<4096> HashTableBlob; |
| 736 | uint32_t Offset; |
| 737 | { |
| 738 | llvm::OnDiskChainedHashTableGenerator<ObjCPropertyTableInfo> Generator; |
| 739 | for (auto &OP : ObjCProperties) |
| 740 | Generator.insert(Key: OP.first, Data&: OP.second); |
| 741 | |
| 742 | llvm::raw_svector_ostream BlobStream(HashTableBlob); |
| 743 | // Make sure that no bucket is at offset 0 |
| 744 | llvm::support::endian::write<uint32_t>(os&: BlobStream, value: 0, |
| 745 | endian: llvm::endianness::little); |
| 746 | Offset = Generator.Emit(Out&: BlobStream); |
| 747 | } |
| 748 | |
| 749 | objc_property_block::ObjCPropertyDataLayout ObjCPropertyData(Stream); |
| 750 | ObjCPropertyData.emit(buffer&: Scratch, data&: Offset, data&: HashTableBlob); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | namespace { |
| 755 | unsigned getFunctionInfoSize(const FunctionInfo &); |
| 756 | void emitFunctionInfo(llvm::raw_ostream &, const FunctionInfo &); |
| 757 | void emitParamInfo(raw_ostream &OS, const ParamInfo &PI); |
| 758 | |
| 759 | /// Used to serialize the on-disk Objective-C method table. |
| 760 | class ObjCMethodTableInfo |
| 761 | : public VersionedTableInfo<ObjCMethodTableInfo, |
| 762 | std::tuple<unsigned, unsigned, char>, |
| 763 | ObjCMethodInfo> { |
| 764 | public: |
| 765 | unsigned getKeyLength(key_type_ref) { |
| 766 | return sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t); |
| 767 | } |
| 768 | |
| 769 | void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) { |
| 770 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 771 | writer.write<uint32_t>(Val: std::get<0>(t&: Key)); |
| 772 | writer.write<uint32_t>(Val: std::get<1>(t&: Key)); |
| 773 | writer.write<uint8_t>(Val: std::get<2>(t&: Key)); |
| 774 | } |
| 775 | |
| 776 | hash_value_type ComputeHash(key_type_ref key) { |
| 777 | return static_cast<size_t>(llvm::hash_value(arg: key)); |
| 778 | } |
| 779 | |
| 780 | unsigned getUnversionedInfoSize(const ObjCMethodInfo &OMI) { |
| 781 | auto size = getFunctionInfoSize(OMI) + 1; |
| 782 | if (OMI.Self) |
| 783 | size += getParamInfoSize(PI: *OMI.Self); |
| 784 | return size; |
| 785 | } |
| 786 | |
| 787 | void emitUnversionedInfo(raw_ostream &OS, const ObjCMethodInfo &OMI) { |
| 788 | uint8_t flags = 0; |
| 789 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 790 | flags = (flags << 1) | OMI.DesignatedInit; |
| 791 | flags = (flags << 1) | OMI.RequiredInit; |
| 792 | flags = (flags << 1) | static_cast<bool>(OMI.Self); |
| 793 | writer.write<uint8_t>(Val: flags); |
| 794 | |
| 795 | emitFunctionInfo(OS, OMI); |
| 796 | |
| 797 | if (OMI.Self) |
| 798 | emitParamInfo(OS, PI: *OMI.Self); |
| 799 | } |
| 800 | }; |
| 801 | |
| 802 | /// Used to serialize the on-disk C++ method table. |
| 803 | class CXXMethodTableInfo |
| 804 | : public VersionedTableInfo<CXXMethodTableInfo, SingleDeclTableKey, |
| 805 | CXXMethodInfo> { |
| 806 | public: |
| 807 | unsigned getKeyLength(key_type_ref) { |
| 808 | return sizeof(uint32_t) + sizeof(uint32_t); |
| 809 | } |
| 810 | |
| 811 | void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) { |
| 812 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 813 | writer.write<uint32_t>(Val: Key.parentContextID); |
| 814 | writer.write<uint32_t>(Val: Key.nameID); |
| 815 | } |
| 816 | |
| 817 | hash_value_type ComputeHash(key_type_ref key) { |
| 818 | return static_cast<size_t>(key.hashValue()); |
| 819 | } |
| 820 | |
| 821 | unsigned getUnversionedInfoSize(const CXXMethodInfo &MI) { |
| 822 | auto size = getFunctionInfoSize(MI) + 1; |
| 823 | if (MI.This) |
| 824 | size += getParamInfoSize(PI: *MI.This); |
| 825 | return size; |
| 826 | } |
| 827 | |
| 828 | void emitUnversionedInfo(raw_ostream &OS, const CXXMethodInfo &MI) { |
| 829 | uint8_t flags = 0; |
| 830 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 831 | flags = (flags << 1) | static_cast<bool>(MI.This); |
| 832 | writer.write<uint8_t>(Val: flags); |
| 833 | |
| 834 | emitFunctionInfo(OS, MI); |
| 835 | if (MI.This) |
| 836 | emitParamInfo(OS, PI: *MI.This); |
| 837 | } |
| 838 | }; |
| 839 | } // namespace |
| 840 | |
| 841 | void APINotesWriter::Implementation::writeObjCMethodBlock( |
| 842 | llvm::BitstreamWriter &Stream) { |
| 843 | llvm::BCBlockRAII Scope(Stream, OBJC_METHOD_BLOCK_ID, 3); |
| 844 | |
| 845 | if (ObjCMethods.empty()) |
| 846 | return; |
| 847 | |
| 848 | { |
| 849 | llvm::SmallString<4096> HashTableBlob; |
| 850 | uint32_t Offset; |
| 851 | { |
| 852 | llvm::OnDiskChainedHashTableGenerator<ObjCMethodTableInfo> Generator; |
| 853 | for (auto &OM : ObjCMethods) |
| 854 | Generator.insert(Key: OM.first, Data&: OM.second); |
| 855 | |
| 856 | llvm::raw_svector_ostream BlobStream(HashTableBlob); |
| 857 | // Make sure that no bucket is at offset 0 |
| 858 | llvm::support::endian::write<uint32_t>(os&: BlobStream, value: 0, |
| 859 | endian: llvm::endianness::little); |
| 860 | Offset = Generator.Emit(Out&: BlobStream); |
| 861 | } |
| 862 | |
| 863 | objc_method_block::ObjCMethodDataLayout ObjCMethodData(Stream); |
| 864 | ObjCMethodData.emit(buffer&: Scratch, data&: Offset, data&: HashTableBlob); |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | void APINotesWriter::Implementation::writeCXXMethodBlock( |
| 869 | llvm::BitstreamWriter &Stream) { |
| 870 | llvm::BCBlockRAII Scope(Stream, CXX_METHOD_BLOCK_ID, 3); |
| 871 | |
| 872 | if (CXXMethods.empty()) |
| 873 | return; |
| 874 | |
| 875 | { |
| 876 | llvm::SmallString<4096> HashTableBlob; |
| 877 | uint32_t Offset; |
| 878 | { |
| 879 | llvm::OnDiskChainedHashTableGenerator<CXXMethodTableInfo> Generator; |
| 880 | for (auto &MD : CXXMethods) |
| 881 | Generator.insert(Key: MD.first, Data&: MD.second); |
| 882 | |
| 883 | llvm::raw_svector_ostream BlobStream(HashTableBlob); |
| 884 | // Make sure that no bucket is at offset 0 |
| 885 | llvm::support::endian::write<uint32_t>(os&: BlobStream, value: 0, |
| 886 | endian: llvm::endianness::little); |
| 887 | Offset = Generator.Emit(Out&: BlobStream); |
| 888 | } |
| 889 | |
| 890 | cxx_method_block::CXXMethodDataLayout CXXMethodData(Stream); |
| 891 | CXXMethodData.emit(buffer&: Scratch, data&: Offset, data&: HashTableBlob); |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | namespace { |
| 896 | /// Used to serialize the on-disk C field table. |
| 897 | class FieldTableInfo |
| 898 | : public VersionedTableInfo<FieldTableInfo, SingleDeclTableKey, FieldInfo> { |
| 899 | public: |
| 900 | unsigned getKeyLength(key_type_ref) { |
| 901 | return sizeof(uint32_t) + sizeof(uint32_t); |
| 902 | } |
| 903 | |
| 904 | void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) { |
| 905 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 906 | writer.write<uint32_t>(Val: Key.parentContextID); |
| 907 | writer.write<uint32_t>(Val: Key.nameID); |
| 908 | } |
| 909 | |
| 910 | hash_value_type ComputeHash(key_type_ref key) { |
| 911 | return static_cast<size_t>(key.hashValue()); |
| 912 | } |
| 913 | |
| 914 | unsigned getUnversionedInfoSize(const FieldInfo &FI) { |
| 915 | return getVariableInfoSize(VI: FI); |
| 916 | } |
| 917 | |
| 918 | void emitUnversionedInfo(raw_ostream &OS, const FieldInfo &FI) { |
| 919 | emitVariableInfo(OS, VI: FI); |
| 920 | } |
| 921 | }; |
| 922 | } // namespace |
| 923 | |
| 924 | void APINotesWriter::Implementation::writeFieldBlock( |
| 925 | llvm::BitstreamWriter &Stream) { |
| 926 | llvm::BCBlockRAII Scope(Stream, FIELD_BLOCK_ID, 3); |
| 927 | |
| 928 | if (Fields.empty()) |
| 929 | return; |
| 930 | |
| 931 | { |
| 932 | llvm::SmallString<4096> HashTableBlob; |
| 933 | uint32_t Offset; |
| 934 | { |
| 935 | llvm::OnDiskChainedHashTableGenerator<FieldTableInfo> Generator; |
| 936 | for (auto &FD : Fields) |
| 937 | Generator.insert(Key: FD.first, Data&: FD.second); |
| 938 | |
| 939 | llvm::raw_svector_ostream BlobStream(HashTableBlob); |
| 940 | // Make sure that no bucket is at offset 0 |
| 941 | llvm::support::endian::write<uint32_t>(os&: BlobStream, value: 0, |
| 942 | endian: llvm::endianness::little); |
| 943 | Offset = Generator.Emit(Out&: BlobStream); |
| 944 | } |
| 945 | |
| 946 | field_block::FieldDataLayout FieldData(Stream); |
| 947 | FieldData.emit(buffer&: Scratch, data&: Offset, data&: HashTableBlob); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | namespace { |
| 952 | /// Used to serialize the on-disk Objective-C selector table. |
| 953 | class ObjCSelectorTableInfo { |
| 954 | public: |
| 955 | using key_type = StoredObjCSelector; |
| 956 | using key_type_ref = const key_type &; |
| 957 | using data_type = SelectorID; |
| 958 | using data_type_ref = data_type; |
| 959 | using hash_value_type = unsigned; |
| 960 | using offset_type = unsigned; |
| 961 | |
| 962 | hash_value_type ComputeHash(key_type_ref Key) { |
| 963 | return llvm::DenseMapInfo<StoredObjCSelector>::getHashValue(Selector: Key); |
| 964 | } |
| 965 | |
| 966 | std::pair<unsigned, unsigned> |
| 967 | EmitKeyDataLength(raw_ostream &OS, key_type_ref Key, data_type_ref) { |
| 968 | uint32_t KeyLength = |
| 969 | sizeof(uint16_t) + sizeof(uint32_t) * Key.Identifiers.size(); |
| 970 | uint32_t DataLength = sizeof(uint32_t); |
| 971 | |
| 972 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 973 | writer.write<uint16_t>(Val: KeyLength); |
| 974 | writer.write<uint16_t>(Val: DataLength); |
| 975 | return {KeyLength, DataLength}; |
| 976 | } |
| 977 | |
| 978 | void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) { |
| 979 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 980 | writer.write<uint16_t>(Val: Key.NumArgs); |
| 981 | for (auto Identifier : Key.Identifiers) |
| 982 | writer.write<uint32_t>(Val: Identifier); |
| 983 | } |
| 984 | |
| 985 | void EmitData(raw_ostream &OS, key_type_ref, data_type_ref Data, unsigned) { |
| 986 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 987 | writer.write<uint32_t>(Val: Data); |
| 988 | } |
| 989 | }; |
| 990 | } // namespace |
| 991 | |
| 992 | void APINotesWriter::Implementation::writeObjCSelectorBlock( |
| 993 | llvm::BitstreamWriter &Stream) { |
| 994 | llvm::BCBlockRAII Scope(Stream, OBJC_SELECTOR_BLOCK_ID, 3); |
| 995 | |
| 996 | if (SelectorIDs.empty()) |
| 997 | return; |
| 998 | |
| 999 | { |
| 1000 | llvm::SmallString<4096> HashTableBlob; |
| 1001 | uint32_t Offset; |
| 1002 | { |
| 1003 | llvm::OnDiskChainedHashTableGenerator<ObjCSelectorTableInfo> Generator; |
| 1004 | for (auto &S : SelectorIDs) |
| 1005 | Generator.insert(Key: S.first, Data: S.second); |
| 1006 | |
| 1007 | llvm::raw_svector_ostream BlobStream(HashTableBlob); |
| 1008 | // Make sure that no bucket is at offset 0 |
| 1009 | llvm::support::endian::write<uint32_t>(os&: BlobStream, value: 0, |
| 1010 | endian: llvm::endianness::little); |
| 1011 | Offset = Generator.Emit(Out&: BlobStream); |
| 1012 | } |
| 1013 | |
| 1014 | objc_selector_block::ObjCSelectorDataLayout ObjCSelectorData(Stream); |
| 1015 | ObjCSelectorData.emit(buffer&: Scratch, data&: Offset, data&: HashTableBlob); |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | namespace { |
| 1020 | /// Used to serialize the on-disk global variable table. |
| 1021 | class GlobalVariableTableInfo |
| 1022 | : public VersionedTableInfo<GlobalVariableTableInfo, SingleDeclTableKey, |
| 1023 | GlobalVariableInfo> { |
| 1024 | public: |
| 1025 | unsigned getKeyLength(key_type_ref) { |
| 1026 | return sizeof(uint32_t) + sizeof(uint32_t); |
| 1027 | } |
| 1028 | |
| 1029 | void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) { |
| 1030 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 1031 | writer.write<uint32_t>(Val: Key.parentContextID); |
| 1032 | writer.write<uint32_t>(Val: Key.nameID); |
| 1033 | } |
| 1034 | |
| 1035 | hash_value_type ComputeHash(key_type_ref Key) { |
| 1036 | return static_cast<size_t>(Key.hashValue()); |
| 1037 | } |
| 1038 | |
| 1039 | unsigned getUnversionedInfoSize(const GlobalVariableInfo &GVI) { |
| 1040 | return getVariableInfoSize(VI: GVI); |
| 1041 | } |
| 1042 | |
| 1043 | void emitUnversionedInfo(raw_ostream &OS, const GlobalVariableInfo &GVI) { |
| 1044 | emitVariableInfo(OS, VI: GVI); |
| 1045 | } |
| 1046 | }; |
| 1047 | } // namespace |
| 1048 | |
| 1049 | void APINotesWriter::Implementation::writeGlobalVariableBlock( |
| 1050 | llvm::BitstreamWriter &Stream) { |
| 1051 | llvm::BCBlockRAII Scope(Stream, GLOBAL_VARIABLE_BLOCK_ID, 3); |
| 1052 | |
| 1053 | if (GlobalVariables.empty()) |
| 1054 | return; |
| 1055 | |
| 1056 | { |
| 1057 | llvm::SmallString<4096> HashTableBlob; |
| 1058 | uint32_t Offset; |
| 1059 | { |
| 1060 | llvm::OnDiskChainedHashTableGenerator<GlobalVariableTableInfo> Generator; |
| 1061 | for (auto &GV : GlobalVariables) |
| 1062 | Generator.insert(Key: GV.first, Data&: GV.second); |
| 1063 | |
| 1064 | llvm::raw_svector_ostream BlobStream(HashTableBlob); |
| 1065 | // Make sure that no bucket is at offset 0 |
| 1066 | llvm::support::endian::write<uint32_t>(os&: BlobStream, value: 0, |
| 1067 | endian: llvm::endianness::little); |
| 1068 | Offset = Generator.Emit(Out&: BlobStream); |
| 1069 | } |
| 1070 | |
| 1071 | global_variable_block::GlobalVariableDataLayout GlobalVariableData(Stream); |
| 1072 | GlobalVariableData.emit(buffer&: Scratch, data&: Offset, data&: HashTableBlob); |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | namespace { |
| 1077 | unsigned getParamInfoSize(const ParamInfo &PI) { |
| 1078 | return getVariableInfoSize(VI: PI) + 1; |
| 1079 | } |
| 1080 | |
| 1081 | void emitParamInfo(raw_ostream &OS, const ParamInfo &PI) { |
| 1082 | emitVariableInfo(OS, VI: PI); |
| 1083 | |
| 1084 | uint8_t flags = 0; |
| 1085 | if (auto noescape = PI.isNoEscape()) { |
| 1086 | flags |= 0x01; |
| 1087 | if (*noescape) |
| 1088 | flags |= 0x02; |
| 1089 | } |
| 1090 | flags <<= 2; |
| 1091 | if (auto lifetimebound = PI.isLifetimebound()) { |
| 1092 | flags |= 0x01; |
| 1093 | if (*lifetimebound) |
| 1094 | flags |= 0x02; |
| 1095 | } |
| 1096 | flags <<= 3; |
| 1097 | if (auto RCC = PI.getRetainCountConvention()) |
| 1098 | flags |= static_cast<uint8_t>(RCC.value()) + 1; |
| 1099 | |
| 1100 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 1101 | writer.write<uint8_t>(Val: flags); |
| 1102 | } |
| 1103 | |
| 1104 | /// Retrieve the serialized size of the given FunctionInfo, for use in on-disk |
| 1105 | /// hash tables. |
| 1106 | unsigned getFunctionInfoSize(const FunctionInfo &FI) { |
| 1107 | unsigned size = getCommonEntityInfoSize(CEI: FI) + 2 + sizeof(uint64_t); |
| 1108 | size += sizeof(uint16_t); |
| 1109 | for (const auto &P : FI.Params) |
| 1110 | size += getParamInfoSize(PI: P); |
| 1111 | size += sizeof(uint16_t) + FI.ResultType.size(); |
| 1112 | size += sizeof(uint16_t) + FI.SwiftReturnOwnership.size(); |
| 1113 | return size; |
| 1114 | } |
| 1115 | |
| 1116 | /// Emit a serialized representation of the function information. |
| 1117 | void emitFunctionInfo(raw_ostream &OS, const FunctionInfo &FI) { |
| 1118 | emitCommonEntityInfo(OS, CEI: FI); |
| 1119 | |
| 1120 | uint8_t flags = 0; |
| 1121 | flags |= FI.NullabilityAudited; |
| 1122 | flags <<= 3; |
| 1123 | if (auto RCC = FI.getRetainCountConvention()) |
| 1124 | flags |= static_cast<uint8_t>(RCC.value()) + 1; |
| 1125 | |
| 1126 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 1127 | |
| 1128 | writer.write<uint8_t>(Val: flags); |
| 1129 | writer.write<uint8_t>(Val: FI.NumAdjustedNullable); |
| 1130 | writer.write<uint64_t>(Val: FI.NullabilityPayload); |
| 1131 | |
| 1132 | writer.write<uint16_t>(Val: FI.Params.size()); |
| 1133 | for (const auto &PI : FI.Params) |
| 1134 | emitParamInfo(OS, PI); |
| 1135 | |
| 1136 | writer.write<uint16_t>(Val: FI.ResultType.size()); |
| 1137 | writer.write(Val: ArrayRef<char>{FI.ResultType}); |
| 1138 | writer.write<uint16_t>(Val: FI.SwiftReturnOwnership.size()); |
| 1139 | writer.write(Val: ArrayRef<char>{FI.SwiftReturnOwnership}); |
| 1140 | } |
| 1141 | |
| 1142 | /// Used to serialize the on-disk global function table. |
| 1143 | class GlobalFunctionTableInfo |
| 1144 | : public VersionedTableInfo<GlobalFunctionTableInfo, SingleDeclTableKey, |
| 1145 | GlobalFunctionInfo> { |
| 1146 | public: |
| 1147 | unsigned getKeyLength(key_type_ref) { |
| 1148 | return sizeof(uint32_t) + sizeof(uint32_t); |
| 1149 | } |
| 1150 | |
| 1151 | void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) { |
| 1152 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 1153 | writer.write<uint32_t>(Val: Key.parentContextID); |
| 1154 | writer.write<uint32_t>(Val: Key.nameID); |
| 1155 | } |
| 1156 | |
| 1157 | hash_value_type ComputeHash(key_type_ref Key) { |
| 1158 | return static_cast<size_t>(Key.hashValue()); |
| 1159 | } |
| 1160 | |
| 1161 | unsigned getUnversionedInfoSize(const GlobalFunctionInfo &GFI) { |
| 1162 | return getFunctionInfoSize(FI: GFI); |
| 1163 | } |
| 1164 | |
| 1165 | void emitUnversionedInfo(raw_ostream &OS, const GlobalFunctionInfo &GFI) { |
| 1166 | emitFunctionInfo(OS, FI: GFI); |
| 1167 | } |
| 1168 | }; |
| 1169 | } // namespace |
| 1170 | |
| 1171 | void APINotesWriter::Implementation::writeGlobalFunctionBlock( |
| 1172 | llvm::BitstreamWriter &Stream) { |
| 1173 | llvm::BCBlockRAII Scope(Stream, GLOBAL_FUNCTION_BLOCK_ID, 3); |
| 1174 | |
| 1175 | if (GlobalFunctions.empty()) |
| 1176 | return; |
| 1177 | |
| 1178 | { |
| 1179 | llvm::SmallString<4096> HashTableBlob; |
| 1180 | uint32_t Offset; |
| 1181 | { |
| 1182 | llvm::OnDiskChainedHashTableGenerator<GlobalFunctionTableInfo> Generator; |
| 1183 | for (auto &F : GlobalFunctions) |
| 1184 | Generator.insert(Key: F.first, Data&: F.second); |
| 1185 | |
| 1186 | llvm::raw_svector_ostream BlobStream(HashTableBlob); |
| 1187 | // Make sure that no bucket is at offset 0 |
| 1188 | llvm::support::endian::write<uint32_t>(os&: BlobStream, value: 0, |
| 1189 | endian: llvm::endianness::little); |
| 1190 | Offset = Generator.Emit(Out&: BlobStream); |
| 1191 | } |
| 1192 | |
| 1193 | global_function_block::GlobalFunctionDataLayout GlobalFunctionData(Stream); |
| 1194 | GlobalFunctionData.emit(buffer&: Scratch, data&: Offset, data&: HashTableBlob); |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | namespace { |
| 1199 | /// Used to serialize the on-disk global enum constant. |
| 1200 | class EnumConstantTableInfo |
| 1201 | : public VersionedTableInfo<EnumConstantTableInfo, unsigned, |
| 1202 | EnumConstantInfo> { |
| 1203 | public: |
| 1204 | unsigned getKeyLength(key_type_ref) { return sizeof(uint32_t); } |
| 1205 | |
| 1206 | void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) { |
| 1207 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 1208 | writer.write<uint32_t>(Val: Key); |
| 1209 | } |
| 1210 | |
| 1211 | hash_value_type ComputeHash(key_type_ref Key) { |
| 1212 | return static_cast<size_t>(llvm::hash_value(value: Key)); |
| 1213 | } |
| 1214 | |
| 1215 | unsigned getUnversionedInfoSize(const EnumConstantInfo &ECI) { |
| 1216 | return getCommonEntityInfoSize(CEI: ECI); |
| 1217 | } |
| 1218 | |
| 1219 | void emitUnversionedInfo(raw_ostream &OS, const EnumConstantInfo &ECI) { |
| 1220 | emitCommonEntityInfo(OS, CEI: ECI); |
| 1221 | } |
| 1222 | }; |
| 1223 | } // namespace |
| 1224 | |
| 1225 | void APINotesWriter::Implementation::writeEnumConstantBlock( |
| 1226 | llvm::BitstreamWriter &Stream) { |
| 1227 | llvm::BCBlockRAII Scope(Stream, ENUM_CONSTANT_BLOCK_ID, 3); |
| 1228 | |
| 1229 | if (EnumConstants.empty()) |
| 1230 | return; |
| 1231 | |
| 1232 | { |
| 1233 | llvm::SmallString<4096> HashTableBlob; |
| 1234 | uint32_t Offset; |
| 1235 | { |
| 1236 | llvm::OnDiskChainedHashTableGenerator<EnumConstantTableInfo> Generator; |
| 1237 | for (auto &EC : EnumConstants) |
| 1238 | Generator.insert(Key: EC.first, Data&: EC.second); |
| 1239 | |
| 1240 | llvm::raw_svector_ostream BlobStream(HashTableBlob); |
| 1241 | // Make sure that no bucket is at offset 0 |
| 1242 | llvm::support::endian::write<uint32_t>(os&: BlobStream, value: 0, |
| 1243 | endian: llvm::endianness::little); |
| 1244 | Offset = Generator.Emit(Out&: BlobStream); |
| 1245 | } |
| 1246 | |
| 1247 | enum_constant_block::EnumConstantDataLayout EnumConstantData(Stream); |
| 1248 | EnumConstantData.emit(buffer&: Scratch, data&: Offset, data&: HashTableBlob); |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | namespace { |
| 1253 | template <typename Derived, typename UnversionedDataType> |
| 1254 | class CommonTypeTableInfo |
| 1255 | : public VersionedTableInfo<Derived, SingleDeclTableKey, |
| 1256 | UnversionedDataType> { |
| 1257 | public: |
| 1258 | using key_type_ref = typename CommonTypeTableInfo::key_type_ref; |
| 1259 | using hash_value_type = typename CommonTypeTableInfo::hash_value_type; |
| 1260 | |
| 1261 | unsigned getKeyLength(key_type_ref) { |
| 1262 | return sizeof(uint32_t) + sizeof(IdentifierID); |
| 1263 | } |
| 1264 | |
| 1265 | void EmitKey(raw_ostream &OS, key_type_ref Key, unsigned) { |
| 1266 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 1267 | writer.write<uint32_t>(Key.parentContextID); |
| 1268 | writer.write<IdentifierID>(Key.nameID); |
| 1269 | } |
| 1270 | |
| 1271 | hash_value_type ComputeHash(key_type_ref Key) { |
| 1272 | return static_cast<size_t>(Key.hashValue()); |
| 1273 | } |
| 1274 | |
| 1275 | unsigned getUnversionedInfoSize(const UnversionedDataType &UDT) { |
| 1276 | return getCommonTypeInfoSize(UDT); |
| 1277 | } |
| 1278 | |
| 1279 | void emitUnversionedInfo(raw_ostream &OS, const UnversionedDataType &UDT) { |
| 1280 | emitCommonTypeInfo(OS, UDT); |
| 1281 | } |
| 1282 | }; |
| 1283 | |
| 1284 | /// Used to serialize the on-disk tag table. |
| 1285 | class TagTableInfo : public CommonTypeTableInfo<TagTableInfo, TagInfo> { |
| 1286 | public: |
| 1287 | unsigned getUnversionedInfoSize(const TagInfo &TI) { |
| 1288 | // clang-format off |
| 1289 | return 2 + (TI.SwiftImportAs ? TI.SwiftImportAs->size() : 0) + |
| 1290 | 2 + (TI.SwiftRetainOp ? TI.SwiftRetainOp->size() : 0) + |
| 1291 | 2 + (TI.SwiftReleaseOp ? TI.SwiftReleaseOp->size() : 0) + |
| 1292 | 2 + (TI.SwiftDestroyOp ? TI.SwiftDestroyOp->size() : 0) + |
| 1293 | 2 + (TI.SwiftDefaultOwnership ? TI.SwiftDefaultOwnership->size() : 0) + |
| 1294 | 3 + getCommonTypeInfoSize(CTI: TI); |
| 1295 | // clang-format on |
| 1296 | } |
| 1297 | |
| 1298 | void emitUnversionedInfo(raw_ostream &OS, const TagInfo &TI) { |
| 1299 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 1300 | |
| 1301 | uint8_t Flags = 0; |
| 1302 | if (auto extensibility = TI.EnumExtensibility) { |
| 1303 | Flags |= static_cast<uint8_t>(extensibility.value()) + 1; |
| 1304 | assert((Flags < (1 << 2)) && "must fit in two bits" ); |
| 1305 | } |
| 1306 | |
| 1307 | Flags <<= 2; |
| 1308 | if (auto value = TI.isFlagEnum()) |
| 1309 | Flags |= (value.value() << 1 | 1 << 0); |
| 1310 | |
| 1311 | writer.write<uint8_t>(Val: Flags); |
| 1312 | |
| 1313 | if (auto Copyable = TI.isSwiftCopyable()) |
| 1314 | writer.write<uint8_t>(Val: *Copyable ? kSwiftConforms : kSwiftDoesNotConform); |
| 1315 | else |
| 1316 | writer.write<uint8_t>(Val: 0); |
| 1317 | |
| 1318 | if (auto Escapable = TI.isSwiftEscapable()) |
| 1319 | writer.write<uint8_t>(Val: *Escapable ? kSwiftConforms : kSwiftDoesNotConform); |
| 1320 | else |
| 1321 | writer.write<uint8_t>(Val: 0); |
| 1322 | |
| 1323 | if (auto ImportAs = TI.SwiftImportAs) { |
| 1324 | writer.write<uint16_t>(Val: ImportAs->size() + 1); |
| 1325 | OS.write(Ptr: ImportAs->c_str(), Size: ImportAs->size()); |
| 1326 | } else { |
| 1327 | writer.write<uint16_t>(Val: 0); |
| 1328 | } |
| 1329 | if (auto RetainOp = TI.SwiftRetainOp) { |
| 1330 | writer.write<uint16_t>(Val: RetainOp->size() + 1); |
| 1331 | OS.write(Ptr: RetainOp->c_str(), Size: RetainOp->size()); |
| 1332 | } else { |
| 1333 | writer.write<uint16_t>(Val: 0); |
| 1334 | } |
| 1335 | if (auto ReleaseOp = TI.SwiftReleaseOp) { |
| 1336 | writer.write<uint16_t>(Val: ReleaseOp->size() + 1); |
| 1337 | OS.write(Ptr: ReleaseOp->c_str(), Size: ReleaseOp->size()); |
| 1338 | } else { |
| 1339 | writer.write<uint16_t>(Val: 0); |
| 1340 | } |
| 1341 | if (auto DefaultOwnership = TI.SwiftDefaultOwnership) { |
| 1342 | writer.write<uint16_t>(Val: DefaultOwnership->size() + 1); |
| 1343 | OS.write(Ptr: DefaultOwnership->c_str(), Size: DefaultOwnership->size()); |
| 1344 | } else { |
| 1345 | writer.write<uint16_t>(Val: 0); |
| 1346 | } |
| 1347 | if (auto DestroyOp = TI.SwiftDestroyOp) { |
| 1348 | writer.write<uint16_t>(Val: DestroyOp->size() + 1); |
| 1349 | OS.write(Ptr: DestroyOp->c_str(), Size: DestroyOp->size()); |
| 1350 | } else { |
| 1351 | writer.write<uint16_t>(Val: 0); |
| 1352 | } |
| 1353 | |
| 1354 | emitCommonTypeInfo(OS, CTI: TI); |
| 1355 | } |
| 1356 | }; |
| 1357 | } // namespace |
| 1358 | |
| 1359 | void APINotesWriter::Implementation::writeTagBlock( |
| 1360 | llvm::BitstreamWriter &Stream) { |
| 1361 | llvm::BCBlockRAII Scope(Stream, TAG_BLOCK_ID, 3); |
| 1362 | |
| 1363 | if (Tags.empty()) |
| 1364 | return; |
| 1365 | |
| 1366 | { |
| 1367 | llvm::SmallString<4096> HashTableBlob; |
| 1368 | uint32_t Offset; |
| 1369 | { |
| 1370 | llvm::OnDiskChainedHashTableGenerator<TagTableInfo> Generator; |
| 1371 | for (auto &T : Tags) |
| 1372 | Generator.insert(Key: T.first, Data&: T.second); |
| 1373 | |
| 1374 | llvm::raw_svector_ostream BlobStream(HashTableBlob); |
| 1375 | // Make sure that no bucket is at offset 0 |
| 1376 | llvm::support::endian::write<uint32_t>(os&: BlobStream, value: 0, |
| 1377 | endian: llvm::endianness::little); |
| 1378 | Offset = Generator.Emit(Out&: BlobStream); |
| 1379 | } |
| 1380 | |
| 1381 | tag_block::TagDataLayout TagData(Stream); |
| 1382 | TagData.emit(buffer&: Scratch, data&: Offset, data&: HashTableBlob); |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | namespace { |
| 1387 | /// Used to serialize the on-disk typedef table. |
| 1388 | class TypedefTableInfo |
| 1389 | : public CommonTypeTableInfo<TypedefTableInfo, TypedefInfo> { |
| 1390 | public: |
| 1391 | unsigned getUnversionedInfoSize(const TypedefInfo &TI) { |
| 1392 | return 1 + getCommonTypeInfoSize(CTI: TI); |
| 1393 | } |
| 1394 | |
| 1395 | void emitUnversionedInfo(raw_ostream &OS, const TypedefInfo &TI) { |
| 1396 | llvm::support::endian::Writer writer(OS, llvm::endianness::little); |
| 1397 | |
| 1398 | uint8_t Flags = 0; |
| 1399 | if (auto swiftWrapper = TI.SwiftWrapper) |
| 1400 | Flags |= static_cast<uint8_t>(*swiftWrapper) + 1; |
| 1401 | |
| 1402 | writer.write<uint8_t>(Val: Flags); |
| 1403 | |
| 1404 | emitCommonTypeInfo(OS, CTI: TI); |
| 1405 | } |
| 1406 | }; |
| 1407 | } // namespace |
| 1408 | |
| 1409 | void APINotesWriter::Implementation::writeTypedefBlock( |
| 1410 | llvm::BitstreamWriter &Stream) { |
| 1411 | llvm::BCBlockRAII Scope(Stream, TYPEDEF_BLOCK_ID, 3); |
| 1412 | |
| 1413 | if (Typedefs.empty()) |
| 1414 | return; |
| 1415 | |
| 1416 | { |
| 1417 | llvm::SmallString<4096> HashTableBlob; |
| 1418 | uint32_t Offset; |
| 1419 | { |
| 1420 | llvm::OnDiskChainedHashTableGenerator<TypedefTableInfo> Generator; |
| 1421 | for (auto &T : Typedefs) |
| 1422 | Generator.insert(Key: T.first, Data&: T.second); |
| 1423 | |
| 1424 | llvm::raw_svector_ostream BlobStream(HashTableBlob); |
| 1425 | // Make sure that no bucket is at offset 0 |
| 1426 | llvm::support::endian::write<uint32_t>(os&: BlobStream, value: 0, |
| 1427 | endian: llvm::endianness::little); |
| 1428 | Offset = Generator.Emit(Out&: BlobStream); |
| 1429 | } |
| 1430 | |
| 1431 | typedef_block::TypedefDataLayout TypedefData(Stream); |
| 1432 | TypedefData.emit(buffer&: Scratch, data&: Offset, data&: HashTableBlob); |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | // APINotesWriter |
| 1437 | |
| 1438 | APINotesWriter::APINotesWriter(llvm::StringRef ModuleName, const FileEntry *SF) |
| 1439 | : Implementation(new class Implementation(ModuleName, SF)) {} |
| 1440 | |
| 1441 | APINotesWriter::~APINotesWriter() = default; |
| 1442 | |
| 1443 | void APINotesWriter::writeToStream(llvm::raw_ostream &OS) { |
| 1444 | Implementation->writeToStream(OS); |
| 1445 | } |
| 1446 | |
| 1447 | ContextID APINotesWriter::addContext(std::optional<ContextID> ParentCtxID, |
| 1448 | llvm::StringRef Name, ContextKind Kind, |
| 1449 | const ContextInfo &Info, |
| 1450 | llvm::VersionTuple SwiftVersion) { |
| 1451 | IdentifierID NameID = Implementation->getIdentifier(Identifier: Name); |
| 1452 | |
| 1453 | uint32_t RawParentCtxID = ParentCtxID ? ParentCtxID->Value : -1; |
| 1454 | ContextTableKey Key(RawParentCtxID, static_cast<uint8_t>(Kind), NameID); |
| 1455 | auto Known = Implementation->Contexts.find(Val: Key); |
| 1456 | if (Known == Implementation->Contexts.end()) { |
| 1457 | unsigned NextID = Implementation->Contexts.size() + 1; |
| 1458 | |
| 1459 | Implementation::VersionedSmallVector<ContextInfo> EmptyVersionedInfo; |
| 1460 | Known = Implementation->Contexts |
| 1461 | .insert(KV: std::make_pair( |
| 1462 | x&: Key, y: std::make_pair(x&: NextID, y&: EmptyVersionedInfo))) |
| 1463 | .first; |
| 1464 | |
| 1465 | Implementation->ContextNames[NextID] = NameID; |
| 1466 | Implementation->ParentContexts[NextID] = RawParentCtxID; |
| 1467 | Implementation->ContextKinds[NextID] = static_cast<uint8_t>(Kind); |
| 1468 | } |
| 1469 | |
| 1470 | // Add this version information. |
| 1471 | auto &VersionedVec = Known->second.second; |
| 1472 | bool Found = false; |
| 1473 | for (auto &Versioned : VersionedVec) { |
| 1474 | if (Versioned.first == SwiftVersion) { |
| 1475 | Versioned.second |= Info; |
| 1476 | Found = true; |
| 1477 | break; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | if (!Found) |
| 1482 | VersionedVec.push_back(Elt: {SwiftVersion, Info}); |
| 1483 | |
| 1484 | return ContextID(Known->second.first); |
| 1485 | } |
| 1486 | |
| 1487 | void APINotesWriter::addObjCProperty(ContextID CtxID, StringRef Name, |
| 1488 | bool IsInstanceProperty, |
| 1489 | const ObjCPropertyInfo &Info, |
| 1490 | VersionTuple SwiftVersion) { |
| 1491 | IdentifierID NameID = Implementation->getIdentifier(Identifier: Name); |
| 1492 | Implementation |
| 1493 | ->ObjCProperties[std::make_tuple(args&: CtxID.Value, args&: NameID, args&: IsInstanceProperty)] |
| 1494 | .push_back(Elt: {SwiftVersion, Info}); |
| 1495 | } |
| 1496 | |
| 1497 | void APINotesWriter::addObjCMethod(ContextID CtxID, ObjCSelectorRef Selector, |
| 1498 | bool IsInstanceMethod, |
| 1499 | const ObjCMethodInfo &Info, |
| 1500 | VersionTuple SwiftVersion) { |
| 1501 | SelectorID SelID = Implementation->getSelector(SelectorRef: Selector); |
| 1502 | auto Key = std::tuple<unsigned, unsigned, char>{CtxID.Value, SelID, |
| 1503 | IsInstanceMethod}; |
| 1504 | Implementation->ObjCMethods[Key].push_back(Elt: {SwiftVersion, Info}); |
| 1505 | |
| 1506 | // If this method is a designated initializer, update the class to note that |
| 1507 | // it has designated initializers. |
| 1508 | if (Info.DesignatedInit) { |
| 1509 | assert(Implementation->ParentContexts.contains(CtxID.Value)); |
| 1510 | uint32_t ParentCtxID = Implementation->ParentContexts[CtxID.Value]; |
| 1511 | ContextTableKey CtxKey(ParentCtxID, |
| 1512 | Implementation->ContextKinds[CtxID.Value], |
| 1513 | Implementation->ContextNames[CtxID.Value]); |
| 1514 | assert(Implementation->Contexts.contains(CtxKey)); |
| 1515 | auto &VersionedVec = Implementation->Contexts[CtxKey].second; |
| 1516 | bool Found = false; |
| 1517 | for (auto &Versioned : VersionedVec) { |
| 1518 | if (Versioned.first == SwiftVersion) { |
| 1519 | Versioned.second.setHasDesignatedInits(true); |
| 1520 | Found = true; |
| 1521 | break; |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | if (!Found) { |
| 1526 | VersionedVec.push_back(Elt: {SwiftVersion, ContextInfo()}); |
| 1527 | VersionedVec.back().second.setHasDesignatedInits(true); |
| 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | void APINotesWriter::addCXXMethod(ContextID CtxID, llvm::StringRef Name, |
| 1533 | const CXXMethodInfo &Info, |
| 1534 | VersionTuple SwiftVersion) { |
| 1535 | IdentifierID NameID = Implementation->getIdentifier(Identifier: Name); |
| 1536 | SingleDeclTableKey Key(CtxID.Value, NameID); |
| 1537 | Implementation->CXXMethods[Key].push_back(Elt: {SwiftVersion, Info}); |
| 1538 | } |
| 1539 | |
| 1540 | void APINotesWriter::addField(ContextID CtxID, llvm::StringRef Name, |
| 1541 | const FieldInfo &Info, |
| 1542 | VersionTuple SwiftVersion) { |
| 1543 | IdentifierID NameID = Implementation->getIdentifier(Identifier: Name); |
| 1544 | SingleDeclTableKey Key(CtxID.Value, NameID); |
| 1545 | Implementation->Fields[Key].push_back(Elt: {SwiftVersion, Info}); |
| 1546 | } |
| 1547 | |
| 1548 | void APINotesWriter::addGlobalVariable(std::optional<Context> Ctx, |
| 1549 | llvm::StringRef Name, |
| 1550 | const GlobalVariableInfo &Info, |
| 1551 | VersionTuple SwiftVersion) { |
| 1552 | IdentifierID VariableID = Implementation->getIdentifier(Identifier: Name); |
| 1553 | SingleDeclTableKey Key(Ctx, VariableID); |
| 1554 | Implementation->GlobalVariables[Key].push_back(Elt: {SwiftVersion, Info}); |
| 1555 | } |
| 1556 | |
| 1557 | void APINotesWriter::addGlobalFunction(std::optional<Context> Ctx, |
| 1558 | llvm::StringRef Name, |
| 1559 | const GlobalFunctionInfo &Info, |
| 1560 | VersionTuple SwiftVersion) { |
| 1561 | IdentifierID NameID = Implementation->getIdentifier(Identifier: Name); |
| 1562 | SingleDeclTableKey Key(Ctx, NameID); |
| 1563 | Implementation->GlobalFunctions[Key].push_back(Elt: {SwiftVersion, Info}); |
| 1564 | } |
| 1565 | |
| 1566 | void APINotesWriter::addEnumConstant(llvm::StringRef Name, |
| 1567 | const EnumConstantInfo &Info, |
| 1568 | VersionTuple SwiftVersion) { |
| 1569 | IdentifierID EnumConstantID = Implementation->getIdentifier(Identifier: Name); |
| 1570 | Implementation->EnumConstants[EnumConstantID].push_back(Elt: {SwiftVersion, Info}); |
| 1571 | } |
| 1572 | |
| 1573 | void APINotesWriter::addTag(std::optional<Context> Ctx, llvm::StringRef Name, |
| 1574 | const TagInfo &Info, VersionTuple SwiftVersion) { |
| 1575 | IdentifierID TagID = Implementation->getIdentifier(Identifier: Name); |
| 1576 | SingleDeclTableKey Key(Ctx, TagID); |
| 1577 | Implementation->Tags[Key].push_back(Elt: {SwiftVersion, Info}); |
| 1578 | } |
| 1579 | |
| 1580 | void APINotesWriter::addTypedef(std::optional<Context> Ctx, |
| 1581 | llvm::StringRef Name, const TypedefInfo &Info, |
| 1582 | VersionTuple SwiftVersion) { |
| 1583 | IdentifierID TypedefID = Implementation->getIdentifier(Identifier: Name); |
| 1584 | SingleDeclTableKey Key(Ctx, TypedefID); |
| 1585 | Implementation->Typedefs[Key].push_back(Elt: {SwiftVersion, Info}); |
| 1586 | } |
| 1587 | } // namespace api_notes |
| 1588 | } // namespace clang |
| 1589 | |