diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-06-20 16:50:46 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-06-20 16:50:46 +0200 |
commit | 6e5468e256dd06eb6988a0eca37c86bc52722457 (patch) | |
tree | 76ad85909cc24b3fab44967026e65b812e868bbb | |
parent | 8f996567e84015714bcf322d11a46580611e3377 (diff) | |
download | gcc-6e5468e256dd06eb6988a0eca37c86bc52722457.zip gcc-6e5468e256dd06eb6988a0eca37c86bc52722457.tar.gz gcc-6e5468e256dd06eb6988a0eca37c86bc52722457.tar.bz2 |
hir: Add ConstGenericArg and lower them properly
-rw-r--r-- | gcc/rust/ast/rust-path.h | 2 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-base.cc | 35 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-path.h | 52 |
3 files changed, 62 insertions, 27 deletions
diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h index 5642226..03758d0 100644 --- a/gcc/rust/ast/rust-path.h +++ b/gcc/rust/ast/rust-path.h @@ -189,6 +189,8 @@ public: Kind get_kind () const { return kind; } + std::unique_ptr<AST::Expr> &get_expression () { return expression; } + std::string as_string () const { switch (get_kind ()) diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc index 68bec88..7e80f81 100644 --- a/gcc/rust/hir/rust-ast-lower-base.cc +++ b/gcc/rust/hir/rust-ast-lower-base.cc @@ -612,8 +612,19 @@ ASTLoweringBase::lower_generic_args (AST::GenericArgs &args) type_args.push_back (std::unique_ptr<HIR::Type> (t)); } + std::vector<HIR::ConstGenericArg> const_args; + for (auto &const_arg : args.get_const_args ()) + const_args.emplace_back (HIR::ConstGenericArg ( + std::unique_ptr<HIR::Expr> ( + ASTLoweringExpr::translate (const_arg.get_expression ().get ())), + Location ())); + + // FIXME: + // const_arg.get_locus ()); + return HIR::GenericArgs (std::move (lifetime_args), std::move (type_args), - std::move (binding_args), args.get_locus ()); + std::move (binding_args), std::move (const_args), + args.get_locus ()); } HIR::SelfParam @@ -650,29 +661,17 @@ ASTLowerTypePath::visit (AST::TypePathSegmentGeneric &segment) bool has_separating_scope_resolution = segment.get_separating_scope_resolution (); - std::vector<HIR::Lifetime> lifetime_args; - for (auto &lifetime : segment.get_generic_args ().get_lifetime_args ()) - { - HIR::Lifetime l = lower_lifetime (lifetime); - lifetime_args.push_back (std::move (l)); - } - - std::vector<std::unique_ptr<HIR::Type>> type_args; - for (auto &type : segment.get_generic_args ().get_type_args ()) - { - HIR::Type *t = ASTLoweringType::translate (type.get ()); - type_args.push_back (std::unique_ptr<HIR::Type> (t)); - } + auto generic_args = lower_generic_args (segment.get_generic_args ()); auto crate_num = mappings->get_current_crate (); auto hirid = mappings->get_next_hir_id (crate_num); Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid, UNKNOWN_LOCAL_DEFID); - translated_segment = new HIR::TypePathSegmentGeneric ( - std::move (mapping), segment_name, has_separating_scope_resolution, - std::move (lifetime_args), std::move (type_args), std::move (binding_args), - segment.get_locus ()); + translated_segment + = new HIR::TypePathSegmentGeneric (std::move (mapping), segment_name, + has_separating_scope_resolution, + generic_args, segment.get_locus ()); } void diff --git a/gcc/rust/hir/tree/rust-hir-path.h b/gcc/rust/hir/tree/rust-hir-path.h index b18bf1d..a5006f5 100644 --- a/gcc/rust/hir/tree/rust-hir-path.h +++ b/gcc/rust/hir/tree/rust-hir-path.h @@ -112,12 +112,41 @@ public: Location get_locus () const { return locus; } }; +class ConstGenericArg +{ + // FIXME: Do we need to disambiguate or no? We should be able to disambiguate + // at name-resolution, hence no need for ambiguities here + +public: + ConstGenericArg (std::unique_ptr<Expr> expression, Location locus) + : expression (std::move (expression)), locus (locus) + {} + + ConstGenericArg (const ConstGenericArg &other) : locus (other.locus) + { + expression = other.expression->clone_expr (); + } + + ConstGenericArg operator= (const ConstGenericArg &other) + { + expression = other.expression->clone_expr (); + locus = other.locus; + + return *this; + } + +private: + std::unique_ptr<Expr> expression; + Location locus; +}; + // Generic arguments allowed in each path expression segment - inline? struct GenericArgs { std::vector<Lifetime> lifetime_args; std::vector<std::unique_ptr<Type> > type_args; std::vector<GenericArgsBinding> binding_args; + std::vector<ConstGenericArg> const_args; Location locus; public: @@ -130,18 +159,21 @@ public: GenericArgs (std::vector<Lifetime> lifetime_args, std::vector<std::unique_ptr<Type> > type_args, - std::vector<GenericArgsBinding> binding_args, Location locus) + std::vector<GenericArgsBinding> binding_args, + std::vector<ConstGenericArg> const_args, Location locus) : lifetime_args (std::move (lifetime_args)), type_args (std::move (type_args)), - binding_args (std::move (binding_args)), locus (locus) + binding_args (std::move (binding_args)), + const_args (std::move (const_args)), locus (locus) {} // copy constructor with vector clone GenericArgs (GenericArgs const &other) : lifetime_args (other.lifetime_args), binding_args (other.binding_args), - locus (other.locus) + const_args (other.const_args), locus (other.locus) { type_args.reserve (other.type_args.size ()); + for (const auto &e : other.type_args) type_args.push_back (e->clone_type ()); } @@ -153,6 +185,7 @@ public: { lifetime_args = other.lifetime_args; binding_args = other.binding_args; + const_args = other.const_args; locus = other.locus; type_args.reserve (other.type_args.size ()); @@ -169,9 +202,7 @@ public: // Creates an empty GenericArgs (no arguments) static GenericArgs create_empty (Location locus = Location ()) { - return GenericArgs (std::vector<Lifetime> (), - std::vector<std::unique_ptr<Type> > (), - std::vector<GenericArgsBinding> (), locus); + return GenericArgs ({}, {}, {}, {}, locus); } bool is_empty () const @@ -188,6 +219,8 @@ public: std::vector<GenericArgsBinding> &get_binding_args () { return binding_args; } + std::vector<ConstGenericArg> &get_const_args () { return const_args; } + Location get_locus () const { return locus; } }; @@ -464,12 +497,13 @@ public: std::vector<Lifetime> lifetime_args, std::vector<std::unique_ptr<Type> > type_args, std::vector<GenericArgsBinding> binding_args, + std::vector<ConstGenericArg> const_args, Location locus) : TypePathSegment (std::move (mappings), std::move (segment_name), has_separating_scope_resolution, locus), - generic_args (GenericArgs (std::move (lifetime_args), - std::move (type_args), - std::move (binding_args), locus)) + generic_args ( + GenericArgs (std::move (lifetime_args), std::move (type_args), + std::move (binding_args), std::move (const_args), locus)) {} std::string as_string () const override; |