LLVM  14.0.0git
SimpleTypeSerializer.cpp
Go to the documentation of this file.
1 //===- SimpleTypeSerializer.cpp -----------------------------------------===//
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 
13 
14 using namespace llvm;
15 using namespace llvm::codeview;
16 
17 static void addPadding(BinaryStreamWriter &Writer) {
18  uint32_t Align = Writer.getOffset() % 4;
19  if (Align == 0)
20  return;
21 
22  int PaddingBytes = 4 - Align;
23  while (PaddingBytes > 0) {
24  uint8_t Pad = static_cast<uint8_t>(LF_PAD0 + PaddingBytes);
25  cantFail(Writer.writeInteger(Pad));
26  --PaddingBytes;
27  }
28 }
29 
31 
33 
34 template <typename T>
36  BinaryStreamWriter Writer(ScratchBuffer, support::little);
37  TypeRecordMapping Mapping(Writer);
38 
39  // Write the record prefix first with a dummy length but real kind.
40  RecordPrefix DummyPrefix(uint16_t(Record.getKind()));
41  cantFail(Writer.writeObject(DummyPrefix));
42 
43  RecordPrefix *Prefix = reinterpret_cast<RecordPrefix *>(ScratchBuffer.data());
44  CVType CVT(Prefix, sizeof(RecordPrefix));
45 
46  cantFail(Mapping.visitTypeBegin(CVT));
47  cantFail(Mapping.visitKnownRecord(CVT, Record));
48  cantFail(Mapping.visitTypeEnd(CVT));
49 
50  addPadding(Writer);
51 
52  // Update the size and kind after serialization.
53  Prefix->RecordKind = CVT.kind();
54  Prefix->RecordLen = Writer.getOffset() - sizeof(uint16_t);
55 
56  return {ScratchBuffer.data(), static_cast<size_t>(Writer.getOffset())};
57 }
58 
59 // Explicitly instantiate the member function for each known type so that we can
60 // implement this in the cpp file.
61 #define TYPE_RECORD(EnumName, EnumVal, Name) \
62  template ArrayRef<uint8_t> llvm::codeview::SimpleTypeSerializer::serialize( \
63  Name##Record &Record);
64 #define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
65 #define MEMBER_RECORD(EnumName, EnumVal, Name)
66 #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
67 #include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
addPadding
static void addPadding(BinaryStreamWriter &Writer)
Definition: SimpleTypeSerializer.cpp:17
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::BinaryStreamWriter::writeInteger
Error writeInteger(T Value)
Write the integer Value to the underlying stream in the specified endianness.
Definition: BinaryStreamWriter.h:64
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:164
llvm::BinaryStreamWriter
Provides write only access to a subclass of WritableBinaryStream.
Definition: BinaryStreamWriter.h:31
llvm::codeview::TypeRecordMapping
Definition: TypeRecordMapping.h:23
llvm::codeview::SimpleTypeSerializer::~SimpleTypeSerializer
~SimpleTypeSerializer()
Definition: SimpleTypeSerializer.cpp:32
llvm::codeview::TypeRecordMapping::visitTypeEnd
Error visitTypeEnd(CVType &Record) override
Definition: TypeRecordMapping.cpp:244
llvm::codeview::RecordPrefix
Definition: RecordSerialization.h:33
llvm::support::little
@ little
Definition: Endian.h:27
SimpleTypeSerializer.h
Align
uint64_t Align
Definition: ELFObjHandler.cpp:83
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::codeview::TypeRecordMapping::visitTypeBegin
Error visitTypeBegin(CVType &Record) override
Paired begin/end actions for all types.
Definition: TypeRecordMapping.cpp:212
llvm::codeview::MaxRecordLength
@ MaxRecordLength
Definition: RecordSerialization.h:31
TypeRecord.h
llvm::codeview::CVRecord::kind
Kind kind() const
Definition: CVRecord.h:43
TypeRecordMapping.h
llvm::BinaryStreamWriter::getOffset
uint64_t getOffset() const
Definition: BinaryStreamWriter.h:184
llvm::Record
Definition: Record.h:1472
llvm::ArrayRef< uint8_t >
llvm::BinaryStreamWriter::writeObject
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
Definition: BinaryStreamWriter.h:135
llvm::codeview::CVRecord< TypeLeafKind >
llvm::cantFail
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:737
uint32_t
uint16_t
llvm::codeview::SimpleTypeSerializer::SimpleTypeSerializer
SimpleTypeSerializer()
Definition: SimpleTypeSerializer.cpp:30
llvm::codeview
Definition: AppendingTypeTableBuilder.h:22
llvm::codeview::SimpleTypeSerializer::serialize
ArrayRef< uint8_t > serialize(T &Record)
Definition: SimpleTypeSerializer.cpp:35
BinaryStreamWriter.h