diff options
author | John Hodge <tpg@mutabah.net> | 2016-03-18 13:06:35 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-03-18 13:06:35 +0800 |
commit | 629fa5d38be99066f0fdb664796e9f7c6aea49ba (patch) | |
tree | 7c27ce31fcf97811ae3fde81f19f132009d51353 | |
parent | 28e67c7ed47e9b3891f5dc72de46edf3a3b94a6e (diff) | |
download | mrust-629fa5d38be99066f0fdb664796e9f7c6aea49ba.tar.gz |
AST - Switch impl blocks to contain `Item`s (merges code)
-rw-r--r-- | src/ast/ast.cpp | 19 | ||||
-rw-r--r-- | src/ast/ast.hpp | 34 | ||||
-rw-r--r-- | src/ast/provided_module.cpp | 1 | ||||
-rw-r--r-- | src/convert/ast_iterate.cpp | 23 | ||||
-rw-r--r-- | src/convert/resolve.cpp | 17 | ||||
-rw-r--r-- | src/convert/typecheck_expr.cpp | 12 | ||||
-rw-r--r-- | src/dump_as_rust.cpp | 18 |
7 files changed, 71 insertions, 53 deletions
diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 0739fde2..4229f1e8 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -140,9 +140,22 @@ SERIALISE_TYPE(ImplDef::, "AST_ImplDef", { s.item(m_type);
})
+void Impl::add_function(bool is_public, ::std::string name, Function fcn)
+{
+ m_items.push_back( Named<Item>( mv$(name), Item::make_Function({::std::move(fcn)}), is_public ) );
+}
+void Impl::add_type(bool is_public, ::std::string name, TypeRef type)
+{
+ m_items.push_back( Named<Item>( mv$(name), Item::make_Type({TypeAlias(GenericParams(), mv$(type))}), is_public ) );
+}
+void Impl::add_static(bool is_public, ::std::string name, Static v)
+{
+ m_items.push_back( Named<Item>( mv$(name), Item::make_Static({mv$(v)}), is_public ) );
+}
+
bool Impl::has_named_item(const ::std::string& name) const
{
- for( const auto& it : this->functions() )
+ for( const auto& it : this->items() )
{
if( it.name == name ) {
return true;
@@ -210,10 +223,10 @@ Impl Impl::make_concrete(const ::std::vector<TypeRef>& types) const }
SERIALISE_TYPE(Impl::, "AST_Impl", {
s << m_def;
- s << m_functions;
+ s << m_items;
},{
s.item(m_def);
- s.item(m_functions);
+ s.item(m_items);
})
::rust::option<char> ImplRef::find_named_item(const ::std::string& name) const
diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index d79adf8d..0c653e73 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -33,6 +33,9 @@ namespace AST { class Crate;
+class Module;
+class Item;
+
using ::std::unique_ptr;
using ::std::move;
@@ -95,8 +98,6 @@ struct TupleItem: SERIALISABLE_PROTOTYPES();
};
-class Crate;
-
class TypeAlias:
public Serialisable
{
@@ -417,10 +418,10 @@ class Impl: {
ImplDef m_def;
- //NamedList<Item> m_items;
- NamedList<TypeRef> m_types;
- NamedList<Function> m_functions;
- NamedList<Static> m_statics;
+ NamedList<Item> m_items;
+ //NamedList<TypeRef> m_types;
+ //NamedList<Function> m_functions;
+ //NamedList<Static> m_statics;
::std::vector<MacroInvocation> m_macro_invocations;
::std::vector< ::std::pair< ::std::vector<TypeRef>, Impl > > m_concrete_impls;
@@ -431,26 +432,18 @@ public: m_def( move(attrs), move(params), move(trait_type), move(impl_type) )
{}
- void add_function(bool is_public, ::std::string name, Function fcn) {
- m_functions.push_back( Named<Function>( ::std::move(name), ::std::move(fcn), is_public ) );
- }
- void add_type(bool is_public, ::std::string name, TypeRef type) {
- m_types.push_back( Named<TypeRef>( ::std::move(name), ::std::move(type), is_public ) );
- }
- void add_static(bool is_public, ::std::string name, Static v) {
- m_statics.push_back( Named<Static>( mv$(name), mv$(v), is_public ) );
- }
+ void add_function(bool is_public, ::std::string name, Function fcn);
+ void add_type(bool is_public, ::std::string name, TypeRef type);
+ void add_static(bool is_public, ::std::string name, Static v);
void add_macro_invocation( MacroInvocation inv ) {
m_macro_invocations.push_back( mv$(inv) );
}
const ImplDef& def() const { return m_def; }
- const NamedList<Function>& functions() const { return m_functions; }
- const NamedList<TypeRef>& types() const { return m_types; }
+ const NamedList<Item>& items() const { return m_items; }
ImplDef& def() { return m_def; }
- NamedList<Function>& functions() { return m_functions; }
- NamedList<TypeRef>& types() { return m_types; }
+ NamedList<Item>& items() { return m_items; }
bool has_named_item(const ::std::string& name) const;
@@ -466,9 +459,6 @@ private: };
-class Module;
-class Item;
-
typedef void fcn_visitor_t(const AST::Crate& crate, const AST::Module& mod, Function& fcn);
/// Representation of a parsed (and being converted) function
diff --git a/src/ast/provided_module.cpp b/src/ast/provided_module.cpp index 99c86d3c..e36784f0 100644 --- a/src/ast/provided_module.cpp +++ b/src/ast/provided_module.cpp @@ -15,7 +15,6 @@ void AST_InitProvidedModule() fields.push_back( AST::StructItem(AST::MetaItems(), false, "", TypeRef(TypeRef::TagUnsizedArray(), Span(), TypeRef(Span(), CORETYPE_U8))) ); g_compiler_module.add_struct(true, "str", AST::Struct(AST::GenericParams(), mv$(fields)), AST::MetaItems()); - // TODO: Defer this until AFTER AST_InitProvidedModule_Impls(); } diff --git a/src/convert/ast_iterate.cpp b/src/convert/ast_iterate.cpp index 5f309361..adcc4970 100644 --- a/src/convert/ast_iterate.cpp +++ b/src/convert/ast_iterate.cpp @@ -442,17 +442,20 @@ void CASTIterator::handle_impl(AST::Path modpath, AST::Impl& impl) handle_impl_def(impl.def()); // Associated types - for( auto& at : impl.types() ) + for( auto& it : impl.items() ) { - DEBUG("- Type '" << at.name << "'"); - handle_type( at.data ); - } - - // Functions - for( auto& fcn : impl.functions() ) - { - DEBUG("- Function '" << fcn.name << "'"); - handle_function(AST::Path(AST::Path::TagRelative(), { AST::PathNode(fcn.name) }), fcn.data); + TU_MATCH_DEF(AST::Item, (it.data), (e), + ( + ), + (Type, + DEBUG("- Type '" << it.name << "'"); + handle_type( e.e.type() ); + ), + (Function, + DEBUG("- Function '" << it.name << "'"); + handle_function(AST::Path(AST::Path::TagRelative(), { AST::PathNode(it.name) }), e.e); + ) + ) } pop_self(); diff --git a/src/convert/resolve.cpp b/src/convert/resolve.cpp index b20df695..ff3aa05a 100644 --- a/src/convert/resolve.cpp +++ b/src/convert/resolve.cpp @@ -1181,12 +1181,19 @@ void CPathResolver::handle_path_ufcs(const Span& span, AST::Path& path, CASTIter DEBUG("Searching for inherent impls on " << *info.type);
bool found = m_crate.find_inherent_impls( *info.type, [&](const AST::Impl& impl, ::std::vector<TypeRef> _) -> bool {
DEBUG("Searching impl " << impl);
- for( const auto& fcn : impl.functions() )
+ for( const auto& i : impl.items() )
{
- if( fcn.name == name ) {
- path.bind_function(fcn.data, path.nodes()[0].args());
- info.trait = make_unique_ptr( TypeRef(TypeRef::TagInvalid(), span) );
- return true;
+ if( i.name == name ) {
+ TU_MATCH_DEF(AST::Item, (i.data), (e),
+ (
+ // Ignore?
+ ),
+ (Function,
+ path.bind_function(e.e, path.nodes()[0].args());
+ info.trait = make_unique_ptr( TypeRef(TypeRef::TagInvalid(), span) );
+ return true;
+ )
+ )
}
}
return false;
diff --git a/src/convert/typecheck_expr.cpp b/src/convert/typecheck_expr.cpp index a91e2207..4afb64c9 100644 --- a/src/convert/typecheck_expr.cpp +++ b/src/convert/typecheck_expr.cpp @@ -502,11 +502,11 @@ void CTC_NodeVisitor::visit(AST::ExprNode_CallMethod& node) { AST::Impl& impl = oimpl.unwrap(); // 1.1. Search impl for this method - for(auto& fcn : impl.functions()) + for(auto& i : impl.items()) { - if( fcn.name == name ) + if( i.name == name && i.data.is_Function() ) { - fcnp = &fcn.data; + fcnp = &i.data.as_Function().e; break; } } @@ -520,11 +520,11 @@ void CTC_NodeVisitor::visit(AST::ExprNode_CallMethod& node) if( oimpl.is_some() ) { AST::Impl& impl = oimpl.unwrap(); - for(auto& fcn : impl.functions()) + for(auto& i : impl.items()) { - if( fcn.name == name ) + if( i.name == name && i.data.is_Function() ) { - fcnp = &fcn.data; + fcnp = &i.data.as_Function().e; break; } } diff --git a/src/dump_as_rust.cpp b/src/dump_as_rust.cpp index be8dbb64..79ca0460 100644 --- a/src/dump_as_rust.cpp +++ b/src/dump_as_rust.cpp @@ -666,13 +666,19 @@ void RustPrinter::handle_module(const AST::Module& mod) print_bounds(i.def().params()); m_os << indent() << "{\n"; inc_indent(); - for( const auto& t : i.types() ) + for( const auto& it : i.items() ) { - m_os << indent() << "type " << t.name << " = " << t.data << ";\n"; - } - for( const auto& t : i.functions() ) - { - handle_function(t.is_pub, t.name, t.data); + TU_MATCH_DEF(AST::Item, (it.data), (e), + ( + throw ::std::runtime_error("Unexpected item type in impl block"); + ), + (Type, + m_os << indent() << "type " << it.name << " = " << e.e.type() << ";\n"; + ), + (Function, + handle_function(it.is_pub, it.name, e.e); + ) + ) } dec_indent(); m_os << indent() << "}\n"; |