LCOV - code coverage report
Current view: top level - lib/ObjectYAML - CodeViewYAMLTypes.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 352 457 77.0 %
Date: 2017-09-14 15:23:50 Functions: 189 254 74.4 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13