https://gcc.gnu.org/g:837189032be19939107be95139636f2a5491fd31
commit r16-2860-g837189032be19939107be95139636f2a5491fd31 Author: Arthur Cohen <arthur.co...@embecosm.com> Date: Tue Apr 22 20:02:47 2025 +0200 gccrs: builder: Add Builder::discriminant_value gcc/rust/ChangeLog: * ast/rust-ast-builder.cc (Builder::discriminant_value): New function. * ast/rust-ast-builder.h: Declare it. Diff: --- 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 2692684a9bac..914b321279f5 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 cb922dba6589..36c3bc0ce901 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