isl_stream_read_multi_aff: extract out read_universe_params
[isl.git] / isl_ast.c
blob5e06b1993971215783b07b1f4cbfa413a76d0f5c
1 /*
2 * Copyright 2012-2013 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
8 */
10 #include <string.h>
12 #include <isl/id.h>
13 #include <isl/val.h>
14 #include <isl_ast_private.h>
16 #undef EL_BASE
17 #define EL_BASE ast_expr
19 #include <isl_list_templ.c>
21 #undef EL_BASE
22 #define EL_BASE ast_node
24 #include <isl_list_templ.c>
26 isl_ctx *isl_ast_print_options_get_ctx(
27 __isl_keep isl_ast_print_options *options)
29 return options ? options->ctx : NULL;
32 __isl_give isl_ast_print_options *isl_ast_print_options_alloc(isl_ctx *ctx)
34 isl_ast_print_options *options;
36 options = isl_calloc_type(ctx, isl_ast_print_options);
37 if (!options)
38 return NULL;
40 options->ctx = ctx;
41 isl_ctx_ref(ctx);
42 options->ref = 1;
44 return options;
47 __isl_give isl_ast_print_options *isl_ast_print_options_dup(
48 __isl_keep isl_ast_print_options *options)
50 isl_ctx *ctx;
51 isl_ast_print_options *dup;
53 if (!options)
54 return NULL;
56 ctx = isl_ast_print_options_get_ctx(options);
57 dup = isl_ast_print_options_alloc(ctx);
58 if (!dup)
59 return NULL;
61 dup->print_for = options->print_for;
62 dup->print_for_user = options->print_for_user;
63 dup->print_user = options->print_user;
64 dup->print_user_user = options->print_user_user;
66 return dup;
69 __isl_give isl_ast_print_options *isl_ast_print_options_cow(
70 __isl_take isl_ast_print_options *options)
72 if (!options)
73 return NULL;
75 if (options->ref == 1)
76 return options;
77 options->ref--;
78 return isl_ast_print_options_dup(options);
81 __isl_give isl_ast_print_options *isl_ast_print_options_copy(
82 __isl_keep isl_ast_print_options *options)
84 if (!options)
85 return NULL;
87 options->ref++;
88 return options;
91 __isl_null isl_ast_print_options *isl_ast_print_options_free(
92 __isl_take isl_ast_print_options *options)
94 if (!options)
95 return NULL;
97 if (--options->ref > 0)
98 return NULL;
100 isl_ctx_deref(options->ctx);
102 free(options);
103 return NULL;
106 /* Set the print_user callback of "options" to "print_user".
108 * If this callback is set, then it is used to print user nodes in the AST.
109 * Otherwise, the expression associated to the user node is printed.
111 __isl_give isl_ast_print_options *isl_ast_print_options_set_print_user(
112 __isl_take isl_ast_print_options *options,
113 __isl_give isl_printer *(*print_user)(__isl_take isl_printer *p,
114 __isl_take isl_ast_print_options *options,
115 __isl_keep isl_ast_node *node, void *user),
116 void *user)
118 options = isl_ast_print_options_cow(options);
119 if (!options)
120 return NULL;
122 options->print_user = print_user;
123 options->print_user_user = user;
125 return options;
128 /* Set the print_for callback of "options" to "print_for".
130 * If this callback is set, then it used to print for nodes in the AST.
132 __isl_give isl_ast_print_options *isl_ast_print_options_set_print_for(
133 __isl_take isl_ast_print_options *options,
134 __isl_give isl_printer *(*print_for)(__isl_take isl_printer *p,
135 __isl_take isl_ast_print_options *options,
136 __isl_keep isl_ast_node *node, void *user),
137 void *user)
139 options = isl_ast_print_options_cow(options);
140 if (!options)
141 return NULL;
143 options->print_for = print_for;
144 options->print_for_user = user;
146 return options;
149 __isl_give isl_ast_expr *isl_ast_expr_copy(__isl_keep isl_ast_expr *expr)
151 if (!expr)
152 return NULL;
154 expr->ref++;
155 return expr;
158 __isl_give isl_ast_expr *isl_ast_expr_dup(__isl_keep isl_ast_expr *expr)
160 int i;
161 isl_ctx *ctx;
162 isl_ast_expr *dup;
164 if (!expr)
165 return NULL;
167 ctx = isl_ast_expr_get_ctx(expr);
168 switch (expr->type) {
169 case isl_ast_expr_int:
170 dup = isl_ast_expr_from_val(isl_val_copy(expr->u.v));
171 break;
172 case isl_ast_expr_id:
173 dup = isl_ast_expr_from_id(isl_id_copy(expr->u.id));
174 break;
175 case isl_ast_expr_op:
176 dup = isl_ast_expr_alloc_op(ctx,
177 expr->u.op.op, expr->u.op.n_arg);
178 if (!dup)
179 return NULL;
180 for (i = 0; i < expr->u.op.n_arg; ++i)
181 dup->u.op.args[i] =
182 isl_ast_expr_copy(expr->u.op.args[i]);
183 break;
184 case isl_ast_expr_error:
185 dup = NULL;
188 if (!dup)
189 return NULL;
191 return dup;
194 __isl_give isl_ast_expr *isl_ast_expr_cow(__isl_take isl_ast_expr *expr)
196 if (!expr)
197 return NULL;
199 if (expr->ref == 1)
200 return expr;
201 expr->ref--;
202 return isl_ast_expr_dup(expr);
205 __isl_null isl_ast_expr *isl_ast_expr_free(__isl_take isl_ast_expr *expr)
207 int i;
209 if (!expr)
210 return NULL;
212 if (--expr->ref > 0)
213 return NULL;
215 isl_ctx_deref(expr->ctx);
217 switch (expr->type) {
218 case isl_ast_expr_int:
219 isl_val_free(expr->u.v);
220 break;
221 case isl_ast_expr_id:
222 isl_id_free(expr->u.id);
223 break;
224 case isl_ast_expr_op:
225 if (expr->u.op.args)
226 for (i = 0; i < expr->u.op.n_arg; ++i)
227 isl_ast_expr_free(expr->u.op.args[i]);
228 free(expr->u.op.args);
229 break;
230 case isl_ast_expr_error:
231 break;
234 free(expr);
235 return NULL;
238 isl_ctx *isl_ast_expr_get_ctx(__isl_keep isl_ast_expr *expr)
240 return expr ? expr->ctx : NULL;
243 enum isl_ast_expr_type isl_ast_expr_get_type(__isl_keep isl_ast_expr *expr)
245 return expr ? expr->type : isl_ast_expr_error;
248 /* Return the integer value represented by "expr".
250 __isl_give isl_val *isl_ast_expr_int_get_val(__isl_keep isl_ast_expr *expr)
252 if (!expr)
253 return NULL;
254 if (expr->type != isl_ast_expr_int)
255 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
256 "expression not an int", return NULL);
257 return isl_val_copy(expr->u.v);
260 /* This is an alternative name for the function above.
262 __isl_give isl_val *isl_ast_expr_get_val(__isl_keep isl_ast_expr *expr)
264 return isl_ast_expr_int_get_val(expr);
267 __isl_give isl_id *isl_ast_expr_id_get_id(__isl_keep isl_ast_expr *expr)
269 if (!expr)
270 return NULL;
271 if (expr->type != isl_ast_expr_id)
272 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
273 "expression not an identifier", return NULL);
275 return isl_id_copy(expr->u.id);
278 /* This is an alternative name for the function above.
280 __isl_give isl_id *isl_ast_expr_get_id(__isl_keep isl_ast_expr *expr)
282 return isl_ast_expr_id_get_id(expr);
285 /* Return the type of operation represented by "expr".
287 enum isl_ast_expr_op_type isl_ast_expr_op_get_type(
288 __isl_keep isl_ast_expr *expr)
290 if (!expr)
291 return isl_ast_expr_op_error;
292 if (expr->type != isl_ast_expr_op)
293 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
294 "expression not an operation",
295 return isl_ast_expr_op_error);
296 return expr->u.op.op;
299 /* This is an alternative name for the function above.
301 enum isl_ast_expr_op_type isl_ast_expr_get_op_type(
302 __isl_keep isl_ast_expr *expr)
304 return isl_ast_expr_op_get_type(expr);
307 /* Return the number of arguments of the operation represented by "expr".
309 isl_size isl_ast_expr_op_get_n_arg(__isl_keep isl_ast_expr *expr)
311 if (!expr)
312 return isl_size_error;
313 if (expr->type != isl_ast_expr_op)
314 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
315 "expression not an operation", return isl_size_error);
316 return expr->u.op.n_arg;
319 /* This is an alternative name for the function above.
321 isl_size isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr *expr)
323 return isl_ast_expr_op_get_n_arg(expr);
326 /* Return the argument at position "pos" of the operation represented by "expr".
328 __isl_give isl_ast_expr *isl_ast_expr_op_get_arg(__isl_keep isl_ast_expr *expr,
329 int pos)
331 if (!expr)
332 return NULL;
333 if (expr->type != isl_ast_expr_op)
334 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
335 "expression not an operation", return NULL);
336 if (pos < 0 || pos >= expr->u.op.n_arg)
337 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
338 "index out of bounds", return NULL);
340 return isl_ast_expr_copy(expr->u.op.args[pos]);
343 /* This is an alternative name for the function above.
345 __isl_give isl_ast_expr *isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr *expr,
346 int pos)
348 return isl_ast_expr_op_get_arg(expr, pos);
351 /* Replace the argument at position "pos" of "expr" by "arg".
353 __isl_give isl_ast_expr *isl_ast_expr_set_op_arg(__isl_take isl_ast_expr *expr,
354 int pos, __isl_take isl_ast_expr *arg)
356 expr = isl_ast_expr_cow(expr);
357 if (!expr || !arg)
358 goto error;
359 if (expr->type != isl_ast_expr_op)
360 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
361 "expression not an operation", goto error);
362 if (pos < 0 || pos >= expr->u.op.n_arg)
363 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
364 "index out of bounds", goto error);
366 isl_ast_expr_free(expr->u.op.args[pos]);
367 expr->u.op.args[pos] = arg;
369 return expr;
370 error:
371 isl_ast_expr_free(arg);
372 return isl_ast_expr_free(expr);
375 /* Is "expr1" equal to "expr2"?
377 isl_bool isl_ast_expr_is_equal(__isl_keep isl_ast_expr *expr1,
378 __isl_keep isl_ast_expr *expr2)
380 int i;
382 if (!expr1 || !expr2)
383 return isl_bool_error;
385 if (expr1 == expr2)
386 return isl_bool_true;
387 if (expr1->type != expr2->type)
388 return isl_bool_false;
389 switch (expr1->type) {
390 case isl_ast_expr_int:
391 return isl_val_eq(expr1->u.v, expr2->u.v);
392 case isl_ast_expr_id:
393 return isl_bool_ok(expr1->u.id == expr2->u.id);
394 case isl_ast_expr_op:
395 if (expr1->u.op.op != expr2->u.op.op)
396 return isl_bool_false;
397 if (expr1->u.op.n_arg != expr2->u.op.n_arg)
398 return isl_bool_false;
399 for (i = 0; i < expr1->u.op.n_arg; ++i) {
400 isl_bool equal;
401 equal = isl_ast_expr_is_equal(expr1->u.op.args[i],
402 expr2->u.op.args[i]);
403 if (equal < 0 || !equal)
404 return equal;
406 return isl_bool_true;
407 case isl_ast_expr_error:
408 return isl_bool_error;
411 isl_die(isl_ast_expr_get_ctx(expr1), isl_error_internal,
412 "unhandled case", return isl_bool_error);
415 /* Create a new operation expression of operation type "op",
416 * with "n_arg" as yet unspecified arguments.
418 __isl_give isl_ast_expr *isl_ast_expr_alloc_op(isl_ctx *ctx,
419 enum isl_ast_expr_op_type op, int n_arg)
421 isl_ast_expr *expr;
423 expr = isl_calloc_type(ctx, isl_ast_expr);
424 if (!expr)
425 return NULL;
427 expr->ctx = ctx;
428 isl_ctx_ref(ctx);
429 expr->ref = 1;
430 expr->type = isl_ast_expr_op;
431 expr->u.op.op = op;
432 expr->u.op.n_arg = n_arg;
433 expr->u.op.args = isl_calloc_array(ctx, isl_ast_expr *, n_arg);
435 if (n_arg && !expr->u.op.args)
436 return isl_ast_expr_free(expr);
438 return expr;
441 /* Create a new id expression representing "id".
443 __isl_give isl_ast_expr *isl_ast_expr_from_id(__isl_take isl_id *id)
445 isl_ctx *ctx;
446 isl_ast_expr *expr;
448 if (!id)
449 return NULL;
451 ctx = isl_id_get_ctx(id);
452 expr = isl_calloc_type(ctx, isl_ast_expr);
453 if (!expr)
454 goto error;
456 expr->ctx = ctx;
457 isl_ctx_ref(ctx);
458 expr->ref = 1;
459 expr->type = isl_ast_expr_id;
460 expr->u.id = id;
462 return expr;
463 error:
464 isl_id_free(id);
465 return NULL;
468 /* Create a new integer expression representing "i".
470 __isl_give isl_ast_expr *isl_ast_expr_alloc_int_si(isl_ctx *ctx, int i)
472 isl_ast_expr *expr;
474 expr = isl_calloc_type(ctx, isl_ast_expr);
475 if (!expr)
476 return NULL;
478 expr->ctx = ctx;
479 isl_ctx_ref(ctx);
480 expr->ref = 1;
481 expr->type = isl_ast_expr_int;
482 expr->u.v = isl_val_int_from_si(ctx, i);
483 if (!expr->u.v)
484 return isl_ast_expr_free(expr);
486 return expr;
489 /* Create a new integer expression representing "v".
491 __isl_give isl_ast_expr *isl_ast_expr_from_val(__isl_take isl_val *v)
493 isl_ctx *ctx;
494 isl_ast_expr *expr;
496 if (!v)
497 return NULL;
498 if (!isl_val_is_int(v))
499 isl_die(isl_val_get_ctx(v), isl_error_invalid,
500 "expecting integer value", goto error);
502 ctx = isl_val_get_ctx(v);
503 expr = isl_calloc_type(ctx, isl_ast_expr);
504 if (!expr)
505 goto error;
507 expr->ctx = ctx;
508 isl_ctx_ref(ctx);
509 expr->ref = 1;
510 expr->type = isl_ast_expr_int;
511 expr->u.v = v;
513 return expr;
514 error:
515 isl_val_free(v);
516 return NULL;
519 /* Create an expression representing the unary operation "type" applied to
520 * "arg".
522 __isl_give isl_ast_expr *isl_ast_expr_alloc_unary(
523 enum isl_ast_expr_op_type type, __isl_take isl_ast_expr *arg)
525 isl_ctx *ctx;
526 isl_ast_expr *expr = NULL;
528 if (!arg)
529 return NULL;
531 ctx = isl_ast_expr_get_ctx(arg);
532 expr = isl_ast_expr_alloc_op(ctx, type, 1);
533 if (!expr)
534 goto error;
536 expr->u.op.args[0] = arg;
538 return expr;
539 error:
540 isl_ast_expr_free(arg);
541 return NULL;
544 /* Create an expression representing the negation of "arg".
546 __isl_give isl_ast_expr *isl_ast_expr_neg(__isl_take isl_ast_expr *arg)
548 return isl_ast_expr_alloc_unary(isl_ast_expr_op_minus, arg);
551 /* Create an expression representing the address of "expr".
553 __isl_give isl_ast_expr *isl_ast_expr_address_of(__isl_take isl_ast_expr *expr)
555 if (!expr)
556 return NULL;
558 if (isl_ast_expr_get_type(expr) != isl_ast_expr_op ||
559 isl_ast_expr_get_op_type(expr) != isl_ast_expr_op_access)
560 isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
561 "can only take address of access expressions",
562 return isl_ast_expr_free(expr));
564 return isl_ast_expr_alloc_unary(isl_ast_expr_op_address_of, expr);
567 /* Create an expression representing the binary operation "type"
568 * applied to "expr1" and "expr2".
570 __isl_give isl_ast_expr *isl_ast_expr_alloc_binary(
571 enum isl_ast_expr_op_type type,
572 __isl_take isl_ast_expr *expr1, __isl_take isl_ast_expr *expr2)
574 isl_ctx *ctx;
575 isl_ast_expr *expr = NULL;
577 if (!expr1 || !expr2)
578 goto error;
580 ctx = isl_ast_expr_get_ctx(expr1);
581 expr = isl_ast_expr_alloc_op(ctx, type, 2);
582 if (!expr)
583 goto error;
585 expr->u.op.args[0] = expr1;
586 expr->u.op.args[1] = expr2;
588 return expr;
589 error:
590 isl_ast_expr_free(expr1);
591 isl_ast_expr_free(expr2);
592 return NULL;
595 /* Create an expression representing the sum of "expr1" and "expr2".
597 __isl_give isl_ast_expr *isl_ast_expr_add(__isl_take isl_ast_expr *expr1,
598 __isl_take isl_ast_expr *expr2)
600 return isl_ast_expr_alloc_binary(isl_ast_expr_op_add, expr1, expr2);
603 /* Create an expression representing the difference of "expr1" and "expr2".
605 __isl_give isl_ast_expr *isl_ast_expr_sub(__isl_take isl_ast_expr *expr1,
606 __isl_take isl_ast_expr *expr2)
608 return isl_ast_expr_alloc_binary(isl_ast_expr_op_sub, expr1, expr2);
611 /* Create an expression representing the product of "expr1" and "expr2".
613 __isl_give isl_ast_expr *isl_ast_expr_mul(__isl_take isl_ast_expr *expr1,
614 __isl_take isl_ast_expr *expr2)
616 return isl_ast_expr_alloc_binary(isl_ast_expr_op_mul, expr1, expr2);
619 /* Create an expression representing the quotient of "expr1" and "expr2".
621 __isl_give isl_ast_expr *isl_ast_expr_div(__isl_take isl_ast_expr *expr1,
622 __isl_take isl_ast_expr *expr2)
624 return isl_ast_expr_alloc_binary(isl_ast_expr_op_div, expr1, expr2);
627 /* Create an expression representing the quotient of the integer
628 * division of "expr1" by "expr2", where "expr1" is known to be
629 * non-negative.
631 __isl_give isl_ast_expr *isl_ast_expr_pdiv_q(__isl_take isl_ast_expr *expr1,
632 __isl_take isl_ast_expr *expr2)
634 return isl_ast_expr_alloc_binary(isl_ast_expr_op_pdiv_q, expr1, expr2);
637 /* Create an expression representing the remainder of the integer
638 * division of "expr1" by "expr2", where "expr1" is known to be
639 * non-negative.
641 __isl_give isl_ast_expr *isl_ast_expr_pdiv_r(__isl_take isl_ast_expr *expr1,
642 __isl_take isl_ast_expr *expr2)
644 return isl_ast_expr_alloc_binary(isl_ast_expr_op_pdiv_r, expr1, expr2);
647 /* Create an expression representing the conjunction of "expr1" and "expr2".
649 __isl_give isl_ast_expr *isl_ast_expr_and(__isl_take isl_ast_expr *expr1,
650 __isl_take isl_ast_expr *expr2)
652 return isl_ast_expr_alloc_binary(isl_ast_expr_op_and, expr1, expr2);
655 /* Create an expression representing the conjunction of "expr1" and "expr2",
656 * where "expr2" is evaluated only if "expr1" is evaluated to true.
658 __isl_give isl_ast_expr *isl_ast_expr_and_then(__isl_take isl_ast_expr *expr1,
659 __isl_take isl_ast_expr *expr2)
661 return isl_ast_expr_alloc_binary(isl_ast_expr_op_and_then, expr1, expr2);
664 /* Create an expression representing the disjunction of "expr1" and "expr2".
666 __isl_give isl_ast_expr *isl_ast_expr_or(__isl_take isl_ast_expr *expr1,
667 __isl_take isl_ast_expr *expr2)
669 return isl_ast_expr_alloc_binary(isl_ast_expr_op_or, expr1, expr2);
672 /* Create an expression representing the disjunction of "expr1" and "expr2",
673 * where "expr2" is evaluated only if "expr1" is evaluated to false.
675 __isl_give isl_ast_expr *isl_ast_expr_or_else(__isl_take isl_ast_expr *expr1,
676 __isl_take isl_ast_expr *expr2)
678 return isl_ast_expr_alloc_binary(isl_ast_expr_op_or_else, expr1, expr2);
681 /* Create an expression representing "expr1" less than or equal to "expr2".
683 __isl_give isl_ast_expr *isl_ast_expr_le(__isl_take isl_ast_expr *expr1,
684 __isl_take isl_ast_expr *expr2)
686 return isl_ast_expr_alloc_binary(isl_ast_expr_op_le, expr1, expr2);
689 /* Create an expression representing "expr1" less than "expr2".
691 __isl_give isl_ast_expr *isl_ast_expr_lt(__isl_take isl_ast_expr *expr1,
692 __isl_take isl_ast_expr *expr2)
694 return isl_ast_expr_alloc_binary(isl_ast_expr_op_lt, expr1, expr2);
697 /* Create an expression representing "expr1" greater than or equal to "expr2".
699 __isl_give isl_ast_expr *isl_ast_expr_ge(__isl_take isl_ast_expr *expr1,
700 __isl_take isl_ast_expr *expr2)
702 return isl_ast_expr_alloc_binary(isl_ast_expr_op_ge, expr1, expr2);
705 /* Create an expression representing "expr1" greater than "expr2".
707 __isl_give isl_ast_expr *isl_ast_expr_gt(__isl_take isl_ast_expr *expr1,
708 __isl_take isl_ast_expr *expr2)
710 return isl_ast_expr_alloc_binary(isl_ast_expr_op_gt, expr1, expr2);
713 /* Create an expression representing "expr1" equal to "expr2".
715 __isl_give isl_ast_expr *isl_ast_expr_eq(__isl_take isl_ast_expr *expr1,
716 __isl_take isl_ast_expr *expr2)
718 return isl_ast_expr_alloc_binary(isl_ast_expr_op_eq, expr1, expr2);
721 /* Create an expression of type "type" with as arguments "arg0" followed
722 * by "arguments".
724 static __isl_give isl_ast_expr *ast_expr_with_arguments(
725 enum isl_ast_expr_op_type type, __isl_take isl_ast_expr *arg0,
726 __isl_take isl_ast_expr_list *arguments)
728 int i;
729 isl_size n;
730 isl_ctx *ctx;
731 isl_ast_expr *res = NULL;
733 if (!arg0 || !arguments)
734 goto error;
736 ctx = isl_ast_expr_get_ctx(arg0);
737 n = isl_ast_expr_list_n_ast_expr(arguments);
738 if (n < 0)
739 goto error;
740 res = isl_ast_expr_alloc_op(ctx, type, 1 + n);
741 if (!res)
742 goto error;
743 for (i = 0; i < n; ++i) {
744 isl_ast_expr *arg;
745 arg = isl_ast_expr_list_get_ast_expr(arguments, i);
746 res->u.op.args[1 + i] = arg;
747 if (!arg)
748 goto error;
750 res->u.op.args[0] = arg0;
752 isl_ast_expr_list_free(arguments);
753 return res;
754 error:
755 isl_ast_expr_free(arg0);
756 isl_ast_expr_list_free(arguments);
757 isl_ast_expr_free(res);
758 return NULL;
761 /* Create an expression representing an access to "array" with index
762 * expressions "indices".
764 __isl_give isl_ast_expr *isl_ast_expr_access(__isl_take isl_ast_expr *array,
765 __isl_take isl_ast_expr_list *indices)
767 return ast_expr_with_arguments(isl_ast_expr_op_access, array, indices);
770 /* Create an expression representing a call to "function" with argument
771 * expressions "arguments".
773 __isl_give isl_ast_expr *isl_ast_expr_call(__isl_take isl_ast_expr *function,
774 __isl_take isl_ast_expr_list *arguments)
776 return ast_expr_with_arguments(isl_ast_expr_op_call, function, arguments);
779 /* For each subexpression of "expr" of type isl_ast_expr_id,
780 * if it appears in "id2expr", then replace it by the corresponding
781 * expression.
783 __isl_give isl_ast_expr *isl_ast_expr_substitute_ids(
784 __isl_take isl_ast_expr *expr, __isl_take isl_id_to_ast_expr *id2expr)
786 int i;
787 isl_maybe_isl_ast_expr m;
789 if (!expr || !id2expr)
790 goto error;
792 switch (expr->type) {
793 case isl_ast_expr_int:
794 break;
795 case isl_ast_expr_id:
796 m = isl_id_to_ast_expr_try_get(id2expr, expr->u.id);
797 if (m.valid < 0)
798 goto error;
799 if (!m.valid)
800 break;
801 isl_ast_expr_free(expr);
802 expr = m.value;
803 break;
804 case isl_ast_expr_op:
805 for (i = 0; i < expr->u.op.n_arg; ++i) {
806 isl_ast_expr *arg;
807 arg = isl_ast_expr_copy(expr->u.op.args[i]);
808 arg = isl_ast_expr_substitute_ids(arg,
809 isl_id_to_ast_expr_copy(id2expr));
810 if (arg == expr->u.op.args[i]) {
811 isl_ast_expr_free(arg);
812 continue;
814 if (!arg)
815 expr = isl_ast_expr_free(expr);
816 expr = isl_ast_expr_cow(expr);
817 if (!expr) {
818 isl_ast_expr_free(arg);
819 break;
821 isl_ast_expr_free(expr->u.op.args[i]);
822 expr->u.op.args[i] = arg;
824 break;
825 case isl_ast_expr_error:
826 expr = isl_ast_expr_free(expr);
827 break;
830 isl_id_to_ast_expr_free(id2expr);
831 return expr;
832 error:
833 isl_ast_expr_free(expr);
834 isl_id_to_ast_expr_free(id2expr);
835 return NULL;
838 isl_ctx *isl_ast_node_get_ctx(__isl_keep isl_ast_node *node)
840 return node ? node->ctx : NULL;
843 enum isl_ast_node_type isl_ast_node_get_type(__isl_keep isl_ast_node *node)
845 return node ? node->type : isl_ast_node_error;
848 __isl_give isl_ast_node *isl_ast_node_alloc(isl_ctx *ctx,
849 enum isl_ast_node_type type)
851 isl_ast_node *node;
853 node = isl_calloc_type(ctx, isl_ast_node);
854 if (!node)
855 return NULL;
857 node->ctx = ctx;
858 isl_ctx_ref(ctx);
859 node->ref = 1;
860 node->type = type;
862 return node;
865 /* Create an if node with the given guard.
867 * The then body needs to be filled in later.
869 __isl_give isl_ast_node *isl_ast_node_alloc_if(__isl_take isl_ast_expr *guard)
871 isl_ast_node *node;
873 if (!guard)
874 return NULL;
876 node = isl_ast_node_alloc(isl_ast_expr_get_ctx(guard), isl_ast_node_if);
877 if (!node)
878 goto error;
879 node->u.i.guard = guard;
881 return node;
882 error:
883 isl_ast_expr_free(guard);
884 return NULL;
887 /* Create a for node with the given iterator.
889 * The remaining fields need to be filled in later.
891 __isl_give isl_ast_node *isl_ast_node_alloc_for(__isl_take isl_id *id)
893 isl_ast_node *node;
894 isl_ctx *ctx;
896 if (!id)
897 return NULL;
899 ctx = isl_id_get_ctx(id);
900 node = isl_ast_node_alloc(ctx, isl_ast_node_for);
901 if (!node)
902 goto error;
904 node->u.f.iterator = isl_ast_expr_from_id(id);
905 if (!node->u.f.iterator)
906 return isl_ast_node_free(node);
908 return node;
909 error:
910 isl_id_free(id);
911 return NULL;
914 /* Create a mark node, marking "node" with "id".
916 __isl_give isl_ast_node *isl_ast_node_alloc_mark(__isl_take isl_id *id,
917 __isl_take isl_ast_node *node)
919 isl_ctx *ctx;
920 isl_ast_node *mark;
922 if (!id || !node)
923 goto error;
925 ctx = isl_id_get_ctx(id);
926 mark = isl_ast_node_alloc(ctx, isl_ast_node_mark);
927 if (!mark)
928 goto error;
930 mark->u.m.mark = id;
931 mark->u.m.node = node;
933 return mark;
934 error:
935 isl_id_free(id);
936 isl_ast_node_free(node);
937 return NULL;
940 /* Create a user node evaluating "expr".
942 __isl_give isl_ast_node *isl_ast_node_alloc_user(__isl_take isl_ast_expr *expr)
944 isl_ctx *ctx;
945 isl_ast_node *node;
947 if (!expr)
948 return NULL;
950 ctx = isl_ast_expr_get_ctx(expr);
951 node = isl_ast_node_alloc(ctx, isl_ast_node_user);
952 if (!node)
953 goto error;
955 node->u.e.expr = expr;
957 return node;
958 error:
959 isl_ast_expr_free(expr);
960 return NULL;
963 /* Create a block node with the given children.
965 __isl_give isl_ast_node *isl_ast_node_alloc_block(
966 __isl_take isl_ast_node_list *list)
968 isl_ast_node *node;
969 isl_ctx *ctx;
971 if (!list)
972 return NULL;
974 ctx = isl_ast_node_list_get_ctx(list);
975 node = isl_ast_node_alloc(ctx, isl_ast_node_block);
976 if (!node)
977 goto error;
979 node->u.b.children = list;
981 return node;
982 error:
983 isl_ast_node_list_free(list);
984 return NULL;
987 /* Represent the given list of nodes as a single node, either by
988 * extract the node from a single element list or by creating
989 * a block node with the list of nodes as children.
991 __isl_give isl_ast_node *isl_ast_node_from_ast_node_list(
992 __isl_take isl_ast_node_list *list)
994 isl_size n;
995 isl_ast_node *node;
997 n = isl_ast_node_list_n_ast_node(list);
998 if (n < 0)
999 goto error;
1000 if (n != 1)
1001 return isl_ast_node_alloc_block(list);
1003 node = isl_ast_node_list_get_ast_node(list, 0);
1004 isl_ast_node_list_free(list);
1006 return node;
1007 error:
1008 isl_ast_node_list_free(list);
1009 return NULL;
1012 __isl_give isl_ast_node *isl_ast_node_copy(__isl_keep isl_ast_node *node)
1014 if (!node)
1015 return NULL;
1017 node->ref++;
1018 return node;
1021 /* Return a fresh copy of "node".
1023 * In the case of a degenerate for node, take into account
1024 * that "cond" and "inc" are NULL.
1026 __isl_give isl_ast_node *isl_ast_node_dup(__isl_keep isl_ast_node *node)
1028 isl_ast_node *dup;
1030 if (!node)
1031 return NULL;
1033 dup = isl_ast_node_alloc(isl_ast_node_get_ctx(node), node->type);
1034 if (!dup)
1035 return NULL;
1037 switch (node->type) {
1038 case isl_ast_node_if:
1039 dup->u.i.guard = isl_ast_expr_copy(node->u.i.guard);
1040 dup->u.i.then = isl_ast_node_copy(node->u.i.then);
1041 dup->u.i.else_node = isl_ast_node_copy(node->u.i.else_node);
1042 if (!dup->u.i.guard || !dup->u.i.then ||
1043 (node->u.i.else_node && !dup->u.i.else_node))
1044 return isl_ast_node_free(dup);
1045 break;
1046 case isl_ast_node_for:
1047 dup->u.f.degenerate = node->u.f.degenerate;
1048 dup->u.f.iterator = isl_ast_expr_copy(node->u.f.iterator);
1049 dup->u.f.init = isl_ast_expr_copy(node->u.f.init);
1050 dup->u.f.body = isl_ast_node_copy(node->u.f.body);
1051 if (!dup->u.f.iterator || !dup->u.f.init || !dup->u.f.body)
1052 return isl_ast_node_free(dup);
1053 if (node->u.f.degenerate)
1054 break;
1055 dup->u.f.cond = isl_ast_expr_copy(node->u.f.cond);
1056 dup->u.f.inc = isl_ast_expr_copy(node->u.f.inc);
1057 if (!dup->u.f.cond || !dup->u.f.inc)
1058 return isl_ast_node_free(dup);
1059 break;
1060 case isl_ast_node_block:
1061 dup->u.b.children = isl_ast_node_list_copy(node->u.b.children);
1062 if (!dup->u.b.children)
1063 return isl_ast_node_free(dup);
1064 break;
1065 case isl_ast_node_mark:
1066 dup->u.m.mark = isl_id_copy(node->u.m.mark);
1067 dup->u.m.node = isl_ast_node_copy(node->u.m.node);
1068 if (!dup->u.m.mark || !dup->u.m.node)
1069 return isl_ast_node_free(dup);
1070 break;
1071 case isl_ast_node_user:
1072 dup->u.e.expr = isl_ast_expr_copy(node->u.e.expr);
1073 if (!dup->u.e.expr)
1074 return isl_ast_node_free(dup);
1075 break;
1076 case isl_ast_node_error:
1077 break;
1080 if (!node->annotation)
1081 return dup;
1082 dup->annotation = isl_id_copy(node->annotation);
1083 if (!dup->annotation)
1084 return isl_ast_node_free(dup);
1086 return dup;
1089 __isl_give isl_ast_node *isl_ast_node_cow(__isl_take isl_ast_node *node)
1091 if (!node)
1092 return NULL;
1094 if (node->ref == 1)
1095 return node;
1096 node->ref--;
1097 return isl_ast_node_dup(node);
1100 __isl_null isl_ast_node *isl_ast_node_free(__isl_take isl_ast_node *node)
1102 if (!node)
1103 return NULL;
1105 if (--node->ref > 0)
1106 return NULL;
1108 switch (node->type) {
1109 case isl_ast_node_if:
1110 isl_ast_expr_free(node->u.i.guard);
1111 isl_ast_node_free(node->u.i.then);
1112 isl_ast_node_free(node->u.i.else_node);
1113 break;
1114 case isl_ast_node_for:
1115 isl_ast_expr_free(node->u.f.iterator);
1116 isl_ast_expr_free(node->u.f.init);
1117 isl_ast_expr_free(node->u.f.cond);
1118 isl_ast_expr_free(node->u.f.inc);
1119 isl_ast_node_free(node->u.f.body);
1120 break;
1121 case isl_ast_node_block:
1122 isl_ast_node_list_free(node->u.b.children);
1123 break;
1124 case isl_ast_node_mark:
1125 isl_id_free(node->u.m.mark);
1126 isl_ast_node_free(node->u.m.node);
1127 break;
1128 case isl_ast_node_user:
1129 isl_ast_expr_free(node->u.e.expr);
1130 break;
1131 case isl_ast_node_error:
1132 break;
1135 isl_id_free(node->annotation);
1136 isl_ctx_deref(node->ctx);
1137 free(node);
1139 return NULL;
1142 /* Replace the body of the for node "node" by "body".
1144 __isl_give isl_ast_node *isl_ast_node_for_set_body(
1145 __isl_take isl_ast_node *node, __isl_take isl_ast_node *body)
1147 node = isl_ast_node_cow(node);
1148 if (!node || !body)
1149 goto error;
1150 if (node->type != isl_ast_node_for)
1151 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1152 "not a for node", goto error);
1154 isl_ast_node_free(node->u.f.body);
1155 node->u.f.body = body;
1157 return node;
1158 error:
1159 isl_ast_node_free(node);
1160 isl_ast_node_free(body);
1161 return NULL;
1164 __isl_give isl_ast_node *isl_ast_node_for_get_body(
1165 __isl_keep isl_ast_node *node)
1167 if (!node)
1168 return NULL;
1169 if (node->type != isl_ast_node_for)
1170 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1171 "not a for node", return NULL);
1172 return isl_ast_node_copy(node->u.f.body);
1175 /* Mark the given for node as being degenerate.
1177 __isl_give isl_ast_node *isl_ast_node_for_mark_degenerate(
1178 __isl_take isl_ast_node *node)
1180 node = isl_ast_node_cow(node);
1181 if (!node)
1182 return NULL;
1183 node->u.f.degenerate = 1;
1184 return node;
1187 isl_bool isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node *node)
1189 if (!node)
1190 return isl_bool_error;
1191 if (node->type != isl_ast_node_for)
1192 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1193 "not a for node", return isl_bool_error);
1194 return isl_bool_ok(node->u.f.degenerate);
1197 __isl_give isl_ast_expr *isl_ast_node_for_get_iterator(
1198 __isl_keep isl_ast_node *node)
1200 if (!node)
1201 return NULL;
1202 if (node->type != isl_ast_node_for)
1203 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1204 "not a for node", return NULL);
1205 return isl_ast_expr_copy(node->u.f.iterator);
1208 __isl_give isl_ast_expr *isl_ast_node_for_get_init(
1209 __isl_keep isl_ast_node *node)
1211 if (!node)
1212 return NULL;
1213 if (node->type != isl_ast_node_for)
1214 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1215 "not a for node", return NULL);
1216 return isl_ast_expr_copy(node->u.f.init);
1219 /* Return the condition expression of the given for node.
1221 * If the for node is degenerate, then the condition is not explicitly
1222 * stored in the node. Instead, it is constructed as
1224 * iterator <= init
1226 __isl_give isl_ast_expr *isl_ast_node_for_get_cond(
1227 __isl_keep isl_ast_node *node)
1229 if (!node)
1230 return NULL;
1231 if (node->type != isl_ast_node_for)
1232 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1233 "not a for node", return NULL);
1234 if (!node->u.f.degenerate)
1235 return isl_ast_expr_copy(node->u.f.cond);
1237 return isl_ast_expr_alloc_binary(isl_ast_expr_op_le,
1238 isl_ast_expr_copy(node->u.f.iterator),
1239 isl_ast_expr_copy(node->u.f.init));
1242 /* Return the increment of the given for node.
1244 * If the for node is degenerate, then the increment is not explicitly
1245 * stored in the node. We simply return "1".
1247 __isl_give isl_ast_expr *isl_ast_node_for_get_inc(
1248 __isl_keep isl_ast_node *node)
1250 if (!node)
1251 return NULL;
1252 if (node->type != isl_ast_node_for)
1253 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1254 "not a for node", return NULL);
1255 if (!node->u.f.degenerate)
1256 return isl_ast_expr_copy(node->u.f.inc);
1257 return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node), 1);
1260 /* Replace the then branch of the if node "node" by "child".
1262 __isl_give isl_ast_node *isl_ast_node_if_set_then(
1263 __isl_take isl_ast_node *node, __isl_take isl_ast_node *child)
1265 node = isl_ast_node_cow(node);
1266 if (!node || !child)
1267 goto error;
1268 if (node->type != isl_ast_node_if)
1269 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1270 "not an if node", goto error);
1272 isl_ast_node_free(node->u.i.then);
1273 node->u.i.then = child;
1275 return node;
1276 error:
1277 isl_ast_node_free(node);
1278 isl_ast_node_free(child);
1279 return NULL;
1282 /* Return the then-node of the given if-node.
1284 __isl_give isl_ast_node *isl_ast_node_if_get_then_node(
1285 __isl_keep isl_ast_node *node)
1287 if (!node)
1288 return NULL;
1289 if (node->type != isl_ast_node_if)
1290 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1291 "not an if node", return NULL);
1292 return isl_ast_node_copy(node->u.i.then);
1295 /* This is an alternative name for the function above.
1297 __isl_give isl_ast_node *isl_ast_node_if_get_then(
1298 __isl_keep isl_ast_node *node)
1300 return isl_ast_node_if_get_then_node(node);
1303 /* Does the given if-node have an else-node?
1305 isl_bool isl_ast_node_if_has_else_node(__isl_keep isl_ast_node *node)
1307 if (!node)
1308 return isl_bool_error;
1309 if (node->type != isl_ast_node_if)
1310 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1311 "not an if node", return isl_bool_error);
1312 return isl_bool_ok(node->u.i.else_node != NULL);
1315 /* This is an alternative name for the function above.
1317 isl_bool isl_ast_node_if_has_else(__isl_keep isl_ast_node *node)
1319 return isl_ast_node_if_has_else_node(node);
1322 /* Return the else-node of the given if-node,
1323 * assuming there is one.
1325 __isl_give isl_ast_node *isl_ast_node_if_get_else_node(
1326 __isl_keep isl_ast_node *node)
1328 if (!node)
1329 return NULL;
1330 if (node->type != isl_ast_node_if)
1331 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1332 "not an if node", return NULL);
1333 return isl_ast_node_copy(node->u.i.else_node);
1336 /* This is an alternative name for the function above.
1338 __isl_give isl_ast_node *isl_ast_node_if_get_else(
1339 __isl_keep isl_ast_node *node)
1341 return isl_ast_node_if_get_else_node(node);
1344 __isl_give isl_ast_expr *isl_ast_node_if_get_cond(
1345 __isl_keep isl_ast_node *node)
1347 if (!node)
1348 return NULL;
1349 if (node->type != isl_ast_node_if)
1350 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1351 "not a guard node", return NULL);
1352 return isl_ast_expr_copy(node->u.i.guard);
1355 __isl_give isl_ast_node_list *isl_ast_node_block_get_children(
1356 __isl_keep isl_ast_node *node)
1358 if (!node)
1359 return NULL;
1360 if (node->type != isl_ast_node_block)
1361 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1362 "not a block node", return NULL);
1363 return isl_ast_node_list_copy(node->u.b.children);
1366 __isl_give isl_ast_expr *isl_ast_node_user_get_expr(
1367 __isl_keep isl_ast_node *node)
1369 if (!node)
1370 return NULL;
1371 if (node->type != isl_ast_node_user)
1372 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1373 "not a user node", return NULL);
1375 return isl_ast_expr_copy(node->u.e.expr);
1378 /* Return the mark identifier of the mark node "node".
1380 __isl_give isl_id *isl_ast_node_mark_get_id(__isl_keep isl_ast_node *node)
1382 if (!node)
1383 return NULL;
1384 if (node->type != isl_ast_node_mark)
1385 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1386 "not a mark node", return NULL);
1388 return isl_id_copy(node->u.m.mark);
1391 /* Return the node marked by mark node "node".
1393 __isl_give isl_ast_node *isl_ast_node_mark_get_node(
1394 __isl_keep isl_ast_node *node)
1396 if (!node)
1397 return NULL;
1398 if (node->type != isl_ast_node_mark)
1399 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1400 "not a mark node", return NULL);
1402 return isl_ast_node_copy(node->u.m.node);
1405 __isl_give isl_id *isl_ast_node_get_annotation(__isl_keep isl_ast_node *node)
1407 return node ? isl_id_copy(node->annotation) : NULL;
1410 /* Replace node->annotation by "annotation".
1412 __isl_give isl_ast_node *isl_ast_node_set_annotation(
1413 __isl_take isl_ast_node *node, __isl_take isl_id *annotation)
1415 node = isl_ast_node_cow(node);
1416 if (!node || !annotation)
1417 goto error;
1419 isl_id_free(node->annotation);
1420 node->annotation = annotation;
1422 return node;
1423 error:
1424 isl_id_free(annotation);
1425 return isl_ast_node_free(node);
1428 /* Traverse the elements of "list" and all their descendants
1429 * in depth first preorder.
1431 * Return isl_stat_ok on success and isl_stat_error on failure.
1433 static isl_stat nodelist_foreach(__isl_keep isl_ast_node_list *list,
1434 isl_bool (*fn)(__isl_keep isl_ast_node *node, void *user), void *user)
1436 int i;
1438 if (!list)
1439 return isl_stat_error;
1441 for (i = 0; i < list->n; ++i) {
1442 isl_stat ok;
1443 isl_ast_node *node = list->p[i];
1445 ok = isl_ast_node_foreach_descendant_top_down(node, fn, user);
1446 if (ok < 0)
1447 return isl_stat_error;
1450 return isl_stat_ok;
1453 /* Traverse the descendants of "node" (including the node itself)
1454 * in depth first preorder.
1456 * If "fn" returns isl_bool_error on any of the nodes, then the traversal
1457 * is aborted.
1458 * If "fn" returns isl_bool_false on any of the nodes, then the subtree rooted
1459 * at that node is skipped.
1461 * Return isl_stat_ok on success and isl_stat_error on failure.
1463 isl_stat isl_ast_node_foreach_descendant_top_down(
1464 __isl_keep isl_ast_node *node,
1465 isl_bool (*fn)(__isl_keep isl_ast_node *node, void *user), void *user)
1467 isl_bool more;
1468 isl_stat ok;
1470 if (!node)
1471 return isl_stat_error;
1473 more = fn(node, user);
1474 if (more < 0)
1475 return isl_stat_error;
1476 if (!more)
1477 return isl_stat_ok;
1479 switch (node->type) {
1480 case isl_ast_node_for:
1481 node = node->u.f.body;
1482 return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1483 case isl_ast_node_if:
1484 ok = isl_ast_node_foreach_descendant_top_down(node->u.i.then,
1485 fn, user);
1486 if (ok < 0)
1487 return isl_stat_error;
1488 if (!node->u.i.else_node)
1489 return isl_stat_ok;
1490 node = node->u.i.else_node;
1491 return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1492 case isl_ast_node_block:
1493 return nodelist_foreach(node->u.b.children, fn, user);
1494 case isl_ast_node_mark:
1495 node = node->u.m.node;
1496 return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1497 case isl_ast_node_user:
1498 break;
1499 case isl_ast_node_error:
1500 return isl_stat_error;
1503 return isl_stat_ok;
1506 /* Textual C representation of the various operators.
1508 static char *op_str_c[] = {
1509 [isl_ast_expr_op_and] = "&&",
1510 [isl_ast_expr_op_and_then] = "&&",
1511 [isl_ast_expr_op_or] = "||",
1512 [isl_ast_expr_op_or_else] = "||",
1513 [isl_ast_expr_op_max] = "max",
1514 [isl_ast_expr_op_min] = "min",
1515 [isl_ast_expr_op_minus] = "-",
1516 [isl_ast_expr_op_add] = "+",
1517 [isl_ast_expr_op_sub] = "-",
1518 [isl_ast_expr_op_mul] = "*",
1519 [isl_ast_expr_op_fdiv_q] = "floord",
1520 [isl_ast_expr_op_pdiv_q] = "/",
1521 [isl_ast_expr_op_pdiv_r] = "%",
1522 [isl_ast_expr_op_zdiv_r] = "%",
1523 [isl_ast_expr_op_div] = "/",
1524 [isl_ast_expr_op_eq] = "==",
1525 [isl_ast_expr_op_le] = "<=",
1526 [isl_ast_expr_op_ge] = ">=",
1527 [isl_ast_expr_op_lt] = "<",
1528 [isl_ast_expr_op_gt] = ">",
1529 [isl_ast_expr_op_member] = ".",
1530 [isl_ast_expr_op_address_of] = "&"
1533 /* Precedence in C of the various operators.
1534 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1535 * Lowest value means highest precedence.
1537 static int op_prec[] = {
1538 [isl_ast_expr_op_and] = 13,
1539 [isl_ast_expr_op_and_then] = 13,
1540 [isl_ast_expr_op_or] = 14,
1541 [isl_ast_expr_op_or_else] = 14,
1542 [isl_ast_expr_op_max] = 2,
1543 [isl_ast_expr_op_min] = 2,
1544 [isl_ast_expr_op_minus] = 3,
1545 [isl_ast_expr_op_add] = 6,
1546 [isl_ast_expr_op_sub] = 6,
1547 [isl_ast_expr_op_mul] = 5,
1548 [isl_ast_expr_op_div] = 5,
1549 [isl_ast_expr_op_fdiv_q] = 2,
1550 [isl_ast_expr_op_pdiv_q] = 5,
1551 [isl_ast_expr_op_pdiv_r] = 5,
1552 [isl_ast_expr_op_zdiv_r] = 5,
1553 [isl_ast_expr_op_cond] = 15,
1554 [isl_ast_expr_op_select] = 15,
1555 [isl_ast_expr_op_eq] = 9,
1556 [isl_ast_expr_op_le] = 8,
1557 [isl_ast_expr_op_ge] = 8,
1558 [isl_ast_expr_op_lt] = 8,
1559 [isl_ast_expr_op_gt] = 8,
1560 [isl_ast_expr_op_call] = 2,
1561 [isl_ast_expr_op_access] = 2,
1562 [isl_ast_expr_op_member] = 2,
1563 [isl_ast_expr_op_address_of] = 3
1566 /* Is the operator left-to-right associative?
1568 static int op_left[] = {
1569 [isl_ast_expr_op_and] = 1,
1570 [isl_ast_expr_op_and_then] = 1,
1571 [isl_ast_expr_op_or] = 1,
1572 [isl_ast_expr_op_or_else] = 1,
1573 [isl_ast_expr_op_max] = 1,
1574 [isl_ast_expr_op_min] = 1,
1575 [isl_ast_expr_op_minus] = 0,
1576 [isl_ast_expr_op_add] = 1,
1577 [isl_ast_expr_op_sub] = 1,
1578 [isl_ast_expr_op_mul] = 1,
1579 [isl_ast_expr_op_div] = 1,
1580 [isl_ast_expr_op_fdiv_q] = 1,
1581 [isl_ast_expr_op_pdiv_q] = 1,
1582 [isl_ast_expr_op_pdiv_r] = 1,
1583 [isl_ast_expr_op_zdiv_r] = 1,
1584 [isl_ast_expr_op_cond] = 0,
1585 [isl_ast_expr_op_select] = 0,
1586 [isl_ast_expr_op_eq] = 1,
1587 [isl_ast_expr_op_le] = 1,
1588 [isl_ast_expr_op_ge] = 1,
1589 [isl_ast_expr_op_lt] = 1,
1590 [isl_ast_expr_op_gt] = 1,
1591 [isl_ast_expr_op_call] = 1,
1592 [isl_ast_expr_op_access] = 1,
1593 [isl_ast_expr_op_member] = 1,
1594 [isl_ast_expr_op_address_of] = 0
1597 static int is_and(enum isl_ast_expr_op_type op)
1599 return op == isl_ast_expr_op_and || op == isl_ast_expr_op_and_then;
1602 static int is_or(enum isl_ast_expr_op_type op)
1604 return op == isl_ast_expr_op_or || op == isl_ast_expr_op_or_else;
1607 static int is_add_sub(enum isl_ast_expr_op_type op)
1609 return op == isl_ast_expr_op_add || op == isl_ast_expr_op_sub;
1612 static int is_div_mod(enum isl_ast_expr_op_type op)
1614 return op == isl_ast_expr_op_div ||
1615 op == isl_ast_expr_op_pdiv_r ||
1616 op == isl_ast_expr_op_zdiv_r;
1619 static __isl_give isl_printer *print_ast_expr_c(__isl_take isl_printer *p,
1620 __isl_keep isl_ast_expr *expr);
1622 /* Do we need/want parentheses around "expr" as a subexpression of
1623 * an "op" operation? If "left" is set, then "expr" is the left-most
1624 * operand.
1626 * We only need parentheses if "expr" represents an operation.
1628 * If op has a higher precedence than expr->u.op.op, then we need
1629 * parentheses.
1630 * If op and expr->u.op.op have the same precedence, but the operations
1631 * are performed in an order that is different from the associativity,
1632 * then we need parentheses.
1634 * An and inside an or technically does not require parentheses,
1635 * but some compilers complain about that, so we add them anyway.
1637 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1638 * difficult to read, so we add parentheses for those as well.
1640 static int sub_expr_need_parens(enum isl_ast_expr_op_type op,
1641 __isl_keep isl_ast_expr *expr, int left)
1643 if (expr->type != isl_ast_expr_op)
1644 return 0;
1646 if (op_prec[expr->u.op.op] > op_prec[op])
1647 return 1;
1648 if (op_prec[expr->u.op.op] == op_prec[op] && left != op_left[op])
1649 return 1;
1651 if (is_or(op) && is_and(expr->u.op.op))
1652 return 1;
1653 if (op == isl_ast_expr_op_mul && expr->u.op.op != isl_ast_expr_op_mul &&
1654 op_prec[expr->u.op.op] == op_prec[op])
1655 return 1;
1656 if (is_add_sub(op) && is_div_mod(expr->u.op.op))
1657 return 1;
1659 return 0;
1662 /* Print "expr" as a subexpression of an "op" operation in C format.
1663 * If "left" is set, then "expr" is the left-most operand.
1665 static __isl_give isl_printer *print_sub_expr_c(__isl_take isl_printer *p,
1666 enum isl_ast_expr_op_type op, __isl_keep isl_ast_expr *expr, int left)
1668 int need_parens;
1670 need_parens = sub_expr_need_parens(op, expr, left);
1672 if (need_parens)
1673 p = isl_printer_print_str(p, "(");
1674 p = print_ast_expr_c(p, expr);
1675 if (need_parens)
1676 p = isl_printer_print_str(p, ")");
1677 return p;
1680 #define isl_ast_expr_op_last isl_ast_expr_op_address_of
1682 /* Data structure that holds the user-specified textual
1683 * representations for the operators in C format.
1684 * The entries are either NULL or copies of strings.
1685 * A NULL entry means that the default name should be used.
1687 struct isl_ast_expr_op_names {
1688 char *op_str[isl_ast_expr_op_last + 1];
1691 /* Create an empty struct isl_ast_expr_op_names.
1693 static void *create_names(isl_ctx *ctx)
1695 return isl_calloc_type(ctx, struct isl_ast_expr_op_names);
1698 /* Free a struct isl_ast_expr_op_names along with all memory
1699 * owned by the struct.
1701 static void free_names(void *user)
1703 int i;
1704 struct isl_ast_expr_op_names *names = user;
1706 if (!user)
1707 return;
1709 for (i = 0; i <= isl_ast_expr_op_last; ++i)
1710 free(names->op_str[i]);
1711 free(user);
1714 /* Create an identifier that is used to store
1715 * an isl_ast_expr_op_names note.
1717 static __isl_give isl_id *names_id(isl_ctx *ctx)
1719 return isl_id_alloc(ctx, "isl_ast_expr_op_type_names", NULL);
1722 /* Ensure that "p" has a note identified by "id".
1723 * If there is no such note yet, then it is created by "note_create" and
1724 * scheduled do be freed by "note_free".
1726 static __isl_give isl_printer *alloc_note(__isl_take isl_printer *p,
1727 __isl_keep isl_id *id, void *(*note_create)(isl_ctx *),
1728 void (*note_free)(void *))
1730 isl_ctx *ctx;
1731 isl_id *note_id;
1732 isl_bool has_note;
1733 void *note;
1735 has_note = isl_printer_has_note(p, id);
1736 if (has_note < 0)
1737 return isl_printer_free(p);
1738 if (has_note)
1739 return p;
1741 ctx = isl_printer_get_ctx(p);
1742 note = note_create(ctx);
1743 if (!note)
1744 return isl_printer_free(p);
1745 note_id = isl_id_alloc(ctx, NULL, note);
1746 if (!note_id)
1747 note_free(note);
1748 else
1749 note_id = isl_id_set_free_user(note_id, note_free);
1751 p = isl_printer_set_note(p, isl_id_copy(id), note_id);
1753 return p;
1756 /* Ensure that "p" has an isl_ast_expr_op_names note identified by "id".
1758 static __isl_give isl_printer *alloc_names(__isl_take isl_printer *p,
1759 __isl_keep isl_id *id)
1761 return alloc_note(p, id, &create_names, &free_names);
1764 /* Retrieve the note identified by "id" from "p".
1765 * The note is assumed to exist.
1767 static void *get_note(__isl_keep isl_printer *p, __isl_keep isl_id *id)
1769 void *note;
1771 id = isl_printer_get_note(p, isl_id_copy(id));
1772 note = isl_id_get_user(id);
1773 isl_id_free(id);
1775 return note;
1778 /* Use "name" to print operations of type "type" to "p".
1780 * Store the name in an isl_ast_expr_op_names note attached to "p", such that
1781 * it can be retrieved by get_op_str.
1783 __isl_give isl_printer *isl_ast_expr_op_type_set_print_name(
1784 __isl_take isl_printer *p, enum isl_ast_expr_op_type type,
1785 __isl_keep const char *name)
1787 isl_id *id;
1788 struct isl_ast_expr_op_names *names;
1790 if (!p)
1791 return NULL;
1792 if (type > isl_ast_expr_op_last)
1793 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
1794 "invalid type", return isl_printer_free(p));
1796 id = names_id(isl_printer_get_ctx(p));
1797 p = alloc_names(p, id);
1798 names = get_note(p, id);
1799 isl_id_free(id);
1800 if (!names)
1801 return isl_printer_free(p);
1802 free(names->op_str[type]);
1803 names->op_str[type] = strdup(name);
1805 return p;
1808 /* This is an alternative name for the function above.
1810 __isl_give isl_printer *isl_ast_op_type_set_print_name(
1811 __isl_take isl_printer *p, enum isl_ast_expr_op_type type,
1812 __isl_keep const char *name)
1814 return isl_ast_expr_op_type_set_print_name(p, type, name);
1817 /* Return the textual representation of "type" in C format.
1819 * If there is a user-specified name in an isl_ast_expr_op_names note
1820 * associated to "p", then return that.
1821 * Otherwise, return the default name in op_str_c.
1823 static const char *get_op_str_c(__isl_keep isl_printer *p,
1824 enum isl_ast_expr_op_type type)
1826 isl_id *id;
1827 isl_bool has_names;
1828 struct isl_ast_expr_op_names *names = NULL;
1830 id = names_id(isl_printer_get_ctx(p));
1831 has_names = isl_printer_has_note(p, id);
1832 if (has_names >= 0 && has_names)
1833 names = get_note(p, id);
1834 isl_id_free(id);
1835 if (names && names->op_str[type])
1836 return names->op_str[type];
1837 return op_str_c[type];
1840 /* Print a min or max reduction "expr" in C format.
1842 static __isl_give isl_printer *print_min_max_c(__isl_take isl_printer *p,
1843 __isl_keep isl_ast_expr *expr)
1845 int i = 0;
1847 for (i = 1; i < expr->u.op.n_arg; ++i) {
1848 p = isl_printer_print_str(p, get_op_str_c(p, expr->u.op.op));
1849 p = isl_printer_print_str(p, "(");
1851 p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1852 for (i = 1; i < expr->u.op.n_arg; ++i) {
1853 p = isl_printer_print_str(p, ", ");
1854 p = print_ast_expr_c(p, expr->u.op.args[i]);
1855 p = isl_printer_print_str(p, ")");
1858 return p;
1861 /* Print a function call "expr" in C format.
1863 * The first argument represents the function to be called.
1865 static __isl_give isl_printer *print_call_c(__isl_take isl_printer *p,
1866 __isl_keep isl_ast_expr *expr)
1868 int i = 0;
1870 p = print_ast_expr_c(p, expr->u.op.args[0]);
1871 p = isl_printer_print_str(p, "(");
1872 for (i = 1; i < expr->u.op.n_arg; ++i) {
1873 if (i != 1)
1874 p = isl_printer_print_str(p, ", ");
1875 p = print_ast_expr_c(p, expr->u.op.args[i]);
1877 p = isl_printer_print_str(p, ")");
1879 return p;
1882 /* Print an array access "expr" in C format.
1884 * The first argument represents the array being accessed.
1886 static __isl_give isl_printer *print_access_c(__isl_take isl_printer *p,
1887 __isl_keep isl_ast_expr *expr)
1889 int i = 0;
1891 p = print_ast_expr_c(p, expr->u.op.args[0]);
1892 for (i = 1; i < expr->u.op.n_arg; ++i) {
1893 p = isl_printer_print_str(p, "[");
1894 p = print_ast_expr_c(p, expr->u.op.args[i]);
1895 p = isl_printer_print_str(p, "]");
1898 return p;
1901 /* Print "expr" to "p" in C format.
1903 static __isl_give isl_printer *print_ast_expr_c(__isl_take isl_printer *p,
1904 __isl_keep isl_ast_expr *expr)
1906 if (!p)
1907 return NULL;
1908 if (!expr)
1909 return isl_printer_free(p);
1911 switch (expr->type) {
1912 case isl_ast_expr_op:
1913 if (expr->u.op.op == isl_ast_expr_op_call) {
1914 p = print_call_c(p, expr);
1915 break;
1917 if (expr->u.op.op == isl_ast_expr_op_access) {
1918 p = print_access_c(p, expr);
1919 break;
1921 if (expr->u.op.n_arg == 1) {
1922 p = isl_printer_print_str(p,
1923 get_op_str_c(p, expr->u.op.op));
1924 p = print_sub_expr_c(p, expr->u.op.op,
1925 expr->u.op.args[0], 0);
1926 break;
1928 if (expr->u.op.op == isl_ast_expr_op_fdiv_q) {
1929 const char *name;
1931 name = get_op_str_c(p, isl_ast_expr_op_fdiv_q);
1932 p = isl_printer_print_str(p, name);
1933 p = isl_printer_print_str(p, "(");
1934 p = print_ast_expr_c(p, expr->u.op.args[0]);
1935 p = isl_printer_print_str(p, ", ");
1936 p = print_ast_expr_c(p, expr->u.op.args[1]);
1937 p = isl_printer_print_str(p, ")");
1938 break;
1940 if (expr->u.op.op == isl_ast_expr_op_max ||
1941 expr->u.op.op == isl_ast_expr_op_min) {
1942 p = print_min_max_c(p, expr);
1943 break;
1945 if (expr->u.op.op == isl_ast_expr_op_cond ||
1946 expr->u.op.op == isl_ast_expr_op_select) {
1947 p = print_ast_expr_c(p, expr->u.op.args[0]);
1948 p = isl_printer_print_str(p, " ? ");
1949 p = print_ast_expr_c(p, expr->u.op.args[1]);
1950 p = isl_printer_print_str(p, " : ");
1951 p = print_ast_expr_c(p, expr->u.op.args[2]);
1952 break;
1954 if (expr->u.op.n_arg != 2)
1955 isl_die(isl_printer_get_ctx(p), isl_error_internal,
1956 "operation should have two arguments",
1957 return isl_printer_free(p));
1958 p = print_sub_expr_c(p, expr->u.op.op, expr->u.op.args[0], 1);
1959 if (expr->u.op.op != isl_ast_expr_op_member)
1960 p = isl_printer_print_str(p, " ");
1961 p = isl_printer_print_str(p, get_op_str_c(p, expr->u.op.op));
1962 if (expr->u.op.op != isl_ast_expr_op_member)
1963 p = isl_printer_print_str(p, " ");
1964 p = print_sub_expr_c(p, expr->u.op.op, expr->u.op.args[1], 0);
1965 break;
1966 case isl_ast_expr_id:
1967 p = isl_printer_print_str(p, isl_id_get_name(expr->u.id));
1968 break;
1969 case isl_ast_expr_int:
1970 p = isl_printer_print_val(p, expr->u.v);
1971 break;
1972 case isl_ast_expr_error:
1973 break;
1976 return p;
1979 /* Textual representation of the isl_ast_expr_op_type elements
1980 * for use in a YAML representation of an isl_ast_expr.
1982 static char *op_str[] = {
1983 [isl_ast_expr_op_and] = "and",
1984 [isl_ast_expr_op_and_then] = "and_then",
1985 [isl_ast_expr_op_or] = "or",
1986 [isl_ast_expr_op_or_else] = "or_else",
1987 [isl_ast_expr_op_max] = "max",
1988 [isl_ast_expr_op_min] = "min",
1989 [isl_ast_expr_op_minus] = "minus",
1990 [isl_ast_expr_op_add] = "add",
1991 [isl_ast_expr_op_sub] = "sub",
1992 [isl_ast_expr_op_mul] = "mul",
1993 [isl_ast_expr_op_div] = "div",
1994 [isl_ast_expr_op_fdiv_q] = "fdiv_q",
1995 [isl_ast_expr_op_pdiv_q] = "pdiv_q",
1996 [isl_ast_expr_op_pdiv_r] = "pdiv_r",
1997 [isl_ast_expr_op_zdiv_r] = "zdiv_r",
1998 [isl_ast_expr_op_cond] = "cond",
1999 [isl_ast_expr_op_select] = "select",
2000 [isl_ast_expr_op_eq] = "eq",
2001 [isl_ast_expr_op_le] = "le",
2002 [isl_ast_expr_op_lt] = "lt",
2003 [isl_ast_expr_op_ge] = "ge",
2004 [isl_ast_expr_op_gt] = "gt",
2005 [isl_ast_expr_op_call] = "call",
2006 [isl_ast_expr_op_access] = "access",
2007 [isl_ast_expr_op_member] = "member",
2008 [isl_ast_expr_op_address_of] = "address_of"
2011 static __isl_give isl_printer *print_ast_expr_isl(__isl_take isl_printer *p,
2012 __isl_keep isl_ast_expr *expr);
2014 /* Print the arguments of "expr" to "p" in isl format.
2016 * If there are no arguments, then nothing needs to be printed.
2017 * Otherwise add an "args" key to the current mapping with as value
2018 * the list of arguments of "expr".
2020 static __isl_give isl_printer *print_arguments(__isl_take isl_printer *p,
2021 __isl_keep isl_ast_expr *expr)
2023 int i;
2024 isl_size n;
2026 n = isl_ast_expr_get_op_n_arg(expr);
2027 if (n < 0)
2028 return isl_printer_free(p);
2029 if (n == 0)
2030 return p;
2032 p = isl_printer_print_str(p, "args");
2033 p = isl_printer_yaml_next(p);
2034 p = isl_printer_yaml_start_sequence(p);
2035 for (i = 0; i < n; ++i) {
2036 isl_ast_expr *arg;
2038 arg = isl_ast_expr_get_op_arg(expr, i);
2039 p = print_ast_expr_isl(p, arg);
2040 isl_ast_expr_free(arg);
2041 p = isl_printer_yaml_next(p);
2043 p = isl_printer_yaml_end_sequence(p);
2045 return p;
2048 /* Print "expr" to "p" in isl format.
2050 * In particular, print the isl_ast_expr as a YAML document.
2052 static __isl_give isl_printer *print_ast_expr_isl(__isl_take isl_printer *p,
2053 __isl_keep isl_ast_expr *expr)
2055 enum isl_ast_expr_type type;
2056 enum isl_ast_expr_op_type op;
2057 isl_id *id;
2058 isl_val *v;
2060 if (!expr)
2061 return isl_printer_free(p);
2063 p = isl_printer_yaml_start_mapping(p);
2064 type = isl_ast_expr_get_type(expr);
2065 switch (type) {
2066 case isl_ast_expr_error:
2067 return isl_printer_free(p);
2068 case isl_ast_expr_op:
2069 op = isl_ast_expr_get_op_type(expr);
2070 if (op == isl_ast_expr_op_error)
2071 return isl_printer_free(p);
2072 p = isl_printer_print_str(p, "op");
2073 p = isl_printer_yaml_next(p);
2074 p = isl_printer_print_str(p, op_str[op]);
2075 p = isl_printer_yaml_next(p);
2076 p = print_arguments(p, expr);
2077 break;
2078 case isl_ast_expr_id:
2079 p = isl_printer_print_str(p, "id");
2080 p = isl_printer_yaml_next(p);
2081 id = isl_ast_expr_get_id(expr);
2082 p = isl_printer_print_id(p, id);
2083 isl_id_free(id);
2084 break;
2085 case isl_ast_expr_int:
2086 p = isl_printer_print_str(p, "val");
2087 p = isl_printer_yaml_next(p);
2088 v = isl_ast_expr_get_val(expr);
2089 p = isl_printer_print_val(p, v);
2090 isl_val_free(v);
2091 break;
2093 p = isl_printer_yaml_end_mapping(p);
2095 return p;
2098 /* Print "expr" to "p".
2100 * Only an isl and a C format are supported.
2102 __isl_give isl_printer *isl_printer_print_ast_expr(__isl_take isl_printer *p,
2103 __isl_keep isl_ast_expr *expr)
2105 int format;
2107 if (!p)
2108 return NULL;
2110 format = isl_printer_get_output_format(p);
2111 switch (format) {
2112 case ISL_FORMAT_ISL:
2113 p = print_ast_expr_isl(p, expr);
2114 break;
2115 case ISL_FORMAT_C:
2116 p = print_ast_expr_c(p, expr);
2117 break;
2118 default:
2119 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2120 "output format not supported for ast_expr",
2121 return isl_printer_free(p));
2124 return p;
2127 static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
2128 __isl_keep isl_ast_node *node);
2130 /* Print a YAML sequence containing the entries in "list" to "p".
2132 static __isl_give isl_printer *print_ast_node_list(__isl_take isl_printer *p,
2133 __isl_keep isl_ast_node_list *list)
2135 int i;
2136 isl_size n;
2138 n = isl_ast_node_list_n_ast_node(list);
2139 if (n < 0)
2140 return isl_printer_free(p);
2142 p = isl_printer_yaml_start_sequence(p);
2143 for (i = 0; i < n; ++i) {
2144 isl_ast_node *node;
2146 node = isl_ast_node_list_get_ast_node(list, i);
2147 p = print_ast_node_isl(p, node);
2148 isl_ast_node_free(node);
2149 p = isl_printer_yaml_next(p);
2151 p = isl_printer_yaml_end_sequence(p);
2153 return p;
2156 /* Print "node" to "p" in "isl format".
2158 * In particular, print the isl_ast_node as a YAML document.
2160 static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
2161 __isl_keep isl_ast_node *node)
2163 switch (node->type) {
2164 case isl_ast_node_for:
2165 p = isl_printer_yaml_start_mapping(p);
2166 p = isl_printer_print_str(p, "iterator");
2167 p = isl_printer_yaml_next(p);
2168 p = isl_printer_print_ast_expr(p, node->u.f.iterator);
2169 p = isl_printer_yaml_next(p);
2170 if (node->u.f.degenerate) {
2171 p = isl_printer_print_str(p, "value");
2172 p = isl_printer_yaml_next(p);
2173 p = isl_printer_print_ast_expr(p, node->u.f.init);
2174 p = isl_printer_yaml_next(p);
2175 } else {
2176 p = isl_printer_print_str(p, "init");
2177 p = isl_printer_yaml_next(p);
2178 p = isl_printer_print_ast_expr(p, node->u.f.init);
2179 p = isl_printer_yaml_next(p);
2180 p = isl_printer_print_str(p, "cond");
2181 p = isl_printer_yaml_next(p);
2182 p = isl_printer_print_ast_expr(p, node->u.f.cond);
2183 p = isl_printer_yaml_next(p);
2184 p = isl_printer_print_str(p, "inc");
2185 p = isl_printer_yaml_next(p);
2186 p = isl_printer_print_ast_expr(p, node->u.f.inc);
2187 p = isl_printer_yaml_next(p);
2189 if (node->u.f.body) {
2190 p = isl_printer_print_str(p, "body");
2191 p = isl_printer_yaml_next(p);
2192 p = isl_printer_print_ast_node(p, node->u.f.body);
2193 p = isl_printer_yaml_next(p);
2195 p = isl_printer_yaml_end_mapping(p);
2196 break;
2197 case isl_ast_node_mark:
2198 p = isl_printer_yaml_start_mapping(p);
2199 p = isl_printer_print_str(p, "mark");
2200 p = isl_printer_yaml_next(p);
2201 p = isl_printer_print_id(p, node->u.m.mark);
2202 p = isl_printer_yaml_next(p);
2203 p = isl_printer_print_str(p, "node");
2204 p = isl_printer_yaml_next(p);
2205 p = isl_printer_print_ast_node(p, node->u.m.node);
2206 p = isl_printer_yaml_end_mapping(p);
2207 break;
2208 case isl_ast_node_user:
2209 p = isl_printer_yaml_start_mapping(p);
2210 p = isl_printer_print_str(p, "user");
2211 p = isl_printer_yaml_next(p);
2212 p = isl_printer_print_ast_expr(p, node->u.e.expr);
2213 p = isl_printer_yaml_end_mapping(p);
2214 break;
2215 case isl_ast_node_if:
2216 p = isl_printer_yaml_start_mapping(p);
2217 p = isl_printer_print_str(p, "guard");
2218 p = isl_printer_yaml_next(p);
2219 p = isl_printer_print_ast_expr(p, node->u.i.guard);
2220 p = isl_printer_yaml_next(p);
2221 if (node->u.i.then) {
2222 p = isl_printer_print_str(p, "then");
2223 p = isl_printer_yaml_next(p);
2224 p = isl_printer_print_ast_node(p, node->u.i.then);
2225 p = isl_printer_yaml_next(p);
2227 if (node->u.i.else_node) {
2228 p = isl_printer_print_str(p, "else");
2229 p = isl_printer_yaml_next(p);
2230 p = isl_printer_print_ast_node(p, node->u.i.else_node);
2232 p = isl_printer_yaml_end_mapping(p);
2233 break;
2234 case isl_ast_node_block:
2235 p = print_ast_node_list(p, node->u.b.children);
2236 break;
2237 case isl_ast_node_error:
2238 break;
2240 return p;
2243 /* Do we need to print a block around the body "node" of a for or if node?
2245 * If the node is a block, then we need to print a block.
2246 * Also if the node is a degenerate for then we will print it as
2247 * an assignment followed by the body of the for loop, so we need a block
2248 * as well.
2249 * If the node is an if node with an else, then we print a block
2250 * to avoid spurious dangling else warnings emitted by some compilers.
2251 * If the node is a mark, then in principle, we would have to check
2252 * the child of the mark node. However, even if the child would not
2253 * require us to print a block, for readability it is probably best
2254 * to print a block anyway.
2255 * If the ast_always_print_block option has been set, then we print a block.
2257 static int need_block(__isl_keep isl_ast_node *node)
2259 isl_ctx *ctx;
2261 if (node->type == isl_ast_node_block)
2262 return 1;
2263 if (node->type == isl_ast_node_for && node->u.f.degenerate)
2264 return 1;
2265 if (node->type == isl_ast_node_if && node->u.i.else_node)
2266 return 1;
2267 if (node->type == isl_ast_node_mark)
2268 return 1;
2270 ctx = isl_ast_node_get_ctx(node);
2271 return isl_options_get_ast_always_print_block(ctx);
2274 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
2275 __isl_keep isl_ast_node *node,
2276 __isl_keep isl_ast_print_options *options, int in_block, int in_list);
2277 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
2278 __isl_keep isl_ast_node *node,
2279 __isl_keep isl_ast_print_options *options, int new_line,
2280 int force_block);
2282 /* Print the body "node" of a for or if node.
2283 * If "else_node" is set, then it is printed as well.
2284 * If "force_block" is set, then print out the body as a block.
2286 * We first check if we need to print out a block.
2287 * We always print out a block if there is an else node to make
2288 * sure that the else node is matched to the correct if node.
2289 * For consistency, the corresponding else node is also printed as a block.
2291 * If the else node is itself an if, then we print it as
2293 * } else if (..) {
2296 * Otherwise the else node is printed as
2298 * } else {
2299 * node
2302 static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p,
2303 __isl_keep isl_ast_node *node, __isl_keep isl_ast_node *else_node,
2304 __isl_keep isl_ast_print_options *options, int force_block)
2306 if (!node)
2307 return isl_printer_free(p);
2309 if (!force_block && !else_node && !need_block(node)) {
2310 p = isl_printer_end_line(p);
2311 p = isl_printer_indent(p, 2);
2312 p = isl_ast_node_print(node, p,
2313 isl_ast_print_options_copy(options));
2314 p = isl_printer_indent(p, -2);
2315 return p;
2318 p = isl_printer_print_str(p, " {");
2319 p = isl_printer_end_line(p);
2320 p = isl_printer_indent(p, 2);
2321 p = print_ast_node_c(p, node, options, 1, 0);
2322 p = isl_printer_indent(p, -2);
2323 p = isl_printer_start_line(p);
2324 p = isl_printer_print_str(p, "}");
2325 if (else_node) {
2326 if (else_node->type == isl_ast_node_if) {
2327 p = isl_printer_print_str(p, " else ");
2328 p = print_if_c(p, else_node, options, 0, 1);
2329 } else {
2330 p = isl_printer_print_str(p, " else");
2331 p = print_body_c(p, else_node, NULL, options, 1);
2333 } else
2334 p = isl_printer_end_line(p);
2336 return p;
2339 /* Print the start of a compound statement.
2341 static __isl_give isl_printer *start_block(__isl_take isl_printer *p)
2343 p = isl_printer_start_line(p);
2344 p = isl_printer_print_str(p, "{");
2345 p = isl_printer_end_line(p);
2346 p = isl_printer_indent(p, 2);
2348 return p;
2351 /* Print the end of a compound statement.
2353 static __isl_give isl_printer *end_block(__isl_take isl_printer *p)
2355 p = isl_printer_indent(p, -2);
2356 p = isl_printer_start_line(p);
2357 p = isl_printer_print_str(p, "}");
2358 p = isl_printer_end_line(p);
2360 return p;
2363 /* Print the for node "node".
2365 * If the for node is degenerate, it is printed as
2367 * type iterator = init;
2368 * body
2370 * Otherwise, it is printed as
2372 * for (type iterator = init; cond; iterator += inc)
2373 * body
2375 * "in_block" is set if we are currently inside a block.
2376 * "in_list" is set if the current node is not alone in the block.
2377 * If we are not in a block or if the current not is not alone in the block
2378 * then we print a block around a degenerate for loop such that the variable
2379 * declaration will not conflict with any potential other declaration
2380 * of the same variable.
2382 static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p,
2383 __isl_keep isl_ast_node *node,
2384 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
2386 isl_id *id;
2387 const char *name;
2388 const char *type;
2390 type = isl_options_get_ast_iterator_type(isl_printer_get_ctx(p));
2391 if (!node->u.f.degenerate) {
2392 id = isl_ast_expr_get_id(node->u.f.iterator);
2393 name = isl_id_get_name(id);
2394 isl_id_free(id);
2395 p = isl_printer_start_line(p);
2396 p = isl_printer_print_str(p, "for (");
2397 p = isl_printer_print_str(p, type);
2398 p = isl_printer_print_str(p, " ");
2399 p = isl_printer_print_str(p, name);
2400 p = isl_printer_print_str(p, " = ");
2401 p = isl_printer_print_ast_expr(p, node->u.f.init);
2402 p = isl_printer_print_str(p, "; ");
2403 p = isl_printer_print_ast_expr(p, node->u.f.cond);
2404 p = isl_printer_print_str(p, "; ");
2405 p = isl_printer_print_str(p, name);
2406 p = isl_printer_print_str(p, " += ");
2407 p = isl_printer_print_ast_expr(p, node->u.f.inc);
2408 p = isl_printer_print_str(p, ")");
2409 p = print_body_c(p, node->u.f.body, NULL, options, 0);
2410 } else {
2411 id = isl_ast_expr_get_id(node->u.f.iterator);
2412 name = isl_id_get_name(id);
2413 isl_id_free(id);
2414 if (!in_block || in_list)
2415 p = start_block(p);
2416 p = isl_printer_start_line(p);
2417 p = isl_printer_print_str(p, type);
2418 p = isl_printer_print_str(p, " ");
2419 p = isl_printer_print_str(p, name);
2420 p = isl_printer_print_str(p, " = ");
2421 p = isl_printer_print_ast_expr(p, node->u.f.init);
2422 p = isl_printer_print_str(p, ";");
2423 p = isl_printer_end_line(p);
2424 p = print_ast_node_c(p, node->u.f.body, options, 1, 0);
2425 if (!in_block || in_list)
2426 p = end_block(p);
2429 return p;
2432 /* Print the if node "node".
2433 * If "new_line" is set then the if node should be printed on a new line.
2434 * If "force_block" is set, then print out the body as a block.
2436 static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
2437 __isl_keep isl_ast_node *node,
2438 __isl_keep isl_ast_print_options *options, int new_line,
2439 int force_block)
2441 if (new_line)
2442 p = isl_printer_start_line(p);
2443 p = isl_printer_print_str(p, "if (");
2444 p = isl_printer_print_ast_expr(p, node->u.i.guard);
2445 p = isl_printer_print_str(p, ")");
2446 p = print_body_c(p, node->u.i.then, node->u.i.else_node, options,
2447 force_block);
2449 return p;
2452 /* Print the "node" to "p".
2454 * "in_block" is set if we are currently inside a block.
2455 * If so, we do not print a block around the children of a block node.
2456 * We do this to avoid an extra block around the body of a degenerate
2457 * for node.
2459 * "in_list" is set if the current node is not alone in the block.
2461 static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
2462 __isl_keep isl_ast_node *node,
2463 __isl_keep isl_ast_print_options *options, int in_block, int in_list)
2465 switch (node->type) {
2466 case isl_ast_node_for:
2467 if (options->print_for)
2468 return options->print_for(p,
2469 isl_ast_print_options_copy(options),
2470 node, options->print_for_user);
2471 p = print_for_c(p, node, options, in_block, in_list);
2472 break;
2473 case isl_ast_node_if:
2474 p = print_if_c(p, node, options, 1, 0);
2475 break;
2476 case isl_ast_node_block:
2477 if (!in_block)
2478 p = start_block(p);
2479 p = isl_ast_node_list_print(node->u.b.children, p, options);
2480 if (!in_block)
2481 p = end_block(p);
2482 break;
2483 case isl_ast_node_mark:
2484 p = isl_printer_start_line(p);
2485 p = isl_printer_print_str(p, "// ");
2486 p = isl_printer_print_str(p, isl_id_get_name(node->u.m.mark));
2487 p = isl_printer_end_line(p);
2488 p = print_ast_node_c(p, node->u.m.node, options, 0, in_list);
2489 break;
2490 case isl_ast_node_user:
2491 if (options->print_user)
2492 return options->print_user(p,
2493 isl_ast_print_options_copy(options),
2494 node, options->print_user_user);
2495 p = isl_printer_start_line(p);
2496 p = isl_printer_print_ast_expr(p, node->u.e.expr);
2497 p = isl_printer_print_str(p, ";");
2498 p = isl_printer_end_line(p);
2499 break;
2500 case isl_ast_node_error:
2501 break;
2503 return p;
2506 /* Print the for node "node" to "p".
2508 __isl_give isl_printer *isl_ast_node_for_print(__isl_keep isl_ast_node *node,
2509 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2511 if (!node || !options)
2512 goto error;
2513 if (node->type != isl_ast_node_for)
2514 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
2515 "not a for node", goto error);
2516 p = print_for_c(p, node, options, 0, 0);
2517 isl_ast_print_options_free(options);
2518 return p;
2519 error:
2520 isl_ast_print_options_free(options);
2521 isl_printer_free(p);
2522 return NULL;
2525 /* Print the if node "node" to "p".
2527 __isl_give isl_printer *isl_ast_node_if_print(__isl_keep isl_ast_node *node,
2528 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2530 if (!node || !options)
2531 goto error;
2532 if (node->type != isl_ast_node_if)
2533 isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
2534 "not an if node", goto error);
2535 p = print_if_c(p, node, options, 1, 0);
2536 isl_ast_print_options_free(options);
2537 return p;
2538 error:
2539 isl_ast_print_options_free(options);
2540 isl_printer_free(p);
2541 return NULL;
2544 /* Print "node" to "p".
2546 * "node" is assumed to be either the outermost node in an AST or
2547 * a node that is known not to be a block.
2548 * If "node" is a block (and is therefore outermost) and
2549 * if the ast_print_outermost_block options is not set,
2550 * then act as if the printing occurs inside a block, such
2551 * that no "extra" block will get printed.
2553 __isl_give isl_printer *isl_ast_node_print(__isl_keep isl_ast_node *node,
2554 __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2556 int in_block = 0;
2558 if (!options || !node)
2559 goto error;
2560 if (node->type == isl_ast_node_block) {
2561 isl_ctx *ctx;
2563 ctx = isl_ast_node_get_ctx(node);
2564 in_block = !isl_options_get_ast_print_outermost_block(ctx);
2566 p = print_ast_node_c(p, node, options, in_block, 0);
2567 isl_ast_print_options_free(options);
2568 return p;
2569 error:
2570 isl_ast_print_options_free(options);
2571 isl_printer_free(p);
2572 return NULL;
2575 /* Print "node" to "p".
2577 __isl_give isl_printer *isl_printer_print_ast_node(__isl_take isl_printer *p,
2578 __isl_keep isl_ast_node *node)
2580 int format;
2581 isl_ast_print_options *options;
2583 if (!p)
2584 return NULL;
2586 format = isl_printer_get_output_format(p);
2587 switch (format) {
2588 case ISL_FORMAT_ISL:
2589 p = print_ast_node_isl(p, node);
2590 break;
2591 case ISL_FORMAT_C:
2592 options = isl_ast_print_options_alloc(isl_printer_get_ctx(p));
2593 p = isl_ast_node_print(node, p, options);
2594 break;
2595 default:
2596 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2597 "output format not supported for ast_node",
2598 return isl_printer_free(p));
2601 return p;
2604 /* Print the list of nodes "list" to "p".
2606 __isl_give isl_printer *isl_ast_node_list_print(
2607 __isl_keep isl_ast_node_list *list, __isl_take isl_printer *p,
2608 __isl_keep isl_ast_print_options *options)
2610 int i;
2612 if (!p || !list || !options)
2613 return isl_printer_free(p);
2615 for (i = 0; i < list->n; ++i)
2616 p = print_ast_node_c(p, list->p[i], options, 1, 1);
2618 return p;
2621 #define ISL_AST_MACRO_FDIV_Q (1 << 0)
2622 #define ISL_AST_MACRO_MIN (1 << 1)
2623 #define ISL_AST_MACRO_MAX (1 << 2)
2624 #define ISL_AST_MACRO_ALL (ISL_AST_MACRO_FDIV_Q | \
2625 ISL_AST_MACRO_MIN | \
2626 ISL_AST_MACRO_MAX)
2628 /* If "expr" contains an isl_ast_expr_op_min, isl_ast_expr_op_max or
2629 * isl_ast_expr_op_fdiv_q then set the corresponding bit in "macros".
2631 static int ast_expr_required_macros(__isl_keep isl_ast_expr *expr, int macros)
2633 int i;
2635 if (macros == ISL_AST_MACRO_ALL)
2636 return macros;
2638 if (expr->type != isl_ast_expr_op)
2639 return macros;
2641 if (expr->u.op.op == isl_ast_expr_op_min)
2642 macros |= ISL_AST_MACRO_MIN;
2643 if (expr->u.op.op == isl_ast_expr_op_max)
2644 macros |= ISL_AST_MACRO_MAX;
2645 if (expr->u.op.op == isl_ast_expr_op_fdiv_q)
2646 macros |= ISL_AST_MACRO_FDIV_Q;
2648 for (i = 0; i < expr->u.op.n_arg; ++i)
2649 macros = ast_expr_required_macros(expr->u.op.args[i], macros);
2651 return macros;
2654 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
2655 int macros);
2657 /* If "node" contains an isl_ast_expr_op_min, isl_ast_expr_op_max or
2658 * isl_ast_expr_op_fdiv_q then set the corresponding bit in "macros".
2660 static int ast_node_required_macros(__isl_keep isl_ast_node *node, int macros)
2662 if (macros == ISL_AST_MACRO_ALL)
2663 return macros;
2665 switch (node->type) {
2666 case isl_ast_node_for:
2667 macros = ast_expr_required_macros(node->u.f.init, macros);
2668 if (!node->u.f.degenerate) {
2669 macros = ast_expr_required_macros(node->u.f.cond,
2670 macros);
2671 macros = ast_expr_required_macros(node->u.f.inc,
2672 macros);
2674 macros = ast_node_required_macros(node->u.f.body, macros);
2675 break;
2676 case isl_ast_node_if:
2677 macros = ast_expr_required_macros(node->u.i.guard, macros);
2678 macros = ast_node_required_macros(node->u.i.then, macros);
2679 if (node->u.i.else_node)
2680 macros = ast_node_required_macros(node->u.i.else_node,
2681 macros);
2682 break;
2683 case isl_ast_node_block:
2684 macros = ast_node_list_required_macros(node->u.b.children,
2685 macros);
2686 break;
2687 case isl_ast_node_mark:
2688 macros = ast_node_required_macros(node->u.m.node, macros);
2689 break;
2690 case isl_ast_node_user:
2691 macros = ast_expr_required_macros(node->u.e.expr, macros);
2692 break;
2693 case isl_ast_node_error:
2694 break;
2697 return macros;
2700 /* If "list" contains an isl_ast_expr_op_min, isl_ast_expr_op_max or
2701 * isl_ast_expr_op_fdiv_q then set the corresponding bit in "macros".
2703 static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
2704 int macros)
2706 int i;
2708 for (i = 0; i < list->n; ++i)
2709 macros = ast_node_required_macros(list->p[i], macros);
2711 return macros;
2714 /* Data structure for keeping track of whether a macro definition
2715 * for a given type has already been printed.
2716 * The value is zero if no definition has been printed and non-zero otherwise.
2718 struct isl_ast_expr_op_printed {
2719 char printed[isl_ast_expr_op_last + 1];
2722 /* Create an empty struct isl_ast_expr_op_printed.
2724 static void *create_printed(isl_ctx *ctx)
2726 return isl_calloc_type(ctx, struct isl_ast_expr_op_printed);
2729 /* Free a struct isl_ast_expr_op_printed.
2731 static void free_printed(void *user)
2733 free(user);
2736 /* Ensure that "p" has an isl_ast_expr_op_printed note identified by "id".
2738 static __isl_give isl_printer *alloc_printed(__isl_take isl_printer *p,
2739 __isl_keep isl_id *id)
2741 return alloc_note(p, id, &create_printed, &free_printed);
2744 /* Create an identifier that is used to store
2745 * an isl_ast_expr_op_printed note.
2747 static __isl_give isl_id *printed_id(isl_ctx *ctx)
2749 return isl_id_alloc(ctx, "isl_ast_expr_op_type_printed", NULL);
2752 /* Did the user specify that a macro definition should only be
2753 * printed once and has a macro definition for "type" already
2754 * been printed to "p"?
2755 * If definitions should only be printed once, but a definition
2756 * for "p" has not yet been printed, then mark it as having been
2757 * printed so that it will not printed again.
2758 * The actual printing is taken care of by the caller.
2760 static isl_bool already_printed_once(__isl_keep isl_printer *p,
2761 enum isl_ast_expr_op_type type)
2763 isl_ctx *ctx;
2764 isl_id *id;
2765 struct isl_ast_expr_op_printed *printed;
2767 if (!p)
2768 return isl_bool_error;
2770 ctx = isl_printer_get_ctx(p);
2771 if (!isl_options_get_ast_print_macro_once(ctx))
2772 return isl_bool_false;
2774 if (type > isl_ast_expr_op_last)
2775 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
2776 "invalid type", return isl_bool_error);
2778 id = printed_id(isl_printer_get_ctx(p));
2779 p = alloc_printed(p, id);
2780 printed = get_note(p, id);
2781 isl_id_free(id);
2782 if (!printed)
2783 return isl_bool_error;
2785 if (printed->printed[type])
2786 return isl_bool_true;
2788 printed->printed[type] = 1;
2789 return isl_bool_false;
2792 /* Print a macro definition for the operator "type".
2794 * If the user has specified that a macro definition should
2795 * only be printed once to any given printer and if the macro definition
2796 * has already been printed to "p", then do not print the definition.
2798 __isl_give isl_printer *isl_ast_expr_op_type_print_macro(
2799 enum isl_ast_expr_op_type type, __isl_take isl_printer *p)
2801 isl_bool skip;
2803 skip = already_printed_once(p, type);
2804 if (skip < 0)
2805 return isl_printer_free(p);
2806 if (skip)
2807 return p;
2809 switch (type) {
2810 case isl_ast_expr_op_min:
2811 p = isl_printer_start_line(p);
2812 p = isl_printer_print_str(p, "#define ");
2813 p = isl_printer_print_str(p, get_op_str_c(p, type));
2814 p = isl_printer_print_str(p,
2815 "(x,y) ((x) < (y) ? (x) : (y))");
2816 p = isl_printer_end_line(p);
2817 break;
2818 case isl_ast_expr_op_max:
2819 p = isl_printer_start_line(p);
2820 p = isl_printer_print_str(p, "#define ");
2821 p = isl_printer_print_str(p, get_op_str_c(p, type));
2822 p = isl_printer_print_str(p,
2823 "(x,y) ((x) > (y) ? (x) : (y))");
2824 p = isl_printer_end_line(p);
2825 break;
2826 case isl_ast_expr_op_fdiv_q:
2827 p = isl_printer_start_line(p);
2828 p = isl_printer_print_str(p, "#define ");
2829 p = isl_printer_print_str(p, get_op_str_c(p, type));
2830 p = isl_printer_print_str(p,
2831 "(n,d) "
2832 "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
2833 p = isl_printer_end_line(p);
2834 break;
2835 default:
2836 break;
2839 return p;
2842 /* This is an alternative name for the function above.
2844 __isl_give isl_printer *isl_ast_op_type_print_macro(
2845 enum isl_ast_expr_op_type type, __isl_take isl_printer *p)
2847 return isl_ast_expr_op_type_print_macro(type, p);
2850 /* Call "fn" for each type of operation represented in the "macros"
2851 * bit vector.
2853 static isl_stat foreach_ast_expr_op_type(int macros,
2854 isl_stat (*fn)(enum isl_ast_expr_op_type type, void *user), void *user)
2856 if (macros & ISL_AST_MACRO_MIN && fn(isl_ast_expr_op_min, user) < 0)
2857 return isl_stat_error;
2858 if (macros & ISL_AST_MACRO_MAX && fn(isl_ast_expr_op_max, user) < 0)
2859 return isl_stat_error;
2860 if (macros & ISL_AST_MACRO_FDIV_Q &&
2861 fn(isl_ast_expr_op_fdiv_q, user) < 0)
2862 return isl_stat_error;
2864 return isl_stat_ok;
2867 /* Call "fn" for each type of operation that appears in "expr"
2868 * and that requires a macro definition.
2870 isl_stat isl_ast_expr_foreach_ast_expr_op_type(__isl_keep isl_ast_expr *expr,
2871 isl_stat (*fn)(enum isl_ast_expr_op_type type, void *user), void *user)
2873 int macros;
2875 if (!expr)
2876 return isl_stat_error;
2878 macros = ast_expr_required_macros(expr, 0);
2879 return foreach_ast_expr_op_type(macros, fn, user);
2882 /* This is an alternative name for the function above.
2884 isl_stat isl_ast_expr_foreach_ast_op_type(__isl_keep isl_ast_expr *expr,
2885 isl_stat (*fn)(enum isl_ast_expr_op_type type, void *user), void *user)
2887 return isl_ast_expr_foreach_ast_expr_op_type(expr, fn, user);
2890 /* Call "fn" for each type of operation that appears in "node"
2891 * and that requires a macro definition.
2893 isl_stat isl_ast_node_foreach_ast_expr_op_type(__isl_keep isl_ast_node *node,
2894 isl_stat (*fn)(enum isl_ast_expr_op_type type, void *user), void *user)
2896 int macros;
2898 if (!node)
2899 return isl_stat_error;
2901 macros = ast_node_required_macros(node, 0);
2902 return foreach_ast_expr_op_type(macros, fn, user);
2905 /* This is an alternative name for the function above.
2907 isl_stat isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node *node,
2908 isl_stat (*fn)(enum isl_ast_expr_op_type type, void *user), void *user)
2910 return isl_ast_node_foreach_ast_expr_op_type(node, fn, user);
2913 static isl_stat ast_op_type_print_macro(enum isl_ast_expr_op_type type,
2914 void *user)
2916 isl_printer **p = user;
2918 *p = isl_ast_expr_op_type_print_macro(type, *p);
2920 return isl_stat_ok;
2923 /* Print macro definitions for all the macros used in the result
2924 * of printing "expr".
2926 __isl_give isl_printer *isl_ast_expr_print_macros(
2927 __isl_keep isl_ast_expr *expr, __isl_take isl_printer *p)
2929 if (isl_ast_expr_foreach_ast_expr_op_type(expr,
2930 &ast_op_type_print_macro, &p) < 0)
2931 return isl_printer_free(p);
2932 return p;
2935 /* Print macro definitions for all the macros used in the result
2936 * of printing "node".
2938 __isl_give isl_printer *isl_ast_node_print_macros(
2939 __isl_keep isl_ast_node *node, __isl_take isl_printer *p)
2941 if (isl_ast_node_foreach_ast_expr_op_type(node,
2942 &ast_op_type_print_macro, &p) < 0)
2943 return isl_printer_free(p);
2944 return p;
2947 /* Return a string containing C code representing this isl_ast_expr.
2949 __isl_give char *isl_ast_expr_to_C_str(__isl_keep isl_ast_expr *expr)
2951 isl_printer *p;
2952 char *str;
2954 if (!expr)
2955 return NULL;
2957 p = isl_printer_to_str(isl_ast_expr_get_ctx(expr));
2958 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2959 p = isl_printer_print_ast_expr(p, expr);
2961 str = isl_printer_get_str(p);
2963 isl_printer_free(p);
2965 return str;
2968 /* Return a string containing C code representing this isl_ast_node.
2970 __isl_give char *isl_ast_node_to_C_str(__isl_keep isl_ast_node *node)
2972 isl_printer *p;
2973 char *str;
2975 if (!node)
2976 return NULL;
2978 p = isl_printer_to_str(isl_ast_node_get_ctx(node));
2979 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2980 p = isl_printer_print_ast_node(p, node);
2982 str = isl_printer_get_str(p);
2984 isl_printer_free(p);
2986 return str;