From: Pierre-Emmanuel Patry <[email protected]>
We require lang item names because we're dumping the AST. When this will
change, we won't have to keep the segment name within the AST and won't
be required to provide those values for the builder. In the meantime
we're forced to hardcode those values because we do not want to move the
lang item name discovery up in the pipeline. We want to generate the
macros and then error out during name resolution.
gcc/rust/ChangeLog:
* ast/rust-ast-builder.cc (Builder::type_path_segment): Remove name.
(Builder::type_path_segment_generic): Likewise.
(Builder::single_generic_type_path): Likewise.
(Builder::type_path): Likewise.
* ast/rust-ast-builder.h: Update function prototypes.
* expand/rust-derive-clone.cc (DeriveClone::visit_union): do not inject
trait name.
* expand/rust-derive-copy.cc: Likewise.
* expand/rust-derive-eq.cc (DeriveEq::assert_param_is_eq): Likewise.
* expand/rust-derive-partial-eq.cc: Likewise.
* util/rust-hir-map.cc (Mappings::get_lang_item_identifier): Add a
function to retrieve a lang item identifier from it's kind.
* util/rust-hir-map.h: Add function prototype.
Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
---
gcc/rust/ast/rust-ast-builder.cc | 16 ++++++++++------
gcc/rust/ast/rust-ast-builder.h | 10 ++++------
gcc/rust/expand/rust-derive-clone.cc | 14 +++++---------
gcc/rust/expand/rust-derive-copy.cc | 5 ++---
gcc/rust/expand/rust-derive-eq.cc | 9 +++------
gcc/rust/expand/rust-derive-partial-eq.cc | 5 ++---
gcc/rust/util/rust-hir-map.cc | 19 +++++++++++++++++++
gcc/rust/util/rust-hir-map.h | 1 +
8 files changed, 46 insertions(+), 33 deletions(-)
diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc
index 9802f2a569f..5c30a477e49 100644
--- a/gcc/rust/ast/rust-ast-builder.cc
+++ b/gcc/rust/ast/rust-ast-builder.cc
@@ -182,8 +182,10 @@ Builder::type_path_segment (std::string seg) const
}
std::unique_ptr<TypePathSegment>
-Builder::type_path_segment (LangItem::Kind lang_item, std::string &name) const
+Builder::type_path_segment (LangItem::Kind lang_item) const
{
+ auto &mappings = Analysis::Mappings::get ();
+ auto name = mappings.get_lang_item_identifier (lang_item);
return std::unique_ptr<TypePathSegment> (
new TypePathSegment (lang_item, PathIdentSegment (name, loc), loc));
}
@@ -196,9 +198,11 @@ Builder::type_path_segment_generic (std::string seg, GenericArgs args) const
}
std::unique_ptr<TypePathSegment>
-Builder::type_path_segment_generic (LangItem::Kind lang_item, std::string &name,
+Builder::type_path_segment_generic (LangItem::Kind lang_item,
GenericArgs args) const
{
+ auto &mappings = Analysis::Mappings::get ();
+ auto name = mappings.get_lang_item_identifier (lang_item);
return std::unique_ptr<TypePathSegment> (
new TypePathSegmentGeneric (lang_item, PathIdentSegment (name, loc), args,
loc));
@@ -229,11 +233,11 @@ Builder::single_generic_type_path (std::string type, GenericArgs args) const
}
std::unique_ptr<Type>
-Builder::single_generic_type_path (LangItem::Kind lang_item, std::string &name,
+Builder::single_generic_type_path (LangItem::Kind lang_item,
GenericArgs args) const
{
auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
- segments.emplace_back (type_path_segment_generic (lang_item, name, args));
+ segments.emplace_back (type_path_segment_generic (lang_item, args));
return std::unique_ptr<Type> (new TypePath (std::move (segments), loc));
}
@@ -273,9 +277,9 @@ Builder::type_path (std::string type) const
}
TypePath
-Builder::type_path (LangItem::Kind lang_item, std::string &name) const
+Builder::type_path (LangItem::Kind lang_item) const
{
- return type_path (type_path_segment (lang_item, name));
+ return type_path (type_path_segment (lang_item));
}
std::unique_ptr<Type>
diff --git a/gcc/rust/ast/rust-ast-builder.h b/gcc/rust/ast/rust-ast-builder.h
index 143616816c0..2b34c2ab1e2 100644
--- a/gcc/rust/ast/rust-ast-builder.h
+++ b/gcc/rust/ast/rust-ast-builder.h
@@ -198,14 +198,13 @@ public:
/* And similarly for type path segments */
std::unique_ptr<TypePathSegment> type_path_segment (std::string seg) const;
- std::unique_ptr<TypePathSegment> type_path_segment (LangItem::Kind lang_item,
- std::string &name) const;
+ std::unique_ptr<TypePathSegment>
+ type_path_segment (LangItem::Kind lang_item) const;
std::unique_ptr<TypePathSegment>
type_path_segment_generic (std::string seg, GenericArgs args) const;
std::unique_ptr<TypePathSegment>
- type_path_segment_generic (LangItem::Kind lang_item, std::string &name,
- GenericArgs args) const;
+ type_path_segment_generic (LangItem::Kind lang_item, GenericArgs args) const;
/* Create a Type from a single string - the most basic kind of type in our AST
*/
@@ -215,7 +214,6 @@ public:
std::unique_ptr<Type> single_generic_type_path (std::string type,
GenericArgs args) const;
std::unique_ptr<Type> single_generic_type_path (LangItem::Kind lang_item,
- std::string &name,
GenericArgs args) const;
TypePath type_path (std::vector<std::unique_ptr<TypePathSegment>> &&segment,
@@ -224,7 +222,7 @@ public:
bool opening_scope = false) const;
TypePath type_path (std::unique_ptr<TypePathSegment> &&segment) const;
TypePath type_path (std::string type) const;
- TypePath type_path (LangItem::Kind lang_item, std::string &name) const;
+ TypePath type_path (LangItem::Kind lang_item) const;
std::unique_ptr<Type>
reference_type (std::unique_ptr<TypeNoBounds> &&inner_type,
diff --git a/gcc/rust/expand/rust-derive-clone.cc b/gcc/rust/expand/rust-derive-clone.cc
index bef5fdae407..d83743786c1 100644
--- a/gcc/rust/expand/rust-derive-clone.cc
+++ b/gcc/rust/expand/rust-derive-clone.cc
@@ -86,9 +86,8 @@ DeriveClone::clone_impl (
std::unique_ptr<AssociatedItem> &&clone_fn, std::string name,
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
{
- auto clone_trait_path = [this, &name] () {
- return builder.type_path (LangItem::Kind::CLONE, name);
- };
+ auto clone_trait_path
+ = [this] () { return builder.type_path (LangItem::Kind::CLONE); };
auto trait_items = vec (std::move (clone_fn));
@@ -350,10 +349,8 @@ DeriveClone::visit_union (Union &item)
// FIXME: Should be $crate::core::clone::AssertParamIsCopy (or similar)
// (Rust-GCC#3329)
- std::string copy_name = "Copy";
- std::string sized_name = "Sized";
- auto copy_path = builder.type_path (LangItem::Kind::COPY, copy_name);
- auto sized_path = builder.type_path (LangItem::Kind::SIZED, sized_name);
+ auto copy_path = builder.type_path (LangItem::Kind::COPY);
+ auto sized_path = builder.type_path (LangItem::Kind::SIZED);
auto copy_bound = std::unique_ptr<TypeParamBound> (
new TraitBound (copy_path, item.get_locus ()));
@@ -367,13 +364,12 @@ DeriveClone::visit_union (Union &item)
auto assert_param_is_copy = "AssertParamIsCopy";
auto t = std::unique_ptr<GenericParam> (
new TypeParam (Identifier ("T"), item.get_locus (), std::move (bounds)));
- std::string phantom_data_name = "PhantomData";
auto assert_param_is_copy_struct = builder.struct_struct (
assert_param_is_copy, vec (std::move (t)),
{StructField (
Identifier ("_t"),
builder.single_generic_type_path (
- LangItem::Kind::PHANTOM_DATA, phantom_data_name,
+ LangItem::Kind::PHANTOM_DATA,
GenericArgs (
{}, {GenericArg::create_type (builder.single_type_path ("T"))}, {})),
Visibility::create_private (), item.get_locus ())});
diff --git a/gcc/rust/expand/rust-derive-copy.cc b/gcc/rust/expand/rust-derive-copy.cc
index 330ec2d7f76..305100768c9 100644
--- a/gcc/rust/expand/rust-derive-copy.cc
+++ b/gcc/rust/expand/rust-derive-copy.cc
@@ -42,9 +42,8 @@ DeriveCopy::copy_impl (
std::string name,
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
{
- auto copy_trait_path = [this, &name] () {
- return builder.type_path (LangItem::Kind::COPY, name);
- };
+ auto copy_trait_path
+ = [this] () { return builder.type_path (LangItem::Kind::COPY); };
auto generics = setup_impl_generics (name, type_generics, [&, this] () {
return builder.trait_bound (copy_trait_path ());
diff --git a/gcc/rust/expand/rust-derive-eq.cc b/gcc/rust/expand/rust-derive-eq.cc
index 2ace157a69f..c388c379e4a 100644
--- a/gcc/rust/expand/rust-derive-eq.cc
+++ b/gcc/rust/expand/rust-derive-eq.cc
@@ -69,9 +69,8 @@ DeriveEq::assert_param_is_eq ()
auto eq_bound = std::unique_ptr<TypeParamBound> (
new TraitBound (get_eq_trait_path (builder), loc));
- auto name = std::string{"Sized"};
auto sized_bound = std::unique_ptr<TypeParamBound> (
- new TraitBound (builder.type_path (LangItem::Kind::SIZED, name), loc, false,
+ new TraitBound (builder.type_path (LangItem::Kind::SIZED), loc, false,
true /* opening_question_mark */));
auto bounds = vec (std::move (eq_bound), std::move (sized_bound));
@@ -81,14 +80,12 @@ DeriveEq::assert_param_is_eq ()
auto t = std::unique_ptr<GenericParam> (
new TypeParam (Identifier ("T"), loc, std::move (bounds)));
- std::string phantom_data = "PhantomData";
-
return builder.struct_struct (
assert_param_is_eq, vec (std::move (t)),
{StructField (
Identifier ("_t"),
builder.single_generic_type_path (
- LangItem::Kind::PHANTOM_DATA, phantom_data,
+ LangItem::Kind::PHANTOM_DATA,
GenericArgs (
{}, {GenericArg::create_type (builder.single_type_path ("T"))}, {})),
Visibility::create_private (), loc)});
@@ -124,7 +121,7 @@ DeriveEq::eq_impls (
auto eq = [this] () { return get_eq_trait_path (builder); };
auto eq_bound = [&, this] () { return builder.trait_bound (eq ()); };
- auto steq = builder.type_path (LangItem::Kind::STRUCTURAL_TEQ, name);
+ auto steq = builder.type_path (LangItem::Kind::STRUCTURAL_TEQ);
auto trait_items = vec (std::move (fn));
diff --git a/gcc/rust/expand/rust-derive-partial-eq.cc b/gcc/rust/expand/rust-derive-partial-eq.cc
index 10212c2ab82..03879fad95c 100644
--- a/gcc/rust/expand/rust-derive-partial-eq.cc
+++ b/gcc/rust/expand/rust-derive-partial-eq.cc
@@ -42,9 +42,8 @@ DerivePartialEq::partialeq_impls (
std::unique_ptr<AssociatedItem> &&eq_fn, std::string name,
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
{
- auto eq
- = [this, &name] () { return builder.type_path (LangItem::Kind::EQ, name); };
- auto speq = builder.type_path (LangItem::Kind::STRUCTURAL_PEQ, name);
+ auto eq = [this] () { return builder.type_path (LangItem::Kind::EQ); };
+ auto speq = builder.type_path (LangItem::Kind::STRUCTURAL_PEQ);
auto trait_items = vec (std::move (eq_fn));
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index f5ad6db5547..10ee87e1bc8 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -1350,6 +1350,25 @@ Mappings::get_lang_item_node (LangItem::Kind item_type)
LangItem::PrettyString (item_type).c_str ());
}
+std::string &
+Mappings::get_lang_item_identifier (LangItem::Kind item_type)
+{
+ // Lang item names are hardcoded because they're only required for metadata
+ // dump which will get removed at some point
+ static std::unordered_map<LangItem::Kind, std::string> identifiers
+ = {{LangItem::Kind::COPY, "Copy"},
+ {LangItem::Kind::CLONE, "Clone"},
+ {LangItem::Kind::STRUCTURAL_PEQ, "StructuralPartialEq"},
+ {LangItem::Kind::STRUCTURAL_TEQ, "StructuralEq"},
+ {LangItem::Kind::SIZED, "Sized"},
+ {LangItem::Kind::EQ, "PartialEq"},
+ {LangItem::Kind::PHANTOM_DATA, "PhantomData"}};
+ auto result = identifiers.find (item_type);
+ if (result != identifiers.cend ())
+ return result->second;
+ rust_unreachable ();
+}
+
void
Mappings::insert_auto_trait (HIR::Trait *trait)
{
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index a1544a15283..3212083b376 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -264,6 +264,7 @@ public:
void insert_lang_item_node (LangItem::Kind item_type, NodeId node_id);
tl::optional<NodeId &> lookup_lang_item_node (LangItem::Kind item_type);
NodeId get_lang_item_node (LangItem::Kind item_type);
+ std::string &get_lang_item_identifier (LangItem::Kind item_type);
// This will fatal_error when this lang item does not exist
DefId get_lang_item (LangItem::Kind item_type, location_t locus);
--
2.50.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.