diff options
author | Philip Herron <philip.herron@embecosm.com> | 2022-04-20 18:04:23 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2022-04-20 18:04:42 +0100 |
commit | c18257c7265470a071f7ed9fe29899ece839fcf4 (patch) | |
tree | 012c451c43e92f6cf5d29c8a73023515493cee06 | |
parent | 53ce1d5ac9106bc7427f14c285d782c1023124ec (diff) | |
download | gcc-c18257c7265470a071f7ed9fe29899ece839fcf4.zip gcc-c18257c7265470a071f7ed9fe29899ece839fcf4.tar.gz gcc-c18257c7265470a071f7ed9fe29899ece839fcf4.tar.bz2 |
Add HIR Lowering for TuplePattern
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-base.cc | 75 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-base.h | 8 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-expr.h | 36 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-pattern.cc | 45 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-pattern.h | 4 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-expr.h | 4 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-enumitem.h | 6 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-expr.h | 4 |
8 files changed, 143 insertions, 39 deletions
diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc index fa7c700..28895ba 100644 --- a/gcc/rust/hir/rust-ast-lower-base.cc +++ b/gcc/rust/hir/rust-ast-lower-base.cc @@ -18,6 +18,7 @@ #include "rust-ast-lower-base.h" #include "rust-ast-lower-type.h" +#include "rust-ast-lower-pattern.h" namespace Rust { namespace HIR { @@ -885,5 +886,79 @@ ASTLoweringBase::attribute_handled_in_another_pass ( return lookup.handler != Analysis::CompilerPass::HIR_LOWERING; } +std::unique_ptr<HIR::TuplePatternItems> +ASTLoweringBase::lower_tuple_pattern_multiple ( + AST::TuplePatternItemsMultiple &pattern) +{ + std::vector<std::unique_ptr<HIR::Pattern> > patterns; + for (auto &p : pattern.get_patterns ()) + { + HIR::Pattern *translated = ASTLoweringPattern::translate (p.get ()); + patterns.push_back (std::unique_ptr<HIR::Pattern> (translated)); + } + + return std::unique_ptr<HIR::TuplePatternItems> ( + new HIR::TuplePatternItemsMultiple (std::move (patterns))); +} + +std::unique_ptr<TuplePatternItems> +ASTLoweringBase::lower_tuple_pattern_ranged ( + AST::TuplePatternItemsRanged &pattern) +{ + std::vector<std::unique_ptr<HIR::Pattern> > lower_patterns; + std::vector<std::unique_ptr<HIR::Pattern> > upper_patterns; + + for (auto &p : pattern.get_lower_patterns ()) + { + HIR::Pattern *translated = ASTLoweringPattern::translate (p.get ()); + lower_patterns.push_back (std::unique_ptr<HIR::Pattern> (translated)); + } + + for (auto &p : pattern.get_upper_patterns ()) + { + HIR::Pattern *translated = ASTLoweringPattern::translate (p.get ()); + upper_patterns.push_back (std::unique_ptr<HIR::Pattern> (translated)); + } + + return std::unique_ptr<HIR::TuplePatternItems> ( + new HIR::TuplePatternItemsRanged (std::move (lower_patterns), + std::move (upper_patterns))); +} + +HIR::Literal +ASTLoweringBase::lower_literal (const AST::Literal &literal) +{ + HIR::Literal::LitType type = HIR::Literal::LitType::CHAR; + switch (literal.get_lit_type ()) + { + case AST::Literal::LitType::CHAR: + type = HIR::Literal::LitType::CHAR; + break; + case AST::Literal::LitType::STRING: + type = HIR::Literal::LitType::STRING; + break; + case AST::Literal::LitType::BYTE: + type = HIR::Literal::LitType::BYTE; + break; + case AST::Literal::LitType::BYTE_STRING: + type = HIR::Literal::LitType::BYTE_STRING; + break; + case AST::Literal::LitType::INT: + type = HIR::Literal::LitType::INT; + break; + case AST::Literal::LitType::FLOAT: + type = HIR::Literal::LitType::FLOAT; + break; + case AST::Literal::LitType::BOOL: + type = HIR::Literal::LitType::BOOL; + break; + case AST::Literal::LitType::ERROR: + gcc_unreachable (); + break; + } + + return HIR::Literal (literal.as_string (), type, literal.get_type_hint ()); +} + } // namespace HIR } // namespace Rust diff --git a/gcc/rust/hir/rust-ast-lower-base.h b/gcc/rust/hir/rust-ast-lower-base.h index 185f467..33009ae 100644 --- a/gcc/rust/hir/rust-ast-lower-base.h +++ b/gcc/rust/hir/rust-ast-lower-base.h @@ -272,6 +272,14 @@ protected: bool attribute_handled_in_another_pass (const std::string &attribute_path) const; + + std::unique_ptr<TuplePatternItems> + lower_tuple_pattern_multiple (AST::TuplePatternItemsMultiple &pattern); + + std::unique_ptr<TuplePatternItems> + lower_tuple_pattern_ranged (AST::TuplePatternItemsRanged &pattern); + + HIR::Literal lower_literal (const AST::Literal &literal); }; } // namespace HIR diff --git a/gcc/rust/hir/rust-ast-lower-expr.h b/gcc/rust/hir/rust-ast-lower-expr.h index 022002e..5ae5386 100644 --- a/gcc/rust/hir/rust-ast-lower-expr.h +++ b/gcc/rust/hir/rust-ast-lower-expr.h @@ -327,43 +327,15 @@ public: void visit (AST::LiteralExpr &expr) override { - HIR::Literal::LitType type = HIR::Literal::LitType::CHAR; - switch (expr.get_lit_type ()) - { - case AST::Literal::LitType::CHAR: - type = HIR::Literal::LitType::CHAR; - break; - case AST::Literal::LitType::STRING: - type = HIR::Literal::LitType::STRING; - break; - case AST::Literal::LitType::BYTE: - type = HIR::Literal::LitType::BYTE; - break; - case AST::Literal::LitType::BYTE_STRING: - type = HIR::Literal::LitType::BYTE_STRING; - break; - case AST::Literal::LitType::INT: - type = HIR::Literal::LitType::INT; - break; - case AST::Literal::LitType::FLOAT: - type = HIR::Literal::LitType::FLOAT; - break; - case AST::Literal::LitType::BOOL: - type = HIR::Literal::LitType::BOOL; - break; - // Error literals should have been stripped during expansion - case AST::Literal::LitType::ERROR: - gcc_unreachable (); - break; - } auto crate_num = mappings->get_current_crate (); Analysis::NodeMapping mapping (crate_num, expr.get_node_id (), mappings->get_next_hir_id (crate_num), UNKNOWN_LOCAL_DEFID); - translated = new HIR::LiteralExpr (mapping, expr.as_string (), type, - expr.get_literal ().get_type_hint (), - expr.get_locus ()); + HIR::Literal l = lower_literal (expr.get_literal ()); + translated + = new HIR::LiteralExpr (mapping, std::move (l), expr.get_locus (), + expr.get_outer_attrs ()); } void visit (AST::ArithmeticOrLogicalExpr &expr) override diff --git a/gcc/rust/hir/rust-ast-lower-pattern.cc b/gcc/rust/hir/rust-ast-lower-pattern.cc index 9d733f3..957f8cd 100644 --- a/gcc/rust/hir/rust-ast-lower-pattern.cc +++ b/gcc/rust/hir/rust-ast-lower-pattern.cc @@ -163,5 +163,50 @@ ASTLoweringPattern::visit (AST::WildcardPattern &pattern) translated = new HIR::WildcardPattern (mapping, pattern.get_locus ()); } +void +ASTLoweringPattern::visit (AST::TuplePattern &pattern) +{ + std::unique_ptr<HIR::TuplePatternItems> items; + switch (pattern.get_items ()->get_pattern_type ()) + { + case AST::TuplePatternItems::TuplePatternItemType::MULTIPLE: { + AST::TuplePatternItemsMultiple &ref + = *static_cast<AST::TuplePatternItemsMultiple *> ( + pattern.get_items ().get ()); + items = lower_tuple_pattern_multiple (ref); + } + break; + + case AST::TuplePatternItems::TuplePatternItemType::RANGED: { + AST::TuplePatternItemsRanged &ref + = *static_cast<AST::TuplePatternItemsRanged *> ( + pattern.get_items ().get ()); + items = lower_tuple_pattern_ranged (ref); + } + break; + } + + 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); + + translated + = new HIR::TuplePattern (mapping, std::move (items), pattern.get_locus ()); +} + +void +ASTLoweringPattern::visit (AST::LiteralPattern &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::Literal l = lower_literal (pattern.get_literal ()); + translated + = new HIR::LiteralPattern (mapping, std::move (l), 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 80ff97a..32e9dac 100644 --- a/gcc/rust/hir/rust-ast-lower-pattern.h +++ b/gcc/rust/hir/rust-ast-lower-pattern.h @@ -58,6 +58,10 @@ public: void visit (AST::WildcardPattern &pattern) override; + void visit (AST::TuplePattern &pattern) override; + + void visit (AST::LiteralPattern &pattern) override; + private: ASTLoweringPattern () : translated (nullptr) {} diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h index 40c9fef..e585dd2 100644 --- a/gcc/rust/hir/tree/rust-hir-expr.h +++ b/gcc/rust/hir/tree/rust-hir-expr.h @@ -77,13 +77,13 @@ public: LiteralExpr (Analysis::NodeMapping mappings, std::string value_as_string, Literal::LitType type, PrimitiveCoreType type_hint, - Location locus, AST::AttrVec outer_attrs = AST::AttrVec ()) + Location locus, AST::AttrVec outer_attrs) : ExprWithoutBlock (std::move (mappings), std::move (outer_attrs)), literal (std::move (value_as_string), type, type_hint), locus (locus) {} LiteralExpr (Analysis::NodeMapping mappings, Literal literal, Location locus, - AST::AttrVec outer_attrs = AST::AttrVec ()) + AST::AttrVec outer_attrs) : ExprWithoutBlock (std::move (mappings), std::move (outer_attrs)), literal (std::move (literal)), locus (locus) {} diff --git a/gcc/rust/typecheck/rust-hir-type-check-enumitem.h b/gcc/rust/typecheck/rust-hir-type-check-enumitem.h index 9701a20..d6baff1 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-enumitem.h +++ b/gcc/rust/typecheck/rust-hir-type-check-enumitem.h @@ -57,7 +57,7 @@ public: = new HIR::LiteralExpr (mapping, std::to_string (last_discriminant), HIR::Literal::LitType::INT, PrimitiveCoreType::CORETYPE_I64, - item.get_locus ()); + item.get_locus (), {}); TyTy::BaseType *isize = nullptr; bool ok = context->lookup_builtin ("isize", &isize); @@ -135,7 +135,7 @@ public: = new HIR::LiteralExpr (mapping, std::to_string (last_discriminant), HIR::Literal::LitType::INT, PrimitiveCoreType::CORETYPE_I64, - item.get_locus ()); + item.get_locus (), {}); TyTy::BaseType *isize = nullptr; bool ok = context->lookup_builtin ("isize", &isize); @@ -182,7 +182,7 @@ public: = new HIR::LiteralExpr (mapping, std::to_string (last_discriminant), HIR::Literal::LitType::INT, PrimitiveCoreType::CORETYPE_I64, - item.get_locus ()); + item.get_locus (), {}); TyTy::BaseType *isize = nullptr; bool ok = context->lookup_builtin ("isize", &isize); diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h index 560581d..b24ad8b 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.h +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h @@ -670,7 +670,7 @@ public: = new HIR::LiteralExpr (capacity_mapping, capacity_str, HIR::Literal::LitType::INT, PrimitiveCoreType::CORETYPE_USIZE, - expr.get_locus ()); + expr.get_locus (), expr.get_outer_attrs ()); // mark the type for this implicit node TyTy::BaseType *expected_ty = nullptr; @@ -929,7 +929,7 @@ public: = new HIR::LiteralExpr (mapping, capacity_str, HIR::Literal::LitType::INT, PrimitiveCoreType::CORETYPE_USIZE, - Location ()); + Location (), {}); // mark the type for this implicit node TyTy::BaseType *expected_ty = nullptr; |