sway_ast/ty/
mod.rs

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
use crate::priv_prelude::*;

#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, Serialize)]
pub enum Ty {
    Path(PathType),
    Tuple(Parens<TyTupleDescriptor>),
    Array(SquareBrackets<TyArrayDescriptor>),
    StringSlice(StrToken),
    StringArray {
        str_token: StrToken,
        length: SquareBrackets<Box<Expr>>,
    },
    Infer {
        underscore_token: UnderscoreToken,
    },
    Ptr {
        ptr_token: PtrToken,
        ty: SquareBrackets<Box<Ty>>,
    },
    Slice {
        slice_token: Option<SliceToken>,
        ty: SquareBrackets<Box<Ty>>,
    },
    Ref {
        ampersand_token: AmpersandToken,
        mut_token: Option<MutToken>,
        ty: Box<Ty>,
    },
    Never {
        bang_token: BangToken,
    },
}

impl Spanned for Ty {
    fn span(&self) -> Span {
        match self {
            Ty::Path(path_type) => path_type.span(),
            Ty::Tuple(tuple_type) => tuple_type.span(),
            Ty::Array(array_type) => array_type.span(),
            Ty::StringSlice(str_token) => str_token.span(),
            Ty::StringArray { str_token, length } => Span::join(str_token.span(), &length.span()),
            Ty::Infer { underscore_token } => underscore_token.span(),
            Ty::Ptr { ptr_token, ty } => Span::join(ptr_token.span(), &ty.span()),
            Ty::Slice { slice_token, ty } => {
                let span = slice_token
                    .as_ref()
                    .map(|s| Span::join(s.span(), &ty.span()));
                span.unwrap_or_else(|| ty.span())
            }
            Ty::Ref {
                ampersand_token,
                mut_token: _,
                ty,
            } => Span::join(ampersand_token.span(), &ty.span()),
            Ty::Never { bang_token } => bang_token.span(),
        }
    }
}

impl Ty {
    pub fn name_span(&self) -> Option<Span> {
        if let Ty::Path(path_type) = self {
            Some(path_type.last_segment().name.span())
        } else {
            None
        }
    }
}

#[derive(Clone, Debug, Serialize)]
pub enum TyTupleDescriptor {
    Nil,
    Cons {
        head: Box<Ty>,
        comma_token: CommaToken,
        tail: Punctuated<Ty, CommaToken>,
    },
}

impl TyTupleDescriptor {
    pub fn to_tys(self) -> Vec<Ty> {
        match self {
            TyTupleDescriptor::Nil => vec![],
            TyTupleDescriptor::Cons { head, tail, .. } => {
                let mut tys = vec![*head];
                for ty in tail.into_iter() {
                    tys.push(ty);
                }
                tys
            }
        }
    }
}

#[derive(Clone, Debug, Serialize)]
pub struct TyArrayDescriptor {
    pub ty: Box<Ty>,
    pub semicolon_token: SemicolonToken,
    pub length: Box<Expr>,
}