aboutsummaryrefslogtreecommitdiff
path: root/crates/cfg/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/cfg/src/lib.rs')
-rw-r--r--crates/cfg/src/lib.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/crates/cfg/src/lib.rs b/crates/cfg/src/lib.rs
new file mode 100644
index 000000000..a9d50e698
--- /dev/null
+++ b/crates/cfg/src/lib.rs
@@ -0,0 +1,51 @@
1//! cfg defines conditional compiling options, `cfg` attibute parser and evaluator
2
3mod cfg_expr;
4
5use rustc_hash::FxHashSet;
6use tt::SmolStr;
7
8pub use cfg_expr::CfgExpr;
9
10/// Configuration options used for conditional compilition on items with `cfg` attributes.
11/// We have two kind of options in different namespaces: atomic options like `unix`, and
12/// key-value options like `target_arch="x86"`.
13///
14/// Note that for key-value options, one key can have multiple values (but not none).
15/// `feature` is an example. We have both `feature="foo"` and `feature="bar"` if features
16/// `foo` and `bar` are both enabled. And here, we store key-value options as a set of tuple
17/// of key and value in `key_values`.
18///
19/// See: https://doc.rust-lang.org/reference/conditional-compilation.html#set-configuration-options
20#[derive(Debug, Clone, PartialEq, Eq, Default)]
21pub struct CfgOptions {
22 atoms: FxHashSet<SmolStr>,
23 key_values: FxHashSet<(SmolStr, SmolStr)>,
24}
25
26impl CfgOptions {
27 pub fn check(&self, cfg: &CfgExpr) -> Option<bool> {
28 cfg.fold(&|key, value| match value {
29 None => self.atoms.contains(key),
30 Some(value) => self.key_values.contains(&(key.clone(), value.clone())),
31 })
32 }
33
34 pub fn insert_atom(&mut self, key: SmolStr) {
35 self.atoms.insert(key);
36 }
37
38 pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) {
39 self.key_values.insert((key, value));
40 }
41
42 pub fn append(&mut self, other: &CfgOptions) {
43 for atom in &other.atoms {
44 self.atoms.insert(atom.clone());
45 }
46
47 for (key, value) in &other.key_values {
48 self.key_values.insert((key.clone(), value.clone()));
49 }
50 }
51}