From b372cfc36b5be1f73d3d79c399530cffd4ce444a Mon Sep 17 00:00:00 2001 From: John Hodge Date: Tue, 25 Aug 2015 12:21:41 +0800 Subject: Compiling once more --- src/convert/ast_iterate.cpp | 48 +++++++++++++++++++++++++++++--------- src/convert/typecheck_expr.cpp | 52 ++++++++++++++++++++++-------------------- src/include/rustic.hpp | 20 ++++++++++++++++ src/types.cpp | 6 +++++ src/types.hpp | 30 +++++++++++++++++++++--- 5 files changed, 117 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/convert/ast_iterate.cpp b/src/convert/ast_iterate.cpp index 32125eaf..72cfd009 100644 --- a/src/convert/ast_iterate.cpp +++ b/src/convert/ast_iterate.cpp @@ -9,16 +9,41 @@ void CASTIterator::handle_path(AST::Path& path, CASTIterator::PathMode pm) void CASTIterator::handle_type(TypeRef& type) { TRACE_FUNCTION_F("type = " << type); - if( type.is_path() ) + #define _(VAR, ...) case TypeData::VAR: { auto &ent = type.m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break; + switch(type.m_data.tag()) { - handle_path(type.path(), MODE_TYPE); - } - else - { - throw ::std::runtime_error("TODO: handle_type"); - //for(auto& subtype : type.sub_types()) - // handle_type(subtype); + _(None) + _(Any) + _(Unit) + _(Primitive) + _(Path, + handle_path(ent.path, MODE_TYPE); + ) + _(Tuple, + for(auto& subtype : ent.inner_types) + handle_type(subtype); + ) + _(Borrow, + handle_type(*ent.inner); + ) + _(Pointer, + handle_type(*ent.inner); + ) + _(Array, + handle_type(*ent.inner); + ) + _(Function, + handle_type(*ent.info.m_rettype); + for(auto& arg : ent.info.m_arg_types) + handle_type(arg); + ) + _(Generic) + _(TraitObject, + for(auto& trait : ent.traits) + handle_path(trait, MODE_TYPE); + ) } + #undef _ } void CASTIterator::handle_expr(AST::ExprNode& node) { @@ -80,7 +105,7 @@ void CASTIterator::handle_pattern(AST::Pattern& pat, const TypeRef& type_hint) else if( !type_hint.is_reference() ) throw ::std::runtime_error( FMT("Ref pattern on non-ref value: " << type_hint) ); else - handle_pattern(*v.sub, type_hint.sub_types()[0]); + handle_pattern(*v.sub, type_hint.inner_type()); break; } case AST::Pattern::Data::MaybeBind: throw ::std::runtime_error("Calling CASTIterator::handle_pattern on MAYBE_BIND, not valid"); @@ -104,13 +129,14 @@ void CASTIterator::handle_pattern(AST::Pattern& pat, const TypeRef& type_hint) } else { - if( type_hint.sub_types().size() != v.sub_patterns.size() ) + const auto& inner_types = type_hint.m_data.as_Tuple().inner_types; + if( inner_types.size() != v.sub_patterns.size() ) { throw ::std::runtime_error("Tuple pattern count doesn't match"); } for( unsigned int i = 0; i < v.sub_patterns.size(); i ++ ) { - handle_pattern(v.sub_patterns[i], type_hint.sub_types()[i]); + handle_pattern(v.sub_patterns[i], inner_types[i]); } } break; } diff --git a/src/convert/typecheck_expr.cpp b/src/convert/typecheck_expr.cpp index da64580d..8a4bc84f 100644 --- a/src/convert/typecheck_expr.cpp +++ b/src/convert/typecheck_expr.cpp @@ -119,7 +119,8 @@ void CTypeChecker::handle_params(AST::TypeParams& params) const auto& name = bound.test().type_param(); int i = params.find_name(name.c_str()); assert(i >= 0); - trs[name].add_trait( bound.bound() ); + // - Just assert that it's valid. + //trs[name].add_trait( bound.bound() ); } } @@ -403,7 +404,7 @@ void CTC_NodeVisitor::visit(AST::ExprNode_Field& node) unsigned int deref_count = 0; while( tr->is_reference() ) { - tr = &tr->sub_types()[0]; + tr = &tr->inner_type(); DEBUG("ExprNode_Field - ref deref to " << *tr); deref_count ++; } @@ -476,29 +477,30 @@ void CTC_NodeVisitor::visit(AST::ExprNode_CallMethod& node) // 1. Handle bounded wildcard types if( ltype.is_wildcard() ) { - if( ltype.traits().size() == 0 ) { - DEBUG("- Unconstrained wildcard, returning"); - return ; - } - - for( const auto& t : ltype.traits() ) - { - DEBUG("- Trait " << t.path()); - AST::Trait& trait = const_cast( t.path().binding().bound_trait() ); - // - Find method on one of them - for( auto& m : trait.functions() ) - { - DEBUG(" > method: " << m.name << " search: " << node.m_method.name()); - if( m.name == node.m_method.name() ) - { - DEBUG(" > Found method"); - fcnp = &m.data; - break; - } - } - if(fcnp) break; - } - if(fcnp) break; + throw ::std::runtime_error("TODO: _ in CallMethod"); + //if( ltype.traits().size() == 0 ) { + // DEBUG("- Unconstrained wildcard, returning"); + // return ; + //} + // + //for( const auto& t : ltype.traits() ) + //{ + // DEBUG("- Trait " << t.path()); + // AST::Trait& trait = const_cast( t.path().binding().bound_trait() ); + // // - Find method on one of them + // for( auto& m : trait.functions() ) + // { + // DEBUG(" > method: " << m.name << " search: " << node.m_method.name()); + // if( m.name == node.m_method.name() ) + // { + // DEBUG(" > Found method"); + // fcnp = &m.data; + // break; + // } + // } + // if(fcnp) break; + //} + //if(fcnp) break; } else { diff --git a/src/include/rustic.hpp b/src/include/rustic.hpp index 0e46777d..f986a91d 100644 --- a/src/include/rustic.hpp +++ b/src/include/rustic.hpp @@ -68,6 +68,16 @@ public: assert(is_some()); return m_data; } + + //template + //U match(::std::function if_some, ::Std::function if_none) const { + // if( m_set ) { + // return if_some(m_data); + // } + // else { + // return if_none(); + // } + //} }; template class option @@ -87,6 +97,16 @@ public: assert(is_some()); return *m_ptr; } + + //template + //U match(::std::function if_some, ::Std::function if_none) const { + // if( m_set ) { + // return if_some(*m_ptr); + // } + // else { + // return if_none(); + // } + //} }; template option Some(T data) { diff --git a/src/types.cpp b/src/types.cpp index 9924ff0f..4761846d 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -41,6 +41,12 @@ Type_Function::Type_Function(const Type_Function& other): m_arg_types(other.m_arg_types) { } +SERIALISE_TYPE_A(Type_Function::, "Type_Function", { + s.item( is_unsafe ); + s.item( m_abi ); + s.item( m_rettype ); + s.item( m_arg_types ); + }) Ordering Type_Function::ord(const Type_Function& x) const { diff --git a/src/types.hpp b/src/types.hpp index 8e869f19..9259fce8 100644 --- a/src/types.hpp +++ b/src/types.hpp @@ -97,12 +97,12 @@ TAGGED_ENUM(TypeData, None, class TypeRef: public Serialisable { - TypeData m_data; - /// A generic pointer, used for tagging with extra information /// e.g. The source TypeParams for GENERIC const void* m_tagged_ptr; public: + TypeData m_data; + TypeRef(TypeRef&& other) noexcept: m_data( mv$(other.m_data) ) {} @@ -130,7 +130,7 @@ public: } } TypeRef& operator=(const TypeRef& other) { - *this = TypeRef(other); + m_data = TypeRef(other).m_data; return *this; } @@ -240,7 +240,31 @@ public: bool is_reference() const { return m_data.is_Borrow(); } bool is_pointer() const { return m_data.is_Pointer(); } bool is_tuple() const { return m_data.is_Tuple(); } + + //::option as_tuple() const { + // switch(m_data.tag()) + // { + // } + //} + const TypeRef& inner_type() const { + switch(m_data.tag()) + { + case TypeData::Borrow: return *m_data.as_Borrow().inner; + case TypeData::Pointer: return *m_data.as_Pointer().inner; + case TypeData::Array: return *m_data.as_Array().inner; + default: throw ::std::runtime_error("Called inner_type on non-wrapper"); + } + } + TypeRef& inner_type() { + switch(m_data.tag()) + { + case TypeData::Borrow: return *m_data.as_Borrow().inner; + case TypeData::Pointer: return *m_data.as_Pointer().inner; + case TypeData::Array: return *m_data.as_Array().inner; + default: throw ::std::runtime_error("Called inner_type on non-wrapper"); + } + } //::std::vector& sub_types() { return m_inner_types; } //const ::std::vector& sub_types() const { return m_inner_types; } -- cgit v1.2.3