]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: builder: Add Builder::discriminant_value
authorArthur Cohen <arthur.cohen@embecosm.com>
Tue, 22 Apr 2025 18:02:47 +0000 (20:02 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:36:47 +0000 (16:36 +0200)
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
gcc/rust/ast/rust-ast-builder.h

index 2692684a9bac3618550b7576b7bd1805ef3cce51..914b321279f5ddeff8d067133623f92628ad97f6 100644 (file)
@@ -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)
 {
index cb922dba6589f55ec6c678ce5703b167dbc2ffdf..36c3bc0ce901f9bf8f9ce11bf017ed20fc4bf9ef 100644 (file)
@@ -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