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