From 870dd9d5d3c5180efb147f93ed6ae454605c9037 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Mon, 17 Oct 2022 16:51:05 +0100 Subject: 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. --- gcc/rust/hir/tree/rust-hir-expr.h | 213 ++++++++++++++++---------------------- 1 file changed, 89 insertions(+), 124 deletions(-) (limited to 'gcc/rust/hir/tree/rust-hir-expr.h') 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 outer_attrs; std::unique_ptr pattern; - - // bool has_type_given; std::unique_ptr 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 param_pattern, - std::unique_ptr param_type = nullptr) - : pattern (std::move (param_pattern)), type (std::move (param_type)) + ClosureParam (std::unique_ptr param_pattern, Location locus, + std::unique_ptr param_type = nullptr, + std::vector 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 &get_outer_attrs () const + { + return outer_attrs; + } + std::vector &get_outer_attrs () { return outer_attrs; } - std::string as_string () const; + std::unique_ptr &get_pattern () + { + rust_assert (pattern != nullptr); + return pattern; + } + + std::unique_ptr &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 params; Location locus; + std::unique_ptr return_type; + std::unique_ptr expr; -protected: +public: ClosureExpr (Analysis::NodeMapping mappings, - std::vector closure_params, bool has_move, + std::vector closure_params, + std::unique_ptr closure_return_type, + std::unique_ptr 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 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 closure_inner_expr, - std::vector 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 &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 &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 return_type; - std::unique_ptr - 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 closure_return_type, - std::unique_ptr closure_expr, - std::vector 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 { -- cgit v1.1