1#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
2pub struct TypeDefId(pub(crate) usize);
3
4#[derive(Clone, Debug, Eq, PartialEq, Hash)]
5pub struct Struct {
6 pub fields: Vec<NamedField>,
7}
8
9#[derive(Clone, Debug, Eq, PartialEq, Hash)]
10pub struct NamedField {
11 pub name: String,
12 pub ty: Type,
13}
14
15#[derive(Clone, Debug, Eq, PartialEq, Hash)]
16pub struct Enum {
17 pub variants: Vec<(String, EnumVariant)>,
18}
19
20#[derive(Clone, Debug, Eq, PartialEq, Hash)]
21pub enum EnumVariant {
22 Empty,
23 NamedFields { fields: Vec<NamedField> },
24}
25
26#[derive(Clone, Debug, Eq, PartialEq, Hash)]
27pub enum PrimitiveType {
28 Void,
29 U8,
30 U16,
31 U32,
32 U64,
33 U128,
34 I8,
35 I16,
36 I32,
37 I64,
38 I128,
39 Bool,
40 F32,
41 String,
42 Box(Box<Type>),
43 List(Box<Type>),
44 Option(Box<Type>),
45 Result(Box<Type>, Box<Type>),
46}
47
48#[derive(Clone, Debug, Eq, PartialEq, Hash)]
49pub enum Type {
50 Primitive(PrimitiveType),
51 Defined {
52 ident: QualifiedIdentifier,
53 args: Vec<Type>,
54 },
55}
56
57impl Type {
58 pub fn local(name: impl Into<String>) -> Self {
59 Self::Defined {
60 ident: QualifiedIdentifier::local(name),
61 args: Vec::new(),
62 }
63 }
64}
65
66#[derive(Clone, Debug, Eq, PartialEq, Hash)]
67pub enum TypeBody {
68 Struct(Struct),
69 Enum(Enum),
70}
71
72impl TypeBody {
73 pub fn is_struct(&self) -> bool {
74 if let Self::Struct(_) = self {
75 true
76 } else {
77 false
78 }
79 }
80
81 pub fn is_enum(&self) -> bool {
82 if let Self::Enum(_) = self {
83 true
84 } else {
85 false
86 }
87 }
88}
89
90#[derive(Clone, Debug, Eq, PartialEq, Hash)]
91pub struct TypeDef {
92 pub name: String,
93 pub params: Vec<String>,
94 pub body: TypeBody,
95}
96
97#[derive(Clone, Debug, Eq, PartialEq, Hash)]
98pub struct QualifiedIdentifier {
99 pub name: String,
100 pub module: Option<String>,
101}
102
103impl QualifiedIdentifier {
104 pub fn local(name: impl Into<String>) -> Self {
105 Self {
106 name: name.into(),
107 module: None,
108 }
109 }
110}