diff options
author | John Hodge <tpg@mutabah.net> | 2015-04-05 13:47:01 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2015-04-05 13:47:01 +0800 |
commit | cae16a7a72a75f08e01b63ef31a56e75125556b6 (patch) | |
tree | 0413915c3eb2925585d844bf7f72d1abd2a0d687 /src/ast/ast.cpp | |
parent | 89788c5801e03b0c5b0b288d0fefb717f75c67f0 (diff) | |
download | mrust-cae16a7a72a75f08e01b63ef31a56e75125556b6.tar.gz |
Move handling of wildcard trait destructure to TypeRef
Diffstat (limited to 'src/ast/ast.cpp')
-rw-r--r-- | src/ast/ast.cpp | 80 |
1 files changed, 1 insertions, 79 deletions
diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 63e33ba6..aadade4f 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -10,26 +10,6 @@ namespace AST {
-class GenericResolveClosure
-{
- const TypeParams& m_params;
- const ::std::vector<TypeRef>& m_args;
-public:
- GenericResolveClosure(const TypeParams& params, const ::std::vector<TypeRef>& args):
- m_params(params),
- m_args(args)
- {}
- TypeRef operator()(const char *argname) {
- for(unsigned int i = 0; i < m_params.ty_params().size(); i ++)
- {
- if( m_params.ty_params()[i].name() == argname ) {
- return m_args.at(i);
- }
- }
- throw ::std::runtime_error("BUGCHECK - Unknown arg in field type");
- }
-};
-
void MetaItems::push_back(MetaItem i)
{
@@ -278,65 +258,7 @@ bool Crate::check_impls_wildcard(const Path& trait, const TypeRef& type) DEBUG("No positive impl of " << trait << " for " << type);
// 3. If none found, destructure the type
- // Primitives always impl
- if( type.is_primitive() || type.is_unit() ) {
- return true;
- }
- else if( type.is_reference() ) {
- return find_impl(trait, type.sub_types()[0], nullptr);
- }
- else if( type.is_pointer() ) {
- return find_impl(trait, type.sub_types()[0], nullptr);
- }
- else if( type.is_type_param() ) {
- // TODO: Include an annotation to the TypeParams structure relevant to this type
- // - Allows searching the params for the impl, without having to pass the params down
- throw CompileError::Todo("check_impls_wildcard - param");
- }
- else if( type.is_path() ) {
- // - structs need all fields to impl this trait (cache result)
- // - same for enums, tuples, arrays, pointers...
- // - traits check the Self bounds
- // CATCH: Need to handle recursion, keep a list of currently processing paths and assume true if found
- switch(type.path().binding().type())
- {
- case AST::PathBinding::STRUCT: {
- const auto &s = type.path().binding().bound_struct();
- GenericResolveClosure resolve_fn( s.params(), type.path().nodes().back().args() );
- for(const auto& fld : s.fields())
- {
- TypeRef fld_ty = fld.data;
- fld_ty.resolve_args( resolve_fn );
- DEBUG("- Fld '" << fld.name << "' := " << fld.data << " => " << fld_ty);
- // TODO: Defer failure until after all fields are processed
- if( !find_impl(trait, fld_ty, nullptr) )
- return false;
- }
- return true; }
- case AST::PathBinding::ENUM: {
- const auto& i = type.path().binding().bound_enum();
- GenericResolveClosure resolve_fn( i.params(), type.path().nodes().back().args() );
- for( const auto& var : i.variants() )
- {
- for( const auto& ty : var.m_sub_types )
- {
- TypeRef real_ty = ty;
- real_ty.resolve_args( resolve_fn );
- DEBUG("- Var '" << var.m_name << "' := " << ty << " => " << real_ty);
- // TODO: Defer failure until after all fields are processed
- if( !find_impl(trait, real_ty, nullptr) )
- return false;
- }
- }
- return true; }
- default:
- throw CompileError::Todo("wildcard impls - auto determine path");
- }
- }
- else {
- DEBUG("type = " << type);
- throw CompileError::Todo("wildcard impls - auto determine");
- }
+ return type.impls_wildcard(*this, trait);
}
bool Crate::find_impl(const Path& trait, const TypeRef& type, Impl** out_impl)
|