reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
//===-- PdbSymUid.cpp -------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "PdbSymUid.h"

using namespace lldb_private;
using namespace lldb_private::npdb;
using namespace llvm::codeview;

namespace {
struct GenericIdRepr {
  uint64_t tag : 4;
  uint64_t data : 60;
};

struct CompilandIdRepr {
  uint64_t tag : 4;
  uint64_t modi : 16;
  uint64_t unused : 44;
};

struct CompilandSymIdRepr {
  uint64_t tag : 4;
  uint64_t modi : 16;
  uint64_t offset : 32;
  uint64_t unused : 12;
};

struct GlobalSymIdRepr {
  uint64_t tag : 4;
  uint64_t offset : 32;
  uint64_t pub : 1;
  uint64_t unused : 27;
};

struct TypeSymIdRepr {
  uint64_t tag : 4;
  uint64_t index : 32;
  uint64_t ipi : 1;
  uint64_t unused : 27;
};

struct FieldListMemberIdRepr {
  uint64_t tag : 4;
  uint64_t index : 32;
  uint64_t offset : 16;
  uint64_t unused : 12;
};

static_assert(sizeof(CompilandIdRepr) == 8, "Invalid structure size!");
static_assert(sizeof(CompilandSymIdRepr) == 8, "Invalid structure size!");
static_assert(sizeof(GlobalSymIdRepr) == 8, "Invalid structure size!");
static_assert(sizeof(TypeSymIdRepr) == 8, "Invalid structure size!");
static_assert(sizeof(FieldListMemberIdRepr) == 8, "Invalid structure size!");
} // namespace

template <typename OutT, typename InT> static OutT repr_cast(const InT &value) {
  OutT result;
  ::memcpy(&result, &value, sizeof(value));
  return result;
}

PdbSymUid::PdbSymUid(const PdbCompilandId &cid) {
  CompilandIdRepr repr;
  ::memset(&repr, 0, sizeof(repr));
  repr.modi = cid.modi;
  repr.tag = static_cast<uint64_t>(PdbSymUidKind::Compiland);
  m_repr = repr_cast<uint64_t>(repr);
}

PdbSymUid::PdbSymUid(const PdbCompilandSymId &csid) {
  CompilandSymIdRepr repr;
  ::memset(&repr, 0, sizeof(repr));
  repr.modi = csid.modi;
  repr.offset = csid.offset;
  repr.tag = static_cast<uint64_t>(PdbSymUidKind::CompilandSym);
  m_repr = repr_cast<uint64_t>(repr);
}

PdbSymUid::PdbSymUid(const PdbGlobalSymId &gsid) {
  GlobalSymIdRepr repr;
  ::memset(&repr, 0, sizeof(repr));
  repr.pub = gsid.is_public;
  repr.offset = gsid.offset;
  repr.tag = static_cast<uint64_t>(PdbSymUidKind::GlobalSym);
  m_repr = repr_cast<uint64_t>(repr);
}

PdbSymUid::PdbSymUid(const PdbTypeSymId &tsid) {
  TypeSymIdRepr repr;
  ::memset(&repr, 0, sizeof(repr));
  repr.index = tsid.index.getIndex();
  repr.ipi = tsid.is_ipi;
  repr.tag = static_cast<uint64_t>(PdbSymUidKind::Type);
  m_repr = repr_cast<uint64_t>(repr);
}

PdbSymUid::PdbSymUid(const PdbFieldListMemberId &flmid) {
  FieldListMemberIdRepr repr;
  ::memset(&repr, 0, sizeof(repr));
  repr.index = flmid.index.getIndex();
  repr.offset = flmid.offset;
  repr.tag = static_cast<uint64_t>(PdbSymUidKind::FieldListMember);
  m_repr = repr_cast<uint64_t>(repr);
}

PdbSymUidKind PdbSymUid::kind() const {
  GenericIdRepr generic = repr_cast<GenericIdRepr>(m_repr);
  return static_cast<PdbSymUidKind>(generic.tag);
}

PdbCompilandId PdbSymUid::asCompiland() const {
  assert(kind() == PdbSymUidKind::Compiland);
  auto repr = repr_cast<CompilandIdRepr>(m_repr);
  PdbCompilandId result;
  result.modi = repr.modi;
  return result;
}

PdbCompilandSymId PdbSymUid::asCompilandSym() const {
  assert(kind() == PdbSymUidKind::CompilandSym);
  auto repr = repr_cast<CompilandSymIdRepr>(m_repr);
  PdbCompilandSymId result;
  result.modi = repr.modi;
  result.offset = repr.offset;
  return result;
}

PdbGlobalSymId PdbSymUid::asGlobalSym() const {
  assert(kind() == PdbSymUidKind::GlobalSym ||
         kind() == PdbSymUidKind::PublicSym);
  auto repr = repr_cast<GlobalSymIdRepr>(m_repr);
  PdbGlobalSymId result;
  result.is_public = repr.pub;
  result.offset = repr.offset;
  return result;
}

PdbTypeSymId PdbSymUid::asTypeSym() const {
  assert(kind() == PdbSymUidKind::Type);
  auto repr = repr_cast<TypeSymIdRepr>(m_repr);
  PdbTypeSymId result;
  result.index.setIndex(repr.index);
  result.is_ipi = repr.ipi;
  return result;
}

PdbFieldListMemberId PdbSymUid::asFieldListMember() const {
  assert(kind() == PdbSymUidKind::FieldListMember);
  auto repr = repr_cast<FieldListMemberIdRepr>(m_repr);
  PdbFieldListMemberId result;
  result.index.setIndex(repr.index);
  result.offset = repr.offset;
  return result;
}