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