Skip to content

Commit

Permalink
hir: Lower lang-item paths
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* hir/rust-ast-lower-type.cc (ASTLowerTypePath::translate): Adapt to
	handle lang item paths.
	(ASTLowerTypePath::visit): Likewise.
	(ASTLowerTypePath::translate_type_path): New.
	(ASTLowerTypePath::translate_lang_item_type_path): New.
	* hir/rust-ast-lower-type.h: Adapt to handle lang item paths.
	* resolve/rust-ast-resolve-type.h: Likewise.
  • Loading branch information
CohenArthur committed Dec 5, 2024
1 parent eeedb0b commit 408aeed
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
53 changes: 48 additions & 5 deletions gcc/rust/hir/rust-ast-lower-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,42 @@
// <http://www.gnu.org/licenses/>.

#include "rust-ast-lower-type.h"
#include "optional.h"
#include "rust-attribute-values.h"
#include "rust-hir-map.h"
#include "rust-hir-path.h"
#include "rust-path.h"
#include "rust-pattern.h"

namespace Rust {
namespace HIR {

HIR::TypePath *
ASTLowerTypePath::translate (AST::Path &type)
{
rust_assert (type.get_path_kind () == AST::Path::Kind::Type);
switch (type.get_path_kind ())
{
case AST::Path::Kind::LangItem:
return translate_lang_item_type_path (
static_cast<AST::LangItemPath &> (type));
case AST::Path::Kind::Type:
return translate_type_path (static_cast<AST::TypePath &> (type));
default:
rust_unreachable ();
}
}

return ASTLowerTypePath::translate (static_cast<AST::TypePath &> (type));
// FIXME: Before merging: Can we simplify these two with a simple call to visit
// to do dynamic dispatch?
HIR::TypePath *
ASTLowerTypePath::translate_type_path (AST::TypePath &type)
{
ASTLowerTypePath resolver;
type.accept_vis (resolver);
rust_assert (resolver.translated != nullptr);
return resolver.translated;
}

HIR::TypePath *
ASTLowerTypePath::translate (AST::TypePath &type)
ASTLowerTypePath::translate_lang_item_type_path (AST::LangItemPath &type)
{
ASTLowerTypePath resolver;
type.accept_vis (resolver);
Expand Down Expand Up @@ -135,6 +154,30 @@ ASTLowerTypePath::visit (AST::TypePath &path)
path.has_opening_scope_resolution_op ());
}

void
ASTLowerTypePath::visit (AST::LangItemPath &path)
{
auto crate_num = mappings.get_current_crate ();
auto hirid = mappings.get_next_hir_id (crate_num);
// FIXME: Here, do we want the path's node ID or the lang-item's node id? Same
// for the DefId? This is where we want to add our lang item to the mappings
// too no? -> no, cause this is a usage of the lang item, but not the
// definition itself e.g. impl #[lang = copy] for i32 {}. so we don't want to
// add to mappings
Analysis::NodeMapping mapping (crate_num, path.get_node_id (), hirid,
mappings.get_next_localdef_id (crate_num));

std::vector<std::unique_ptr<HIR::TypePathSegment>> translated_segments;
translated_segments.emplace_back (std::unique_ptr<HIR::TypePathSegment> (
new HIR::TypePathSegment (mapping,
LangItem::ToString (path.get_lang_item_kind ()),
false, path.get_locus ())));

translated
= new HIR::TypePath (std::move (mapping), std::move (translated_segments),
path.get_locus ());
}

HIR::QualifiedPathInType *
ASTLowerQualifiedPathInType::translate (AST::QualifiedPathInType &type)
{
Expand Down
6 changes: 5 additions & 1 deletion gcc/rust/hir/rust-ast-lower-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "rust-ast-lower-base.h"
#include "rust-ast-lower-expr.h"
#include "rust-hir-path.h"

namespace Rust {
namespace HIR {
Expand All @@ -32,18 +33,21 @@ class ASTLowerTypePath : public ASTLoweringBase

public:
static HIR::TypePath *translate (AST::Path &type);
static HIR::TypePath *translate (AST::TypePath &type);

void visit (AST::TypePathSegmentFunction &segment) override;
void visit (AST::TypePathSegment &segment) override;
void visit (AST::TypePathSegmentGeneric &segment) override;
void visit (AST::TypePath &path) override;
void visit (AST::LangItemPath &path) override;

protected:
HIR::TypePathSegment *translated_segment;

private:
HIR::TypePath *translated;

static HIR::TypePath *translate_type_path (AST::TypePath &type);
static HIR::TypePath *translate_lang_item_type_path (AST::LangItemPath &type);
};

class ASTLowerQualifiedPathInType : public ASTLowerTypePath
Expand Down
3 changes: 3 additions & 0 deletions gcc/rust/resolve/rust-ast-resolve-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class ResolveType : public ResolverBase
.lookup_lang_item_node (type.get_lang_item_kind ())
.value ();

auto resolver = Resolver::get ();
resolver->insert_resolved_type (type.get_node_id (), lang_item);

return lang_item;
}

Expand Down

0 comments on commit 408aeed

Please sign in to comment.