diff options
author | John Hodge <tpg@mutabah.net> | 2017-08-27 23:23:29 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2017-08-31 16:31:24 +0800 |
commit | 0f3b5722d594bec8c425dd64cecbd63e3b5ca228 (patch) | |
tree | ab8ddd40667ca1d974c6c778cbd9506c9d7a23ed | |
parent | ac76c324ab657c58c37af983a31ec7f6c06270e1 (diff) | |
download | mrust-0f3b5722d594bec8c425dd64cecbd63e3b5ca228.tar.gz |
Expand - #[cfg] on struct literals
-rw-r--r-- | src/expand/cfg.cpp | 6 | ||||
-rw-r--r-- | src/expand/mod.cpp | 12 | ||||
-rw-r--r-- | src/include/synext_decorator.hpp | 3 |
3 files changed, 20 insertions, 1 deletions
diff --git a/src/expand/cfg.cpp b/src/expand/cfg.cpp index fcd31742..02f0c9b1 100644 --- a/src/expand/cfg.cpp +++ b/src/expand/cfg.cpp @@ -169,6 +169,12 @@ class CCfgHandler: i.m_name = ""; } } + void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::ExprNode_StructLiteral::Ent& i) const override { + DEBUG("#[cfg] struct lit - " << mi); + if( !check_cfg(sp, mi) ) { + i.value.reset(); + } + } }; STATIC_MACRO("cfg", CCfgExpander); diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp index 9137cc02..7bcf0774 100644 --- a/src/expand/mod.cpp +++ b/src/expand/mod.cpp @@ -656,8 +656,18 @@ struct CExpandExpr: this->visit_nodelete(node, node.m_base_value); for(auto& val : node.m_values) { - // TODO: Attributes on struct literal items (#[cfg] only?) + Expand_Attrs(val.attrs, AttrStage::Pre , [&](const auto& sp, const auto& d, const auto& a){ d.handle(sp, a, crate, val); }); + if( !val.value ) + continue ; this->visit_nodelete(node, val.value); + Expand_Attrs(val.attrs, AttrStage::Post, [&](const auto& sp, const auto& d, const auto& a){ d.handle(sp, a, crate, val); }); + } + for(auto it = node.m_values.begin(); it != node.m_values.end(); ) + { + if( it->value ) + ++it; + else + it = node.m_values.erase(it); } } void visit(::AST::ExprNode_Array& node) override { diff --git a/src/include/synext_decorator.hpp b/src/include/synext_decorator.hpp index 77b55710..af19491d 100644 --- a/src/include/synext_decorator.hpp +++ b/src/include/synext_decorator.hpp @@ -8,6 +8,7 @@ #include <memory> #include <span.hpp> #include "../ast/item.hpp" +#include "../ast/expr.hpp" class TypeRef; namespace AST { @@ -55,6 +56,8 @@ public: virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::std::unique_ptr<AST::ExprNode>& expr) const { unexpected(sp, mi, "expression"); } // NOTE: To delete, clear the patterns vector virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::ExprNode_Match_Arm& expr) const { unexpected(sp, mi, "match arm"); } + // NOTE: To delete, clear the value + virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::ExprNode_StructLiteral::Ent& expr) const { unexpected(sp, mi, "struct literal ent"); } }; struct DecoratorDef; |