aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/hir/tree/rust-hir-expr.h
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/rust/hir/tree/rust-hir-expr.h')
-rw-r--r--gcc/rust/hir/tree/rust-hir-expr.h213
1 files changed, 89 insertions, 124 deletions
diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h
index 564d521..4c5caf1 100644
--- a/gcc/rust/hir/tree/rust-hir-expr.h
+++ b/gcc/rust/hir/tree/rust-hir-expr.h
@@ -1985,21 +1985,22 @@ protected:
struct ClosureParam
{
private:
+ std::vector<AST::Attribute> outer_attrs;
std::unique_ptr<Pattern> pattern;
-
- // bool has_type_given;
std::unique_ptr<Type> type;
-
- // TODO: should this store location data?
+ Location locus;
public:
// Returns whether the type of the parameter has been given.
bool has_type_given () const { return type != nullptr; }
// Constructor for closure parameter
- ClosureParam (std::unique_ptr<Pattern> param_pattern,
- std::unique_ptr<Type> param_type = nullptr)
- : pattern (std::move (param_pattern)), type (std::move (param_type))
+ ClosureParam (std::unique_ptr<Pattern> param_pattern, Location locus,
+ std::unique_ptr<Type> param_type = nullptr,
+ std::vector<AST::Attribute> outer_attrs = {})
+ : outer_attrs (std::move (outer_attrs)),
+ pattern (std::move (param_pattern)), type (std::move (param_type)),
+ locus (locus)
{}
// Copy constructor required due to cloning as a result of unique_ptrs
@@ -2007,6 +2008,8 @@ public:
: pattern (other.pattern->clone_pattern ())
{
// guard to protect from null pointer dereference
+ if (other.pattern != nullptr)
+ pattern = other.pattern->clone_pattern ();
if (other.type != nullptr)
type = other.type->clone_type ();
}
@@ -2016,8 +2019,17 @@ public:
// Assignment operator must be overloaded to clone as well
ClosureParam &operator= (ClosureParam const &other)
{
- pattern = other.pattern->clone_pattern ();
- type = other.type->clone_type ();
+ outer_attrs = other.outer_attrs;
+
+ // guard to protect from null pointer dereference
+ if (other.pattern != nullptr)
+ pattern = other.pattern->clone_pattern ();
+ else
+ pattern = nullptr;
+ if (other.type != nullptr)
+ type = other.type->clone_type ();
+ else
+ type = nullptr;
return *this;
}
@@ -2026,31 +2038,79 @@ public:
ClosureParam (ClosureParam &&other) = default;
ClosureParam &operator= (ClosureParam &&other) = default;
- // Returns whether closure parameter is in an error state.
- bool is_error () const { return pattern == nullptr; }
+ std::string as_string () const;
- // Creates an error state closure parameter.
- static ClosureParam create_error () { return ClosureParam (nullptr); }
+ const std::vector<AST::Attribute> &get_outer_attrs () const
+ {
+ return outer_attrs;
+ }
+ std::vector<AST::Attribute> &get_outer_attrs () { return outer_attrs; }
- std::string as_string () const;
+ std::unique_ptr<Pattern> &get_pattern ()
+ {
+ rust_assert (pattern != nullptr);
+ return pattern;
+ }
+
+ std::unique_ptr<Type> &get_type ()
+ {
+ rust_assert (has_type_given ());
+ return type;
+ }
+
+ Location get_locus () const { return locus; }
};
// Base closure definition expression HIR node - abstract
class ClosureExpr : public ExprWithoutBlock
{
+private:
bool has_move;
std::vector<ClosureParam> params;
Location locus;
+ std::unique_ptr<Type> return_type;
+ std::unique_ptr<Expr> expr;
-protected:
+public:
ClosureExpr (Analysis::NodeMapping mappings,
- std::vector<ClosureParam> closure_params, bool has_move,
+ std::vector<ClosureParam> closure_params,
+ std::unique_ptr<Type> closure_return_type,
+ std::unique_ptr<Expr> closure_expr, bool has_move,
AST::AttrVec outer_attribs, Location locus)
: ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)),
- has_move (has_move), params (std::move (closure_params)), locus (locus)
+ has_move (has_move), params (std::move (closure_params)), locus (locus),
+ return_type (std::move (closure_return_type)),
+ expr (std::move (closure_expr))
{}
-public:
+ // Copy constructor requires cloning
+ ClosureExpr (ClosureExpr const &other)
+ : ExprWithoutBlock (other.get_mappings (), other.get_outer_attrs ())
+ {
+ return_type
+ = other.has_return_type () ? other.return_type->clone_type () : nullptr;
+ expr = other.expr->clone_expr ();
+ params = other.params;
+ has_move = other.has_move;
+ }
+
+ // Overload assignment operator to clone unique_ptrs
+ ClosureExpr &operator= (ClosureExpr const &other)
+ {
+ mappings = other.mappings;
+ return_type
+ = other.has_return_type () ? other.return_type->clone_type () : nullptr;
+ expr = other.expr->clone_expr ();
+ params = other.params;
+ has_move = other.has_move;
+
+ return *this;
+ }
+
+ // move constructors
+ ClosureExpr (ClosureExpr &&other) = default;
+ ClosureExpr &operator= (ClosureExpr &&other) = default;
+
std::string as_string () const override;
Location get_locus () const override final { return locus; }
@@ -2059,47 +2119,17 @@ public:
{
return ExprType::Closure;
}
-};
-// Represents a non-type-specified closure expression HIR node
-class ClosureExprInner : public ClosureExpr
-{
- std::unique_ptr<Expr> closure_inner;
+ bool get_has_move () const { return has_move; }
-public:
- std::string as_string () const override;
+ bool has_return_type () const { return return_type != nullptr; }
- // Constructor for a ClosureExprInner
- ClosureExprInner (Analysis::NodeMapping mappings,
- std::unique_ptr<Expr> closure_inner_expr,
- std::vector<ClosureParam> closure_params, Location locus,
- bool is_move = false,
- AST::AttrVec outer_attribs = AST::AttrVec ())
- : ClosureExpr (std::move (mappings), std::move (closure_params), is_move,
- std::move (outer_attribs), locus),
- closure_inner (std::move (closure_inner_expr))
- {}
-
- // Copy constructor must be defined to allow copying via cloning of unique_ptr
- ClosureExprInner (ClosureExprInner const &other)
- : ClosureExpr (other), closure_inner (other.closure_inner->clone_expr ())
- {}
-
- // Overload assignment operator to clone closure_inner
- ClosureExprInner &operator= (ClosureExprInner const &other)
+ std::unique_ptr<Type> &get_return_type ()
{
- ClosureExpr::operator= (other);
- closure_inner = other.closure_inner->clone_expr ();
- // params = other.params;
- // has_move = other.has_move;
- // outer_attrs = other.outer_attrs;
-
- return *this;
- }
-
- // move constructors
- ClosureExprInner (ClosureExprInner &&other) = default;
- ClosureExprInner &operator= (ClosureExprInner &&other) = default;
+ rust_assert (has_return_type ());
+ return return_type;
+ };
+ std::unique_ptr<Expr> &get_expr () { return expr; }
void accept_vis (HIRFullVisitor &vis) override;
void accept_vis (HIRExpressionVisitor &vis) override;
@@ -2107,16 +2137,16 @@ public:
protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
- ClosureExprInner *clone_expr_impl () const override
+ ClosureExpr *clone_expr_impl () const override
{
- return new ClosureExprInner (*this);
+ return new ClosureExpr (*this);
}
/* Use covariance to implement clone function as returning this object rather
* than base */
- ClosureExprInner *clone_expr_without_block_impl () const override
+ ClosureExpr *clone_expr_without_block_impl () const override
{
- return new ClosureExprInner (*this);
+ return new ClosureExpr (*this);
}
};
@@ -2239,71 +2269,6 @@ protected:
}
};
-// Represents a type-specified closure expression HIR node
-class ClosureExprInnerTyped : public ClosureExpr
-{
- std::unique_ptr<Type> return_type;
- std::unique_ptr<BlockExpr>
- expr; // only used because may be polymorphic in future
-
-public:
- std::string as_string () const override;
-
- // Constructor potentially with a move
- ClosureExprInnerTyped (Analysis::NodeMapping mappings,
- std::unique_ptr<Type> closure_return_type,
- std::unique_ptr<BlockExpr> closure_expr,
- std::vector<ClosureParam> closure_params,
- Location locus, bool is_move = false,
- AST::AttrVec outer_attribs = AST::AttrVec ())
- : ClosureExpr (std::move (mappings), std::move (closure_params), is_move,
- std::move (outer_attribs), locus),
- return_type (std::move (closure_return_type)),
- expr (std::move (closure_expr))
- {}
-
- // Copy constructor requires cloning
- ClosureExprInnerTyped (ClosureExprInnerTyped const &other)
- : ClosureExpr (other), return_type (other.return_type->clone_type ()),
- expr (other.expr->clone_block_expr ())
- {}
-
- // Overload assignment operator to clone unique_ptrs
- ClosureExprInnerTyped &operator= (ClosureExprInnerTyped const &other)
- {
- ClosureExpr::operator= (other);
- return_type = other.return_type->clone_type ();
- expr = other.expr->clone_block_expr ();
- // params = other.params;
- // has_move = other.has_move;
- // outer_attrs = other.outer_attrs;
-
- return *this;
- }
-
- // move constructors
- ClosureExprInnerTyped (ClosureExprInnerTyped &&other) = default;
- ClosureExprInnerTyped &operator= (ClosureExprInnerTyped &&other) = default;
-
- void accept_vis (HIRFullVisitor &vis) override;
- void accept_vis (HIRExpressionVisitor &vis) override;
-
-protected:
- /* Use covariance to implement clone function as returning this object rather
- * than base */
- ClosureExprInnerTyped *clone_expr_impl () const override
- {
- return new ClosureExprInnerTyped (*this);
- }
-
- /* Use covariance to implement clone function as returning this object rather
- * than base */
- ClosureExprInnerTyped *clone_expr_without_block_impl () const override
- {
- return new ClosureExprInnerTyped (*this);
- }
-};
-
// HIR node representing continue expression within loops
class ContinueExpr : public ExprWithoutBlock
{