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