Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast-builder: Add more methods #3322

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 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,13 @@ 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 +152,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 +240,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
30 changes: 30 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,8 @@ 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 +56,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 +72,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 +112,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 +139,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
21 changes: 20 additions & 1 deletion gcc/rust/ast/rust-path.h
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,24 @@ class PathInExpression : public Pattern, public ExprWithoutBlock
marked_for_strip (false)
{}

PathInExpression (LangItem::Kind lang_item_kind,
std::vector<Attribute> 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<LangItemPath> (lang_item_kind, locus)),
marked_for_strip (false)
{}

PathInExpression (std::unique_ptr<Path> path,
std::vector<Attribute> 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),
Expand All @@ -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<PathExprSegment> (), {},
UNDEF_LOCATION);
}

// Returns whether path in expression is in an error state.
Expand Down
Loading