From: Arthur Cohen Date: Thu, 30 Jan 2025 09:50:13 +0000 (+0100) Subject: gccrs: ast-builder: Add new methods for functions, traits and tuples. X-Git-Tag: basepoints/gcc-16~784 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=554150f889ad2a00456c1d5c44bf12e025557aaa;p=thirdparty%2Fgcc.git gccrs: ast-builder: Add new methods for functions, traits and tuples. gcc/rust/ChangeLog: * ast/rust-ast-builder.cc: New methods. * ast/rust-ast-builder.h: Declare them. --- diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc index 4d94f0da573..3a3181f3752 100644 --- a/gcc/rust/ast/rust-ast-builder.cc +++ b/gcc/rust/ast/rust-ast-builder.cc @@ -26,6 +26,7 @@ #include "rust-path.h" #include "rust-system.h" #include "rust-token.h" +#include namespace Rust { namespace AST { @@ -83,12 +84,46 @@ Builder::tuple_idx (std::string receiver, int idx) const new TupleIndexExpr (identifier (receiver), idx, {}, loc)); } +std::unique_ptr +Builder::tuple (std::vector> &&values) const +{ + return std::unique_ptr ( + new TupleExpr (std::move (values), {}, {}, loc)); +} + +std::unique_ptr +Builder::self_ref_param (bool mutability) const +{ + return std::make_unique (Lifetime::error (), mutability, loc); +} + +std::unique_ptr +Builder::function_param (std::unique_ptr &&pattern, + std::unique_ptr &&type) const +{ + return std::unique_ptr ( + new FunctionParam (std::move (pattern), std::move (type), {}, loc)); +} + FunctionQualifiers Builder::fn_qualifiers () const { return FunctionQualifiers (loc, Async::No, Const::No, Unsafety::Normal); } +Function +Builder::function (Identifier function_name, + std::vector> params, + std::unique_ptr return_type, + std::unique_ptr block, + FunctionQualifiers qualifiers, WhereClause where_clause, + Visibility visibility) const +{ + return Function (function_name, qualifiers, {}, std::move (params), + std::move (return_type), where_clause, std::move (block), + visibility, {}, loc); +} + PathExprSegment Builder::path_segment (std::string seg) const { @@ -198,6 +233,14 @@ Builder::type_path (LangItem::Kind lang_item) const return type_path (type_path_segment (lang_item)); } +std::unique_ptr +Builder::reference_type (std::unique_ptr &&inner_type, + bool mutability) const +{ + return std::make_unique (mutability, std::move (inner_type), + loc); +} + PathInExpression Builder::path_in_expression (std::vector &&segments) const { @@ -356,6 +399,25 @@ Builder::loop (std::vector> &&stmts) return std::unique_ptr (new LoopExpr (std::move (block), loc)); } +std::unique_ptr +Builder::trait_bound (TypePath bound) +{ + return std::make_unique (bound, loc); +} + +std::unique_ptr +Builder::trait_impl (TypePath trait_path, std::unique_ptr target, + std::vector> trait_items, + std::vector> generics, + WhereClause where_clause, Visibility visibility) const +{ + return std::unique_ptr ( + new TraitImpl (trait_path, /* unsafe */ false, + /* exclam */ false, std::move (trait_items), + std::move (generics), std::move (target), where_clause, + visibility, {}, {}, loc)); +} + 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 47e8f7f5bbb..9c5c1645eb9 100644 --- a/gcc/rust/ast/rust-ast-builder.h +++ b/gcc/rust/ast/rust-ast-builder.h @@ -70,6 +70,10 @@ public: /* Create a tuple index expression (`receiver.0`) */ std::unique_ptr tuple_idx (std::string receiver, int idx) const; + /* Create a tuple expression (`(a1, a2, a3)`) */ + std::unique_ptr tuple (std::vector> &&values + = {}) const; + /* Create a reference to an expression (`&of`) */ std::unique_ptr ref (std::unique_ptr &&of, bool mut = false) const; @@ -106,9 +110,25 @@ public: std::unique_ptr array (std::vector> &&members) const; + /* Self parameter for a function definition (`&self`) */ + std::unique_ptr self_ref_param (bool mutability = false) const; + /* A regular named function parameter for a definition (`a: type`) */ + std::unique_ptr function_param (std::unique_ptr &&pattern, + std::unique_ptr &&type) const; + /* Empty function qualifiers, with no specific qualifiers */ FunctionQualifiers fn_qualifiers () const; + Function + function (Identifier function_name, + std::vector> params, + std::unique_ptr return_type, std::unique_ptr block, + FunctionQualifiers qualifiers + = FunctionQualifiers (UNKNOWN_LOCATION, Async::No, Const::No, + Unsafety::Normal), + WhereClause where_clause = WhereClause::create_empty (), + Visibility visibility = Visibility::create_private ()) const; + /* Create a single path segment from one string */ PathExprSegment path_segment (std::string seg) const; @@ -140,6 +160,10 @@ public: TypePath type_path (std::string type) const; TypePath type_path (LangItem::Kind lang_item) const; + std::unique_ptr + reference_type (std::unique_ptr &&inner_type, + bool mutability = false) const; + /** * Create a path in expression from multiple segments (`Clone::clone`). You * do not need to separate the segments using `::`, you can simply provide a @@ -164,8 +188,8 @@ public: /** * Create an expression for struct instantiation with fields (`S { a, b: c }`) - * Tuple expressions are call expressions and can thus be constructed with - * `call` + * Named tuple expressions (`S(a, b, c)`) are call expressions and can thus be + * constructed with `call` */ std::unique_ptr struct_expr (std::string struct_name, @@ -199,6 +223,14 @@ public: /* Create a loop expression */ std::unique_ptr loop (std::vector> &&stmts); + std::unique_ptr trait_bound (TypePath bound); + std::unique_ptr + trait_impl (TypePath trait_path, std::unique_ptr target, + std::vector> trait_items = {}, + std::vector> generics = {}, + WhereClause where_clause = WhereClause::create_empty (), + Visibility visibility = Visibility::create_private ()) const; + static std::unique_ptr new_type (Type &type); static std::unique_ptr