From: Arthur Cohen Date: Tue, 22 Apr 2025 18:02:47 +0000 (+0200) Subject: gccrs: builder: Add Builder::discriminant_value X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=837189032be19939107be95139636f2a5491fd31;p=thirdparty%2Fgcc.git 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 --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 ()); } +std::unique_ptr +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 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 namespace Rust { namespace AST { @@ -306,6 +307,14 @@ public: std::vector> &&bounds, std::unique_ptr &&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 discriminant_value (std::string binding_name, + std::string instance = "self"); + static std::unique_ptr new_type (Type &type); static std::unique_ptr @@ -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 discriminant_value_path + = {"core", "intrinsics", "discriminant_value"}; }; } // namespace AST