1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
/*
* MRustC - Rust Compiler
* - By John Hodge (Mutabah/thePowersGang)
*
* expand/cfg.hpp
* - cfg! and #[cfg] handling
*/
#include <synext.hpp>
#include <parse/common.hpp>
#include <parse/tokentree.hpp>
#include <parse/ttstream.hpp>
#include "cfg.hpp"
#include <ast/expr.hpp> // Needed to clear a ExprNodeP
#include <ast/crate.hpp>
#include <map>
#include <set>
::std::multimap< ::std::string, ::std::string> g_cfg_values;
::std::map< ::std::string, ::std::function<bool(const ::std::string&)> > g_cfg_value_fcns;
::std::set< ::std::string > g_cfg_flags;
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$(val)) );
}
void Cfg_SetValueCb(::std::string name, ::std::function<bool(const ::std::string&)> cb) {
g_cfg_value_fcns.insert( ::std::make_pair(mv$(name), mv$(cb)) );
}
bool check_cfg(const Span& sp, const ::AST::Attribute& mi) {
if( mi.has_sub_items() ) {
// Must be `any`/`not`/`all`
if( mi.name() == "any" || mi.name() == "cfg" ) {
for(const auto& si : mi.items()) {
if( check_cfg(sp, si) )
return true;
}
return false;
}
else if( mi.name() == "not" ) {
if( mi.items().size() != 1 )
ERROR(sp, E0000, "cfg(not()) with != 1 argument");
return !check_cfg(sp, mi.items()[0]);
}
else if( mi.name() == "all" ) {
for(const auto& si : mi.items()) {
if( ! check_cfg(sp, si) )
return false;
}
return true;
}
else {
// oops
ERROR(sp, E0000, "Unknown cfg() function - " << mi.name());
}
}
else if( mi.has_string() ) {
// Equaliy
auto its = g_cfg_values.equal_range(mi.name().c_str());
for(auto it = its.first; it != its.second; ++it)
{
DEBUG(""<<mi.name()<<": '"<<it->second<<"' == '"<<mi.string()<<"'");
if( it->second == mi.string() )
return true;
}
if( its.first != its.second )
return false;
auto it2 = g_cfg_value_fcns.find(mi.name().c_str());
if(it2 != g_cfg_value_fcns.end() )
{
DEBUG(""<<mi.name()<<": ('"<<mi.string()<<"')?");
return it2->second( mi.string() );
}
WARNING(sp, W0000, "Unknown cfg() param '" << mi.name() << "'");
return false;
}
else {
// Flag
auto it = g_cfg_flags.find(mi.name().c_str());
return (it != g_cfg_flags.end());
}
BUG(sp, "Fell off the end of check_cfg");
}
class CCfgExpander:
public ExpandProcMacro
{
::std::unique_ptr<TokenStream> expand(const Span& sp, const ::AST::Crate& crate, const TokenTree& tt, AST::Module& mod) override
{
auto lex = TTStream(sp, tt);
auto attrs = Parse_MetaItem(lex);
DEBUG("cfg!() - " << attrs);
if( check_cfg(sp, attrs) ) {
return box$( TTStreamO(sp, TokenTree({},TOK_RWORD_TRUE )) );
}
else {
return box$( TTStreamO(sp, TokenTree({},TOK_RWORD_FALSE)) );
}
}
};
class CCfgHandler:
public ExpandDecorator
{
AttrStage stage() const override { return AttrStage::Pre; }
void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate) const override {
DEBUG("#[cfg] crate - " << mi);
// Ignore, as #[cfg] on a crate is handled in expand/mod.cpp
if( check_cfg(sp, mi) ) {
}
else {
crate.m_root_module.items().clear();
}
}
void handle(const Span& sp, const AST::Attribute& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const override {
TRACE_FUNCTION_FR("#[cfg] item - " << mi, (i.is_None() ? "Deleted" : ""));
if( check_cfg(sp, mi) ) {
// Leave
}
else {
i = AST::Item::make_None({});
}
}
void handle(const Span& sp, const AST::Attribute& mi, ::AST::Crate& crate, ::std::unique_ptr<AST::ExprNode>& expr) const override {
DEBUG("#[cfg] expr - " << mi);
if( check_cfg(sp, mi) ) {
// Leave
}
else {
expr.reset();
}
}
void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, const AST::Module& mod, AST::ImplDef& impl) const override {
DEBUG("#[cfg] impl - " << mi);
if( check_cfg(sp, mi) ) {
// Leave
}
else {
impl.type() = ::TypeRef(sp);
}
}
void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::StructItem& si) const override {
DEBUG("#[cfg] struct item - " << mi);
if( !check_cfg(sp, mi) ) {
si.m_name = "";
}
}
void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::TupleItem& i) const override {
DEBUG("#[cfg] tuple item - " << mi);
if( !check_cfg(sp, mi) ) {
i.m_type = ::TypeRef(sp);
}
}
void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::EnumVariant& i) const override {
DEBUG("#[cfg] enum variant - " << mi);
if( !check_cfg(sp, mi) ) {
i.m_name = "";
}
}
void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::ExprNode_Match_Arm& i) const override {
DEBUG("#[cfg] match arm - " << mi);
if( !check_cfg(sp, mi) ) {
i.m_patterns.clear();
}
}
void handle(const Span& sp, const AST::Attribute& 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);
STATIC_DECORATOR("cfg", CCfgHandler);
|