From 485dfcb3fe4ddfeb7c0342ec288807fde3ef9504 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 18 Mar 2016 13:58:11 +0800 Subject: AST - Switch traits to contain items --- src/ast/ast.cpp | 26 +++++++++++++++++++++---- src/ast/ast.hpp | 46 +++++++++++--------------------------------- src/convert/ast_iterate.cpp | 18 +++++++++++++++-- src/convert/resolve.cpp | 47 +++++++++++++++++++++++++-------------------- src/dump_as_rust.cpp | 17 ++++++++++------ 5 files changed, 86 insertions(+), 68 deletions(-) diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 4229f1e8..16fad76e 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -501,13 +501,31 @@ SERIALISE_TYPE(Function::, "AST_Function", { SERIALISE_TYPE(Trait::, "AST_Trait", { s << m_params; - s << m_types; - s << m_functions; + s << m_items; },{ s.item(m_params); - s.item(m_types); - s.item(m_functions); + s.item(m_items); }) +void Trait::add_type(::std::string name, TypeRef type) { + m_items.push_back( Named(mv$(name), Item::make_Type({TypeAlias(GenericParams(), mv$(type))}), true) ); +} +void Trait::add_function(::std::string name, Function fcn) { + m_items.push_back( Named(mv$(name), Item::make_Function({mv$(fcn)}), true) ); +} +void Trait::add_static(::std::string name, Static v) { + m_items.push_back( Named(mv$(name), Item::make_Static({mv$(v)}), true) ); +} +bool Trait::has_named_item(const ::std::string& name, bool& out_is_fcn) const +{ + for( const auto& i : m_items ) + { + if( i.name == name ) { + out_is_fcn = i.data.is_Function(); + return true; + } + } + return false; +} SERIALISE_TYPE_A(EnumVariant::, "AST_EnumVariant", { s.item(m_attrs); diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index 0c653e73..3dc45305 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -198,9 +198,8 @@ class Trait: { GenericParams m_params; ::std::vector m_supertraits; - NamedList m_types; - NamedList m_functions; - NamedList m_statics; + + NamedList m_items; public: Trait() {} Trait(GenericParams params, ::std::vector supertraits): @@ -210,41 +209,18 @@ public: } const GenericParams& params() const { return m_params; } + GenericParams& params() { return m_params; } const ::std::vector& supertraits() const { return m_supertraits; } - const NamedList& functions() const { return m_functions; } - const NamedList& types() const { return m_types; } - const NamedList& statics() const { return m_statics; } - - GenericParams& params() { return m_params; } - ::std::vector& supertraits() { return m_supertraits; } - NamedList& functions() { return m_functions; } - NamedList& types() { return m_types; } + ::std::vector& supertraits() { return m_supertraits; } - void add_type(::std::string name, TypeRef type) { - m_types.push_back( Named(move(name), TypeAlias(GenericParams(), move(type)), true) ); - } - void add_function(::std::string name, Function fcn) { - m_functions.push_back( Named(::std::move(name), ::std::move(fcn), true) ); - } - void add_static(::std::string name, Static v) { - m_statics.push_back( Named(mv$(name), mv$(v), true) ); - } + const NamedList& items() const { return m_items; } + NamedList& items() { return m_items; } - bool has_named_item(const ::std::string& name, bool& out_is_fcn) const { - for( const auto& f : m_functions ) - if( f.name == name ) { - out_is_fcn = true; - return true; - } - for( const auto& f : m_types ) - if( f.name == name ) { - out_is_fcn = false; - return true; - } - - //for( const auto& st : - return false; - } + void add_type(::std::string name, TypeRef type); + void add_function(::std::string name, Function fcn); + void add_static(::std::string name, Static v); + + bool has_named_item(const ::std::string& name, bool& out_is_fcn) const; SERIALISABLE_PROTOTYPES(); }; diff --git a/src/convert/ast_iterate.cpp b/src/convert/ast_iterate.cpp index adcc4970..66ff0516 100644 --- a/src/convert/ast_iterate.cpp +++ b/src/convert/ast_iterate.cpp @@ -514,8 +514,22 @@ void CASTIterator::handle_trait(AST::Path path, AST::Trait& trait) } } - for( auto& fcn : trait.functions() ) - handle_function( path + fcn.name, fcn.data ); + for( auto& i : trait.items() ) + { + TU_MATCH_DEF(AST::Item, (i.data), (e), + ( + ), + (Type, + // TODO: Can trait associated types have default types? + ), + (Static, + handle_type(e.e.type()); + ), + (Function, + handle_function( path + i.name, e.e ); + ) + ) + } pop_self(); end_scope(); } diff --git a/src/convert/resolve.cpp b/src/convert/resolve.cpp index ff3aa05a..f9115e0b 100644 --- a/src/convert/resolve.cpp +++ b/src/convert/resolve.cpp @@ -1377,28 +1377,33 @@ bool CPathResolver::find_trait_item(const Span& span, const AST::Path& path, AST { TRACE_FUNCTION_F("path=" << path << ", trait=..., item_name=" << item_name); { - const auto& fcns = trait.functions(); - //DEBUG("fcns = " << fcns); - auto it = ::std::find_if( fcns.begin(), fcns.end(), [&](const AST::Named& a) { DEBUG("fcn " << a.name); return a.name == item_name; } ); - if( it != fcns.end() ) { + const auto& items = trait.items(); + auto it = ::std::find_if( items.begin(), items.end(), [&](const auto& a) { DEBUG("fcn " << a.name); return a.name == item_name; } ); + if( it != items.end() ) { // Found it. - out_is_method = true; - out_ptr = &it->data; - out_trait_path = AST::Path(path); - DEBUG("Fcn, out_trait_path = " << out_trait_path); - return true; - } - } - { - const auto& types = trait.types(); - auto it = ::std::find_if( types.begin(), types.end(), [&](const AST::Named& a) { DEBUG("type " << a.name); return a.name == item_name; } ); - if( it != types.end() ) { - // Found it. - out_is_method = false; - out_ptr = &it->data; - out_trait_path = AST::Path(path); - DEBUG("Ty, out_trait_path = " << out_trait_path << " path=" << path); - return true; + const auto& i = *it; + TU_MATCH_DEF(AST::Item, (i.data), (e), + ( + BUG(Span(), "Unknown item type in trait"); + ), + (Function, + out_is_method = true; + out_ptr = &e.e; + out_trait_path = AST::Path(path); + DEBUG("Fcn, out_trait_path = " << out_trait_path); + return true; + ), + (Type, + out_is_method = false; + out_ptr = &it->data; + out_trait_path = AST::Path(path); + DEBUG("Ty, out_trait_path = " << out_trait_path << " path=" << path); + return true; + ), + (Static, + TODO(Span(), "Handle resolving associated statics/consts in traits (CPathResolver::find_trait_item)"); + ) + ) } } diff --git a/src/dump_as_rust.cpp b/src/dump_as_rust.cpp index 79ca0460..42cbf48e 100644 --- a/src/dump_as_rust.cpp +++ b/src/dump_as_rust.cpp @@ -949,13 +949,18 @@ void RustPrinter::handle_trait(const AST::Trait& s) m_os << indent() << "{\n"; inc_indent(); - for( const auto& i : s.types() ) + for( const auto& i : s.items() ) { - m_os << indent() << "type " << i.name << ";\n"; - } - for( const auto& i : s.functions() ) - { - handle_function(false, i.name, i.data); + TU_MATCH_DEF(AST::Item, (i.data), (e), + ( + ), + (Type, + m_os << indent() << "type " << i.name << ";\n"; + ), + (Function, + handle_function(false, i.name, e.e); + ) + ) } dec_indent(); -- cgit v1.2.3