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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
use heck::*;
use std::io::{Read, Write};
use std::path::Path;
use std::process::{Command, Stdio};
use witx::*;

pub fn generate<P: AsRef<Path>>(witx_paths: &[P]) -> String {
    let doc = witx::load(witx_paths).unwrap();

    let mut raw = String::new();
    raw.push_str(
        "\
// This file is automatically generated, DO NOT EDIT
//
// To regenerate this file run the `crates/witx-bindgen` command

use core::mem::MaybeUninit;

pub use crate::error::Error;
pub type Result<T, E = Error> = core::result::Result<T, E>;
",
    );
    for ty in doc.typenames() {
        ty.render(&mut raw);
        raw.push_str("\n");
    }
    for m in doc.modules() {
        m.render(&mut raw);
        raw.push_str("\n");
    }

    let mut rustfmt = Command::new("rustfmt")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();
    rustfmt
        .stdin
        .take()
        .unwrap()
        .write_all(raw.as_bytes())
        .unwrap();
    let mut ret = String::new();
    rustfmt
        .stdout
        .take()
        .unwrap()
        .read_to_string(&mut ret)
        .unwrap();
    let status = rustfmt.wait().unwrap();
    assert!(status.success());
    return ret;
}

trait Render {
    fn render(&self, src: &mut String);
}

impl Render for NamedType {
    fn render(&self, src: &mut String) {
        let name = self.name.as_str();
        match &self.tref {
            TypeRef::Value(ty) => match &**ty {
                Type::Enum(e) => render_enum(src, name, e),
                Type::Flags(f) => render_flags(src, name, f),
                Type::Int(c) => render_const(src, name, c),
                Type::Struct(s) => render_struct(src, name, s),
                Type::Union(u) => render_union(src, name, u),
                Type::Handle(h) => render_handle(src, name, h),
                Type::Array { .. }
                | Type::Pointer { .. }
                | Type::ConstPointer { .. }
                | Type::Builtin { .. } => render_alias(src, name, &self.tref),
            },
            TypeRef::Name(_nt) => render_alias(src, name, &self.tref),
        }
    }
}

// TODO verify this is correct way of handling IntDatatype
fn render_const(src: &mut String, name: &str, c: &IntDatatype) {
    src.push_str(&format!("pub type {} = ", name.to_camel_case()));
    c.repr.render(src);
    src.push_str(";\n");
    for r#const in c.consts.iter() {
        rustdoc(&r#const.docs, src);
        src.push_str(&format!(
            "pub const {}_{}: {} = {};",
            name.to_shouty_snake_case(),
            r#const.name.as_str().to_shouty_snake_case(),
            name.to_camel_case(),
            r#const.value
        ));
    }
}

fn render_union(src: &mut String, name: &str, u: &UnionDatatype) {
    src.push_str("#[repr(C)]\n");
    src.push_str("#[derive(Copy, Clone)]\n");
    src.push_str(&format!("pub union {}U {{\n", name.to_camel_case()));
    for variant in u.variants.iter() {
        if let Some(ref tref) = variant.tref {
            rustdoc(&variant.docs, src);
            src.push_str("pub ");
            variant.name.render(src);
            src.push_str(": ");
            tref.render(src);
            src.push_str(",\n");
        }
    }
    src.push_str("}\n");
    src.push_str("#[repr(C)]\n");
    src.push_str("#[derive(Copy, Clone)]\n");
    src.push_str(&format!("pub struct {} {{\n", name.to_camel_case()));
    src.push_str(&format!(
        "pub tag: {},\n",
        u.tag.name.as_str().to_camel_case()
    ));
    src.push_str(&format!("pub u: {}U,\n", name.to_camel_case()));
    src.push_str("}\n");
}

fn render_struct(src: &mut String, name: &str, s: &StructDatatype) {
    src.push_str("#[repr(C)]\n");
    if struct_contains_union(s) {
        // Unions can't automatically derive `Debug`.
        src.push_str("#[derive(Copy, Clone)]\n");
    } else {
        src.push_str("#[derive(Copy, Clone, Debug)]\n");
    }
    src.push_str(&format!("pub struct {} {{\n", name.to_camel_case()));
    for member in s.members.iter() {
        rustdoc(&member.docs, src);
        src.push_str("pub ");
        member.name.render(src);
        src.push_str(": ");
        member.tref.render(src);
        src.push_str(",\n");
    }
    src.push_str("}");
}

fn render_flags(src: &mut String, name: &str, f: &FlagsDatatype) {
    src.push_str(&format!("pub type {} = ", name.to_camel_case()));
    f.repr.render(src);
    src.push_str(";\n");
    for (i, variant) in f.flags.iter().enumerate() {
        rustdoc(&variant.docs, src);
        src.push_str(&format!(
            "pub const {}_{}: {} = 0x{:x};",
            name.to_shouty_snake_case(),
            variant.name.as_str().to_shouty_snake_case(),
            name.to_camel_case(),
            1 << i
        ));
    }
}

fn render_enum(src: &mut String, name: &str, e: &EnumDatatype) {
    src.push_str(&format!("pub type {} = ", name.to_camel_case()));
    e.repr.render(src);
    src.push_str(";\n");
    for (i, variant) in e.variants.iter().enumerate() {
        rustdoc(&variant.docs, src);
        src.push_str(&format!(
            "pub const {}_{}: {} = {};",
            name.to_shouty_snake_case(),
            variant.name.as_str().to_shouty_snake_case(),
            name.to_camel_case(),
            i
        ));
    }

    if name == "errno" {
        src.push_str("pub(crate) fn strerror(code: u16) -> &'static str {");
        src.push_str("match code {");
        for variant in e.variants.iter() {
            src.push_str(&name.to_shouty_snake_case());
            src.push_str("_");
            src.push_str(&variant.name.as_str().to_shouty_snake_case());
            src.push_str(" => \"");
            src.push_str(variant.docs.trim());
            src.push_str("\",");
        }
        src.push_str("_ => \"Unknown error.\",");
        src.push_str("}");
        src.push_str("}");
    }
}

impl Render for IntRepr {
    fn render(&self, src: &mut String) {
        match self {
            IntRepr::U8 => src.push_str("u8"),
            IntRepr::U16 => src.push_str("u16"),
            IntRepr::U32 => src.push_str("u32"),
            IntRepr::U64 => src.push_str("u64"),
        }
    }
}

fn render_alias(src: &mut String, name: &str, dest: &TypeRef) {
    src.push_str(&format!("pub type {}", name.to_camel_case()));
    if dest.type_().passed_by() == TypePassedBy::PointerLengthPair {
        src.push_str("<'a>");
    }
    src.push_str(" = ");

    // Give `size` special treatment to translate it to `usize` in Rust instead of `u32`. Makes
    // things a bit nicer for client libraries. We can remove this hack once WASI moves to a
    // snapshot that uses BuiltinType::Size.
    if name == "size" {
        src.push_str("usize");
    } else {
        dest.render(src);
    }
    src.push(';');
}

impl Render for TypeRef {
    fn render(&self, src: &mut String) {
        match self {
            TypeRef::Name(t) => {
                src.push_str(&t.name.as_str().to_camel_case());
                if t.type_().passed_by() == TypePassedBy::PointerLengthPair {
                    src.push_str("<'_>");
                }
            }
            TypeRef::Value(v) => match &**v {
                Type::Builtin(t) => t.render(src),
                Type::Array(t) => {
                    src.push_str("&'a [");
                    t.render(src);
                    src.push_str("]");
                }
                Type::Pointer(t) => {
                    src.push_str("*mut ");
                    t.render(src);
                }
                Type::ConstPointer(t) => {
                    src.push_str("*const ");
                    t.render(src);
                }
                t => panic!("reference to anonymous {} not possible!", t.kind()),
            },
        }
    }
}

impl Render for BuiltinType {
    fn render(&self, src: &mut String) {
        match self {
            BuiltinType::String => src.push_str("&str"),
            BuiltinType::U8 => src.push_str("u8"),
            BuiltinType::U16 => src.push_str("u16"),
            BuiltinType::U32 => src.push_str("u32"),
            BuiltinType::U64 => src.push_str("u64"),
            BuiltinType::S8 => src.push_str("i8"),
            BuiltinType::S16 => src.push_str("i16"),
            BuiltinType::S32 => src.push_str("i32"),
            BuiltinType::S64 => src.push_str("i64"),
            BuiltinType::F32 => src.push_str("f32"),
            BuiltinType::F64 => src.push_str("f64"),
            BuiltinType::USize => src.push_str("usize"),
            BuiltinType::Char8 => {
                // Char8 represents a UTF8 code *unit* (`u8` in Rust, `char8_t` in C++20)
                // rather than a code *point* (`char` in Rust which is multi-byte)
                src.push_str("u8")
            }
        }
    }
}

impl Render for Module {
    fn render(&self, src: &mut String) {
        let rust_name = self.name.as_str().to_snake_case();
        // wrapper functions
        for f in self.funcs() {
            render_highlevel(&f, &rust_name, src);
            src.push_str("\n\n");
        }

        // raw module
        src.push_str("pub mod ");
        src.push_str(&rust_name);
        src.push_str("{\nuse super::*;");
        src.push_str("#[link(wasm_import_module =\"");
        src.push_str(self.name.as_str());
        src.push_str("\")]\n");
        src.push_str("extern \"C\" {\n");
        for f in self.funcs() {
            f.render(src);
            src.push_str("\n");
        }
        src.push_str("}");
        src.push_str("}");
    }
}

fn render_highlevel(func: &InterfaceFunc, module: &str, src: &mut String) {
    let mut rust_name = String::new();
    func.name.render(&mut rust_name);
    let rust_name = rust_name.to_snake_case();
    rustdoc(&func.docs, src);
    rustdoc_params(&func.params, "Parameters", src);
    rustdoc_params(&func.results, "Return", src);

    // Render the function and its arguments, and note that the arguments here
    // are the exact type name arguments as opposed to the pointer/length pair
    // ones. These functions are unsafe because they work with integer file
    // descriptors, which are effectively forgeable and danglable raw pointers
    // into the file descriptor address space.
    src.push_str("pub unsafe fn ");

    // TODO workout how to handle wasi-ephemeral which introduces multiple
    // WASI modules into the picture. For now, feature-gate it, and if we're
    // compiling ephmeral bindings, prefix wrapper syscall with module name.
    cfg_if::cfg_if! {
        if #[cfg(feature = "multi-module")] {
            src.push_str(&[module, &rust_name].join("_"));
        } else {
            src.push_str(&rust_name);
        }
    }

    src.push_str("(");
    for param in func.params.iter() {
        param.name.render(src);
        src.push_str(": ");
        param.tref.render(src);
        src.push_str(",");
    }
    src.push_str(")");

    // Render the result type of this function, if there is one.
    if let Some(first) = func.results.get(0) {
        // only know how to generate bindings for arguments where the first
        // results is an errno, so assert this here and if it ever changes we'll
        // need to update codegen below.
        assert_eq!(first.name.as_str(), "error");
        src.push_str(" -> Result<");
        // 1 == `Result<()>`, 2 == `Result<T>`, 3+ == `Result<(...)>`
        if func.results.len() != 2 {
            src.push_str("(");
        }
        for result in func.results.iter().skip(1) {
            result.tref.render(src);
            src.push_str(",");
        }
        if func.results.len() != 2 {
            src.push_str(")");
        }
        src.push_str(">");
    }

    src.push_str("{");
    for result in func.results.iter().skip(1) {
        src.push_str("let mut ");
        result.name.render(src);
        src.push_str(" = MaybeUninit::uninit();");
    }
    if func.results.len() > 0 {
        src.push_str("let rc = ");
    }
    src.push_str(module);
    src.push_str("::");
    src.push_str(&rust_name);
    src.push_str("(");

    // Forward all parameters, fetching the pointer/length as appropriate
    for param in func.params.iter() {
        match param.tref.type_().passed_by() {
            TypePassedBy::Value(_) => param.name.render(src),
            TypePassedBy::Pointer => unreachable!(
                "unable to translate parameter `{}` of type `{}` in function `{}`",
                param.name.as_str(),
                param.tref.type_name(),
                func.name.as_str()
            ),
            TypePassedBy::PointerLengthPair => {
                param.name.render(src);
                src.push_str(".as_ptr(), ");
                param.name.render(src);
                src.push_str(".len()");
            }
        }
        src.push_str(",");
    }

    // Forward all out-pointers as trailing arguments
    for result in func.results.iter().skip(1) {
        result.name.render(src);
        src.push_str(".as_mut_ptr(),");
    }
    src.push_str(");");

    // Check the return value, and if successful load all of the out pointers
    // assuming they were initialized (part of the wasi contract).
    if func.results.len() > 0 {
        src.push_str("if let Some(err) = Error::from_raw_error(rc) { ");
        src.push_str("Err(err)");
        src.push_str("} else {");
        src.push_str("Ok(");
        if func.results.len() != 2 {
            src.push_str("(");
        }
        for result in func.results.iter().skip(1) {
            result.name.render(src);
            src.push_str(".assume_init(),");
        }
        if func.results.len() != 2 {
            src.push_str(")");
        }
        src.push_str(") }");
    }
    src.push_str("}");
}

impl Render for InterfaceFunc {
    fn render(&self, src: &mut String) {
        rustdoc(&self.docs, src);
        if self.name.as_str() != self.name.as_str().to_snake_case() {
            src.push_str("#[link_name = \"");
            src.push_str(self.name.as_str());
            src.push_str("\"]\n");
        }
        src.push_str("pub fn ");
        let mut name = String::new();
        self.name.render(&mut name);
        src.push_str(&name.to_snake_case());
        src.push_str("(");
        for param in self.params.iter() {
            param.render(src);
            src.push_str(",");
        }
        for result in self.results.iter().skip(1) {
            result.name.render(src);
            src.push_str(": *mut ");
            result.tref.render(src);
            src.push_str(",");
        }
        src.push_str(")");
        if let Some(result) = self.results.get(0) {
            src.push_str(" -> ");
            result.render(src);
        // special-case the `proc_exit` function for now to be "noreturn", and
        // eventually we'll have an attribute in `*.witx` to specify this as
        // well.
        } else if self.name.as_str() == "proc_exit" {
            src.push_str(" -> !");
        }
        src.push_str(";");
    }
}

impl Render for InterfaceFuncParam {
    fn render(&self, src: &mut String) {
        let is_param = match self.position {
            InterfaceFuncParamPosition::Param(_) => true,
            _ => false,
        };
        match self.tref.type_().passed_by() {
            // By-value arguments are passed as-is
            TypePassedBy::Value(_) => {
                if is_param {
                    self.name.render(src);
                    src.push_str(": ");
                }
                self.tref.render(src);
            }
            // Pointer arguments are passed with a `*mut` out in front
            TypePassedBy::Pointer => {
                if is_param {
                    self.name.render(src);
                    src.push_str(": ");
                }
                src.push_str("*mut ");
                self.tref.render(src);
            }
            // ... and pointer/length arguments are passed with first their
            // pointer and then their length, as the name would otherwise imply
            TypePassedBy::PointerLengthPair => {
                assert!(is_param);
                src.push_str(self.name.as_str());
                src.push_str("_ptr");
                src.push_str(": ");
                src.push_str("*const ");
                match &*self.tref.type_() {
                    Type::Array(x) => x.render(src),
                    Type::Builtin(BuiltinType::String) => src.push_str("u8"),
                    x => panic!("unexpected pointer length pair type {:?}", x),
                }
                src.push_str(", ");
                src.push_str(self.name.as_str());
                src.push_str("_len");
                src.push_str(": ");
                src.push_str("usize");
            }
        }
    }
}

impl Render for Id {
    fn render(&self, src: &mut String) {
        match self.as_str() {
            "in" => src.push_str("r#in"),
            "type" => src.push_str("r#type"),
            "yield" => src.push_str("r#yield"),
            s => src.push_str(s),
        }
    }
}

fn render_handle(src: &mut String, name: &str, _h: &HandleDatatype) {
    src.push_str(&format!("pub type {} = u32;", name.to_camel_case()));
}

fn rustdoc(docs: &str, dst: &mut String) {
    if docs.trim().is_empty() {
        return;
    }
    for line in docs.lines() {
        dst.push_str("/// ");
        dst.push_str(line);
        dst.push_str("\n");
    }
}

fn rustdoc_params(docs: &[InterfaceFuncParam], header: &str, dst: &mut String) {
    let docs = docs
        .iter()
        .filter(|param| param.docs.trim().len() > 0)
        .collect::<Vec<_>>();
    if docs.len() == 0 {
        return;
    }

    dst.push_str("///\n");
    dst.push_str("/// ## ");
    dst.push_str(header);
    dst.push_str("\n");
    dst.push_str("///\n");

    for param in docs {
        for (i, line) in param.docs.lines().enumerate() {
            dst.push_str("/// ");
            if i == 0 {
                dst.push_str("* `");
                param.name.render(dst);
                dst.push_str("` - ");
            } else {
                dst.push_str("  ");
            }
            dst.push_str(line);
            dst.push_str("\n");
        }
    }
}

fn struct_contains_union(s: &StructDatatype) -> bool {
    s.members
        .iter()
        .any(|member| type_contains_union(&member.tref.type_()))
}

fn type_contains_union(ty: &Type) -> bool {
    match ty {
        Type::Union(_) => true,
        Type::Array(tref) => type_contains_union(&tref.type_()),
        Type::Struct(st) => struct_contains_union(st),
        _ => false,
    }
}