1use crate::{
2 CallSignature, Type,
3 inst::LiteralInstance,
4 syntax::{BinaryOperator, UnaryOperator},
5};
6
7#[derive(Clone, Debug)]
9pub enum Error {
10 Todo(String),
11
12 NotScalar(Type),
14 NotConstructible(Type),
15 SampledType(Type),
16 UnknownType(String),
17 UnexpectedTemplate(String),
18 MissingTemplate(&'static str),
19
20 WriteRefType(Type, Type),
22 NotWrite,
23 NotRead,
24 NotReadWrite,
25 PtrHandle,
26 PtrVecComp,
27
28 Conversion(Type, Type),
30 ConvOverflow(LiteralInstance, Type),
31
32 Component(Type, String),
34 NotIndexable(Type),
35 OutOfBounds(usize, Type, usize),
36
37 Unary(UnaryOperator, Type),
39 Binary(BinaryOperator, Type, Type),
40 CompwiseBinary(Type, Type),
41 AddOverflow,
42 SubOverflow,
43 MulOverflow,
44 DivByZero,
45 RemZeroDiv,
46 ShlOverflow(u32, LiteralInstance),
47 ShrOverflow(u32, LiteralInstance),
48
49 Signature(CallSignature),
51 Builtin(&'static str),
52 TemplateArgs(&'static str),
53 ParamCount(String, usize, usize),
54 ParamType(Type, Type),
55}
56
57impl std::fmt::Display for Error {
58 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
59 match self {
60 Error::Todo(v) => write!(fmt, "not implemented: `{v}`"),
61 Error::NotScalar(ty) => write!(fmt, "expected a scalar type, got `{ty}`"),
62 Error::NotConstructible(ty) => write!(fmt, "`{ty}` is not constructible"),
63 Error::SampledType(ty) => write!(
64 fmt,
65 "invalid sampled type, expected `i32`, `u32` or `f32`, got `{ty}`"
66 ),
67 Error::UnknownType(ty) => {
68 write!(fmt, "unknown type `{ty}`")
69 }
70 Error::UnexpectedTemplate(ty) => {
71 write!(fmt, "type `{ty}` does not take any template arguments")
72 }
73 Error::MissingTemplate(ty) => write!(fmt, "missing template arguments for type `{ty}`"),
74 Error::WriteRefType(new_ty, ty) => {
75 write!(fmt, "cannot write a `{new_ty}` to a reference to `{ty}`")
76 }
77 Error::NotWrite => write!(fmt, "attempt to write to a read-only reference"),
78 Error::NotRead => write!(fmt, "attempt to read a write-only reference"),
79 Error::NotReadWrite => write!(fmt, "reference is not read-write"),
80 Error::PtrHandle => write!(fmt, "cannot create a pointer in `handle` address space"),
81 Error::PtrVecComp => write!(fmt, "cannot create a pointer to a vector component"),
82 Error::Conversion(from_ty, to_ty) => {
83 write!(fmt, "cannot convert from `{from_ty}` to `{to_ty}`")
84 }
85 Error::ConvOverflow(literal, ty) => {
86 write!(fmt, "overflow while converting `{literal}` to `{ty}`")
87 }
88 Error::Component(ty, name) => write!(fmt, "`{ty}` has no component `{name}`"),
89 Error::NotIndexable(ty) => write!(fmt, "`{ty}` cannot be indexed"),
90 Error::OutOfBounds(index, ty, num_components) => write!(
91 fmt,
92 "index `{index}` is out-of-bounds for `{ty}` of `{num_components}` components"
93 ),
94 Error::Unary(op, ty) => write!(fmt, "cannot use unary operator `{op}` on type `{ty}`"),
95 Error::Binary(op, left_ty, right_ty) => write!(
96 fmt,
97 "cannot use binary operator `{op}` with operands `{left_ty}` and `{right_ty}`"
98 ),
99 Error::CompwiseBinary(ty_1, ty_2) => write!(
100 fmt,
101 "cannot apply component-wise binary operation on operands `{ty_1}` and `{ty_2}`"
102 ),
103 Error::AddOverflow => write!(fmt, "attempt to add with overflow"),
104 Error::SubOverflow => write!(fmt, "attempt to subtract with overflow"),
105 Error::MulOverflow => write!(fmt, "attempt to multiply with overflow"),
106 Error::DivByZero => write!(fmt, "attempt to divide by zero"),
107 Error::RemZeroDiv => write!(
108 fmt,
109 "attempt to calculate the remainder with a divisor of zero"
110 ),
111 Error::ShlOverflow(num, ty) => write!(
112 fmt,
113 "attempt to shift left by `{num}`, which would overflow `{ty}`"
114 ),
115 Error::ShrOverflow(num, ty) => write!(
116 fmt,
117 "attempt to shift right by `{num}`, which would overflow `{ty}`"
118 ),
119 Error::Signature(call_signature) => {
120 write!(fmt, "invalid function call signature: `{call_signature}`")
121 }
122 Error::Builtin(name) => write!(fmt, "{name}"),
123 Error::TemplateArgs(name) => write!(fmt, "invalid template arguments to `{name}`"),
124 Error::ParamCount(name, expected_count, actual_count) => write!(
125 fmt,
126 "incorrect number of arguments to `{name}`, expected `{expected_count}`, got `{actual_count}`"
127 ),
128 Error::ParamType(expected_ty, actual_ty) => write!(
129 fmt,
130 "invalid parameter type, expected `{expected_ty}`, got `{actual_ty}`"
131 ),
132 }
133 }
134}