1use super::{AstNode, expr::Expr};
4use crate::{ast::path::Path, source_tracking::fragment::Fragment};
5
6#[derive(Debug, Clone, PartialEq)]
8pub enum Type {
9 Atomic(AtomicTy),
11 Reference(ReferenceTy),
13 Named(NamedTy),
15 Array(ArrayTy),
17 Function(FunctionTy),
19 Error(Fragment),
23}
24
25impl Type {
26 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 pub fn downcast_primitive(&self) -> Option<&AtomicTy> {
40 match self {
41 Type::Atomic(atomic) => Some(atomic),
42 _ => None,
43 }
44 }
45
46 pub fn downcast_reference(&self) -> Option<&ReferenceTy> {
48 match self {
49 Type::Reference(reference) => Some(reference),
50 _ => None,
51 }
52 }
53
54 pub fn downcast_named(&self) -> Option<&NamedTy> {
56 match self {
57 Type::Named(named) => Some(named),
58 _ => None,
59 }
60 }
61
62 pub fn downcast_array(&self) -> Option<&ArrayTy> {
64 match self {
65 Type::Array(array) => Some(array),
66 _ => None,
67 }
68 }
69
70 pub fn downcast_function(&self) -> Option<&FunctionTy> {
72 match self {
73 Type::Function(function) => Some(function),
74 _ => None,
75 }
76 }
77
78 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#[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 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#[derive(Debug, Clone, PartialEq)]
132#[allow(missing_docs)]
133pub struct AtomicTy {
134 pub variant: AtomicTyVariant,
135 pub matching_source: Fragment,
136}
137
138#[derive(Debug, Clone, PartialEq)]
140pub struct ReferenceTy {
141 pub target_ty: Box<Type>,
143 pub matching_source: Fragment,
145}
146
147#[derive(Debug, Clone, PartialEq)]
151pub struct NamedTy {
152 pub matching_source: Fragment,
154
155 pub name: Path,
157
158 pub generic_tys: Vec<Type>,
160}
161
162#[derive(Debug, Clone, PartialEq)]
164pub struct ArrayTy {
165 pub matching_source: Fragment,
167
168 pub element_ty: Box<Type>,
170
171 pub length: Option<Box<Expr>>,
173}
174
175#[derive(Debug, Clone, PartialEq)]
177pub struct FunctionTy {
178 pub matching_source: Fragment,
180
181 pub param_tys: Vec<Type>,
183
184 pub return_ty: Option<Box<Type>>,
186}