diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc index 121b8c8d7e0..92bc5a2f1f4 100644 --- a/gcc/rust/ast/rust-ast-builder.cc +++ b/gcc/rust/ast/rust-ast-builder.cc @@ -20,6 +20,7 @@ #include "rust-ast-builder-type.h" #include "rust-common.h" #include "rust-expr.h" +#include "rust-path.h" #include "rust-token.h" #include "rust-make-unique.h" @@ -42,6 +43,33 @@ Builder::call (std::unique_ptr &&path, new CallExpr (std::move (path), std::move (args), {}, loc)); } +std::unique_ptr +Builder::call (std::unique_ptr &&path, + std::vector> &&args) const +{ + return call (std::unique_ptr ( + new PathInExpression (std::move (path), {}, loc)), + std::move (args)); +} + +std::unique_ptr +Builder::call (std::unique_ptr &&path, std::unique_ptr &&arg) const +{ + auto args = std::vector> (); + args.emplace_back (std::move (arg)); + + return call (std::move (path), std::move (args)); +} + +std::unique_ptr +Builder::call (std::unique_ptr &&path, std::unique_ptr &&arg) const +{ + auto args = std::vector> (); + args.emplace_back (std::move (arg)); + + return call (std::move (path), std::move (args)); +} + std::unique_ptr Builder::array (std::vector> &&members) const { @@ -56,6 +84,13 @@ Builder::identifier (std::string name) const return std::unique_ptr (new IdentifierExpr (name, {}, loc)); } +std::unique_ptr +Builder::identifier_pattern (std::string name, bool mut) const +{ + return std::unique_ptr ( + new IdentifierPattern (name, loc, false, mut)); +} + std::unique_ptr Builder::tuple_idx (std::string receiver, int idx) const { @@ -117,6 +152,22 @@ Builder::path_in_expression (std::vector &&segments) const return PathInExpression (std::move (path_segments), {}, loc); } +PathInExpression +Builder::path_in_expression (LangItem::Kind lang_item) const +{ + return PathInExpression (lang_item, {}, loc); +} + +std::unique_ptr +Builder::block (std::unique_ptr &&stmt, + std::unique_ptr &&tail_expr) const +{ + auto stmts = std::vector> (); + stmts.emplace_back (std::move (stmt)); + + return block (std::move (stmts), std::move (tail_expr)); +} + std::unique_ptr Builder::block (std::vector> &&stmts, std::unique_ptr &&tail_expr) const @@ -189,6 +240,46 @@ Builder::wildcard () const return std::unique_ptr (new WildcardPattern (loc)); } +std::unique_ptr +Builder::lang_item_path (LangItem::Kind kind) const +{ + return std::unique_ptr (new LangItemPath (kind, loc)); +} + +std::unique_ptr +Builder::match (std::unique_ptr &&scrutinee, + std::vector &&cases) +{ + return std::unique_ptr ( + new MatchExpr (std::move (scrutinee), std::move (cases), {}, {}, loc)); +} + +MatchArm +Builder::match_arm (std::unique_ptr &&pattern) +{ + auto patterns = std::vector> (); + patterns.emplace_back (std::move (pattern)); + + return MatchArm (std::move (patterns), loc); +} + +MatchCase +Builder::match_case (std::unique_ptr &&pattern, + std::unique_ptr &&expr) +{ + return MatchCase (match_arm (std::move (pattern)), std::move (expr)); +} + +std::unique_ptr +Builder::loop (std::vector> &&stmts) +{ + auto block = std::unique_ptr ( + new BlockExpr (std::move (stmts), nullptr, {}, {}, LoopLabel::error (), loc, + loc)); + + return std::unique_ptr (new LoopExpr (std::move (block), 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 fa258c7dfa8..85469097fb5 100644 --- a/gcc/rust/ast/rust-ast-builder.h +++ b/gcc/rust/ast/rust-ast-builder.h @@ -20,6 +20,7 @@ #define AST_BUILDER_H #include "rust-ast-full.h" +#include "rust-expr.h" namespace Rust { namespace AST { @@ -38,6 +39,8 @@ class Builder /* Create an identifier expression (`variable`) */ std::unique_ptr identifier (std::string name) const; + std::unique_ptr identifier_pattern (std::string name, + bool mut = false) const; /* Create a tuple index expression (`receiver.0`) */ std::unique_ptr tuple_idx (std::string receiver, int idx) const; @@ -53,6 +56,9 @@ class Builder std::unique_ptr block (std::vector> &&stmts, std::unique_ptr &&tail_expr = nullptr) const; + std::unique_ptr block (std::unique_ptr &&stmt, + std::unique_ptr &&tail_expr + = nullptr) const; /* Create a let binding with an optional type and initializer (`let : * = `) */ @@ -66,6 +72,12 @@ class Builder */ std::unique_ptr call (std::unique_ptr &&path, std::vector> &&args) const; + std::unique_ptr call (std::unique_ptr &&path, + std::vector> &&args) const; + std::unique_ptr call (std::unique_ptr &&path, + std::unique_ptr &&arg) const; + std::unique_ptr call (std::unique_ptr &&path, + std::unique_ptr &&arg) const; /** * Create an array expression (`[member0, member1, member2]`) @@ -100,6 +112,11 @@ class Builder PathInExpression path_in_expression (std::vector &&segments) const; + /** + * Create a path in expression from a lang item. + */ + PathInExpression path_in_expression (LangItem::Kind lang_item) const; + /* Create a struct expression for unit structs (`S`) */ std::unique_ptr struct_expr_struct (std::string struct_name) const; @@ -122,6 +139,19 @@ class Builder /* Create a wildcard pattern (`_`) */ std::unique_ptr wildcard () const; + /* Create a lang item path usable as a general path */ + std::unique_ptr lang_item_path (LangItem::Kind) const; + + /* Create match expressions and their components */ + std::unique_ptr match (std::unique_ptr &&scrutinee, + std::vector &&cases); + MatchArm match_arm (std::unique_ptr &&pattern); + MatchCase match_case (std::unique_ptr &&pattern, + std::unique_ptr &&expr); + + /* Create a loop expression */ + std::unique_ptr loop (std::vector> &&stmts); + static std::unique_ptr new_type (Type &type); static std::unique_ptr diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h index 93171321ea9..09d15165a8c 100644 --- a/gcc/rust/ast/rust-path.h +++ b/gcc/rust/ast/rust-path.h @@ -715,6 +715,24 @@ class PathInExpression : public Pattern, public ExprWithoutBlock marked_for_strip (false) {} + PathInExpression (LangItem::Kind lang_item_kind, + std::vector outer_attrs, location_t locus) + : outer_attrs (std::move (outer_attrs)), + has_opening_scope_resolution (false), locus (locus), + _node_id (Analysis::Mappings::get ().get_next_node_id ()), + path (Rust::make_unique (lang_item_kind, locus)), + marked_for_strip (false) + {} + + PathInExpression (std::unique_ptr path, + std::vector outer_attrs, location_t locus, + bool has_opening_scope_resolution = false) + : outer_attrs (std::move (outer_attrs)), + has_opening_scope_resolution (has_opening_scope_resolution), + locus (locus), _node_id (Analysis::Mappings::get ().get_next_node_id ()), + path (std::move (path)), marked_for_strip (false) + {} + PathInExpression (const PathInExpression &other) : outer_attrs (other.outer_attrs), has_opening_scope_resolution (other.has_opening_scope_resolution), @@ -738,7 +756,8 @@ class PathInExpression : public Pattern, public ExprWithoutBlock // Creates an error state path in expression. static PathInExpression create_error () { - return PathInExpression ({}, {}, UNDEF_LOCATION); + return PathInExpression (std::vector (), {}, + UNDEF_LOCATION); } // Returns whether path in expression is in an error state.