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
// 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 tools for creating and managing C variables in Rust.
//!
//! It allows for programmatic generation of C variable declarations with support
//! for various attributes such as `static` and `extern`, initial values, types,
//! and documentation. This is particularly useful for generating C code or bindings
//! from Rust.

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

use crate::{
    Attribute, DocComment, Expr, Format, Formatter, StorageClass, Type, declare_with,
    has_annotations, write_annotations,
};
use tamacro::DisplayFromFormat;

/// Represents a C variable with its properties and attributes.
///
/// The `Variable` struct enables the creation of complete C variable declarations,
/// supporting type definitions, initial values, static and extern modifiers, and
/// documentation comments.
///
/// # Examples
///
/// Basic variable declaration:
/// ```c
/// int counter;
/// ```
///
/// Initialized static variable:
/// ```c
/// static float value = 3.14;
/// ```
///
/// External variable declaration:
/// ```c
/// extern char *message;
/// ```
#[derive(Debug, Clone, DisplayFromFormat)]
pub struct Variable {
    /// The name of the variable
    pub name: String,

    /// The type of the variable
    pub t: Type,

    /// The optional initial value of the variable
    pub value: Option<Expr>,

    /// The storage class (`static`, `extern`, or none). `static` and `extern`
    /// are mutually exclusive, so they share one field.
    pub storage: StorageClass,

    /// The attributes applied to the variable (e.g. `aligned`, `section`, `used`)
    pub attrs: Vec<Attribute>,

    /// Raw macro/specifier tokens emitted verbatim at the leading annotation slot.
    pub raw_attrs: Vec<String>,

    /// The optional documentation comment for the variable
    pub doc: Option<DocComment>,
}

impl Variable {
    /// Creates and returns a new `VariableBuilder` to construct a `Variable` using the builder pattern.
    ///
    /// This method simplifies the process of creating complex variable declarations
    /// by providing an interface for defining all variable properties.
    ///
    /// # Parameters
    ///
    /// * `name` - The name of the variable
    /// * `t` - The type of the variable
    ///
    /// # Returns
    ///
    /// A `VariableBuilder` instance that can be used to configure and build a `Variable`
    ///
    /// # Examples
    ///
    /// ```rust
    /// let var = Variable::new("message".to_string(), Type::new(BaseType::Char).make_pointer().build())
    ///     .value(Expr::Str("Hello".to_string()))
    ///     .build();
    /// ```
    pub fn new(name: String, t: Type) -> VariableBuilder {
        VariableBuilder::new(name, t)
    }

    /// Returns the type of the variable.
    ///
    /// # Returns
    ///
    /// A clone of the variable's `Type`
    ///
    /// # Examples
    ///
    /// ```rust
    /// let var = Variable::new("x".to_string(), Type::new(BaseType::Int).build()).build();
    /// let t = var.to_type();
    /// assert_eq!(t.to_string(), "int");
    /// ```
    pub fn to_type(&self) -> Type {
        self.t.clone()
    }
}

impl Format for Variable {
    fn format(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
        if let Some(doc) = &self.doc {
            doc.format(fmt)?;
        }

        if has_annotations(&self.raw_attrs, &self.attrs) {
            write_annotations(fmt, &self.raw_attrs, &self.attrs)?;
            write!(fmt, " ")?;
        }

        if let Some(kw) = self.storage.keyword() {
            write!(fmt, "{kw} ")?;
        }

        write!(fmt, "{}", declare_with(&self.t, &self.name, fmt.options()))?;

        if !self.storage.is_extern()
            && let Some(value) = &self.value {
                write!(fmt, " = ")?;
                value.format(fmt)?;
            }

        Ok(())
    }
}

/// A builder for constructing a `Variable` instance with a fluent interface.
///
/// The `VariableBuilder` provides a step-by-step approach to creating complex C variables
/// by allowing incremental configuration of all variable attributes and properties.
pub struct VariableBuilder {
    name: String,
    t: Type,
    value: Option<Expr>,
    storage: StorageClass,
    attrs: Vec<Attribute>,
    raw_attrs: Vec<String>,
    doc: Option<DocComment>,
}

impl VariableBuilder {
    /// Creates and returns a new `VariableBuilder` with the specified name and type.
    ///
    /// # Parameters
    ///
    /// * `name` - The name of the variable as a String
    /// * `t` - The type of the variable
    ///
    /// # Returns
    ///
    /// A new `VariableBuilder` instance with default values for other properties
    ///
    /// # Examples
    ///
    /// ```rust
    /// let builder = VariableBuilder::new("count".to_string(), Type::new(BaseType::Int).build());
    /// ```
    pub fn new(name: String, t: Type) -> Self {
        Self {
            name,
            t,
            value: None,
            storage: StorageClass::None,
            attrs: vec![],
            raw_attrs: vec![],
            doc: None,
        }
    }

    /// Creates and returns a new `VariableBuilder` using a string slice for the name.
    ///
    /// This is a convenience method that converts the provided string slice to a String
    /// before delegating to the standard `new` method.
    ///
    /// # Parameters
    ///
    /// * `name` - The name of the variable as a string slice
    /// * `t` - The type of the variable
    ///
    /// # Returns
    ///
    /// A new `VariableBuilder` instance
    ///
    /// # Examples
    ///
    /// ```rust
    /// let builder = VariableBuilder::new_with_str("flag", Type::new(BaseType::Bool).build());
    /// ```
    pub fn new_with_str(name: &str, t: Type) -> Self {
        Self::new(name.to_string(), t)
    }

    /// Sets the initial value for the variable being built.
    ///
    /// # Parameters
    ///
    /// * `value` - The expression to initialize the variable with
    ///
    /// # Returns
    ///
    /// The builder instance for method chaining
    ///
    /// # Examples
    ///
    /// ```rust
    /// let builder = VariableBuilder::new_with_str("pi", Type::new(BaseType::Float).build())
    ///     .value(Expr::Float(3.14));
    /// ```
    pub fn value(mut self, value: Expr) -> Self {
        self.value = Some(value);
        self
    }

    /// Sets the documentation comment for the variable being built.
    ///
    /// # Parameters
    ///
    /// * `doc` - The documentation comment to attach to the variable
    ///
    /// # Returns
    ///
    /// The builder instance for method chaining
    ///
    /// # Examples
    ///
    /// ```rust
    /// let builder = VariableBuilder::new_with_str("size", Type::new(BaseType::Int).build())
    ///     .doc(DocComment::new("The size of the buffer").build());
    /// ```
    pub fn doc(mut self, doc: DocComment) -> Self {
        self.doc = Some(doc);
        self
    }

    /// Makes the variable static.
    ///
    /// In C, the `static` keyword limits the variable's scope to the file it is defined in
    /// and preserves its value between function calls if local.
    ///
    /// # Returns
    ///
    /// The builder instance for method chaining
    ///
    /// # Examples
    ///
    /// ```rust
    /// let builder = VariableBuilder::new_with_str("counter", Type::new(BaseType::Int).build())
    ///     .make_static();
    /// ```
    pub fn make_static(mut self) -> Self {
        // `static` and `extern` are mutually exclusive; the enum enforces that.
        self.storage = StorageClass::Static;
        self
    }

    /// Makes the variable extern.
    ///
    /// In C, the `extern` keyword indicates that the variable is defined elsewhere,
    /// typically in another compilation unit.
    ///
    /// # Returns
    ///
    /// The builder instance for method chaining
    ///
    /// # Examples
    ///
    /// ```rust
    /// let builder = VariableBuilder::new_with_str("global_flag", Type::new(BaseType::Bool).build())
    ///     .make_extern();
    /// ```
    pub fn make_extern(mut self) -> Self {
        self.storage = StorageClass::Extern;
        self
    }

    /// Sets the storage class explicitly.
    pub fn storage(mut self, storage: StorageClass) -> Self {
        self.storage = storage;
        self
    }

    /// Sets the initial value of the variable using a raw string.
    ///
    /// This is a convenience method for setting the value without constructing an `Expr` manually.
    ///
    /// # Parameters
    ///
    /// * `value` - The raw string value to initialize the variable with
    ///
    /// # Returns
    ///
    /// The builder instance for method chaining
    ///
    /// # Examples
    ///
    /// ```rust
    /// let builder = VariableBuilder::new_with_str("name", Type::new(BaseType::Char).make_pointer().build())
    ///     .raw_value("\"John\"".to_string());
    /// ```
    pub fn raw_value(mut self, value: String) -> Self {
        self.value = Some(Expr::Raw(value));
        self
    }

    /// Finalizes the variable definition and returns a fully constructed `Variable`.
    ///
    /// # Returns
    ///
    /// A fully constructed `Variable` instance
    ///
    /// # Examples
    ///
    /// ```rust
    /// let var = VariableBuilder::new_with_str("id", Type::new(BaseType::Int).build())
    ///     .value(Expr::Int(42))
    ///     .build();
    /// assert_eq!(var.to_string(), "int id = 42");
    /// ```
    /// Adds a single attribute (e.g. [`Attribute::aligned`]) to the variable.
    ///
    /// Variable attributes are emitted at the front of the declaration, e.g.
    /// `__attribute__((aligned(16))) static int buf;`.
    pub fn attr(mut self, attr: Attribute) -> Self {
        self.attrs.push(attr);
        self
    }

    /// Replaces the variable's attribute list.
    pub fn attrs(mut self, attrs: Vec<Attribute>) -> Self {
        self.attrs = attrs;
        self
    }

    /// Adds a raw macro/specifier token emitted verbatim at the leading
    /// annotation slot (like a thread-local or export macro)
    pub fn raw_attr(mut self, token: &str) -> Self {
        self.raw_attrs.push(token.to_string());
        self
    }

    /// Replaces the variable's raw annotation tokens.
    pub fn raw_attrs(mut self, tokens: Vec<String>) -> Self {
        self.raw_attrs = tokens;
        self
    }

    pub fn build(self) -> Variable {
        Variable {
            name: self.name,
            t: self.t,
            value: self.value,
            storage: self.storage,
            attrs: self.attrs,
            raw_attrs: self.raw_attrs,
            doc: self.doc,
        }
    }
}

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

    #[test]
    fn var() {
        let var = VariableBuilder::new_with_str(
            "some_var",
            TypeBuilder::new(BaseType::Char)
                .make_pointer()
                .make_const()
                .build(),
        )
        .value(Expr::Str("Hello, world".to_string()))
        .build();

        let res = "const char *some_var = \"Hello, world\"";

        assert_eq!(var.to_string(), res);

        let another_var =
            VariableBuilder::new_with_str("another_var", TypeBuilder::new(BaseType::Bool).build())
                .make_static()
                .build();

        let another_res = "static bool another_var";

        assert_eq!(another_var.to_string(), another_res);
    }

    #[test]
    fn extern_omits_initializer_and_storage_is_exclusive() {
        let ext = VariableBuilder::new_with_str("g", TypeBuilder::new(BaseType::Int).build())
            .value(Expr::Int(5))
            .make_extern()
            .build();
        assert_eq!(ext.storage, StorageClass::Extern);
        assert_eq!(ext.to_string(), "extern int g");

        let stat = VariableBuilder::new_with_str("g", TypeBuilder::new(BaseType::Int).build())
            .value(Expr::Int(5))
            .make_extern()
            .make_static()
            .build();
        assert_eq!(stat.storage, StorageClass::Static);
        assert_eq!(stat.to_string(), "static int g = 5");
    }
}