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
use crate::parser::{Parse, Parser, Result};
use crate::token::{self, Span};
use crate::{annotation, kw};

/// A custom section within a wasm module.
#[derive(Debug)]
pub enum Custom<'a> {
    /// A raw custom section with the manual placement and bytes specified.
    Raw(RawCustomSection<'a>),
    /// A producers custom section.
    Producers(Producers<'a>),
    /// The `dylink.0` custom section
    Dylink0(Dylink0<'a>),
}

impl Custom<'_> {
    /// Where this custom section is placed.
    pub fn place(&self) -> CustomPlace {
        match self {
            Custom::Raw(s) => s.place,
            Custom::Producers(_) => CustomPlace::AfterLast,
            Custom::Dylink0(_) => CustomPlace::BeforeFirst,
        }
    }

    /// The name of this custom section
    pub fn name(&self) -> &str {
        match self {
            Custom::Raw(s) => s.name,
            Custom::Producers(_) => "producers",
            Custom::Dylink0(_) => "dylink.0",
        }
    }
}

impl<'a> Parse<'a> for Custom<'a> {
    fn parse(parser: Parser<'a>) -> Result<Self> {
        if parser.peek::<annotation::producers>()? {
            Ok(Custom::Producers(parser.parse()?))
        } else if parser.peek::<annotation::dylink_0>()? {
            Ok(Custom::Dylink0(parser.parse()?))
        } else {
            Ok(Custom::Raw(parser.parse()?))
        }
    }
}

/// A wasm custom section within a module.
#[derive(Debug)]
pub struct RawCustomSection<'a> {
    /// Where this `@custom` was defined.
    pub span: Span,

    /// Name of the custom section.
    pub name: &'a str,

    /// Where the custom section is being placed,
    pub place: CustomPlace,

    /// Payload of this custom section.
    pub data: Vec<&'a [u8]>,
}

/// Possible locations to place a custom section within a module.
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum CustomPlace {
    /// This custom section will appear before the first section in the module.
    BeforeFirst,
    /// This custom section will be placed just before a known section.
    Before(CustomPlaceAnchor),
    /// This custom section will be placed just after a known section.
    After(CustomPlaceAnchor),
    /// This custom section will appear after the last section in the module.
    AfterLast,
}

/// Known sections that custom sections can be placed relative to.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[allow(missing_docs)]
pub enum CustomPlaceAnchor {
    Type,
    Import,
    Func,
    Table,
    Memory,
    Global,
    Export,
    Start,
    Elem,
    Code,
    Data,
    Tag,
}

impl<'a> Parse<'a> for RawCustomSection<'a> {
    fn parse(parser: Parser<'a>) -> Result<Self> {
        let span = parser.parse::<annotation::custom>()?.0;
        let name = parser.parse()?;
        let place = if parser.peek::<token::LParen>()? {
            parser.parens(|p| p.parse())?
        } else {
            CustomPlace::AfterLast
        };
        let mut data = Vec::new();
        while !parser.is_empty() {
            data.push(parser.parse()?);
        }
        Ok(RawCustomSection {
            span,
            name,
            place,
            data,
        })
    }
}

impl<'a> Parse<'a> for CustomPlace {
    fn parse(parser: Parser<'a>) -> Result<Self> {
        let mut l = parser.lookahead1();
        let ctor = if l.peek::<kw::before>()? {
            parser.parse::<kw::before>()?;
            if l.peek::<kw::first>()? {
                parser.parse::<kw::first>()?;
                return Ok(CustomPlace::BeforeFirst);
            }
            CustomPlace::Before as fn(CustomPlaceAnchor) -> _
        } else if l.peek::<kw::after>()? {
            parser.parse::<kw::after>()?;
            if l.peek::<kw::last>()? {
                parser.parse::<kw::last>()?;
                return Ok(CustomPlace::AfterLast);
            }
            CustomPlace::After
        } else {
            return Err(l.error());
        };
        Ok(ctor(parser.parse()?))
    }
}

impl<'a> Parse<'a> for CustomPlaceAnchor {
    fn parse(parser: Parser<'a>) -> Result<Self> {
        if parser.peek::<kw::r#type>()? {
            parser.parse::<kw::r#type>()?;
            return Ok(CustomPlaceAnchor::Type);
        }
        if parser.peek::<kw::import>()? {
            parser.parse::<kw::import>()?;
            return Ok(CustomPlaceAnchor::Import);
        }
        if parser.peek::<kw::func>()? {
            parser.parse::<kw::func>()?;
            return Ok(CustomPlaceAnchor::Func);
        }
        if parser.peek::<kw::table>()? {
            parser.parse::<kw::table>()?;
            return Ok(CustomPlaceAnchor::Table);
        }
        if parser.peek::<kw::memory>()? {
            parser.parse::<kw::memory>()?;
            return Ok(CustomPlaceAnchor::Memory);
        }
        if parser.peek::<kw::global>()? {
            parser.parse::<kw::global>()?;
            return Ok(CustomPlaceAnchor::Global);
        }
        if parser.peek::<kw::export>()? {
            parser.parse::<kw::export>()?;
            return Ok(CustomPlaceAnchor::Export);
        }
        if parser.peek::<kw::start>()? {
            parser.parse::<kw::start>()?;
            return Ok(CustomPlaceAnchor::Start);
        }
        if parser.peek::<kw::elem>()? {
            parser.parse::<kw::elem>()?;
            return Ok(CustomPlaceAnchor::Elem);
        }
        if parser.peek::<kw::code>()? {
            parser.parse::<kw::code>()?;
            return Ok(CustomPlaceAnchor::Code);
        }
        if parser.peek::<kw::data>()? {
            parser.parse::<kw::data>()?;
            return Ok(CustomPlaceAnchor::Data);
        }
        if parser.peek::<kw::tag>()? {
            parser.parse::<kw::tag>()?;
            return Ok(CustomPlaceAnchor::Tag);
        }

        Err(parser.error("expected a valid section name"))
    }
}

/// A producers custom section
#[allow(missing_docs)]
#[derive(Debug)]
pub struct Producers<'a> {
    pub fields: Vec<(&'a str, Vec<(&'a str, &'a str)>)>,
}

impl<'a> Parse<'a> for Producers<'a> {
    fn parse(parser: Parser<'a>) -> Result<Self> {
        parser.parse::<annotation::producers>()?.0;
        let mut languages = Vec::new();
        let mut sdks = Vec::new();
        let mut processed_by = Vec::new();
        while !parser.is_empty() {
            parser.parens(|parser| {
                let mut l = parser.lookahead1();
                let dst = if l.peek::<kw::language>()? {
                    parser.parse::<kw::language>()?;
                    &mut languages
                } else if l.peek::<kw::sdk>()? {
                    parser.parse::<kw::sdk>()?;
                    &mut sdks
                } else if l.peek::<kw::processed_by>()? {
                    parser.parse::<kw::processed_by>()?;
                    &mut processed_by
                } else {
                    return Err(l.error());
                };

                dst.push((parser.parse()?, parser.parse()?));
                Ok(())
            })?;
        }

        let mut fields = Vec::new();
        if !languages.is_empty() {
            fields.push(("language", languages));
        }
        if !sdks.is_empty() {
            fields.push(("sdk", sdks));
        }
        if !processed_by.is_empty() {
            fields.push(("processed-by", processed_by));
        }
        Ok(Producers { fields })
    }
}

/// A `dylink.0` custom section
#[allow(missing_docs)]
#[derive(Debug)]
pub struct Dylink0<'a> {
    pub subsections: Vec<Dylink0Subsection<'a>>,
}

/// Possible subsections of the `dylink.0` custom section
#[derive(Debug)]
#[allow(missing_docs)]
pub enum Dylink0Subsection<'a> {
    MemInfo {
        memory_size: u32,
        memory_align: u32,
        table_size: u32,
        table_align: u32,
    },
    Needed(Vec<&'a str>),
    ExportInfo(Vec<(&'a str, u32)>),
    ImportInfo(Vec<(&'a str, &'a str, u32)>),
}

impl<'a> Parse<'a> for Dylink0<'a> {
    fn parse(parser: Parser<'a>) -> Result<Self> {
        parser.parse::<annotation::dylink_0>()?.0;
        let mut ret = Dylink0 {
            subsections: Vec::new(),
        };
        while !parser.is_empty() {
            parser.parens(|p| ret.parse_next(p))?;
        }
        Ok(ret)
    }
}

impl<'a> Dylink0<'a> {
    fn parse_next(&mut self, parser: Parser<'a>) -> Result<()> {
        let mut l = parser.lookahead1();
        if l.peek::<kw::mem_info>()? {
            parser.parse::<kw::mem_info>()?;
            let mut memory_size = 0;
            let mut memory_align = 0;
            let mut table_size = 0;
            let mut table_align = 0;
            if parser.peek2::<kw::memory>()? {
                parser.parens(|p| {
                    p.parse::<kw::memory>()?;
                    memory_size = p.parse()?;
                    memory_align = p.parse()?;
                    Ok(())
                })?;
            }
            if parser.peek2::<kw::table>()? {
                parser.parens(|p| {
                    p.parse::<kw::table>()?;
                    table_size = p.parse()?;
                    table_align = p.parse()?;
                    Ok(())
                })?;
            }
            self.subsections.push(Dylink0Subsection::MemInfo {
                memory_size,
                memory_align,
                table_size,
                table_align,
            });
        } else if l.peek::<kw::needed>()? {
            parser.parse::<kw::needed>()?;
            let mut names = Vec::new();
            while !parser.is_empty() {
                names.push(parser.parse()?);
            }
            self.subsections.push(Dylink0Subsection::Needed(names));
        } else if l.peek::<kw::export_info>()? {
            parser.parse::<kw::export_info>()?;
            let name = parser.parse()?;
            let flags = parse_sym_flags(parser)?;
            match self.subsections.last_mut() {
                Some(Dylink0Subsection::ExportInfo(list)) => list.push((name, flags)),
                _ => self
                    .subsections
                    .push(Dylink0Subsection::ExportInfo(vec![(name, flags)])),
            }
        } else if l.peek::<kw::import_info>()? {
            parser.parse::<kw::import_info>()?;
            let module = parser.parse()?;
            let name = parser.parse()?;
            let flags = parse_sym_flags(parser)?;
            match self.subsections.last_mut() {
                Some(Dylink0Subsection::ImportInfo(list)) => list.push((module, name, flags)),
                _ => self
                    .subsections
                    .push(Dylink0Subsection::ImportInfo(vec![(module, name, flags)])),
            }
        } else {
            return Err(l.error());
        }
        Ok(())
    }
}

fn parse_sym_flags(parser: Parser<'_>) -> Result<u32> {
    let mut flags = 0;
    while !parser.is_empty() {
        let mut l = parser.lookahead1();
        if l.peek::<u32>()? {
            flags |= parser.parse::<u32>()?;
            continue;
        }
        macro_rules! parse_flags {
            ($($kw:tt = $val:expr,)*) => {$({
                custom_keyword!(flag = $kw);
                if l.peek::<flag>()? {
                    parser.parse::<flag>()?;
                    flags |= $val;
                    continue;
                }
            })*};
        }
        // N.B.: Keep in sync with `print_dylink0_flags` in `crates/wasmprinter/src/lib.rs`.
        parse_flags! {
            "binding-weak" = 1 << 0,
            "binding-local" = 1 << 1,
            "visibility-hidden" = 1 << 2,
            "undefined" = 1 << 4,
            "exported" = 1 << 5,
            "explicit-name" = 1 << 6,
            "no-strip" = 1 << 7,
            "tls" = 1 << 8,
            "absolute" = 1 << 9,
        }
        return Err(l.error());
    }
    Ok(flags)
}

mod flag {}

impl Dylink0Subsection<'_> {
    /// Returns the byte id of this subsection used to identify it.
    pub fn id(&self) -> u8 {
        use Dylink0Subsection::*;
        match self {
            MemInfo { .. } => 1,
            Needed(..) => 2,
            ExportInfo(..) => 3,
            ImportInfo(..) => 4,
        }
    }
}