tamago 0.2.0

A code generator library for C, with a builder-pattern API
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
// Copyright (c) 2025 Nobuharu Shimazu
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

//! This module provides a formatter implementation that emits C code with the
//! right indentation.
//!
//! Output can be built into a `String` via `to_string()` / [`render`], or
//! *streamed* into any [`fmt::Write`] or [`io::Write`] sink via [`render_to`] /
//! [`render_to_io`], which avoids materializing one large `String` for big
//! translation units. [`RenderOptions`] controls opt-in behavior: the
//! [`AttrStyle`] for attributes (GNU vs C23), the [`IndentStyle`] (spaces or
//! tabs) and [`BraceStyle`] (K&R vs Allman), and `#line` directives driven by
//! [`SourceLoc`]-tagged items.

use std::fmt::{self, Write};
use std::io;

const DEFAULT_INDENT: usize = 2;

pub trait Format {
    fn format(&self, fmt: &mut Formatter<'_>) -> fmt::Result;
}

/// A source location used for `#line` mapping: a file path and a 1-based line
/// number in the *original* (pre-generation) source
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SourceLoc {
    /// The originating source file.
    pub file: String,

    /// The 1-based line number within that file.
    pub line: u64,
}

impl SourceLoc {
    /// Creates a new source location.
    pub fn new(file: impl Into<String>, line: u64) -> Self {
        Self {
            file: file.into(),
            line,
        }
    }
}

/// The spelling style used when emitting attributes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AttrStyle {
    /// GNU style, e.g. `__attribute__((packed))`. This is the default.
    #[default]
    Gnu,

    /// C23 bracket style, e.g. `[[gnu::packed]]`.
    C23,
}

/// How each level of indentation is rendered.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IndentStyle {
    /// Indent with the given number of spaces per level (the default is 2).
    Spaces(usize),

    /// Indent with a single tab per level.
    Tabs,
}

impl Default for IndentStyle {
    fn default() -> Self {
        IndentStyle::Spaces(DEFAULT_INDENT)
    }
}

impl IndentStyle {
    /// The number of indent units added per nesting level.
    fn step(self) -> usize {
        match self {
            IndentStyle::Spaces(n) => n,
            IndentStyle::Tabs => 1,
        }
    }

    /// The character emitted for each indent unit.
    fn unit(self) -> char {
        match self {
            IndentStyle::Spaces(_) => ' ',
            IndentStyle::Tabs => '\t',
        }
    }
}

/// Where the opening brace of a block is placed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BraceStyle {
    /// K&R / "one true brace" style: the `{` stays on the same line, e.g.
    /// `int main(void) {`. This is the default.
    #[default]
    Attach,

    /// Allman style: the `{` goes on its own line, e.g.
    /// ```c
    /// int main(void)
    /// {
    /// ```
    NextLine,
}

/// Options that control how code is rendered. Use with [`render`],
/// [`render_to`], [`render_to_io`], or [`Formatter::with_options`].
#[derive(Debug, Clone, Copy, Default)]
pub struct RenderOptions {
    /// When `true`, located items and statements emit `#line N "file"`
    /// directives whenever the source location changes.
    pub line_directives: bool,

    /// The attribute spelling style.
    pub attr_style: AttrStyle,

    /// When `true`, emit C23 keyword spellings for operators that have both a
    /// classic and a C23 form — currently `alignof` instead of `_Alignof`. This
    /// is deliberately independent of [`attr_style`](Self::attr_style): the
    /// attribute syntax and the choice of keyword spelling are separate language
    /// concerns (you might target C11 with GNU attributes, or C23 while keeping
    /// `_Alignof`).
    pub c23_keywords: bool,

    /// How indentation is rendered (spaces or tabs).
    pub indent: IndentStyle,

    /// Where opening braces are placed.
    pub brace_style: BraceStyle,
}

/// Renders any [`Format`] value to a `String` using the given [`RenderOptions`].
///
/// This is the entry point for opt-in features such as `#line` directives and
/// the C23 attribute style; the plain `to_string()` path always uses the
/// defaults (GNU attributes, no line directives).
pub fn render<T: Format>(item: &T, opts: RenderOptions) -> String {
    let mut dst = String::new();
    let mut fmt = Formatter::with_options(&mut dst, opts);
    // Writing into a String is infallible, so the result is safe to discard.
    let _ = item.format(&mut fmt);
    dst
}

/// Streams a [`Format`] value into any [`fmt::Write`] sink instead of building
/// one big `String` first. This matters for large translation units, where the
/// intermediate `String` would be sizable.
///
/// # Examples
///
/// ```rust
/// let mut out = String::new();
/// render_to(&item, &mut out, RenderOptions::default())?;
/// ```
pub fn render_to<T: Format, W: fmt::Write>(
    item: &T,
    dst: &mut W,
    opts: RenderOptions,
) -> fmt::Result {
    let mut fmt = Formatter::with_options(dst, opts);
    item.format(&mut fmt)
}

/// Streams a [`Format`] value directly into an [`io::Write`] sink (a file, a
/// socket, `stdout`, ...) without an intermediate `String` :)
///
/// I/O errors from the sink are surfaced as [`io::Error`].
///
/// # Examples
///
/// ```rust
/// let file = std::fs::File::create("out.c")?;
/// let mut writer = std::io::BufWriter::new(file);
/// render_to_io(&translation_unit, &mut writer, RenderOptions::default())?;
/// ```
pub fn render_to_io<T: Format, W: io::Write>(
    item: &T,
    dst: &mut W,
    opts: RenderOptions,
) -> io::Result<()> {
    /// Bridges an [`io::Write`] to [`fmt::Write`], capturing the first I/O error.
    struct IoAdapter<'w, W: io::Write> {
        w: &'w mut W,
        err: Option<io::Error>,
    }

    impl<W: io::Write> fmt::Write for IoAdapter<'_, W> {
        fn write_str(&mut self, s: &str) -> fmt::Result {
            self.w.write_all(s.as_bytes()).map_err(|e| {
                self.err = Some(e);
                fmt::Error
            })
        }
    }

    let mut adapter = IoAdapter { w: dst, err: None };
    let res = {
        let mut fmt = Formatter::with_options(&mut adapter, opts);
        item.format(&mut fmt)
    };

    if let Some(e) = adapter.err {
        return Err(e);
    }
    res.map_err(|_| io::Error::other("formatting failed"))
}

/// Formatter for a scope.
///
/// Writes into any [`fmt::Write`] sink (a `String`, or an [`io::Write`] adapter),
/// so output can be streamed rather than accumulated into one large `String`.
pub struct Formatter<'a> {
    /// The sink the formatted code is written to.
    dst: &'a mut dyn fmt::Write,

    /// Whether the cursor is at the start of a line (indentation pending).
    at_line_start: bool,

    /// The current indentation width, in indent units (spaces or tabs).
    spaces: usize,

    /// The number of indent units added per nesting level.
    indent: usize,

    /// The character emitted per indent unit (`' '` or `'\t'`).
    indent_unit: char,

    /// Rendering options (attribute style, indent/brace style, `#line`).
    opts: RenderOptions,

    /// The most recently emitted source location, for `#line` de-duplication.
    last_loc: Option<SourceLoc>,
}

impl fmt::Debug for Formatter<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Formatter")
            .field("spaces", &self.spaces)
            .field("indent", &self.indent)
            .field("opts", &self.opts)
            .finish_non_exhaustive()
    }
}

impl<'a> Formatter<'a> {
    pub fn new<W: fmt::Write>(dst: &'a mut W) -> Self {
        Self::with_options(dst, RenderOptions::default())
    }

    /// Creates a formatter with explicit [`RenderOptions`], over any sink.
    pub fn with_options<W: fmt::Write>(dst: &'a mut W, opts: RenderOptions) -> Self {
        Self {
            dst,
            at_line_start: true,
            spaces: 0,
            indent: opts.indent.step(),
            indent_unit: opts.indent.unit(),
            opts,
            last_loc: None,
        }
    }

    /// Returns the attribute spelling style in effect.
    pub fn attr_style(&self) -> AttrStyle {
        self.opts.attr_style
    }

    /// Returns whether C23 keyword spellings (e.g. `alignof`) are in effect.
    pub fn c23_keywords(&self) -> bool {
        self.opts.c23_keywords
    }

    /// Returns the rendering options in effect.
    ///
    /// Useful when a sub-render has to be materialized into a `String` yet must
    /// preserve the ambient options (see the prefix-unary path in `expr`), so
    /// that nested option-sensitive nodes render consistently.
    pub(crate) fn options(&self) -> RenderOptions {
        self.opts
    }

    /// The current indentation width, in indent units (spaces or tabs).
    pub(crate) fn current_indent(&self) -> usize {
        self.spaces
    }

    /// Returns whether `#line` directives are being emitted
    pub fn line_directives_enabled(&self) -> bool {
        self.opts.line_directives
    }

    /// Emits a `#line N "file"` directive if line directives are enabled and the
    /// location differs from the last one emitted, and otherwise, it does nothing.
    ///
    /// Located items and statements call this before emitting their content, so
    /// that the C compiler and any debugger map the following code back to the
    /// original source.
    pub fn sync_line(&mut self, loc: &SourceLoc) -> fmt::Result {
        if !self.opts.line_directives {
            return Ok(());
        }
        if self.last_loc.as_ref() == Some(loc) {
            return Ok(());
        }
        if !self.is_start_of_line() {
            writeln!(self)?;
        }
        writeln!(
            self,
            "#line {} \"{}\"",
            loc.line,
            crate::escape::escape_c_str(&loc.file)
        )?;
        self.last_loc = Some(loc.clone());
        Ok(())
    }

    pub fn block<F>(&mut self, f: F) -> fmt::Result
    where
        F: FnOnce(&mut Self) -> fmt::Result,
    {
        match self.opts.brace_style {
            BraceStyle::Attach => {
                if !self.is_start_of_line() {
                    write!(self, " ")?;
                }
            }
            BraceStyle::NextLine => {
                if !self.is_start_of_line() {
                    writeln!(self)?;
                }
            }
        }

        writeln!(self, "{{")?;
        self.indent(f)?;
        write!(self, "}}")?;

        Ok(())
    }

    pub fn indent<F, R>(&mut self, f: F) -> R
    where
        F: FnOnce(&mut Self) -> R,
    {
        self.spaces += self.indent;
        let res = f(self);
        self.spaces -= self.indent;
        res
    }

    pub fn is_start_of_line(&self) -> bool {
        self.at_line_start
    }
}

/// Writes `n` indent units of the given character to the sink, batching to avoid
/// a virtual call per character.
fn write_indent(dst: &mut dyn fmt::Write, unit: char, n: usize) -> fmt::Result {
    const SPACES: &str = "                                "; // 32 spaces
    const TABS: &str = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; // 16 tabs
    let filler = if unit == '\t' { TABS } else { SPACES };
    let mut remaining = n;
    while remaining > 0 {
        let take = remaining.min(filler.len());
        dst.write_str(&filler[..take])?;
        remaining -= take;
    }
    Ok(())
}

impl Write for Formatter<'_> {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        for (idx, line) in s.split('\n').enumerate() {
            if idx != 0 {
                self.dst.write_char('\n')?;
                self.at_line_start = true;
            }

            if line.is_empty() {
                continue;
            }

            if self.at_line_start {
                write_indent(self.dst, self.indent_unit, self.spaces)?;
                self.at_line_start = false;
            }

            self.dst.write_str(line)?;
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{BaseType, BlockBuilder, Function, FunctionBuilder, Statement, Type};

    fn sample() -> Function {
        FunctionBuilder::new_with_str("f", Type::base(BaseType::Void))
            .body(
                BlockBuilder::new()
                    .statement(Statement::Return(None))
                    .build(),
            )
            .build()
    }

    #[test]
    fn stream_to_fmt_write_matches_to_string() {
        let f = sample();
        let mut streamed = String::new();
        render_to(&f, &mut streamed, RenderOptions::default()).unwrap();
        assert_eq!(streamed, f.to_string());
    }

    #[test]
    fn stream_to_io_write() {
        let f = sample();
        let mut bytes: Vec<u8> = Vec::new();
        render_to_io(&f, &mut bytes, RenderOptions::default()).unwrap();
        assert_eq!(String::from_utf8(bytes).unwrap(), f.to_string());
    }

    #[test]
    fn indent_spaces_width() {
        let f = sample();
        let out = render(
            &f,
            RenderOptions {
                indent: IndentStyle::Spaces(4),
                ..Default::default()
            },
        );
        assert_eq!(out, "void f(void) {\n    return;\n}\n");
    }

    #[test]
    fn indent_tabs() {
        let f = sample();
        let out = render(
            &f,
            RenderOptions {
                indent: IndentStyle::Tabs,
                ..Default::default()
            },
        );
        assert_eq!(out, "void f(void) {\n\treturn;\n}\n");
    }

    #[test]
    fn brace_style_next_line() {
        let f = sample();
        let out = render(
            &f,
            RenderOptions {
                brace_style: BraceStyle::NextLine,
                ..Default::default()
            },
        );
        assert_eq!(out, "void f(void)\n{\n  return;\n}\n");
    }
}