public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] hir: Add ConstGenericArg and lower them properly
@ 2022-06-25 9:31 Thomas Schwinge
0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-25 9:31 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:6e5468e256dd06eb6988a0eca37c86bc52722457
commit 6e5468e256dd06eb6988a0eca37c86bc52722457
Author: Arthur Cohen <arthur.cohen@embecosm.com>
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<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 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<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 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<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;
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2022-06-25 9:31 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-25 9:31 [gcc/devel/rust/master] hir: Add ConstGenericArg and lower them properly Thomas Schwinge
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).