diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2024-02-22 16:26:40 +0100 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2024-08-01 13:12:16 +0200 |
commit | 9b540c4299a6567d0e7642a4664dc76291dc8cbc (patch) | |
tree | 2a18a4b22f8c5901961ad92b5c60c0a9740b96f8 /gcc | |
parent | 5ed71ad2a2f394afc9d66aee0c30476879292cab (diff) | |
download | gcc-9b540c4299a6567d0e7642a4664dc76291dc8cbc.zip gcc-9b540c4299a6567d0e7642a4664dc76291dc8cbc.tar.gz gcc-9b540c4299a6567d0e7642a4664dc76291dc8cbc.tar.bz2 |
gccrs: format-args: Start storing string in Rust memory
gcc/rust/ChangeLog:
* ast/rust-fmt.cc (ffi::RustHamster::to_string): New.
(Pieces::collect): Adapt to use new handle API.
(Pieces::~Pieces): Likewise.
(Pieces::Pieces): Likewise.
(Pieces::operator=): Likewise.
* ast/rust-fmt.h (struct RustString): Add members.
(struct FormatArgsHandle): New.
(clone_pieces): Adapt for new FFI API.
(destroy_pieces): Likewise.
(struct Pieces): Store new FormatArgsHandle type.
* expand/rust-expand-format-args.cc (expand_format_args): Use proper
namespace.
* resolve/rust-ast-resolve-base.cc (ResolverBase::visit): FormatArgs
nodes are already resolved, so do nothing.
libgrust/ChangeLog:
* libformat_parser/src/lib.rs: Use new Handle struct and expose it.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/ast/rust-fmt.cc | 32 | ||||
-rw-r--r-- | gcc/rust/ast/rust-fmt.h | 46 | ||||
-rw-r--r-- | gcc/rust/expand/rust-expand-format-args.cc | 4 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-base.cc | 5 |
4 files changed, 53 insertions, 34 deletions
diff --git a/gcc/rust/ast/rust-fmt.cc b/gcc/rust/ast/rust-fmt.cc index b82e089..fda02e5 100644 --- a/gcc/rust/ast/rust-fmt.cc +++ b/gcc/rust/ast/rust-fmt.cc @@ -22,44 +22,48 @@ namespace Rust { namespace Fmt { +std::string +ffi::RustHamster::to_string () const +{ + return std::string (ptr, len); +} + Pieces -Pieces::collect (std::string &&to_parse, bool append_newline) +Pieces::collect (const std::string &to_parse, bool append_newline) { - auto piece_slice = collect_pieces (to_parse.c_str (), append_newline); + auto handle = ffi::collect_pieces (to_parse.c_str (), append_newline); // this performs multiple copies, can we avoid them maybe? // TODO: Instead of just creating a vec of, basically, `ffi::Piece`s, we // should transform them into the proper C++ type which we can work with. so // transform all the strings into C++ strings? all the Option<T> into // tl::optional<T>? - auto pieces = std::vector<Piece> (piece_slice.base_ptr, - piece_slice.base_ptr + piece_slice.len); + auto pieces_vector = std::vector<ffi::Piece> (handle.piece_slice.base_ptr, + handle.piece_slice.base_ptr + + handle.piece_slice.len); - return Pieces (std::move (pieces), piece_slice, std::move (to_parse)); + return Pieces (handle, std::move (pieces_vector)); } -Pieces::~Pieces () { destroy_pieces (slice); } +Pieces::~Pieces () { ffi::destroy_pieces (handle); } -Pieces::Pieces (const Pieces &other) - : pieces_vector (other.pieces_vector), to_parse (other.to_parse) +Pieces::Pieces (const Pieces &other) : pieces_vector (other.pieces_vector) { - slice = clone_pieces (other.slice.base_ptr, other.slice.len, other.slice.cap); + handle = ffi::clone_pieces (other.handle); } Pieces & Pieces::operator= (const Pieces &other) { - slice = clone_pieces (other.slice.base_ptr, other.slice.len, other.slice.cap); - to_parse = other.to_parse; + handle = ffi::clone_pieces (other.handle); + pieces_vector = other.pieces_vector; return *this; } Pieces::Pieces (Pieces &&other) : pieces_vector (std::move (other.pieces_vector)), - slice ( - clone_pieces (other.slice.base_ptr, other.slice.len, other.slice.cap)), - to_parse (std::move (other.to_parse)) + handle (clone_pieces (other.handle)) {} } // namespace Fmt diff --git a/gcc/rust/ast/rust-fmt.h b/gcc/rust/ast/rust-fmt.h index ba412f9..6a0c116 100644 --- a/gcc/rust/ast/rust-fmt.h +++ b/gcc/rust/ast/rust-fmt.h @@ -20,15 +20,21 @@ #define RUST_FMT_H #include "rust-system.h" +#include <cstddef> // FIXME: How to encode Option? namespace Rust { namespace Fmt { +namespace ffi { + struct RustHamster { - // hehe + const char *ptr; + size_t len; + + std::string to_string () const; }; /// Enum of alignments which are supported. @@ -240,21 +246,36 @@ struct PieceSlice size_t cap; }; +struct RustString +{ + const unsigned char *ptr; + size_t len; + size_t cap; +}; + +struct FormatArgsHandle +{ + PieceSlice piece_slice; + RustString rust_string; +}; + extern "C" { -PieceSlice +FormatArgsHandle collect_pieces (const char *input, bool append_newline); -PieceSlice -clone_pieces (const Piece *base_ptr, size_t len, size_t cap); +FormatArgsHandle +clone_pieces (const FormatArgsHandle &); -void destroy_pieces (PieceSlice); +void destroy_pieces (FormatArgsHandle); } // extern "C" +} // namespace ffi + struct Pieces { - static Pieces collect (std::string &&to_parse, bool append_newline); + static Pieces collect (const std::string &to_parse, bool append_newline); ~Pieces (); Pieces (const Pieces &other); @@ -262,7 +283,7 @@ struct Pieces Pieces (Pieces &&other); - const std::vector<Piece> &get_pieces () const { return pieces_vector; } + const std::vector<ffi::Piece> &get_pieces () const { return pieces_vector; } // { // slice = clone_pieces (&other.slice); @@ -272,19 +293,16 @@ struct Pieces // } private: - Pieces (std::vector<Piece> &&pieces_vector, PieceSlice slice, - std::string &&to_parse) - : pieces_vector (std::move (pieces_vector)), slice (slice), - to_parse (std::move (to_parse)) + Pieces (ffi::FormatArgsHandle handle, std::vector<ffi::Piece> &&pieces_vector) + : pieces_vector (std::move (pieces_vector)), handle (handle) {} - std::vector<Piece> pieces_vector; + std::vector<ffi::Piece> pieces_vector; // this memory is held for FFI reasons - it needs to be released and cloned // precisely, so try to not access it/modify it if possible. you should // instead work with `pieces_vector` - PieceSlice slice; - std::string to_parse; + ffi::FormatArgsHandle handle; }; } // namespace Fmt diff --git a/gcc/rust/expand/rust-expand-format-args.cc b/gcc/rust/expand/rust-expand-format-args.cc index 52249cb..276ffd5 100644 --- a/gcc/rust/expand/rust-expand-format-args.cc +++ b/gcc/rust/expand/rust-expand-format-args.cc @@ -28,10 +28,10 @@ expand_format_args (AST::FormatArgs &fmt) { switch (node.tag) { - case Fmt::Piece::Tag::String: + case Fmt::ffi::Piece::Tag::String: // rust_debug ("[ARTHUR]: %s", node.string._0.c_str ()); - case Fmt::Piece::Tag::NextArgument: + case Fmt::ffi::Piece::Tag::NextArgument: rust_debug ("[ARTHUR]: NextArgument"); break; } diff --git a/gcc/rust/resolve/rust-ast-resolve-base.cc b/gcc/rust/resolve/rust-ast-resolve-base.cc index 1939a20..fa1be19 100644 --- a/gcc/rust/resolve/rust-ast-resolve-base.cc +++ b/gcc/rust/resolve/rust-ast-resolve-base.cc @@ -648,10 +648,7 @@ ResolverBase::visit (AST::FunctionParam &) void ResolverBase::visit (AST::FormatArgs &fmt) -{ - rust_sorry_at (fmt.get_locus (), "%s:%u: unimplemented FormatArgs visitor", - __FILE__, __LINE__); -} +{} } // namespace Resolver } // namespace Rust |