Skip to content

Commit

Permalink
backend: Improve handling of lang-item PathInExpressions
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* backend/rust-compile-resolve-path.cc (ResolvePathRef::visit): Call into
	resolve_path_like instead.
	(ResolvePathRef::resolve_path_like): New.
	(ResolvePathRef::resolve): Call into resolve_with_node_id.
	* backend/rust-compile-resolve-path.h: Declare new functions and document them.
  • Loading branch information
CohenArthur committed Jan 28, 2025
1 parent 35e1931 commit 9053922
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 47 deletions.
116 changes: 69 additions & 47 deletions gcc/rust/backend/rust-compile-resolve-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,39 @@
namespace Rust {
namespace Compile {

void
ResolvePathRef::visit (HIR::QualifiedPathInExpression &expr)
template <typename T>
tree
ResolvePathRef::resolve_path_like (T &expr)
{
auto final_segment = HIR::PathIdentSegment::create_error ();
if (expr.is_lang_item ())
final_segment
= HIR::PathIdentSegment (LangItem::ToString (expr.get_lang_item ()));
{
auto lang_item
= Analysis::Mappings::get ().get_lang_item_node (expr.get_lang_item ());

// FIXME: Is that correct? :/
auto final_segment
= HIR::PathIdentSegment (LangItem::ToString (expr.get_lang_item ()));

return resolve_with_node_id (final_segment, expr.get_mappings (),
expr.get_locus (), true, lang_item);
}
else
final_segment = expr.get_final_segment ().get_segment ();
{
return resolve (expr.get_final_segment ().get_segment (),
expr.get_mappings (), expr.get_locus (), true);
}
}

resolved
= resolve (final_segment, expr.get_mappings (), expr.get_locus (), true);
void
ResolvePathRef::visit (HIR::QualifiedPathInExpression &expr)
{
resolved = resolve_path_like (expr);
}

void
ResolvePathRef::visit (HIR::PathInExpression &expr)
{
auto final_segment = HIR::PathIdentSegment::create_error ();
if (expr.is_lang_item ())
final_segment
= HIR::PathIdentSegment (LangItem::ToString (expr.get_lang_item ()));
else
final_segment = expr.get_final_segment ().get_segment ();

resolved
= resolve (final_segment, expr.get_mappings (), expr.get_locus (), true);
resolved = resolve_path_like (expr);
}

tree
Expand Down Expand Up @@ -106,42 +113,17 @@ ResolvePathRef::attempt_constructor_expression_lookup (
}

tree
ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
const Analysis::NodeMapping &mappings,
location_t expr_locus, bool is_qualified_path)
ResolvePathRef::resolve_with_node_id (
const HIR::PathIdentSegment &final_segment,
const Analysis::NodeMapping &mappings, location_t expr_locus,
bool is_qualified_path, NodeId resolved_node_id)
{
TyTy::BaseType *lookup = nullptr;
bool ok = ctx->get_tyctx ()->lookup_type (mappings.get_hirid (), &lookup);
rust_assert (ok);

// need to look up the reference for this identifier

// this can fail because it might be a Constructor for something
// in that case the caller should attempt ResolvePathType::Compile
NodeId ref_node_id = UNKNOWN_NODEID;
if (flag_name_resolution_2_0)
{
auto nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();

auto resolved = nr_ctx.lookup (mappings.get_nodeid ());

if (!resolved)
return attempt_constructor_expression_lookup (lookup, ctx, mappings,
expr_locus);

ref_node_id = *resolved;
}
else
{
if (!ctx->get_resolver ()->lookup_resolved_name (mappings.get_nodeid (),
&ref_node_id))
return attempt_constructor_expression_lookup (lookup, ctx, mappings,
expr_locus);
}

tl::optional<HirId> hid
= ctx->get_mappings ().lookup_node_to_hir (ref_node_id);
= ctx->get_mappings ().lookup_node_to_hir (resolved_node_id);
if (!hid.has_value ())
{
rust_error_at (expr_locus, "reverse call path lookup failure");
Expand Down Expand Up @@ -207,9 +189,49 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
{
TREE_USED (resolved_item) = 1;
}

return resolved_item;
}

tree
ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
const Analysis::NodeMapping &mappings,
location_t expr_locus, bool is_qualified_path)
{
TyTy::BaseType *lookup = nullptr;
bool ok = ctx->get_tyctx ()->lookup_type (mappings.get_hirid (), &lookup);
rust_assert (ok);

// need to look up the reference for this identifier

// this can fail because it might be a Constructor for something
// in that case the caller should attempt ResolvePathType::Compile
NodeId ref_node_id = UNKNOWN_NODEID;
if (flag_name_resolution_2_0)
{
auto nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();

auto resolved = nr_ctx.lookup (mappings.get_nodeid ());

if (!resolved)
return attempt_constructor_expression_lookup (lookup, ctx, mappings,
expr_locus);

ref_node_id = *resolved;
}
else
{
if (!ctx->get_resolver ()->lookup_resolved_name (mappings.get_nodeid (),
&ref_node_id))
return attempt_constructor_expression_lookup (lookup, ctx, mappings,
expr_locus);
}

return resolve_with_node_id (final_segment, mappings, expr_locus,
is_qualified_path, ref_node_id);
}

tree
HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
const HIR::PathIdentSegment &final_segment,
Expand Down
16 changes: 16 additions & 0 deletions gcc/rust/backend/rust-compile-resolve-path.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ class ResolvePathRef : public HIRCompileBase, public HIR::HIRPatternVisitor
: HIRCompileBase (ctx), resolved (error_mark_node)
{}

/**
* Generic visitor for both PathInExpression and QualifiedPathInExpression
*/
template <typename T> tree resolve_path_like (T &expr);

/**
* Inner implementation of `resolve` - resolution with an already known NodeId
*/
tree resolve_with_node_id (const HIR::PathIdentSegment &final_segment,
const Analysis::NodeMapping &mappings,
location_t locus, bool is_qualified_path,
NodeId resolved_node_id);
/**
* Resolve a mappings' NodeId and call into `resolve_with_node_id` which
* performs the rest of the path resolution
*/
tree resolve (const HIR::PathIdentSegment &final_segment,
const Analysis::NodeMapping &mappings, location_t locus,
bool is_qualified_path);
Expand Down

0 comments on commit 9053922

Please sign in to comment.