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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_EXPRESSION_EXPRESSION_HPP
#define INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_EXPRESSION_EXPRESSION_HPP
#include <exception>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <type_traits>
#include <variant>
#include <vector>
#include "fmt/core.h"
#include "gsl-lite/gsl-lite.hpp"
#include "src/buildtool/build_engine/base_maps/entity_name_data.hpp"
#include "src/buildtool/build_engine/expression/expression_ptr.hpp"
#include "src/buildtool/build_engine/expression/function_map.hpp"
#include "src/buildtool/build_engine/expression/linked_map.hpp"
#include "src/buildtool/build_engine/expression/target_node.hpp"
#include "src/buildtool/build_engine/expression/target_result.hpp"
#include "src/buildtool/common/artifact_description.hpp"
#include "src/buildtool/crypto/hash_function.hpp"
#include "src/buildtool/multithreading/atomic_value.hpp"
#include "src/utils/cpp/hex_string.hpp"
#include "src/utils/cpp/json.hpp"
class Expression {
friend auto operator+(Expression const& /*lhs*/, Expression const& /*rhs*/)
-> Expression;
public:
using none_t = std::monostate;
using number_t = double;
using artifact_t = ArtifactDescription;
using result_t = TargetResult;
using node_t = TargetNode;
using list_t = std::vector<ExpressionPtr>;
using map_t = LinkedMap<std::string, ExpressionPtr, ExpressionPtr>;
using name_t = BuildMaps::Base::EntityName;
template <class T, size_t kIndex = 0>
static consteval auto IsValidType() -> bool {
if constexpr (kIndex < std::variant_size_v<decltype(data_)>) {
return std::is_same_v<
T,
std::variant_alternative_t<kIndex, decltype(data_)>> or
IsValidType<T, kIndex + 1>();
}
return false;
}
class ExpressionTypeError : public std::exception {
public:
explicit ExpressionTypeError(std::string const& msg) noexcept
: msg_{"ExpressionTypeError: " + msg} {}
[[nodiscard]] auto what() const noexcept -> char const* final {
return msg_.c_str();
}
private:
std::string msg_;
};
Expression() noexcept = default;
~Expression() noexcept = default;
Expression(Expression const& other) noexcept = delete;
Expression(Expression&& other) noexcept = default;
auto operator=(Expression const& other) noexcept = delete;
auto operator=(Expression&& other) noexcept = delete;
template <class T>
requires(IsValidType<std::remove_cvref_t<T>>())
// NOLINTNEXTLINE(bugprone-forwarding-reference-overload)
explicit Expression(T&& data) noexcept
: data_{std::forward<T>(data)} {}
[[nodiscard]] auto IsNone() const noexcept -> bool { return IsA<none_t>(); }
[[nodiscard]] auto IsBool() const noexcept -> bool { return IsA<bool>(); }
[[nodiscard]] auto IsNumber() const noexcept -> bool {
return IsA<number_t>();
}
[[nodiscard]] auto IsString() const noexcept -> bool {
return IsA<std::string>();
}
[[nodiscard]] auto IsName() const noexcept -> bool { return IsA<name_t>(); }
[[nodiscard]] auto IsArtifact() const noexcept -> bool {
return IsA<artifact_t>();
}
[[nodiscard]] auto IsResult() const noexcept -> bool {
return IsA<result_t>();
}
[[nodiscard]] auto IsNode() const noexcept -> bool { return IsA<node_t>(); }
[[nodiscard]] auto IsList() const noexcept -> bool { return IsA<list_t>(); }
[[nodiscard]] auto IsMap() const noexcept -> bool { return IsA<map_t>(); }
[[nodiscard]] auto Bool() const -> bool { return Cast<bool>(); }
[[nodiscard]] auto Number() const -> number_t { return Cast<number_t>(); }
[[nodiscard]] auto Name() const -> name_t { return Cast<name_t>(); }
[[nodiscard]] auto String() const& -> std::string const& {
return Cast<std::string>();
}
[[nodiscard]] auto String() && -> std::string {
return std::move(*this).Cast<std::string>();
}
[[nodiscard]] auto Artifact() const& -> artifact_t const& {
return Cast<artifact_t>();
}
[[nodiscard]] auto Artifact() && -> artifact_t {
return std::move(*this).Cast<artifact_t>();
}
[[nodiscard]] auto Result() const& -> result_t const& {
return Cast<result_t>();
}
[[nodiscard]] auto Result() && -> result_t {
return std::move(*this).Cast<result_t>();
}
[[nodiscard]] auto Node() const& -> node_t const& { return Cast<node_t>(); }
[[nodiscard]] auto Node() && -> node_t {
return std::move(*this).Cast<node_t>();
}
[[nodiscard]] auto List() const& -> list_t const& { return Cast<list_t>(); }
[[nodiscard]] auto List() && -> list_t {
return std::move(*this).Cast<list_t>();
}
[[nodiscard]] auto Map() const& -> map_t const& { return Cast<map_t>(); }
[[nodiscard]] auto Map() && -> map_t {
return std::move(*this).Cast<map_t>();
}
[[nodiscard]] auto At(std::string const& key)
const& -> std::optional<std::reference_wrapper<ExpressionPtr const>> {
auto value = Map().Find(key);
if (value) {
return value;
}
return std::nullopt;
}
[[nodiscard]] auto At(
std::string const& key) && -> std::optional<ExpressionPtr> {
auto value = std::move(*this).Map().Find(key);
if (value) {
return std::move(*value);
}
return std::nullopt;
}
template <class T>
requires(IsValidType<std::remove_cvref_t<T>>() or
std::is_same_v<std::remove_cvref_t<T>, ExpressionPtr>)
[[nodiscard]] auto Get(std::string const& key, T&& default_value) const
-> ExpressionPtr {
auto value = At(key);
if (value) {
return value->get();
}
if constexpr (std::is_same_v<std::remove_cvref_t<T>, ExpressionPtr>) {
return std::forward<T>(default_value);
}
else {
return ExpressionPtr{std::forward<T>(default_value)};
}
}
template <class T>
requires(IsValidType<T>()) [[nodiscard]] auto Value() const& noexcept
-> std::optional<std::reference_wrapper<T const>> {
if (GetIndexOf<T>() == data_.index()) {
return std::make_optional(std::ref(std::get<T>(data_)));
}
return std::nullopt;
}
template <class T>
requires(IsValidType<T>()) [[nodiscard]] auto Value() && noexcept
-> std::optional<T> {
if (GetIndexOf<T>() == data_.index()) {
return std::make_optional(std::move(std::get<T>(data_)));
}
return std::nullopt;
}
template <class T>
[[nodiscard]] auto operator==(T const& other) const noexcept -> bool {
if constexpr (std::is_same_v<T, Expression>) {
return (&data_ == &other.data_) or (ToHash() == other.ToHash());
}
else {
return IsValidType<T>() and (GetIndexOf<T>() == data_.index()) and
((static_cast<void const*>(&data_) ==
static_cast<void const*>(&other)) or
(std::get<T>(data_) == other));
}
}
template <class T>
[[nodiscard]] auto operator!=(T const& other) const noexcept -> bool {
return !(*this == other);
}
[[nodiscard]] auto operator[](
std::string const& key) const& -> ExpressionPtr const&;
[[nodiscard]] auto operator[](std::string const& key) && -> ExpressionPtr;
[[nodiscard]] auto operator[](
ExpressionPtr const& key) const& -> ExpressionPtr const&;
[[nodiscard]] auto operator[](ExpressionPtr const& key) && -> ExpressionPtr;
[[nodiscard]] auto operator[](size_t pos) const& -> ExpressionPtr const&;
[[nodiscard]] auto operator[](size_t pos) && -> ExpressionPtr;
enum class JsonMode { SerializeAll, SerializeAllButNodes, NullForNonJson };
[[nodiscard]] auto ToJson(JsonMode mode = JsonMode::SerializeAll) const
-> nlohmann::json;
[[nodiscard]] auto IsCacheable() const -> bool;
[[nodiscard]] auto ToString() const -> std::string;
[[nodiscard]] auto ToAbbrevString(size_t len) const -> std::string;
[[nodiscard]] auto ToHash() const noexcept -> std::string;
[[nodiscard]] auto ToIdentifier() const noexcept -> std::string {
return ToHexString(ToHash());
}
[[nodiscard]] static auto FromJson(nlohmann::json const& json) noexcept
-> ExpressionPtr;
inline static ExpressionPtr const kNone = Expression::FromJson("null"_json);
inline static ExpressionPtr const kEmptyMap =
Expression::FromJson("{}"_json);
inline static ExpressionPtr const kEmptyList =
Expression::FromJson("[]"_json);
inline static ExpressionPtr const kEmptyMapExpr =
Expression::FromJson(R"({"type": "empty_map"})"_json);
private:
std::variant<none_t,
bool,
number_t,
std::string,
name_t,
artifact_t,
result_t,
node_t,
list_t,
map_t>
data_{none_t{}};
AtomicValue<std::string> hash_{};
AtomicValue<bool> is_cachable_{};
template <class T, std::size_t kIndex = 0>
requires(IsValidType<T>()) [[nodiscard]] static consteval auto GetIndexOf()
-> std::size_t {
static_assert(kIndex < std::variant_size_v<decltype(data_)>,
"kIndex out of range");
if constexpr (std::is_same_v<
T,
std::variant_alternative_t<kIndex,
decltype(data_)>>) {
return kIndex;
}
else {
return GetIndexOf<T, kIndex + 1>();
}
}
template <class T>
[[nodiscard]] auto IsA() const noexcept -> bool {
return std::holds_alternative<T>(data_);
}
template <class T>
[[nodiscard]] auto Cast() const& -> T const& {
if (GetIndexOf<T>() == data_.index()) {
return std::get<T>(data_);
}
// throw descriptive ExpressionTypeError
throw ExpressionTypeError{
fmt::format("Expression is not of type '{}' but '{}'.",
TypeToString<T>(),
TypeString())};
}
template <class T>
[[nodiscard]] auto Cast() && -> T {
if (GetIndexOf<T>() == data_.index()) {
return std::move(std::get<T>(data_));
}
// throw descriptive ExpressionTypeError
throw ExpressionTypeError{
fmt::format("Expression is not of type '{}' but '{}'.",
TypeToString<T>(),
TypeString())};
}
template <class T>
requires(Expression::IsValidType<T>())
[[nodiscard]] static auto TypeToString() noexcept -> std::string {
if constexpr (std::is_same_v<T, bool>) {
return "bool";
}
else if constexpr (std::is_same_v<T, Expression::number_t>) {
return "number";
}
else if constexpr (std::is_same_v<T, Expression::name_t>) {
return "name";
}
else if constexpr (std::is_same_v<T, std::string>) {
return "string";
}
else if constexpr (std::is_same_v<T, Expression::artifact_t>) {
return "artifact";
}
else if constexpr (std::is_same_v<T, Expression::result_t>) {
return "result";
}
else if constexpr (std::is_same_v<T, Expression::node_t>) {
return "node";
}
else if constexpr (std::is_same_v<T, Expression::list_t>) {
return "list";
}
else if constexpr (std::is_same_v<T, Expression::map_t>) {
return "map";
}
return "none";
}
template <size_t kIndex = 0>
[[nodiscard]] auto TypeStringForIndex() const noexcept -> std::string;
[[nodiscard]] auto TypeString() const noexcept -> std::string;
[[nodiscard]] auto ComputeHash() const noexcept -> std::string;
[[nodiscard]] auto ComputeIsCacheable() const -> bool;
};
namespace std {
template <>
struct hash<Expression> {
[[nodiscard]] auto operator()(Expression const& e) const noexcept
-> std::size_t {
auto hash = std::size_t{};
auto bytes = e.ToHash();
std::memcpy(&hash, bytes.data(), std::min(sizeof(hash), bytes.size()));
return hash;
}
};
} // namespace std
#endif // INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_EXPRESSION_EXPRESSION_HPP
|