diff options
author | Philip Herron <herron.philip@googlemail.com> | 2023-04-04 15:49:20 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2023-04-05 09:24:55 +0000 |
commit | f83e63a89a030fce62c8de56b3c1e61cec77ef52 (patch) | |
tree | 57a9fb7ca2c5cd967d86c78680788a96067ff8b9 /gcc/rust | |
parent | 4713107dffad79ac0c7863411928b536f90f768a (diff) | |
download | gcc-f83e63a89a030fce62c8de56b3c1e61cec77ef52.zip gcc-f83e63a89a030fce62c8de56b3c1e61cec77ef52.tar.gz gcc-f83e63a89a030fce62c8de56b3c1e61cec77ef52.tar.bz2 |
gccrs: refactor resolve_method_address to be inside base class
gcc/rust/ChangeLog:
* backend/rust-compile-base.cc (HIRCompileBase::resolve_method_address): moved here
* backend/rust-compile-base.h: refactored prototype
* backend/rust-compile-expr.cc (CompileExpr::resolve_method_address): refactor
* backend/rust-compile-expr.h: likewise
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Diffstat (limited to 'gcc/rust')
-rw-r--r-- | gcc/rust/backend/rust-compile-base.cc | 85 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-base.h | 6 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.cc | 85 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.h | 6 |
4 files changed, 91 insertions, 91 deletions
diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 80c2391..9650d1a 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -27,6 +27,9 @@ #include "rust-diagnostics.h" #include "rust-expr.h" // for AST::AttrInputLiteral #include "rust-macro.h" // for AST::MetaNameValueStr +#include "rust-hir-path-probe.h" +#include "rust-type-util.h" +#include "rust-compile-implitem.h" #include "fold-const.h" #include "stringpool.h" @@ -739,5 +742,87 @@ HIRCompileBase::named_constant_expression (tree type_tree, return decl; } +tree +HIRCompileBase::resolve_method_address ( + TyTy::FnType *fntype, HirId ref, TyTy::BaseType *receiver, + const HIR::PathIdentSegment &segment, + const Analysis::NodeMapping &expr_mappings, Location expr_locus) +{ + // Now we can try and resolve the address since this might be a forward + // declared function, generic function which has not be compiled yet or + // its an not yet trait bound function + HIR::ImplItem *resolved_item + = ctx->get_mappings ()->lookup_hir_implitem (ref, nullptr); + if (resolved_item != nullptr) + { + if (!fntype->has_subsititions_defined ()) + return CompileInherentImplItem::Compile (resolved_item, ctx); + + return CompileInherentImplItem::Compile (resolved_item, ctx, fntype); + } + + // it might be resolved to a trait item + HIR::TraitItem *trait_item + = ctx->get_mappings ()->lookup_hir_trait_item (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); + + // 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 + + auto root = receiver->get_root (); + auto candidates + = Resolver::PathProbeImplTrait::Probe (root, segment, trait_ref); + 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 + + // FIXME Optional means it has a definition and an associated + // block which can be a default implementation, if it does not + // contain an implementation we should actually return + // error_mark_node + + return CompileTraitItem::Compile (trait_item_ref->get_hir_trait_item (), + ctx, fntype, true, expr_locus); + } + + // FIXME this will be a case to return error_mark_node, there is + // an error scenario where a Trait Foo has a method Bar, but this + // receiver does not implement this trait or has an incompatible + // implementation and we should just return error_mark_node + + rust_assert (candidates.size () == 1); + auto &candidate = *candidates.begin (); + rust_assert (candidate.is_impl_candidate ()); + rust_assert (candidate.ty->get_kind () == TyTy::TypeKind::FNDEF); + TyTy::FnType *candidate_call = static_cast<TyTy::FnType *> (candidate.ty); + HIR::ImplItem *impl_item = candidate.item.impl.impl_item; + + TyTy::BaseType *monomorphized = candidate_call; + if (candidate_call->needs_generic_substitutions ()) + { + TyTy::BaseType *infer_impl_call + = candidate_call->infer_substitions (expr_locus); + monomorphized + = Resolver::unify_site (ref, TyTy::TyWithLocation (infer_impl_call), + TyTy::TyWithLocation (fntype), expr_locus); + } + + return CompileInherentImplItem::Compile (impl_item, ctx, monomorphized); +} + } // namespace Compile } // namespace Rust diff --git a/gcc/rust/backend/rust-compile-base.h b/gcc/rust/backend/rust-compile-base.h index 281761e..d276ec3 100644 --- a/gcc/rust/backend/rust-compile-base.h +++ b/gcc/rust/backend/rust-compile-base.h @@ -80,6 +80,12 @@ protected: tree resolve_unsized_dyn_adjustment (Resolver::Adjustment &adjustment, tree expression, Location locus); + tree resolve_method_address (TyTy::FnType *fntype, HirId ref, + TyTy::BaseType *receiver, + const HIR::PathIdentSegment &segment, + const Analysis::NodeMapping &expr_mappings, + Location expr_locus); + static void setup_fndecl (tree fndecl, bool is_main_entry_point, bool is_generic_fn, HIR::Visibility &visibility, const HIR::FunctionQualifiers &qualifiers, diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 90f1b14..7a04e2b 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -18,13 +18,11 @@ #include "rust-compile-expr.h" #include "rust-compile-struct-field-expr.h" -#include "rust-hir-path-probe.h" #include "rust-compile-pattern.h" #include "rust-compile-resolve-path.h" #include "rust-compile-block.h" #include "rust-compile-implitem.h" #include "rust-constexpr.h" -#include "rust-type-util.h" #include "rust-compile-type.h" #include "rust-gcc.h" @@ -1881,89 +1879,6 @@ CompileExpr::get_receiver_from_dyn (const TyTy::DynamicObjectType *dyn, } tree -CompileExpr::resolve_method_address (TyTy::FnType *fntype, HirId ref, - TyTy::BaseType *receiver, - HIR::PathIdentSegment &segment, - Analysis::NodeMapping expr_mappings, - Location expr_locus) -{ - // Now we can try and resolve the address since this might be a forward - // declared function, generic function which has not be compiled yet or - // its an not yet trait bound function - HIR::ImplItem *resolved_item - = ctx->get_mappings ()->lookup_hir_implitem (ref, nullptr); - if (resolved_item != nullptr) - { - if (!fntype->has_subsititions_defined ()) - return CompileInherentImplItem::Compile (resolved_item, ctx); - - return CompileInherentImplItem::Compile (resolved_item, ctx, fntype); - } - - // it might be resolved to a trait item - HIR::TraitItem *trait_item - = ctx->get_mappings ()->lookup_hir_trait_item (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); - - // 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 - - auto root = receiver->get_root (); - auto candidates - = Resolver::PathProbeImplTrait::Probe (root, segment, trait_ref); - 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 - - // FIXME Optional means it has a definition and an associated - // block which can be a default implementation, if it does not - // contain an implementation we should actually return - // error_mark_node - - return CompileTraitItem::Compile (trait_item_ref->get_hir_trait_item (), - ctx, fntype, true, expr_locus); - } - - // FIXME this will be a case to return error_mark_node, there is - // an error scenario where a Trait Foo has a method Bar, but this - // receiver does not implement this trait or has an incompatible - // implementation and we should just return error_mark_node - - rust_assert (candidates.size () == 1); - auto &candidate = *candidates.begin (); - rust_assert (candidate.is_impl_candidate ()); - rust_assert (candidate.ty->get_kind () == TyTy::TypeKind::FNDEF); - TyTy::FnType *candidate_call = static_cast<TyTy::FnType *> (candidate.ty); - HIR::ImplItem *impl_item = candidate.item.impl.impl_item; - - TyTy::BaseType *monomorphized = candidate_call; - if (candidate_call->needs_generic_substitutions ()) - { - TyTy::BaseType *infer_impl_call - = candidate_call->infer_substitions (expr_locus); - monomorphized - = Resolver::unify_site (ref, TyTy::TyWithLocation (infer_impl_call), - TyTy::TyWithLocation (fntype), expr_locus); - } - - return CompileInherentImplItem::Compile (impl_item, ctx, monomorphized); -} - -tree CompileExpr::resolve_operator_overload ( Analysis::RustLangItem::ItemType lang_item_type, HIR::OperatorExprMeta expr, tree lhs, tree rhs, HIR::Expr *lhs_expr, HIR::Expr *rhs_expr) diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index 5f6e54b..0962652 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -97,12 +97,6 @@ protected: TyTy::BaseType *receiver, TyTy::FnType *fntype, tree receiver_ref, Location expr_locus); - tree resolve_method_address (TyTy::FnType *fntype, HirId ref, - TyTy::BaseType *receiver, - HIR::PathIdentSegment &segment, - Analysis::NodeMapping expr_mappings, - Location expr_locus); - tree resolve_operator_overload (Analysis::RustLangItem::ItemType lang_item_type, HIR::OperatorExprMeta expr, tree lhs, tree rhs, |