diff options
Diffstat (limited to 'gcc/rust/hir/tree')
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-full-decls.h | 1 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-path.cc | 20 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-path.h | 95 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-stmt.cc | 12 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-stmt.h | 16 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-type.cc | 3 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-type.h | 32 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-visitor.h | 3 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir.cc | 26 |
9 files changed, 141 insertions, 67 deletions
diff --git a/gcc/rust/hir/tree/rust-hir-full-decls.h b/gcc/rust/hir/tree/rust-hir-full-decls.h index c64c8bf..6c19f24 100644 --- a/gcc/rust/hir/tree/rust-hir-full-decls.h +++ b/gcc/rust/hir/tree/rust-hir-full-decls.h @@ -211,7 +211,6 @@ class TraitBound; class ImplTraitType; class TraitObjectType; class ParenthesisedType; -class ImplTraitTypeOneBound; class TupleType; class NeverType; class RawPointerType; diff --git a/gcc/rust/hir/tree/rust-hir-path.cc b/gcc/rust/hir/tree/rust-hir-path.cc index 7db2b25..ee4a572 100644 --- a/gcc/rust/hir/tree/rust-hir-path.cc +++ b/gcc/rust/hir/tree/rust-hir-path.cc @@ -133,6 +133,8 @@ PathExprSegment::operator= (PathExprSegment const &other) void PathPattern::iterate_path_segments (std::function<bool (PathExprSegment &)> cb) { + rust_assert (kind == Kind::Segmented); + for (auto it = segments.begin (); it != segments.end (); it++) { if (!cb (*it)) @@ -150,6 +152,15 @@ PathInExpression::PathInExpression (Analysis::NodeMapping mappings, has_opening_scope_resolution (has_opening_scope_resolution), locus (locus) {} +PathInExpression::PathInExpression (Analysis::NodeMapping mappings, + LangItem::Kind lang_item, location_t locus, + bool has_opening_scope_resolution, + std::vector<AST::Attribute> outer_attrs) + : PathPattern (lang_item), + PathExpr (std::move (mappings), std::move (outer_attrs)), + has_opening_scope_resolution (has_opening_scope_resolution), locus (locus) +{} + bool PathInExpression::is_self () const @@ -358,6 +369,15 @@ QualifiedPathInExpression::QualifiedPathInExpression ( path_type (std::move (qual_path_type)), locus (locus) {} +QualifiedPathInExpression::QualifiedPathInExpression ( + Analysis::NodeMapping mappings, QualifiedPathType qual_path_type, + LangItem::Kind lang_item, location_t locus, + std::vector<AST::Attribute> outer_attrs) + : PathPattern (lang_item), + PathExpr (std::move (mappings), std::move (outer_attrs)), + path_type (std::move (qual_path_type)), locus (locus) +{} + QualifiedPathInType::QualifiedPathInType ( Analysis::NodeMapping mappings, QualifiedPathType qual_path_type, std::unique_ptr<TypePathSegment> associated_segment, diff --git a/gcc/rust/hir/tree/rust-hir-path.h b/gcc/rust/hir/tree/rust-hir-path.h index bbb9c2d..3ce2662 100644 --- a/gcc/rust/hir/tree/rust-hir-path.h +++ b/gcc/rust/hir/tree/rust-hir-path.h @@ -19,6 +19,7 @@ #ifndef RUST_HIR_PATH_H #define RUST_HIR_PATH_H +#include "rust-hir-map.h" #include "rust-hir-simple-path.h" #include "rust-hir-type-no-bounds.h" #include "rust-hir-pattern-abstract.h" @@ -230,15 +231,34 @@ public: // HIR node representing a pattern that involves a "path" - abstract base class class PathPattern : public Pattern { +public: + enum class Kind + { + Segmented, + LangItem + }; + +private: std::vector<PathExprSegment> segments; + tl::optional<LangItem::Kind> lang_item; + Kind kind; protected: PathPattern (std::vector<PathExprSegment> segments) - : segments (std::move (segments)) + : segments (std::move (segments)), lang_item (tl::nullopt), + kind (Kind::Segmented) + {} + + PathPattern (LangItem::Kind lang_item) + : segments ({}), lang_item (lang_item), kind (Kind::LangItem) {} // Returns whether path has segments. - bool has_segments () const { return !segments.empty (); } + bool has_segments () const + { + rust_assert (kind == Kind::Segmented); + return !segments.empty (); + } /* Converts path segments to their equivalent SimplePath segments if possible, * and creates a SimplePath from them. */ @@ -248,26 +268,61 @@ protected: public: /* Returns whether the path is a single segment (excluding qualified path * initial as segment). */ - bool is_single_segment () const { return segments.size () == 1; } + bool is_single_segment () const + { + rust_assert (kind == Kind::Segmented); + return segments.size () == 1; + } std::string as_string () const override; void iterate_path_segments (std::function<bool (PathExprSegment &)> cb); - size_t get_num_segments () const { return segments.size (); } + size_t get_num_segments () const + { + rust_assert (kind == Kind::Segmented); + return segments.size (); + } + + std::vector<PathExprSegment> &get_segments () + { + rust_assert (kind == Kind::Segmented); + return segments; + } + + const std::vector<PathExprSegment> &get_segments () const + { + rust_assert (kind == Kind::Segmented); + return segments; + } - std::vector<PathExprSegment> &get_segments () { return segments; } + PathExprSegment &get_root_seg () + { + rust_assert (kind == Kind::Segmented); + return segments.at (0); + } - const std::vector<PathExprSegment> &get_segments () const { return segments; } + const PathExprSegment &get_final_segment () const + { + rust_assert (kind == Kind::Segmented); + return segments.back (); + } - PathExprSegment &get_root_seg () { return segments.at (0); } + LangItem::Kind get_lang_item () const + { + rust_assert (kind == Kind::LangItem); - const PathExprSegment &get_final_segment () const { return segments.back (); } + return *lang_item; + } PatternType get_pattern_type () const override final { return PatternType::PATH; } + + bool is_lang_item () const { return kind == Kind::LangItem; } + + Kind get_path_kind () const { return kind; } }; /* HIR node representing a path-in-expression pattern (path that allows generic @@ -288,6 +343,13 @@ public: std::vector<AST::Attribute> outer_attrs = std::vector<AST::Attribute> ()); + // lang-item Constructor + PathInExpression (Analysis::NodeMapping mappings, LangItem::Kind kind, + location_t locus = UNDEF_LOCATION, + bool has_opening_scope_resolution = false, + std::vector<AST::Attribute> outer_attrs + = std::vector<AST::Attribute> ()); + // Creates an error state path in expression. static PathInExpression create_error () { @@ -671,13 +733,20 @@ public: Analysis::NodeMapping get_mappings () const { return mappings; } + bool has_type () { return type != nullptr; } + bool has_trait () { return trait != nullptr; } + Type &get_type () { rust_assert (type); return *type; } - TypePath &get_trait () { return *trait; } + TypePath &get_trait () + { + rust_assert (trait); + return *trait; + } bool trait_has_generic_args () const; @@ -701,6 +770,14 @@ public: std::vector<AST::Attribute> outer_attrs = std::vector<AST::Attribute> ()); + // lang-item constructor + QualifiedPathInExpression (Analysis::NodeMapping mappings, + QualifiedPathType qual_path_type, + LangItem::Kind lang_item, + location_t locus = UNDEF_LOCATION, + std::vector<AST::Attribute> outer_attrs + = std::vector<AST::Attribute> ()); + location_t get_locus () const override final { return locus; } void accept_vis (HIRFullVisitor &vis) override; diff --git a/gcc/rust/hir/tree/rust-hir-stmt.cc b/gcc/rust/hir/tree/rust-hir-stmt.cc index 025f67e..fd58e29 100644 --- a/gcc/rust/hir/tree/rust-hir-stmt.cc +++ b/gcc/rust/hir/tree/rust-hir-stmt.cc @@ -26,11 +26,13 @@ namespace HIR { LetStmt::LetStmt (Analysis::NodeMapping mappings, std::unique_ptr<Pattern> variables_pattern, tl::optional<std::unique_ptr<Expr>> init_expr, + tl::optional<std::unique_ptr<Expr>> else_expr, tl::optional<std::unique_ptr<Type>> type, AST::AttrVec outer_attrs, location_t locus) : Stmt (std::move (mappings)), outer_attrs (std::move (outer_attrs)), variables_pattern (std::move (variables_pattern)), type (std::move (type)), - init_expr (std::move (init_expr)), locus (locus) + init_expr (std::move (init_expr)), else_expr (std::move (else_expr)), + locus (locus) {} LetStmt::LetStmt (LetStmt const &other) @@ -43,6 +45,8 @@ LetStmt::LetStmt (LetStmt const &other) // guard to prevent null dereference (always required) if (other.has_init_expr ()) init_expr = other.get_init_expr ().clone_expr (); + if (other.has_else_expr ()) + else_expr = other.get_else_expr ().clone_expr (); if (other.has_type ()) type = other.get_type ().clone_type (); @@ -67,6 +71,12 @@ LetStmt::operator= (LetStmt const &other) init_expr = other.get_init_expr ().clone_expr (); else init_expr = nullptr; + + if (other.has_else_expr ()) + else_expr = other.get_else_expr ().clone_expr (); + else + else_expr = tl::nullopt; + if (other.has_type ()) type = other.get_type ().clone_type (); else diff --git a/gcc/rust/hir/tree/rust-hir-stmt.h b/gcc/rust/hir/tree/rust-hir-stmt.h index 3db1728..9c1a9ec 100644 --- a/gcc/rust/hir/tree/rust-hir-stmt.h +++ b/gcc/rust/hir/tree/rust-hir-stmt.h @@ -101,6 +101,7 @@ class LetStmt : public Stmt tl::optional<std::unique_ptr<Type>> type; tl::optional<std::unique_ptr<Expr>> init_expr; + tl::optional<std::unique_ptr<Expr>> else_expr; location_t locus; @@ -113,12 +114,15 @@ public: // Returns whether let statement has an initialisation expression. bool has_init_expr () const { return init_expr.has_value (); } + // Returns whether let statement has a diverging else expression. + bool has_else_expr () const { return else_expr.has_value (); } std::string as_string () const override; LetStmt (Analysis::NodeMapping mappings, std::unique_ptr<Pattern> variables_pattern, tl::optional<std::unique_ptr<Expr>> init_expr, + tl::optional<std::unique_ptr<Expr>> else_expr, tl::optional<std::unique_ptr<Type>> type, AST::AttrVec outer_attrs, location_t locus); @@ -167,6 +171,18 @@ public: return *init_expr.value (); } + HIR::Expr &get_else_expr () + { + rust_assert (*else_expr); + return *else_expr.value (); + } + + const HIR::Expr &get_else_expr () const + { + rust_assert (*else_expr); + return *else_expr.value (); + } + HIR::Pattern &get_pattern () { return *variables_pattern; } bool is_item () const override final { return false; } diff --git a/gcc/rust/hir/tree/rust-hir-type.cc b/gcc/rust/hir/tree/rust-hir-type.cc index 689d86b..6a6c319 100644 --- a/gcc/rust/hir/tree/rust-hir-type.cc +++ b/gcc/rust/hir/tree/rust-hir-type.cc @@ -268,7 +268,8 @@ BareFunctionType::BareFunctionType (BareFunctionType const &other) for_lifetimes (other.for_lifetimes), function_qualifiers (other.function_qualifiers), params (other.params), is_variadic (other.is_variadic), - return_type (other.return_type->clone_type ()) + return_type (other.has_return_type () ? other.return_type->clone_type () + : nullptr) {} BareFunctionType & diff --git a/gcc/rust/hir/tree/rust-hir-type.h b/gcc/rust/hir/tree/rust-hir-type.h index e231d78..bd0f2b6 100644 --- a/gcc/rust/hir/tree/rust-hir-type.h +++ b/gcc/rust/hir/tree/rust-hir-type.h @@ -164,38 +164,6 @@ public: void accept_vis (HIRTypeVisitor &vis) override; }; -// Impl trait with a single bound? Poor reference material here. -class ImplTraitTypeOneBound : public TypeNoBounds -{ - TraitBound trait_bound; - -protected: - /* Use covariance to implement clone function as returning this object rather - * than base */ - ImplTraitTypeOneBound *clone_type_impl () const override - { - return new ImplTraitTypeOneBound (*this); - } - - /* Use covariance to implement clone function as returning this object rather - * than base */ - ImplTraitTypeOneBound *clone_type_no_bounds_impl () const override - { - return new ImplTraitTypeOneBound (*this); - } - -public: - ImplTraitTypeOneBound (Analysis::NodeMapping mappings, TraitBound trait_bound, - location_t locus) - : TypeNoBounds (mappings, locus), trait_bound (std::move (trait_bound)) - {} - - std::string as_string () const override; - TraitBound &get_trait_bound () { return trait_bound; } - void accept_vis (HIRFullVisitor &vis) override; - void accept_vis (HIRTypeVisitor &vis) override; -}; - /* A type consisting of the "product" of others (the tuple's elements) in a * specific order */ class TupleType : public TypeNoBounds diff --git a/gcc/rust/hir/tree/rust-hir-visitor.h b/gcc/rust/hir/tree/rust-hir-visitor.h index 33e6b7b..800e647 100644 --- a/gcc/rust/hir/tree/rust-hir-visitor.h +++ b/gcc/rust/hir/tree/rust-hir-visitor.h @@ -142,7 +142,6 @@ public: virtual void visit (ImplTraitType &type) = 0; virtual void visit (TraitObjectType &type) = 0; virtual void visit (ParenthesisedType &type) = 0; - virtual void visit (ImplTraitTypeOneBound &type) = 0; virtual void visit (TupleType &type) = 0; virtual void visit (NeverType &type) = 0; virtual void visit (RawPointerType &type) = 0; @@ -290,7 +289,6 @@ public: virtual void visit (ImplTraitType &) override {} virtual void visit (TraitObjectType &) override {} virtual void visit (ParenthesisedType &) override {} - virtual void visit (ImplTraitTypeOneBound &) override {} virtual void visit (TupleType &) override {} virtual void visit (NeverType &) override {} virtual void visit (RawPointerType &) override {} @@ -354,7 +352,6 @@ public: virtual void visit (ImplTraitType &type) = 0; virtual void visit (TraitObjectType &type) = 0; virtual void visit (ParenthesisedType &type) = 0; - virtual void visit (ImplTraitTypeOneBound &type) = 0; virtual void visit (TupleType &type) = 0; virtual void visit (NeverType &type) = 0; virtual void visit (RawPointerType &type) = 0; diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc index da1c8b9..822eaff 100644 --- a/gcc/rust/hir/tree/rust-hir.cc +++ b/gcc/rust/hir/tree/rust-hir.cc @@ -19,6 +19,7 @@ #include "rust-ast-full.h" #include "rust-hir-expr.h" #include "rust-hir-full.h" +#include "rust-hir-path.h" #include "rust-hir-visitor.h" #include "rust-diagnostics.h" @@ -1210,6 +1211,9 @@ ClosureExpr::as_string () const std::string PathPattern::as_string () const { + if (is_lang_item ()) + return LangItem::PrettyString (*lang_item); + std::string str; for (const auto &segment : segments) @@ -2187,6 +2191,8 @@ TypeParam::as_string () const AST::SimplePath PathPattern::convert_to_simple_path (bool with_opening_scope_resolution) const { + rust_assert (kind == Kind::Segmented); + if (!has_segments ()) { return AST::SimplePath::create_empty (); @@ -2860,14 +2866,6 @@ BareFunctionType::as_string () const } std::string -ImplTraitTypeOneBound::as_string () const -{ - std::string str ("ImplTraitTypeOneBound: \n TraitBound: "); - - return str + trait_bound.as_string (); -} - -std::string TypePathSegmentGeneric::as_string () const { return TypePathSegment::as_string () + "<" + generic_args.as_string () + ">"; @@ -4520,12 +4518,6 @@ ParenthesisedType::accept_vis (HIRFullVisitor &vis) } void -ImplTraitTypeOneBound::accept_vis (HIRFullVisitor &vis) -{ - vis.visit (*this); -} - -void TupleType::accept_vis (HIRFullVisitor &vis) { vis.visit (*this); @@ -4718,12 +4710,6 @@ ArrayType::accept_vis (HIRTypeVisitor &vis) } void -ImplTraitTypeOneBound::accept_vis (HIRTypeVisitor &vis) -{ - vis.visit (*this); -} - -void BareFunctionType::accept_vis (HIRTypeVisitor &vis) { vis.visit (*this); |