summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2016-11-07 09:04:07 +0800
committerJohn Hodge <tpg@mutabah.net>2016-11-07 09:04:07 +0800
commit4f29602290f5faa8ca6b0ee224d621ca86b85c39 (patch)
treec270a4fa6057af06c89c125ab4b54c2eb7d45994
parent815ae041cff5bf744e41cf7d70c81b156609831c (diff)
downloadmrust-4f29602290f5faa8ca6b0ee224d621ca86b85c39.tar.gz
HIR Gen - Extract #[macro_export] macros from deep within the tree
-rw-r--r--src/expand/macro_rules.cpp1
-rw-r--r--src/expand/mod.cpp2
-rw-r--r--src/hir/from_ast.cpp60
3 files changed, 41 insertions, 22 deletions
diff --git a/src/expand/macro_rules.cpp b/src/expand/macro_rules.cpp
index 163504db..05bf291c 100644
--- a/src/expand/macro_rules.cpp
+++ b/src/expand/macro_rules.cpp
@@ -57,6 +57,7 @@ class CMacroUseHandler:
else
{
ec.with_all_macros([&](const auto& name, const auto& mac) {
+ DEBUG("Imported " << name << "!");
mod.add_macro_import( name, mac );
});
}
diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp
index d0e9718d..8cc4ea2e 100644
--- a/src/expand/mod.cpp
+++ b/src/expand/mod.cpp
@@ -952,6 +952,8 @@ void Expand_Mod_IndexAnon(::AST::Crate& crate, ::AST::Module& mod)
DEBUG("- " << i.data.tag_str() << " '" << i.name << "'");
TU_IFLET(::AST::Item, (i.data), Module, e,
Expand_Mod_IndexAnon(crate, e);
+
+ // TODO: Also ensure that all #[macro_export] macros end up in parent
)
}
diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp
index b1d8bd38..78f6b683 100644
--- a/src/hir/from_ast.cpp
+++ b/src/hir/from_ast.cpp
@@ -1419,29 +1419,45 @@ public:
g_core_crate = (crate.m_load_std == ::AST::Crate::LOAD_NONE ? "" : "core");
auto& macros = rv.m_exported_macros;
- // - Extract macros from root module
- for( /*const*/ auto& mac : crate.m_root_module.macros() ) {
- if( mac.data->m_exported ) {
- auto res = macros.insert( ::std::make_pair( mac.name, mv$(mac.data) ) );
- if( res.second )
- DEBUG("- Define " << mac.name << "!");
- }
- else {
- DEBUG("- Non-exported " << mac.name << "!");
- }
- }
- for( auto& mac : crate.m_root_module.macro_imports_res() ) {
- if( mac.data->m_exported && mac.name != "" ) {
- auto v = ::std::make_pair( mac.name, MacroRulesPtr(new MacroRules( mv$(*const_cast<MacroRules*>(mac.data)) )) );
- auto it = macros.find(mac.name);
- if( it == macros.end() )
- {
- auto res = macros.insert( mv$(v) );
- DEBUG("- Import " << mac.name << "! (from \"" << res.first->second->m_source_crate << "\")");
+ // - Extract exported macros
+ {
+ ::std::vector< ::AST::Module*> mods;
+ mods.push_back( &crate.m_root_module );
+ do
+ {
+ auto& mod = *mods.back();
+ mods.pop_back();
+
+ for( /*const*/ auto& mac : mod.macros() ) {
+ if( mac.data->m_exported ) {
+ auto res = macros.insert( ::std::make_pair( mac.name, mv$(mac.data) ) );
+ if( res.second )
+ DEBUG("- Define " << mac.name << "!");
+ }
+ else {
+ DEBUG("- Non-exported " << mac.name << "!");
+ }
}
- else {
- DEBUG("- Replace " << mac.name << "! (from \"" << it->second->m_source_crate << "\") with one from \"" << v.second->m_source_crate << "\"");
- it->second = mv$( v.second );
+
+ for(auto& i : mod.items()) {
+ if( i.data.is_Module() )
+ mods.push_back( &i.data.as_Module() );
+ }
+ } while( mods.size() > 0 );
+
+ for( auto& mac : crate.m_root_module.macro_imports_res() ) {
+ if( mac.data->m_exported && mac.name != "" ) {
+ auto v = ::std::make_pair( mac.name, MacroRulesPtr(new MacroRules( mv$(*const_cast<MacroRules*>(mac.data)) )) );
+ auto it = macros.find(mac.name);
+ if( it == macros.end() )
+ {
+ auto res = macros.insert( mv$(v) );
+ DEBUG("- Import " << mac.name << "! (from \"" << res.first->second->m_source_crate << "\")");
+ }
+ else {
+ DEBUG("- Replace " << mac.name << "! (from \"" << it->second->m_source_crate << "\") with one from \"" << v.second->m_source_crate << "\"");
+ it->second = mv$( v.second );
+ }
}
}
}