summaryrefslogtreecommitdiffstats
path: root/third_party/rust/wast/src/ast/alias.rs
blob: 9bcb4969fbacdd4afc0a6c665fb07aced26c21e5 (plain)
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
use crate::ast::{self, kw};
use crate::parser::{Parse, Parser, Result};

/// An `alias` statement used to juggle indices with nested modules.
#[derive(Debug)]
pub struct Alias<'a> {
    /// Where this `alias` was defined.
    pub span: ast::Span,
    /// An identifier that this alias is resolved with (optionally) for name
    /// resolution.
    pub id: Option<ast::Id<'a>>,
    /// An optional name for this alias stored in the custom `name` section.
    pub name: Option<ast::NameAnnotation<'a>>,
    /// The item in the parent instance that we're aliasing.
    pub kind: AliasKind<'a>,
}

#[derive(Debug)]
#[allow(missing_docs)]
pub enum AliasKind<'a> {
    InstanceExport {
        instance: ast::ItemRef<'a, kw::instance>,
        export: &'a str,
        kind: ast::ExportKind,
    },
    Outer {
        /// The index of the module that this reference is referring to.
        module: ast::Index<'a>,
        /// The index of the item within `module` that this alias is referering
        /// to.
        index: ast::Index<'a>,
        /// The kind of item that's being aliased.
        kind: ast::ExportKind,
    },
}

impl Alias<'_> {
    /// Returns the kind of item defined by this alias.
    pub fn item_kind(&self) -> ast::ExportKind {
        match self.kind {
            AliasKind::InstanceExport { kind, .. } => kind,
            AliasKind::Outer { kind, .. } => kind,
        }
    }
}

impl<'a> Parse<'a> for Alias<'a> {
    fn parse(parser: Parser<'a>) -> Result<Self> {
        let span = parser.parse::<kw::alias>()?.0;
        let id = parser.parse()?;
        let name = parser.parse()?;
        let kind = parser.parens(|p| {
            let kind = p.parse()?;
            Ok(if parser.parse::<Option<kw::outer>>()?.is_some() {
                AliasKind::Outer {
                    module: parser.parse()?,
                    index: parser.parse()?,
                    kind,
                }
            } else {
                AliasKind::InstanceExport {
                    instance: parser.parse::<ast::IndexOrRef<_>>()?.0,
                    export: parser.parse()?,
                    kind,
                }
            })
        })?;

        Ok(Alias {
            span,
            id,
            name,
            kind,
        })
    }
}