Detect cycles in type declarations
[tiger.ml.git] / compiler / src / lib / tiger / tiger_error.ml
CommitLineData
c16dd441 1module Abs = Tiger_absyn
7c14a966 2module Pos = Tiger_position
c16dd441
SK
3module Sym = Tiger_symbol
4module Typ = Tiger_env_type
7c14a966 5
c16dd441 6type t =
e6e82c08
SK
7 | Cycle_in_type_decs of
8 { from_id : Sym.t
9 ; from_pos : Pos.t
10 ; to_id : Sym.t
11 ; to_pos : Pos.t
12 }
c16dd441
SK
13 | Invalid_syntax of Pos.t
14 | Unknown_id of {id : Sym.t; pos : Pos.t}
15 | Unknown_type of {ty_id : Sym.t; pos : Pos.t}
523e2b06 16 | Id_is_a_function of {id : Sym.t; pos : Pos.t}
c16dd441 17 | Id_not_a_function of {id : Sym.t; pos : Pos.t}
523e2b06
SK
18 | No_such_field_in_record of {field : Sym.t; record : Typ.t; pos : Pos.t}
19 | Exp_not_a_record of {ty : Typ.t; pos : Pos.t}
161a300d 20 | Exp_not_an_array of {ty : Typ.t; pos : Pos.t}
523e2b06
SK
21 | Wrong_type of
22 { expected : Typ.t
23 ; given : Typ.t
24 ; pos : Pos.t
25 }
c16dd441
SK
26 | Wrong_type_of_expression_in_var_dec of
27 { var_id : Sym.t
28 ; expected : Typ.t
29 ; given : Typ.t
30 ; pos : Pos.t
31 }
32 | Wrong_type_used_as_record of
33 { ty_id : Sym.t
34 ; ty : Typ.t
35 ; pos : Pos.t
36 }
4c550cd5
SK
37 | Wrong_type_used_as_array of
38 { ty_id : Sym.t
39 ; ty : Typ.t
40 ; pos : Pos.t
41 }
c16dd441
SK
42 | Wrong_type_of_field_value of
43 { field_id : Sym.t
44 ; expected : Typ.t
45 ; given : Typ.t
46 ; pos : Pos.t
47 }
48 | Wrong_type_of_arg of
49 { func : Sym.t
50 ; expected : Typ.t
51 ; given : Typ.t
52 ; pos : Pos.t
53 }
54 | Wrong_number_of_args of
55 { func : Sym.t
56 ; expected : int
57 ; given : int
58 ; pos : Pos.t
59 }
60 | Invalid_operand_type of
61 { oper : Abs.oper
62 ; valid : string list
63 ; given : Typ.t
64 ; pos : Pos.t
65 }
66 | Different_operand_types of
67 { oper : Abs.oper
68 ; left : Typ.t
69 ; right : Typ.t
70 ; pos : Pos.t
71 }
7c14a966 72
c16dd441
SK
73exception T of t
74
75let raise t =
76 raise (T t)
77
78let to_string =
79 let s = Printf.sprintf in
80 function
81 | Invalid_syntax pos ->
82 s "Invalid syntax in %s" (Pos.to_string pos)
e6e82c08
SK
83 | Cycle_in_type_decs {from_id; from_pos; to_id; to_pos} ->
84 s ( "Circular type declaration between %S and %S."
85 ^^"Locations: %S (in %s), %S (in %s).")
86 (Sym.to_string from_id)
87 (Sym.to_string to_id)
88 (Sym.to_string from_id)
89 (Pos.to_string from_pos)
90 (Sym.to_string to_id)
91 (Pos.to_string to_pos)
c16dd441
SK
92 | Unknown_id {id; pos} ->
93 s "Unknown identifier %S in %s" (Sym.to_string id) (Pos.to_string pos)
94 | Unknown_type {ty_id; pos} ->
95 s "Unknown type %S in %s" (Sym.to_string ty_id) (Pos.to_string pos)
523e2b06
SK
96 | Id_is_a_function {id; pos} ->
97 s "Identifier %S is a function, it cannot be used as a variable in %s"
98 (Sym.to_string id) (Pos.to_string pos)
c16dd441
SK
99 | Id_not_a_function {id; pos} ->
100 s "Identifier %S is not a function, it cannot be called in %s"
101 (Sym.to_string id) (Pos.to_string pos)
523e2b06
SK
102 | No_such_field_in_record {field; record; pos} ->
103 s "No field %S in record %S in %s"
104 (Sym.to_string field) (Typ.to_string record) (Pos.to_string pos)
105 | Exp_not_a_record {ty; pos} ->
161a300d
SK
106 s ( "The expression of type %S is not a record, it cannot be"
107 ^^"accessed in %s")
108 (Typ.to_string ty) (Pos.to_string pos)
109 | Exp_not_an_array {ty; pos} ->
110 s ( "The expression of type %S is not an array, it cannot be"
523e2b06
SK
111 ^^"accessed in %s")
112 (Typ.to_string ty) (Pos.to_string pos)
113 | Wrong_type {expected; given; pos} ->
114 s "Type error: expected: %S, but given: %S, in %s"
115 (Typ.to_string expected)
116 (Typ.to_string given)
117 (Pos.to_string pos)
c16dd441
SK
118 | Wrong_type_of_expression_in_var_dec {var_id; expected; given; pos} ->
119 s ( "Wrong type of expression in declaration of %S. "
120 ^^"Expected: %S, given: %S. In %s")
121 (Sym.to_string var_id)
122 (Typ.to_string expected)
123 (Typ.to_string given)
124 (Pos.to_string pos)
4c550cd5
SK
125 | Wrong_type_used_as_array {ty_id; ty; pos} ->
126 s ( "Identifier %S is bound to type %S, not an array. "
127 ^^"It cannot be used in %s")
128 (Sym.to_string ty_id) (Typ.to_string ty) (Pos.to_string pos)
c16dd441
SK
129 | Wrong_type_used_as_record {ty_id; ty; pos} ->
130 s ( "Identifier %S is bound to type %S, not a record. "
131 ^^"It cannot be used in %s")
132 (Sym.to_string ty_id) (Typ.to_string ty) (Pos.to_string pos)
133 | Wrong_type_of_field_value {field_id; expected; given; pos} ->
134 s ( "Field %S is declared to be of type %S, but is bound to expression "
135 ^^"of type %S in %s")
136 (Sym.to_string field_id)
137 (Typ.to_string expected)
138 (Typ.to_string given)
139 (Pos.to_string pos)
140 | Wrong_type_of_arg {func; expected; given; pos} ->
141 s ( "Incorrect type of argument to function %S, expected: %S, given: %S,"
142 ^^" in %s")
143 (Sym.to_string func)
144 (Typ.to_string expected)
145 (Typ.to_string given)
146 (Pos.to_string pos)
147 | Wrong_number_of_args {func; expected; given; pos} ->
148 s ( "Incorrect number of arguments to function %S, "
149 ^^"expected: %d, given: %d,"
150 ^^" in %s")
151 (Sym.to_string func) expected given (Pos.to_string pos)
152 | Invalid_operand_type {oper; valid; given; pos} ->
153 s ( "Invalid operand type %S for operator %S, which expects only: %s"
154 ^^". In %s")
155 (Typ.to_string given)
156 (Abs.op_show oper)
157 (String.concat ", " valid)
158 (Pos.to_string pos)
159 | Different_operand_types {oper; left; right; pos} ->
160 s "Operands of different types (%S %S %S) given in %s"
161 (Typ.to_string left)
162 (Abs.op_show oper)
163 (Typ.to_string right)
164 (Pos.to_string pos)
5da420a8
SK
165
166let is_unknown_id t =
167 match t with
168 | Unknown_id _ ->
169 true
e6e82c08 170 | Cycle_in_type_decs _
5da420a8
SK
171 | Invalid_syntax _
172 | Unknown_type _
523e2b06 173 | Id_is_a_function _
5da420a8 174 | Id_not_a_function _
523e2b06
SK
175 | No_such_field_in_record _
176 | Exp_not_a_record _
161a300d 177 | Exp_not_an_array _
523e2b06 178 | Wrong_type _
5da420a8 179 | Wrong_type_of_expression_in_var_dec _
4c550cd5 180 | Wrong_type_used_as_array _
5da420a8
SK
181 | Wrong_type_used_as_record _
182 | Wrong_type_of_field_value _
183 | Wrong_type_of_arg _
184 | Wrong_number_of_args _
185 | Invalid_operand_type _
186 | Different_operand_types _ ->
187 false
155073e2
SK
188
189let is_unknown_type t =
190 match t with
191 | Unknown_type _ ->
192 true
e6e82c08 193 | Cycle_in_type_decs _
155073e2
SK
194 | Unknown_id _
195 | Invalid_syntax _
196 | Id_is_a_function _
197 | Id_not_a_function _
198 | No_such_field_in_record _
199 | Exp_not_a_record _
200 | Exp_not_an_array _
201 | Wrong_type _
202 | Wrong_type_of_expression_in_var_dec _
203 | Wrong_type_used_as_array _
204 | Wrong_type_used_as_record _
205 | Wrong_type_of_field_value _
206 | Wrong_type_of_arg _
207 | Wrong_number_of_args _
208 | Invalid_operand_type _
209 | Different_operand_types _ ->
210 false
211
212let is_wrong_type t =
213 match t with
214 | Wrong_type _ ->
215 true
e6e82c08 216 | Cycle_in_type_decs _
155073e2
SK
217 | Unknown_type _
218 | Unknown_id _
219 | Invalid_syntax _
220 | Id_is_a_function _
221 | Id_not_a_function _
222 | No_such_field_in_record _
223 | Exp_not_a_record _
224 | Exp_not_an_array _
225 | Wrong_type_of_expression_in_var_dec _
226 | Wrong_type_used_as_array _
227 | Wrong_type_used_as_record _
228 | Wrong_type_of_field_value _
229 | Wrong_type_of_arg _
230 | Wrong_number_of_args _
231 | Invalid_operand_type _
232 | Different_operand_types _ ->
233 false
789ef466 234
54e838f4
SK
235let is_wrong_number_of_args t =
236 match t with
237 | Wrong_number_of_args _ ->
238 true
e6e82c08 239 | Cycle_in_type_decs _
54e838f4
SK
240 | Wrong_type _
241 | Unknown_type _
242 | Unknown_id _
243 | Invalid_syntax _
244 | Id_is_a_function _
245 | Id_not_a_function _
246 | No_such_field_in_record _
247 | Exp_not_a_record _
248 | Exp_not_an_array _
249 | Wrong_type_of_expression_in_var_dec _
250 | Wrong_type_used_as_array _
251 | Wrong_type_used_as_record _
252 | Wrong_type_of_field_value _
253 | Wrong_type_of_arg _
254 | Invalid_operand_type _
255 | Different_operand_types _ ->
256 false
257
789ef466
SK
258let is_invalid_syntax t =
259 match t with
260 | Invalid_syntax _ ->
261 true
e6e82c08 262 | Cycle_in_type_decs _
789ef466
SK
263 | Wrong_type _
264 | Unknown_type _
265 | Unknown_id _
266 | Id_is_a_function _
267 | Id_not_a_function _
268 | No_such_field_in_record _
269 | Exp_not_a_record _
270 | Exp_not_an_array _
271 | Wrong_type_of_expression_in_var_dec _
272 | Wrong_type_used_as_array _
273 | Wrong_type_used_as_record _
274 | Wrong_type_of_field_value _
275 | Wrong_type_of_arg _
276 | Wrong_number_of_args _
277 | Invalid_operand_type _
278 | Different_operand_types _ ->
279 false
1d155dc1
SK
280
281let is_not_a_record t =
282 match t with
283 | Exp_not_a_record _ ->
284 true
e6e82c08 285 | Cycle_in_type_decs _
1d155dc1
SK
286 | Invalid_syntax _
287 | Wrong_type _
288 | Unknown_type _
289 | Unknown_id _
290 | Id_is_a_function _
291 | Id_not_a_function _
292 | No_such_field_in_record _
293 | Exp_not_an_array _
294 | Wrong_type_of_expression_in_var_dec _
295 | Wrong_type_used_as_array _
296 | Wrong_type_used_as_record _
297 | Wrong_type_of_field_value _
298 | Wrong_type_of_arg _
299 | Wrong_number_of_args _
300 | Invalid_operand_type _
301 | Different_operand_types _ ->
302 false
303
304let is_not_an_array t =
305 match t with
306 | Exp_not_an_array _ ->
307 true
e6e82c08 308 | Cycle_in_type_decs _
1d155dc1
SK
309 | Exp_not_a_record _
310 | Invalid_syntax _
311 | Wrong_type _
312 | Unknown_type _
313 | Unknown_id _
314 | Id_is_a_function _
315 | Id_not_a_function _
316 | No_such_field_in_record _
317 | Wrong_type_of_expression_in_var_dec _
318 | Wrong_type_used_as_array _
319 | Wrong_type_used_as_record _
320 | Wrong_type_of_field_value _
321 | Wrong_type_of_arg _
322 | Wrong_number_of_args _
323 | Invalid_operand_type _
324 | Different_operand_types _ ->
325 false
326
327let is_no_such_field_in_record t =
328 match t with
329 | No_such_field_in_record _ ->
330 true
e6e82c08
SK
331 | Cycle_in_type_decs _
332 | Exp_not_an_array _
333 | Exp_not_a_record _
334 | Invalid_syntax _
335 | Wrong_type _
336 | Unknown_type _
337 | Unknown_id _
338 | Id_is_a_function _
339 | Id_not_a_function _
340 | Wrong_type_of_expression_in_var_dec _
341 | Wrong_type_used_as_array _
342 | Wrong_type_used_as_record _
343 | Wrong_type_of_field_value _
344 | Wrong_type_of_arg _
345 | Wrong_number_of_args _
346 | Invalid_operand_type _
347 | Different_operand_types _ ->
348 false
349
350let is_cycle_in_type_dec t =
351 match t with
352 | Cycle_in_type_decs _ ->
353 true
354 | No_such_field_in_record _
1d155dc1
SK
355 | Exp_not_an_array _
356 | Exp_not_a_record _
357 | Invalid_syntax _
358 | Wrong_type _
359 | Unknown_type _
360 | Unknown_id _
361 | Id_is_a_function _
362 | Id_not_a_function _
363 | Wrong_type_of_expression_in_var_dec _
364 | Wrong_type_used_as_array _
365 | Wrong_type_used_as_record _
366 | Wrong_type_of_field_value _
367 | Wrong_type_of_arg _
368 | Wrong_number_of_args _
369 | Invalid_operand_type _
370 | Different_operand_types _ ->
371 false
This page took 0.045036 seconds and 4 git commands to generate.