From: Arthur Cohen <[email protected]>
gcc/rust/ChangeLog:
* ast/rust-ast-builder.cc (Builder::discriminant_value): New function.
* ast/rust-ast-builder.h: Declare it.
---
gcc/rust/ast/rust-ast-builder.cc | 10 ++++++++++
gcc/rust/ast/rust-ast-builder.h | 17 ++++++++++++++---
2 files changed, 24 insertions(+), 3 deletions(-)
diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc
index 2692684a9ba..914b321279f 100644
--- a/gcc/rust/ast/rust-ast-builder.cc
+++ b/gcc/rust/ast/rust-ast-builder.cc
@@ -542,6 +542,16 @@ Builder::generic_type_param (
std::vector<Attribute> ());
}
+std::unique_ptr<Stmt>
+Builder::discriminant_value (std::string binding_name, std::string instance)
+{
+ auto intrinsic = ptrify (
+ path_in_expression ({"core", "intrinsics", "discriminant_value"}, true));
+
+ return let (identifier_pattern (binding_name), nullptr,
+ call (std::move (intrinsic), identifier (instance)));
+}
+
std::unique_ptr<Type>
Builder::new_type (Type &type)
{
diff --git a/gcc/rust/ast/rust-ast-builder.h b/gcc/rust/ast/rust-ast-builder.h
index cb922dba658..36c3bc0ce90 100644
--- a/gcc/rust/ast/rust-ast-builder.h
+++ b/gcc/rust/ast/rust-ast-builder.h
@@ -24,6 +24,7 @@
#include "rust-ast.h"
#include "rust-item.h"
#include "rust-operators.h"
+#include <initializer_list>
namespace Rust {
namespace AST {
@@ -306,6 +307,14 @@ public:
std::vector<std::unique_ptr<TypeParamBound>> &&bounds,
std::unique_ptr<Type> &&type = nullptr);
+ /**
+ * Create a let statement with the discriminant value of a given enum
+ * instance. This helper exists since it is a common operation in a lot of
the
+ * derive implementations, and it sucks to repeat all the steps every time.
+ */
+ std::unique_ptr<Stmt> discriminant_value (std::string binding_name,
+ std::string instance = "self");
+
static std::unique_ptr<Type> new_type (Type &type);
static std::unique_ptr<GenericParam>
@@ -323,10 +332,12 @@ public:
static GenericArgs new_generic_args (GenericArgs &args);
private:
- /**
- * Location of the generated AST nodes
- */
+ /* Location of the generated AST nodes */
location_t loc;
+
+ /* Some constexpr helpers for some of the builders */
+ static constexpr std::initializer_list<const char *> discriminant_value_path
+ = {"core", "intrinsics", "discriminant_value"};
};
} // namespace AST
--
2.49.0