From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id C1E4F3875A2A; Sat, 25 Jun 2022 09:31:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C1E4F3875A2A Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] hir: Add ConstGenericArg and lower them properly X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 8f996567e84015714bcf322d11a46580611e3377 X-Git-Newrev: 6e5468e256dd06eb6988a0eca37c86bc52722457 Message-Id: <20220625093158.C1E4F3875A2A@sourceware.org> Date: Sat, 25 Jun 2022 09:31:58 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 25 Jun 2022 09:31:58 -0000 https://gcc.gnu.org/g:6e5468e256dd06eb6988a0eca37c86bc52722457 commit 6e5468e256dd06eb6988a0eca37c86bc52722457 Author: Arthur Cohen Date: Mon Jun 20 16:50:46 2022 +0200 hir: Add ConstGenericArg and lower them properly Diff: --- gcc/rust/ast/rust-path.h | 2 ++ gcc/rust/hir/rust-ast-lower-base.cc | 35 ++++++++++++------------- 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 5642226afce..03758d01dde 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 &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 68bec882daa..7e80f811092 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 (t)); } + std::vector const_args; + for (auto &const_arg : args.get_const_args ()) + const_args.emplace_back (HIR::ConstGenericArg ( + std::unique_ptr ( + 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 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> 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 (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 b18bf1dde22..a5006f5f4c7 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 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 expression; + Location locus; +}; + // Generic arguments allowed in each path expression segment - inline? struct GenericArgs { std::vector lifetime_args; std::vector > type_args; std::vector binding_args; + std::vector const_args; Location locus; public: @@ -130,18 +159,21 @@ public: GenericArgs (std::vector lifetime_args, std::vector > type_args, - std::vector binding_args, Location locus) + std::vector binding_args, + std::vector 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 (), - std::vector > (), - std::vector (), locus); + return GenericArgs ({}, {}, {}, {}, locus); } bool is_empty () const @@ -188,6 +219,8 @@ public: std::vector &get_binding_args () { return binding_args; } + std::vector &get_const_args () { return const_args; } + Location get_locus () const { return locus; } }; @@ -464,12 +497,13 @@ public: std::vector lifetime_args, std::vector > type_args, std::vector binding_args, + std::vector 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;