]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: builder: Allow generating struct statements
authorArthur Cohen <arthur.cohen@embecosm.com>
Thu, 26 Dec 2024 10:49:16 +0000 (10:49 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Fri, 21 Mar 2025 11:57:50 +0000 (12:57 +0100)
gcc/rust/ChangeLog:

* ast/rust-ast-builder.cc (Builder::struct_struct): New function.
* ast/rust-ast-builder.h (vec): New function.

gcc/rust/ast/rust-ast-builder.cc
gcc/rust/ast/rust-ast-builder.h

index f6d0f0dc4aed7fbc54ff5548ada2c20c2bdaf39e..55d59c94b52c937e0dddf7bbffca43cf266aadfe 100644 (file)
 
 #include "rust-ast-builder.h"
 #include "rust-ast-builder-type.h"
+#include "rust-ast.h"
 #include "rust-common.h"
 #include "rust-expr.h"
 #include "rust-path.h"
+#include "rust-item.h"
 #include "rust-token.h"
 
 namespace Rust {
@@ -182,6 +184,19 @@ Builder::deref (std::unique_ptr<Expr> &&of) const
   return std::unique_ptr<Expr> (new DereferenceExpr (std::move (of), {}, loc));
 }
 
+std::unique_ptr<Stmt>
+Builder::struct_struct (std::string struct_name,
+                       std::vector<std::unique_ptr<GenericParam>> &&generics,
+                       std::vector<StructField> &&fields)
+{
+  auto is_unit = fields.empty ();
+
+  return std::unique_ptr<Stmt> (
+    new StructStruct (std::move (fields), struct_name, std::move (generics),
+                     WhereClause::create_empty (), is_unit,
+                     Visibility::create_private (), {}, loc));
+}
+
 std::unique_ptr<Expr>
 Builder::struct_expr_struct (std::string struct_name) const
 {
index aaf05c6d1fb1999292e1329e79a9138f2a179b03..36c0da2cff211956c139e63fa705c2b6e6864662 100644 (file)
 
 #include "rust-ast-full.h"
 #include "rust-expr.h"
+#include "rust-ast.h"
+#include "rust-item.h"
 
 namespace Rust {
 namespace AST {
 
+template <typename T>
+std::vector<std::unique_ptr<T>>
+vec (std::unique_ptr<T> &&t)
+{
+  auto v = std::vector<std::unique_ptr<T>> ();
+
+  v.emplace_back (std::move (t));
+
+  return v;
+}
+
+template <typename T>
+std::vector<std::unique_ptr<T>>
+vec (std::unique_ptr<T> &&t1, std::unique_ptr<T> &&t2)
+{
+  auto v = std::vector<std::unique_ptr<T>> ();
+
+  v.emplace_back (std::move (t1));
+  v.emplace_back (std::move (t2));
+
+  return v;
+}
+
 // TODO: Use this builder when expanding regular macros
 /* Builder class with helper methods to create AST nodes. This builder is
  * tailored towards generating multiple AST nodes from a single location, and
@@ -113,6 +138,12 @@ public:
    */
   PathInExpression path_in_expression (LangItem::Kind lang_item) const;
 
+  /* Create a new struct */
+  std::unique_ptr<Stmt>
+  struct_struct (std::string struct_name,
+                std::vector<std::unique_ptr<GenericParam>> &&generics,
+                std::vector<StructField> &&fields);
+
   /* Create a struct expression for unit structs (`S`) */
   std::unique_ptr<Expr> struct_expr_struct (std::string struct_name) const;