nandi/rustnimpublic Fork 0
d4592812fae6b652d94be442f5b3a408d3ab21b6
Commits
Clone
git clone https://git.rickub.com/nandi/rustnim.git
git clone ssh://git@rickub.com/nandi/rustnim.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Lower `extern "C"` blocks to Nim importc; do not transpile libc 7db7991 · on d4592812fae6b652d94be442f5b3a408d3ab21b6 · nandithebull · 6h ago
ty.rs · 403 lines · 15.0 KBRust Blame HistoryRaw
  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
//! Rust type -> Nim type mapping.
//!
//! Integer width is preserved exactly. Anything that cannot be represented
//! faithfully in Nim is reported as an error rather than approximated: a
//! silently widened integer would change the meaning of wrapping arithmetic,
//! which is precisely the kind of code people write in Rust.

use syn::{GenericArgument, PathArguments, Type, TypeParamBound};

#[derive(Debug, Clone, PartialEq)]
pub enum Nim {
    Prim(String),
    Seq(Box<Nim>),
    OpenArray(Box<Nim>),
    Array(usize, Box<Nim>),
    Tuple(Vec<Nim>),
    Named(String, Vec<Nim>),
    Var(Box<Nim>),
    /// `*mut T`. Nim's `ptr` is the same thing: an unmanaged address.
    Ptr(Box<Nim>),
    /// `*const T`. The const matters at the C level even though it does not at
    /// the Rust one: Nim emits a real prototype where Rust emits none, and a
    /// `char *` declaration against C's `const char *` is a compile error.
    ConstPtr(Box<Nim>),
    /// `impl Fn(A) -> B` / `fn(A) -> B`. Left at Nim's default calling
    /// convention (`closure`), which accepts both a plain top-level proc and
    /// a closure that captures -- and Rust's `impl Fn` accepts both too.
    Proc(Vec<Nim>, Box<Nim>),
    Unit,
}

impl Nim {
    pub fn render(&self) -> String {
        match self {
            Nim::Prim(s) => s.clone(),
            Nim::Seq(t) => format!("seq[{}]", t.render()),
            Nim::OpenArray(t) => format!("openArray[{}]", t.render()),
            Nim::Array(n, t) => format!("array[{}, {}]", n, t.render()),
            Nim::Tuple(ts) => {
                let inner: Vec<String> = ts.iter().map(|t| t.render()).collect();
                format!("({})", inner.join(", "))
            }
            Nim::Named(n, args) if args.is_empty() => n.clone(),
            Nim::Named(n, args) => {
                let inner: Vec<String> = args.iter().map(|t| t.render()).collect();
                format!("{}[{}]", n, inner.join(", "))
            }
            Nim::Var(t) => format!("var {}", t.render()),
            Nim::Ptr(t) => match &**t {
                Nim::Prim(p) if p == "void" => "pointer".into(),
                inner => format!("ptr {}", inner.render()),
            },
            Nim::ConstPtr(t) => match &**t {
                Nim::Prim(p) if p == "void" => "pointer".into(),
                inner => const_ptr_alias(inner),
            },
            Nim::Proc(args, ret) => {
                // Nim's proc types name their parameters even when the name is
                // never used.
                let inner: Vec<String> = args
                    .iter()
                    .enumerate()
                    .map(|(i, t)| format!("a{}: {}", i, t.render()))
                    .collect();
                match &**ret {
                    Nim::Unit => format!("proc ({})", inner.join(", ")),
                    r => format!("proc ({}): {}", inner.join(", "), r.render()),
                }
            }
            Nim::Unit => "void".into(),
        }
    }

    /// Strip a `var`, which is a parameter-passing mode rather than a type.
    /// Unlike `owned`, this keeps a view a view.
    pub fn unvar(self) -> Nim {
        match self {
            Nim::Var(t) => t.unvar(),
            other => other,
        }
    }

    /// Owned form: a borrowed slice parameter is `openArray[T]`, but the same
    /// type in an owned position (a field, a return value) must be `seq[T]`.
    pub fn owned(self) -> Nim {
        match self {
            Nim::OpenArray(t) => Nim::Seq(t),
            Nim::Var(t) => t.owned(),
            other => other,
        }
    }

    pub fn is_integer(&self) -> bool {
        matches!(self, Nim::Prim(p) if matches!(p.as_str(),
            "int8"|"int16"|"int32"|"int64"|"int"|
            "uint8"|"uint16"|"uint32"|"uint64"|"uint"))
    }

    pub fn is_unsigned(&self) -> bool {
        matches!(self, Nim::Prim(p) if p.starts_with("uint"))
    }
}

/// C types, as `libc` spells them. Nim has the same set under its own names,
/// and both are defined by the platform's C compiler, so these are equal by
/// construction rather than by assumption.
pub fn c_type(name: &str) -> Option<&'static str> {
    Some(match name {
        "c_char" => "cchar",
        "c_schar" => "cschar",
        "c_uchar" => "cuchar",
        "c_short" => "cshort",
        "c_ushort" => "cushort",
        "c_int" => "cint",
        "c_uint" => "cuint",
        "c_long" => "clong",
        "c_ulong" => "culong",
        "c_longlong" => "clonglong",
        "c_ulonglong" => "culonglong",
        "c_float" => "cfloat",
        "c_double" => "cdouble",
        "size_t" => "csize_t",
        "ssize_t" => "int",
        "intptr_t" => "int",
        "uintptr_t" => "uint",
        "c_void" => "void",
        _ => return None,
    })
}

pub fn prim(name: &str) -> Option<Nim> {
    let mapped = match name {
        "i8" => "int8",
        "i16" => "int16",
        "i32" => "int32",
        "i64" => "int64",
        "isize" => "int",
        "u8" => "uint8",
        "u16" => "uint16",
        "u32" => "uint32",
        "u64" => "uint64",
        "usize" => "uint",
        "f32" => "float32",
        "f64" => "float64",
        "bool" => "bool",
        "char" => "Rune",
        "str" | "String" => "string",
        other => match c_type(other) {
            Some(c) => c,
            None => return None,
        },
    };
    Some(Nim::Prim(mapped.into()))
}

/// Types we refuse rather than approximate.
pub fn rejected(name: &str) -> Option<&'static str> {
    match name {
        "i128" | "u128" => Some("128-bit integers have no faithful Nim equivalent"),
        _ => None,
    }
}

/// The Nim type name for a const-qualified C pointer to `t`.
pub fn const_ptr_alias(t: &Nim) -> String {
    format!("RsConstPtr{}", t.render().replace(' ', ""))
}

/// The C spelling of a Nim type, for a const-qualified pointer declaration.
pub fn c_spelling(t: &Nim) -> Option<&'static str> {
    let Nim::Prim(p) = t else { return None };
    Some(match p.as_str() {
        "cchar" => "char",
        "cschar" => "signed char",
        "cuchar" => "unsigned char",
        "cshort" => "short",
        "cushort" => "unsigned short",
        "cint" => "int",
        "cuint" => "unsigned int",
        "clong" => "long",
        "culong" => "unsigned long",
        "clonglong" => "long long",
        "culonglong" => "unsigned long long",
        "cfloat" => "float",
        "cdouble" => "double",
        "uint8" => "unsigned char",
        "int8" => "signed char",
        "uint16" => "unsigned short",
        "int16" => "short",
        "uint32" => "unsigned int",
        "int32" => "int",
        "uint64" => "unsigned long long",
        "int64" => "long long",
        _ => return None,
    })
}

fn ret_ty(r: &syn::ReturnType) -> Result<Nim, String> {
    match r {
        syn::ReturnType::Default => Ok(Nim::Unit),
        syn::ReturnType::Type(_, t) => Ok(map(t)?.owned()),
    }
}

/// Types from `core::fmt` that must not be confused with a user type of the
/// same short name. `fmt::Error` and a crate's own `Error` are different
/// types, and collapsing a path to its last segment would merge them.
fn std_qualified(p: &syn::Path) -> Option<Nim> {
    let segs: Vec<String> = p.segments.iter().map(|s| s.ident.to_string()).collect();
    if segs.len() < 2 {
        return None;
    }
    let (q, name) = (&segs[segs.len() - 2], segs.last()?.as_str());
    if q != "fmt" {
        return None;
    }
    Some(Nim::Prim(
        match name {
            "Error" => "FmtError",
            "Formatter" => "Formatter",
            // `fmt::Result` is `Result<(), fmt::Error>`. A formatting impl is
            // lowered to a proc that returns the formatted string, so the
            // result type is erased there; this spelling exists so that a
            // signature mentioning it still maps to something.
            "Result" => "FmtResult",
            _ => return None,
        }
        .into(),
    ))
}

pub fn map(t: &Type) -> Result<Nim, String> {
    match t {
        Type::Path(p) => {
            if let Some(n) = std_qualified(&p.path) {
                return Ok(n);
            }
            let seg = p
                .path
                .segments
                .last()
                .ok_or_else(|| "empty type path".to_string())?;
            let name = seg.ident.to_string();

            if let Some(why) = rejected(&name) {
                return Err(format!("unsupported type `{}`: {}", name, why));
            }

            let args: Vec<Nim> = match &seg.arguments {
                PathArguments::AngleBracketed(a) => a
                    .args
                    .iter()
                    .filter_map(|g| match g {
                        GenericArgument::Type(t) => Some(map(t)),
                        _ => None,
                    })
                    .collect::<Result<_, _>>()?,
                _ => vec![],
            };

            match (name.as_str(), args.len()) {
                // The `log` facade's types, under shim names.
                ("Level", 0) => Ok(Nim::Prim("RsLogLevel".into())),
                ("LevelFilter", 0) => Ok(Nim::Prim("RsLogFilter".into())),
                ("Vec", 1) => Ok(Nim::Seq(Box::new(args[0].clone().owned()))),
                ("Option", 1) => Ok(Nim::Named("Option".into(), args)),
                ("Result", 2) => Ok(Nim::Named("Result".into(), args)),
                ("Box", 1) => Ok(args[0].clone()),
                _ => {
                    if let Some(p) = prim(&name) {
                        Ok(p)
                    } else {
                        Ok(Nim::Named(name, args))
                    }
                }
            }
        }
        // &T is a value in Nim; &mut T becomes a `var` parameter. The caller
        // decides whether a `var` is legal in the position it is used.
        Type::Reference(r) => {
            // `&str` is a borrowed view of characters, not an owned string.
            // Nim accepts a `string` argument for an `openArray[char]`
            // parameter, so a literal still passes straight through.
            if let Type::Path(p) = &*r.elem {
                if p.path.is_ident("str") {
                    return Ok(Nim::OpenArray(Box::new(Nim::Prim("char".into()))));
                }
            }
            let inner = map(&r.elem)?;
            if r.mutability.is_some() {
                Ok(Nim::Var(Box::new(inner)))
            } else {
                Ok(inner)
            }
        }
        Type::Ptr(p) => {
            let inner = Box::new(map(&p.elem)?);
            Ok(if matches!(p.mutability, syn::PointerMutability::Mut(_)) {
                Nim::Ptr(inner)
            } else {
                Nim::ConstPtr(inner)
            })
        }
        Type::Slice(s) => Ok(Nim::OpenArray(Box::new(map(&s.elem)?))),
        Type::Array(a) => {
            let len = match &a.len {
                syn::Expr::Lit(syn::ExprLit {
                    lit: syn::Lit::Int(i),
                    ..
                }) => i
                    .base10_parse::<usize>()
                    .map_err(|e| format!("array length: {}", e))?,
                _ => return Err("array length must be a literal".into()),
            };
            Ok(Nim::Array(len, Box::new(map(&a.elem)?)))
        }
        Type::Tuple(t) if t.elems.is_empty() => Ok(Nim::Unit),
        Type::Tuple(t) => Ok(Nim::Tuple(
            t.elems.iter().map(map).collect::<Result<_, _>>()?,
        )),
        Type::Paren(p) => map(&p.elem),
        Type::Group(g) => map(&g.elem),
        Type::FnPtr(f) => {
            let args: Vec<Nim> = f
                .inputs
                .iter()
                .map(|a| map(&a.ty))
                .collect::<Result<_, _>>()?;
            Ok(Nim::Proc(args, Box::new(ret_ty(&f.output)?)))
        }
        // `dyn Fn(A) -> B` is a callable, exactly as `impl Fn(A) -> B` is.
        // Other trait objects need a vtable, which the lowering builds from
        // the trait's declaration; `dyn` is spelled `<Trait>Dyn` there.
        Type::TraitObject(t) => {
            for b in &t.bounds {
                if let TypeParamBound::Trait(tb) = b {
                    if let Some(seg) = tb.path.segments.last() {
                        let n = seg.ident.to_string();
                        if n == "Fn" || n == "FnMut" || n == "FnOnce" {
                            if let PathArguments::Parenthesized(a) = &seg.arguments {
                                let args: Vec<Nim> = a
                                    .inputs
                                    .iter()
                                    .map(|a| map(&a.ty))
                                    .collect::<Result<_, _>>()?;
                                return Ok(Nim::Proc(args, Box::new(ret_ty(&a.output)?)));
                            }
                        }
                        // `dyn T + Send + Sync`: the auto traits carry no
                        // methods, so the first real bound names the object.
                        if !matches!(n.as_str(), "Send" | "Sync" | "Unpin" | "Sized") {
                            return Ok(Nim::Named(format!("{n}Dyn"), vec![]));
                        }
                    }
                }
            }
            Err("a trait object with no nameable trait bound".into())
        }
        Type::ImplTrait(i) => {
            // `impl AsRef<[u8]>` and friends: fall back to the bound's own
            // shape where we can recognise it, since Nim has no impl-trait.
            for b in &i.bounds {
                if let TypeParamBound::Trait(tb) = b {
                    if let Some(seg) = tb.path.segments.last() {
                        // `impl Fn(A) -> B` is a callable; Nim has a proc type
                        // for exactly this.
                        if seg.ident == "Fn" || seg.ident == "FnMut" || seg.ident == "FnOnce" {
                            if let PathArguments::Parenthesized(a) = &seg.arguments {
                                let args: Vec<Nim> = a
                                    .inputs
                                    .iter()
                                    .map(|a| map(&a.ty))
                                    .collect::<Result<_, _>>()?;
                                return Ok(Nim::Proc(args, Box::new(ret_ty(&a.output)?)));
                            }
                        }
                        if seg.ident == "AsRef" || seg.ident == "Into" {
                            if let PathArguments::AngleBracketed(a) = &seg.arguments {
                                for g in &a.args {
                                    if let GenericArgument::Type(t) = g {
                                        return map(t);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Err("unsupported `impl Trait` type".into())
        }
        Type::Infer(_) => Err("inferred type in a position that needs a name".into()),
        other => Err(format!("unsupported type form: {:?}", discriminant(other))),
    }
}

fn discriminant(t: &Type) -> &'static str {
    match t {
        Type::TraitObject(_) => "trait object",
        Type::Never(_) => "never",
        Type::Macro(_) => "macro",
        _ => "other",
    }
}