From c4969068c9456b2559b3a7d056992177b23dc310 Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 7 Mar 2023 20:32:04 +0000 Subject: gccrs: hir: add a helper function for visit This commit adds a helper function for TypeCheckPattern::visit (HIR::RangePattern &pattern) to remove redundancy gcc/rust/ChangeLog: * typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): rewrite part code to helper function (TypeCheckPattern::typecheck_range_pattern_bound): helper function * typecheck/rust-hir-type-check-pattern.h (TypeCheckPattern::typecheck_range_pattern_bound): change the parameter of the function Signed-off-by: Jiakun Fan --- gcc/rust/typecheck/rust-hir-type-check-pattern.cc | 110 +++++++++------------- gcc/rust/typecheck/rust-hir-type-check-pattern.h | 7 +- 2 files changed, 47 insertions(+), 70 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc index b277a56..e4cf1a8 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc @@ -321,73 +321,13 @@ TypeCheckPattern::visit (HIR::RangePattern &pattern) // Resolve the upper and lower bounds, and ensure they are compatible types TyTy::BaseType *upper = nullptr, *lower = nullptr; - // TODO: It would be nice to factor this out into a helper since the logic for - // both bounds is exactly the same... - switch (pattern.get_upper_bound ()->get_bound_type ()) - { - case HIR::RangePatternBound::RangePatternBoundType::LITERAL: { - HIR::RangePatternBoundLiteral &ref - = *static_cast ( - pattern.get_upper_bound ().get ()); - - HIR::Literal lit = ref.get_literal (); - - upper = resolve_literal (pattern.get_pattern_mappings (), lit, - pattern.get_locus ()); - } - break; - - case HIR::RangePatternBound::RangePatternBoundType::PATH: { - HIR::RangePatternBoundPath &ref - = *static_cast ( - pattern.get_upper_bound ().get ()); - - upper = TypeCheckExpr::Resolve (&ref.get_path ()); - } - break; - - case HIR::RangePatternBound::RangePatternBoundType::QUALPATH: { - HIR::RangePatternBoundQualPath &ref - = *static_cast ( - pattern.get_upper_bound ().get ()); - - upper = TypeCheckExpr::Resolve (&ref.get_qualified_path ()); - } - break; - } - - switch (pattern.get_lower_bound ()->get_bound_type ()) - { - case HIR::RangePatternBound::RangePatternBoundType::LITERAL: { - HIR::RangePatternBoundLiteral &ref - = *static_cast ( - pattern.get_lower_bound ().get ()); - - HIR::Literal lit = ref.get_literal (); - - lower = resolve_literal (pattern.get_pattern_mappings (), lit, - pattern.get_locus ()); - } - break; - - case HIR::RangePatternBound::RangePatternBoundType::PATH: { - HIR::RangePatternBoundPath &ref - = *static_cast ( - pattern.get_lower_bound ().get ()); - - lower = TypeCheckExpr::Resolve (&ref.get_path ()); - } - break; + upper = typecheck_range_pattern_bound (pattern.get_upper_bound (), + pattern.get_pattern_mappings (), + pattern.get_locus ()); - case HIR::RangePatternBound::RangePatternBoundType::QUALPATH: { - HIR::RangePatternBoundQualPath &ref - = *static_cast ( - pattern.get_lower_bound ().get ()); - - lower = TypeCheckExpr::Resolve (&ref.get_qualified_path ()); - } - break; - } + lower = typecheck_range_pattern_bound (pattern.get_lower_bound (), + pattern.get_pattern_mappings (), + pattern.get_locus ()); infered = unify_site (pattern.get_pattern_mappings ().get_hirid (), TyTy::TyWithLocation (upper), @@ -442,6 +382,44 @@ TypeCheckPattern::emit_pattern_size_error (const HIR::Pattern &pattern, got_field_count == 1 ? "element" : "elements"); } +TyTy::BaseType * +TypeCheckPattern::typecheck_range_pattern_bound ( + std::unique_ptr &bound, + Analysis::NodeMapping mappings, Location locus) +{ + TyTy::BaseType *resolved_bound = nullptr; + switch (bound->get_bound_type ()) + { + case HIR::RangePatternBound::RangePatternBoundType::LITERAL: { + HIR::RangePatternBoundLiteral &ref + = *static_cast (bound.get ()); + + HIR::Literal lit = ref.get_literal (); + + resolved_bound = resolve_literal (mappings, lit, locus); + } + break; + + case HIR::RangePatternBound::RangePatternBoundType::PATH: { + HIR::RangePatternBoundPath &ref + = *static_cast (bound.get ()); + + resolved_bound = TypeCheckExpr::Resolve (&ref.get_path ()); + } + break; + + case HIR::RangePatternBound::RangePatternBoundType::QUALPATH: { + HIR::RangePatternBoundQualPath &ref + = *static_cast (bound.get ()); + + resolved_bound = TypeCheckExpr::Resolve (&ref.get_qualified_path ()); + } + break; + } + + return resolved_bound; +} + void TypeCheckPattern::visit (HIR::AltPattern &pattern) { diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.h b/gcc/rust/typecheck/rust-hir-type-check-pattern.h index 7b10c28..dcec03a 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-pattern.h +++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.h @@ -47,10 +47,9 @@ public: private: TypeCheckPattern (TyTy::BaseType *parent); - static TyTy::BaseType * - typecheck_range_pattern_bound (HIR::RangePatternBound *bound, - Analysis::NodeMapping mappings, - Location locus); + TyTy::BaseType *typecheck_range_pattern_bound ( + std::unique_ptr &bound, + Analysis::NodeMapping mappings, Location locus); void emit_pattern_size_error (const HIR::Pattern &pattern, size_t expected_field_count, -- cgit v1.1