From 3e20e1a674114c36b200c698dfbd2242d3859e85 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 13 Mar 2016 10:03:27 +0800 Subject: Parse+Expand - Remove a todo, fix bug in cfg handling --- src/ast/ast.cpp | 73 ++---------------------------------------------------- src/ast/ast.hpp | 2 +- src/expand/cfg.cpp | 6 ++++- src/expand/mod.cpp | 4 +-- src/main.cpp | 1 - src/parse/root.cpp | 24 +++++++++--------- 6 files changed, 22 insertions(+), 88 deletions(-) diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 2a6b0b39..15b425c8 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -259,9 +259,8 @@ void Module::add_item(bool is_pub, ::std::string name, Item it, MetaItems attrs) m_items.back().data.attrs = mv$(attrs); DEBUG("Item " << ::AST::Item::tag_to_str( m_items.back().data.tag() ) << " - attrs = " << m_items.back().data.attrs); } -void Module::add_ext_crate(::std::string ext_name, ::std::string imp_name, MetaItems attrs) { - // TODO: Extern crates can be public - this->add_item( false, imp_name, Item::make_Crate({mv$(ext_name)}), mv$(attrs) ); +void Module::add_ext_crate(bool is_public, ::std::string ext_name, ::std::string imp_name, MetaItems attrs) { + this->add_item( is_public, imp_name, Item::make_Crate({mv$(ext_name)}), mv$(attrs) ); } void Module::add_alias(bool is_public, Path path, ::std::string name, MetaItems attrs) { // TODO: Attributes on aliases / imports @@ -299,74 +298,6 @@ void Module::prescan() //{ // sm_p.first.prescan(); //} - // - ///* - //for( const auto& macro_imp : m_macro_imports ) - //{ - // resolve_macro_import( *(Crate*)0, macro_imp.first, macro_imp.second ); - //} - //*/ -} - -void Module::resolve_macro_import(const Crate& crate, const ::std::string& modname, const ::std::string& macro_name) -{ - /* - DEBUG("Import macros from " << modname << " matching '" << macro_name << "'"); - for( const auto& sm_p : m_submods ) - { - const AST::Module& sm = sm_p.first; - if( sm.name() == modname ) - { - DEBUG("Using module"); - if( macro_name == "" ) - { - for( const auto& macro_p : sm.m_macro_import_res ) - m_macro_import_res.push_back( macro_p ); - for( const auto& macro_i : sm.m_macros ) - m_macro_import_res.push_back( ItemNS( ::std::string(macro_i.name), ¯o_i.data, false ) ); - return ; - } - else - { - for( const auto& macro_p : sm.m_macro_import_res ) - { - if( macro_p.name == macro_name ) { - m_macro_import_res.push_back( macro_p ); - return ; - } - } - throw ::std::runtime_error("Macro not in module"); - } - } - } - - for( const auto& cr : m_extern_crates ) - { - if( cr.name == modname ) - { - DEBUG("Using crate import " << cr.name << " == '" << cr.data << "'"); - if( macro_name == "" ) { - for( const auto& macro_p : crate.extern_crates().at(cr.data).crate().m_exported_macros ) - m_macro_import_res.push_back( ItemNS( ::std::string(macro_p.first), &*macro_p.second, false ) ); - return ; - } - else { - for( const auto& macro_p : crate.extern_crates().at(cr.data).crate().m_exported_macros ) - { - DEBUG("Macro " << macro_p.first); - if( macro_p.first == macro_name ) { - // TODO: Handle #[macro_export] on extern crate - m_macro_import_res.push_back( ItemNS( ::std::string(macro_p.first), &*macro_p.second, false ) ); - return ; - } - } - throw ::std::runtime_error("Macro not in crate"); - } - } - } - - throw ::std::runtime_error( FMT("Could not find sub-module '" << modname << "' for macro import") ); - */ } void Module::iterate_functions(fcn_visitor_t *visitor, const Crate& crate) diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index a84097d1..ed6ad07b 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -419,7 +419,7 @@ public: ::std::unique_ptr add_anon(); void add_item(bool is_pub, ::std::string name, Item it, MetaItems attrs); - void add_ext_crate(::std::string ext_name, ::std::string imp_name, MetaItems attrs); + void add_ext_crate(bool is_public, ::std::string ext_name, ::std::string imp_name, MetaItems attrs); void add_alias(bool is_public, Path path, ::std::string name, MetaItems attrs); void add_typealias(bool is_public, ::std::string name, TypeAlias alias, MetaItems attrs); void add_static(bool is_public, ::std::string name, Static item, MetaItems attrs); diff --git a/src/expand/cfg.cpp b/src/expand/cfg.cpp index c77ada2a..431933f0 100644 --- a/src/expand/cfg.cpp +++ b/src/expand/cfg.cpp @@ -15,7 +15,7 @@ void Cfg_SetFlag(::std::string name) { g_cfg_flags.insert( mv$(name) ); } void Cfg_SetValue(::std::string name, ::std::string val) { - g_cfg_values.insert( ::std::make_pair(mv$(name), mv$(name)) ); + g_cfg_values.insert( ::std::make_pair(mv$(name), mv$(val)) ); } bool check_cfg(Span sp, const ::AST::MetaItem& mi) { @@ -51,6 +51,7 @@ bool check_cfg(Span sp, const ::AST::MetaItem& mi) { auto it = g_cfg_values.find(mi.name()); if( it != g_cfg_values.end() ) { + DEBUG(""<second<<"' == '"<second == mi.string(); } else @@ -99,6 +100,7 @@ class CCfgHandler: void handle(const AST::MetaItem& mi, ::AST::Crate& crate, AST::MacroInvocation& mac) const override { + DEBUG("#[cfg] mac! - " << mi); if( check_cfg(mac.span(), mi) ) { // Leave as is } @@ -107,6 +109,7 @@ class CCfgHandler: } } void handle(const AST::MetaItem& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const override { + DEBUG("#[cfg] item - " << mi); if( check_cfg(Span(), mi) ) { // Leave } @@ -115,6 +118,7 @@ class CCfgHandler: } } void handle(const AST::MetaItem& mi, ::AST::Crate& crate, ::std::unique_ptr& expr) const override { + DEBUG("#[cfg] expr - " << mi); if( check_cfg(Span(expr->get_pos()), mi) ) { // Leave } diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp index ab69ce1c..27de20ed 100644 --- a/src/expand/mod.cpp +++ b/src/expand/mod.cpp @@ -417,8 +417,8 @@ void Expand(::AST::Crate& crate) } // 3. Module tree - Expand_Mod(true , crate, modstack, ::AST::Path(), crate.m_root_module); - Expand_Mod(false, crate, modstack, ::AST::Path(), crate.m_root_module); + Expand_Mod(true , crate, modstack, ::AST::Path("",{}), crate.m_root_module); + Expand_Mod(false, crate, modstack, ::AST::Path("",{}), crate.m_root_module); // Post-process #if 0 diff --git a/src/main.cpp b/src/main.cpp index ddc233fb..28477657 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -76,7 +76,6 @@ int main(int argc, char *argv[]) Cfg_SetValue("target_pointer_width", "64"); - try { // Parse the crate into AST diff --git a/src/parse/root.cpp b/src/parse/root.cpp index 5cfef49e..83afc191 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -1021,12 +1021,11 @@ void Parse_Impl_Item(TokenStream& lex, AST::Impl& impl) } } -void Parse_ExternBlock(TokenStream& lex, AST::Module& mod, ::std::string abi) +void Parse_ExternBlock(TokenStream& lex, AST::Module& mod, ::std::string abi, ::AST::MetaItems block_attrs) { TRACE_FUNCTION; Token tok; - AST::MetaItems block_attrs; while( GET_TOK(tok, lex) == TOK_CATTR_OPEN ) { block_attrs.push_back( Parse_MetaItem(lex) ); @@ -1226,24 +1225,26 @@ void Parse_Use(TokenStream& lex, ::std::function return ::AST::MacroInvocation( lex.end_span(span_start), mv$(meta_items), mv$(name), mv$(ident), mv$(tt)); } -void Parse_ExternCrate(TokenStream& lex, AST::Module& mod, AST::MetaItems meta_items) +void Parse_ExternCrate(TokenStream& lex, AST::Module& mod, bool is_public, AST::MetaItems meta_items) { Token tok; ::std::string path, name; switch( GET_TOK(tok, lex) ) { // `extern crate "crate-name" as crate_name;` - // TODO: rustc no longer supports this feature + // NOTE: rustc doesn't allow this, keep in mrustc for for reparse support case TOK_STRING: - path = tok.str(); + path = mv$(tok.str()); GET_CHECK_TOK(tok, lex, TOK_RWORD_AS); GET_CHECK_TOK(tok, lex, TOK_IDENT); - name = tok.str(); + name = mv$(tok.str()); break; // `extern crate crate_name;` + // `extern crate crate_name as other_name;` case TOK_IDENT: - name = tok.str(); + name = mv$(tok.str()); if(GET_TOK(tok, lex) == TOK_RWORD_AS) { + path = mv$(name); GET_CHECK_TOK(tok, lex, TOK_IDENT); name = mv$(tok.str()); } @@ -1257,7 +1258,7 @@ void Parse_ExternCrate(TokenStream& lex, AST::Module& mod, AST::MetaItems meta_i } GET_CHECK_TOK(tok, lex, TOK_SEMICOLON); - mod.add_ext_crate(path, name, mv$(meta_items)); + mod.add_ext_crate(is_public, mv$(path), mv$(name), mv$(meta_items)); } void Parse_Mod_Item(TokenStream& lex, bool file_controls_dir, const ::std::string& file_path, AST::Module& mod, bool is_public, AST::MetaItems meta_items) @@ -1297,8 +1298,7 @@ void Parse_Mod_Item(TokenStream& lex, bool file_controls_dir, const ::std::strin break; } // `extern "" { ...` case TOK_BRACE_OPEN: - // TODO: Use meta items on extern block - Parse_ExternBlock(lex, mod, mv$(abi)); + Parse_ExternBlock(lex, mod, mv$(abi), mv$(meta_items)); break; default: throw ParseError::Unexpected(lex, tok, {TOK_RWORD_FN, TOK_BRACE_OPEN}); @@ -1312,12 +1312,12 @@ void Parse_Mod_Item(TokenStream& lex, bool file_controls_dir, const ::std::strin break; } // `extern { ...` case TOK_BRACE_OPEN: - Parse_ExternBlock(lex, mod, "C"); + Parse_ExternBlock(lex, mod, "C", mv$(meta_items)); break; // `extern crate "crate-name" as crate_name;` // `extern crate crate_name;` case TOK_RWORD_CRATE: - Parse_ExternCrate(lex, mod, mv$(meta_items)); + Parse_ExternCrate(lex, mod, is_public, mv$(meta_items)); break; default: throw ParseError::Unexpected(lex, tok, {TOK_STRING, TOK_RWORD_FN, TOK_BRACE_OPEN, TOK_RWORD_CRATE}); -- cgit v1.2.3