diff options
author | Philip Herron <philip.herron@embecosm.com> | 2022-10-17 16:51:05 +0100 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2023-02-21 12:36:38 +0100 |
commit | 870dd9d5d3c5180efb147f93ed6ae454605c9037 (patch) | |
tree | de7e47f3cf7cfb997a89bb50947658ca4389c53c /gcc/rust/hir/tree | |
parent | ed75e142a7082b2f4f0a50a39939566c53b5697b (diff) | |
download | gcc-870dd9d5d3c5180efb147f93ed6ae454605c9037.zip gcc-870dd9d5d3c5180efb147f93ed6ae454605c9037.tar.gz gcc-870dd9d5d3c5180efb147f93ed6ae454605c9037.tar.bz2 |
gccrs: Refactor expression hir lowering into cc file
gcc/rust/ChangeLog:
* Make-lang.in: Add new object file for expression lowering.
* ast/rust-expr.h: Move implementation of expr lowering to source file.
* backend/rust-compile-block.h: Likewise.
* backend/rust-compile-expr.cc (CompileExpr::visit): Likewise.
* backend/rust-compile-expr.h: Likewise.
* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::visit): Likewise.
* checks/errors/privacy/rust-privacy-reporter.h: Likewise.
* checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise.
* checks/errors/rust-const-checker.h: Likewise.
* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Likewise.
* checks/errors/rust-unsafe-checker.h: Likewise.
* hir/rust-ast-lower-base.h: Likewise.
* hir/rust-ast-lower-expr.h (RUST_AST_LOWER_EXPR): Likewise.
* hir/rust-ast-lower.cc (ASTLoweringBase::lower_closure_param): Likewise.
* hir/rust-hir-dump.cc (Dump::visit): Likewise.
* hir/rust-hir-dump.h: Likewise.
* hir/tree/rust-hir-expr.h (class ClosureExpr): Likewise.
(class ClosureExprInner): Likewise.
(class ClosureExprInnerTyped): Likewise.
* hir/tree/rust-hir-full-decls.h (class ClosureExprInner): Likewise.
(class ClosureExprInnerTyped): Likewise.
* hir/tree/rust-hir-full-test.cc (ClosureExprInnerTyped::as_string): Likewise.
(ClosureExprInner::as_string): Likewise.
(ClosureExprInner::accept_vis): Likewise.
(ClosureExpr::accept_vis): Likewise.
(ClosureExprInnerTyped::accept_vis): Likewise.
* hir/tree/rust-hir-visitor.h: Likewise.
* hir/tree/rust-hir.h (class Expr): Likewise.
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Likewise.
* typecheck/rust-hir-type-check-expr.h: Likewise.
* hir/rust-ast-lower-expr.cc: New file.
Diffstat (limited to 'gcc/rust/hir/tree')
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-expr.h | 213 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-full-decls.h | 2 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-full-test.cc | 37 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-visitor.h | 9 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir.h | 1 |
5 files changed, 97 insertions, 165 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 { diff --git a/gcc/rust/hir/tree/rust-hir-full-decls.h b/gcc/rust/hir/tree/rust-hir-full-decls.h index 3cb3ffe..70ee753 100644 --- a/gcc/rust/hir/tree/rust-hir-full-decls.h +++ b/gcc/rust/hir/tree/rust-hir-full-decls.h @@ -94,9 +94,7 @@ class MethodCallExpr; class FieldAccessExpr; struct ClosureParam; class ClosureExpr; -class ClosureExprInner; class BlockExpr; -class ClosureExprInnerTyped; class ContinueExpr; class BreakExpr; class RangeExpr; diff --git a/gcc/rust/hir/tree/rust-hir-full-test.cc b/gcc/rust/hir/tree/rust-hir-full-test.cc index 48af16a..f6e27b9 100644 --- a/gcc/rust/hir/tree/rust-hir-full-test.cc +++ b/gcc/rust/hir/tree/rust-hir-full-test.cc @@ -1133,15 +1133,8 @@ ClosureExpr::as_string () const } } - return str; -} - -std::string -ClosureExprInnerTyped::as_string () const -{ - std::string str = ClosureExpr::as_string (); - - str += "\n Return type: " + return_type->as_string (); + str += "\n Return type: " + + (has_return_type () ? return_type->as_string () : "none"); str += "\n Body: " + expr->as_string (); @@ -1516,16 +1509,6 @@ UnsafeBlockExpr::as_string () const } std::string -ClosureExprInner::as_string () const -{ - std::string str = ClosureExpr::as_string (); - - str += "\n Expression: " + closure_inner->as_string (); - - return str; -} - -std::string IfExpr::as_string () const { std::string str ("IfExpr: "); @@ -4038,7 +4021,7 @@ FieldAccessExpr::accept_vis (HIRFullVisitor &vis) } void -ClosureExprInner::accept_vis (HIRFullVisitor &vis) +ClosureExpr::accept_vis (HIRFullVisitor &vis) { vis.visit (*this); } @@ -4050,12 +4033,6 @@ BlockExpr::accept_vis (HIRFullVisitor &vis) } void -ClosureExprInnerTyped::accept_vis (HIRFullVisitor &vis) -{ - vis.visit (*this); -} - -void ContinueExpr::accept_vis (HIRFullVisitor &vis) { vis.visit (*this); @@ -4986,7 +4963,7 @@ IfExpr::accept_vis (HIRExpressionVisitor &vis) } void -ClosureExprInner::accept_vis (HIRExpressionVisitor &vis) +ClosureExpr::accept_vis (HIRExpressionVisitor &vis) { vis.visit (*this); } @@ -5076,12 +5053,6 @@ QualifiedPathInExpression::accept_vis (HIRPatternVisitor &vis) } void -ClosureExprInnerTyped::accept_vis (HIRExpressionVisitor &vis) -{ - vis.visit (*this); -} - -void ExprStmtWithBlock::accept_vis (HIRStmtVisitor &vis) { vis.visit (*this); diff --git a/gcc/rust/hir/tree/rust-hir-visitor.h b/gcc/rust/hir/tree/rust-hir-visitor.h index 187f079..ba6cad7 100644 --- a/gcc/rust/hir/tree/rust-hir-visitor.h +++ b/gcc/rust/hir/tree/rust-hir-visitor.h @@ -63,9 +63,8 @@ public: virtual void visit (CallExpr &expr) = 0; virtual void visit (MethodCallExpr &expr) = 0; virtual void visit (FieldAccessExpr &expr) = 0; - virtual void visit (ClosureExprInner &expr) = 0; virtual void visit (BlockExpr &expr) = 0; - virtual void visit (ClosureExprInnerTyped &expr) = 0; + virtual void visit (ClosureExpr &expr) = 0; virtual void visit (ContinueExpr &expr) = 0; virtual void visit (BreakExpr &expr) = 0; virtual void visit (RangeFromToExpr &expr) = 0; @@ -206,9 +205,8 @@ public: virtual void visit (CallExpr &) override {} virtual void visit (MethodCallExpr &) override {} virtual void visit (FieldAccessExpr &) override {} - virtual void visit (ClosureExprInner &) override {} + virtual void visit (ClosureExpr &) override {} virtual void visit (BlockExpr &) override {} - virtual void visit (ClosureExprInnerTyped &) override {} virtual void visit (ContinueExpr &) override {} virtual void visit (BreakExpr &) override {} virtual void visit (RangeFromToExpr &) override {} @@ -419,8 +417,7 @@ public: virtual void visit (HIR::QualifiedPathInExpression &expr) = 0; virtual void visit (HIR::PathInExpression &expr) = 0; - virtual void visit (ClosureExprInnerTyped &) = 0; - virtual void visit (ClosureExprInner &expr) = 0; + virtual void visit (ClosureExpr &) = 0; virtual void visit (StructExprStructFields &) = 0; virtual void visit (StructExprStruct &) = 0; virtual void visit (LiteralExpr &expr) = 0; diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h index aa305f1..314aafb 100644 --- a/gcc/rust/hir/tree/rust-hir.h +++ b/gcc/rust/hir/tree/rust-hir.h @@ -238,6 +238,7 @@ class ExprWithoutBlock; // Base expression HIR node - abstract class Expr : public Node { +protected: AST::AttrVec outer_attrs; Analysis::NodeMapping mappings; |