From 4d6039f0755b88c135492d80e18ab640d402de88 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Mon, 24 Oct 2016 08:39:49 +0800 Subject: AST - Remove separate impls list (now in item list) --- src/ast/ast.hpp | 18 -------- src/ast/dump.cpp | 5 ++- src/expand/derive.cpp | 2 +- src/expand/mod.cpp | 2 + src/hir/from_ast.cpp | 13 ++++-- src/parse/root.cpp | 32 +++------------ src/resolve/absolute.cpp | 105 ++++++++++++++++++++++------------------------- src/resolve/use.cpp | 38 ++++++++--------- 8 files changed, 87 insertions(+), 128 deletions(-) (limited to 'src') diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index e6b1ea1c..7f34e30b 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -482,12 +482,6 @@ class Module // Module-level items /// General items ::std::vector> m_items; - - /// Impl blocks - ::std::vector m_impls; - /// Negative impl blocks - ::std::vector m_neg_impls; - // --- Runtime caches and state --- ::std::vector< ::std::shared_ptr > m_anon_modules; @@ -537,12 +531,6 @@ public: void add_alias(bool is_public, UseStmt path, ::std::string name, MetaItems attrs); void add_macro_invocation(MacroInvocation item); - void add_impl(Impl impl) { - m_impls.emplace_back( mv$(impl) ); - } - void add_neg_impl(ImplDef impl) { - m_neg_impls.emplace_back( mv$(impl) ); - } void add_macro(bool is_exported, ::std::string name, MacroRulesPtr macro); void add_macro_import(::std::string name, const MacroRules& mr); @@ -553,12 +541,6 @@ public: ::std::vector>& items() { return m_items; } const ::std::vector>& items() const { return m_items; } - ::std::vector& impls() { return m_impls; } - const ::std::vector& impls() const { return m_impls; } - - ::std::vector& neg_impls() { return m_neg_impls; } - const ::std::vector& neg_impls() const { return m_neg_impls; } - ::std::vector< ::std::shared_ptr >& anon_mods() { return m_anon_modules; } const ::std::vector< ::std::shared_ptr >& anon_mods() const { return m_anon_modules; } diff --git a/src/ast/dump.cpp b/src/ast/dump.cpp index f1b0a619..89b21a97 100644 --- a/src/ast/dump.cpp +++ b/src/ast/dump.cpp @@ -696,8 +696,11 @@ void RustPrinter::handle_module(const AST::Module& mod) handle_function(item.is_pub, item.name, e); } - for( const auto& i : mod.impls() ) + for( const auto& item : mod.items() ) { + if( !item.data.is_Impl() ) continue ; + const auto& i = item.data.as_Impl(); + m_os << "\n"; m_os << indent() << "impl"; print_params(i.def().params()); diff --git a/src/expand/derive.cpp b/src/expand/derive.cpp index 668ecc9b..d8114e35 100644 --- a/src/expand/derive.cpp +++ b/src/expand/derive.cpp @@ -1704,7 +1704,7 @@ static void derive_item(const Span& sp, const AST::Crate& crate, AST::Module& mo continue ; } - mod.add_impl( dp->handle_item(sp, (crate.m_load_std == ::AST::Crate::LOAD_NONE ? "" : "core"), params, type, item) ); + mod.add_item(false, "", dp->handle_item(sp, (crate.m_load_std == ::AST::Crate::LOAD_NONE ? "" : "core"), params, type, item), {} ); } if( fail ) { diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp index 607b8f3c..2f2f377a 100644 --- a/src/expand/mod.cpp +++ b/src/expand/mod.cpp @@ -873,6 +873,7 @@ void Expand_Mod(::AST::Crate& crate, LList modstack, ::AST:: // IGNORE m_anon_modules, handled as part of expressions + /* DEBUG("Impls"); for( auto it = mod.impls().begin(); it != mod.impls().end(); ) { @@ -901,6 +902,7 @@ void Expand_Mod(::AST::Crate& crate, LList modstack, ::AST:: else ++ it; } + */ for( const auto& mi: mod.macro_imports_res() ) DEBUG("- Imports '" << mi.name << "'"); diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp index 23cca0e2..1abd7771 100644 --- a/src/hir/from_ast.cpp +++ b/src/hir/from_ast.cpp @@ -1072,10 +1072,10 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H // TODO: Insert a record of the `link` attribute ), (Impl, - TODO(sp, "Expand Item::Impl"); + //TODO(sp, "Expand Item::Impl"); ), (NegImpl, - TODO(sp, "Expand Item::NegImpl"); + //TODO(sp, "Expand Item::NegImpl"); ), (Use, // Ignore - The index is used to add `Import`s @@ -1165,8 +1165,10 @@ void LowerHIR_Module_Impls(const ::AST::Module& ast_mod, ::HIR::Crate& hir_crat } // - for( const auto& impl : ast_mod.impls() ) + for( const auto& i : ast_mod.items() ) { + if( !i.data.is_Impl() ) continue; + const auto& impl = i.data.as_Impl(); auto params = LowerHIR_GenericParams(impl.def().params(), nullptr); if( impl.def().trait().ent.is_valid() ) @@ -1301,8 +1303,11 @@ void LowerHIR_Module_Impls(const ::AST::Module& ast_mod, ::HIR::Crate& hir_crat } ); } } - for( const auto& impl : ast_mod.neg_impls() ) + for( const auto& i : ast_mod.items() ) { + if( !i.data.is_NegImpl() ) continue; + const auto& impl = i.data.as_NegImpl(); + auto params = LowerHIR_GenericParams(impl.params(), nullptr); auto type = LowerHIR_Type(impl.type()); auto trait = LowerHIR_GenericPath(impl.trait().sp, impl.trait().ent); diff --git a/src/parse/root.cpp b/src/parse/root.cpp index 488ef9d3..eca14572 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -871,7 +871,7 @@ AST::MetaItem Parse_MetaItem(TokenStream& lex) } } -void Parse_Impl(TokenStream& lex, AST::Module& mod, AST::MetaItems attrs, bool is_unsafe=false) +::AST::Item Parse_Impl(TokenStream& lex, AST::MetaItems attrs, bool is_unsafe=false) { TRACE_FUNCTION; Token tok; @@ -908,8 +908,7 @@ void Parse_Impl(TokenStream& lex, AST::Module& mod, AST::MetaItems attrs, bool i // negative impls can't have any content GET_CHECK_TOK(tok, lex, TOK_BRACE_CLOSE); - mod.add_neg_impl( AST::ImplDef(lex.end_span(ps), AST::MetaItems(), mv$(params), mv$(trait_path), mv$(impl_type) ) ); - return ; + return ::AST::Item::make_NegImpl( AST::ImplDef(lex.end_span(ps), mv$(attrs), mv$(params), mv$(trait_path), mv$(impl_type) ) ); } // - Don't care which at this stage @@ -979,7 +978,7 @@ void Parse_Impl(TokenStream& lex, AST::Module& mod, AST::MetaItems attrs, bool i } } - mod.add_impl( ::std::move(impl) ); + return ::AST::Item::make_Impl( mv$(impl) ); } void Parse_Impl_Item(TokenStream& lex, AST::Impl& impl) @@ -1300,7 +1299,6 @@ void Parse_Use(TokenStream& lex, ::std::function Parse_Mod_Item_S(TokenStream& lex, const AST::Module::FileInfo& mod_fileinfo, const ::AST::Path& mod_path, AST::MetaItems meta_items) { TRACE_FUNCTION_F("mod_path="< { "", Parse_Impl(lex, mv$(meta_items), true), false }; default: throw ParseError::Unexpected(lex, tok, {TOK_RWORD_FN, TOK_RWORD_TRAIT, TOK_RWORD_IMPL}); } @@ -1573,8 +1567,7 @@ void Parse_Use(TokenStream& lex, ::std::function { "", Parse_Impl(lex, mv$(meta_items)), false }; // `trait` case TOK_RWORD_TRAIT: GET_CHECK_TOK(tok, lex, TOK_IDENT); @@ -1710,19 +1703,6 @@ void Parse_Mod_Item(TokenStream& lex, AST::Module& mod, AST::MetaItems meta_item }); GET_CHECK_TOK(tok, lex, TOK_SEMICOLON); } - // `unsafe impl` - // TODO: Move these two into Parse_Mod_Item_S - //else if( LOOKAHEAD1(lex, TOK_RWORD_IMPL) || LOOKAHEAD2(lex, TOK_RWORD_UNSAFE, TOK_RWORD_IMPL) ) - else if( lex.lookahead(0) == TOK_RWORD_IMPL || (lex.lookahead(0) == TOK_RWORD_UNSAFE && lex.lookahead(1) == TOK_RWORD_IMPL) ) - { - bool is_unsafe = false; - if( lex.lookahead(0) == TOK_RWORD_UNSAFE ) { - GET_CHECK_TOK(tok, lex, TOK_RWORD_UNSAFE); - is_unsafe = true; - } - GET_CHECK_TOK(tok, lex, TOK_RWORD_IMPL); - Parse_Impl(lex, mod, mv$(meta_items), is_unsafe); - } else { mod.add_item( Parse_Mod_Item_S(lex, mod.m_file_info, mod.path(), mv$(meta_items)) ); diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp index ca79094e..646e4aaf 100644 --- a/src/resolve/absolute.cpp +++ b/src/resolve/absolute.cpp @@ -2023,10 +2023,56 @@ void Resolve_Absolute_Mod( Context item_context, ::AST::Module& mod ) } ), (Impl, - TODO(i.data.span, "Impl"); + auto& def = e.def(); + DEBUG("impl " << def.trait().ent << " for " << def.type()); + if( !def.type().is_valid() ) + { + DEBUG("---- MARKER IMPL for " << def.trait().ent); + item_context.push(def.params(), GenericSlot::Level::Top); + Resolve_Absolute_Generic(item_context, def.params()); + assert( def.trait().ent.is_valid() ); + Resolve_Absolute_Path(item_context, def.trait().sp, Context::LookupMode::Type, def.trait().ent); + + if( e.items().size() != 0 ) { + ERROR(def.span(), E0000, "impl Trait for .. with methods"); + } + + item_context.pop(def.params()); + + const_cast< ::AST::Trait*>(def.trait().ent.binding().as_Trait().trait_)->set_is_marker(); + } + else + { + item_context.push_self( def.type() ); + item_context.push(def.params(), GenericSlot::Level::Top); + Resolve_Absolute_Generic(item_context, def.params()); + + Resolve_Absolute_Type(item_context, def.type()); + if( def.trait().ent.is_valid() ) { + Resolve_Absolute_Path(item_context, def.trait().sp, Context::LookupMode::Type, def.trait().ent); + } + + Resolve_Absolute_ImplItems(item_context, e.items()); + + item_context.pop(def.params()); + item_context.pop_self( def.type() ); + } ), (NegImpl, - TODO(i.data.span, "NegImpl"); + auto& impl_def = e; + item_context.push_self( impl_def.type() ); + item_context.push(impl_def.params(), GenericSlot::Level::Top); + Resolve_Absolute_Generic(item_context, impl_def.params()); + + Resolve_Absolute_Type(item_context, impl_def.type()); + if( !impl_def.trait().ent.is_valid() ) + BUG(impl_def.span(), "Encountered negative impl with no trait"); + Resolve_Absolute_Path(item_context, impl_def.trait().sp, Context::LookupMode::Type, impl_def.trait().ent); + + // No items + + item_context.pop(impl_def.params()); + item_context.pop_self( impl_def.type() ); ), (Module, DEBUG("Module - " << i.name); @@ -2067,61 +2113,6 @@ void Resolve_Absolute_Mod( Context item_context, ::AST::Module& mod ) ) } - for(auto& impl : mod.impls()) - { - auto& def = impl.def(); - DEBUG("impl " << def.trait().ent << " for " << def.type()); - if( !def.type().is_valid() ) - { - DEBUG("---- MARKER IMPL for " << def.trait().ent); - item_context.push(def.params(), GenericSlot::Level::Top); - Resolve_Absolute_Generic(item_context, def.params()); - assert( def.trait().ent.is_valid() ); - Resolve_Absolute_Path(item_context, def.trait().sp, Context::LookupMode::Type, def.trait().ent); - - if( impl.items().size() != 0 ) { - ERROR(def.span(), E0000, "impl Trait for .. with methods"); - } - - item_context.pop(def.params()); - - const_cast< ::AST::Trait*>(def.trait().ent.binding().as_Trait().trait_)->set_is_marker(); - } - else - { - item_context.push_self( impl.def().type() ); - item_context.push(impl.def().params(), GenericSlot::Level::Top); - Resolve_Absolute_Generic(item_context, impl.def().params()); - - Resolve_Absolute_Type(item_context, impl.def().type()); - if( def.trait().ent.is_valid() ) { - Resolve_Absolute_Path(item_context, def.trait().sp, Context::LookupMode::Type, def.trait().ent); - } - - Resolve_Absolute_ImplItems(item_context, impl.items()); - - item_context.pop(def.params()); - item_context.pop_self( def.type() ); - } - } - - for(auto& impl_def : mod.neg_impls()) - { - item_context.push_self( impl_def.type() ); - item_context.push(impl_def.params(), GenericSlot::Level::Top); - Resolve_Absolute_Generic(item_context, impl_def.params()); - - Resolve_Absolute_Type(item_context, impl_def.type()); - if( !impl_def.trait().ent.is_valid() ) - BUG(impl_def.span(), "Encountered negative impl with no trait"); - Resolve_Absolute_Path(item_context, impl_def.trait().sp, Context::LookupMode::Type, impl_def.trait().ent); - - // No items - - item_context.pop(impl_def.params()); - item_context.pop_self( impl_def.type() ); - } - // - Run through the indexed items and fix up those paths static Span sp; DEBUG("mod = " << mod.path()); diff --git a/src/resolve/use.cpp b/src/resolve/use.cpp index 03cff143..0946f8b0 100644 --- a/src/resolve/use.cpp +++ b/src/resolve/use.cpp @@ -183,7 +183,23 @@ void Resolve_Use_Mod(const ::AST::Crate& crate, ::AST::Module& mod, ::AST::Path Resolve_Use_Mod(crate, i.data.as_Module(), path + i.name); ), (Impl, - TODO(Span(), "Recurse into Impl"); + for(auto& i : e.items()) + { + TU_MATCH_DEF( AST::Item, (*i.data), (e), + ( + ), + (Function, + if( e.code().is_valid() ) { + e.code().node().visit( expr_iter ); + } + ), + (Static, + if( e.value().is_valid() ) { + e.value().node().visit( expr_iter ); + } + ) + ) + } ), (Trait, for(auto& ti : e.items()) @@ -219,26 +235,6 @@ void Resolve_Use_Mod(const ::AST::Crate& crate, ::AST::Module& mod, ::AST::Path ) ) } - for(auto& im : mod.impls()) - { - for(auto& i : im.items()) - { - TU_MATCH_DEF( AST::Item, (*i.data), (e), - ( - ), - (Function, - if( e.code().is_valid() ) { - e.code().node().visit( expr_iter ); - } - ), - (Static, - if( e.value().is_valid() ) { - e.value().node().visit( expr_iter ); - } - ) - ) - } - } } ::AST::PathBinding Resolve_Use_GetBinding_Mod( -- cgit v1.2.3