1use super::{ASTNode, Context, Error, LibISLError};
5use libc::uintptr_t;
6use std::ffi::CStr;
7use std::os::raw::c_char;
8
9pub struct ASTNodeList {
11 pub ptr: uintptr_t,
12 pub should_free_on_drop: bool,
13}
14
15extern "C" {
16
17 fn isl_ast_node_list_add(list: uintptr_t, el: uintptr_t) -> uintptr_t;
18
19 fn isl_ast_node_list_alloc(ctx: uintptr_t, n: i32) -> uintptr_t;
20
21 fn isl_ast_node_list_clear(list: uintptr_t) -> uintptr_t;
22
23 fn isl_ast_node_list_concat(list1: uintptr_t, list2: uintptr_t) -> uintptr_t;
24
25 fn isl_ast_node_list_copy(list: uintptr_t) -> uintptr_t;
26
27 fn isl_ast_node_list_drop(list: uintptr_t, first: u32, n: u32) -> uintptr_t;
28
29 fn isl_ast_node_list_dump(list: uintptr_t) -> ();
30
31 fn isl_ast_node_list_free(list: uintptr_t) -> uintptr_t;
32
33 fn isl_ast_node_list_from_ast_node(el: uintptr_t) -> uintptr_t;
34
35 fn isl_ast_node_list_get_ast_node(list: uintptr_t, index: i32) -> uintptr_t;
36
37 fn isl_ast_node_list_get_at(list: uintptr_t, index: i32) -> uintptr_t;
38
39 fn isl_ast_node_list_get_ctx(list: uintptr_t) -> uintptr_t;
40
41 fn isl_ast_node_list_insert(list: uintptr_t, pos: u32, el: uintptr_t) -> uintptr_t;
42
43 fn isl_ast_node_list_n_ast_node(list: uintptr_t) -> i32;
44
45 fn isl_ast_node_list_reverse(list: uintptr_t) -> uintptr_t;
46
47 fn isl_ast_node_list_set_ast_node(list: uintptr_t, index: i32, el: uintptr_t) -> uintptr_t;
48
49 fn isl_ast_node_list_set_at(list: uintptr_t, index: i32, el: uintptr_t) -> uintptr_t;
50
51 fn isl_ast_node_list_size(list: uintptr_t) -> i32;
52
53 fn isl_ast_node_list_swap(list: uintptr_t, pos1: u32, pos2: u32) -> uintptr_t;
54
55 fn isl_ast_node_list_to_str(list: uintptr_t) -> *const c_char;
56
57}
58
59impl ASTNodeList {
60 pub fn add(self, el: ASTNode) -> Result<ASTNodeList, LibISLError> {
62 let list = self;
63 let isl_rs_ctx = list.get_ctx();
64 let mut list = list;
65 list.do_not_free_on_drop();
66 let list = list.ptr;
67 let mut el = el;
68 el.do_not_free_on_drop();
69 let el = el.ptr;
70 let isl_rs_result = unsafe { isl_ast_node_list_add(list, el) };
71 let isl_rs_result = ASTNodeList { ptr: isl_rs_result,
72 should_free_on_drop: true };
73 let err = isl_rs_ctx.last_error();
74 if err != Error::None_ {
75 let err_msg = isl_rs_ctx.last_error_msg();
76 isl_rs_ctx.reset_error();
77 return Err(LibISLError::new(err, err_msg));
78 }
79 Ok(isl_rs_result)
80 }
81
82 pub fn alloc(ctx: &Context, n: i32) -> Result<ASTNodeList, LibISLError> {
84 let isl_rs_ctx = Context { ptr: ctx.ptr,
85 should_free_on_drop: false };
86 let ctx = ctx.ptr;
87 let isl_rs_result = unsafe { isl_ast_node_list_alloc(ctx, n) };
88 let isl_rs_result = ASTNodeList { ptr: isl_rs_result,
89 should_free_on_drop: true };
90 let err = isl_rs_ctx.last_error();
91 if err != Error::None_ {
92 let err_msg = isl_rs_ctx.last_error_msg();
93 isl_rs_ctx.reset_error();
94 return Err(LibISLError::new(err, err_msg));
95 }
96 Ok(isl_rs_result)
97 }
98
99 pub fn clear(self) -> Result<ASTNodeList, LibISLError> {
101 let list = self;
102 let isl_rs_ctx = list.get_ctx();
103 let mut list = list;
104 list.do_not_free_on_drop();
105 let list = list.ptr;
106 let isl_rs_result = unsafe { isl_ast_node_list_clear(list) };
107 let isl_rs_result = ASTNodeList { ptr: isl_rs_result,
108 should_free_on_drop: true };
109 let err = isl_rs_ctx.last_error();
110 if err != Error::None_ {
111 let err_msg = isl_rs_ctx.last_error_msg();
112 isl_rs_ctx.reset_error();
113 return Err(LibISLError::new(err, err_msg));
114 }
115 Ok(isl_rs_result)
116 }
117
118 pub fn concat(self, list2: ASTNodeList) -> Result<ASTNodeList, LibISLError> {
120 let list1 = self;
121 let isl_rs_ctx = list1.get_ctx();
122 let mut list1 = list1;
123 list1.do_not_free_on_drop();
124 let list1 = list1.ptr;
125 let mut list2 = list2;
126 list2.do_not_free_on_drop();
127 let list2 = list2.ptr;
128 let isl_rs_result = unsafe { isl_ast_node_list_concat(list1, list2) };
129 let isl_rs_result = ASTNodeList { ptr: isl_rs_result,
130 should_free_on_drop: true };
131 let err = isl_rs_ctx.last_error();
132 if err != Error::None_ {
133 let err_msg = isl_rs_ctx.last_error_msg();
134 isl_rs_ctx.reset_error();
135 return Err(LibISLError::new(err, err_msg));
136 }
137 Ok(isl_rs_result)
138 }
139
140 pub fn copy(&self) -> Result<ASTNodeList, LibISLError> {
142 let list = self;
143 let isl_rs_ctx = list.get_ctx();
144 let list = list.ptr;
145 let isl_rs_result = unsafe { isl_ast_node_list_copy(list) };
146 let isl_rs_result = ASTNodeList { ptr: isl_rs_result,
147 should_free_on_drop: true };
148 let err = isl_rs_ctx.last_error();
149 if err != Error::None_ {
150 let err_msg = isl_rs_ctx.last_error_msg();
151 isl_rs_ctx.reset_error();
152 return Err(LibISLError::new(err, err_msg));
153 }
154 Ok(isl_rs_result)
155 }
156
157 pub fn drop(self, first: u32, n: u32) -> Result<ASTNodeList, LibISLError> {
159 let list = self;
160 let isl_rs_ctx = list.get_ctx();
161 let mut list = list;
162 list.do_not_free_on_drop();
163 let list = list.ptr;
164 let isl_rs_result = unsafe { isl_ast_node_list_drop(list, first, n) };
165 let isl_rs_result = ASTNodeList { ptr: isl_rs_result,
166 should_free_on_drop: true };
167 let err = isl_rs_ctx.last_error();
168 if err != Error::None_ {
169 let err_msg = isl_rs_ctx.last_error_msg();
170 isl_rs_ctx.reset_error();
171 return Err(LibISLError::new(err, err_msg));
172 }
173 Ok(isl_rs_result)
174 }
175
176 pub fn dump(&self) -> Result<(), LibISLError> {
178 let list = self;
179 let isl_rs_ctx = list.get_ctx();
180 let list = list.ptr;
181 let isl_rs_result = unsafe { isl_ast_node_list_dump(list) };
182 let err = isl_rs_ctx.last_error();
183 if err != Error::None_ {
184 let err_msg = isl_rs_ctx.last_error_msg();
185 isl_rs_ctx.reset_error();
186 return Err(LibISLError::new(err, err_msg));
187 }
188 Ok(isl_rs_result)
189 }
190
191 pub fn free(self) -> Result<ASTNodeList, LibISLError> {
193 let list = self;
194 let isl_rs_ctx = list.get_ctx();
195 let mut list = list;
196 list.do_not_free_on_drop();
197 let list = list.ptr;
198 let isl_rs_result = unsafe { isl_ast_node_list_free(list) };
199 let isl_rs_result = ASTNodeList { ptr: isl_rs_result,
200 should_free_on_drop: true };
201 let err = isl_rs_ctx.last_error();
202 if err != Error::None_ {
203 let err_msg = isl_rs_ctx.last_error_msg();
204 isl_rs_ctx.reset_error();
205 return Err(LibISLError::new(err, err_msg));
206 }
207 Ok(isl_rs_result)
208 }
209
210 pub fn from_ast_node(el: ASTNode) -> Result<ASTNodeList, LibISLError> {
212 let isl_rs_ctx = el.get_ctx();
213 let mut el = el;
214 el.do_not_free_on_drop();
215 let el = el.ptr;
216 let isl_rs_result = unsafe { isl_ast_node_list_from_ast_node(el) };
217 let isl_rs_result = ASTNodeList { ptr: isl_rs_result,
218 should_free_on_drop: true };
219 let err = isl_rs_ctx.last_error();
220 if err != Error::None_ {
221 let err_msg = isl_rs_ctx.last_error_msg();
222 isl_rs_ctx.reset_error();
223 return Err(LibISLError::new(err, err_msg));
224 }
225 Ok(isl_rs_result)
226 }
227
228 pub fn get_ast_node(&self, index: i32) -> Result<ASTNode, LibISLError> {
230 let list = self;
231 let isl_rs_ctx = list.get_ctx();
232 let list = list.ptr;
233 let isl_rs_result = unsafe { isl_ast_node_list_get_ast_node(list, index) };
234 let isl_rs_result = ASTNode { ptr: isl_rs_result,
235 should_free_on_drop: true };
236 let err = isl_rs_ctx.last_error();
237 if err != Error::None_ {
238 let err_msg = isl_rs_ctx.last_error_msg();
239 isl_rs_ctx.reset_error();
240 return Err(LibISLError::new(err, err_msg));
241 }
242 Ok(isl_rs_result)
243 }
244
245 pub fn get_at(&self, index: i32) -> Result<ASTNode, LibISLError> {
247 let list = self;
248 let isl_rs_ctx = list.get_ctx();
249 let list = list.ptr;
250 let isl_rs_result = unsafe { isl_ast_node_list_get_at(list, index) };
251 let isl_rs_result = ASTNode { ptr: isl_rs_result,
252 should_free_on_drop: true };
253 let err = isl_rs_ctx.last_error();
254 if err != Error::None_ {
255 let err_msg = isl_rs_ctx.last_error_msg();
256 isl_rs_ctx.reset_error();
257 return Err(LibISLError::new(err, err_msg));
258 }
259 Ok(isl_rs_result)
260 }
261
262 pub fn get_ctx(&self) -> Context {
264 let list = self;
265 let list = list.ptr;
266 let isl_rs_result = unsafe { isl_ast_node_list_get_ctx(list) };
267 let isl_rs_result = Context { ptr: isl_rs_result,
268 should_free_on_drop: false };
269 isl_rs_result
270 }
271
272 pub fn insert(self, pos: u32, el: ASTNode) -> Result<ASTNodeList, LibISLError> {
274 let list = self;
275 let isl_rs_ctx = list.get_ctx();
276 let mut list = list;
277 list.do_not_free_on_drop();
278 let list = list.ptr;
279 let mut el = el;
280 el.do_not_free_on_drop();
281 let el = el.ptr;
282 let isl_rs_result = unsafe { isl_ast_node_list_insert(list, pos, el) };
283 let isl_rs_result = ASTNodeList { ptr: isl_rs_result,
284 should_free_on_drop: true };
285 let err = isl_rs_ctx.last_error();
286 if err != Error::None_ {
287 let err_msg = isl_rs_ctx.last_error_msg();
288 isl_rs_ctx.reset_error();
289 return Err(LibISLError::new(err, err_msg));
290 }
291 Ok(isl_rs_result)
292 }
293
294 pub fn n_ast_node(&self) -> Result<i32, LibISLError> {
296 let list = self;
297 let isl_rs_ctx = list.get_ctx();
298 let list = list.ptr;
299 let isl_rs_result = unsafe { isl_ast_node_list_n_ast_node(list) };
300 let err = isl_rs_ctx.last_error();
301 if err != Error::None_ {
302 let err_msg = isl_rs_ctx.last_error_msg();
303 isl_rs_ctx.reset_error();
304 return Err(LibISLError::new(err, err_msg));
305 }
306 Ok(isl_rs_result)
307 }
308
309 pub fn reverse(self) -> Result<ASTNodeList, LibISLError> {
311 let list = self;
312 let isl_rs_ctx = list.get_ctx();
313 let mut list = list;
314 list.do_not_free_on_drop();
315 let list = list.ptr;
316 let isl_rs_result = unsafe { isl_ast_node_list_reverse(list) };
317 let isl_rs_result = ASTNodeList { ptr: isl_rs_result,
318 should_free_on_drop: true };
319 let err = isl_rs_ctx.last_error();
320 if err != Error::None_ {
321 let err_msg = isl_rs_ctx.last_error_msg();
322 isl_rs_ctx.reset_error();
323 return Err(LibISLError::new(err, err_msg));
324 }
325 Ok(isl_rs_result)
326 }
327
328 pub fn set_ast_node(self, index: i32, el: ASTNode) -> Result<ASTNodeList, LibISLError> {
330 let list = self;
331 let isl_rs_ctx = list.get_ctx();
332 let mut list = list;
333 list.do_not_free_on_drop();
334 let list = list.ptr;
335 let mut el = el;
336 el.do_not_free_on_drop();
337 let el = el.ptr;
338 let isl_rs_result = unsafe { isl_ast_node_list_set_ast_node(list, index, el) };
339 let isl_rs_result = ASTNodeList { ptr: isl_rs_result,
340 should_free_on_drop: true };
341 let err = isl_rs_ctx.last_error();
342 if err != Error::None_ {
343 let err_msg = isl_rs_ctx.last_error_msg();
344 isl_rs_ctx.reset_error();
345 return Err(LibISLError::new(err, err_msg));
346 }
347 Ok(isl_rs_result)
348 }
349
350 pub fn set_at(self, index: i32, el: ASTNode) -> Result<ASTNodeList, LibISLError> {
352 let list = self;
353 let isl_rs_ctx = list.get_ctx();
354 let mut list = list;
355 list.do_not_free_on_drop();
356 let list = list.ptr;
357 let mut el = el;
358 el.do_not_free_on_drop();
359 let el = el.ptr;
360 let isl_rs_result = unsafe { isl_ast_node_list_set_at(list, index, el) };
361 let isl_rs_result = ASTNodeList { ptr: isl_rs_result,
362 should_free_on_drop: true };
363 let err = isl_rs_ctx.last_error();
364 if err != Error::None_ {
365 let err_msg = isl_rs_ctx.last_error_msg();
366 isl_rs_ctx.reset_error();
367 return Err(LibISLError::new(err, err_msg));
368 }
369 Ok(isl_rs_result)
370 }
371
372 pub fn size(&self) -> Result<i32, LibISLError> {
374 let list = self;
375 let isl_rs_ctx = list.get_ctx();
376 let list = list.ptr;
377 let isl_rs_result = unsafe { isl_ast_node_list_size(list) };
378 let err = isl_rs_ctx.last_error();
379 if err != Error::None_ {
380 let err_msg = isl_rs_ctx.last_error_msg();
381 isl_rs_ctx.reset_error();
382 return Err(LibISLError::new(err, err_msg));
383 }
384 Ok(isl_rs_result)
385 }
386
387 pub fn swap(self, pos1: u32, pos2: u32) -> Result<ASTNodeList, LibISLError> {
389 let list = self;
390 let isl_rs_ctx = list.get_ctx();
391 let mut list = list;
392 list.do_not_free_on_drop();
393 let list = list.ptr;
394 let isl_rs_result = unsafe { isl_ast_node_list_swap(list, pos1, pos2) };
395 let isl_rs_result = ASTNodeList { ptr: isl_rs_result,
396 should_free_on_drop: true };
397 let err = isl_rs_ctx.last_error();
398 if err != Error::None_ {
399 let err_msg = isl_rs_ctx.last_error_msg();
400 isl_rs_ctx.reset_error();
401 return Err(LibISLError::new(err, err_msg));
402 }
403 Ok(isl_rs_result)
404 }
405
406 pub fn to_str(&self) -> Result<&str, LibISLError> {
408 let list = self;
409 let isl_rs_ctx = list.get_ctx();
410 let list = list.ptr;
411 let isl_rs_result = unsafe { isl_ast_node_list_to_str(list) };
412 let isl_rs_result = unsafe { CStr::from_ptr(isl_rs_result) };
413 let isl_rs_result = isl_rs_result.to_str().unwrap();
414 let err = isl_rs_ctx.last_error();
415 if err != Error::None_ {
416 let err_msg = isl_rs_ctx.last_error_msg();
417 isl_rs_ctx.reset_error();
418 return Err(LibISLError::new(err, err_msg));
419 }
420 Ok(isl_rs_result)
421 }
422
423 pub fn do_not_free_on_drop(&mut self) {
426 self.should_free_on_drop = false;
427 }
428}
429
430impl Drop for ASTNodeList {
431 fn drop(&mut self) {
432 if self.should_free_on_drop {
433 unsafe {
434 isl_ast_node_list_free(self.ptr);
435 }
436 }
437 }
438}