Skip to main content

type_crawler/
env.rs

1pub struct Env {
2    word_size: WordSize,
3    short_enums: bool,
4    signed_char: bool,
5}
6
7pub struct EnvOptions {
8    pub word_size: WordSize,
9    pub short_enums: bool,
10    pub signed_char: bool,
11}
12
13impl Env {
14    pub fn new(options: EnvOptions) -> Self {
15        let EnvOptions { word_size, short_enums, signed_char } = options;
16        Env { word_size, short_enums, signed_char }
17    }
18
19    pub fn word_size(&self) -> &WordSize {
20        &self.word_size
21    }
22
23    pub fn short_enums_clang_arg(&self) -> &'static str {
24        if self.short_enums { "-fshort-enums" } else { "-fno-short-enums" }
25    }
26
27    pub fn signed_char_clang_arg(&self) -> &'static str {
28        if self.signed_char { "-fsigned-char" } else { "-funsigned-char" }
29    }
30}
31
32impl Default for EnvOptions {
33    fn default() -> Self {
34        EnvOptions { word_size: WordSize::Size64, short_enums: true, signed_char: true }
35    }
36}
37
38pub enum WordSize {
39    Size16,
40    Size32,
41    Size64,
42}
43
44impl WordSize {
45    pub fn bits(&self) -> usize {
46        match self {
47            WordSize::Size16 => 16,
48            WordSize::Size32 => 32,
49            WordSize::Size64 => 64,
50        }
51    }
52
53    pub fn bytes(&self) -> usize {
54        match self {
55            WordSize::Size16 => 2,
56            WordSize::Size32 => 4,
57            WordSize::Size64 => 8,
58        }
59    }
60
61    pub fn clang_arg(&self) -> &'static str {
62        match self {
63            WordSize::Size16 => "-m16",
64            WordSize::Size32 => "-m32",
65            WordSize::Size64 => "-m64",
66        }
67    }
68}