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