buildlang 1.15.0

BuildLang (buildc): an effects-oriented compiler with a verified C path, shader output, experimental backends, opt-in linear types, and re-checkable accountability + scientific-runtime receipts
Documentation
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// ===============================================================================
// BUILDLANG CODEGEN - FORMAT STRINGS
// ===============================================================================
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (c) 2022-2026 Zain Dana Harper.
// ===============================================================================

//! Format strings for `println!`, `print!`, `eprintln!`, `eprint!` and `format!`.
//!
//! A format string is parsed into literal text and placeholders, each placeholder
//! is resolved to an argument (next positional, `{0}`, or a captured name such as
//! `{x}`), and each is turned into a C `printf` conversion that prints what Rust
//! would print. A spec the C backend cannot honour is rejected at check time with a
//! hint. The old path printed `{x}` literally, ignored widths and alignment, fell
//! back to `%d` for any spec it did not know, and printed `{:?}` of a vector as
//! `i32(<pointer>)`: programs that compiled and printed the wrong thing.

use std::sync::Arc;

use crate::codegen::backend::{CodegenError, CodegenResult};
use crate::codegen::ir::*;

use super::MirLowerer;

/// Which argument a placeholder prints.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ArgRef {
    /// `{}`: the next positional argument.
    Next,
    /// `{2}`: positional argument 2.
    Index(usize),
    /// `{name}`: a variable captured from the enclosing scope.
    Name(String),
}

/// The parts of a `{:spec}` the C backend supports.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Spec {
    /// `<` or `>`; `None` is the type's default (left for text, right for numbers).
    pub align: Option<char>,
    /// `+`: always print the sign of a number.
    pub plus: bool,
    /// `#`: alternate form (`0x` prefix for hex).
    pub alternate: bool,
    /// `0`: pad numbers with zeros.
    pub zero: bool,
    pub width: Option<usize>,
    pub precision: Option<usize>,
    /// `' '` for Display, `'?'` for Debug, or `x`, `X`, `o`.
    pub kind: char,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Piece {
    Text(String),
    Arg(ArgRef, Spec),
}

/// Parse a Rust format string into pieces. Returns a message naming the first
/// construct the C backend cannot print.
pub(crate) fn parse_format(fmt: &str) -> Result<Vec<Piece>, String> {
    let mut pieces = Vec::new();
    let mut text = String::new();
    let mut chars = fmt.chars().peekable();
    while let Some(ch) = chars.next() {
        match ch {
            '{' if chars.peek() == Some(&'{') => {
                chars.next();
                text.push('{');
            }
            '}' if chars.peek() == Some(&'}') => {
                chars.next();
                text.push('}');
            }
            '}' => {
                return Err(
                    "unmatched `}` in format string (write `}}` for a literal brace)".into(),
                )
            }
            '{' => {
                let mut inner = String::new();
                let mut closed = false;
                for c in chars.by_ref() {
                    if c == '}' {
                        closed = true;
                        break;
                    }
                    inner.push(c);
                }
                if !closed {
                    return Err(
                        "unclosed `{` in format string (write `{{` for a literal brace)".into(),
                    );
                }
                if !text.is_empty() {
                    pieces.push(Piece::Text(std::mem::take(&mut text)));
                }
                let (arg, spec) = match inner.split_once(':') {
                    Some((a, s)) => (a.trim(), Some(s)),
                    None => (inner.trim(), None),
                };
                let arg_ref = if arg.is_empty() {
                    ArgRef::Next
                } else if let Ok(i) = arg.parse::<usize>() {
                    ArgRef::Index(i)
                } else if arg.chars().all(|c| c.is_alphanumeric() || c == '_')
                    && !arg.starts_with(|c: char| c.is_ascii_digit())
                {
                    ArgRef::Name(arg.to_string())
                } else {
                    return Err(format!("`{{{inner}}}` is not a supported placeholder"));
                };
                let spec = match spec {
                    Some(s) => parse_spec(s)?,
                    None => Spec {
                        kind: ' ',
                        ..Spec::default()
                    },
                };
                pieces.push(Piece::Arg(arg_ref, spec));
            }
            _ => text.push(ch),
        }
    }
    if !text.is_empty() {
        pieces.push(Piece::Text(text));
    }
    Ok(pieces)
}

fn parse_spec(spec: &str) -> Result<Spec, String> {
    let mut out = Spec {
        kind: ' ',
        ..Spec::default()
    };
    let s: Vec<char> = spec.chars().collect();
    let mut i = 0;
    // [[fill]align]: only the default space fill is supported.
    if s.len() >= 2 && matches!(s[1], '<' | '>' | '^') {
        if s[0] != ' ' {
            return Err(format!(
                "fill character `{}` in `{{:{spec}}}` is not supported",
                s[0]
            ));
        }
        i = 1;
    }
    if i < s.len() && matches!(s[i], '<' | '>' | '^') {
        if s[i] == '^' {
            return Err(format!(
                "centre alignment `^` in `{{:{spec}}}` is not supported"
            ));
        }
        out.align = Some(s[i]);
        i += 1;
    }
    if i < s.len() && s[i] == '+' {
        out.plus = true;
        i += 1;
    } else if i < s.len() && s[i] == '-' {
        i += 1;
    }
    if i < s.len() && s[i] == '#' {
        out.alternate = true;
        i += 1;
    }
    if i < s.len() && s[i] == '0' {
        out.zero = true;
        i += 1;
    }
    let start = i;
    while i < s.len() && s[i].is_ascii_digit() {
        i += 1;
    }
    if i > start {
        out.width = Some(s[start..i].iter().collect::<String>().parse().unwrap_or(0));
    }
    if i < s.len() && s[i] == '$' {
        return Err(format!(
            "a width taken from an argument in `{{:{spec}}}` is not supported"
        ));
    }
    if i < s.len() && s[i] == '.' {
        i += 1;
        let p0 = i;
        while i < s.len() && s[i].is_ascii_digit() {
            i += 1;
        }
        if i == p0 {
            return Err(format!("precision in `{{:{spec}}}` must be a number"));
        }
        out.precision = Some(s[p0..i].iter().collect::<String>().parse().unwrap_or(0));
    }
    match &s[i..] {
        [] => {}
        ['?'] => out.kind = '?',
        ['x'] => out.kind = 'x',
        ['X'] => out.kind = 'X',
        ['o'] => out.kind = 'o',
        rest => {
            let rest: String = rest.iter().collect();
            return Err(format!(
                "format type `{rest}` in `{{:{spec}}}` is not supported"
            ));
        }
    }
    Ok(out)
}

/// C printf flags and width/precision for a spec. `left_default` is Rust's default
/// alignment for the value (left for text, right for numbers).
fn c_flags(spec: &Spec, left_default: bool, numeric: bool) -> String {
    let mut f = String::from("%");
    // Alignment only matters with a width; without one, emit no flag so the
    // plain conversions (`%s`, `%d`) stay as other backends expect them.
    let left = spec.width.is_some()
        && match spec.align {
            Some('<') => true,
            Some('>') => false,
            _ => left_default,
        };
    if left {
        f.push('-');
    }
    if numeric && spec.plus {
        f.push('+');
    }
    if spec.alternate {
        f.push('#');
    }
    if numeric && spec.zero && !left {
        f.push('0');
    }
    if let Some(w) = spec.width {
        f.push_str(&w.to_string());
    }
    f
}

const STR: &str = "BuildString";

fn is_string(ty: &MirType) -> bool {
    matches!(ty, MirType::Struct(n) if n.as_ref() == STR)
}

impl<'ctx> MirLowerer<'ctx> {
    fn fmt_reject(&self, message: String, help: &str) -> CodegenError {
        CodegenError::Rejected {
            message,
            location: None,
            help: Some(help.to_string()),
        }
    }

    /// Call a runtime function that returns a BuildString and give back its
    /// `.ptr` for `%s`.
    fn fmt_string_call(&mut self, func: &str, args: Vec<MirValue>) -> CodegenResult<MirValue> {
        let builder = self
            .current_fn
            .as_mut()
            .ok_or_else(|| CodegenError::Internal("No current function for macro".into()))?;
        let s = builder.create_local(MirType::Struct(Arc::from(STR)));
        let cont = builder.create_block();
        builder.call(MirValue::Function(Arc::from(func)), args, Some(s), cont);
        builder.switch_to_block(cont);
        Ok(self.fmt_ptr_of(MirValue::Local(s)))
    }

    /// `.ptr` of a BuildString value.
    fn fmt_ptr_of(&mut self, val: MirValue) -> MirValue {
        let builder = self.current_fn.as_mut().expect("current function");
        let base = match val {
            MirValue::Local(_) => val,
            other => {
                let tmp = builder.create_local(MirType::Struct(Arc::from(STR)));
                builder.assign(tmp, MirRValue::Use(other));
                MirValue::Local(tmp)
            }
        };
        let ptr = builder.create_local(MirType::Ptr(Box::new(MirType::i8())));
        builder.assign(
            ptr,
            MirRValue::FieldAccess {
                base,
                field_name: Arc::from("ptr"),
                field_ty: MirType::Ptr(Box::new(MirType::i8())),
            },
        );
        MirValue::Local(ptr)
    }

    /// The C conversion and the argument to pass for one placeholder.
    pub(crate) fn fmt_placeholder(
        &mut self,
        val: MirValue,
        spec: &Spec,
        what: &str,
    ) -> CodegenResult<(String, Option<MirValue>)> {
        let val = self.deref_if_pointer(val)?;
        let ty = self.type_of_value(&val);
        let debug = spec.kind == '?';
        // A char prints as the character (Debug in single quotes), not its code.
        let is_char = what.starts_with('\'')
            || matches!(&val, MirValue::Local(id) if self.char_locals.contains(id));
        if is_char && matches!(ty, MirType::Int(..)) && !matches!(spec.kind, 'x' | 'X' | 'o') {
            let arg = self.fmt_string_call("build_char_to_string", vec![val])?;
            if debug {
                return Ok(("'%s'".to_string(), Some(arg)));
            }
            return Ok((format!("{}s", c_flags(spec, true, false)), Some(arg)));
        }
        let radix = matches!(spec.kind, 'x' | 'X' | 'o');
        match &ty {
            MirType::Int(IntSize::I128, signed) => {
                if radix {
                    return Err(self.fmt_reject(
                        format!("hex or octal formatting of the 128-bit integer `{what}` is not supported"),
                        "convert to i64 or u64 first",
                    ));
                }
                let func = if *signed { "build_i128_to_string" } else { "build_u128_to_string" };
                let arg = self.fmt_string_call(func, vec![val])?;
                Ok((format!("{}s", c_flags(spec, false, false)), Some(arg)))
            }
            MirType::Int(size, signed) => {
                let wide = matches!(size, IntSize::I64 | IntSize::ISize);
                let conv = match (spec.kind, *signed) {
                    ('x', _) => "x",
                    ('X', _) => "X",
                    ('o', _) => "o",
                    (_, true) => "d",
                    (_, false) => "u",
                };
                let len = if wide { "ll" } else { "" };
                // Rust ignores a precision on integers, and so does this.
                if spec.alternate && radix {
                    // Rust writes `0xff`, `0xFF` and `0o10`; C's `#` gives
                    // `0XFF`, `010` and no prefix for zero. Write the prefix
                    // as text instead.
                    if spec.width.is_some() {
                        return Err(self.fmt_reject(
                            format!("a width together with `#` on `{what}` is not supported"),
                            "drop the width, or drop `#` and write the prefix in the format string",
                        ));
                    }
                    let prefix = if spec.kind == 'o' { "0o" } else { "0x" };
                    let plain = Spec { alternate: false, ..spec.clone() };
                    return Ok((
                        format!("{prefix}{}{len}{conv}", c_flags(&plain, false, true)),
                        Some(val),
                    ));
                }
                Ok((format!("{}{len}{conv}", c_flags(spec, false, true)), Some(val)))
            }
            MirType::Float(size) => {
                if radix {
                    return Err(self.fmt_reject(
                        format!("hex or octal formatting of the float `{what}` is not supported"),
                        "format the float with `{}` or `{:.N}`",
                    ));
                }
                if let Some(p) = spec.precision {
                    return Ok((format!("{}.{p}f", c_flags(spec, false, true)), Some(val)));
                }
                // Shortest round-trip text, as Rust prints it; Debug adds `.0`
                // to a whole number.
                let func = match (size, debug) {
                    (FloatSize::F32, false) => "build_f32_to_string",
                    (FloatSize::F64, false) => "build_f64_to_string",
                    (FloatSize::F32, true) => "build_f32_to_debug_string",
                    (FloatSize::F64, true) => "build_f64_to_debug_string",
                };
                if spec.plus || spec.zero {
                    return Err(self.fmt_reject(
                        format!("`+` or `0` padding on the float `{what}` needs a precision here"),
                        "add a precision, for example `{:+08.2}`",
                    ));
                }
                let arg = self.fmt_string_call(func, vec![val])?;
                Ok((format!("{}s", c_flags(spec, false, false)), Some(arg)))
            }
            MirType::Bool => {
                // The C backend passes a bool to printf as "true"/"false".
                Ok((format!("{}s", c_flags(spec, true, false)), Some(val)))
            }
            t if is_string(t) => {
                let arg = self.fmt_ptr_of(val);
                let conv = match spec.precision {
                    Some(p) => format!("{}.{p}s", c_flags(spec, true, false)),
                    None => format!("{}s", c_flags(spec, true, false)),
                };
                if debug {
                    if spec.width.is_some() {
                        return Err(self.fmt_reject(
                            format!("a width on `{{:?}}` of the string `{what}` is not supported"),
                            "use `{}` with the width, or `{:?}` without it",
                        ));
                    }
                    return Ok(("\"%s\"".to_string(), Some(arg)));
                }
                Ok((conv, Some(arg)))
            }
            MirType::Ptr(inner) if matches!(**inner, MirType::Int(IntSize::I8, _)) => {
                // A C string (`const char*`), already printable.
                Ok((format!("{}s", c_flags(spec, true, false)), Some(val)))
            }
            MirType::Vec(elem) => {
                if !debug {
                    return Err(self.fmt_reject(
                        format!("`{what}` is a vector, which has no `{{}}` (Display) form"),
                        "print it with `{:?}`, or loop over it and print each element",
                    ));
                }
                let suffix = match elem.as_ref() {
                    MirType::Int(IntSize::I64 | IntSize::ISize, _) => "i64",
                    MirType::Int(_, _) | MirType::Bool => "i32",
                    MirType::Float(_) => "f64",
                    t if is_string(t) => "str",
                    other => {
                        return Err(self.fmt_reject(
                            format!(
                                "`{{:?}}` of a vector of `{}` is not supported",
                                other.to_string().replace(STR, "str")
                            ),
                            "loop over the vector and print each element",
                        ))
                    }
                };
                let arg = self.fmt_string_call(&format!("build_hvec_debug_{suffix}"), vec![val])?;
                Ok((format!("{}s", c_flags(spec, true, false)), Some(arg)))
            }
            other => Err(self.fmt_reject(
                format!(
                    "`{what}` has type `{}`, which cannot be formatted by the C backend",
                    other.to_string().replace(STR, "str")
                ),
                "format its fields one by one, or give the type a `to_string` method and print that",
            )),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn spec(kind: char) -> Spec {
        Spec {
            kind,
            ..Spec::default()
        }
    }

    #[test]
    fn parses_positional_named_and_escaped_pieces() {
        let p = parse_format("a {} {{b}} {0} {x} {:?}").unwrap();
        assert_eq!(
            p,
            vec![
                Piece::Text("a ".into()),
                Piece::Arg(ArgRef::Next, spec(' ')),
                Piece::Text(" {b} ".into()),
                Piece::Arg(ArgRef::Index(0), spec(' ')),
                Piece::Text(" ".into()),
                Piece::Arg(ArgRef::Name("x".into()), spec(' ')),
                Piece::Text(" ".into()),
                Piece::Arg(ArgRef::Next, spec('?')),
            ]
        );
    }

    #[test]
    fn parses_width_alignment_precision_and_radix() {
        let p = parse_format("{:>8.3}{:<5}{:08x}{:#X}{:+}").unwrap();
        let specs: Vec<Spec> = p
            .into_iter()
            .map(|x| match x {
                Piece::Arg(_, s) => s,
                Piece::Text(_) => unreachable!(),
            })
            .collect();
        assert_eq!(specs[0].align, Some('>'));
        assert_eq!((specs[0].width, specs[0].precision), (Some(8), Some(3)));
        assert_eq!((specs[1].align, specs[1].width), (Some('<'), Some(5)));
        assert!(specs[2].zero && specs[2].width == Some(8) && specs[2].kind == 'x');
        assert!(specs[3].alternate && specs[3].kind == 'X');
        assert!(specs[4].plus);
    }

    #[test]
    fn rejects_what_c_cannot_print() {
        for bad in [
            "{:^5}", "{:*<5}", "{:1$}", "{:e}", "{:b}", "{", "}", "{a.b}",
        ] {
            assert!(parse_format(bad).is_err(), "{bad} should be rejected");
        }
    }

    #[test]
    fn c_flags_follow_rust_default_alignment() {
        let s = Spec {
            width: Some(4),
            kind: ' ',
            ..Spec::default()
        };
        assert_eq!(c_flags(&s, false, true), "%4");
        assert_eq!(c_flags(&s, true, false), "%-4");
        let z = Spec {
            width: Some(5),
            zero: true,
            kind: ' ',
            ..Spec::default()
        };
        assert_eq!(c_flags(&z, false, true), "%05");
    }
}