Skip to main content

wright/ast/
ty.rs

1//! AST models for type signatures in wright source.
2
3use super::{AstNode, expr::Expr};
4use crate::{ast::path::Path, source_tracking::fragment::Fragment};
5
6/// A type signature in source code.
7#[derive(Debug, Clone, PartialEq)]
8pub enum Type {
9    /// A primitive type such as `u8` or `bool`.
10    Atomic(AtomicTy),
11    /// A reference to another type, such as `@u8`.
12    Reference(ReferenceTy),
13    /// A type referred to by name, such as `String` or `Vec<u8>`.
14    Named(NamedTy),
15    /// An array or slice type, such as `[u8; 4]` or `[u8]`.
16    Array(ArrayTy),
17    /// A function type, such as `func(u8, u8) -> bool`.
18    Function(FunctionTy),
19    /// A type signature that could not be parsed. The parser will have emitted an error for it.
20    ///
21    /// The fragment is where the type was expected (it may be empty).
22    Error(Fragment),
23}
24
25impl Type {
26    /// Get the matching source for this type signature in source code.
27    pub fn matching_source(&self) -> &Fragment {
28        match self {
29            Type::Atomic(atomic_ty) => &atomic_ty.matching_source,
30            Type::Reference(reference_ty) => &reference_ty.matching_source,
31            Type::Named(named_ty) => &named_ty.matching_source,
32            Type::Array(array_ty) => &array_ty.matching_source,
33            Type::Function(function_ty) => &function_ty.matching_source,
34            Type::Error(fragment) => fragment,
35        }
36    }
37
38    /// Attempt to "downcast" this to an atomic type signature if it is one.
39    pub fn downcast_primitive(&self) -> Option<&AtomicTy> {
40        match self {
41            Type::Atomic(atomic) => Some(atomic),
42            _ => None,
43        }
44    }
45
46    /// Attempt to "downcast" this to a reference type signature if it is one.
47    pub fn downcast_reference(&self) -> Option<&ReferenceTy> {
48        match self {
49            Type::Reference(reference) => Some(reference),
50            _ => None,
51        }
52    }
53
54    /// Attempt to "downcast" this to a named type signature if it is one.
55    pub fn downcast_named(&self) -> Option<&NamedTy> {
56        match self {
57            Type::Named(named) => Some(named),
58            _ => None,
59        }
60    }
61
62    /// Attempt to "downcast" this to an array/slice type signature if it is one.
63    pub fn downcast_array(&self) -> Option<&ArrayTy> {
64        match self {
65            Type::Array(array) => Some(array),
66            _ => None,
67        }
68    }
69
70    /// Attempt to "downcast" this to a function type signature if it is one.
71    pub fn downcast_function(&self) -> Option<&FunctionTy> {
72        match self {
73            Type::Function(function) => Some(function),
74            _ => None,
75        }
76    }
77
78    /// Is this an [error](Type::Error) type?
79    pub fn is_error(&self) -> bool {
80        matches!(self, Type::Error(_))
81    }
82}
83
84impl AstNode for Type {
85    fn matching_source(&self) -> &Fragment {
86        Type::matching_source(self)
87    }
88}
89
90/// The atomic types of wright -- primitive numeric types, boolean, char, etc.
91#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
92#[allow(missing_docs)]
93pub enum AtomicTyVariant {
94    Bool,
95    U8,
96    I8,
97    U16,
98    I16,
99    U32,
100    I32,
101    U64,
102    I64,
103    F32,
104    F64,
105    Char,
106}
107
108impl AtomicTyVariant {
109    /// Get the keyword used for this type in source code.
110    pub const fn keyword(self) -> &'static str {
111        use AtomicTyVariant::*;
112
113        match self {
114            Bool => "bool",
115            U8 => "u8",
116            I8 => "i8",
117            U16 => "u16",
118            I16 => "i16",
119            U32 => "u32",
120            I32 => "i32",
121            U64 => "u64",
122            I64 => "i64",
123            F32 => "f32",
124            F64 => "f64",
125            Char => "char",
126        }
127    }
128}
129
130/// An atomic type signature in wright source code.
131#[derive(Debug, Clone, PartialEq)]
132#[allow(missing_docs)]
133pub struct AtomicTy {
134    pub variant: AtomicTyVariant,
135    pub matching_source: Fragment,
136}
137
138/// Source code for a reference type signature, such as `@u64`.
139#[derive(Debug, Clone, PartialEq)]
140pub struct ReferenceTy {
141    /// The source code of the target type.
142    pub target_ty: Box<Type>,
143    /// The fragment of the whole reference.
144    pub matching_source: Fragment,
145}
146
147/// A named type (such as a reference to a type from the standard libarary or a user defined struct).
148///
149/// i.e. `MyType<Generics>`
150#[derive(Debug, Clone, PartialEq)]
151pub struct NamedTy {
152    /// The matching source code of the type signature.
153    pub matching_source: Fragment,
154
155    /// The name/identifier of the type.
156    pub name: Path,
157
158    /// An ordered series of generic type parameters passed to it.
159    pub generic_tys: Vec<Type>,
160}
161
162/// An array type with a known length (`[T; N]`) or a slice type with an unknown length (`[T]`).
163#[derive(Debug, Clone, PartialEq)]
164pub struct ArrayTy {
165    /// The matching source code of the type signature.
166    pub matching_source: Fragment,
167
168    /// The type of the elements of the array.
169    pub element_ty: Box<Type>,
170
171    /// The length of the array, if specified. Slice types have no length.
172    pub length: Option<Box<Expr>>,
173}
174
175/// A function type signature, such as `func(u8, u8) -> bool`.
176#[derive(Debug, Clone, PartialEq)]
177pub struct FunctionTy {
178    /// The matching source code of the type signature.
179    pub matching_source: Fragment,
180
181    /// The types of the function's parameters, in order.
182    pub param_tys: Vec<Type>,
183
184    /// The return type of the function, if it has one.
185    pub return_ty: Option<Box<Type>>,
186}