1//===- CodeViewYAMLTypes.cpp - CodeView YAMLIO types implementation -------===//
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// This file defines classes for handling the YAML representation of CodeView
10// Debug Info.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ObjectYAML/CodeViewYAMLTypes.h"
15#include "llvm/ADT/APSInt.h"
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/BinaryFormat/COFF.h"
19#include "llvm/DebugInfo/CodeView/AppendingTypeTableBuilder.h"
20#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
21#include "llvm/DebugInfo/CodeView/CodeView.h"
22#include "llvm/DebugInfo/CodeView/CodeViewError.h"
23#include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h"
24#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
25#include "llvm/DebugInfo/CodeView/TypeIndex.h"
26#include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
27#include "llvm/ObjectYAML/YAML.h"
28#include "llvm/Support/Allocator.h"
29#include "llvm/Support/BinaryStreamReader.h"
30#include "llvm/Support/BinaryStreamWriter.h"
31#include "llvm/Support/Endian.h"
32#include "llvm/Support/Error.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/YAMLTraits.h"
35#include "llvm/Support/raw_ostream.h"
36#include <algorithm>
37#include <cassert>
38#include <cstdint>
39#include <vector>
40
41using namespace llvm;
42using namespace llvm::codeview;
43using namespace llvm::CodeViewYAML;
44using namespace llvm::CodeViewYAML::detail;
45using namespace llvm::yaml;
46
47LLVM_YAML_IS_SEQUENCE_VECTOR(OneMethodRecord)
48LLVM_YAML_IS_SEQUENCE_VECTOR(VFTableSlotKind)
49LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(TypeIndex)
50
51LLVM_YAML_DECLARE_SCALAR_TRAITS(TypeIndex, QuotingType::None)
52LLVM_YAML_DECLARE_SCALAR_TRAITS(APSInt, QuotingType::None)
53
54LLVM_YAML_DECLARE_ENUM_TRAITS(TypeLeafKind)
55LLVM_YAML_DECLARE_ENUM_TRAITS(PointerToMemberRepresentation)
56LLVM_YAML_DECLARE_ENUM_TRAITS(VFTableSlotKind)
57LLVM_YAML_DECLARE_ENUM_TRAITS(CallingConvention)
58LLVM_YAML_DECLARE_ENUM_TRAITS(PointerKind)
59LLVM_YAML_DECLARE_ENUM_TRAITS(PointerMode)
60LLVM_YAML_DECLARE_ENUM_TRAITS(HfaKind)
61LLVM_YAML_DECLARE_ENUM_TRAITS(MemberAccess)
62LLVM_YAML_DECLARE_ENUM_TRAITS(MethodKind)
63LLVM_YAML_DECLARE_ENUM_TRAITS(WindowsRTClassKind)
64LLVM_YAML_DECLARE_ENUM_TRAITS(LabelType)
65
66LLVM_YAML_DECLARE_BITSET_TRAITS(PointerOptions)
67LLVM_YAML_DECLARE_BITSET_TRAITS(ModifierOptions)
68LLVM_YAML_DECLARE_BITSET_TRAITS(FunctionOptions)
69LLVM_YAML_DECLARE_BITSET_TRAITS(ClassOptions)
70LLVM_YAML_DECLARE_BITSET_TRAITS(MethodOptions)
71
72LLVM_YAML_DECLARE_MAPPING_TRAITS(OneMethodRecord)
73LLVM_YAML_DECLARE_MAPPING_TRAITS(MemberPointerInfo)
74
75namespace llvm {
76namespace CodeViewYAML {
77namespace detail {
78
79struct LeafRecordBase {
80 TypeLeafKind Kind;
81
82 explicit LeafRecordBase(TypeLeafKind K) : Kind(K) {}
83 virtual ~LeafRecordBase() = default;
84
85 virtual void map(yaml::IO &io) = 0;
86 virtual CVType toCodeViewRecord(AppendingTypeTableBuilder &TS) const = 0;
87 virtual Error fromCodeViewRecord(CVType Type) = 0;
88};
89
90struct UnknownLeafRecord : public LeafRecordBase {
91 explicit UnknownLeafRecord(TypeLeafKind K) : LeafRecordBase(K) {}
92
93 void map(yaml::IO &IO) override;
94
95 CVType toCodeViewRecord(AppendingTypeTableBuilder &TS) const override {
96 RecordPrefix Prefix;
97 uint32_t TotalLen = sizeof(RecordPrefix) + Data.size();
98 Prefix.RecordKind = Kind;
99 Prefix.RecordLen = TotalLen - 2;
100 uint8_t *Buffer = TS.getAllocator().Allocate<uint8_t>(Num: TotalLen);
101 ::memcpy(dest: Buffer, src: &Prefix, n: sizeof(RecordPrefix));
102 ::memcpy(dest: Buffer + sizeof(RecordPrefix), src: Data.data(), n: Data.size());
103 return CVType(ArrayRef<uint8_t>(Buffer, TotalLen));
104 }
105
106 Error fromCodeViewRecord(CVType Type) override {
107 this->Kind = Type.kind();
108 Data = Type.content();
109 return Error::success();
110 }
111
112 std::vector<uint8_t> Data;
113};
114
115template <typename T> struct LeafRecordImpl : public LeafRecordBase {
116 explicit LeafRecordImpl(TypeLeafKind K)
117 : LeafRecordBase(K), Record(static_cast<TypeRecordKind>(K)) {}
118
119 void map(yaml::IO &io) override;
120
121 Error fromCodeViewRecord(CVType Type) override {
122 return TypeDeserializer::deserializeAs<T>(Type, Record);
123 }
124
125 CVType toCodeViewRecord(AppendingTypeTableBuilder &TS) const override {
126 TS.writeLeafType(Record);
127 return CVType(TS.records().back());
128 }
129
130 mutable T Record;
131};
132
133template <> struct LeafRecordImpl<FieldListRecord> : public LeafRecordBase {
134 explicit LeafRecordImpl(TypeLeafKind K) : LeafRecordBase(K) {}
135
136 void map(yaml::IO &io) override;
137 CVType toCodeViewRecord(AppendingTypeTableBuilder &TS) const override;
138 Error fromCodeViewRecord(CVType Type) override;
139
140 std::vector<MemberRecord> Members;
141};
142
143struct MemberRecordBase {
144 TypeLeafKind Kind;
145
146 explicit MemberRecordBase(TypeLeafKind K) : Kind(K) {}
147 virtual ~MemberRecordBase() = default;
148
149 virtual void map(yaml::IO &io) = 0;
150 virtual void writeTo(ContinuationRecordBuilder &CRB) = 0;
151};
152
153template <typename T> struct MemberRecordImpl : public MemberRecordBase {
154 explicit MemberRecordImpl(TypeLeafKind K)
155 : MemberRecordBase(K), Record(static_cast<TypeRecordKind>(K)) {}
156
157 void map(yaml::IO &io) override;
158
159 void writeTo(ContinuationRecordBuilder &CRB) override {
160 CRB.writeMemberType(Record);
161 }
162
163 mutable T Record;
164};
165
166} // end namespace detail
167} // end namespace CodeViewYAML
168} // end namespace llvm
169
170void ScalarTraits<GUID>::output(const GUID &G, void *, llvm::raw_ostream &OS) {
171 OS << G;
172}
173
174StringRef ScalarTraits<GUID>::input(StringRef Scalar, void *Ctx, GUID &S) {
175 if (Scalar.size() != 38)
176 return "GUID strings are 38 characters long";
177 if (Scalar.front() != '{' || Scalar.back() != '}')
178 return "GUID is not enclosed in {}";
179 Scalar = Scalar.substr(Start: 1, N: Scalar.size() - 2);
180 SmallVector<StringRef, 6> A;
181 Scalar.split(A, Separator: '-', MaxSplit: 5);
182 if (A.size() != 5 || Scalar[8] != '-' || Scalar[13] != '-' ||
183 Scalar[18] != '-' || Scalar[23] != '-')
184 return "GUID sections are not properly delineated with dashes";
185 struct MSGuid {
186 support::ulittle32_t Data1;
187 support::ulittle16_t Data2;
188 support::ulittle16_t Data3;
189 support::ubig64_t Data4;
190 };
191 MSGuid G = {};
192 uint64_t D41{}, D42{};
193 if (!to_integer(S: A[0], Num&: G.Data1, Base: 16) || !to_integer(S: A[1], Num&: G.Data2, Base: 16) ||
194 !to_integer(S: A[2], Num&: G.Data3, Base: 16) || !to_integer(S: A[3], Num&: D41, Base: 16) ||
195 !to_integer(S: A[4], Num&: D42, Base: 16))
196 return "GUID contains non hex digits";
197 G.Data4 = (D41 << 48) | D42;
198 ::memcpy(dest: &S, src: &G, n: sizeof(GUID));
199 return "";
200}
201
202void ScalarTraits<TypeIndex>::output(const TypeIndex &S, void *,
203 raw_ostream &OS) {
204 OS << S.getIndex();
205}
206
207StringRef ScalarTraits<TypeIndex>::input(StringRef Scalar, void *Ctx,
208 TypeIndex &S) {
209 uint32_t I;
210 StringRef Result = ScalarTraits<uint32_t>::input(Scalar, Ctx, I);
211 S.setIndex(I);
212 return Result;
213}
214
215void ScalarTraits<APSInt>::output(const APSInt &S, void *, raw_ostream &OS) {
216 S.print(OS, isSigned: S.isSigned());
217}
218
219StringRef ScalarTraits<APSInt>::input(StringRef Scalar, void *Ctx, APSInt &S) {
220 S = APSInt(Scalar);
221 return "";
222}
223
224void ScalarEnumerationTraits<TypeLeafKind>::enumeration(IO &io,
225 TypeLeafKind &Value) {
226#define CV_TYPE(name, val) io.enumCase(Value, #name, name);
227#include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
228#undef CV_TYPE
229 io.enumFallback<Hex16>(Val&: Value);
230}
231
232void ScalarEnumerationTraits<PointerToMemberRepresentation>::enumeration(
233 IO &IO, PointerToMemberRepresentation &Value) {
234 IO.enumCase(Val&: Value, Str: "Unknown", ConstVal: PointerToMemberRepresentation::Unknown);
235 IO.enumCase(Val&: Value, Str: "SingleInheritanceData",
236 ConstVal: PointerToMemberRepresentation::SingleInheritanceData);
237 IO.enumCase(Val&: Value, Str: "MultipleInheritanceData",
238 ConstVal: PointerToMemberRepresentation::MultipleInheritanceData);
239 IO.enumCase(Val&: Value, Str: "VirtualInheritanceData",
240 ConstVal: PointerToMemberRepresentation::VirtualInheritanceData);
241 IO.enumCase(Val&: Value, Str: "GeneralData", ConstVal: PointerToMemberRepresentation::GeneralData);
242 IO.enumCase(Val&: Value, Str: "SingleInheritanceFunction",
243 ConstVal: PointerToMemberRepresentation::SingleInheritanceFunction);
244 IO.enumCase(Val&: Value, Str: "MultipleInheritanceFunction",
245 ConstVal: PointerToMemberRepresentation::MultipleInheritanceFunction);
246 IO.enumCase(Val&: Value, Str: "VirtualInheritanceFunction",
247 ConstVal: PointerToMemberRepresentation::VirtualInheritanceFunction);
248 IO.enumCase(Val&: Value, Str: "GeneralFunction",
249 ConstVal: PointerToMemberRepresentation::GeneralFunction);
250}
251
252void ScalarEnumerationTraits<VFTableSlotKind>::enumeration(
253 IO &IO, VFTableSlotKind &Kind) {
254 IO.enumCase(Val&: Kind, Str: "Near16", ConstVal: VFTableSlotKind::Near16);
255 IO.enumCase(Val&: Kind, Str: "Far16", ConstVal: VFTableSlotKind::Far16);
256 IO.enumCase(Val&: Kind, Str: "This", ConstVal: VFTableSlotKind::This);
257 IO.enumCase(Val&: Kind, Str: "Outer", ConstVal: VFTableSlotKind::Outer);
258 IO.enumCase(Val&: Kind, Str: "Meta", ConstVal: VFTableSlotKind::Meta);
259 IO.enumCase(Val&: Kind, Str: "Near", ConstVal: VFTableSlotKind::Near);
260 IO.enumCase(Val&: Kind, Str: "Far", ConstVal: VFTableSlotKind::Far);
261}
262
263void ScalarEnumerationTraits<CallingConvention>::enumeration(
264 IO &IO, CallingConvention &Value) {
265 IO.enumCase(Val&: Value, Str: "NearC", ConstVal: CallingConvention::NearC);
266 IO.enumCase(Val&: Value, Str: "FarC", ConstVal: CallingConvention::FarC);
267 IO.enumCase(Val&: Value, Str: "NearPascal", ConstVal: CallingConvention::NearPascal);
268 IO.enumCase(Val&: Value, Str: "FarPascal", ConstVal: CallingConvention::FarPascal);
269 IO.enumCase(Val&: Value, Str: "NearFast", ConstVal: CallingConvention::NearFast);
270 IO.enumCase(Val&: Value, Str: "FarFast", ConstVal: CallingConvention::FarFast);
271 IO.enumCase(Val&: Value, Str: "NearStdCall", ConstVal: CallingConvention::NearStdCall);
272 IO.enumCase(Val&: Value, Str: "FarStdCall", ConstVal: CallingConvention::FarStdCall);
273 IO.enumCase(Val&: Value, Str: "NearSysCall", ConstVal: CallingConvention::NearSysCall);
274 IO.enumCase(Val&: Value, Str: "FarSysCall", ConstVal: CallingConvention::FarSysCall);
275 IO.enumCase(Val&: Value, Str: "ThisCall", ConstVal: CallingConvention::ThisCall);
276 IO.enumCase(Val&: Value, Str: "MipsCall", ConstVal: CallingConvention::MipsCall);
277 IO.enumCase(Val&: Value, Str: "Generic", ConstVal: CallingConvention::Generic);
278 IO.enumCase(Val&: Value, Str: "AlphaCall", ConstVal: CallingConvention::AlphaCall);
279 IO.enumCase(Val&: Value, Str: "PpcCall", ConstVal: CallingConvention::PpcCall);
280 IO.enumCase(Val&: Value, Str: "SHCall", ConstVal: CallingConvention::SHCall);
281 IO.enumCase(Val&: Value, Str: "ArmCall", ConstVal: CallingConvention::ArmCall);
282 IO.enumCase(Val&: Value, Str: "AM33Call", ConstVal: CallingConvention::AM33Call);
283 IO.enumCase(Val&: Value, Str: "TriCall", ConstVal: CallingConvention::TriCall);
284 IO.enumCase(Val&: Value, Str: "SH5Call", ConstVal: CallingConvention::SH5Call);
285 IO.enumCase(Val&: Value, Str: "M32RCall", ConstVal: CallingConvention::M32RCall);
286 IO.enumCase(Val&: Value, Str: "ClrCall", ConstVal: CallingConvention::ClrCall);
287 IO.enumCase(Val&: Value, Str: "Inline", ConstVal: CallingConvention::Inline);
288 IO.enumCase(Val&: Value, Str: "NearVector", ConstVal: CallingConvention::NearVector);
289 IO.enumCase(Val&: Value, Str: "Swift", ConstVal: CallingConvention::Swift);
290}
291
292void ScalarEnumerationTraits<PointerKind>::enumeration(IO &IO,
293 PointerKind &Kind) {
294 IO.enumCase(Val&: Kind, Str: "Near16", ConstVal: PointerKind::Near16);
295 IO.enumCase(Val&: Kind, Str: "Far16", ConstVal: PointerKind::Far16);
296 IO.enumCase(Val&: Kind, Str: "Huge16", ConstVal: PointerKind::Huge16);
297 IO.enumCase(Val&: Kind, Str: "BasedOnSegment", ConstVal: PointerKind::BasedOnSegment);
298 IO.enumCase(Val&: Kind, Str: "BasedOnValue", ConstVal: PointerKind::BasedOnValue);
299 IO.enumCase(Val&: Kind, Str: "BasedOnSegmentValue", ConstVal: PointerKind::BasedOnSegmentValue);
300 IO.enumCase(Val&: Kind, Str: "BasedOnAddress", ConstVal: PointerKind::BasedOnAddress);
301 IO.enumCase(Val&: Kind, Str: "BasedOnSegmentAddress",
302 ConstVal: PointerKind::BasedOnSegmentAddress);
303 IO.enumCase(Val&: Kind, Str: "BasedOnType", ConstVal: PointerKind::BasedOnType);
304 IO.enumCase(Val&: Kind, Str: "BasedOnSelf", ConstVal: PointerKind::BasedOnSelf);
305 IO.enumCase(Val&: Kind, Str: "Near32", ConstVal: PointerKind::Near32);
306 IO.enumCase(Val&: Kind, Str: "Far32", ConstVal: PointerKind::Far32);
307 IO.enumCase(Val&: Kind, Str: "Near64", ConstVal: PointerKind::Near64);
308}
309
310void ScalarEnumerationTraits<PointerMode>::enumeration(IO &IO,
311 PointerMode &Mode) {
312 IO.enumCase(Val&: Mode, Str: "Pointer", ConstVal: PointerMode::Pointer);
313 IO.enumCase(Val&: Mode, Str: "LValueReference", ConstVal: PointerMode::LValueReference);
314 IO.enumCase(Val&: Mode, Str: "PointerToDataMember", ConstVal: PointerMode::PointerToDataMember);
315 IO.enumCase(Val&: Mode, Str: "PointerToMemberFunction",
316 ConstVal: PointerMode::PointerToMemberFunction);
317 IO.enumCase(Val&: Mode, Str: "RValueReference", ConstVal: PointerMode::RValueReference);
318}
319
320void ScalarEnumerationTraits<HfaKind>::enumeration(IO &IO, HfaKind &Value) {
321 IO.enumCase(Val&: Value, Str: "None", ConstVal: HfaKind::None);
322 IO.enumCase(Val&: Value, Str: "Float", ConstVal: HfaKind::Float);
323 IO.enumCase(Val&: Value, Str: "Double", ConstVal: HfaKind::Double);
324 IO.enumCase(Val&: Value, Str: "Other", ConstVal: HfaKind::Other);
325}
326
327void ScalarEnumerationTraits<MemberAccess>::enumeration(IO &IO,
328 MemberAccess &Access) {
329 IO.enumCase(Val&: Access, Str: "None", ConstVal: MemberAccess::None);
330 IO.enumCase(Val&: Access, Str: "Private", ConstVal: MemberAccess::Private);
331 IO.enumCase(Val&: Access, Str: "Protected", ConstVal: MemberAccess::Protected);
332 IO.enumCase(Val&: Access, Str: "Public", ConstVal: MemberAccess::Public);
333}
334
335void ScalarEnumerationTraits<MethodKind>::enumeration(IO &IO,
336 MethodKind &Kind) {
337 IO.enumCase(Val&: Kind, Str: "Vanilla", ConstVal: MethodKind::Vanilla);
338 IO.enumCase(Val&: Kind, Str: "Virtual", ConstVal: MethodKind::Virtual);
339 IO.enumCase(Val&: Kind, Str: "Static", ConstVal: MethodKind::Static);
340 IO.enumCase(Val&: Kind, Str: "Friend", ConstVal: MethodKind::Friend);
341 IO.enumCase(Val&: Kind, Str: "IntroducingVirtual", ConstVal: MethodKind::IntroducingVirtual);
342 IO.enumCase(Val&: Kind, Str: "PureVirtual", ConstVal: MethodKind::PureVirtual);
343 IO.enumCase(Val&: Kind, Str: "PureIntroducingVirtual",
344 ConstVal: MethodKind::PureIntroducingVirtual);
345}
346
347void ScalarEnumerationTraits<WindowsRTClassKind>::enumeration(
348 IO &IO, WindowsRTClassKind &Value) {
349 IO.enumCase(Val&: Value, Str: "None", ConstVal: WindowsRTClassKind::None);
350 IO.enumCase(Val&: Value, Str: "Ref", ConstVal: WindowsRTClassKind::RefClass);
351 IO.enumCase(Val&: Value, Str: "Value", ConstVal: WindowsRTClassKind::ValueClass);
352 IO.enumCase(Val&: Value, Str: "Interface", ConstVal: WindowsRTClassKind::Interface);
353}
354
355void ScalarEnumerationTraits<LabelType>::enumeration(IO &IO, LabelType &Value) {
356 IO.enumCase(Val&: Value, Str: "Near", ConstVal: LabelType::Near);
357 IO.enumCase(Val&: Value, Str: "Far", ConstVal: LabelType::Far);
358}
359
360void ScalarBitSetTraits<PointerOptions>::bitset(IO &IO,
361 PointerOptions &Options) {
362 IO.bitSetCase(Val&: Options, Str: "None", ConstVal: PointerOptions::None);
363 IO.bitSetCase(Val&: Options, Str: "Flat32", ConstVal: PointerOptions::Flat32);
364 IO.bitSetCase(Val&: Options, Str: "Volatile", ConstVal: PointerOptions::Volatile);
365 IO.bitSetCase(Val&: Options, Str: "Const", ConstVal: PointerOptions::Const);
366 IO.bitSetCase(Val&: Options, Str: "Unaligned", ConstVal: PointerOptions::Unaligned);
367 IO.bitSetCase(Val&: Options, Str: "Restrict", ConstVal: PointerOptions::Restrict);
368 IO.bitSetCase(Val&: Options, Str: "WinRTSmartPointer",
369 ConstVal: PointerOptions::WinRTSmartPointer);
370}
371
372void ScalarBitSetTraits<ModifierOptions>::bitset(IO &IO,
373 ModifierOptions &Options) {
374 IO.bitSetCase(Val&: Options, Str: "None", ConstVal: ModifierOptions::None);
375 IO.bitSetCase(Val&: Options, Str: "Const", ConstVal: ModifierOptions::Const);
376 IO.bitSetCase(Val&: Options, Str: "Volatile", ConstVal: ModifierOptions::Volatile);
377 IO.bitSetCase(Val&: Options, Str: "Unaligned", ConstVal: ModifierOptions::Unaligned);
378}
379
380void ScalarBitSetTraits<FunctionOptions>::bitset(IO &IO,
381 FunctionOptions &Options) {
382 IO.bitSetCase(Val&: Options, Str: "None", ConstVal: FunctionOptions::None);
383 IO.bitSetCase(Val&: Options, Str: "CxxReturnUdt", ConstVal: FunctionOptions::CxxReturnUdt);
384 IO.bitSetCase(Val&: Options, Str: "Constructor", ConstVal: FunctionOptions::Constructor);
385 IO.bitSetCase(Val&: Options, Str: "ConstructorWithVirtualBases",
386 ConstVal: FunctionOptions::ConstructorWithVirtualBases);
387}
388
389void ScalarBitSetTraits<ClassOptions>::bitset(IO &IO, ClassOptions &Options) {
390 IO.bitSetCase(Val&: Options, Str: "None", ConstVal: ClassOptions::None);
391 IO.bitSetCase(Val&: Options, Str: "HasConstructorOrDestructor",
392 ConstVal: ClassOptions::HasConstructorOrDestructor);
393 IO.bitSetCase(Val&: Options, Str: "HasOverloadedOperator",
394 ConstVal: ClassOptions::HasOverloadedOperator);
395 IO.bitSetCase(Val&: Options, Str: "Nested", ConstVal: ClassOptions::Nested);
396 IO.bitSetCase(Val&: Options, Str: "ContainsNestedClass",
397 ConstVal: ClassOptions::ContainsNestedClass);
398 IO.bitSetCase(Val&: Options, Str: "HasOverloadedAssignmentOperator",
399 ConstVal: ClassOptions::HasOverloadedAssignmentOperator);
400 IO.bitSetCase(Val&: Options, Str: "HasConversionOperator",
401 ConstVal: ClassOptions::HasConversionOperator);
402 IO.bitSetCase(Val&: Options, Str: "ForwardReference", ConstVal: ClassOptions::ForwardReference);
403 IO.bitSetCase(Val&: Options, Str: "Scoped", ConstVal: ClassOptions::Scoped);
404 IO.bitSetCase(Val&: Options, Str: "HasUniqueName", ConstVal: ClassOptions::HasUniqueName);
405 IO.bitSetCase(Val&: Options, Str: "Sealed", ConstVal: ClassOptions::Sealed);
406 IO.bitSetCase(Val&: Options, Str: "Intrinsic", ConstVal: ClassOptions::Intrinsic);
407}
408
409void ScalarBitSetTraits<MethodOptions>::bitset(IO &IO, MethodOptions &Options) {
410 IO.bitSetCase(Val&: Options, Str: "None", ConstVal: MethodOptions::None);
411 IO.bitSetCase(Val&: Options, Str: "Pseudo", ConstVal: MethodOptions::Pseudo);
412 IO.bitSetCase(Val&: Options, Str: "NoInherit", ConstVal: MethodOptions::NoInherit);
413 IO.bitSetCase(Val&: Options, Str: "NoConstruct", ConstVal: MethodOptions::NoConstruct);
414 IO.bitSetCase(Val&: Options, Str: "CompilerGenerated", ConstVal: MethodOptions::CompilerGenerated);
415 IO.bitSetCase(Val&: Options, Str: "Sealed", ConstVal: MethodOptions::Sealed);
416}
417
418void MappingTraits<MemberPointerInfo>::mapping(IO &IO, MemberPointerInfo &MPI) {
419 IO.mapRequired(Key: "ContainingType", Val&: MPI.ContainingType);
420 IO.mapRequired(Key: "Representation", Val&: MPI.Representation);
421}
422
423namespace llvm {
424namespace CodeViewYAML {
425namespace detail {
426
427void UnknownLeafRecord::map(IO &IO) {
428 yaml::BinaryRef Binary;
429 if (IO.outputting())
430 Binary = yaml::BinaryRef(Data);
431 IO.mapRequired(Key: "Data", Val&: Binary);
432 if (!IO.outputting()) {
433 std::string Str;
434 raw_string_ostream OS(Str);
435 Binary.writeAsBinary(OS);
436 Data.assign(first: Str.begin(), last: Str.end());
437 }
438}
439
440template <> void LeafRecordImpl<ModifierRecord>::map(IO &IO) {
441 IO.mapRequired(Key: "ModifiedType", Val&: Record.ModifiedType);
442 IO.mapRequired(Key: "Modifiers", Val&: Record.Modifiers);
443}
444
445template <> void LeafRecordImpl<ProcedureRecord>::map(IO &IO) {
446 IO.mapRequired(Key: "ReturnType", Val&: Record.ReturnType);
447 IO.mapRequired(Key: "CallConv", Val&: Record.CallConv);
448 IO.mapRequired(Key: "Options", Val&: Record.Options);
449 IO.mapRequired(Key: "ParameterCount", Val&: Record.ParameterCount);
450 IO.mapRequired(Key: "ArgumentList", Val&: Record.ArgumentList);
451}
452
453template <> void LeafRecordImpl<MemberFunctionRecord>::map(IO &IO) {
454 IO.mapRequired(Key: "ReturnType", Val&: Record.ReturnType);
455 IO.mapRequired(Key: "ClassType", Val&: Record.ClassType);
456 IO.mapRequired(Key: "ThisType", Val&: Record.ThisType);
457 IO.mapRequired(Key: "CallConv", Val&: Record.CallConv);
458 IO.mapRequired(Key: "Options", Val&: Record.Options);
459 IO.mapRequired(Key: "ParameterCount", Val&: Record.ParameterCount);
460 IO.mapRequired(Key: "ArgumentList", Val&: Record.ArgumentList);
461 IO.mapRequired(Key: "ThisPointerAdjustment", Val&: Record.ThisPointerAdjustment);
462}
463
464template <> void LeafRecordImpl<LabelRecord>::map(IO &IO) {
465 IO.mapRequired(Key: "Mode", Val&: Record.Mode);
466}
467
468template <> void LeafRecordImpl<MemberFuncIdRecord>::map(IO &IO) {
469 IO.mapRequired(Key: "ClassType", Val&: Record.ClassType);
470 IO.mapRequired(Key: "FunctionType", Val&: Record.FunctionType);
471 IO.mapRequired(Key: "Name", Val&: Record.Name);
472}
473
474template <> void LeafRecordImpl<ArgListRecord>::map(IO &IO) {
475 IO.mapRequired(Key: "ArgIndices", Val&: Record.ArgIndices);
476}
477
478template <> void LeafRecordImpl<StringListRecord>::map(IO &IO) {
479 IO.mapRequired(Key: "StringIndices", Val&: Record.StringIndices);
480}
481
482template <> void LeafRecordImpl<PointerRecord>::map(IO &IO) {
483 IO.mapRequired(Key: "ReferentType", Val&: Record.ReferentType);
484 IO.mapRequired(Key: "Attrs", Val&: Record.Attrs);
485 IO.mapOptional(Key: "MemberInfo", Val&: Record.MemberInfo);
486}
487
488template <> void LeafRecordImpl<ArrayRecord>::map(IO &IO) {
489 IO.mapRequired(Key: "ElementType", Val&: Record.ElementType);
490 IO.mapRequired(Key: "IndexType", Val&: Record.IndexType);
491 IO.mapRequired(Key: "Size", Val&: Record.Size);
492 IO.mapRequired(Key: "Name", Val&: Record.Name);
493}
494
495void LeafRecordImpl<FieldListRecord>::map(IO &IO) {
496 IO.mapRequired(Key: "FieldList", Val&: Members);
497}
498
499} // end namespace detail
500} // end namespace CodeViewYAML
501} // end namespace llvm
502
503namespace {
504
505class MemberRecordConversionVisitor : public TypeVisitorCallbacks {
506public:
507 explicit MemberRecordConversionVisitor(std::vector<MemberRecord> &Records)
508 : Records(Records) {}
509
510#define TYPE_RECORD(EnumName, EnumVal, Name)
511#define MEMBER_RECORD(EnumName, EnumVal, Name) \
512 Error visitKnownMember(CVMemberRecord &CVR, Name##Record &Record) override { \
513 return visitKnownMemberImpl(Record); \
514 }
515#define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
516#define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
517#include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
518private:
519 template <typename T> Error visitKnownMemberImpl(T &Record) {
520 TypeLeafKind K = static_cast<TypeLeafKind>(Record.getKind());
521 auto Impl = std::make_shared<MemberRecordImpl<T>>(K);
522 Impl->Record = Record;
523 Records.push_back(x: MemberRecord{Impl});
524 return Error::success();
525 }
526
527 std::vector<MemberRecord> &Records;
528};
529
530} // end anonymous namespace
531
532Error LeafRecordImpl<FieldListRecord>::fromCodeViewRecord(CVType Type) {
533 MemberRecordConversionVisitor V(Members);
534 FieldListRecord FieldList;
535 cantFail(Err: TypeDeserializer::deserializeAs<FieldListRecord>(CVT&: Type,
536 Record&: FieldList));
537 return visitMemberRecordStream(FieldList: FieldList.Data, Callbacks&: V);
538}
539
540CVType LeafRecordImpl<FieldListRecord>::toCodeViewRecord(
541 AppendingTypeTableBuilder &TS) const {
542 ContinuationRecordBuilder CRB;
543 CRB.begin(RecordKind: ContinuationRecordKind::FieldList);
544 for (const auto &Member : Members) {
545 Member.Member->writeTo(CRB);
546 }
547 TS.insertRecord(Builder&: CRB);
548 return CVType(TS.records().back());
549}
550
551void MappingTraits<OneMethodRecord>::mapping(IO &io, OneMethodRecord &Record) {
552 io.mapRequired(Key: "Type", Val&: Record.Type);
553 io.mapRequired(Key: "Attrs", Val&: Record.Attrs.Attrs);
554 io.mapRequired(Key: "VFTableOffset", Val&: Record.VFTableOffset);
555 io.mapRequired(Key: "Name", Val&: Record.Name);
556}
557
558namespace llvm {
559namespace CodeViewYAML {
560namespace detail {
561
562template <> void LeafRecordImpl<ClassRecord>::map(IO &IO) {
563 IO.mapRequired(Key: "MemberCount", Val&: Record.MemberCount);
564 IO.mapRequired(Key: "Options", Val&: Record.Options);
565 IO.mapRequired(Key: "FieldList", Val&: Record.FieldList);
566 IO.mapRequired(Key: "Name", Val&: Record.Name);
567 IO.mapRequired(Key: "UniqueName", Val&: Record.UniqueName);
568 IO.mapRequired(Key: "DerivationList", Val&: Record.DerivationList);
569 IO.mapRequired(Key: "VTableShape", Val&: Record.VTableShape);
570 IO.mapRequired(Key: "Size", Val&: Record.Size);
571}
572
573template <> void LeafRecordImpl<UnionRecord>::map(IO &IO) {
574 IO.mapRequired(Key: "MemberCount", Val&: Record.MemberCount);
575 IO.mapRequired(Key: "Options", Val&: Record.Options);
576 IO.mapRequired(Key: "FieldList", Val&: Record.FieldList);
577 IO.mapRequired(Key: "Name", Val&: Record.Name);
578 IO.mapRequired(Key: "UniqueName", Val&: Record.UniqueName);
579 IO.mapRequired(Key: "Size", Val&: Record.Size);
580}
581
582template <> void LeafRecordImpl<EnumRecord>::map(IO &IO) {
583 IO.mapRequired(Key: "NumEnumerators", Val&: Record.MemberCount);
584 IO.mapRequired(Key: "Options", Val&: Record.Options);
585 IO.mapRequired(Key: "FieldList", Val&: Record.FieldList);
586 IO.mapRequired(Key: "Name", Val&: Record.Name);
587 IO.mapRequired(Key: "UniqueName", Val&: Record.UniqueName);
588 IO.mapRequired(Key: "UnderlyingType", Val&: Record.UnderlyingType);
589}
590
591template <> void LeafRecordImpl<BitFieldRecord>::map(IO &IO) {
592 IO.mapRequired(Key: "Type", Val&: Record.Type);
593 IO.mapRequired(Key: "BitSize", Val&: Record.BitSize);
594 IO.mapRequired(Key: "BitOffset", Val&: Record.BitOffset);
595}
596
597template <> void LeafRecordImpl<VFTableShapeRecord>::map(IO &IO) {
598 IO.mapRequired(Key: "Slots", Val&: Record.Slots);
599}
600
601template <> void LeafRecordImpl<TypeServer2Record>::map(IO &IO) {
602 IO.mapRequired(Key: "Guid", Val&: Record.Guid);
603 IO.mapRequired(Key: "Age", Val&: Record.Age);
604 IO.mapRequired(Key: "Name", Val&: Record.Name);
605}
606
607template <> void LeafRecordImpl<StringIdRecord>::map(IO &IO) {
608 IO.mapRequired(Key: "Id", Val&: Record.Id);
609 IO.mapRequired(Key: "String", Val&: Record.String);
610}
611
612template <> void LeafRecordImpl<FuncIdRecord>::map(IO &IO) {
613 IO.mapRequired(Key: "ParentScope", Val&: Record.ParentScope);
614 IO.mapRequired(Key: "FunctionType", Val&: Record.FunctionType);
615 IO.mapRequired(Key: "Name", Val&: Record.Name);
616}
617
618template <> void LeafRecordImpl<UdtSourceLineRecord>::map(IO &IO) {
619 IO.mapRequired(Key: "UDT", Val&: Record.UDT);
620 IO.mapRequired(Key: "SourceFile", Val&: Record.SourceFile);
621 IO.mapRequired(Key: "LineNumber", Val&: Record.LineNumber);
622}
623
624template <> void LeafRecordImpl<UdtModSourceLineRecord>::map(IO &IO) {
625 IO.mapRequired(Key: "UDT", Val&: Record.UDT);
626 IO.mapRequired(Key: "SourceFile", Val&: Record.SourceFile);
627 IO.mapRequired(Key: "LineNumber", Val&: Record.LineNumber);
628 IO.mapRequired(Key: "Module", Val&: Record.Module);
629}
630
631template <> void LeafRecordImpl<BuildInfoRecord>::map(IO &IO) {
632 IO.mapRequired(Key: "ArgIndices", Val&: Record.ArgIndices);
633}
634
635template <> void LeafRecordImpl<VFTableRecord>::map(IO &IO) {
636 IO.mapRequired(Key: "CompleteClass", Val&: Record.CompleteClass);
637 IO.mapRequired(Key: "OverriddenVFTable", Val&: Record.OverriddenVFTable);
638 IO.mapRequired(Key: "VFPtrOffset", Val&: Record.VFPtrOffset);
639 IO.mapRequired(Key: "MethodNames", Val&: Record.MethodNames);
640}
641
642template <> void LeafRecordImpl<MethodOverloadListRecord>::map(IO &IO) {
643 IO.mapRequired(Key: "Methods", Val&: Record.Methods);
644}
645
646template <> void LeafRecordImpl<PrecompRecord>::map(IO &IO) {
647 IO.mapRequired(Key: "StartTypeIndex", Val&: Record.StartTypeIndex);
648 IO.mapRequired(Key: "TypesCount", Val&: Record.TypesCount);
649 IO.mapRequired(Key: "Signature", Val&: Record.Signature);
650 IO.mapRequired(Key: "PrecompFilePath", Val&: Record.PrecompFilePath);
651}
652
653template <> void LeafRecordImpl<EndPrecompRecord>::map(IO &IO) {
654 IO.mapRequired(Key: "Signature", Val&: Record.Signature);
655}
656
657template <> void MemberRecordImpl<OneMethodRecord>::map(IO &IO) {
658 MappingTraits<OneMethodRecord>::mapping(io&: IO, Record);
659}
660
661template <> void MemberRecordImpl<OverloadedMethodRecord>::map(IO &IO) {
662 IO.mapRequired(Key: "NumOverloads", Val&: Record.NumOverloads);
663 IO.mapRequired(Key: "MethodList", Val&: Record.MethodList);
664 IO.mapRequired(Key: "Name", Val&: Record.Name);
665}
666
667template <> void MemberRecordImpl<NestedTypeRecord>::map(IO &IO) {
668 IO.mapRequired(Key: "Type", Val&: Record.Type);
669 IO.mapRequired(Key: "Name", Val&: Record.Name);
670}
671
672template <> void MemberRecordImpl<DataMemberRecord>::map(IO &IO) {
673 IO.mapRequired(Key: "Attrs", Val&: Record.Attrs.Attrs);
674 IO.mapRequired(Key: "Type", Val&: Record.Type);
675 IO.mapRequired(Key: "FieldOffset", Val&: Record.FieldOffset);
676 IO.mapRequired(Key: "Name", Val&: Record.Name);
677}
678
679template <> void MemberRecordImpl<StaticDataMemberRecord>::map(IO &IO) {
680 IO.mapRequired(Key: "Attrs", Val&: Record.Attrs.Attrs);
681 IO.mapRequired(Key: "Type", Val&: Record.Type);
682 IO.mapRequired(Key: "Name", Val&: Record.Name);
683}
684
685template <> void MemberRecordImpl<EnumeratorRecord>::map(IO &IO) {
686 IO.mapRequired(Key: "Attrs", Val&: Record.Attrs.Attrs);
687 IO.mapRequired(Key: "Value", Val&: Record.Value);
688 IO.mapRequired(Key: "Name", Val&: Record.Name);
689}
690
691template <> void MemberRecordImpl<VFPtrRecord>::map(IO &IO) {
692 IO.mapRequired(Key: "Type", Val&: Record.Type);
693}
694
695template <> void MemberRecordImpl<BaseClassRecord>::map(IO &IO) {
696 IO.mapRequired(Key: "Attrs", Val&: Record.Attrs.Attrs);
697 IO.mapRequired(Key: "Type", Val&: Record.Type);
698 IO.mapRequired(Key: "Offset", Val&: Record.Offset);
699}
700
701template <> void MemberRecordImpl<VirtualBaseClassRecord>::map(IO &IO) {
702 IO.mapRequired(Key: "Attrs", Val&: Record.Attrs.Attrs);
703 IO.mapRequired(Key: "BaseType", Val&: Record.BaseType);
704 IO.mapRequired(Key: "VBPtrType", Val&: Record.VBPtrType);
705 IO.mapRequired(Key: "VBPtrOffset", Val&: Record.VBPtrOffset);
706 IO.mapRequired(Key: "VTableIndex", Val&: Record.VTableIndex);
707}
708
709template <> void MemberRecordImpl<ListContinuationRecord>::map(IO &IO) {
710 IO.mapRequired(Key: "ContinuationIndex", Val&: Record.ContinuationIndex);
711}
712
713} // end namespace detail
714} // end namespace CodeViewYAML
715} // end namespace llvm
716
717template <typename T>
718static inline Expected<LeafRecord> fromCodeViewRecordImpl(CVType Type) {
719 LeafRecord Result;
720
721 auto Impl = std::make_shared<T>(Type.kind());
722 if (auto EC = Impl->fromCodeViewRecord(Type))
723 return std::move(EC);
724 Result.Leaf = std::move(Impl);
725 return Result;
726}
727
728Expected<LeafRecord> LeafRecord::fromCodeViewRecord(CVType Type) {
729#define TYPE_RECORD(EnumName, EnumVal, ClassName) \
730 case EnumName: \
731 return fromCodeViewRecordImpl<LeafRecordImpl<ClassName##Record>>(Type);
732#define TYPE_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName) \
733 TYPE_RECORD(EnumName, EnumVal, ClassName)
734#define MEMBER_RECORD(EnumName, EnumVal, ClassName)
735#define MEMBER_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName)
736 switch (Type.kind()) {
737#include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
738 default:
739 return fromCodeViewRecordImpl<UnknownLeafRecord>(Type);
740 }
741}
742
743CVType
744LeafRecord::toCodeViewRecord(AppendingTypeTableBuilder &Serializer) const {
745 return Leaf->toCodeViewRecord(TS&: Serializer);
746}
747
748namespace llvm {
749namespace yaml {
750
751template <> struct MappingTraits<LeafRecordBase> {
752 static void mapping(IO &io, LeafRecordBase &Record) { Record.map(io); }
753};
754
755template <> struct MappingTraits<MemberRecordBase> {
756 static void mapping(IO &io, MemberRecordBase &Record) { Record.map(io); }
757};
758
759} // end namespace yaml
760} // end namespace llvm
761
762template <typename ConcreteType>
763static void mapLeafRecordImpl(IO &IO, const char *Class, TypeLeafKind Kind,
764 LeafRecord &Obj) {
765 if (!IO.outputting())
766 Obj.Leaf = std::make_shared<ConcreteType>(Kind);
767
768 if (Kind == LF_FIELDLIST)
769 Obj.Leaf->map(io&: IO);
770 else
771 IO.mapRequired(Key: Class, Val&: *Obj.Leaf);
772}
773
774void MappingTraits<LeafRecord>::mapping(IO &IO, LeafRecord &Obj) {
775 TypeLeafKind Kind;
776 if (IO.outputting())
777 Kind = Obj.Leaf->Kind;
778 IO.mapRequired(Key: "Kind", Val&: Kind);
779
780#define TYPE_RECORD(EnumName, EnumVal, ClassName) \
781 case EnumName: \
782 mapLeafRecordImpl<LeafRecordImpl<ClassName##Record>>(IO, #ClassName, Kind, \
783 Obj); \
784 break;
785#define TYPE_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName) \
786 TYPE_RECORD(EnumName, EnumVal, ClassName)
787#define MEMBER_RECORD(EnumName, EnumVal, ClassName)
788#define MEMBER_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName)
789 switch (Kind) {
790#include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
791 default:
792 mapLeafRecordImpl<UnknownLeafRecord>(IO, Class: "UnknownLeaf", Kind, Obj);
793 }
794}
795
796template <typename ConcreteType>
797static void mapMemberRecordImpl(IO &IO, const char *Class, TypeLeafKind Kind,
798 MemberRecord &Obj) {
799 if (!IO.outputting())
800 Obj.Member = std::make_shared<MemberRecordImpl<ConcreteType>>(Kind);
801
802 IO.mapRequired(Key: Class, Val&: *Obj.Member);
803}
804
805void MappingTraits<MemberRecord>::mapping(IO &IO, MemberRecord &Obj) {
806 TypeLeafKind Kind;
807 if (IO.outputting())
808 Kind = Obj.Member->Kind;
809 IO.mapRequired(Key: "Kind", Val&: Kind);
810
811#define MEMBER_RECORD(EnumName, EnumVal, ClassName) \
812 case EnumName: \
813 mapMemberRecordImpl<ClassName##Record>(IO, #ClassName, Kind, Obj); \
814 break;
815#define MEMBER_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName) \
816 MEMBER_RECORD(EnumName, EnumVal, ClassName)
817#define TYPE_RECORD(EnumName, EnumVal, ClassName)
818#define TYPE_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName)
819 switch (Kind) {
820#include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
821 default: { llvm_unreachable("Unknown member kind!"); }
822 }
823}
824
825std::vector<LeafRecord>
826llvm::CodeViewYAML::fromDebugT(ArrayRef<uint8_t> DebugTorP,
827 StringRef SectionName) {
828 ExitOnError Err("Invalid " + std::string(SectionName) + " section!");
829 BinaryStreamReader Reader(DebugTorP, llvm::endianness::little);
830 CVTypeArray Types;
831 uint32_t Magic;
832
833 Err(Reader.readInteger(Dest&: Magic));
834 assert(Magic == COFF::DEBUG_SECTION_MAGIC &&
835 "Invalid .debug$T or .debug$P section!");
836
837 std::vector<LeafRecord> Result;
838 Err(Reader.readArray(Array&: Types, Size: Reader.bytesRemaining()));
839 for (const auto &T : Types) {
840 auto CVT = Err(LeafRecord::fromCodeViewRecord(Type: T));
841 Result.push_back(x: CVT);
842 }
843 return Result;
844}
845
846ArrayRef<uint8_t> llvm::CodeViewYAML::toDebugT(ArrayRef<LeafRecord> Leafs,
847 BumpPtrAllocator &Alloc,
848 StringRef SectionName) {
849 AppendingTypeTableBuilder TS(Alloc);
850 uint32_t Size = sizeof(uint32_t);
851 for (const auto &Leaf : Leafs) {
852 CVType T = Leaf.Leaf->toCodeViewRecord(TS);
853 Size += T.length();
854 assert(T.length() % 4 == 0 && "Improper type record alignment!");
855 }
856 uint8_t *ResultBuffer = Alloc.Allocate<uint8_t>(Num: Size);
857 MutableArrayRef<uint8_t> Output(ResultBuffer, Size);
858 BinaryStreamWriter Writer(Output, llvm::endianness::little);
859 ExitOnError Err("Error writing type record to " + std::string(SectionName) +
860 " section");
861 Err(Writer.writeInteger<uint32_t>(Value: COFF::DEBUG_SECTION_MAGIC));
862 for (const auto &R : TS.records()) {
863 Err(Writer.writeBytes(Buffer: R));
864 }
865 assert(Writer.bytesRemaining() == 0 && "Didn't write all type record bytes!");
866 return Output;
867}
868