1 //===- TypeRecordBuilder.h --------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLVM_DEBUGINFO_CODEVIEW_TYPERECORDBUILDER_H 11 #define LLVM_DEBUGINFO_CODEVIEW_TYPERECORDBUILDER_H 12 13 #include "llvm/ADT/SmallVector.h" 14 #include "llvm/DebugInfo/CodeView/CodeView.h" 15 #include "llvm/DebugInfo/CodeView/TypeIndex.h" 16 #include "llvm/DebugInfo/CodeView/TypeRecord.h" 17 #include "llvm/Support/EndianStream.h" 18 #include "llvm/Support/raw_ostream.h" 19 20 namespace llvm { 21 namespace codeview { 22 23 class TypeRecordBuilder { 24 private: 25 TypeRecordBuilder(const TypeRecordBuilder &) = delete; 26 TypeRecordBuilder &operator=(const TypeRecordBuilder &) = delete; 27 28 public: 29 explicit TypeRecordBuilder(TypeRecordKind Kind); 30 31 void writeUInt8(uint8_t Value); 32 void writeInt16(int16_t Value); 33 void writeUInt16(uint16_t Value); 34 void writeInt32(int32_t Value); 35 void writeUInt32(uint32_t Value); 36 void writeInt64(int64_t Value); 37 void writeUInt64(uint64_t Value); 38 void writeTypeIndex(TypeIndex TypeInd); 39 void writeTypeRecordKind(TypeRecordKind Kind); 40 void writeEncodedInteger(int64_t Value); 41 void writeEncodedSignedInteger(int64_t Value); 42 void writeEncodedUnsignedInteger(uint64_t Value); 43 void writeNullTerminatedString(StringRef Value); 44 void writeGuid(StringRef Guid); 45 void writeBytes(StringRef Value) { Stream << Value; } 46 47 llvm::StringRef str(); 48 49 uint64_t size() const { return Stream.tell(); } 50 TypeRecordKind kind() const { return Kind; } 51 52 /// Returns the number of bytes remaining before this record is larger than 53 /// the maximum record length. Accounts for the extra two byte size field in 54 /// the header. 55 size_t maxBytesRemaining() const { return MaxRecordLength - size() - 2; } 56 57 void truncate(uint64_t Size) { 58 // This works because raw_svector_ostream is not buffered. 59 assert(Size < Buffer.size()); 60 Buffer.resize(Size); 61 } 62 63 void reset(TypeRecordKind K) { 64 Buffer.clear(); 65 Kind = K; 66 writeTypeRecordKind(K); 67 } 68 69 private: 70 TypeRecordKind Kind; 71 llvm::SmallVector<char, 256> Buffer; 72 llvm::raw_svector_ostream Stream; 73 llvm::support::endian::Writer<llvm::support::endianness::little> Writer; 74 }; 75 } 76 } 77 78 #endif 79