From ce771e31b4cd157a87e7ec6531d625a895228ebf Mon Sep 17 00:00:00 2001 From: John Hodge Date: Thu, 10 Mar 2016 10:18:59 +0800 Subject: Expand - cfg!/#[cfg]/#[cfg_attr] hacked up --- src/expand/mod.cpp | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'src/expand/mod.cpp') diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp index 33f982da..8eb6dd87 100644 --- a/src/expand/mod.cpp +++ b/src/expand/mod.cpp @@ -9,6 +9,9 @@ #include "../parse/common.hpp" // For reparse from macros #include + +extern bool check_cfg(const ::AST::MetaItem& mi); + ::std::map< ::std::string, ::std::unique_ptr > g_decorators; ::std::map< ::std::string, ::std::unique_ptr > g_macros; @@ -28,18 +31,29 @@ namespace { } } +void Expand_Attr(const ::AST::MetaItem& a, AttrStage stage, ::std::function f) +{ + for( auto& d : g_decorators ) { + if( d.first == a.name() ) { + DEBUG("#[" << d.first << "] " << (int)d.second->stage() << "-" << (int)stage); + if( d.second->stage() == stage ) { + f(*d.second, a); + } + } + } +} void Expand_Attrs(const ::AST::MetaItems& attrs, AttrStage stage, ::std::function f) { for( auto& a : attrs.m_items ) { - for( auto& d : g_decorators ) { - if( d.first == a.name() ) { - DEBUG("#[" << d.first << "] " << (int)d.second->stage() << "-" << (int)stage); - if( d.second->stage() == stage ) { - f(*d.second, a); - } + if( a.name() == "cfg_attr" ) { + if( check_cfg(a.items().m_items.at(0)) ) { + Expand_Attr(a.items().m_items.at(1), stage, f); } } + else { + Expand_Attr(a, stage, f); + } } } void Expand_Attrs(const ::AST::MetaItems& attrs, AttrStage stage, ::AST::Crate& crate, const ::AST::Path& path, ::AST::Module& mod, ::AST::Item& item) @@ -118,7 +132,7 @@ void Expand_Expr(bool is_early, ::AST::Crate& crate, LList m void visit(::std::unique_ptr& cnode) { if(cnode.get()) - Expand_Attrs(cnode->attrs(), stage_pre(is_early), [&](const auto& d, const auto& a){ d.handle(a, *cnode); }); + Expand_Attrs(cnode->attrs(), stage_pre(is_early), [&](const auto& d, const auto& a){ d.handle(a, cnode); }); if(cnode.get()) { cnode->visit(*this); @@ -128,7 +142,7 @@ void Expand_Expr(bool is_early, ::AST::Crate& crate, LList m } if(cnode.get()) - Expand_Attrs(cnode->attrs(), stage_post(is_early), [&](const auto& d, const auto& a){ d.handle(a, *cnode); }); + Expand_Attrs(cnode->attrs(), stage_post(is_early), [&](const auto& d, const auto& a){ d.handle(a, cnode); }); } void visit_nodelete(const ::AST::ExprNode& parent, ::std::unique_ptr& cnode) { if( cnode.get() != nullptr ) -- cgit v1.2.3