From 595eacc9931b2e4cee22c052eca85059358a6961 Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Thu, 2 Feb 2023 11:38:44 -0500 Subject: Optimize HIR::ReferencePattern gcc/rust/ChangeLog: * hir/tree/rust-hir-pattern.h (class ReferencePattern): Remove has_two_amps field. * hir/tree/rust-hir-full-test.cc (ReferencePattern::as_string): Remove usage of ReferencePattern::has_two_amps. Signed-off-by: Owen Avery --- gcc/rust/hir/tree/rust-hir-full-test.cc | 5 ----- gcc/rust/hir/tree/rust-hir-pattern.h | 13 +++++-------- 2 files changed, 5 insertions(+), 13 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/hir/tree/rust-hir-full-test.cc b/gcc/rust/hir/tree/rust-hir-full-test.cc index a36e0f3..cd09249 100644 --- a/gcc/rust/hir/tree/rust-hir-full-test.cc +++ b/gcc/rust/hir/tree/rust-hir-full-test.cc @@ -2583,11 +2583,6 @@ ReferencePattern::as_string () const { std::string str ("&"); - if (has_two_amps) - { - str += "&"; - } - if (is_mut ()) { str += "mut "; diff --git a/gcc/rust/hir/tree/rust-hir-pattern.h b/gcc/rust/hir/tree/rust-hir-pattern.h index d994e37..3eea456 100644 --- a/gcc/rust/hir/tree/rust-hir-pattern.h +++ b/gcc/rust/hir/tree/rust-hir-pattern.h @@ -424,7 +424,6 @@ protected: // HIR node for pattern based on dereferencing the pointers given class ReferencePattern : public Pattern { - bool has_two_amps; Mutability mut; std::unique_ptr pattern; Location locus; @@ -435,16 +434,15 @@ public: ReferencePattern (Analysis::NodeMapping mappings, std::unique_ptr pattern, Mutability reference_mut, - bool ref_has_two_amps, Location locus) - : has_two_amps (ref_has_two_amps), mut (reference_mut), - pattern (std::move (pattern)), locus (locus), mappings (mappings) + Location locus) + : mut (reference_mut), pattern (std::move (pattern)), locus (locus), + mappings (mappings) {} // Copy constructor requires clone ReferencePattern (ReferencePattern const &other) - : has_two_amps (other.has_two_amps), mut (other.mut), - pattern (other.pattern->clone_pattern ()), locus (other.locus), - mappings (other.mappings) + : mut (other.mut), pattern (other.pattern->clone_pattern ()), + locus (other.locus), mappings (other.mappings) {} // Overload assignment operator to clone @@ -452,7 +450,6 @@ public: { pattern = other.pattern->clone_pattern (); mut = other.mut; - has_two_amps = other.has_two_amps; locus = other.locus; mappings = other.mappings; -- cgit v1.1 From 1b46cfb35ac4ea7e5e00ac9a332ac7daeeeeb0ba Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Thu, 2 Feb 2023 00:05:36 -0500 Subject: Implement lowering ReferencePattern from AST to HIR gcc/rust/ChangeLog: * ast/rust-pattern.h: (ReferencePattern::is_double_reference): Add method. (ReferencePattern::get_is_mut): Add method. * hir/rust-ast-lower-pattern.cc (ASTLoweringPattern::visit): Add ReferencePattern visitor. * hir/rust-ast-lower-pattern.h: (ASTLoweringPattern::visit): Add ReferencePattern visitor. Signed-off-by: Owen Avery --- gcc/rust/ast/rust-pattern.h | 4 ++++ gcc/rust/hir/rust-ast-lower-pattern.cc | 29 +++++++++++++++++++++++++++++ gcc/rust/hir/rust-ast-lower-pattern.h | 1 + 3 files changed, 34 insertions(+) (limited to 'gcc') diff --git a/gcc/rust/ast/rust-pattern.h b/gcc/rust/ast/rust-pattern.h index 247af5d..1337bc0 100644 --- a/gcc/rust/ast/rust-pattern.h +++ b/gcc/rust/ast/rust-pattern.h @@ -469,6 +469,10 @@ public: return pattern; } + bool is_double_reference () const { return has_two_amps; } + + bool get_is_mut () const { return is_mut; } + NodeId get_node_id () const { return node_id; } NodeId get_pattern_node_id () const override final { return node_id; } diff --git a/gcc/rust/hir/rust-ast-lower-pattern.cc b/gcc/rust/hir/rust-ast-lower-pattern.cc index d13af46..9f67392 100644 --- a/gcc/rust/hir/rust-ast-lower-pattern.cc +++ b/gcc/rust/hir/rust-ast-lower-pattern.cc @@ -259,5 +259,34 @@ ASTLoweringPattern::visit (AST::GroupedPattern &pattern) pattern.get_locus ()); } +void +ASTLoweringPattern::visit (AST::ReferencePattern &pattern) +{ + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, pattern.get_node_id (), + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + + HIR::Pattern *inner + = ASTLoweringPattern::translate (pattern.get_referenced_pattern ().get ()); + + translated + = new HIR::ReferencePattern (mapping, std::unique_ptr (inner), + pattern.get_is_mut () ? Mutability::Mut + : Mutability::Imm, + pattern.get_locus ()); + + if (pattern.is_double_reference ()) + { + Analysis::NodeMapping mapping2 (crate_num, pattern.get_node_id (), + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + translated + = new HIR::ReferencePattern (mapping2, + std::unique_ptr (translated), + Mutability::Imm, pattern.get_locus ()); + } +} + } // namespace HIR } // namespace Rust diff --git a/gcc/rust/hir/rust-ast-lower-pattern.h b/gcc/rust/hir/rust-ast-lower-pattern.h index 20a5529..c7ef31a 100644 --- a/gcc/rust/hir/rust-ast-lower-pattern.h +++ b/gcc/rust/hir/rust-ast-lower-pattern.h @@ -40,6 +40,7 @@ public: void visit (AST::LiteralPattern &pattern) override; void visit (AST::RangePattern &pattern) override; void visit (AST::GroupedPattern &pattern) override; + void visit (AST::ReferencePattern &pattern) override; private: ASTLoweringPattern (); -- cgit v1.1