aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/MIR/EmbeddedIR.h
blob: 8e9e0f6b6f2367b568a22584cd5c50375640167b (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_MIR_EMBEDDEDIR_H
#define RCC_MIR_EMBEDDEDIR_H

#include "rcc/Support/TextDiagnostic.h"

#include <cstddef>
#include <functional>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>

namespace rcc::mir {

class EmbeddedIRIndexer;

struct EmbeddedIRReference final {
  std::string Name;
  TextRange Range;
};

class EmbeddedIRFunction final {
public:
  [[nodiscard]] std::string_view name() const noexcept {
    return Name;
  }

  [[nodiscard]] const TextRange &nameRange() const noexcept {
    return NameRange;
  }

  [[nodiscard]] bool isDefinition() const noexcept {
    return IsDefinition;
  }

  [[nodiscard]] std::span<const EmbeddedIRReference> blocks() const noexcept {
    return Blocks;
  }

  [[nodiscard]] std::span<const EmbeddedIRReference> allocas() const noexcept {
    return Allocas;
  }

  [[nodiscard]] const EmbeddedIRReference *block(std::string_view QueryName) const noexcept;
  [[nodiscard]] const EmbeddedIRReference *alloca(std::string_view QueryName) const noexcept;

private:
  struct StringHash final {
    using is_transparent = void; // NOLINT(readability-identifier-naming)

    [[nodiscard]] std::size_t operator()(std::string_view Value) const noexcept {
      return std::hash<std::string_view>{}(Value);
    }
  };

  std::string Name;
  TextRange NameRange;
  bool IsDefinition = false;
  std::vector<EmbeddedIRReference> Blocks;
  std::vector<EmbeddedIRReference> Allocas;
  std::unordered_map<std::string, std::size_t, StringHash, std::equal_to<>> BlockNameToIndex;
  std::unordered_map<std::string, std::size_t, StringHash, std::equal_to<>> AllocaNameToIndex;

  friend class EmbeddedIRIndexer;
};

class EmbeddedIRReferenceIndex final {
public:
  EmbeddedIRReferenceIndex(const EmbeddedIRReferenceIndex &) = delete;
  EmbeddedIRReferenceIndex &operator=(const EmbeddedIRReferenceIndex &) = delete;
  EmbeddedIRReferenceIndex(EmbeddedIRReferenceIndex &&) noexcept = default;
  EmbeddedIRReferenceIndex &operator=(EmbeddedIRReferenceIndex &&) noexcept = default;
  ~EmbeddedIRReferenceIndex() = default;

  [[nodiscard]] const TextSource &source() const noexcept {
    return Source;
  }

  [[nodiscard]] std::optional<std::string_view> targetTriple() const noexcept {
    if (!TargetTriple)
      return std::nullopt;
    return *TargetTriple;
  }

  [[nodiscard]] std::optional<std::string_view> dataLayout() const noexcept {
    if (!DataLayout)
      return std::nullopt;
    return *DataLayout;
  }

  [[nodiscard]] std::span<const EmbeddedIRFunction> functions() const noexcept {
    return Functions;
  }

  [[nodiscard]] std::span<const EmbeddedIRReference> globalObjects() const noexcept {
    return GlobalObjects;
  }

  [[nodiscard]] std::span<const EmbeddedIRReference> metadataDefinitions() const noexcept {
    return MetadataDefinitions;
  }

  [[nodiscard]] const EmbeddedIRFunction *function(std::string_view Name) const noexcept;
  [[nodiscard]] const EmbeddedIRReference *globalObject(std::string_view Name) const noexcept;
  [[nodiscard]] const EmbeddedIRReference *metadata(std::string_view Name) const noexcept;
  [[nodiscard]] bool hasGlobalValue(std::string_view Name) const noexcept {
    return function(Name) != nullptr || globalObject(Name) != nullptr;
  }

private:
  struct StringHash final {
    using is_transparent = void; // NOLINT(readability-identifier-naming)

    [[nodiscard]] std::size_t operator()(std::string_view Value) const noexcept {
      return std::hash<std::string_view>{}(Value);
    }
  };

  EmbeddedIRReferenceIndex(std::string SourceName, std::string SourceText)
      : Source(std::move(SourceName), std::move(SourceText)) {}

  TextSource Source;
  std::optional<std::string> TargetTriple;
  std::optional<TextRange> TargetTripleRange;
  std::optional<std::string> DataLayout;
  std::optional<TextRange> DataLayoutRange;
  std::vector<EmbeddedIRFunction> Functions;
  std::vector<EmbeddedIRReference> GlobalObjects;
  std::vector<EmbeddedIRReference> MetadataDefinitions;
  std::unordered_map<std::string, std::size_t, StringHash, std::equal_to<>> FunctionNameToIndex;
  std::unordered_map<std::string, std::size_t, StringHash, std::equal_to<>> GlobalNameToIndex;
  std::unordered_map<std::string, std::size_t, StringHash, std::equal_to<>> MetadataNameToIndex;

  friend class EmbeddedIRIndexer;
};

class EmbeddedIRIndexResult final {
public:
  EmbeddedIRIndexResult(const EmbeddedIRIndexResult &) = delete;
  EmbeddedIRIndexResult &operator=(const EmbeddedIRIndexResult &) = delete;
  EmbeddedIRIndexResult(EmbeddedIRIndexResult &&) noexcept = default;
  EmbeddedIRIndexResult &operator=(EmbeddedIRIndexResult &&) noexcept = default;
  ~EmbeddedIRIndexResult() = default;

  [[nodiscard]] bool succeeded() const noexcept {
    return Index.has_value() && Diagnostics.empty();
  }

  [[nodiscard]] const EmbeddedIRReferenceIndex *index() const noexcept {
    return Index.has_value() ? Index.operator->() : nullptr;
  }

  [[nodiscard]] std::span<const TextDiagnostic> diagnostics() const noexcept {
    return Diagnostics;
  }

private:
  EmbeddedIRIndexResult() = default;

  std::optional<EmbeddedIRReferenceIndex> Index;
  std::vector<TextDiagnostic> Diagnostics;

  friend class EmbeddedIRIndexer;
};

[[nodiscard]] EmbeddedIRIndexResult indexEmbeddedIR(std::string SourceName, std::string SourceText);

} // namespace rcc::mir

#endif // RCC_MIR_EMBEDDEDIR_H