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