]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/ObjectYAML/CodeViewYAMLTypes.cpp
Vendor import of llvm trunk r304460:
[FreeBSD/FreeBSD.git] / lib / ObjectYAML / CodeViewYAMLTypes.cpp
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
24 using namespace llvm;
25 using namespace llvm::codeview;
26 using namespace llvm::CodeViewYAML;
27 using namespace llvm::CodeViewYAML::detail;
28 using namespace llvm::yaml;
29
30 LLVM_YAML_IS_SEQUENCE_VECTOR(OneMethodRecord)
31 LLVM_YAML_IS_SEQUENCE_VECTOR(StringRef)
32 LLVM_YAML_IS_SEQUENCE_VECTOR(VFTableSlotKind)
33 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(TypeIndex)
34
35 LLVM_YAML_DECLARE_SCALAR_TRAITS(TypeIndex, false)
36 LLVM_YAML_DECLARE_SCALAR_TRAITS(APSInt, false)
37
38 LLVM_YAML_DECLARE_ENUM_TRAITS(TypeLeafKind)
39 LLVM_YAML_DECLARE_ENUM_TRAITS(PointerToMemberRepresentation)
40 LLVM_YAML_DECLARE_ENUM_TRAITS(VFTableSlotKind)
41 LLVM_YAML_DECLARE_ENUM_TRAITS(CallingConvention)
42 LLVM_YAML_DECLARE_ENUM_TRAITS(PointerKind)
43 LLVM_YAML_DECLARE_ENUM_TRAITS(PointerMode)
44 LLVM_YAML_DECLARE_ENUM_TRAITS(HfaKind)
45 LLVM_YAML_DECLARE_ENUM_TRAITS(MemberAccess)
46 LLVM_YAML_DECLARE_ENUM_TRAITS(MethodKind)
47 LLVM_YAML_DECLARE_ENUM_TRAITS(WindowsRTClassKind)
48 LLVM_YAML_DECLARE_ENUM_TRAITS(LabelType)
49
50 LLVM_YAML_DECLARE_BITSET_TRAITS(PointerOptions)
51 LLVM_YAML_DECLARE_BITSET_TRAITS(ModifierOptions)
52 LLVM_YAML_DECLARE_BITSET_TRAITS(FunctionOptions)
53 LLVM_YAML_DECLARE_BITSET_TRAITS(ClassOptions)
54 LLVM_YAML_DECLARE_BITSET_TRAITS(MethodOptions)
55
56 LLVM_YAML_DECLARE_MAPPING_TRAITS(OneMethodRecord)
57 LLVM_YAML_DECLARE_MAPPING_TRAITS(MemberPointerInfo)
58
59 namespace llvm {
60 namespace CodeViewYAML {
61 namespace detail {
62
63 struct LeafRecordBase {
64   TypeLeafKind Kind;
65   explicit LeafRecordBase(TypeLeafKind K) : Kind(K) {}
66
67   virtual ~LeafRecordBase() {}
68   virtual void map(yaml::IO &io) = 0;
69   virtual CVType toCodeViewRecord(BumpPtrAllocator &Allocator) const = 0;
70   virtual Error fromCodeViewRecord(CVType Type) = 0;
71 };
72
73 template <typename T> struct LeafRecordImpl : public LeafRecordBase {
74   explicit LeafRecordImpl(TypeLeafKind K)
75       : LeafRecordBase(K), Record(static_cast<TypeRecordKind>(K)) {}
76
77   void map(yaml::IO &io) override;
78
79   Error fromCodeViewRecord(CVType Type) override {
80     return TypeDeserializer::deserializeAs<T>(Type, Record);
81   }
82
83   CVType toCodeViewRecord(BumpPtrAllocator &Allocator) const override {
84     TypeTableBuilder Table(Allocator);
85     Table.writeKnownType(Record);
86     return CVType(Kind, Table.records().front());
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(BumpPtrAllocator &Allocator) 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 LeafRecordImpl<FieldListRecord>::toCodeViewRecord(
444     BumpPtrAllocator &Allocator) const {
445   TypeTableBuilder TTB(Allocator);
446   FieldListRecordBuilder FLRB(TTB);
447   FLRB.begin();
448   for (const auto &Member : Members) {
449     Member.Member->writeTo(FLRB);
450   }
451   FLRB.end(true);
452   return CVType(Kind, TTB.records().front());
453 }
454
455 void MappingTraits<OneMethodRecord>::mapping(IO &io, OneMethodRecord &Record) {
456   io.mapRequired("Type", Record.Type);
457   io.mapRequired("Attrs", Record.Attrs.Attrs);
458   io.mapRequired("VFTableOffset", Record.VFTableOffset);
459   io.mapRequired("Name", Record.Name);
460 }
461
462 namespace llvm {
463 namespace CodeViewYAML {
464 namespace detail {
465 template <> void LeafRecordImpl<ClassRecord>::map(IO &IO) {
466   IO.mapRequired("MemberCount", Record.MemberCount);
467   IO.mapRequired("Options", Record.Options);
468   IO.mapRequired("FieldList", Record.FieldList);
469   IO.mapRequired("Name", Record.Name);
470   IO.mapRequired("UniqueName", Record.UniqueName);
471
472   IO.mapRequired("DerivationList", Record.DerivationList);
473   IO.mapRequired("VTableShape", Record.VTableShape);
474   IO.mapRequired("Size", Record.Size);
475 }
476
477 template <> void LeafRecordImpl<UnionRecord>::map(IO &IO) {
478   IO.mapRequired("MemberCount", Record.MemberCount);
479   IO.mapRequired("Options", Record.Options);
480   IO.mapRequired("FieldList", Record.FieldList);
481   IO.mapRequired("Name", Record.Name);
482   IO.mapRequired("UniqueName", Record.UniqueName);
483
484   IO.mapRequired("Size", Record.Size);
485 }
486
487 template <> void LeafRecordImpl<EnumRecord>::map(IO &IO) {
488   IO.mapRequired("NumEnumerators", Record.MemberCount);
489   IO.mapRequired("Options", Record.Options);
490   IO.mapRequired("FieldList", Record.FieldList);
491   IO.mapRequired("Name", Record.Name);
492   IO.mapRequired("UniqueName", Record.UniqueName);
493
494   IO.mapRequired("UnderlyingType", Record.UnderlyingType);
495 }
496
497 template <> void LeafRecordImpl<BitFieldRecord>::map(IO &IO) {
498   IO.mapRequired("Type", Record.Type);
499   IO.mapRequired("BitSize", Record.BitSize);
500   IO.mapRequired("BitOffset", Record.BitOffset);
501 }
502
503 template <> void LeafRecordImpl<VFTableShapeRecord>::map(IO &IO) {
504   IO.mapRequired("Slots", Record.Slots);
505 }
506
507 template <> void LeafRecordImpl<TypeServer2Record>::map(IO &IO) {
508   IO.mapRequired("Guid", Record.Guid);
509   IO.mapRequired("Age", Record.Age);
510   IO.mapRequired("Name", Record.Name);
511 }
512
513 template <> void LeafRecordImpl<StringIdRecord>::map(IO &IO) {
514   IO.mapRequired("Id", Record.Id);
515   IO.mapRequired("String", Record.String);
516 }
517
518 template <> void LeafRecordImpl<FuncIdRecord>::map(IO &IO) {
519   IO.mapRequired("ParentScope", Record.ParentScope);
520   IO.mapRequired("FunctionType", Record.FunctionType);
521   IO.mapRequired("Name", Record.Name);
522 }
523
524 template <> void LeafRecordImpl<UdtSourceLineRecord>::map(IO &IO) {
525   IO.mapRequired("UDT", Record.UDT);
526   IO.mapRequired("SourceFile", Record.SourceFile);
527   IO.mapRequired("LineNumber", Record.LineNumber);
528 }
529
530 template <> void LeafRecordImpl<UdtModSourceLineRecord>::map(IO &IO) {
531   IO.mapRequired("UDT", Record.UDT);
532   IO.mapRequired("SourceFile", Record.SourceFile);
533   IO.mapRequired("LineNumber", Record.LineNumber);
534   IO.mapRequired("Module", Record.Module);
535 }
536
537 template <> void LeafRecordImpl<BuildInfoRecord>::map(IO &IO) {
538   IO.mapRequired("ArgIndices", Record.ArgIndices);
539 }
540
541 template <> void LeafRecordImpl<VFTableRecord>::map(IO &IO) {
542   IO.mapRequired("CompleteClass", Record.CompleteClass);
543   IO.mapRequired("OverriddenVFTable", Record.OverriddenVFTable);
544   IO.mapRequired("VFPtrOffset", Record.VFPtrOffset);
545   IO.mapRequired("MethodNames", Record.MethodNames);
546 }
547
548 template <> void LeafRecordImpl<MethodOverloadListRecord>::map(IO &IO) {
549   IO.mapRequired("Methods", Record.Methods);
550 }
551
552 template <> void MemberRecordImpl<OneMethodRecord>::map(IO &IO) {
553   MappingTraits<OneMethodRecord>::mapping(IO, Record);
554 }
555
556 template <> void MemberRecordImpl<OverloadedMethodRecord>::map(IO &IO) {
557   IO.mapRequired("NumOverloads", Record.NumOverloads);
558   IO.mapRequired("MethodList", Record.MethodList);
559   IO.mapRequired("Name", Record.Name);
560 }
561
562 template <> void MemberRecordImpl<NestedTypeRecord>::map(IO &IO) {
563   IO.mapRequired("Type", Record.Type);
564   IO.mapRequired("Name", Record.Name);
565 }
566
567 template <> void MemberRecordImpl<DataMemberRecord>::map(IO &IO) {
568   IO.mapRequired("Attrs", Record.Attrs.Attrs);
569   IO.mapRequired("Type", Record.Type);
570   IO.mapRequired("FieldOffset", Record.FieldOffset);
571   IO.mapRequired("Name", Record.Name);
572 }
573
574 template <> void MemberRecordImpl<StaticDataMemberRecord>::map(IO &IO) {
575   IO.mapRequired("Attrs", Record.Attrs.Attrs);
576   IO.mapRequired("Type", Record.Type);
577   IO.mapRequired("Name", Record.Name);
578 }
579
580 template <> void MemberRecordImpl<EnumeratorRecord>::map(IO &IO) {
581   IO.mapRequired("Attrs", Record.Attrs.Attrs);
582   IO.mapRequired("Value", Record.Value);
583   IO.mapRequired("Name", Record.Name);
584 }
585
586 template <> void MemberRecordImpl<VFPtrRecord>::map(IO &IO) {
587   IO.mapRequired("Type", Record.Type);
588 }
589
590 template <> void MemberRecordImpl<BaseClassRecord>::map(IO &IO) {
591   IO.mapRequired("Attrs", Record.Attrs.Attrs);
592   IO.mapRequired("Type", Record.Type);
593   IO.mapRequired("Offset", Record.Offset);
594 }
595
596 template <> void MemberRecordImpl<VirtualBaseClassRecord>::map(IO &IO) {
597   IO.mapRequired("Attrs", Record.Attrs.Attrs);
598   IO.mapRequired("BaseType", Record.BaseType);
599   IO.mapRequired("VBPtrType", Record.VBPtrType);
600   IO.mapRequired("VBPtrOffset", Record.VBPtrOffset);
601   IO.mapRequired("VTableIndex", Record.VTableIndex);
602 }
603
604 template <> void MemberRecordImpl<ListContinuationRecord>::map(IO &IO) {
605   IO.mapRequired("ContinuationIndex", Record.ContinuationIndex);
606 }
607 }
608 }
609 }
610
611 template <typename T>
612 static inline Expected<LeafRecord> fromCodeViewRecordImpl(CVType Type) {
613   LeafRecord Result;
614
615   auto Impl = std::make_shared<LeafRecordImpl<T>>(Type.kind());
616   if (auto EC = Impl->fromCodeViewRecord(Type))
617     return std::move(EC);
618   Result.Leaf = Impl;
619   return Result;
620 }
621
622 Expected<LeafRecord> LeafRecord::fromCodeViewRecord(CVType Type) {
623 #define TYPE_RECORD(EnumName, EnumVal, ClassName)                              \
624   case EnumName:                                                               \
625     return fromCodeViewRecordImpl<ClassName##Record>(Type);
626 #define TYPE_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName)             \
627   TYPE_RECORD(EnumName, EnumVal, ClassName)
628 #define MEMBER_RECORD(EnumName, EnumVal, ClassName)
629 #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName)
630   switch (Type.kind()) {
631 #include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
632   default: { llvm_unreachable("Unknown leaf kind!"); }
633   }
634   return make_error<CodeViewError>(cv_error_code::corrupt_record);
635 }
636
637 CVType LeafRecord::toCodeViewRecord(BumpPtrAllocator &Allocator) const {
638   return Leaf->toCodeViewRecord(Allocator);
639 }
640
641 namespace llvm {
642 namespace yaml {
643 template <> struct MappingTraits<LeafRecordBase> {
644   static void mapping(IO &io, LeafRecordBase &Record) { Record.map(io); }
645 };
646
647 template <> struct MappingTraits<MemberRecordBase> {
648   static void mapping(IO &io, MemberRecordBase &Record) { Record.map(io); }
649 };
650 }
651 }
652
653 template <typename ConcreteType>
654 static void mapLeafRecordImpl(IO &IO, const char *Class, TypeLeafKind Kind,
655                               LeafRecord &Obj) {
656   if (!IO.outputting())
657     Obj.Leaf = std::make_shared<LeafRecordImpl<ConcreteType>>(Kind);
658
659   if (Kind == LF_FIELDLIST)
660     Obj.Leaf->map(IO);
661   else
662     IO.mapRequired(Class, *Obj.Leaf);
663 }
664
665 void MappingTraits<LeafRecord>::mapping(IO &IO, LeafRecord &Obj) {
666   TypeLeafKind Kind;
667   if (IO.outputting())
668     Kind = Obj.Leaf->Kind;
669   IO.mapRequired("Kind", Kind);
670
671 #define TYPE_RECORD(EnumName, EnumVal, ClassName)                              \
672   case EnumName:                                                               \
673     mapLeafRecordImpl<ClassName##Record>(IO, #ClassName, Kind, Obj);           \
674     break;
675 #define TYPE_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName)             \
676   TYPE_RECORD(EnumName, EnumVal, ClassName)
677 #define MEMBER_RECORD(EnumName, EnumVal, ClassName)
678 #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName)
679   switch (Kind) {
680 #include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
681   default: { llvm_unreachable("Unknown leaf kind!"); }
682   }
683 }
684
685 template <typename ConcreteType>
686 static void mapMemberRecordImpl(IO &IO, const char *Class, TypeLeafKind Kind,
687                                 MemberRecord &Obj) {
688   if (!IO.outputting())
689     Obj.Member = std::make_shared<MemberRecordImpl<ConcreteType>>(Kind);
690
691   IO.mapRequired(Class, *Obj.Member);
692 }
693
694 void MappingTraits<MemberRecord>::mapping(IO &IO, MemberRecord &Obj) {
695   TypeLeafKind Kind;
696   if (IO.outputting())
697     Kind = Obj.Member->Kind;
698   IO.mapRequired("Kind", Kind);
699
700 #define MEMBER_RECORD(EnumName, EnumVal, ClassName)                            \
701   case EnumName:                                                               \
702     mapMemberRecordImpl<ClassName##Record>(IO, #ClassName, Kind, Obj);         \
703     break;
704 #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName)           \
705   MEMBER_RECORD(EnumName, EnumVal, ClassName)
706 #define TYPE_RECORD(EnumName, EnumVal, ClassName)
707 #define TYPE_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName)
708   switch (Kind) {
709 #include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
710   default: { llvm_unreachable("Unknown member kind!"); }
711   }
712 }