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
//! A builder API for `OptimizationOptions`.

use crate::api::*;
use std::collections::HashSet;

/// Builder methods.
impl OptimizationOptions {
    /// Sets [`ReaderOptions::file_type`].
    pub fn reader_file_type(&mut self, value: FileType) -> &mut Self {
        self.reader.file_type = value;
        self
    }

    /// Sets [`WriterOptions::file_type`].
    pub fn writer_file_type(&mut self, value: FileType) -> &mut Self {
        self.writer.file_type = value;
        self
    }

    /// Sets [`OptimizationOptions::converge`].
    pub fn set_converge(&mut self) -> &mut Self {
        self.converge = true;
        self
    }

    /// Sets [`InliningOptions::always_inline_max_size`].
    pub fn always_inline_max_size(&mut self, value: u32) -> &mut Self {
        self.inlining.always_inline_max_size = value;
        self
    }

    /// Sets [`InliningOptions::one_caller_inline_max_size`].
    pub fn one_caller_inline_max_size(&mut self, value: u32) -> &mut Self {
        self.inlining.one_caller_inline_max_size = value;
        self
    }

    /// Sets [`InliningOptions::flexible_inline_max_size`].
    pub fn flexible_inline_max_size(&mut self, value: u32) -> &mut Self {
        self.inlining.flexible_inline_max_size = value;
        self
    }

    /// Sets [`InliningOptions::allow_functions_with_loops`].
    pub fn allow_functions_with_loops(&mut self, value: bool) -> &mut Self {
        self.inlining.allow_functions_with_loops = value;
        self
    }

    /// Sets [`InliningOptions::partial_inlining_ifs`].
    pub fn partial_inlining_ifs(&mut self, value: u32) -> &mut Self {
        self.inlining.partial_inlining_ifs = value;
        self
    }

    /// Sets [`PassOptions::validate`].
    pub fn validate(&mut self, value: bool) -> &mut Self {
        self.passopts.validate = value;
        self
    }

    /// Sets [`PassOptions::validate_globally`].
    pub fn validate_globally(&mut self, value: bool) -> &mut Self {
        self.passopts.validate_globally = value;
        self
    }

    /// Sets [`PassOptions::optimize_level`].
    pub fn optimize_level(&mut self, value: OptimizeLevel) -> &mut Self {
        self.passopts.optimize_level = value;
        self
    }

    /// Sets [`PassOptions::shrink_level`].
    pub fn shrink_level(&mut self, value: ShrinkLevel) -> &mut Self {
        self.passopts.shrink_level = value;
        self
    }

    /// Sets [`PassOptions::traps_never_happen`].
    pub fn traps_never_happen(&mut self, value: bool) -> &mut Self {
        self.passopts.traps_never_happen = value;
        self
    }

    /// Sets [`PassOptions::low_memory_unused`].
    pub fn low_memory_unused(&mut self, value: bool) -> &mut Self {
        self.passopts.low_memory_unused = value;
        self
    }

    /// Sets [`PassOptions::fast_math`].
    pub fn fast_math(&mut self, value: bool) -> &mut Self {
        self.passopts.fast_math = value;
        self
    }

    /// Sets [`PassOptions::zero_filled_memory`].
    pub fn zero_filled_memory(&mut self, value: bool) -> &mut Self {
        self.passopts.zero_filled_memory = value;
        self
    }

    /// Sets [`PassOptions::debug_info`].
    pub fn debug_info(&mut self, value: bool) -> &mut Self {
        self.passopts.debug_info = value;
        self
    }

    /// Adds a pass argument to [`PassOptions::arguments`].
    pub fn set_pass_arg(&mut self, key: &str, value: &str) -> &mut Self {
        self.passopts
            .arguments
            .insert(key.to_string(), value.to_string());
        self
    }

    /// Sets [`Passes::add_default_passes`].
    pub fn add_default_passes(&mut self, value: bool) -> &mut Self {
        self.passes.add_default_passes = value;
        self
    }

    /// Adds a pass to [`Passes::more_passes`].
    pub fn add_pass(&mut self, value: Pass) -> &mut Self {
        self.passes.more_passes.push(value);
        self
    }

    /// Sets [`OptimizationOptions::features`] to [`Features::MvpOnly`].
    pub fn mvp_features_only(&mut self) -> &mut Self {
        self.features = Features::MvpOnly;
        self
    }

    /// Sets [`OptimizationOptions::features`] to [`Features::All`].
    pub fn all_features(&mut self) -> &mut Self {
        self.features = Features::All;
        self
    }

    /// Adds a feature to [`Features::Custom::enabled`].
    ///
    /// Sets [`OptimizationOptions::features`] to [`Features::Custom`] if not already.
    pub fn enable_feature(&mut self, feature: Feature) -> &mut Self {
        match &mut self.features {
            Features::Default | Features::MvpOnly | Features::All => {
                self.features = Features::Custom {
                    enabled: HashSet::new(),
                    disabled: HashSet::new(),
                };
                self.enable_feature(feature)
            }
            Features::Custom {
                enabled: features,
                disabled: _,
            } => {
                features.insert(feature);
                self
            }
        }
    }

    /// Adds a feature to [`Features::Custom::disabled`].
    ///
    /// Sets [`OptimizationOptions::features`] to [`Features::Custom`] if not already.
    pub fn disable_feature(&mut self, feature: Feature) -> &mut Self {
        match &mut self.features {
            Features::Default | Features::MvpOnly | Features::All => {
                self.features = Features::Custom {
                    enabled: HashSet::new(),
                    disabled: HashSet::new(),
                };
                self.disable_feature(feature)
            }
            Features::Custom {
                enabled: _,
                disabled: features,
            } => {
                features.insert(feature);
                self
            }
        }
    }
}