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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
use crate::*;


pub fn resolve_builtin_fn(
    name: &str)
    -> Option<fn(&mut expr::EvalFunctionQuery) -> Result<expr::Value, ()>>
{
    match name.as_ref()
    {
        "assert" => Some(eval_builtin_assert),
        "le" => Some(eval_builtin_le),
        "ascii" => Some(eval_builtin_ascii),
        "utf8" => Some(eval_builtin_utf8),
        "utf16be" => Some(eval_builtin_utf16be),
        "utf16le" => Some(eval_builtin_utf16le),
        "utf32be" => Some(eval_builtin_utf32be),
        "utf32le" => Some(eval_builtin_utf32le),
        "strlen" => Some(eval_builtin_strlen),
        _ => None,
    }
}


pub fn get_static_size_builtin_fn(
    name: &str,
    provider: &expr::StaticallyKnownProvider,
    args: &Vec<expr::Expr>)
    -> Option<usize>
{
    let get_static_size_fn = {
        match name.as_ref()
        {
            "le" => get_static_size_builtin_le,
            _ => return None,
        }
    };

    get_static_size_fn(
        provider,
        args)
}


pub fn get_statically_known_value_builtin_fn(
    name: &str,
    _args: &Vec<expr::Expr>)
    -> bool
{
    match name.as_ref()
    {
        "assert" => false,
        "le" => true,
        "ascii" => true,
        "utf8" => true,
        "utf16be" => true,
        "utf16le" => true,
        "utf32be" => true,
        "utf32le" => true,
        "strlen" => true,
        _ => false,
    }
}


pub fn eval_builtin_fn(
    query: &mut expr::EvalFunctionQuery)
    -> Result<expr::Value, ()>
{
    let builtin_name = {
        match query.func
        {
            expr::Value::ExprBuiltInFunction(ref name) => name,
            _ => unreachable!(),
        }
    };

    let builtin_fn = resolve_builtin_fn(builtin_name).unwrap();
    builtin_fn(query)
}


pub fn eval_builtin_assert(
    query: &mut expr::EvalFunctionQuery)
    -> Result<expr::Value, ()>
{
    query.ensure_min_max_arg_number(1, 2)?;

    let condition = query.args[0]
        .value
        .expect_bool(
            query.report,
            query.args[0].span)?;

    if !condition
    {
        let msg = {
            if query.args.len() == 2
            {
                diagn::Message::error_span(
                    format!(
                        "assertion failed: {}",
                        query.args[1]
                            .value
                            .expect_string(query.report, query.args[1].span)?
                            .utf8_contents),
                    query.span)
            }
            else {
                diagn::Message::error_span("assertion failed", query.span)
            }
        };
        
        return Ok(expr::Value::FailedConstraint(
            query.report.wrap_in_parents_capped(msg)));
    }

    Ok(expr::Value::Void)
}


pub fn eval_builtin_le(
    query: &mut expr::EvalFunctionQuery)
    -> Result<expr::Value, ()>
{
    query.ensure_arg_number(1)?;

    let bigint = query.args[0].value.expect_sized_bigint(
        query.report,
        query.args[0].span)?;
    
    if bigint.size.unwrap() % 8 != 0
    {
        query.report.push_parent(
            "argument to `le` must have a size multiple of 8",
            query.args[0].span);

        query.report.note(format!(
            "got size {}",
            bigint.size.unwrap()));

        query.report.pop_parent();
        
        return Err(());
    }

    Ok(expr::Value::make_integer(bigint.convert_le()))
}


pub fn get_static_size_builtin_le(
    provider: &expr::StaticallyKnownProvider,
    args: &Vec<expr::Expr>)
    -> Option<usize>
{
    if args.len() == 1
    {
        args[0].get_static_size(provider)
    }
    else
    {
        None
    }
}


pub fn eval_builtin_string_encoding(
    encoding: &str,
    query: &mut expr::EvalFunctionQuery)
    -> Result<expr::Value, ()>
{
    query.ensure_arg_number(1)?;

    let s = query.args[0].value.expect_string(
        query.report,
        query.args[0].span)?;

    Ok(expr::Value::make_string(
        &s.utf8_contents,
        encoding))
}


pub fn eval_builtin_ascii(
    query: &mut expr::EvalFunctionQuery)
    -> Result<expr::Value, ()>
{
    eval_builtin_string_encoding("ascii", query)
}


pub fn eval_builtin_utf8(
    query: &mut expr::EvalFunctionQuery)
    -> Result<expr::Value, ()>
{
    eval_builtin_string_encoding("utf8", query)
}


pub fn eval_builtin_utf16be(
    query: &mut expr::EvalFunctionQuery)
    -> Result<expr::Value, ()>
{
    eval_builtin_string_encoding("utf16be", query)
}


pub fn eval_builtin_utf16le(
    query: &mut expr::EvalFunctionQuery)
    -> Result<expr::Value, ()>
{
    eval_builtin_string_encoding("utf16le", query)
}


pub fn eval_builtin_utf32be(
    query: &mut expr::EvalFunctionQuery)
    -> Result<expr::Value, ()>
{
    eval_builtin_string_encoding("utf32be", query)
}


pub fn eval_builtin_utf32le(
    query: &mut expr::EvalFunctionQuery)
    -> Result<expr::Value, ()>
{
    eval_builtin_string_encoding("utf32le", query)
}


pub fn eval_builtin_strlen(
    query: &mut expr::EvalFunctionQuery)
    -> Result<expr::Value, ()>
{
    query.ensure_arg_number(1)?;

    let s = query.args[0].value.expect_string(
        query.report,
        query.args[0].span)?;

    Ok(expr::Value::make_integer(s.utf8_contents.len()))

}