aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm16/include/llvm/CodeGen/DwarfStringPoolEntry.h
blob: e33f6027e428e3ef9f40c305d49755e469ff1bcd (plain) (blame)
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
#pragma once

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

//===- llvm/CodeGen/DwarfStringPoolEntry.h - String pool entry --*- 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CODEGEN_DWARFSTRINGPOOLENTRY_H
#define LLVM_CODEGEN_DWARFSTRINGPOOLENTRY_H

#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/StringMap.h"

namespace llvm {

class MCSymbol;

/// Data for a string pool entry.
struct DwarfStringPoolEntry {
  static constexpr unsigned NotIndexed = -1;

  MCSymbol *Symbol = nullptr;
  uint64_t Offset = 0;
  unsigned Index = 0;

  bool isIndexed() const { return Index != NotIndexed; }
};

/// DwarfStringPoolEntryRef: Dwarf string pool entry reference.
///
/// Dwarf string pool entry keeps string value and its data.
/// There are two variants how data are represented:
///
///   1. By value - StringMapEntry<DwarfStringPoolEntry>.
///   2. By pointer - StringMapEntry<DwarfStringPoolEntry *>.
///
/// The "By pointer" variant allows for reducing memory usage for the case
/// when string pool entry does not have data: it keeps the null pointer
/// and so no need to waste space for the full DwarfStringPoolEntry.
/// It is recommended to use "By pointer" variant if not all entries
/// of dwarf string pool have corresponding DwarfStringPoolEntry.

class DwarfStringPoolEntryRef {
  /// Pointer type for "By value" string entry.
  using ByValStringEntryPtr = const StringMapEntry<DwarfStringPoolEntry> *;

  /// Pointer type for "By pointer" string entry.
  using ByPtrStringEntryPtr = const StringMapEntry<DwarfStringPoolEntry *> *;

  /// Pointer to the dwarf string pool Entry.
  PointerUnion<ByValStringEntryPtr, ByPtrStringEntryPtr> MapEntry = nullptr;

public:
  DwarfStringPoolEntryRef() = default;

  /// ASSUMPTION: DwarfStringPoolEntryRef keeps pointer to \p Entry,
  /// thus specified entry mustn`t be reallocated.
  DwarfStringPoolEntryRef(const StringMapEntry<DwarfStringPoolEntry> &Entry)
      : MapEntry(&Entry) {}

  /// ASSUMPTION: DwarfStringPoolEntryRef keeps pointer to \p Entry,
  /// thus specified entry mustn`t be reallocated.
  DwarfStringPoolEntryRef(const StringMapEntry<DwarfStringPoolEntry *> &Entry)
      : MapEntry(&Entry) {
    assert(MapEntry.get<ByPtrStringEntryPtr>()->second != nullptr);
  }

  explicit operator bool() const { return !MapEntry.isNull(); }

  /// \returns symbol for the dwarf string.
  MCSymbol *getSymbol() const {
    assert(getEntry().Symbol && "No symbol available!");
    return getEntry().Symbol;
  }

  /// \returns offset for the dwarf string.
  uint64_t getOffset() const { return getEntry().Offset; }

  /// \returns index for the dwarf string.
  unsigned getIndex() const {
    assert(getEntry().isIndexed() && "Index is not set!");
    return getEntry().Index;
  }

  /// \returns string.
  StringRef getString() const {
    if (MapEntry.is<ByValStringEntryPtr>())
      return MapEntry.get<ByValStringEntryPtr>()->first();

    return MapEntry.get<ByPtrStringEntryPtr>()->first();
  }

  /// \returns the entire string pool entry for convenience.
  const DwarfStringPoolEntry &getEntry() const {
    if (MapEntry.is<ByValStringEntryPtr>())
      return MapEntry.get<ByValStringEntryPtr>()->second;

    return *MapEntry.get<ByPtrStringEntryPtr>()->second;
  }

  bool operator==(const DwarfStringPoolEntryRef &X) const {
    return MapEntry.getOpaqueValue() == X.MapEntry.getOpaqueValue();
  }

  bool operator!=(const DwarfStringPoolEntryRef &X) const {
    return MapEntry.getOpaqueValue() != X.MapEntry.getOpaqueValue();
  }
};

} // end namespace llvm

#endif

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif