From 76f199888f65fd21ffdae7c3518748098fdc0726 Mon Sep 17 00:00:00 2001 From: "John Hodge (sonata)" Date: Sun, 25 Jan 2015 17:59:48 +0800 Subject: Hacked in dumping as rust --- src/types.cpp | 116 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 97 insertions(+), 19 deletions(-) (limited to 'src/types.cpp') diff --git a/src/types.cpp b/src/types.cpp index 33b564dd..445f394c 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -64,6 +64,7 @@ bool TypeRef::deref(bool is_implicit) case TypeRef::ASSOCIATED: throw ::std::runtime_error("TODO: TypeRef::deref on ASSOCIATED"); } + throw ::std::runtime_error("BUGCHECK: Fell off end of TypeRef::deref"); } /// Merge the contents of the passed type with this type @@ -72,14 +73,15 @@ bool TypeRef::deref(bool is_implicit) void TypeRef::merge_with(const TypeRef& other) { // Ignore if other is wildcard - if( other.m_class == TypeRef::ANY ) { - assert(other.m_inner_types.size() == 0 && "TODO: merge_with on bounded _"); - return; - } + //if( other.m_class == TypeRef::ANY ) { + // assert(other.m_inner_types.size() == 0 && "TODO: merge_with on bounded _"); + // return; + //} - // If this is a wildcard, then replace with the othet type + // If this is a wildcard, then replace with the other type if( m_class == TypeRef::ANY ) { - assert(m_inner_types.size() == 0 && "TODO: merge_with on bounded _"); + if( m_inner_types.size() && m_inner_types.size() != other.m_inner_types.size() ) + throw ::std::runtime_error("TypeRef::merge_with - Handle bounded wildcards"); *this = other; return; } @@ -177,16 +179,19 @@ void TypeRef::resolve_args(::std::function fcn) /// This is used to handle extracting types passsed to methods/enum variants void TypeRef::match_args(const TypeRef& other, ::std::function fcn) const { - // If the other type is a wildcard, early return - // - TODO - Might want to restrict the other type to be of the same form as this type - if( other.m_class == TypeRef::ANY ) - return; // If this type is a generic, then call the closure with the other type if( m_class == TypeRef::GENERIC ) { fcn( m_path[0].name().c_str(), other ); return ; } + // If the other type is a wildcard, early return + // - TODO - Might want to restrict the other type to be of the same form as this type + if( other.m_class == TypeRef::ANY ) + return; + + DEBUG("this = " << *this << ", other = " << other); + // Any other case, it's a "pattern" match if( m_class != other.m_class ) throw ::std::runtime_error("Type mismatch (class)"); @@ -215,11 +220,35 @@ void TypeRef::match_args(const TypeRef& other, ::std::function::" << tr.m_path[0].name(); os << "<" << tr.m_inner_types[0] << " as " << tr.m_inner_types[1] << ">::" << tr.m_path[0].name(); break; } - os << ")"; + //os << ")"; return os; } @@ -442,3 +469,54 @@ SERIALISE_TYPE(TypeRef::, "TypeRef", { m_size_expr.reset(); s.item( m_path ); }) + + +void PrettyPrintType::print(::std::ostream& os) const +{ + switch(m_type.m_class) + { + case TypeRef::ANY: + os << "_"; + if( m_type.m_inner_types.size() ) { + os << "/* : " << m_type.m_inner_types << "*/"; + } + break; + case TypeRef::UNIT: + os << "()"; + break; + case TypeRef::PRIMITIVE: + os << m_type.m_core_type; + break; + case TypeRef::TUPLE: + os << "("; + for(const auto& t : m_type.m_inner_types) + os << t.print_pretty() << ","; + os << ")"; + break; + case TypeRef::REFERENCE: + os << "&" << (m_type.m_is_inner_mutable ? "mut " : "") << m_type.m_inner_types[0].print_pretty(); + break; + case TypeRef::POINTER: + os << "*" << (m_type.m_is_inner_mutable ? "mut" : "const") << " " << m_type.m_inner_types[0].print_pretty(); + break; + case TypeRef::ARRAY: + os << "[" << m_type.m_inner_types[0].print_pretty() << ", " << m_type.m_size_expr << "]"; + break; + case TypeRef::GENERIC: + os << m_type.m_path[0].name(); + break; + case TypeRef::PATH: + os << m_type.m_path; + break; + case TypeRef::ASSOCIATED: + os << "<" << m_type.m_inner_types[0].print_pretty() << " as " << m_type.m_inner_types[1].print_pretty() << ">::" << m_type.m_path[0].name(); + break; + } + +} + +::std::ostream& operator<<(::std::ostream& os, const PrettyPrintType& v) +{ + v.print(os); + return os; +} -- cgit v1.2.3