diff options
Diffstat (limited to 'gcc/rust/ast')
-rw-r--r-- | gcc/rust/ast/rust-ast.cc | 51 | ||||
-rw-r--r-- | gcc/rust/ast/rust-builtin-ast-nodes.h | 133 | ||||
-rw-r--r-- | gcc/rust/ast/rust-fmt.cc | 38 | ||||
-rw-r--r-- | gcc/rust/ast/rust-fmt.h | 21 |
4 files changed, 226 insertions, 17 deletions
diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc index 5d571b4..f3dabc6 100644 --- a/gcc/rust/ast/rust-ast.cc +++ b/gcc/rust/ast/rust-ast.cc @@ -19,6 +19,7 @@ along with GCC; see the file COPYING3. If not see #include "rust-ast.h" #include "optional.h" +#include "rust-builtin-ast-nodes.h" #include "rust-system.h" #include "rust-ast-full.h" #include "rust-diagnostics.h" @@ -5054,6 +5055,56 @@ FormatArgs::accept_vis (ASTVisitor &vis) vis.visit (*this); } +std::string +FormatArgs::as_string () const +{ + // FIXME(Arthur): Improve + return "FormatArgs"; +} + +location_t +FormatArgs::get_locus () const +{ + rust_unreachable (); +} + +bool +FormatArgs::is_expr_without_block () const +{ + return false; +} + +void +FormatArgs::mark_for_strip () +{ + marked_for_strip = true; +} + +bool +FormatArgs::is_marked_for_strip () const +{ + return marked_for_strip; +} + +std::vector<Attribute> & +FormatArgs::get_outer_attrs () +{ + rust_unreachable (); +} + +void FormatArgs::set_outer_attrs (std::vector<Attribute>) +{ + rust_unreachable (); +} + +Expr * +FormatArgs::clone_expr_impl () const +{ + std::cerr << "[ARTHUR] cloning FormatArgs! " << std::endl; + + return new FormatArgs (*this); +} + } // namespace AST std::ostream & diff --git a/gcc/rust/ast/rust-builtin-ast-nodes.h b/gcc/rust/ast/rust-builtin-ast-nodes.h index 3998fbf..6e26717 100644 --- a/gcc/rust/ast/rust-builtin-ast-nodes.h +++ b/gcc/rust/ast/rust-builtin-ast-nodes.h @@ -59,9 +59,17 @@ namespace AST { // └─┘ └─┘ // positions (could be names, numbers, empty, or `*`) +// FIXME: Merge with the class below this one? class FormatArgumentKind { public: + enum class Kind + { + Normal, + Named, + Captured, + } kind; + Identifier &get_ident () { rust_assert (kind == Kind::Captured || kind == Kind::Named); @@ -69,25 +77,90 @@ public: return ident.value (); } -private: - enum class Kind + FormatArgumentKind (Kind kind, tl::optional<Identifier> ident) + : kind (kind), ident (ident) + {} + + FormatArgumentKind (const FormatArgumentKind &other) { - Normal, - Named, - Captured, - } kind; + kind = other.kind; + ident = other.ident; + } + + FormatArgumentKind operator= (const FormatArgumentKind &other) + { + kind = other.kind; + ident = other.ident; + return *this; + } + +private: tl::optional<Identifier> ident; }; class FormatArgument { +public: + static FormatArgument normal (std::unique_ptr<Expr> expr) + { + return FormatArgument (FormatArgumentKind::Kind::Normal, tl::nullopt, + std::move (expr)); + } + + static FormatArgument named (Identifier ident, std::unique_ptr<Expr> expr) + { + return FormatArgument (FormatArgumentKind::Kind::Named, ident, + std::move (expr)); + } + + static FormatArgument captured (Identifier ident, std::unique_ptr<Expr> expr) + { + return FormatArgument (FormatArgumentKind::Kind::Captured, ident, + std::move (expr)); + } + + FormatArgument (const FormatArgument &other) + : kind (other.kind), expr (other.expr->clone_expr ()) + {} + + FormatArgument operator= (const FormatArgument &other) + { + kind = other.kind; + expr = other.expr->clone_expr (); + + return *this; + } + +private: + FormatArgument (FormatArgumentKind::Kind kind, tl::optional<Identifier> ident, + std::unique_ptr<Expr> expr) + : kind (FormatArgumentKind (kind, ident)), expr (std::move (expr)) + {} + FormatArgumentKind kind; std::unique_ptr<Expr> expr; }; class FormatArguments { +public: + FormatArguments () {} + FormatArguments (FormatArguments &&) = default; + FormatArguments (const FormatArguments &other) + { + args = std::vector<FormatArgument> (); + args.reserve (other.args.size ()); + + for (const auto &arg : other.args) + args.emplace_back (arg); + }; + + FormatArguments &operator= (const FormatArguments &other) = default; + + void push (FormatArgument &&elt) { args.emplace_back (std::move (elt)); } + +private: std::vector<FormatArgument> args; }; @@ -100,7 +173,7 @@ class FormatArguments // format_args!("result: {}", some_result))` -> `format_args!("heyo result: {}", // some_result)` // FIXME: Move to rust-macro.h -class FormatArgs : public Visitable +class FormatArgs : public Expr { public: enum class Newline @@ -109,18 +182,56 @@ public: No }; - FormatArgs (location_t loc, Fmt::PieceSlice template_str, - FormatArguments arguments) + FormatArgs (location_t loc, Fmt::Pieces &&template_str, + FormatArguments &&arguments) : loc (loc), template_str (std::move (template_str)), arguments (std::move (arguments)) {} - void accept_vis (AST::ASTVisitor &vis); + FormatArgs (FormatArgs &&other) + : loc (std::move (other.loc)), + template_str (std::move (other.template_str)), + arguments (std::move (other.arguments)) + { + std::cerr << "[ARTHUR] moving FormatArgs" << std::endl; + } + + // FIXME: This might be invalid - we are reusing the same memory allocated + // on the Rust side for `other`. This is probably valid as long as we only + // ever read that memory and never write to it. + FormatArgs (const FormatArgs &other) + : loc (other.loc), template_str (other.template_str), + arguments (other.arguments) + { + std::cerr << "[ARTHUR] copying FormatArgs" << std::endl; + } + + // FormatArgs &operator= (const FormatArgs &other) = default; + // : template_str (other.template_str), arguments (other.arguments) + // {} + + void accept_vis (AST::ASTVisitor &vis) override; private: location_t loc; - Fmt::PieceSlice template_str; + // FIXME: This probably needs to be a separate type - it is one in rustc's + // expansion of format_args!(). There is extra handling associated with it. + // we can maybe do that in rust-fmt.cc? in collect_pieces()? like do the + // transformation into something we can handle better + Fmt::Pieces template_str; FormatArguments arguments; + + bool marked_for_strip = false; + +protected: + virtual std::string as_string () const override; + virtual location_t get_locus () const override; + virtual bool is_expr_without_block () const override; + virtual void mark_for_strip () override; + virtual bool is_marked_for_strip () const override; + virtual std::vector<Attribute> &get_outer_attrs () override; + virtual void set_outer_attrs (std::vector<Attribute>) override; + virtual Expr *clone_expr_impl () const override; }; } // namespace AST diff --git a/gcc/rust/ast/rust-fmt.cc b/gcc/rust/ast/rust-fmt.cc index 511e947..c367e30 100644 --- a/gcc/rust/ast/rust-fmt.cc +++ b/gcc/rust/ast/rust-fmt.cc @@ -23,9 +23,9 @@ namespace Rust { namespace Fmt { Pieces -Pieces::collect (std::string &&to_parse) +Pieces::collect (std::string &&to_parse, bool append_newline) { - auto piece_slice = collect_pieces (to_parse.c_str ()); + auto piece_slice = collect_pieces (to_parse.c_str (), append_newline); rust_debug ("[ARTHUR] %p, %lu", (const void *) piece_slice.base_ptr, piece_slice.len); @@ -37,7 +37,39 @@ Pieces::collect (std::string &&to_parse) return Pieces (piece_slice, std::move (to_parse)); } -Pieces::~Pieces () { destroy_pieces (slice); } +Pieces::~Pieces () +{ + std::cerr << "Arthur: destoying pieces. this: " << (void *) this + << " slice: " << slice.base_ptr << std::endl; + destroy_pieces (slice); +} + +Pieces::Pieces (const Pieces &other) : to_parse (other.to_parse) +{ + slice = clone_pieces (other.slice.base_ptr, other.slice.len, other.slice.cap); + std::cerr << "Arthur: copying pieces: other.to_parse: " + << (void *) other.to_parse.c_str () + << " ours to_parse: " << (void *) to_parse.c_str () << std::endl; + // auto pieces = std::vector (slice.base_ptr, slice.base_ptr + slice.len); +} + +Pieces & +Pieces::operator= (const Pieces &other) +{ + slice = clone_pieces (other.slice.base_ptr, other.slice.len, other.slice.cap); + to_parse = other.to_parse; + + return *this; +} + +Pieces::Pieces (Pieces &&other) + : slice ( + clone_pieces (other.slice.base_ptr, other.slice.len, other.slice.cap)), + to_parse (std::move (other.to_parse)) +{ + std::cerr << "Arthur: moving pieces. to_parse: " << (void *) to_parse.c_str () + << " base_ptr/slice: " << (void *) slice.base_ptr << std::endl; +} } // namespace Fmt } // namespace Rust diff --git a/gcc/rust/ast/rust-fmt.h b/gcc/rust/ast/rust-fmt.h index 0bf9695..22447c4 100644 --- a/gcc/rust/ast/rust-fmt.h +++ b/gcc/rust/ast/rust-fmt.h @@ -222,7 +222,7 @@ struct Piece struct NextArgument_Body { - const Argument *_0; + Argument _0; }; Tag tag; @@ -243,7 +243,10 @@ struct PieceSlice extern "C" { PieceSlice -collect_pieces (const char *input); +collect_pieces (const char *input, bool append_newline); + +PieceSlice +clone_pieces (const Piece *base_ptr, size_t len, size_t cap); void destroy_pieces (PieceSlice); @@ -251,9 +254,21 @@ void destroy_pieces (PieceSlice); struct Pieces { - static Pieces collect (std::string &&to_parse); + static Pieces collect (std::string &&to_parse, bool append_newline); ~Pieces (); + Pieces (const Pieces &other); + Pieces &operator= (const Pieces &other); + + Pieces (Pieces &&other); + + // { + // slice = clone_pieces (&other.slice); + // to_parse = other.to_parse; + + // return *this; + // } + private: Pieces (PieceSlice slice, std::string &&to_parse) : slice (slice), to_parse (std::move (to_parse)) |