wright/ast/
ty.rs

1//! AST models for type signatures in wright source.
2
3use crate::source_tracking::fragment::Fragment;
4
5/// A type signature in source code.
6#[derive(Debug, Clone)]
7#[allow(missing_docs)]
8pub enum Type {
9    Atomic(AtomicTy),
10    Reference(ReferenceTy),
11}
12
13impl Type {
14    /// Get the matching source for this type signature in source code.
15    pub fn matching_source(&self) -> &Fragment {
16        match self {
17            Type::Atomic(atomic_ty) => &atomic_ty.matching_source,
18            Type::Reference(reference_ty) => &reference_ty.matching_source,
19        }
20    }
21
22    /// Attempt to "downcast" this to an atomic type signature if it is one.
23    pub fn downcast_primitive(&self) -> Option<&AtomicTy> {
24        match self {
25            Type::Atomic(atomic) => Some(atomic),
26            _ => None,
27        }
28    }
29
30    /// Attempt to "downcast" this to a reference type signature if it is one.
31    pub fn downcast_reference(&self) -> Option<&ReferenceTy> {
32        match self {
33            Type::Reference(reference) => Some(reference),
34            _ => None,
35        }
36    }
37}
38
39/// The atomic types of wright -- primitive numeric types, boolean, char, etc.
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41#[allow(missing_docs)]
42pub enum AtomicTyVariant {
43    Bool,
44    U8,
45    I8,
46    U16,
47    I16,
48    U32,
49    I32,
50    U64,
51    I64,
52    F32,
53    F64,
54    Char,
55}
56
57/// An atomic type signature in wright source code.
58#[derive(Clone, Debug)]
59#[allow(missing_docs)]
60pub struct AtomicTy {
61    pub variant: AtomicTyVariant,
62    pub matching_source: Fragment,
63}
64
65/// Source code for a reference type signature, such as `@u64`.
66#[derive(Debug, Clone)]
67pub struct ReferenceTy {
68    /// The source code of the target type.
69    pub target_ty: Box<Type>,
70    /// The fragment of the whole reference.
71    pub matching_source: Fragment,
72}