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 F64,
42 String,
43 Box(Box<Type>),
44 List(Box<Type>),
45 Option(Box<Type>),
46 Result(Box<Type>, Box<Type>),
47}
48
49#[derive(Clone, Debug, Eq, PartialEq, Hash)]
50pub enum Type {
51 Primitive(PrimitiveType),
52 Defined {
53 ident: QualifiedIdentifier,
54 args: Vec<Type>,
55 },
56}
57
58impl Type {
59 pub fn local(name: impl Into<String>) -> Self {
60 Self::Defined {
61 ident: QualifiedIdentifier::local(name),
62 args: Vec::new(),
63 }
64 }
65}
66
67#[derive(Clone, Debug, Eq, PartialEq, Hash)]
68pub enum TypeBody {
69 Struct(Struct),
70 Enum(Enum),
71}
72
73impl TypeBody {
74 pub fn is_struct(&self) -> bool {
75 if let Self::Struct(_) = self {
76 true
77 } else {
78 false
79 }
80 }
81
82 pub fn is_enum(&self) -> bool {
83 if let Self::Enum(_) = self {
84 true
85 } else {
86 false
87 }
88 }
89}
90
91#[derive(Clone, Debug, Eq, PartialEq, Hash)]
92pub struct TypeDef {
93 pub name: String,
94 pub params: Vec<String>,
95 pub body: TypeBody,
96}
97
98#[derive(Clone, Debug, Eq, PartialEq, Hash)]
99pub struct QualifiedIdentifier {
100 pub name: String,
101 pub module: Option<String>,
102}
103
104impl QualifiedIdentifier {
105 pub fn local(name: impl Into<String>) -> Self {
106 Self {
107 name: name.into(),
108 module: None,
109 }
110 }
111}