Bug Summary

File:tools/polly/lib/External/isl/isl_ast.c
Warning:line 2402, column 2
Use of memory after it is freed

Annotated Source Code

1/*
2 * Copyright 2012-2013 Ecole Normale Superieure
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
8 */
9
10#include <string.h>
11
12#include <isl_ast_private.h>
13
14#undef BASEast_node
15#define BASEast_node ast_expr
16
17#include <isl_list_templ.c>
18
19#undef BASEast_node
20#define BASEast_node ast_node
21
22#include <isl_list_templ.c>
23
24isl_ctx *isl_ast_print_options_get_ctx(
25 __isl_keep isl_ast_print_options *options)
26{
27 return options ? options->ctx : NULL((void*)0);
28}
29
30__isl_give isl_ast_print_options *isl_ast_print_options_alloc(isl_ctx *ctx)
31{
32 isl_ast_print_options *options;
33
34 options = isl_calloc_type(ctx, isl_ast_print_options)((isl_ast_print_options *)isl_calloc_or_die(ctx, 1, sizeof(isl_ast_print_options
)))
;
35 if (!options)
36 return NULL((void*)0);
37
38 options->ctx = ctx;
39 isl_ctx_ref(ctx);
40 options->ref = 1;
41
42 return options;
43}
44
45__isl_give isl_ast_print_options *isl_ast_print_options_dup(
46 __isl_keep isl_ast_print_options *options)
47{
48 isl_ctx *ctx;
49 isl_ast_print_options *dup;
50
51 if (!options)
52 return NULL((void*)0);
53
54 ctx = isl_ast_print_options_get_ctx(options);
55 dup = isl_ast_print_options_alloc(ctx);
56 if (!dup)
57 return NULL((void*)0);
58
59 dup->print_for = options->print_for;
60 dup->print_for_user = options->print_for_user;
61 dup->print_user = options->print_user;
62 dup->print_user_user = options->print_user_user;
63
64 return dup;
65}
66
67__isl_give isl_ast_print_options *isl_ast_print_options_cow(
68 __isl_take isl_ast_print_options *options)
69{
70 if (!options)
71 return NULL((void*)0);
72
73 if (options->ref == 1)
74 return options;
75 options->ref--;
76 return isl_ast_print_options_dup(options);
77}
78
79__isl_give isl_ast_print_options *isl_ast_print_options_copy(
80 __isl_keep isl_ast_print_options *options)
81{
82 if (!options)
83 return NULL((void*)0);
84
85 options->ref++;
86 return options;
87}
88
89__isl_null isl_ast_print_options *isl_ast_print_options_free(
90 __isl_take isl_ast_print_options *options)
91{
92 if (!options)
16
Taking false branch
93 return NULL((void*)0);
94
95 if (--options->ref > 0)
17
Assuming the condition is false
18
Taking false branch
96 return NULL((void*)0);
97
98 isl_ctx_deref(options->ctx);
99
100 free(options);
19
Memory is released
101 return NULL((void*)0);
102}
103
104/* Set the print_user callback of "options" to "print_user".
105 *
106 * If this callback is set, then it used to print user nodes in the AST.
107 * Otherwise, the expression associated to the user node is printed.
108 */
109__isl_give isl_ast_print_options *isl_ast_print_options_set_print_user(
110 __isl_take isl_ast_print_options *options,
111 __isl_give isl_printer *(*print_user)(__isl_take isl_printer *p,
112 __isl_take isl_ast_print_options *options,
113 __isl_keep isl_ast_node *node, void *user),
114 void *user)
115{
116 options = isl_ast_print_options_cow(options);
117 if (!options)
118 return NULL((void*)0);
119
120 options->print_user = print_user;
121 options->print_user_user = user;
122
123 return options;
124}
125
126/* Set the print_for callback of "options" to "print_for".
127 *
128 * If this callback is set, then it used to print for nodes in the AST.
129 */
130__isl_give isl_ast_print_options *isl_ast_print_options_set_print_for(
131 __isl_take isl_ast_print_options *options,
132 __isl_give isl_printer *(*print_for)(__isl_take isl_printer *p,
133 __isl_take isl_ast_print_options *options,
134 __isl_keep isl_ast_node *node, void *user),
135 void *user)
136{
137 options = isl_ast_print_options_cow(options);
138 if (!options)
139 return NULL((void*)0);
140
141 options->print_for = print_for;
142 options->print_for_user = user;
143
144 return options;
145}
146
147__isl_give isl_ast_expr *isl_ast_expr_copy(__isl_keep isl_ast_expr *expr)
148{
149 if (!expr)
150 return NULL((void*)0);
151
152 expr->ref++;
153 return expr;
154}
155
156__isl_give isl_ast_expr *isl_ast_expr_dup(__isl_keep isl_ast_expr *expr)
157{
158 int i;
159 isl_ctx *ctx;
160 isl_ast_expr *dup;
161
162 if (!expr)
163 return NULL((void*)0);
164
165 ctx = isl_ast_expr_get_ctx(expr);
166 switch (expr->type) {
167 case isl_ast_expr_int:
168 dup = isl_ast_expr_from_val(isl_val_copy(expr->u.v));
169 break;
170 case isl_ast_expr_id:
171 dup = isl_ast_expr_from_id(isl_id_copy(expr->u.id));
172 break;
173 case isl_ast_expr_op:
174 dup = isl_ast_expr_alloc_op(ctx,
175 expr->u.op.op, expr->u.op.n_arg);
176 if (!dup)
177 return NULL((void*)0);
178 for (i = 0; i < expr->u.op.n_arg; ++i)
179 dup->u.op.args[i] =
180 isl_ast_expr_copy(expr->u.op.args[i]);
181 break;
182 case isl_ast_expr_error:
183 dup = NULL((void*)0);
184 }
185
186 if (!dup)
187 return NULL((void*)0);
188
189 return dup;
190}
191
192__isl_give isl_ast_expr *isl_ast_expr_cow(__isl_take isl_ast_expr *expr)
193{
194 if (!expr)
195 return NULL((void*)0);
196
197 if (expr->ref == 1)
198 return expr;
199 expr->ref--;
200 return isl_ast_expr_dup(expr);
201}
202
203__isl_null isl_ast_expr *isl_ast_expr_free(__isl_take isl_ast_expr *expr)
204{
205 int i;
206
207 if (!expr)
208 return NULL((void*)0);
209
210 if (--expr->ref > 0)
211 return NULL((void*)0);
212
213 isl_ctx_deref(expr->ctx);
214
215 switch (expr->type) {
216 case isl_ast_expr_int:
217 isl_val_free(expr->u.v);
218 break;
219 case isl_ast_expr_id:
220 isl_id_free(expr->u.id);
221 break;
222 case isl_ast_expr_op:
223 if (expr->u.op.args)
224 for (i = 0; i < expr->u.op.n_arg; ++i)
225 isl_ast_expr_free(expr->u.op.args[i]);
226 free(expr->u.op.args);
227 break;
228 case isl_ast_expr_error:
229 break;
230 }
231
232 free(expr);
233 return NULL((void*)0);
234}
235
236isl_ctx *isl_ast_expr_get_ctx(__isl_keep isl_ast_expr *expr)
237{
238 return expr ? expr->ctx : NULL((void*)0);
239}
240
241enum isl_ast_expr_type isl_ast_expr_get_type(__isl_keep isl_ast_expr *expr)
242{
243 return expr ? expr->type : isl_ast_expr_error;
244}
245
246/* Return the integer value represented by "expr".
247 */
248__isl_give isl_val *isl_ast_expr_get_val(__isl_keep isl_ast_expr *expr)
249{
250 if (!expr)
251 return NULL((void*)0);
252 if (expr->type != isl_ast_expr_int)
253 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "expression not an int", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 254); return ((void*)0); } while (0)
254 "expression not an int", return NULL)do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "expression not an int", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 254); return ((void*)0); } while (0)
;
255 return isl_val_copy(expr->u.v);
256}
257
258__isl_give isl_id *isl_ast_expr_get_id(__isl_keep isl_ast_expr *expr)
259{
260 if (!expr)
261 return NULL((void*)0);
262 if (expr->type != isl_ast_expr_id)
263 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "expression not an identifier", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 264); return ((void*)0); } while (0)
264 "expression not an identifier", return NULL)do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "expression not an identifier", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 264); return ((void*)0); } while (0)
;
265
266 return isl_id_copy(expr->u.id);
267}
268
269enum isl_ast_op_type isl_ast_expr_get_op_type(__isl_keep isl_ast_expr *expr)
270{
271 if (!expr)
272 return isl_ast_op_error;
273 if (expr->type != isl_ast_expr_op)
274 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "expression not an operation", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 275); return isl_ast_op_error; } while (0)
275 "expression not an operation", return isl_ast_op_error)do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "expression not an operation", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 275); return isl_ast_op_error; } while (0)
;
276 return expr->u.op.op;
277}
278
279int isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr *expr)
280{
281 if (!expr)
282 return -1;
283 if (expr->type != isl_ast_expr_op)
284 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "expression not an operation", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 285); return -1; } while (0)
285 "expression not an operation", return -1)do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "expression not an operation", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 285); return -1; } while (0)
;
286 return expr->u.op.n_arg;
287}
288
289__isl_give isl_ast_expr *isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr *expr,
290 int pos)
291{
292 if (!expr)
293 return NULL((void*)0);
294 if (expr->type != isl_ast_expr_op)
295 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "expression not an operation", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 296); return ((void*)0); } while (0)
296 "expression not an operation", return NULL)do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "expression not an operation", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 296); return ((void*)0); } while (0)
;
297 if (pos < 0 || pos >= expr->u.op.n_arg)
298 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "index out of bounds", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 299); return ((void*)0); } while (0)
299 "index out of bounds", return NULL)do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "index out of bounds", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 299); return ((void*)0); } while (0)
;
300
301 return isl_ast_expr_copy(expr->u.op.args[pos]);
302}
303
304/* Replace the argument at position "pos" of "expr" by "arg".
305 */
306__isl_give isl_ast_expr *isl_ast_expr_set_op_arg(__isl_take isl_ast_expr *expr,
307 int pos, __isl_take isl_ast_expr *arg)
308{
309 expr = isl_ast_expr_cow(expr);
310 if (!expr || !arg)
311 goto error;
312 if (expr->type != isl_ast_expr_op)
313 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "expression not an operation", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 314); goto error; } while (0)
314 "expression not an operation", goto error)do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "expression not an operation", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 314); goto error; } while (0)
;
315 if (pos < 0 || pos >= expr->u.op.n_arg)
316 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "index out of bounds", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 317); goto error; } while (0)
317 "index out of bounds", goto error)do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "index out of bounds", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 317); goto error; } while (0)
;
318
319 isl_ast_expr_free(expr->u.op.args[pos]);
320 expr->u.op.args[pos] = arg;
321
322 return expr;
323error:
324 isl_ast_expr_free(arg);
325 return isl_ast_expr_free(expr);
326}
327
328/* Is "expr1" equal to "expr2"?
329 */
330isl_bool isl_ast_expr_is_equal(__isl_keep isl_ast_expr *expr1,
331 __isl_keep isl_ast_expr *expr2)
332{
333 int i;
334
335 if (!expr1 || !expr2)
336 return isl_bool_error;
337
338 if (expr1 == expr2)
339 return isl_bool_true;
340 if (expr1->type != expr2->type)
341 return isl_bool_false;
342 switch (expr1->type) {
343 case isl_ast_expr_int:
344 return isl_val_eq(expr1->u.v, expr2->u.v);
345 case isl_ast_expr_id:
346 return expr1->u.id == expr2->u.id;
347 case isl_ast_expr_op:
348 if (expr1->u.op.op != expr2->u.op.op)
349 return isl_bool_false;
350 if (expr1->u.op.n_arg != expr2->u.op.n_arg)
351 return isl_bool_false;
352 for (i = 0; i < expr1->u.op.n_arg; ++i) {
353 isl_bool equal;
354 equal = isl_ast_expr_is_equal(expr1->u.op.args[i],
355 expr2->u.op.args[i]);
356 if (equal < 0 || !equal)
357 return equal;
358 }
359 return 1;
360 case isl_ast_expr_error:
361 return isl_bool_error;
362 }
363
364 isl_die(isl_ast_expr_get_ctx(expr1), isl_error_internal,do { isl_handle_error(isl_ast_expr_get_ctx(expr1), isl_error_internal
, "unhandled case", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 365); return isl_bool_error; } while (0)
365 "unhandled case", return isl_bool_error)do { isl_handle_error(isl_ast_expr_get_ctx(expr1), isl_error_internal
, "unhandled case", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 365); return isl_bool_error; } while (0)
;
366}
367
368/* Create a new operation expression of operation type "op",
369 * with "n_arg" as yet unspecified arguments.
370 */
371__isl_give isl_ast_expr *isl_ast_expr_alloc_op(isl_ctx *ctx,
372 enum isl_ast_op_type op, int n_arg)
373{
374 isl_ast_expr *expr;
375
376 expr = isl_calloc_type(ctx, isl_ast_expr)((isl_ast_expr *)isl_calloc_or_die(ctx, 1, sizeof(isl_ast_expr
)))
;
377 if (!expr)
378 return NULL((void*)0);
379
380 expr->ctx = ctx;
381 isl_ctx_ref(ctx);
382 expr->ref = 1;
383 expr->type = isl_ast_expr_op;
384 expr->u.op.op = op;
385 expr->u.op.n_arg = n_arg;
386 expr->u.op.args = isl_calloc_array(ctx, isl_ast_expr *, n_arg)((isl_ast_expr * *)isl_calloc_or_die(ctx, n_arg, sizeof(isl_ast_expr
*)))
;
387
388 if (n_arg && !expr->u.op.args)
389 return isl_ast_expr_free(expr);
390
391 return expr;
392}
393
394/* Create a new id expression representing "id".
395 */
396__isl_give isl_ast_expr *isl_ast_expr_from_id(__isl_take isl_id *id)
397{
398 isl_ctx *ctx;
399 isl_ast_expr *expr;
400
401 if (!id)
402 return NULL((void*)0);
403
404 ctx = isl_id_get_ctx(id);
405 expr = isl_calloc_type(ctx, isl_ast_expr)((isl_ast_expr *)isl_calloc_or_die(ctx, 1, sizeof(isl_ast_expr
)))
;
406 if (!expr)
407 goto error;
408
409 expr->ctx = ctx;
410 isl_ctx_ref(ctx);
411 expr->ref = 1;
412 expr->type = isl_ast_expr_id;
413 expr->u.id = id;
414
415 return expr;
416error:
417 isl_id_free(id);
418 return NULL((void*)0);
419}
420
421/* Create a new integer expression representing "i".
422 */
423__isl_give isl_ast_expr *isl_ast_expr_alloc_int_si(isl_ctx *ctx, int i)
424{
425 isl_ast_expr *expr;
426
427 expr = isl_calloc_type(ctx, isl_ast_expr)((isl_ast_expr *)isl_calloc_or_die(ctx, 1, sizeof(isl_ast_expr
)))
;
428 if (!expr)
429 return NULL((void*)0);
430
431 expr->ctx = ctx;
432 isl_ctx_ref(ctx);
433 expr->ref = 1;
434 expr->type = isl_ast_expr_int;
435 expr->u.v = isl_val_int_from_si(ctx, i);
436 if (!expr->u.v)
437 return isl_ast_expr_free(expr);
438
439 return expr;
440}
441
442/* Create a new integer expression representing "v".
443 */
444__isl_give isl_ast_expr *isl_ast_expr_from_val(__isl_take isl_val *v)
445{
446 isl_ctx *ctx;
447 isl_ast_expr *expr;
448
449 if (!v)
450 return NULL((void*)0);
451 if (!isl_val_is_int(v))
452 isl_die(isl_val_get_ctx(v), isl_error_invalid,do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "expecting integer value"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 453); goto error; } while (0)
453 "expecting integer value", goto error)do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "expecting integer value"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 453); goto error; } while (0)
;
454
455 ctx = isl_val_get_ctx(v);
456 expr = isl_calloc_type(ctx, isl_ast_expr)((isl_ast_expr *)isl_calloc_or_die(ctx, 1, sizeof(isl_ast_expr
)))
;
457 if (!expr)
458 goto error;
459
460 expr->ctx = ctx;
461 isl_ctx_ref(ctx);
462 expr->ref = 1;
463 expr->type = isl_ast_expr_int;
464 expr->u.v = v;
465
466 return expr;
467error:
468 isl_val_free(v);
469 return NULL((void*)0);
470}
471
472/* Create an expression representing the unary operation "type" applied to
473 * "arg".
474 */
475__isl_give isl_ast_expr *isl_ast_expr_alloc_unary(enum isl_ast_op_type type,
476 __isl_take isl_ast_expr *arg)
477{
478 isl_ctx *ctx;
479 isl_ast_expr *expr = NULL((void*)0);
480
481 if (!arg)
482 return NULL((void*)0);
483
484 ctx = isl_ast_expr_get_ctx(arg);
485 expr = isl_ast_expr_alloc_op(ctx, type, 1);
486 if (!expr)
487 goto error;
488
489 expr->u.op.args[0] = arg;
490
491 return expr;
492error:
493 isl_ast_expr_free(arg);
494 return NULL((void*)0);
495}
496
497/* Create an expression representing the negation of "arg".
498 */
499__isl_give isl_ast_expr *isl_ast_expr_neg(__isl_take isl_ast_expr *arg)
500{
501 return isl_ast_expr_alloc_unary(isl_ast_op_minus, arg);
502}
503
504/* Create an expression representing the address of "expr".
505 */
506__isl_give isl_ast_expr *isl_ast_expr_address_of(__isl_take isl_ast_expr *expr)
507{
508 if (!expr)
509 return NULL((void*)0);
510
511 if (isl_ast_expr_get_type(expr) != isl_ast_expr_op ||
512 isl_ast_expr_get_op_type(expr) != isl_ast_op_access)
513 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "can only take address of access expressions", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 515); return isl_ast_expr_free(expr); } while (0)
514 "can only take address of access expressions",do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "can only take address of access expressions", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 515); return isl_ast_expr_free(expr); } while (0)
515 return isl_ast_expr_free(expr))do { isl_handle_error(isl_ast_expr_get_ctx(expr), isl_error_invalid
, "can only take address of access expressions", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 515); return isl_ast_expr_free(expr); } while (0)
;
516
517 return isl_ast_expr_alloc_unary(isl_ast_op_address_of, expr);
518}
519
520/* Create an expression representing the binary operation "type"
521 * applied to "expr1" and "expr2".
522 */
523__isl_give isl_ast_expr *isl_ast_expr_alloc_binary(enum isl_ast_op_type type,
524 __isl_take isl_ast_expr *expr1, __isl_take isl_ast_expr *expr2)
525{
526 isl_ctx *ctx;
527 isl_ast_expr *expr = NULL((void*)0);
528
529 if (!expr1 || !expr2)
530 goto error;
531
532 ctx = isl_ast_expr_get_ctx(expr1);
533 expr = isl_ast_expr_alloc_op(ctx, type, 2);
534 if (!expr)
535 goto error;
536
537 expr->u.op.args[0] = expr1;
538 expr->u.op.args[1] = expr2;
539
540 return expr;
541error:
542 isl_ast_expr_free(expr1);
543 isl_ast_expr_free(expr2);
544 return NULL((void*)0);
545}
546
547/* Create an expression representing the sum of "expr1" and "expr2".
548 */
549__isl_give isl_ast_expr *isl_ast_expr_add(__isl_take isl_ast_expr *expr1,
550 __isl_take isl_ast_expr *expr2)
551{
552 return isl_ast_expr_alloc_binary(isl_ast_op_add, expr1, expr2);
553}
554
555/* Create an expression representing the difference of "expr1" and "expr2".
556 */
557__isl_give isl_ast_expr *isl_ast_expr_sub(__isl_take isl_ast_expr *expr1,
558 __isl_take isl_ast_expr *expr2)
559{
560 return isl_ast_expr_alloc_binary(isl_ast_op_sub, expr1, expr2);
561}
562
563/* Create an expression representing the product of "expr1" and "expr2".
564 */
565__isl_give isl_ast_expr *isl_ast_expr_mul(__isl_take isl_ast_expr *expr1,
566 __isl_take isl_ast_expr *expr2)
567{
568 return isl_ast_expr_alloc_binary(isl_ast_op_mul, expr1, expr2);
569}
570
571/* Create an expression representing the quotient of "expr1" and "expr2".
572 */
573__isl_give isl_ast_expr *isl_ast_expr_div(__isl_take isl_ast_expr *expr1,
574 __isl_take isl_ast_expr *expr2)
575{
576 return isl_ast_expr_alloc_binary(isl_ast_op_div, expr1, expr2);
577}
578
579/* Create an expression representing the quotient of the integer
580 * division of "expr1" by "expr2", where "expr1" is known to be
581 * non-negative.
582 */
583__isl_give isl_ast_expr *isl_ast_expr_pdiv_q(__isl_take isl_ast_expr *expr1,
584 __isl_take isl_ast_expr *expr2)
585{
586 return isl_ast_expr_alloc_binary(isl_ast_op_pdiv_q, expr1, expr2);
587}
588
589/* Create an expression representing the remainder of the integer
590 * division of "expr1" by "expr2", where "expr1" is known to be
591 * non-negative.
592 */
593__isl_give isl_ast_expr *isl_ast_expr_pdiv_r(__isl_take isl_ast_expr *expr1,
594 __isl_take isl_ast_expr *expr2)
595{
596 return isl_ast_expr_alloc_binary(isl_ast_op_pdiv_r, expr1, expr2);
597}
598
599/* Create an expression representing the conjunction of "expr1" and "expr2".
600 */
601__isl_give isl_ast_expr *isl_ast_expr_and(__isl_take isl_ast_expr *expr1,
602 __isl_take isl_ast_expr *expr2)
603{
604 return isl_ast_expr_alloc_binary(isl_ast_op_and, expr1, expr2);
605}
606
607/* Create an expression representing the conjunction of "expr1" and "expr2",
608 * where "expr2" is evaluated only if "expr1" is evaluated to true.
609 */
610__isl_give isl_ast_expr *isl_ast_expr_and_then(__isl_take isl_ast_expr *expr1,
611 __isl_take isl_ast_expr *expr2)
612{
613 return isl_ast_expr_alloc_binary(isl_ast_op_and_then, expr1, expr2);
614}
615
616/* Create an expression representing the disjunction of "expr1" and "expr2".
617 */
618__isl_give isl_ast_expr *isl_ast_expr_or(__isl_take isl_ast_expr *expr1,
619 __isl_take isl_ast_expr *expr2)
620{
621 return isl_ast_expr_alloc_binary(isl_ast_op_or, expr1, expr2);
622}
623
624/* Create an expression representing the disjunction of "expr1" and "expr2",
625 * where "expr2" is evaluated only if "expr1" is evaluated to false.
626 */
627__isl_give isl_ast_expr *isl_ast_expr_or_else(__isl_take isl_ast_expr *expr1,
628 __isl_take isl_ast_expr *expr2)
629{
630 return isl_ast_expr_alloc_binary(isl_ast_op_or_else, expr1, expr2);
631}
632
633/* Create an expression representing "expr1" less than or equal to "expr2".
634 */
635__isl_give isl_ast_expr *isl_ast_expr_le(__isl_take isl_ast_expr *expr1,
636 __isl_take isl_ast_expr *expr2)
637{
638 return isl_ast_expr_alloc_binary(isl_ast_op_le, expr1, expr2);
639}
640
641/* Create an expression representing "expr1" less than "expr2".
642 */
643__isl_give isl_ast_expr *isl_ast_expr_lt(__isl_take isl_ast_expr *expr1,
644 __isl_take isl_ast_expr *expr2)
645{
646 return isl_ast_expr_alloc_binary(isl_ast_op_lt, expr1, expr2);
647}
648
649/* Create an expression representing "expr1" greater than or equal to "expr2".
650 */
651__isl_give isl_ast_expr *isl_ast_expr_ge(__isl_take isl_ast_expr *expr1,
652 __isl_take isl_ast_expr *expr2)
653{
654 return isl_ast_expr_alloc_binary(isl_ast_op_ge, expr1, expr2);
655}
656
657/* Create an expression representing "expr1" greater than "expr2".
658 */
659__isl_give isl_ast_expr *isl_ast_expr_gt(__isl_take isl_ast_expr *expr1,
660 __isl_take isl_ast_expr *expr2)
661{
662 return isl_ast_expr_alloc_binary(isl_ast_op_gt, expr1, expr2);
663}
664
665/* Create an expression representing "expr1" equal to "expr2".
666 */
667__isl_give isl_ast_expr *isl_ast_expr_eq(__isl_take isl_ast_expr *expr1,
668 __isl_take isl_ast_expr *expr2)
669{
670 return isl_ast_expr_alloc_binary(isl_ast_op_eq, expr1, expr2);
671}
672
673/* Create an expression of type "type" with as arguments "arg0" followed
674 * by "arguments".
675 */
676static __isl_give isl_ast_expr *ast_expr_with_arguments(
677 enum isl_ast_op_type type, __isl_take isl_ast_expr *arg0,
678 __isl_take isl_ast_expr_list *arguments)
679{
680 int i, n;
681 isl_ctx *ctx;
682 isl_ast_expr *res = NULL((void*)0);
683
684 if (!arg0 || !arguments)
685 goto error;
686
687 ctx = isl_ast_expr_get_ctx(arg0);
688 n = isl_ast_expr_list_n_ast_expr(arguments);
689 res = isl_ast_expr_alloc_op(ctx, type, 1 + n);
690 if (!res)
691 goto error;
692 for (i = 0; i < n; ++i) {
693 isl_ast_expr *arg;
694 arg = isl_ast_expr_list_get_ast_expr(arguments, i);
695 res->u.op.args[1 + i] = arg;
696 if (!arg)
697 goto error;
698 }
699 res->u.op.args[0] = arg0;
700
701 isl_ast_expr_list_free(arguments);
702 return res;
703error:
704 isl_ast_expr_free(arg0);
705 isl_ast_expr_list_free(arguments);
706 isl_ast_expr_free(res);
707 return NULL((void*)0);
708}
709
710/* Create an expression representing an access to "array" with index
711 * expressions "indices".
712 */
713__isl_give isl_ast_expr *isl_ast_expr_access(__isl_take isl_ast_expr *array,
714 __isl_take isl_ast_expr_list *indices)
715{
716 return ast_expr_with_arguments(isl_ast_op_access, array, indices);
717}
718
719/* Create an expression representing a call to "function" with argument
720 * expressions "arguments".
721 */
722__isl_give isl_ast_expr *isl_ast_expr_call(__isl_take isl_ast_expr *function,
723 __isl_take isl_ast_expr_list *arguments)
724{
725 return ast_expr_with_arguments(isl_ast_op_call, function, arguments);
726}
727
728/* For each subexpression of "expr" of type isl_ast_expr_id,
729 * if it appears in "id2expr", then replace it by the corresponding
730 * expression.
731 */
732__isl_give isl_ast_expr *isl_ast_expr_substitute_ids(
733 __isl_take isl_ast_expr *expr, __isl_take isl_id_to_ast_expr *id2expr)
734{
735 int i;
736 isl_maybe_isl_ast_expr m;
737
738 if (!expr || !id2expr)
739 goto error;
740
741 switch (expr->type) {
742 case isl_ast_expr_int:
743 break;
744 case isl_ast_expr_id:
745 m = isl_id_to_ast_expr_try_get(id2expr, expr->u.id);
746 if (m.valid < 0)
747 goto error;
748 if (!m.valid)
749 break;
750 isl_ast_expr_free(expr);
751 expr = m.value;
752 break;
753 case isl_ast_expr_op:
754 for (i = 0; i < expr->u.op.n_arg; ++i) {
755 isl_ast_expr *arg;
756 arg = isl_ast_expr_copy(expr->u.op.args[i]);
757 arg = isl_ast_expr_substitute_ids(arg,
758 isl_id_to_ast_expr_copy(id2expr));
759 if (arg == expr->u.op.args[i]) {
760 isl_ast_expr_free(arg);
761 continue;
762 }
763 if (!arg)
764 expr = isl_ast_expr_free(expr);
765 expr = isl_ast_expr_cow(expr);
766 if (!expr) {
767 isl_ast_expr_free(arg);
768 break;
769 }
770 isl_ast_expr_free(expr->u.op.args[i]);
771 expr->u.op.args[i] = arg;
772 }
773 break;
774 case isl_ast_expr_error:
775 expr = isl_ast_expr_free(expr);
776 break;
777 }
778
779 isl_id_to_ast_expr_free(id2expr);
780 return expr;
781error:
782 isl_ast_expr_free(expr);
783 isl_id_to_ast_expr_free(id2expr);
784 return NULL((void*)0);
785}
786
787isl_ctx *isl_ast_node_get_ctx(__isl_keep isl_ast_node *node)
788{
789 return node ? node->ctx : NULL((void*)0);
790}
791
792enum isl_ast_node_type isl_ast_node_get_type(__isl_keep isl_ast_node *node)
793{
794 return node ? node->type : isl_ast_node_error;
795}
796
797__isl_give isl_ast_node *isl_ast_node_alloc(isl_ctx *ctx,
798 enum isl_ast_node_type type)
799{
800 isl_ast_node *node;
801
802 node = isl_calloc_type(ctx, isl_ast_node)((isl_ast_node *)isl_calloc_or_die(ctx, 1, sizeof(isl_ast_node
)))
;
803 if (!node)
804 return NULL((void*)0);
805
806 node->ctx = ctx;
807 isl_ctx_ref(ctx);
808 node->ref = 1;
809 node->type = type;
810
811 return node;
812}
813
814/* Create an if node with the given guard.
815 *
816 * The then body needs to be filled in later.
817 */
818__isl_give isl_ast_node *isl_ast_node_alloc_if(__isl_take isl_ast_expr *guard)
819{
820 isl_ast_node *node;
821
822 if (!guard)
823 return NULL((void*)0);
824
825 node = isl_ast_node_alloc(isl_ast_expr_get_ctx(guard), isl_ast_node_if);
826 if (!node)
827 goto error;
828 node->u.i.guard = guard;
829
830 return node;
831error:
832 isl_ast_expr_free(guard);
833 return NULL((void*)0);
834}
835
836/* Create a for node with the given iterator.
837 *
838 * The remaining fields need to be filled in later.
839 */
840__isl_give isl_ast_node *isl_ast_node_alloc_for(__isl_take isl_id *id)
841{
842 isl_ast_node *node;
843 isl_ctx *ctx;
844
845 if (!id)
846 return NULL((void*)0);
847
848 ctx = isl_id_get_ctx(id);
849 node = isl_ast_node_alloc(ctx, isl_ast_node_for);
850 if (!node)
851 goto error;
852
853 node->u.f.iterator = isl_ast_expr_from_id(id);
854 if (!node->u.f.iterator)
855 return isl_ast_node_free(node);
856
857 return node;
858error:
859 isl_id_free(id);
860 return NULL((void*)0);
861}
862
863/* Create a mark node, marking "node" with "id".
864 */
865__isl_give isl_ast_node *isl_ast_node_alloc_mark(__isl_take isl_id *id,
866 __isl_take isl_ast_node *node)
867{
868 isl_ctx *ctx;
869 isl_ast_node *mark;
870
871 if (!id || !node)
872 goto error;
873
874 ctx = isl_id_get_ctx(id);
875 mark = isl_ast_node_alloc(ctx, isl_ast_node_mark);
876 if (!mark)
877 goto error;
878
879 mark->u.m.mark = id;
880 mark->u.m.node = node;
881
882 return mark;
883error:
884 isl_id_free(id);
885 isl_ast_node_free(node);
886 return NULL((void*)0);
887}
888
889/* Create a user node evaluating "expr".
890 */
891__isl_give isl_ast_node *isl_ast_node_alloc_user(__isl_take isl_ast_expr *expr)
892{
893 isl_ctx *ctx;
894 isl_ast_node *node;
895
896 if (!expr)
897 return NULL((void*)0);
898
899 ctx = isl_ast_expr_get_ctx(expr);
900 node = isl_ast_node_alloc(ctx, isl_ast_node_user);
901 if (!node)
902 goto error;
903
904 node->u.e.expr = expr;
905
906 return node;
907error:
908 isl_ast_expr_free(expr);
909 return NULL((void*)0);
910}
911
912/* Create a block node with the given children.
913 */
914__isl_give isl_ast_node *isl_ast_node_alloc_block(
915 __isl_take isl_ast_node_list *list)
916{
917 isl_ast_node *node;
918 isl_ctx *ctx;
919
920 if (!list)
921 return NULL((void*)0);
922
923 ctx = isl_ast_node_list_get_ctx(list);
924 node = isl_ast_node_alloc(ctx, isl_ast_node_block);
925 if (!node)
926 goto error;
927
928 node->u.b.children = list;
929
930 return node;
931error:
932 isl_ast_node_list_free(list);
933 return NULL((void*)0);
934}
935
936/* Represent the given list of nodes as a single node, either by
937 * extract the node from a single element list or by creating
938 * a block node with the list of nodes as children.
939 */
940__isl_give isl_ast_node *isl_ast_node_from_ast_node_list(
941 __isl_take isl_ast_node_list *list)
942{
943 isl_ast_node *node;
944
945 if (isl_ast_node_list_n_ast_node(list) != 1)
946 return isl_ast_node_alloc_block(list);
947
948 node = isl_ast_node_list_get_ast_node(list, 0);
949 isl_ast_node_list_free(list);
950
951 return node;
952}
953
954__isl_give isl_ast_node *isl_ast_node_copy(__isl_keep isl_ast_node *node)
955{
956 if (!node)
957 return NULL((void*)0);
958
959 node->ref++;
960 return node;
961}
962
963__isl_give isl_ast_node *isl_ast_node_dup(__isl_keep isl_ast_node *node)
964{
965 isl_ast_node *dup;
966
967 if (!node)
968 return NULL((void*)0);
969
970 dup = isl_ast_node_alloc(isl_ast_node_get_ctx(node), node->type);
971 if (!dup)
972 return NULL((void*)0);
973
974 switch (node->type) {
975 case isl_ast_node_if:
976 dup->u.i.guard = isl_ast_expr_copy(node->u.i.guard);
977 dup->u.i.then = isl_ast_node_copy(node->u.i.then);
978 dup->u.i.else_node = isl_ast_node_copy(node->u.i.else_node);
979 if (!dup->u.i.guard || !dup->u.i.then ||
980 (node->u.i.else_node && !dup->u.i.else_node))
981 return isl_ast_node_free(dup);
982 break;
983 case isl_ast_node_for:
984 dup->u.f.iterator = isl_ast_expr_copy(node->u.f.iterator);
985 dup->u.f.init = isl_ast_expr_copy(node->u.f.init);
986 dup->u.f.cond = isl_ast_expr_copy(node->u.f.cond);
987 dup->u.f.inc = isl_ast_expr_copy(node->u.f.inc);
988 dup->u.f.body = isl_ast_node_copy(node->u.f.body);
989 if (!dup->u.f.iterator || !dup->u.f.init || !dup->u.f.cond ||
990 !dup->u.f.inc || !dup->u.f.body)
991 return isl_ast_node_free(dup);
992 break;
993 case isl_ast_node_block:
994 dup->u.b.children = isl_ast_node_list_copy(node->u.b.children);
995 if (!dup->u.b.children)
996 return isl_ast_node_free(dup);
997 break;
998 case isl_ast_node_mark:
999 dup->u.m.mark = isl_id_copy(node->u.m.mark);
1000 dup->u.m.node = isl_ast_node_copy(node->u.m.node);
1001 if (!dup->u.m.mark || !dup->u.m.node)
1002 return isl_ast_node_free(dup);
1003 break;
1004 case isl_ast_node_user:
1005 dup->u.e.expr = isl_ast_expr_copy(node->u.e.expr);
1006 if (!dup->u.e.expr)
1007 return isl_ast_node_free(dup);
1008 break;
1009 case isl_ast_node_error:
1010 break;
1011 }
1012
1013 return dup;
1014}
1015
1016__isl_give isl_ast_node *isl_ast_node_cow(__isl_take isl_ast_node *node)
1017{
1018 if (!node)
1019 return NULL((void*)0);
1020
1021 if (node->ref == 1)
1022 return node;
1023 node->ref--;
1024 return isl_ast_node_dup(node);
1025}
1026
1027__isl_null isl_ast_node *isl_ast_node_free(__isl_take isl_ast_node *node)
1028{
1029 if (!node)
1030 return NULL((void*)0);
1031
1032 if (--node->ref > 0)
1033 return NULL((void*)0);
1034
1035 switch (node->type) {
1036 case isl_ast_node_if:
1037 isl_ast_expr_free(node->u.i.guard);
1038 isl_ast_node_free(node->u.i.then);
1039 isl_ast_node_free(node->u.i.else_node);
1040 break;
1041 case isl_ast_node_for:
1042 isl_ast_expr_free(node->u.f.iterator);
1043 isl_ast_expr_free(node->u.f.init);
1044 isl_ast_expr_free(node->u.f.cond);
1045 isl_ast_expr_free(node->u.f.inc);
1046 isl_ast_node_free(node->u.f.body);
1047 break;
1048 case isl_ast_node_block:
1049 isl_ast_node_list_free(node->u.b.children);
1050 break;
1051 case isl_ast_node_mark:
1052 isl_id_free(node->u.m.mark);
1053 isl_ast_node_free(node->u.m.node);
1054 break;
1055 case isl_ast_node_user:
1056 isl_ast_expr_free(node->u.e.expr);
1057 break;
1058 case isl_ast_node_error:
1059 break;
1060 }
1061
1062 isl_id_free(node->annotation);
1063 isl_ctx_deref(node->ctx);
1064 free(node);
1065
1066 return NULL((void*)0);
1067}
1068
1069/* Replace the body of the for node "node" by "body".
1070 */
1071__isl_give isl_ast_node *isl_ast_node_for_set_body(
1072 __isl_take isl_ast_node *node, __isl_take isl_ast_node *body)
1073{
1074 node = isl_ast_node_cow(node);
1075 if (!node || !body)
1076 goto error;
1077 if (node->type != isl_ast_node_for)
1078 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a for node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1079); goto error; } while (0)
1079 "not a for node", goto error)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a for node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1079); goto error; } while (0)
;
1080
1081 isl_ast_node_free(node->u.f.body);
1082 node->u.f.body = body;
1083
1084 return node;
1085error:
1086 isl_ast_node_free(node);
1087 isl_ast_node_free(body);
1088 return NULL((void*)0);
1089}
1090
1091__isl_give isl_ast_node *isl_ast_node_for_get_body(
1092 __isl_keep isl_ast_node *node)
1093{
1094 if (!node)
1095 return NULL((void*)0);
1096 if (node->type != isl_ast_node_for)
1097 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a for node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1098); return ((void*)0); } while (0)
1098 "not a for node", return NULL)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a for node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1098); return ((void*)0); } while (0)
;
1099 return isl_ast_node_copy(node->u.f.body);
1100}
1101
1102/* Mark the given for node as being degenerate.
1103 */
1104__isl_give isl_ast_node *isl_ast_node_for_mark_degenerate(
1105 __isl_take isl_ast_node *node)
1106{
1107 node = isl_ast_node_cow(node);
1108 if (!node)
1109 return NULL((void*)0);
1110 node->u.f.degenerate = 1;
1111 return node;
1112}
1113
1114isl_bool isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node *node)
1115{
1116 if (!node)
1117 return isl_bool_error;
1118 if (node->type != isl_ast_node_for)
1119 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a for node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1120); return isl_bool_error; } while (0)
1120 "not a for node", return isl_bool_error)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a for node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1120); return isl_bool_error; } while (0)
;
1121 return node->u.f.degenerate;
1122}
1123
1124__isl_give isl_ast_expr *isl_ast_node_for_get_iterator(
1125 __isl_keep isl_ast_node *node)
1126{
1127 if (!node)
1128 return NULL((void*)0);
1129 if (node->type != isl_ast_node_for)
1130 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a for node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1131); return ((void*)0); } while (0)
1131 "not a for node", return NULL)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a for node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1131); return ((void*)0); } while (0)
;
1132 return isl_ast_expr_copy(node->u.f.iterator);
1133}
1134
1135__isl_give isl_ast_expr *isl_ast_node_for_get_init(
1136 __isl_keep isl_ast_node *node)
1137{
1138 if (!node)
1139 return NULL((void*)0);
1140 if (node->type != isl_ast_node_for)
1141 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a for node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1142); return ((void*)0); } while (0)
1142 "not a for node", return NULL)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a for node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1142); return ((void*)0); } while (0)
;
1143 return isl_ast_expr_copy(node->u.f.init);
1144}
1145
1146/* Return the condition expression of the given for node.
1147 *
1148 * If the for node is degenerate, then the condition is not explicitly
1149 * stored in the node. Instead, it is constructed as
1150 *
1151 * iterator <= init
1152 */
1153__isl_give isl_ast_expr *isl_ast_node_for_get_cond(
1154 __isl_keep isl_ast_node *node)
1155{
1156 if (!node)
1157 return NULL((void*)0);
1158 if (node->type != isl_ast_node_for)
1159 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a for node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1160); return ((void*)0); } while (0)
1160 "not a for node", return NULL)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a for node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1160); return ((void*)0); } while (0)
;
1161 if (!node->u.f.degenerate)
1162 return isl_ast_expr_copy(node->u.f.cond);
1163
1164 return isl_ast_expr_alloc_binary(isl_ast_op_le,
1165 isl_ast_expr_copy(node->u.f.iterator),
1166 isl_ast_expr_copy(node->u.f.init));
1167}
1168
1169/* Return the increment of the given for node.
1170 *
1171 * If the for node is degenerate, then the increment is not explicitly
1172 * stored in the node. We simply return "1".
1173 */
1174__isl_give isl_ast_expr *isl_ast_node_for_get_inc(
1175 __isl_keep isl_ast_node *node)
1176{
1177 if (!node)
1178 return NULL((void*)0);
1179 if (node->type != isl_ast_node_for)
1180 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a for node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1181); return ((void*)0); } while (0)
1181 "not a for node", return NULL)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a for node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1181); return ((void*)0); } while (0)
;
1182 if (!node->u.f.degenerate)
1183 return isl_ast_expr_copy(node->u.f.inc);
1184 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node), 1);
1185}
1186
1187/* Replace the then branch of the if node "node" by "child".
1188 */
1189__isl_give isl_ast_node *isl_ast_node_if_set_then(
1190 __isl_take isl_ast_node *node, __isl_take isl_ast_node *child)
1191{
1192 node = isl_ast_node_cow(node);
1193 if (!node || !child)
1194 goto error;
1195 if (node->type != isl_ast_node_if)
1196 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not an if node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1197); goto error; } while (0)
1197 "not an if node", goto error)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not an if node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1197); goto error; } while (0)
;
1198
1199 isl_ast_node_free(node->u.i.then);
1200 node->u.i.then = child;
1201
1202 return node;
1203error:
1204 isl_ast_node_free(node);
1205 isl_ast_node_free(child);
1206 return NULL((void*)0);
1207}
1208
1209__isl_give isl_ast_node *isl_ast_node_if_get_then(
1210 __isl_keep isl_ast_node *node)
1211{
1212 if (!node)
1213 return NULL((void*)0);
1214 if (node->type != isl_ast_node_if)
1215 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not an if node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1216); return ((void*)0); } while (0)
1216 "not an if node", return NULL)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not an if node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1216); return ((void*)0); } while (0)
;
1217 return isl_ast_node_copy(node->u.i.then);
1218}
1219
1220isl_bool isl_ast_node_if_has_else(
1221 __isl_keep isl_ast_node *node)
1222{
1223 if (!node)
1224 return isl_bool_error;
1225 if (node->type != isl_ast_node_if)
1226 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not an if node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1227); return isl_bool_error; } while (0)
1227 "not an if node", return isl_bool_error)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not an if node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1227); return isl_bool_error; } while (0)
;
1228 return node->u.i.else_node != NULL((void*)0);
1229}
1230
1231__isl_give isl_ast_node *isl_ast_node_if_get_else(
1232 __isl_keep isl_ast_node *node)
1233{
1234 if (!node)
1235 return NULL((void*)0);
1236 if (node->type != isl_ast_node_if)
1237 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not an if node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1238); return ((void*)0); } while (0)
1238 "not an if node", return NULL)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not an if node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1238); return ((void*)0); } while (0)
;
1239 return isl_ast_node_copy(node->u.i.else_node);
1240}
1241
1242__isl_give isl_ast_expr *isl_ast_node_if_get_cond(
1243 __isl_keep isl_ast_node *node)
1244{
1245 if (!node)
1246 return NULL((void*)0);
1247 if (node->type != isl_ast_node_if)
1248 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a guard node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1249); return ((void*)0); } while (0)
1249 "not a guard node", return NULL)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a guard node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1249); return ((void*)0); } while (0)
;
1250 return isl_ast_expr_copy(node->u.i.guard);
1251}
1252
1253__isl_give isl_ast_node_list *isl_ast_node_block_get_children(
1254 __isl_keep isl_ast_node *node)
1255{
1256 if (!node)
1257 return NULL((void*)0);
1258 if (node->type != isl_ast_node_block)
1259 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a block node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1260); return ((void*)0); } while (0)
1260 "not a block node", return NULL)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a block node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1260); return ((void*)0); } while (0)
;
1261 return isl_ast_node_list_copy(node->u.b.children);
1262}
1263
1264__isl_give isl_ast_expr *isl_ast_node_user_get_expr(
1265 __isl_keep isl_ast_node *node)
1266{
1267 if (!node)
1268 return NULL((void*)0);
1269 if (node->type != isl_ast_node_user)
1270 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a user node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1271); return ((void*)0); } while (0)
1271 "not a user node", return NULL)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a user node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1271); return ((void*)0); } while (0)
;
1272
1273 return isl_ast_expr_copy(node->u.e.expr);
1274}
1275
1276/* Return the mark identifier of the mark node "node".
1277 */
1278__isl_give isl_id *isl_ast_node_mark_get_id(__isl_keep isl_ast_node *node)
1279{
1280 if (!node)
1281 return NULL((void*)0);
1282 if (node->type != isl_ast_node_mark)
1283 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a mark node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1284); return ((void*)0); } while (0)
1284 "not a mark node", return NULL)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a mark node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1284); return ((void*)0); } while (0)
;
1285
1286 return isl_id_copy(node->u.m.mark);
1287}
1288
1289/* Return the node marked by mark node "node".
1290 */
1291__isl_give isl_ast_node *isl_ast_node_mark_get_node(
1292 __isl_keep isl_ast_node *node)
1293{
1294 if (!node)
1295 return NULL((void*)0);
1296 if (node->type != isl_ast_node_mark)
1297 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a mark node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1298); return ((void*)0); } while (0)
1298 "not a mark node", return NULL)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a mark node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1298); return ((void*)0); } while (0)
;
1299
1300 return isl_ast_node_copy(node->u.m.node);
1301}
1302
1303__isl_give isl_id *isl_ast_node_get_annotation(__isl_keep isl_ast_node *node)
1304{
1305 return node ? isl_id_copy(node->annotation) : NULL((void*)0);
1306}
1307
1308/* Replace node->annotation by "annotation".
1309 */
1310__isl_give isl_ast_node *isl_ast_node_set_annotation(
1311 __isl_take isl_ast_node *node, __isl_take isl_id *annotation)
1312{
1313 node = isl_ast_node_cow(node);
1314 if (!node || !annotation)
1315 goto error;
1316
1317 isl_id_free(node->annotation);
1318 node->annotation = annotation;
1319
1320 return node;
1321error:
1322 isl_id_free(annotation);
1323 return isl_ast_node_free(node);
1324}
1325
1326/* Traverse the elements of "list" and all their descendants
1327 * in depth first preorder.
1328 *
1329 * Return isl_stat_ok on success and isl_stat_error on failure.
1330 */
1331static isl_stat nodelist_foreach(__isl_keep isl_ast_node_list *list,
1332 isl_bool (*fn)(__isl_keep isl_ast_node *node, void *user), void *user)
1333{
1334 int i;
1335
1336 if (!list)
1337 return isl_stat_error;
1338
1339 for (i = 0; i < list->n; ++i) {
1340 isl_stat ok;
1341 isl_ast_node *node = list->p[i];
1342
1343 ok = isl_ast_node_foreach_descendant_top_down(node, fn, user);
1344 if (ok < 0)
1345 return isl_stat_error;
1346 }
1347
1348 return isl_stat_ok;
1349}
1350
1351/* Traverse the descendants of "node" (including the node itself)
1352 * in depth first preorder.
1353 *
1354 * If "fn" returns isl_bool_error on any of the nodes, then the traversal
1355 * is aborted.
1356 * If "fn" returns isl_bool_false on any of the nodes, then the subtree rooted
1357 * at that node is skipped.
1358 *
1359 * Return isl_stat_ok on success and isl_stat_error on failure.
1360 */
1361isl_stat isl_ast_node_foreach_descendant_top_down(
1362 __isl_keep isl_ast_node *node,
1363 isl_bool (*fn)(__isl_keep isl_ast_node *node, void *user), void *user)
1364{
1365 isl_bool more;
1366 isl_stat ok;
1367
1368 if (!node)
1369 return isl_stat_error;
1370
1371 more = fn(node, user);
1372 if (more < 0)
1373 return isl_stat_error;
1374 if (!more)
1375 return isl_stat_ok;
1376
1377 switch (node->type) {
1378 case isl_ast_node_for:
1379 node = node->u.f.body;
1380 return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1381 case isl_ast_node_if:
1382 ok = isl_ast_node_foreach_descendant_top_down(node->u.i.then,
1383 fn, user);
1384 if (ok < 0)
1385 return isl_stat_error;
1386 if (!node->u.i.else_node)
1387 return isl_stat_ok;
1388 node = node->u.i.else_node;
1389 return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1390 case isl_ast_node_block:
1391 return nodelist_foreach(node->u.b.children, fn, user);
1392 case isl_ast_node_mark:
1393 node = node->u.m.node;
1394 return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1395 case isl_ast_node_user:
1396 break;
1397 case isl_ast_node_error:
1398 return isl_stat_error;
1399 }
1400
1401 return isl_stat_ok;
1402}
1403
1404/* Textual C representation of the various operators.
1405 */
1406static char *op_str_c[] = {
1407 [isl_ast_op_and] = "&&",
1408 [isl_ast_op_and_then] = "&&",
1409 [isl_ast_op_or] = "||",
1410 [isl_ast_op_or_else] = "||",
1411 [isl_ast_op_max] = "max",
1412 [isl_ast_op_min] = "min",
1413 [isl_ast_op_minus] = "-",
1414 [isl_ast_op_add] = "+",
1415 [isl_ast_op_sub] = "-",
1416 [isl_ast_op_mul] = "*",
1417 [isl_ast_op_fdiv_q] = "floord",
1418 [isl_ast_op_pdiv_q] = "/",
1419 [isl_ast_op_pdiv_r] = "%",
1420 [isl_ast_op_zdiv_r] = "%",
1421 [isl_ast_op_div] = "/",
1422 [isl_ast_op_eq] = "==",
1423 [isl_ast_op_le] = "<=",
1424 [isl_ast_op_ge] = ">=",
1425 [isl_ast_op_lt] = "<",
1426 [isl_ast_op_gt] = ">",
1427 [isl_ast_op_member] = ".",
1428 [isl_ast_op_address_of] = "&"
1429};
1430
1431/* Precedence in C of the various operators.
1432 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1433 * Lowest value means highest precedence.
1434 */
1435static int op_prec[] = {
1436 [isl_ast_op_and] = 13,
1437 [isl_ast_op_and_then] = 13,
1438 [isl_ast_op_or] = 14,
1439 [isl_ast_op_or_else] = 14,
1440 [isl_ast_op_max] = 2,
1441 [isl_ast_op_min] = 2,
1442 [isl_ast_op_minus] = 3,
1443 [isl_ast_op_add] = 6,
1444 [isl_ast_op_sub] = 6,
1445 [isl_ast_op_mul] = 5,
1446 [isl_ast_op_div] = 5,
1447 [isl_ast_op_fdiv_q] = 2,
1448 [isl_ast_op_pdiv_q] = 5,
1449 [isl_ast_op_pdiv_r] = 5,
1450 [isl_ast_op_zdiv_r] = 5,
1451 [isl_ast_op_cond] = 15,
1452 [isl_ast_op_select] = 15,
1453 [isl_ast_op_eq] = 9,
1454 [isl_ast_op_le] = 8,
1455 [isl_ast_op_ge] = 8,
1456 [isl_ast_op_lt] = 8,
1457 [isl_ast_op_gt] = 8,
1458 [isl_ast_op_call] = 2,
1459 [isl_ast_op_access] = 2,
1460 [isl_ast_op_member] = 2,
1461 [isl_ast_op_address_of] = 3
1462};
1463
1464/* Is the operator left-to-right associative?
1465 */
1466static int op_left[] = {
1467 [isl_ast_op_and] = 1,
1468 [isl_ast_op_and_then] = 1,
1469 [isl_ast_op_or] = 1,
1470 [isl_ast_op_or_else] = 1,
1471 [isl_ast_op_max] = 1,
1472 [isl_ast_op_min] = 1,
1473 [isl_ast_op_minus] = 0,
1474 [isl_ast_op_add] = 1,
1475 [isl_ast_op_sub] = 1,
1476 [isl_ast_op_mul] = 1,
1477 [isl_ast_op_div] = 1,
1478 [isl_ast_op_fdiv_q] = 1,
1479 [isl_ast_op_pdiv_q] = 1,
1480 [isl_ast_op_pdiv_r] = 1,
1481 [isl_ast_op_zdiv_r] = 1,
1482 [isl_ast_op_cond] = 0,
1483 [isl_ast_op_select] = 0,
1484 [isl_ast_op_eq] = 1,
1485 [isl_ast_op_le] = 1,
1486 [isl_ast_op_ge] = 1,
1487 [isl_ast_op_lt] = 1,
1488 [isl_ast_op_gt] = 1,
1489 [isl_ast_op_call] = 1,
1490 [isl_ast_op_access] = 1,
1491 [isl_ast_op_member] = 1,
1492 [isl_ast_op_address_of] = 0
1493};
1494
1495static int is_and(enum isl_ast_op_type op)
1496{
1497 return op == isl_ast_op_and || op == isl_ast_op_and_then;
1498}
1499
1500static int is_or(enum isl_ast_op_type op)
1501{
1502 return op == isl_ast_op_or || op == isl_ast_op_or_else;
1503}
1504
1505static int is_add_sub(enum isl_ast_op_type op)
1506{
1507 return op == isl_ast_op_add || op == isl_ast_op_sub;
1508}
1509
1510static int is_div_mod(enum isl_ast_op_type op)
1511{
1512 return op == isl_ast_op_div ||
1513 op == isl_ast_op_pdiv_r ||
1514 op == isl_ast_op_zdiv_r;
1515}
1516
1517static __isl_give isl_printer *print_ast_expr_c(__isl_take isl_printer *p,
1518 __isl_keep isl_ast_expr *expr);
1519
1520/* Do we need/want parentheses around "expr" as a subexpression of
1521 * an "op" operation? If "left" is set, then "expr" is the left-most
1522 * operand.
1523 *
1524 * We only need parentheses if "expr" represents an operation.
1525 *
1526 * If op has a higher precedence than expr->u.op.op, then we need
1527 * parentheses.
1528 * If op and expr->u.op.op have the same precedence, but the operations
1529 * are performed in an order that is different from the associativity,
1530 * then we need parentheses.
1531 *
1532 * An and inside an or technically does not require parentheses,
1533 * but some compilers complain about that, so we add them anyway.
1534 *
1535 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1536 * difficult to read, so we add parentheses for those as well.
1537 */
1538static int sub_expr_need_parens(enum isl_ast_op_type op,
1539 __isl_keep isl_ast_expr *expr, int left)
1540{
1541 if (expr->type != isl_ast_expr_op)
1542 return 0;
1543
1544 if (op_prec[expr->u.op.op] > op_prec[op])
1545 return 1;
1546 if (op_prec[expr->u.op.op] == op_prec[op] && left != op_left[op])
1547 return 1;
1548
1549 if (is_or(op) && is_and(expr->u.op.op))
1550 return 1;
1551 if (op == isl_ast_op_mul && expr->u.op.op != isl_ast_op_mul &&
1552 op_prec[expr->u.op.op] == op_prec[op])
1553 return 1;
1554 if (is_add_sub(op) && is_div_mod(expr->u.op.op))
1555 return 1;
1556
1557 return 0;
1558}
1559
1560/* Print "expr" as a subexpression of an "op" operation in C format.
1561 * If "left" is set, then "expr" is the left-most operand.
1562 */
1563static __isl_give isl_printer *print_sub_expr_c(__isl_take isl_printer *p,
1564 enum isl_ast_op_type op, __isl_keep isl_ast_expr *expr, int left)
1565{
1566 int need_parens;
1567
1568 need_parens = sub_expr_need_parens(op, expr, left);
1569
1570 if (need_parens)
1571 p = isl_printer_print_str(p, "(");
1572 p = print_ast_expr_c(p, expr);
1573 if (need_parens)
1574 p = isl_printer_print_str(p, ")");
1575 return p;
1576}
1577
1578#define isl_ast_op_lastisl_ast_op_address_of isl_ast_op_address_of
1579
1580/* Data structure that holds the user-specified textual
1581 * representations for the operators in C format.
1582 * The entries are either NULL or copies of strings.
1583 * A NULL entry means that the default name should be used.
1584 */
1585struct isl_ast_op_names {
1586 char *op_str[isl_ast_op_lastisl_ast_op_address_of + 1];
1587};
1588
1589/* Create an empty struct isl_ast_op_names.
1590 */
1591static void *create_names(isl_ctx *ctx)
1592{
1593 return isl_calloc_type(ctx, struct isl_ast_op_names)((struct isl_ast_op_names *)isl_calloc_or_die(ctx, 1, sizeof(
struct isl_ast_op_names)))
;
1594}
1595
1596/* Free a struct isl_ast_op_names along with all memory
1597 * owned by the struct.
1598 */
1599static void free_names(void *user)
1600{
1601 int i;
1602 struct isl_ast_op_names *names = user;
1603
1604 if (!user)
1605 return;
1606
1607 for (i = 0; i <= isl_ast_op_lastisl_ast_op_address_of; ++i)
1608 free(names->op_str[i]);
1609 free(user);
1610}
1611
1612/* Create an identifier that is used to store
1613 * an isl_ast_op_names note.
1614 */
1615static __isl_give isl_id *names_id(isl_ctx *ctx)
1616{
1617 return isl_id_alloc(ctx, "isl_ast_op_type_names", NULL((void*)0));
1618}
1619
1620/* Ensure that "p" has a note identified by "id".
1621 * If there is no such note yet, then it is created by "note_create" and
1622 * scheduled do be freed by "note_free".
1623 */
1624static __isl_give isl_printer *alloc_note(__isl_take isl_printer *p,
1625 __isl_keep isl_id *id, void *(*note_create)(isl_ctx *),
1626 void (*note_free)(void *))
1627{
1628 isl_ctx *ctx;
1629 isl_id *note_id;
1630 isl_bool has_note;
1631 void *note;
1632
1633 has_note = isl_printer_has_note(p, id);
1634 if (has_note < 0)
1635 return isl_printer_free(p);
1636 if (has_note)
1637 return p;
1638
1639 ctx = isl_printer_get_ctx(p);
1640 note = note_create(ctx);
1641 if (!note)
1642 return isl_printer_free(p);
1643 note_id = isl_id_alloc(ctx, NULL((void*)0), note);
1644 if (!note_id)
1645 note_free(note);
1646 else
1647 note_id = isl_id_set_free_user(note_id, note_free);
1648
1649 p = isl_printer_set_note(p, isl_id_copy(id), note_id);
1650
1651 return p;
1652}
1653
1654/* Ensure that "p" has an isl_ast_op_names note identified by "id".
1655 */
1656static __isl_give isl_printer *alloc_names(__isl_take isl_printer *p,
1657 __isl_keep isl_id *id)
1658{
1659 return alloc_note(p, id, &create_names, &free_names);
1660}
1661
1662/* Retrieve the note identified by "id" from "p".
1663 * The note is assumed to exist.
1664 */
1665static void *get_note(__isl_keep isl_printer *p, __isl_keep isl_id *id)
1666{
1667 void *note;
1668
1669 id = isl_printer_get_note(p, isl_id_copy(id));
1670 note = isl_id_get_user(id);
1671 isl_id_free(id);
1672
1673 return note;
1674}
1675
1676/* Use "name" to print operations of type "type" to "p".
1677 *
1678 * Store the name in an isl_ast_op_names note attached to "p", such that
1679 * it can be retrieved by get_op_str.
1680 */
1681__isl_give isl_printer *isl_ast_op_type_set_print_name(
1682 __isl_take isl_printer *p, enum isl_ast_op_type type,
1683 __isl_keep const char *name)
1684{
1685 isl_id *id;
1686 struct isl_ast_op_names *names;
1687
1688 if (!p)
1689 return NULL((void*)0);
1690 if (type > isl_ast_op_lastisl_ast_op_address_of)
1691 isl_die(isl_printer_get_ctx(p), isl_error_invalid,do { isl_handle_error(isl_printer_get_ctx(p), isl_error_invalid
, "invalid type", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1692); return isl_printer_free(p); } while (0)
1692 "invalid type", return isl_printer_free(p))do { isl_handle_error(isl_printer_get_ctx(p), isl_error_invalid
, "invalid type", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1692); return isl_printer_free(p); } while (0)
;
1693
1694 id = names_id(isl_printer_get_ctx(p));
1695 p = alloc_names(p, id);
1696 names = get_note(p, id);
1697 isl_id_free(id);
1698 if (!names)
1699 return isl_printer_free(p);
1700 free(names->op_str[type]);
1701 names->op_str[type] = strdup(name)(__extension__ (__builtin_constant_p (name) && ((size_t
)(const void *)((name) + 1) - (size_t)(const void *)(name) ==
1) ? (((const char *) (name))[0] == '\0' ? (char *) calloc (
(size_t) 1, (size_t) 1) : ({ size_t __len = strlen (name) + 1
; char *__retval = (char *) malloc (__len); if (__retval != (
(void*)0)) __retval = (char *) memcpy (__retval, name, __len)
; __retval; })) : __strdup (name)))
;
1702
1703 return p;
1704}
1705
1706/* Return the textual representation of "type" in C format.
1707 *
1708 * If there is a user-specified name in an isl_ast_op_names note
1709 * associated to "p", then return that.
1710 * Otherwise, return the default name in op_str.
1711 */
1712static const char *get_op_str_c(__isl_keep isl_printer *p,
1713 enum isl_ast_op_type type)
1714{
1715 isl_id *id;
1716 isl_bool has_names;
1717 struct isl_ast_op_names *names = NULL((void*)0);
1718
1719 id = names_id(isl_printer_get_ctx(p));
1720 has_names = isl_printer_has_note(p, id);
1721 if (has_names >= 0 && has_names)
1722 names = get_note(p, id);
1723 isl_id_free(id);
1724 if (names && names->op_str[type])
1725 return names->op_str[type];
1726 return op_str_c[type];
1727}
1728
1729/* Print a min or max reduction "expr" in C format.
1730 */
1731static __isl_give isl_printer *print_min_max_c(__isl_take isl_printer *p,
1732 __isl_keep isl_ast_expr *expr)
1733{
1734 int i = 0;
1735
1736 for (i = 1; i < expr->u.op.n_arg; ++i) {
1737 p = isl_printer_print_str(p, get_op_str_c(p, expr->u.op.op));
1738 p = isl_printer_print_str(p, "(");
1739 }
1740 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1741 for (i = 1; i < expr->u.op.n_arg; ++i) {
1742 p = isl_printer_print_str(p, ", ");
1743 p = print_ast_expr_c(p, expr->u.op.args[i]);
1744 p = isl_printer_print_str(p, ")");
1745 }
1746
1747 return p;
1748}
1749
1750/* Print a function call "expr" in C format.
1751 *
1752 * The first argument represents the function to be called.
1753 */
1754static __isl_give isl_printer *print_call_c(__isl_take isl_printer *p,
1755 __isl_keep isl_ast_expr *expr)
1756{
1757 int i = 0;
1758
1759 p = print_ast_expr_c(p, expr->u.op.args[0]);
1760 p = isl_printer_print_str(p, "(");
1761 for (i = 1; i < expr->u.op.n_arg; ++i) {
1762 if (i != 1)
1763 p = isl_printer_print_str(p, ", ");
1764 p = print_ast_expr_c(p, expr->u.op.args[i]);
1765 }
1766 p = isl_printer_print_str(p, ")");
1767
1768 return p;
1769}
1770
1771/* Print an array access "expr" in C format.
1772 *
1773 * The first argument represents the array being accessed.
1774 */
1775static __isl_give isl_printer *print_access_c(__isl_take isl_printer *p,
1776 __isl_keep isl_ast_expr *expr)
1777{
1778 int i = 0;
1779
1780 p = print_ast_expr_c(p, expr->u.op.args[0]);
1781 for (i = 1; i < expr->u.op.n_arg; ++i) {
1782 p = isl_printer_print_str(p, "[");
1783 p = print_ast_expr_c(p, expr->u.op.args[i]);
1784 p = isl_printer_print_str(p, "]");
1785 }
1786
1787 return p;
1788}
1789
1790/* Print "expr" to "p" in C format.
1791 */
1792static __isl_give isl_printer *print_ast_expr_c(__isl_take isl_printer *p,
1793 __isl_keep isl_ast_expr *expr)
1794{
1795 if (!p)
1796 return NULL((void*)0);
1797 if (!expr)
1798 return isl_printer_free(p);
1799
1800 switch (expr->type) {
1801 case isl_ast_expr_op:
1802 if (expr->u.op.op == isl_ast_op_call) {
1803 p = print_call_c(p, expr);
1804 break;
1805 }
1806 if (expr->u.op.op == isl_ast_op_access) {
1807 p = print_access_c(p, expr);
1808 break;
1809 }
1810 if (expr->u.op.n_arg == 1) {
1811 p = isl_printer_print_str(p,
1812 get_op_str_c(p, expr->u.op.op));
1813 p = print_sub_expr_c(p, expr->u.op.op,
1814 expr->u.op.args[0], 0);
1815 break;
1816 }
1817 if (expr->u.op.op == isl_ast_op_fdiv_q) {
1818 const char *name = get_op_str_c(p, isl_ast_op_fdiv_q);
1819 p = isl_printer_print_str(p, name);
1820 p = isl_printer_print_str(p, "(");
1821 p = print_ast_expr_c(p, expr->u.op.args[0]);
1822 p = isl_printer_print_str(p, ", ");
1823 p = print_ast_expr_c(p, expr->u.op.args[1]);
1824 p = isl_printer_print_str(p, ")");
1825 break;
1826 }
1827 if (expr->u.op.op == isl_ast_op_max ||
1828 expr->u.op.op == isl_ast_op_min) {
1829 p = print_min_max_c(p, expr);
1830 break;
1831 }
1832 if (expr->u.op.op == isl_ast_op_cond ||
1833 expr->u.op.op == isl_ast_op_select) {
1834 p = print_ast_expr_c(p, expr->u.op.args[0]);
1835 p = isl_printer_print_str(p, " ? ");
1836 p = print_ast_expr_c(p, expr->u.op.args[1]);
1837 p = isl_printer_print_str(p, " : ");
1838 p = print_ast_expr_c(p, expr->u.op.args[2]);
1839 break;
1840 }
1841 if (expr->u.op.n_arg != 2)
1842 isl_die(isl_printer_get_ctx(p), isl_error_internal,do { isl_handle_error(isl_printer_get_ctx(p), isl_error_internal
, "operation should have two arguments", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1844); return isl_printer_free(p); } while (0)
1843 "operation should have two arguments",do { isl_handle_error(isl_printer_get_ctx(p), isl_error_internal
, "operation should have two arguments", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1844); return isl_printer_free(p); } while (0)
1844 return isl_printer_free(p))do { isl_handle_error(isl_printer_get_ctx(p), isl_error_internal
, "operation should have two arguments", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 1844); return isl_printer_free(p); } while (0)
;
1845 p = print_sub_expr_c(p, expr->u.op.op, expr->u.op.args[0], 1);
1846 if (expr->u.op.op != isl_ast_op_member)
1847 p = isl_printer_print_str(p, " ");
1848 p = isl_printer_print_str(p, get_op_str_c(p, expr->u.op.op));
1849 if (expr->u.op.op != isl_ast_op_member)
1850 p = isl_printer_print_str(p, " ");
1851 p = print_sub_expr_c(p, expr->u.op.op, expr->u.op.args[1], 0);
1852 break;
1853 case isl_ast_expr_id:
1854 p = isl_printer_print_str(p, isl_id_get_name(expr->u.id));
1855 break;
1856 case isl_ast_expr_int:
1857 p = isl_printer_print_val(p, expr->u.v);
1858 break;
1859 case isl_ast_expr_error:
1860 break;
1861 }
1862
1863 return p;
1864}
1865
1866/* Textual representation of the isl_ast_op_type elements
1867 * for use in a YAML representation of an isl_ast_expr.
1868 */
1869static char *op_str[] = {
1870 [isl_ast_op_and] = "and",
1871 [isl_ast_op_and_then] = "and_then",
1872 [isl_ast_op_or] = "or",
1873 [isl_ast_op_or_else] = "or_else",
1874 [isl_ast_op_max] = "max",
1875 [isl_ast_op_min] = "min",
1876 [isl_ast_op_minus] = "minus",
1877 [isl_ast_op_add] = "add",
1878 [isl_ast_op_sub] = "sub",
1879 [isl_ast_op_mul] = "mul",
1880 [isl_ast_op_div] = "div",
1881 [isl_ast_op_fdiv_q] = "fdiv_q",
1882 [isl_ast_op_pdiv_q] = "pdiv_q",
1883 [isl_ast_op_pdiv_r] = "pdiv_r",
1884 [isl_ast_op_zdiv_r] = "zdiv_r",
1885 [isl_ast_op_cond] = "cond",
1886 [isl_ast_op_select] = "select",
1887 [isl_ast_op_eq] = "eq",
1888 [isl_ast_op_le] = "le",
1889 [isl_ast_op_lt] = "lt",
1890 [isl_ast_op_ge] = "ge",
1891 [isl_ast_op_gt] = "gt",
1892 [isl_ast_op_call] = "call",
1893 [isl_ast_op_access] = "access",
1894 [isl_ast_op_member] = "member",
1895 [isl_ast_op_address_of] = "address_of"
1896};
1897
1898static __isl_give isl_printer *print_ast_expr_isl(__isl_take isl_printer *p,
1899 __isl_keep isl_ast_expr *expr);
1900
1901/* Print the arguments of "expr" to "p" in isl format.
1902 *
1903 * If there are no arguments, then nothing needs to be printed.
1904 * Otherwise add an "args" key to the current mapping with as value
1905 * the list of arguments of "expr".
1906 */
1907static __isl_give isl_printer *print_arguments(__isl_take isl_printer *p,
1908 __isl_keep isl_ast_expr *expr)
1909{
1910 int i, n;
1911
1912 n = isl_ast_expr_get_op_n_arg(expr);
1913 if (n < 0)
1914 return isl_printer_free(p);
1915 if (n == 0)
1916 return p;
1917
1918 p = isl_printer_print_str(p, "args");
1919 p = isl_printer_yaml_next(p);
1920 p = isl_printer_yaml_start_sequence(p);
1921 for (i = 0; i < n; ++i) {
1922 isl_ast_expr *arg;
1923
1924 arg = isl_ast_expr_get_op_arg(expr, i);
1925 p = print_ast_expr_isl(p, arg);
1926 isl_ast_expr_free(arg);
1927 p = isl_printer_yaml_next(p);
1928 }
1929 p = isl_printer_yaml_end_sequence(p);
1930
1931 return p;
1932}
1933
1934/* Print "expr" to "p" in isl format.
1935 *
1936 * In particular, print the isl_ast_expr as a YAML document.
1937 */
1938static __isl_give isl_printer *print_ast_expr_isl(__isl_take isl_printer *p,
1939 __isl_keep isl_ast_expr *expr)
1940{
1941 enum isl_ast_expr_type type;
1942 enum isl_ast_op_type op;
1943 isl_id *id;
1944 isl_val *v;
1945
1946 if (!expr)
1947 return isl_printer_free(p);
1948
1949 p = isl_printer_yaml_start_mapping(p);
1950 type = isl_ast_expr_get_type(expr);
1951 switch (type) {
1952 case isl_ast_expr_error:
1953 return isl_printer_free(p);
1954 case isl_ast_expr_op:
1955 op = isl_ast_expr_get_op_type(expr);
1956 if (op == isl_ast_op_error)
1957 return isl_printer_free(p);
1958 p = isl_printer_print_str(p, "op");
1959 p = isl_printer_yaml_next(p);
1960 p = isl_printer_print_str(p, op_str[op]);
1961 p = isl_printer_yaml_next(p);
1962 p = print_arguments(p, expr);
1963 break;
1964 case isl_ast_expr_id:
1965 p = isl_printer_print_str(p, "id");
1966 p = isl_printer_yaml_next(p);
1967 id = isl_ast_expr_get_id(expr);
1968 p = isl_printer_print_id(p, id);
1969 isl_id_free(id);
1970 break;
1971 case isl_ast_expr_int:
1972 p = isl_printer_print_str(p, "val");
1973 p = isl_printer_yaml_next(p);
1974 v = isl_ast_expr_get_val(expr);
1975 p = isl_printer_print_val(p, v);
1976 isl_val_free(v);
1977 break;
1978 }
1979 p = isl_printer_yaml_end_mapping(p);
1980
1981 return p;
1982}
1983
1984/* Print "expr" to "p".
1985 *
1986 * Only an isl and a C format are supported.
1987 */
1988__isl_give isl_printer *isl_printer_print_ast_expr(__isl_take isl_printer *p,
1989 __isl_keep isl_ast_expr *expr)
1990{
1991 int format;
1992
1993 if (!p)
1994 return NULL((void*)0);
1995
1996 format = isl_printer_get_output_format(p);
1997 switch (format) {
1998 case ISL_FORMAT_ISL0:
1999 p = print_ast_expr_isl(p, expr);
2000 break;
2001 case ISL_FORMAT_C4:
2002 p = print_ast_expr_c(p, expr);
2003 break;
2004 default:
2005 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,do { isl_handle_error(isl_printer_get_ctx(p), isl_error_unsupported
, "output format not supported for ast_expr", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 2007); return isl_printer_free(p); } while (0)
2006 "output format not supported for ast_expr",do { isl_handle_error(isl_printer_get_ctx(p), isl_error_unsupported
, "output format not supported for ast_expr", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 2007); return isl_printer_free(p); } while (0)
2007 return isl_printer_free(p))do { isl_handle_error(isl_printer_get_ctx(p), isl_error_unsupported
, "output format not supported for ast_expr", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 2007); return isl_printer_free(p); } while (0)
;
2008 }
2009
2010 return p;
2011}
2012
2013static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
2014 __isl_keep isl_ast_node *node);
2015
2016/* Print a YAML sequence containing the entries in "list" to "p".
2017 */
2018static __isl_give isl_printer *print_ast_node_list(__isl_take isl_printer *p,
2019 __isl_keep isl_ast_node_list *list)
2020{
2021 int i, n;
2022
2023 n = isl_ast_node_list_n_ast_node(list);
2024 if (n < 0)
2025 return isl_printer_free(p);
2026
2027 p = isl_printer_yaml_start_sequence(p);
2028 for (i = 0; i < n; ++i) {
2029 isl_ast_node *node;
2030
2031 node = isl_ast_node_list_get_ast_node(list, i);
2032 p = print_ast_node_isl(p, node);
2033 isl_ast_node_free(node);
2034 p = isl_printer_yaml_next(p);
2035 }
2036 p = isl_printer_yaml_end_sequence(p);
2037
2038 return p;
2039}
2040
2041/* Print "node" to "p" in "isl format".
2042 *
2043 * In particular, print the isl_ast_node as a YAML document.
2044 */
2045static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
2046 __isl_keep isl_ast_node *node)
2047{
2048 switch (node->type) {
2049 case isl_ast_node_for:
2050 p = isl_printer_yaml_start_mapping(p);
2051 p = isl_printer_print_str(p, "iterator");
2052 p = isl_printer_yaml_next(p);
2053 p = isl_printer_print_ast_expr(p, node->u.f.iterator);
2054 p = isl_printer_yaml_next(p);
2055 if (node->u.f.degenerate) {
2056 p = isl_printer_print_str(p, "value");
2057 p = isl_printer_yaml_next(p);
2058 p = isl_printer_print_ast_expr(p, node->u.f.init);
2059 p = isl_printer_yaml_next(p);
2060 } else {
2061 p = isl_printer_print_str(p, "init");
2062 p = isl_printer_yaml_next(p);
2063 p = isl_printer_print_ast_expr(p, node->u.f.init);
2064 p = isl_printer_yaml_next(p);
2065 p = isl_printer_print_str(p, "cond");
2066 p = isl_printer_yaml_next(p);
2067 p = isl_printer_print_ast_expr(p, node->u.f.cond);
2068 p = isl_printer_yaml_next(p);
2069 p = isl_printer_print_str(p, "inc");
2070 p = isl_printer_yaml_next(p);
2071 p = isl_printer_print_ast_expr(p, node->u.f.inc);
2072 p = isl_printer_yaml_next(p);
2073 }
2074 if (node->u.f.body) {
2075 p = isl_printer_print_str(p, "body");
2076 p = isl_printer_yaml_next(p);
2077 p = isl_printer_print_ast_node(p, node->u.f.body);
2078 p = isl_printer_yaml_next(p);
2079 }
2080 p = isl_printer_yaml_end_mapping(p);
2081 break;
2082 case isl_ast_node_mark:
2083 p = isl_printer_yaml_start_mapping(p);
2084 p = isl_printer_print_str(p, "mark");
2085 p = isl_printer_yaml_next(p);
2086 p = isl_printer_print_id(p, node->u.m.mark);
2087 p = isl_printer_yaml_next(p);
2088 p = isl_printer_print_str(p, "node");
2089 p = isl_printer_yaml_next(p);
2090 p = isl_printer_print_ast_node(p, node->u.m.node);
2091 p = isl_printer_yaml_end_mapping(p);
2092 break;
2093 case isl_ast_node_user:
2094 p = isl_printer_yaml_start_mapping(p);
2095 p = isl_printer_print_str(p, "user");
2096 p = isl_printer_yaml_next(p);
2097 p = isl_printer_print_ast_expr(p, node->u.e.expr);
2098 p = isl_printer_yaml_end_mapping(p);
2099 break;
2100 case isl_ast_node_if:
2101 p = isl_printer_yaml_start_mapping(p);
2102 p = isl_printer_print_str(p, "guard");
2103 p = isl_printer_yaml_next(p);
2104 p = isl_printer_print_ast_expr(p, node->u.i.guard);
2105 p = isl_printer_yaml_next(p);
2106 if (node->u.i.then) {
2107 p = isl_printer_print_str(p, "then");
2108 p = isl_printer_yaml_next(p);
2109 p = isl_printer_print_ast_node(p, node->u.i.then);
2110 p = isl_printer_yaml_next(p);
2111 }
2112 if (node->u.i.else_node) {
2113 p = isl_printer_print_str(p, "else");
2114 p = isl_printer_yaml_next(p);
2115 p = isl_printer_print_ast_node(p, node->u.i.else_node);
2116 }
2117 p = isl_printer_yaml_end_mapping(p);
2118 break;
2119 case isl_ast_node_block:
2120 p = print_ast_node_list(p, node->u.b.children);
2121 break;
2122 case isl_ast_node_error:
2123 break;
2124 }
2125 return p;
2126}
2127
2128/* Do we need to print a block around the body "node" of a for or if node?
2129 *
2130 * If the node is a block, then we need to print a block.
2131 * Also if the node is a degenerate for then we will print it as
2132 * an assignment followed by the body of the for loop, so we need a block
2133 * as well.
2134 * If the node is an if node with an else, then we print a block
2135 * to avoid spurious dangling else warnings emitted by some compilers.
2136 * If the node is a mark, then in principle, we would have to check
2137 * the child of the mark node. However, even if the child would not
2138 * require us to print a block, for readability it is probably best
2139 * to print a block anyway.
2140 * If the ast_always_print_block option has been set, then we print a block.
2141 */
2142static int need_block(__isl_keep isl_ast_node *node)
2143{
2144 isl_ctx *ctx;
2145
2146 if (node->type == isl_ast_node_block)
2147 return 1;
2148 if (node->type == isl_ast_node_for && node->u.f.degenerate)
2149 return 1;
2150 if (node->type == isl_ast_node_if && node->u.i.else_node)
2151 return 1;
2152 if (node->type == isl_ast_node_mark)
2153 return 1;
2154
2155 ctx = isl_ast_node_get_ctx(node);
2156 return isl_options_get_ast_always_print_block(ctx);
2157}
2158
2159static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
2160 __isl_keep isl_ast_node *node,
2161 __isl_keep isl_ast_print_options *options, int in_block, int in_list);
2162static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
2163 __isl_keep isl_ast_node *node,
2164 __isl_keep isl_ast_print_options *options, int new_line,
2165 int force_block);
2166
2167/* Print the body "node" of a for or if node.
2168 * If "else_node" is set, then it is printed as well.
2169 * If "force_block" is set, then print out the body as a block.
2170 *
2171 * We first check if we need to print out a block.
2172 * We always print out a block if there is an else node to make
2173 * sure that the else node is matched to the correct if node.
2174 * For consistency, the corresponding else node is also printed as a block.
2175 *
2176 * If the else node is itself an if, then we print it as
2177 *
2178 * } else if (..) {
2179 * }
2180 *
2181 * Otherwise the else node is printed as
2182 *
2183 * } else {
2184 * node
2185 * }
2186 */
2187static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p,
2188 __isl_keep isl_ast_node *node, __isl_keep isl_ast_node *else_node,
2189 __isl_keep isl_ast_print_options *options, int force_block)
2190{
2191 if (!node)
9
Assuming 'node' is non-null
10
Taking false branch
2192 return isl_printer_free(p);
2193
2194 if (!force_block && !else_node && !need_block(node)) {
11
Assuming the condition is true
12
Taking true branch
2195 p = isl_printer_end_line(p);
2196 p = isl_printer_indent(p, 2);
2197 p = isl_ast_node_print(node, p,
13
Calling 'isl_ast_node_print'
21
Returning; memory was released via 3rd parameter
2198 isl_ast_print_options_copy(options));
2199 p = isl_printer_indent(p, -2);
2200 return p;
2201 }
2202
2203 p = isl_printer_print_str(p, " {");
2204 p = isl_printer_end_line(p);
2205 p = isl_printer_indent(p, 2);
2206 p = print_ast_node_c(p, node, options, 1, 0);
2207 p = isl_printer_indent(p, -2);
2208 p = isl_printer_start_line(p);
2209 p = isl_printer_print_str(p, "}");
2210 if (else_node) {
2211 if (else_node->type == isl_ast_node_if) {
2212 p = isl_printer_print_str(p, " else ");
2213 p = print_if_c(p, else_node, options, 0, 1);
2214 } else {
2215 p = isl_printer_print_str(p, " else");
2216 p = print_body_c(p, else_node, NULL((void*)0), options, 1);
2217 }
2218 } else
2219 p = isl_printer_end_line(p);
2220
2221 return p;
2222}
2223
2224/* Print the start of a compound statement.
2225 */
2226static __isl_give isl_printer *start_block(__isl_take isl_printer *p)
2227{
2228 p = isl_printer_start_line(p);
2229 p = isl_printer_print_str(p, "{");
2230 p = isl_printer_end_line(p);
2231 p = isl_printer_indent(p, 2);
2232
2233 return p;
2234}
2235
2236/* Print the end of a compound statement.
2237 */
2238static __isl_give isl_printer *end_block(__isl_take isl_printer *p)
2239{
2240 p = isl_printer_indent(p, -2);
2241 p = isl_printer_start_line(p);
2242 p = isl_printer_print_str(p, "}");
2243 p = isl_printer_end_line(p);
2244
2245 return p;
2246}
2247
2248/* Print the for node "node".
2249 *
2250 * If the for node is degenerate, it is printed as
2251 *
2252 * type iterator = init;
2253 * body
2254 *
2255 * Otherwise, it is printed as
2256 *
2257 * for (type iterator = init; cond; iterator += inc)
2258 * body
2259 *
2260 * "in_block" is set if we are currently inside a block.
2261 * "in_list" is set if the current node is not alone in the block.
2262 * If we are not in a block or if the current not is not alone in the block
2263 * then we print a block around a degenerate for loop such that the variable
2264 * declaration will not conflict with any potential other declaration
2265 * of the same variable.
2266 */
2267static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p,
2268 __isl_keep isl_ast_node *node,
2269 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
2270{
2271 isl_id *id;
2272 const char *name;
2273 const char *type;
2274
2275 type = isl_options_get_ast_iterator_type(isl_printer_get_ctx(p));
2276 if (!node->u.f.degenerate) {
7
Taking true branch
2277 id = isl_ast_expr_get_id(node->u.f.iterator);
2278 name = isl_id_get_name(id);
2279 isl_id_free(id);
2280 p = isl_printer_start_line(p);
2281 p = isl_printer_print_str(p, "for (");
2282 p = isl_printer_print_str(p, type);
2283 p = isl_printer_print_str(p, " ");
2284 p = isl_printer_print_str(p, name);
2285 p = isl_printer_print_str(p, " = ");
2286 p = isl_printer_print_ast_expr(p, node->u.f.init);
2287 p = isl_printer_print_str(p, "; ");
2288 p = isl_printer_print_ast_expr(p, node->u.f.cond);
2289 p = isl_printer_print_str(p, "; ");
2290 p = isl_printer_print_str(p, name);
2291 p = isl_printer_print_str(p, " += ");
2292 p = isl_printer_print_ast_expr(p, node->u.f.inc);
2293 p = isl_printer_print_str(p, ")");
2294 p = print_body_c(p, node->u.f.body, NULL((void*)0), options, 0);
8
Calling 'print_body_c'
22
Returning; memory was released via 4th parameter
2295 } else {
2296 id = isl_ast_expr_get_id(node->u.f.iterator);
2297 name = isl_id_get_name(id);
2298 isl_id_free(id);
2299 if (!in_block || in_list)
2300 p = start_block(p);
2301 p = isl_printer_start_line(p);
2302 p = isl_printer_print_str(p, type);
2303 p = isl_printer_print_str(p, " ");
2304 p = isl_printer_print_str(p, name);
2305 p = isl_printer_print_str(p, " = ");
2306 p = isl_printer_print_ast_expr(p, node->u.f.init);
2307 p = isl_printer_print_str(p, ";");
2308 p = isl_printer_end_line(p);
2309 p = print_ast_node_c(p, node->u.f.body, options, 1, 0);
2310 if (!in_block || in_list)
2311 p = end_block(p);
2312 }
2313
2314 return p;
2315}
2316
2317/* Print the if node "node".
2318 * If "new_line" is set then the if node should be printed on a new line.
2319 * If "force_block" is set, then print out the body as a block.
2320 */
2321static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
2322 __isl_keep isl_ast_node *node,
2323 __isl_keep isl_ast_print_options *options, int new_line,
2324 int force_block)
2325{
2326 if (new_line)
2327 p = isl_printer_start_line(p);
2328 p = isl_printer_print_str(p, "if (");
2329 p = isl_printer_print_ast_expr(p, node->u.i.guard);
2330 p = isl_printer_print_str(p, ")");
2331 p = print_body_c(p, node->u.i.then, node->u.i.else_node, options,
2332 force_block);
2333
2334 return p;
2335}
2336
2337/* Print the "node" to "p".
2338 *
2339 * "in_block" is set if we are currently inside a block.
2340 * If so, we do not print a block around the children of a block node.
2341 * We do this to avoid an extra block around the body of a degenerate
2342 * for node.
2343 *
2344 * "in_list" is set if the current node is not alone in the block.
2345 */
2346static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
2347 __isl_keep isl_ast_node *node,
2348 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
2349{
2350 switch (node->type) {
2351 case isl_ast_node_for:
2352 if (options->print_for)
2353 return options->print_for(p,
2354 isl_ast_print_options_copy(options),
2355 node, options->print_for_user);
2356 p = print_for_c(p, node, options, in_block, in_list);
2357 break;
2358 case isl_ast_node_if:
2359 p = print_if_c(p, node, options, 1, 0);
2360 break;
2361 case isl_ast_node_block:
2362 if (!in_block)
2363 p = start_block(p);
2364 p = isl_ast_node_list_print(node->u.b.children, p, options);
2365 if (!in_block)
2366 p = end_block(p);
2367 break;
2368 case isl_ast_node_mark:
2369 p = isl_printer_start_line(p);
2370 p = isl_printer_print_str(p, "// ");
2371 p = isl_printer_print_str(p, isl_id_get_name(node->u.m.mark));
2372 p = isl_printer_end_line(p);
2373 p = print_ast_node_c(p, node->u.m.node, options, 0, in_list);
2374 break;
2375 case isl_ast_node_user:
2376 if (options->print_user)
2377 return options->print_user(p,
2378 isl_ast_print_options_copy(options),
2379 node, options->print_user_user);
2380 p = isl_printer_start_line(p);
2381 p = isl_printer_print_ast_expr(p, node->u.e.expr);
2382 p = isl_printer_print_str(p, ";");
2383 p = isl_printer_end_line(p);
2384 break;
2385 case isl_ast_node_error:
2386 break;
2387 }
2388 return p;
2389}
2390
2391/* Print the for node "node" to "p".
2392 */
2393__isl_give isl_printer *isl_ast_node_for_print(__isl_keep isl_ast_node *node,
2394 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2395{
2396 if (!node || !options)
1
Assuming 'node' is non-null
2
Assuming 'options' is non-null
3
Taking false branch
2397 goto error;
2398 if (node->type != isl_ast_node_for)
4
Assuming the condition is false
5
Taking false branch
2399 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a for node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 2400); goto error; } while (0)
2400 "not a for node", goto error)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not a for node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 2400); goto error; } while (0)
;
2401 p = print_for_c(p, node, options, 0, 0);
6
Calling 'print_for_c'
23
Returning; memory was released via 3rd parameter
2402 isl_ast_print_options_free(options);
24
Use of memory after it is freed
2403 return p;
2404error:
2405 isl_ast_print_options_free(options);
2406 isl_printer_free(p);
2407 return NULL((void*)0);
2408}
2409
2410/* Print the if node "node" to "p".
2411 */
2412__isl_give isl_printer *isl_ast_node_if_print(__isl_keep isl_ast_node *node,
2413 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2414{
2415 if (!node || !options)
2416 goto error;
2417 if (node->type != isl_ast_node_if)
2418 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not an if node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 2419); goto error; } while (0)
2419 "not an if node", goto error)do { isl_handle_error(isl_ast_node_get_ctx(node), isl_error_invalid
, "not an if node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 2419); goto error; } while (0)
;
2420 p = print_if_c(p, node, options, 1, 0);
2421 isl_ast_print_options_free(options);
2422 return p;
2423error:
2424 isl_ast_print_options_free(options);
2425 isl_printer_free(p);
2426 return NULL((void*)0);
2427}
2428
2429/* Print "node" to "p".
2430 */
2431__isl_give isl_printer *isl_ast_node_print(__isl_keep isl_ast_node *node,
2432 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2433{
2434 if (!options || !node)
14
Taking false branch
2435 goto error;
2436 p = print_ast_node_c(p, node, options, 0, 0);
2437 isl_ast_print_options_free(options);
15
Calling 'isl_ast_print_options_free'
20
Returning; memory was released via 1st parameter
2438 return p;
2439error:
2440 isl_ast_print_options_free(options);
2441 isl_printer_free(p);
2442 return NULL((void*)0);
2443}
2444
2445/* Print "node" to "p".
2446 */
2447__isl_give isl_printer *isl_printer_print_ast_node(__isl_take isl_printer *p,
2448 __isl_keep isl_ast_node *node)
2449{
2450 int format;
2451 isl_ast_print_options *options;
2452
2453 if (!p)
2454 return NULL((void*)0);
2455
2456 format = isl_printer_get_output_format(p);
2457 switch (format) {
2458 case ISL_FORMAT_ISL0:
2459 p = print_ast_node_isl(p, node);
2460 break;
2461 case ISL_FORMAT_C4:
2462 options = isl_ast_print_options_alloc(isl_printer_get_ctx(p));
2463 p = isl_ast_node_print(node, p, options);
2464 break;
2465 default:
2466 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,do { isl_handle_error(isl_printer_get_ctx(p), isl_error_unsupported
, "output format not supported for ast_node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 2468); return isl_printer_free(p); } while (0)
2467 "output format not supported for ast_node",do { isl_handle_error(isl_printer_get_ctx(p), isl_error_unsupported
, "output format not supported for ast_node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 2468); return isl_printer_free(p); } while (0)
2468 return isl_printer_free(p))do { isl_handle_error(isl_printer_get_ctx(p), isl_error_unsupported
, "output format not supported for ast_node", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 2468); return isl_printer_free(p); } while (0)
;
2469 }
2470
2471 return p;
2472}
2473
2474/* Print the list of nodes "list" to "p".
2475 */
2476__isl_give isl_printer *isl_ast_node_list_print(
2477 __isl_keep isl_ast_node_list *list, __isl_take isl_printer *p,
2478 __isl_keep isl_ast_print_options *options)
2479{
2480 int i;
2481
2482 if (!p || !list || !options)
2483 return isl_printer_free(p);
2484
2485 for (i = 0; i < list->n; ++i)
2486 p = print_ast_node_c(p, list->p[i], options, 1, 1);
2487
2488 return p;
2489}
2490
2491#define ISL_AST_MACRO_FLOORD(1 << 0) (1 << 0)
2492#define ISL_AST_MACRO_MIN(1 << 1) (1 << 1)
2493#define ISL_AST_MACRO_MAX(1 << 2) (1 << 2)
2494#define ISL_AST_MACRO_ALL((1 << 0) | (1 << 1) | (1 << 2)) (ISL_AST_MACRO_FLOORD(1 << 0) | \
2495 ISL_AST_MACRO_MIN(1 << 1) | \
2496 ISL_AST_MACRO_MAX(1 << 2))
2497
2498/* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
2499 * then set the corresponding bit in "macros".
2500 */
2501static int ast_expr_required_macros(__isl_keep isl_ast_expr *expr, int macros)
2502{
2503 int i;
2504
2505 if (macros == ISL_AST_MACRO_ALL((1 << 0) | (1 << 1) | (1 << 2)))
2506 return macros;
2507
2508 if (expr->type != isl_ast_expr_op)
2509 return macros;
2510
2511 if (expr->u.op.op == isl_ast_op_min)
2512 macros |= ISL_AST_MACRO_MIN(1 << 1);
2513 if (expr->u.op.op == isl_ast_op_max)
2514 macros |= ISL_AST_MACRO_MAX(1 << 2);
2515 if (expr->u.op.op == isl_ast_op_fdiv_q)
2516 macros |= ISL_AST_MACRO_FLOORD(1 << 0);
2517
2518 for (i = 0; i < expr->u.op.n_arg; ++i)
2519 macros = ast_expr_required_macros(expr->u.op.args[i], macros);
2520
2521 return macros;
2522}
2523
2524static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
2525 int macros);
2526
2527/* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
2528 * then set the corresponding bit in "macros".
2529 */
2530static int ast_node_required_macros(__isl_keep isl_ast_node *node, int macros)
2531{
2532 if (macros == ISL_AST_MACRO_ALL((1 << 0) | (1 << 1) | (1 << 2)))
2533 return macros;
2534
2535 switch (node->type) {
2536 case isl_ast_node_for:
2537 macros = ast_expr_required_macros(node->u.f.init, macros);
2538 if (!node->u.f.degenerate) {
2539 macros = ast_expr_required_macros(node->u.f.cond,
2540 macros);
2541 macros = ast_expr_required_macros(node->u.f.inc,
2542 macros);
2543 }
2544 macros = ast_node_required_macros(node->u.f.body, macros);
2545 break;
2546 case isl_ast_node_if:
2547 macros = ast_expr_required_macros(node->u.i.guard, macros);
2548 macros = ast_node_required_macros(node->u.i.then, macros);
2549 if (node->u.i.else_node)
2550 macros = ast_node_required_macros(node->u.i.else_node,
2551 macros);
2552 break;
2553 case isl_ast_node_block:
2554 macros = ast_node_list_required_macros(node->u.b.children,
2555 macros);
2556 break;
2557 case isl_ast_node_mark:
2558 macros = ast_node_required_macros(node->u.m.node, macros);
2559 break;
2560 case isl_ast_node_user:
2561 macros = ast_expr_required_macros(node->u.e.expr, macros);
2562 break;
2563 case isl_ast_node_error:
2564 break;
2565 }
2566
2567 return macros;
2568}
2569
2570/* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
2571 * then set the corresponding bit in "macros".
2572 */
2573static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
2574 int macros)
2575{
2576 int i;
2577
2578 for (i = 0; i < list->n; ++i)
2579 macros = ast_node_required_macros(list->p[i], macros);
2580
2581 return macros;
2582}
2583
2584/* Data structure for keeping track of whether a macro definition
2585 * for a given type has already been printed.
2586 * The value is zero if no definition has been printed and non-zero otherwise.
2587 */
2588struct isl_ast_op_printed {
2589 char printed[isl_ast_op_lastisl_ast_op_address_of + 1];
2590};
2591
2592/* Create an empty struct isl_ast_op_printed.
2593 */
2594static void *create_printed(isl_ctx *ctx)
2595{
2596 return isl_calloc_type(ctx, struct isl_ast_op_printed)((struct isl_ast_op_printed *)isl_calloc_or_die(ctx, 1, sizeof
(struct isl_ast_op_printed)))
;
2597}
2598
2599/* Free a struct isl_ast_op_printed.
2600 */
2601static void free_printed(void *user)
2602{
2603 free(user);
2604}
2605
2606/* Ensure that "p" has an isl_ast_op_printed note identified by "id".
2607 */
2608static __isl_give isl_printer *alloc_printed(__isl_take isl_printer *p,
2609 __isl_keep isl_id *id)
2610{
2611 return alloc_note(p, id, &create_printed, &free_printed);
2612}
2613
2614/* Create an identifier that is used to store
2615 * an isl_ast_op_printed note.
2616 */
2617static __isl_give isl_id *printed_id(isl_ctx *ctx)
2618{
2619 return isl_id_alloc(ctx, "isl_ast_op_type_printed", NULL((void*)0));
2620}
2621
2622/* Did the user specify that a macro definition should only be
2623 * printed once and has a macro definition for "type" already
2624 * been printed to "p"?
2625 * If definitions should only be printed once, but a definition
2626 * for "p" has not yet been printed, then mark it as having been
2627 * printed so that it will not printed again.
2628 * The actual printing is taken care of by the caller.
2629 */
2630static isl_bool already_printed_once(__isl_keep isl_printer *p,
2631 enum isl_ast_op_type type)
2632{
2633 isl_ctx *ctx;
2634 isl_id *id;
2635 struct isl_ast_op_printed *printed;
2636
2637 if (!p)
2638 return isl_bool_error;
2639
2640 ctx = isl_printer_get_ctx(p);
2641 if (!isl_options_get_ast_print_macro_once(ctx))
2642 return isl_bool_false;
2643
2644 if (type > isl_ast_op_lastisl_ast_op_address_of)
2645 isl_die(isl_printer_get_ctx(p), isl_error_invalid,do { isl_handle_error(isl_printer_get_ctx(p), isl_error_invalid
, "invalid type", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 2646); return isl_bool_error; } while (0)
2646 "invalid type", return isl_bool_error)do { isl_handle_error(isl_printer_get_ctx(p), isl_error_invalid
, "invalid type", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_ast.c"
, 2646); return isl_bool_error; } while (0)
;
2647
2648 id = printed_id(isl_printer_get_ctx(p));
2649 p = alloc_printed(p, id);
2650 printed = get_note(p, id);
2651 isl_id_free(id);
2652 if (!printed)
2653 return isl_bool_error;
2654
2655 if (printed->printed[type])
2656 return isl_bool_true;
2657
2658 printed->printed[type] = 1;
2659 return isl_bool_false;
2660}
2661
2662/* Print a macro definition for the operator "type".
2663 *
2664 * If the user has specified that a macro definition should
2665 * only be printed once to any given printer and if the macro definition
2666 * has already been printed to "p", then do not print the definition.
2667 */
2668__isl_give isl_printer *isl_ast_op_type_print_macro(
2669 enum isl_ast_op_type type, __isl_take isl_printer *p)
2670{
2671 isl_bool skip;
2672
2673 skip = already_printed_once(p, type);
2674 if (skip < 0)
2675 return isl_printer_free(p);
2676 if (skip)
2677 return p;
2678
2679 switch (type) {
2680 case isl_ast_op_min:
2681 p = isl_printer_start_line(p);
2682 p = isl_printer_print_str(p, "#define ");
2683 p = isl_printer_print_str(p, get_op_str_c(p, type));
2684 p = isl_printer_print_str(p,
2685 "(x,y) ((x) < (y) ? (x) : (y))");
2686 p = isl_printer_end_line(p);
2687 break;
2688 case isl_ast_op_max:
2689 p = isl_printer_start_line(p);
2690 p = isl_printer_print_str(p, "#define ");
2691 p = isl_printer_print_str(p, get_op_str_c(p, type));
2692 p = isl_printer_print_str(p,
2693 "(x,y) ((x) > (y) ? (x) : (y))");
2694 p = isl_printer_end_line(p);
2695 break;
2696 case isl_ast_op_fdiv_q:
2697 p = isl_printer_start_line(p);
2698 p = isl_printer_print_str(p, "#define ");
2699 p = isl_printer_print_str(p, get_op_str_c(p, type));
2700 p = isl_printer_print_str(p,
2701 "(n,d) "
2702 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
2703 p = isl_printer_end_line(p);
2704 break;
2705 default:
2706 break;
2707 }
2708
2709 return p;
2710}
2711
2712/* Call "fn" for each type of operation represented in the "macros"
2713 * bit vector.
2714 */
2715static isl_stat foreach_ast_op_type(int macros,
2716 isl_stat (*fn)(enum isl_ast_op_type type, void *user), void *user)
2717{
2718 if (macros & ISL_AST_MACRO_MIN(1 << 1) && fn(isl_ast_op_min, user) < 0)
2719 return isl_stat_error;
2720 if (macros & ISL_AST_MACRO_MAX(1 << 2) && fn(isl_ast_op_max, user) < 0)
2721 return isl_stat_error;
2722 if (macros & ISL_AST_MACRO_FLOORD(1 << 0) && fn(isl_ast_op_fdiv_q, user) < 0)
2723 return isl_stat_error;
2724
2725 return isl_stat_ok;
2726}
2727
2728/* Call "fn" for each type of operation that appears in "expr"
2729 * and that requires a macro definition.
2730 */
2731isl_stat isl_ast_expr_foreach_ast_op_type(__isl_keep isl_ast_expr *expr,
2732 isl_stat (*fn)(enum isl_ast_op_type type, void *user), void *user)
2733{
2734 int macros;
2735
2736 if (!expr)
2737 return isl_stat_error;
2738
2739 macros = ast_expr_required_macros(expr, 0);
2740 return foreach_ast_op_type(macros, fn, user);
2741}
2742
2743/* Call "fn" for each type of operation that appears in "node"
2744 * and that requires a macro definition.
2745 */
2746isl_stat isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node *node,
2747 isl_stat (*fn)(enum isl_ast_op_type type, void *user), void *user)
2748{
2749 int macros;
2750
2751 if (!node)
2752 return isl_stat_error;
2753
2754 macros = ast_node_required_macros(node, 0);
2755 return foreach_ast_op_type(macros, fn, user);
2756}
2757
2758static isl_stat ast_op_type_print_macro(enum isl_ast_op_type type, void *user)
2759{
2760 isl_printer **p = user;
2761
2762 *p = isl_ast_op_type_print_macro(type, *p);
2763
2764 return isl_stat_ok;
2765}
2766
2767/* Print macro definitions for all the macros used in the result
2768 * of printing "expr".
2769 */
2770__isl_give isl_printer *isl_ast_expr_print_macros(
2771 __isl_keep isl_ast_expr *expr, __isl_take isl_printer *p)
2772{
2773 if (isl_ast_expr_foreach_ast_op_type(expr,
2774 &ast_op_type_print_macro, &p) < 0)
2775 return isl_printer_free(p);
2776 return p;
2777}
2778
2779/* Print macro definitions for all the macros used in the result
2780 * of printing "node".
2781 */
2782__isl_give isl_printer *isl_ast_node_print_macros(
2783 __isl_keep isl_ast_node *node, __isl_take isl_printer *p)
2784{
2785 if (isl_ast_node_foreach_ast_op_type(node,
2786 &ast_op_type_print_macro, &p) < 0)
2787 return isl_printer_free(p);
2788 return p;
2789}
2790
2791/* Return a string containing C code representing this isl_ast_expr.
2792 */
2793__isl_give char *isl_ast_expr_to_C_str(__isl_keep isl_ast_expr *expr)
2794{
2795 isl_printer *p;
2796 char *str;
2797
2798 if (!expr)
2799 return NULL((void*)0);
2800
2801 p = isl_printer_to_str(isl_ast_expr_get_ctx(expr));
2802 p = isl_printer_set_output_format(p, ISL_FORMAT_C4);
2803 p = isl_printer_print_ast_expr(p, expr);
2804
2805 str = isl_printer_get_str(p);
2806
2807 isl_printer_free(p);
2808
2809 return str;
2810}
2811
2812/* Return a string containing C code representing this isl_ast_node.
2813 */
2814__isl_give char *isl_ast_node_to_C_str(__isl_keep isl_ast_node *node)
2815{
2816 isl_printer *p;
2817 char *str;
2818
2819 if (!node)
2820 return NULL((void*)0);
2821
2822 p = isl_printer_to_str(isl_ast_node_get_ctx(node));
2823 p = isl_printer_set_output_format(p, ISL_FORMAT_C4);
2824 p = isl_printer_print_ast_node(p, node);
2825
2826 str = isl_printer_get_str(p);
2827
2828 isl_printer_free(p);
2829
2830 return str;
2831}