aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorArthur Cohen <arthur.cohen@embecosm.com>2025-02-21 14:52:17 +0000
committerCohenArthur <arthur.cohen@embecosm.com>2025-02-24 08:53:25 +0000
commite5b7e27532b8a63c344376cb749d1578782fcab3 (patch)
tree8cb15069ccf0fb61580ef7ceeef86f9baa94afaf /gcc
parent22333202dc0da0fe8dc02b1e85d52bf5cd8ca39f (diff)
downloadgcc-e5b7e27532b8a63c344376cb749d1578782fcab3.zip
gcc-e5b7e27532b8a63c344376cb749d1578782fcab3.tar.gz
gcc-e5b7e27532b8a63c344376cb749d1578782fcab3.tar.bz2
ast: builder: Add new builder methods
gcc/rust/ChangeLog: * ast/rust-ast-builder.cc (Builder::statementify): New. (Builder::function): Add generic params optional argument. (Builder::path_in_expression): Add opening_scope_resolution optional argument. (Builder::block): Add function for creating empty blocks. (Builder::generic_type_param): New. * ast/rust-ast-builder.h (ptrify): New.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/ast/rust-ast-builder.cc39
-rw-r--r--gcc/rust/ast/rust-ast-builder.h24
2 files changed, 56 insertions, 7 deletions
diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc
index 2b96274..58c37a5 100644
--- a/gcc/rust/ast/rust-ast-builder.cc
+++ b/gcc/rust/ast/rust-ast-builder.cc
@@ -33,6 +33,14 @@
namespace Rust {
namespace AST {
+std::unique_ptr<Stmt>
+Builder::statementify (std::unique_ptr<Expr> &&value,
+ bool semicolon_followed) const
+{
+ return std::make_unique<ExprStmt> (std::move (value), loc,
+ semicolon_followed);
+}
+
std::unique_ptr<Expr>
Builder::literal_string (std::string &&content) const
{
@@ -150,13 +158,14 @@ Builder::function (std::string function_name,
std::vector<std::unique_ptr<Param>> params,
std::unique_ptr<Type> return_type,
std::unique_ptr<BlockExpr> block,
+ std::vector<std::unique_ptr<GenericParam>> generic_params,
FunctionQualifiers qualifiers, WhereClause where_clause,
Visibility visibility) const
{
return std::unique_ptr<Function> (
- new Function (function_name, qualifiers, {}, std::move (params),
- std::move (return_type), where_clause, std::move (block),
- visibility, {}, loc));
+ new Function (function_name, qualifiers, std::move (generic_params),
+ std::move (params), std::move (return_type), where_clause,
+ std::move (block), visibility, {}, loc));
}
PathExprSegment
@@ -277,13 +286,14 @@ Builder::reference_type (std::unique_ptr<TypeNoBounds> &&inner_type,
}
PathInExpression
-Builder::path_in_expression (std::vector<std::string> &&segments) const
+Builder::path_in_expression (std::vector<std::string> &&segments,
+ bool opening_scope) const
{
auto path_segments = std::vector<PathExprSegment> ();
for (auto &seg : segments)
path_segments.emplace_back (path_segment (seg));
- return PathInExpression (std::move (path_segments), {}, loc);
+ return PathInExpression (std::move (path_segments), {}, loc, opening_scope);
}
PathInExpression
@@ -313,6 +323,14 @@ Builder::block (tl::optional<std::unique_ptr<Stmt>> &&stmt,
}
std::unique_ptr<BlockExpr>
+Builder::block () const
+{
+ auto stmts = std::vector<std::unique_ptr<Stmt>> ();
+
+ return block (std::move (stmts));
+}
+
+std::unique_ptr<BlockExpr>
Builder::block (std::vector<std::unique_ptr<Stmt>> &&stmts,
std::unique_ptr<Expr> &&tail_expr) const
{
@@ -493,6 +511,17 @@ Builder::trait_impl (TypePath trait_path, std::unique_ptr<Type> target,
visibility, {}, {}, loc));
}
+std::unique_ptr<GenericParam>
+Builder::generic_type_param (
+ std::string type_representation,
+ std::vector<std::unique_ptr<TypeParamBound>> &&bounds,
+ std::unique_ptr<Type> &&type)
+{
+ return std::make_unique<TypeParam> (type_representation, loc,
+ std::move (bounds), std::move (type),
+ std::vector<Attribute> ());
+}
+
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 a1c5621..c0550c4 100644
--- a/gcc/rust/ast/rust-ast-builder.h
+++ b/gcc/rust/ast/rust-ast-builder.h
@@ -51,6 +51,14 @@ vec (std::unique_ptr<T> &&t1, std::unique_ptr<T> &&t2)
return v;
}
+/* Pointer-ify something */
+template <typename T>
+static std::unique_ptr<T>
+ptrify (T value)
+{
+ return std::unique_ptr<T> (new T (value));
+}
+
// 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
@@ -60,6 +68,10 @@ class Builder
public:
Builder (location_t loc) : loc (loc) {}
+ /* Create an expression statement from an expression */
+ std::unique_ptr<Stmt> statementify (std::unique_ptr<Expr> &&value,
+ bool semicolon_followed = true) const;
+
/* Create a string literal expression ("content") */
std::unique_ptr<Expr> literal_string (std::string &&content) const;
@@ -102,6 +114,8 @@ public:
std::unique_ptr<BlockExpr> block (tl::optional<std::unique_ptr<Stmt>> &&stmt,
std::unique_ptr<Expr> &&tail_expr
= nullptr) const;
+ /* Create an empty block */
+ std::unique_ptr<BlockExpr> block () const;
/* Create an early return expression with an optional expression */
std::unique_ptr<Expr> return_expr (std::unique_ptr<Expr> &&to_return
@@ -151,6 +165,7 @@ public:
function (std::string function_name,
std::vector<std::unique_ptr<Param>> params,
std::unique_ptr<Type> return_type, std::unique_ptr<BlockExpr> block,
+ std::vector<std::unique_ptr<GenericParam>> generic_params = {},
FunctionQualifiers qualifiers
= FunctionQualifiers (UNKNOWN_LOCATION, Async::No, Const::No,
Unsafety::Normal),
@@ -197,8 +212,8 @@ public:
* do not need to separate the segments using `::`, you can simply provide a
* vector of strings to the functions which will get turned into path segments
*/
- PathInExpression
- path_in_expression (std::vector<std::string> &&segments) const;
+ PathInExpression path_in_expression (std::vector<std::string> &&segments,
+ bool opening_scope = false) const;
/**
* Create a path in expression from a lang item.
@@ -265,6 +280,11 @@ public:
WhereClause where_clause = WhereClause::create_empty (),
Visibility visibility = Visibility::create_private ()) const;
+ std::unique_ptr<GenericParam>
+ generic_type_param (std::string type_representation,
+ std::vector<std::unique_ptr<TypeParamBound>> &&bounds,
+ std::unique_ptr<Type> &&type = nullptr);
+
static std::unique_ptr<Type> new_type (Type &type);
static std::unique_ptr<GenericParam>