Skip to content

Commit

Permalink
ast-builder: Add more methods
Browse files Browse the repository at this point in the history
This commit adds new methods for building pattern nodes, match expressions and more precise call expressions.

gcc/rust/ChangeLog:

	* ast/rust-ast-builder.cc: Add new functions.
	* ast/rust-ast-builder.h: Declare them.
  • Loading branch information
CohenArthur committed Dec 24, 2024
1 parent d28cae5 commit d2045eb
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
90 changes: 90 additions & 0 deletions gcc/rust/ast/rust-ast-builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -42,6 +43,33 @@ Builder::call (std::unique_ptr<Expr> &&path,
new CallExpr (std::move (path), std::move (args), {}, loc));
}

std::unique_ptr<Expr>
Builder::call (std::unique_ptr<Path> &&path,
std::vector<std::unique_ptr<Expr>> &&args) const
{
return call (std::unique_ptr<Expr> (
new PathInExpression (std::move (path), {}, loc)),
std::move (args));
}

std::unique_ptr<Expr>
Builder::call (std::unique_ptr<Expr> &&path, std::unique_ptr<Expr> &&arg) const
{
auto args = std::vector<std::unique_ptr<Expr>> ();
args.emplace_back (std::move (arg));

return call (std::move (path), std::move (args));
}

std::unique_ptr<Expr>
Builder::call (std::unique_ptr<Path> &&path, std::unique_ptr<Expr> &&arg) const
{
auto args = std::vector<std::unique_ptr<Expr>> ();
args.emplace_back (std::move (arg));

return call (std::move (path), std::move (args));
}

std::unique_ptr<Expr>
Builder::array (std::vector<std::unique_ptr<Expr>> &&members) const
{
Expand All @@ -56,6 +84,12 @@ Builder::identifier (std::string name) const
return std::unique_ptr<Expr> (new IdentifierExpr (name, {}, loc));
}

std::unique_ptr<Pattern>
Builder::identifier_pattern (std::string name, bool mut) const
{
return std::unique_ptr<Pattern> (new IdentifierPattern (name, loc, false, mut));
}

std::unique_ptr<Expr>
Builder::tuple_idx (std::string receiver, int idx) const
{
Expand Down Expand Up @@ -117,6 +151,22 @@ Builder::path_in_expression (std::vector<std::string> &&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<Expr>
Builder::block (std::unique_ptr<Stmt> &&stmt,
std::unique_ptr<Expr> &&tail_expr) const
{
auto stmts = std::vector<std::unique_ptr<Stmt>>();
stmts.emplace_back(std::move(stmt));

return block(std::move(stmts), std::move(tail_expr));
}

std::unique_ptr<Expr>
Builder::block (std::vector<std::unique_ptr<Stmt>> &&stmts,
std::unique_ptr<Expr> &&tail_expr) const
Expand Down Expand Up @@ -189,6 +239,46 @@ Builder::wildcard () const
return std::unique_ptr<Pattern> (new WildcardPattern (loc));
}

std::unique_ptr<Path>
Builder::lang_item_path (LangItem::Kind kind) const
{
return std::unique_ptr<Path> (new LangItemPath (kind, loc));
}

std::unique_ptr<Expr>
Builder::match (std::unique_ptr<Expr> &&scrutinee,
std::vector<MatchCase> &&cases)
{
return std::unique_ptr<Expr> (
new MatchExpr (std::move (scrutinee), std::move (cases), {}, {}, loc));
}

MatchArm
Builder::match_arm (std::unique_ptr<Pattern> &&pattern)
{
auto patterns = std::vector<std::unique_ptr<Pattern>> ();
patterns.emplace_back (std::move (pattern));

return MatchArm (std::move (patterns), loc);
}

MatchCase
Builder::match_case (std::unique_ptr<Pattern> &&pattern,
std::unique_ptr<Expr> &&expr)
{
return MatchCase (match_arm (std::move (pattern)), std::move (expr));
}

std::unique_ptr<Expr>
Builder::loop (std::vector<std::unique_ptr<Stmt>> &&stmts)
{
auto block = std::unique_ptr<BlockExpr> (
new BlockExpr (std::move (stmts), nullptr, {}, {}, LoopLabel::error (), loc,
loc));

return std::unique_ptr<Expr> (new LoopExpr (std::move (block), loc));
}

std::unique_ptr<Type>
Builder::new_type (Type &type)
{
Expand Down
29 changes: 29 additions & 0 deletions gcc/rust/ast/rust-ast-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define AST_BUILDER_H

#include "rust-ast-full.h"
#include "rust-expr.h"

namespace Rust {
namespace AST {
Expand All @@ -38,6 +39,7 @@ class Builder

/* Create an identifier expression (`variable`) */
std::unique_ptr<Expr> identifier (std::string name) const;
std::unique_ptr<Pattern> identifier_pattern (std::string name, bool mut = false) const;

/* Create a tuple index expression (`receiver.0`) */
std::unique_ptr<Expr> tuple_idx (std::string receiver, int idx) const;
Expand All @@ -53,6 +55,9 @@ class Builder
std::unique_ptr<Expr> block (std::vector<std::unique_ptr<Stmt>> &&stmts,
std::unique_ptr<Expr> &&tail_expr
= nullptr) const;
std::unique_ptr<Expr> block (std::unique_ptr<Stmt> &&stmt,
std::unique_ptr<Expr> &&tail_expr
= nullptr) const;

/* Create a let binding with an optional type and initializer (`let <name> :
* <type> = <init>`) */
Expand All @@ -66,6 +71,12 @@ class Builder
*/
std::unique_ptr<Expr> call (std::unique_ptr<Expr> &&path,
std::vector<std::unique_ptr<Expr>> &&args) const;
std::unique_ptr<Expr> call (std::unique_ptr<Path> &&path,
std::vector<std::unique_ptr<Expr>> &&args) const;
std::unique_ptr<Expr> call (std::unique_ptr<Expr> &&path,
std::unique_ptr<Expr> &&arg) const;
std::unique_ptr<Expr> call (std::unique_ptr<Path> &&path,
std::unique_ptr<Expr> &&arg) const;

/**
* Create an array expression (`[member0, member1, member2]`)
Expand Down Expand Up @@ -100,6 +111,11 @@ class Builder
PathInExpression
path_in_expression (std::vector<std::string> &&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<Expr> struct_expr_struct (std::string struct_name) const;

Expand All @@ -122,6 +138,19 @@ class Builder
/* Create a wildcard pattern (`_`) */
std::unique_ptr<Pattern> wildcard () const;

/* Create a lang item path usable as a general path */
std::unique_ptr<Path> lang_item_path (LangItem::Kind) const;

/* Create match expressions and their components */
std::unique_ptr<Expr> match (std::unique_ptr<Expr> &&scrutinee,
std::vector<MatchCase> &&cases);
MatchArm match_arm (std::unique_ptr<Pattern> &&pattern);
MatchCase match_case (std::unique_ptr<Pattern> &&pattern,
std::unique_ptr<Expr> &&expr);

/* Create a loop expression */
std::unique_ptr<Expr> loop (std::vector<std::unique_ptr<Stmt>> &&stmts);

static std::unique_ptr<Type> new_type (Type &type);

static std::unique_ptr<GenericParam>
Expand Down

0 comments on commit d2045eb

Please sign in to comment.