LLVM  10.0.0svn
SimpleTypeSerializer.cpp
Go to the documentation of this file.
2 
3 using namespace llvm;
4 using namespace llvm::codeview;
5 
6 static void addPadding(BinaryStreamWriter &Writer) {
7  uint32_t Align = Writer.getOffset() % 4;
8  if (Align == 0)
9  return;
10 
11  int PaddingBytes = 4 - Align;
12  while (PaddingBytes > 0) {
13  uint8_t Pad = static_cast<uint8_t>(LF_PAD0 + PaddingBytes);
14  cantFail(Writer.writeInteger(Pad));
15  --PaddingBytes;
16  }
17 }
18 
20 
22 
23 template <typename T>
25  BinaryStreamWriter Writer(ScratchBuffer, support::little);
26  TypeRecordMapping Mapping(Writer);
27 
28  // Write the record prefix first with a dummy length but real kind.
29  RecordPrefix DummyPrefix(uint16_t(Record.getKind()));
30  cantFail(Writer.writeObject(DummyPrefix));
31 
32  RecordPrefix *Prefix = reinterpret_cast<RecordPrefix *>(ScratchBuffer.data());
33  CVType CVT(Prefix, sizeof(RecordPrefix));
34 
35  cantFail(Mapping.visitTypeBegin(CVT));
36  cantFail(Mapping.visitKnownRecord(CVT, Record));
37  cantFail(Mapping.visitTypeEnd(CVT));
38 
39  addPadding(Writer);
40 
41  // Update the size and kind after serialization.
42  Prefix->RecordKind = CVT.kind();
43  Prefix->RecordLen = Writer.getOffset() - sizeof(uint16_t);
44 
45  return {ScratchBuffer.data(), Writer.getOffset()};
46 }
47 
48 // Explicitly instantiate the member function for each known type so that we can
49 // implement this in the cpp file.
50 #define TYPE_RECORD(EnumName, EnumVal, Name) \
51  template ArrayRef<uint8_t> llvm::codeview::SimpleTypeSerializer::serialize( \
52  Name##Record &Record);
53 #define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
54 #define MEMBER_RECORD(EnumName, EnumVal, Name)
55 #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
56 #include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:703
static void addPadding(BinaryStreamWriter &Writer)
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
Kind kind() const
Definition: CVRecord.h:43
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Error visitTypeEnd(CVType &Record) override
Error visitTypeBegin(CVType &Record) override
Paired begin/end actions for all types.
ArrayRef< uint8_t > serialize(T &Record)
Provides write only access to a subclass of WritableBinaryStream.
Error writeInteger(T Value)
Write the integer Value to the underlying stream in the specified endianness.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:40