From 4ab7564dd217cbb8913d852189302ef9e097d184 Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Thu, 30 Jan 2025 13:07:48 +0100 Subject: ast-builder: Add methods for QualifiedPathInExpressions gcc/rust/ChangeLog: * ast/rust-ast-builder.cc (Builder::qualified_path_in_expression): New. (Builder::function): Change the return type. * ast/rust-ast-builder.h: Declare qualified_path_in_expression functions. * expand/rust-derive-debug.cc (DeriveDebug::stub_debug_fn): Adapt to new APIs. --- gcc/rust/ast/rust-ast-builder.cc | 32 +++++++++++++++++++++++++++----- gcc/rust/ast/rust-ast-builder.h | 16 +++++++++++++--- gcc/rust/expand/rust-derive-debug.cc | 4 ++-- 3 files changed, 42 insertions(+), 10 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc index 0538998..9685ae5 100644 --- a/gcc/rust/ast/rust-ast-builder.cc +++ b/gcc/rust/ast/rust-ast-builder.cc @@ -65,6 +65,27 @@ Builder::array (std::vector> &&members) const } std::unique_ptr +Builder::qualified_path_in_expression (std::unique_ptr &&type, + TypePath trait, + PathExprSegment segment) const +{ + auto segments = {segment}; + + return qualified_path_in_expression (std::move (type), trait, segments); +} + +std::unique_ptr +Builder::qualified_path_in_expression ( + std::unique_ptr &&type, TypePath trait, + std::vector &&segments) const +{ + auto qual_type = QualifiedPathType (std::move (type), loc, trait); + + return std::unique_ptr ( + new QualifiedPathInExpression (qual_type, std::move (segments), {}, loc)); +} + +std::unique_ptr Builder::identifier (std::string name) const { return std::unique_ptr (new IdentifierExpr (name, {}, loc)); @@ -111,17 +132,18 @@ Builder::fn_qualifiers () const return FunctionQualifiers (loc, Async::No, Const::No, Unsafety::Normal); } -Function -Builder::function (Identifier function_name, +std::unique_ptr +Builder::function (std::string 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); + return std::unique_ptr ( + new Function (function_name, qualifiers, {}, std::move (params), + std::move (return_type), where_clause, std::move (block), + visibility, {}, loc)); } PathExprSegment diff --git a/gcc/rust/ast/rust-ast-builder.h b/gcc/rust/ast/rust-ast-builder.h index f67546f..262bdcf 100644 --- a/gcc/rust/ast/rust-ast-builder.h +++ b/gcc/rust/ast/rust-ast-builder.h @@ -104,7 +104,8 @@ public: * arguments (`path(arg0, arg1, arg2)`) */ std::unique_ptr call (std::unique_ptr &&path, - std::vector> &&args) const; + std::vector> &&args + = {}) const; std::unique_ptr call (std::unique_ptr &&path, std::unique_ptr &&arg) const; @@ -114,6 +115,15 @@ public: std::unique_ptr array (std::vector> &&members) const; + /* Create a qualified path in expression (`::seg::expr`) */ + std::unique_ptr + qualified_path_in_expression (std::unique_ptr &&type, TypePath trait, + PathExprSegment segment) const; + std::unique_ptr + qualified_path_in_expression (std::unique_ptr &&type, TypePath trait, + std::vector &&segments + = {}) 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`) */ @@ -123,8 +133,8 @@ public: /* Empty function qualifiers, with no specific qualifiers */ FunctionQualifiers fn_qualifiers () const; - Function - function (Identifier function_name, + std::unique_ptr + function (std::string function_name, std::vector> params, std::unique_ptr return_type, std::unique_ptr block, FunctionQualifiers qualifiers diff --git a/gcc/rust/expand/rust-derive-debug.cc b/gcc/rust/expand/rust-derive-debug.cc index 910f27c..f375474 100644 --- a/gcc/rust/expand/rust-derive-debug.cc +++ b/gcc/rust/expand/rust-derive-debug.cc @@ -77,10 +77,10 @@ DeriveDebug::stub_debug_fn () auto params = vec (std::move (self), std::move (fmt)); - auto function = builder.function ({"fmt"}, std::move (params), + auto function = builder.function ("fmt", std::move (params), std::move (return_type), std::move (block)); - return ptrify (function); + return function; } std::unique_ptr -- cgit v1.1