aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/MIR/EmbeddedIR.h
blob: 50567deacf24fe758a50c0958982e2f2bafda045 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// 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;
};

struct EmbeddedIRMetadataDefinition final {
  std::string Name;
  TextRange Range;
  std::string DynamicType;
};

struct EmbeddedIRTypeDefinition final {
  std::string Name;
  TextRange Range;
  std::string Definition;
  TextRange DefinitionRange;
  bool NumericName = false;
};

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]] std::span<const EmbeddedIRReference> values() const noexcept {
    return Values;
  }

  [[nodiscard]] const EmbeddedIRReference *block(std::string_view QueryName) const noexcept;
  [[nodiscard]] const EmbeddedIRReference *alloca(std::string_view QueryName) const noexcept;
  [[nodiscard]] const EmbeddedIRReference *value(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::vector<EmbeddedIRReference> Values;
  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;
  std::unordered_map<std::string, std::size_t, StringHash, std::equal_to<>> ValueNameToIndex;

  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 EmbeddedIRMetadataDefinition>
  metadataDefinitions() const noexcept {
    return MetadataDefinitions;
  }

  [[nodiscard]] std::span<const EmbeddedIRTypeDefinition> typeDefinitions() const noexcept {
    return TypeDefinitions;
  }

  [[nodiscard]] const EmbeddedIRFunction *function(std::string_view Name) const noexcept;
  [[nodiscard]] const EmbeddedIRReference *globalObject(std::string_view Name) const noexcept;
  [[nodiscard]] const EmbeddedIRMetadataDefinition *metadata(std::string_view Name) const noexcept;
  [[nodiscard]] const EmbeddedIRTypeDefinition *typeDefinition(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<EmbeddedIRMetadataDefinition> MetadataDefinitions;
  std::vector<EmbeddedIRTypeDefinition> TypeDefinitions;
  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;
  std::unordered_map<std::string, std::size_t, StringHash, std::equal_to<>> TypeNameToIndex;

  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;
  }

  [[nodiscard]] std::optional<EmbeddedIRReferenceIndex> takeIndex() && {
    return std::move(Index);
  }

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