diff options
author | vincent <jfan30@u.rochester.edu> | 2023-03-07 20:32:04 +0000 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2023-03-09 13:54:25 +0000 |
commit | 6f9938c712d6d8645714ee21d5850342281022e1 (patch) | |
tree | bd8a4c71087589ca4fe866824384e00437b5f07c | |
parent | 5bd4b19068c8ab8b8324fd3b8f20c4c3cb5cd277 (diff) | |
download | gcc-6f9938c712d6d8645714ee21d5850342281022e1.zip gcc-6f9938c712d6d8645714ee21d5850342281022e1.tar.gz gcc-6f9938c712d6d8645714ee21d5850342281022e1.tar.bz2 |
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 <jfan30@u.rochester.edu>
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-pattern.cc | 110 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-pattern.h | 7 |
2 files changed, 47 insertions, 70 deletions
diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc index e7ed542..cd0dbef 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<HIR::RangePatternBoundLiteral *> ( - 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<HIR::RangePatternBoundPath *> ( - pattern.get_upper_bound ().get ()); - - upper = TypeCheckExpr::Resolve (&ref.get_path ()); - } - break; - - case HIR::RangePatternBound::RangePatternBoundType::QUALPATH: { - HIR::RangePatternBoundQualPath &ref - = *static_cast<HIR::RangePatternBoundQualPath *> ( - 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<HIR::RangePatternBoundLiteral *> ( - 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<HIR::RangePatternBoundPath *> ( - 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<HIR::RangePatternBoundQualPath *> ( - 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<Rust::HIR::RangePatternBound> &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<HIR::RangePatternBoundLiteral *> (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<HIR::RangePatternBoundPath *> (bound.get ()); + + resolved_bound = TypeCheckExpr::Resolve (&ref.get_path ()); + } + break; + + case HIR::RangePatternBound::RangePatternBoundType::QUALPATH: { + HIR::RangePatternBoundQualPath &ref + = *static_cast<HIR::RangePatternBoundQualPath *> (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 3b392b5..d5d0fd0 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<Rust::HIR::RangePatternBound> &bound, + Analysis::NodeMapping mappings, Location locus); void emit_pattern_size_error (const HIR::Pattern &pattern, size_t expected_field_count, |