[gcc/devel/rust/master] Refactor helper from TypeCheckResolveToplevel
Thomas Schwinge
tschwinge@gcc.gnu.org
Thu Aug 11 19:19:26 GMT 2022
https://gcc.gnu.org/g:630ef83dfc9a62ef0b0b822b34bfd7e3389b909d
commit 630ef83dfc9a62ef0b0b822b34bfd7e3389b909d
Author: Philip Herron <philip.herron@embecosm.com>
Date: Wed Aug 10 14:47:44 2022 +0100
Refactor helper from TypeCheckResolveToplevel
The reason we cannot type-resolve forward declared items is due to the
fact our type-resolver runs in a toplevel pass trying to type resolve as
best it can but when we hit structs for example which contain types that
are forward declared they need to be resolved in a query based manar. We
have all the missing pieces to do this now and this means we need to get
rid of this toplevel pass as the starting point.
Addresses #1455
Diff:
---
gcc/rust/typecheck/rust-hir-type-check-base.cc | 46 ++++++++++++++++++++++
gcc/rust/typecheck/rust-hir-type-check-base.h | 4 ++
gcc/rust/typecheck/rust-hir-type-check-toplevel.cc | 44 ---------------------
gcc/rust/typecheck/rust-hir-type-check-toplevel.h | 4 --
4 files changed, 50 insertions(+), 48 deletions(-)
diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc
index c50199847b7..e263056a82c 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc
@@ -17,6 +17,8 @@
// <http://www.gnu.org/licenses/>.
#include "rust-hir-type-check-base.h"
+#include "rust-hir-type-check-type.h"
+#include "rust-hir-type-check-expr.h"
#include "rust-coercion.h"
namespace Rust {
@@ -349,5 +351,49 @@ TypeCheckBase::coercion_site (HirId id, TyTy::BaseType *expected,
return expected->coerce (expr);
}
+void
+TypeCheckBase::resolve_generic_params (
+ const std::vector<std::unique_ptr<HIR::GenericParam>> &generic_params,
+ std::vector<TyTy::SubstitutionParamMapping> &substitutions)
+{
+ for (auto &generic_param : generic_params)
+ {
+ switch (generic_param.get ()->get_kind ())
+ {
+ case HIR::GenericParam::GenericKind::LIFETIME:
+ // FIXME: Skipping Lifetime completely until better
+ // handling.
+ break;
+ case HIR::GenericParam::GenericKind::CONST: {
+ auto param
+ = static_cast<HIR::ConstGenericParam *> (generic_param.get ());
+ auto specified_type
+ = TypeCheckType::Resolve (param->get_type ().get ());
+
+ if (param->has_default_expression ())
+ {
+ auto expr_type = TypeCheckExpr::Resolve (
+ param->get_default_expression ().get ());
+ specified_type->coerce (expr_type);
+ }
+
+ context->insert_type (generic_param->get_mappings (),
+ specified_type);
+ }
+ break;
+
+ case HIR::GenericParam::GenericKind::TYPE: {
+ auto param_type
+ = TypeResolveGenericParam::Resolve (generic_param.get ());
+ context->insert_type (generic_param->get_mappings (), param_type);
+
+ substitutions.push_back (TyTy::SubstitutionParamMapping (
+ static_cast<HIR::TypeParam &> (*generic_param), param_type));
+ }
+ break;
+ }
+ }
+}
+
} // namespace Resolver
} // namespace Rust
diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.h b/gcc/rust/typecheck/rust-hir-type-check-base.h
index 4078697c927..b6f4b636789 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.h
@@ -64,6 +64,10 @@ protected:
TyTy::ADTType::ReprOptions parse_repr_options (const AST::AttrVec &attrs,
Location locus);
+ void resolve_generic_params (
+ const std::vector<std::unique_ptr<HIR::GenericParam>> &generic_params,
+ std::vector<TyTy::SubstitutionParamMapping> &substitutions);
+
Analysis::Mappings *mappings;
Resolver *resolver;
TypeCheckContext *context;
diff --git a/gcc/rust/typecheck/rust-hir-type-check-toplevel.cc b/gcc/rust/typecheck/rust-hir-type-check-toplevel.cc
index 69377e28d22..d5270c9acbb 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-toplevel.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-toplevel.cc
@@ -28,50 +28,6 @@ TypeCheckTopLevel::Resolve (HIR::Item *item)
item->accept_vis (resolver);
}
-void
-TypeCheckTopLevel::resolve_generic_params (
- const std::vector<std::unique_ptr<HIR::GenericParam>> &generic_params,
- std::vector<TyTy::SubstitutionParamMapping> &substitutions)
-{
- for (auto &generic_param : generic_params)
- {
- switch (generic_param.get ()->get_kind ())
- {
- case HIR::GenericParam::GenericKind::LIFETIME:
- // FIXME: Skipping Lifetime completely until better
- // handling.
- break;
- case HIR::GenericParam::GenericKind::CONST: {
- auto param
- = static_cast<HIR::ConstGenericParam *> (generic_param.get ());
- auto specified_type
- = TypeCheckType::Resolve (param->get_type ().get ());
-
- if (param->has_default_expression ())
- {
- auto expr_type = TypeCheckExpr::Resolve (
- param->get_default_expression ().get ());
- specified_type->coerce (expr_type);
- }
-
- context->insert_type (generic_param->get_mappings (),
- specified_type);
- }
- break;
-
- case HIR::GenericParam::GenericKind::TYPE: {
- auto param_type
- = TypeResolveGenericParam::Resolve (generic_param.get ());
- context->insert_type (generic_param->get_mappings (), param_type);
-
- substitutions.push_back (TyTy::SubstitutionParamMapping (
- static_cast<HIR::TypeParam &> (*generic_param), param_type));
- }
- break;
- }
- }
-}
-
void
TypeCheckTopLevel::visit (HIR::TypeAlias &alias)
{
diff --git a/gcc/rust/typecheck/rust-hir-type-check-toplevel.h b/gcc/rust/typecheck/rust-hir-type-check-toplevel.h
index fdc13f7684d..d2785b15e54 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-toplevel.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-toplevel.h
@@ -51,10 +51,6 @@ public:
private:
TypeCheckTopLevel () : TypeCheckBase () {}
-
- void resolve_generic_params (
- const std::vector<std::unique_ptr<HIR::GenericParam>> &generic_params,
- std::vector<TyTy::SubstitutionParamMapping> &substitutions);
};
} // namespace Resolver
More information about the Gcc-cvs
mailing list