diff options
Diffstat (limited to 'src/expand/cfg.cpp')
-rw-r--r-- | src/expand/cfg.cpp | 55 |
1 files changed, 50 insertions, 5 deletions
diff --git a/src/expand/cfg.cpp b/src/expand/cfg.cpp index 546bb690..d178b9ae 100644 --- a/src/expand/cfg.cpp +++ b/src/expand/cfg.cpp @@ -2,6 +2,13 @@ #include <synext.hpp> #include <parse/tokentree.hpp> #include <parse/lex.hpp> +#include <parse/common.hpp> + +bool check_cfg(const ::AST::MetaItem& mi) { + // TODO: Handle cfg conditions + throw ::std::runtime_error("TODO: Handle #[cfg] or cfg! conditions"); + return true; +} class CCfgExpander: public ExpandProcMacro @@ -14,13 +21,51 @@ class CCfgExpander: ERROR(sp, E0000, "cfg! doesn't take an identifier"); } - DEBUG("cfg!()"); - - // TODO: Handle cfg!() + auto lex = TTStreamO(tt); + auto attrs = Parse_MetaItem(lex); + DEBUG("cfg!() - " << attrs); - return box$( TTStreamO(TokenTree(TOK_RWORD_FALSE)) ); + if( check_cfg(attrs) ) { + return box$( TTStreamO(TokenTree(TOK_RWORD_TRUE )) ); + } + else { + return box$( TTStreamO(TokenTree(TOK_RWORD_FALSE)) ); + } + } +}; + + +class CCfgHandler: + public ExpandDecorator +{ + AttrStage stage() const override { return AttrStage::EarlyPre; } + + + void handle(const AST::MetaItem& mi, AST::Crate& crate, AST::MacroInvocation& mac) const override { + if( check_cfg(mi) ) { + // Leave as is + } + else { + mac.clear(); + } + } + void handle(const AST::MetaItem& mi, AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const override { + if( check_cfg(mi) ) { + // Leave + } + else { + i = AST::Item::make_None({}); + } + } + void handle(const AST::MetaItem& mi, ::std::unique_ptr<AST::ExprNode>& expr) const override { + if( check_cfg(mi) ) { + // Leave + } + else { + expr.reset(); + } } }; STATIC_MACRO("cfg", CCfgExpander); -//STATIC_DECORATOR("cfg", CCfgHandler); +STATIC_DECORATOR("cfg", CCfgHandler); |