aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend/rust-compile-resolve-path.cc
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/rust/backend/rust-compile-resolve-path.cc')
-rw-r--r--gcc/rust/backend/rust-compile-resolve-path.cc277
1 files changed, 147 insertions, 130 deletions
diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc
index 1539378..9cceb1e 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -89,164 +89,181 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
TyTy::BaseType *lookup = nullptr;
bool ok = ctx->get_tyctx ()->lookup_type (mappings.get_hirid (), &lookup);
rust_assert (ok);
- rust_assert (lookup->get_kind () == TyTy::TypeKind::FNDEF);
- TyTy::FnType *fntype = static_cast<TyTy::FnType *> (lookup);
+ if (lookup->get_kind () == TyTy::TypeKind::FNDEF)
+ {
+ TyTy::FnType *fntype = static_cast<TyTy::FnType *> (lookup);
- Bfunction *fn = nullptr;
- if (!ctx->lookup_function_decl (lookup->get_ty_ref (), &fn))
+ Bfunction *fn = nullptr;
+ if (ctx->lookup_function_decl (fntype->get_ty_ref (), &fn))
+ {
+ resolved
+ = ctx->get_backend ()->function_code_expression (fn, expr_locus);
+ }
+ else
+ {
+ resolved
+ = query_compile_function (ref, fntype, final_segment, mappings,
+ expr_locus, is_qualified_path);
+ }
+ }
+ else
{
- // it must resolve to some kind of HIR::Item or HIR::InheritImplItem
HIR::Item *resolved_item
= ctx->get_mappings ()->lookup_hir_item (mappings.get_crate_num (),
ref);
- if (resolved_item != nullptr)
+ HirId parent_impl_id = UNKNOWN_HIRID;
+ HIR::ImplItem *resolved_impl_item
+ = ctx->get_mappings ()->lookup_hir_implitem (mappings.get_crate_num (),
+ ref, &parent_impl_id);
+ bool is_impl_item = resolved_impl_item != nullptr;
+ bool is_item = resolved_item != nullptr;
+
+ gcc_unreachable ();
+ }
+}
+
+Bexpression *
+ResolvePathRef::query_compile_function (
+ HirId ref, TyTy::FnType *fntype, const HIR::PathIdentSegment &final_segment,
+ const Analysis::NodeMapping &mappings, Location expr_locus,
+ bool is_qualified_path)
+{
+ HIR::Item *resolved_item
+ = ctx->get_mappings ()->lookup_hir_item (mappings.get_crate_num (), ref);
+ bool is_hir_item = resolved_item != nullptr;
+ if (is_hir_item)
+ {
+ if (!fntype->has_subsititions_defined ())
+ CompileItem::compile (resolved_item, ctx);
+ else
+ CompileItem::compile (resolved_item, ctx, true, fntype);
+ }
+ else
+ {
+ HirId parent_impl_id = UNKNOWN_HIRID;
+ HIR::ImplItem *resolved_item
+ = ctx->get_mappings ()->lookup_hir_implitem (mappings.get_crate_num (),
+ ref, &parent_impl_id);
+ bool is_impl_item = resolved_item != nullptr;
+ if (is_impl_item)
{
- if (!lookup->has_subsititions_defined ())
- CompileItem::compile (resolved_item, ctx);
+ rust_assert (parent_impl_id != UNKNOWN_HIRID);
+ HIR::Item *impl_ref
+ = ctx->get_mappings ()->lookup_hir_item (mappings.get_crate_num (),
+ parent_impl_id);
+ rust_assert (impl_ref != nullptr);
+ HIR::ImplBlock *impl = static_cast<HIR::ImplBlock *> (impl_ref);
+
+ TyTy::BaseType *self = nullptr;
+ bool ok = ctx->get_tyctx ()->lookup_type (
+ impl->get_type ()->get_mappings ().get_hirid (), &self);
+ rust_assert (ok);
+
+ if (!fntype->has_subsititions_defined ())
+ CompileInherentImplItem::Compile (self, resolved_item, ctx, true);
else
- CompileItem::compile (resolved_item, ctx, true, lookup);
+ CompileInherentImplItem::Compile (self, resolved_item, ctx, true,
+ fntype);
}
else
{
- HirId parent_impl_id = UNKNOWN_HIRID;
- HIR::ImplItem *resolved_item
- = ctx->get_mappings ()->lookup_hir_implitem (
- mappings.get_crate_num (), ref, &parent_impl_id);
-
- if (resolved_item == nullptr)
+ // it might be resolved to a trait item
+ HIR::TraitItem *trait_item
+ = ctx->get_mappings ()->lookup_hir_trait_item (
+ mappings.get_crate_num (), ref);
+ HIR::Trait *trait = ctx->get_mappings ()->lookup_trait_item_mapping (
+ trait_item->get_mappings ().get_hirid ());
+
+ Resolver::TraitReference *trait_ref
+ = &Resolver::TraitReference::error_node ();
+ bool ok = ctx->get_tyctx ()->lookup_trait_reference (
+ trait->get_mappings ().get_defid (), &trait_ref);
+ rust_assert (ok);
+
+ TyTy::BaseType *receiver = nullptr;
+ ok = ctx->get_tyctx ()->lookup_receiver (mappings.get_hirid (),
+ &receiver);
+ rust_assert (ok);
+
+ if (receiver->get_kind () == TyTy::TypeKind::PARAM)
{
- // it might be resolved to a trait item
- HIR::TraitItem *trait_item
- = ctx->get_mappings ()->lookup_hir_trait_item (
- mappings.get_crate_num (), ref);
- HIR::Trait *trait
- = ctx->get_mappings ()->lookup_trait_item_mapping (
- trait_item->get_mappings ().get_hirid ());
-
- Resolver::TraitReference *trait_ref
- = &Resolver::TraitReference::error_node ();
- bool ok = ctx->get_tyctx ()->lookup_trait_reference (
- trait->get_mappings ().get_defid (), &trait_ref);
- rust_assert (ok);
+ TyTy::ParamType *p = static_cast<TyTy::ParamType *> (receiver);
+ receiver = p->resolve ();
+ }
- TyTy::BaseType *receiver = nullptr;
- ok = ctx->get_tyctx ()->lookup_receiver (mappings.get_hirid (),
- &receiver);
- rust_assert (ok);
+ // the type resolver can only resolve type bounds to their trait
+ // item so its up to us to figure out if this path should resolve
+ // to an trait-impl-block-item or if it can be defaulted to the
+ // trait-impl-item's definition
+ std::vector<Resolver::PathProbeCandidate> candidates;
+ if (!is_qualified_path)
+ {
+ candidates
+ = Resolver::PathProbeType::Probe (receiver, final_segment, true,
+ false, true);
+ }
- if (receiver->get_kind () == TyTy::TypeKind::PARAM)
- {
- TyTy::ParamType *p
- = static_cast<TyTy::ParamType *> (receiver);
- receiver = p->resolve ();
- }
-
- // the type resolver can only resolve type bounds to their trait
- // item so its up to us to figure out if this path should resolve
- // to an trait-impl-block-item or if it can be defaulted to the
- // trait-impl-item's definition
- std::vector<Resolver::PathProbeCandidate> candidates;
- if (!is_qualified_path)
- {
- candidates
- = Resolver::PathProbeType::Probe (receiver, final_segment,
- true, false, true);
- }
-
- if (candidates.size () == 0)
- {
- // this means we are defaulting back to the trait_item if
- // possible
- Resolver::TraitItemReference *trait_item_ref = nullptr;
- bool ok = trait_ref->lookup_hir_trait_item (*trait_item,
- &trait_item_ref);
- rust_assert (ok); // found
- rust_assert (
- trait_item_ref->is_optional ()); // has definition
-
- Analysis::NodeMapping trait_mappings
- = trait_item_ref->get_parent_trait_mappings ();
- auto associated_impl_id
- = ctx->get_tyctx ()
- ->lookup_associated_impl_mapping_for_self (
- trait_mappings.get_hirid (), receiver);
-
- rust_assert (associated_impl_id != UNKNOWN_HIRID);
-
- Resolver::AssociatedImplTrait *associated = nullptr;
- bool found_associated_trait_impl
- = ctx->get_tyctx ()->lookup_associated_trait_impl (
- associated_impl_id, &associated);
- rust_assert (found_associated_trait_impl);
- associated->setup_associated_types ();
-
- CompileTraitItem::Compile (
- receiver, trait_item_ref->get_hir_trait_item (), ctx,
- fntype);
-
- if (!ctx->lookup_function_decl (fntype->get_ty_ref (), &fn))
- {
- resolved = ctx->get_backend ()->error_expression ();
- rust_error_at (expr_locus,
- "forward declaration was not compiled");
- return;
- }
- }
- else
- {
- Resolver::PathProbeCandidate &candidate = candidates.at (0);
- rust_assert (candidate.is_impl_candidate ());
-
- HIR::ImplBlock *impl = candidate.item.impl.parent;
- HIR::ImplItem *impl_item = candidate.item.impl.impl_item;
-
- TyTy::BaseType *self = nullptr;
- bool ok = ctx->get_tyctx ()->lookup_type (
- impl->get_type ()->get_mappings ().get_hirid (), &self);
- rust_assert (ok);
-
- if (!lookup->has_subsititions_defined ())
- CompileInherentImplItem::Compile (self, impl_item, ctx,
- true);
- else
- CompileInherentImplItem::Compile (self, impl_item, ctx,
- true, lookup);
-
- lookup->set_ty_ref (
- impl_item->get_impl_mappings ().get_hirid ());
- }
+ if (candidates.size () == 0)
+ {
+ // this means we are defaulting back to the trait_item if
+ // possible
+ Resolver::TraitItemReference *trait_item_ref = nullptr;
+ bool ok = trait_ref->lookup_hir_trait_item (*trait_item,
+ &trait_item_ref);
+ rust_assert (ok); // found
+ rust_assert (trait_item_ref->is_optional ()); // has definition
+
+ Analysis::NodeMapping trait_mappings
+ = trait_item_ref->get_parent_trait_mappings ();
+ auto associated_impl_id
+ = ctx->get_tyctx ()->lookup_associated_impl_mapping_for_self (
+ trait_mappings.get_hirid (), receiver);
+
+ rust_assert (associated_impl_id != UNKNOWN_HIRID);
+
+ Resolver::AssociatedImplTrait *associated = nullptr;
+ bool found_associated_trait_impl
+ = ctx->get_tyctx ()->lookup_associated_trait_impl (
+ associated_impl_id, &associated);
+ rust_assert (found_associated_trait_impl);
+ associated->setup_associated_types ();
+
+ CompileTraitItem::Compile (receiver,
+ trait_item_ref->get_hir_trait_item (),
+ ctx, fntype);
}
else
{
- rust_assert (parent_impl_id != UNKNOWN_HIRID);
- HIR::Item *impl_ref = ctx->get_mappings ()->lookup_hir_item (
- mappings.get_crate_num (), parent_impl_id);
- rust_assert (impl_ref != nullptr);
- HIR::ImplBlock *impl = static_cast<HIR::ImplBlock *> (impl_ref);
+ Resolver::PathProbeCandidate &candidate = candidates.at (0);
+ rust_assert (candidate.is_impl_candidate ());
+
+ HIR::ImplBlock *impl = candidate.item.impl.parent;
+ HIR::ImplItem *impl_item = candidate.item.impl.impl_item;
TyTy::BaseType *self = nullptr;
bool ok = ctx->get_tyctx ()->lookup_type (
impl->get_type ()->get_mappings ().get_hirid (), &self);
rust_assert (ok);
- if (!lookup->has_subsititions_defined ())
- CompileInherentImplItem::Compile (self, resolved_item, ctx,
- true);
+ if (!fntype->has_subsititions_defined ())
+ CompileInherentImplItem::Compile (self, impl_item, ctx, true);
else
- CompileInherentImplItem::Compile (self, resolved_item, ctx,
- true, lookup);
+ CompileInherentImplItem::Compile (self, impl_item, ctx, true,
+ fntype);
+
+ fntype->set_ty_ref (impl_item->get_impl_mappings ().get_hirid ());
}
}
+ }
- if (!ctx->lookup_function_decl (lookup->get_ty_ref (), &fn))
- {
- resolved = ctx->get_backend ()->error_expression ();
- rust_error_at (expr_locus, "forward declaration was not compiled");
- return;
- }
+ Bfunction *fn;
+ if (!ctx->lookup_function_decl (fntype->get_ty_ref (), &fn))
+ {
+ rust_error_at (expr_locus, "forward declaration was not compiled");
+ return ctx->get_backend ()->error_expression ();
}
- resolved = ctx->get_backend ()->function_code_expression (fn, expr_locus);
+ return ctx->get_backend ()->function_code_expression (fn, expr_locus);
}
} // namespace Compile