2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
22 #include <isl_stream_private.h>
24 #include "isl_polynomial_private.h"
25 #include <isl/union_set.h>
26 #include <isl/union_map.h>
27 #include <isl_mat_private.h>
28 #include <isl_aff_private.h>
29 #include <isl_vec_private.h>
31 #include <isl_val_private.h>
36 struct variable
*next
;
45 static struct vars
*vars_new(struct isl_ctx
*ctx
)
48 v
= isl_alloc_type(ctx
, struct vars
);
57 static void variable_free(struct variable
*var
)
60 struct variable
*next
= var
->next
;
67 static void vars_free(struct vars
*v
)
75 static void vars_drop(struct vars
*v
, int n
)
86 struct variable
*next
= var
->next
;
94 static struct variable
*variable_new(struct vars
*v
, const char *name
, int len
,
98 var
= isl_calloc_type(v
->ctx
, struct variable
);
101 var
->name
= strdup(name
);
102 var
->name
[len
] = '\0';
111 static int vars_pos(struct vars
*v
, const char *s
, int len
)
118 for (q
= v
->v
; q
; q
= q
->next
) {
119 if (strncmp(q
->name
, s
, len
) == 0 && q
->name
[len
] == '\0')
126 v
->v
= variable_new(v
, s
, len
, v
->n
);
134 static int vars_add_anon(struct vars
*v
)
136 v
->v
= variable_new(v
, "", 0, v
->n
);
145 /* Obtain next token, with some preprocessing.
146 * In particular, evaluate expressions of the form x^y,
147 * with x and y values.
149 static struct isl_token
*next_token(__isl_keep isl_stream
*s
)
151 struct isl_token
*tok
, *tok2
;
153 tok
= isl_stream_next_token(s
);
154 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
)
156 if (!isl_stream_eat_if_available(s
, '^'))
158 tok2
= isl_stream_next_token(s
);
159 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
160 isl_stream_error(s
, tok2
, "expecting constant value");
164 isl_int_pow_ui(tok
->u
.v
, tok
->u
.v
, isl_int_get_ui(tok2
->u
.v
));
166 isl_token_free(tok2
);
170 isl_token_free(tok2
);
174 /* Read an isl_val from "s".
176 * The following token sequences are recognized
179 * "-" "infty" -> -infty
184 * where n, d and v are integer constants.
186 __isl_give isl_val
*isl_stream_read_val(__isl_keep isl_stream
*s
)
188 struct isl_token
*tok
= NULL
;
189 struct isl_token
*tok2
= NULL
;
194 isl_stream_error(s
, NULL
, "unexpected EOF");
197 if (tok
->type
== ISL_TOKEN_INFTY
) {
199 return isl_val_infty(s
->ctx
);
201 if (tok
->type
== '-' &&
202 isl_stream_eat_if_available(s
, ISL_TOKEN_INFTY
)) {
204 return isl_val_neginfty(s
->ctx
);
206 if (tok
->type
== ISL_TOKEN_NAN
) {
208 return isl_val_nan(s
->ctx
);
210 if (tok
->type
!= ISL_TOKEN_VALUE
) {
211 isl_stream_error(s
, tok
, "expecting value");
215 if (isl_stream_eat_if_available(s
, '/')) {
216 tok2
= next_token(s
);
218 isl_stream_error(s
, NULL
, "unexpected EOF");
221 if (tok2
->type
!= ISL_TOKEN_VALUE
) {
222 isl_stream_error(s
, tok2
, "expecting value");
225 val
= isl_val_rat_from_isl_int(s
->ctx
, tok
->u
.v
, tok2
->u
.v
);
226 val
= isl_val_normalize(val
);
228 val
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
232 isl_token_free(tok2
);
236 isl_token_free(tok2
);
240 /* Read an isl_val from "str".
242 struct isl_val
*isl_val_read_from_str(struct isl_ctx
*ctx
,
246 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
249 val
= isl_stream_read_val(s
);
254 static int accept_cst_factor(__isl_keep isl_stream
*s
, isl_int
*f
)
256 struct isl_token
*tok
;
259 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
260 isl_stream_error(s
, tok
, "expecting constant value");
264 isl_int_mul(*f
, *f
, tok
->u
.v
);
268 if (isl_stream_eat_if_available(s
, '*'))
269 return accept_cst_factor(s
, f
);
277 /* Given an affine expression aff, return an affine expression
278 * for aff % d, with d the next token on the stream, which is
279 * assumed to be a constant.
281 * We introduce an integer division q = [aff/d] and the result
282 * is set to aff - d q.
284 static __isl_give isl_pw_aff
*affine_mod(__isl_keep isl_stream
*s
,
285 struct vars
*v
, __isl_take isl_pw_aff
*aff
)
287 struct isl_token
*tok
;
291 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
292 isl_stream_error(s
, tok
, "expecting constant value");
296 q
= isl_pw_aff_copy(aff
);
297 q
= isl_pw_aff_scale_down(q
, tok
->u
.v
);
298 q
= isl_pw_aff_floor(q
);
299 q
= isl_pw_aff_scale(q
, tok
->u
.v
);
301 aff
= isl_pw_aff_sub(aff
, q
);
306 isl_pw_aff_free(aff
);
311 static __isl_give isl_pw_aff
*accept_affine(__isl_keep isl_stream
*s
,
312 __isl_take isl_space
*space
, struct vars
*v
);
313 static __isl_give isl_pw_aff_list
*accept_affine_list(__isl_keep isl_stream
*s
,
314 __isl_take isl_space
*dim
, struct vars
*v
);
316 static __isl_give isl_pw_aff
*accept_minmax(__isl_keep isl_stream
*s
,
317 __isl_take isl_space
*dim
, struct vars
*v
)
319 struct isl_token
*tok
;
320 isl_pw_aff_list
*list
= NULL
;
323 tok
= isl_stream_next_token(s
);
326 min
= tok
->type
== ISL_TOKEN_MIN
;
329 if (isl_stream_eat(s
, '('))
332 list
= accept_affine_list(s
, isl_space_copy(dim
), v
);
336 if (isl_stream_eat(s
, ')'))
340 return min
? isl_pw_aff_list_min(list
) : isl_pw_aff_list_max(list
);
343 isl_pw_aff_list_free(list
);
347 /* Is "tok" the start of an integer division?
349 static int is_start_of_div(struct isl_token
*tok
)
353 if (tok
->type
== '[')
355 if (tok
->type
== ISL_TOKEN_FLOOR
)
357 if (tok
->type
== ISL_TOKEN_CEIL
)
359 if (tok
->type
== ISL_TOKEN_FLOORD
)
361 if (tok
->type
== ISL_TOKEN_CEILD
)
366 /* Read an integer division from "s" and return it as an isl_pw_aff.
368 * The integer division can be of the form
370 * [<affine expression>]
371 * floor(<affine expression>)
372 * ceil(<affine expression>)
373 * floord(<affine expression>,<denominator>)
374 * ceild(<affine expression>,<denominator>)
376 static __isl_give isl_pw_aff
*accept_div(__isl_keep isl_stream
*s
,
377 __isl_take isl_space
*dim
, struct vars
*v
)
379 struct isl_token
*tok
;
383 isl_pw_aff
*pwaff
= NULL
;
385 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOORD
))
387 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEILD
))
389 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOOR
))
391 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEIL
))
394 if (isl_stream_eat(s
, '('))
397 if (isl_stream_eat(s
, '['))
401 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
404 if (isl_stream_eat(s
, ','))
410 if (tok
->type
!= ISL_TOKEN_VALUE
) {
411 isl_stream_error(s
, tok
, "expected denominator");
412 isl_stream_push_token(s
, tok
);
415 pwaff
= isl_pw_aff_scale_down(pwaff
, tok
->u
.v
);
420 pwaff
= isl_pw_aff_ceil(pwaff
);
422 pwaff
= isl_pw_aff_floor(pwaff
);
425 if (isl_stream_eat(s
, ')'))
428 if (isl_stream_eat(s
, ']'))
436 isl_pw_aff_free(pwaff
);
440 static __isl_give isl_pw_aff
*accept_affine_factor(__isl_keep isl_stream
*s
,
441 __isl_take isl_space
*dim
, struct vars
*v
)
443 struct isl_token
*tok
= NULL
;
444 isl_pw_aff
*res
= NULL
;
448 isl_stream_error(s
, NULL
, "unexpected EOF");
452 if (tok
->type
== ISL_TOKEN_AFF
) {
453 res
= isl_pw_aff_copy(tok
->u
.pwaff
);
455 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
457 int pos
= vars_pos(v
, tok
->u
.s
, -1);
463 vars_drop(v
, v
->n
- n
);
464 isl_stream_error(s
, tok
, "unknown identifier");
468 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim
)));
471 isl_int_set_si(aff
->v
->el
[2 + pos
], 1);
472 res
= isl_pw_aff_from_aff(aff
);
474 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
475 if (isl_stream_eat_if_available(s
, '*')) {
476 res
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
477 res
= isl_pw_aff_scale(res
, tok
->u
.v
);
481 ls
= isl_local_space_from_space(isl_space_copy(dim
));
482 aff
= isl_aff_zero_on_domain(ls
);
483 aff
= isl_aff_add_constant(aff
, tok
->u
.v
);
484 res
= isl_pw_aff_from_aff(aff
);
487 } else if (tok
->type
== '(') {
490 res
= accept_affine(s
, isl_space_copy(dim
), v
);
493 if (isl_stream_eat(s
, ')'))
495 } else if (is_start_of_div(tok
)) {
496 isl_stream_push_token(s
, tok
);
498 res
= accept_div(s
, isl_space_copy(dim
), v
);
499 } else if (tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
) {
500 isl_stream_push_token(s
, tok
);
502 res
= accept_minmax(s
, isl_space_copy(dim
), v
);
504 isl_stream_error(s
, tok
, "expecting factor");
507 if (isl_stream_eat_if_available(s
, '%') ||
508 isl_stream_eat_if_available(s
, ISL_TOKEN_MOD
)) {
510 return affine_mod(s
, v
, res
);
512 if (isl_stream_eat_if_available(s
, '*')) {
515 isl_int_set_si(f
, 1);
516 if (accept_cst_factor(s
, &f
) < 0) {
520 res
= isl_pw_aff_scale(res
, f
);
523 if (isl_stream_eat_if_available(s
, '/')) {
526 isl_int_set_si(f
, 1);
527 if (accept_cst_factor(s
, &f
) < 0) {
531 res
= isl_pw_aff_scale_down(res
, f
);
540 isl_pw_aff_free(res
);
545 static __isl_give isl_pw_aff
*add_cst(__isl_take isl_pw_aff
*pwaff
, isl_int v
)
550 space
= isl_pw_aff_get_domain_space(pwaff
);
551 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
552 aff
= isl_aff_add_constant(aff
, v
);
554 return isl_pw_aff_add(pwaff
, isl_pw_aff_from_aff(aff
));
557 /* Return a piecewise affine expression defined on the specified domain
558 * that represents NaN.
560 static __isl_give isl_pw_aff
*nan_on_domain(__isl_keep isl_space
*space
)
564 ls
= isl_local_space_from_space(isl_space_copy(space
));
565 return isl_pw_aff_nan_on_domain(ls
);
568 static __isl_give isl_pw_aff
*accept_affine(__isl_keep isl_stream
*s
,
569 __isl_take isl_space
*space
, struct vars
*v
)
571 struct isl_token
*tok
= NULL
;
576 ls
= isl_local_space_from_space(isl_space_copy(space
));
577 res
= isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
584 isl_stream_error(s
, NULL
, "unexpected EOF");
587 if (tok
->type
== '-') {
592 if (tok
->type
== '(' || is_start_of_div(tok
) ||
593 tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
||
594 tok
->type
== ISL_TOKEN_IDENT
||
595 tok
->type
== ISL_TOKEN_AFF
) {
597 isl_stream_push_token(s
, tok
);
599 term
= accept_affine_factor(s
,
600 isl_space_copy(space
), v
);
602 res
= isl_pw_aff_sub(res
, term
);
604 res
= isl_pw_aff_add(res
, term
);
608 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
610 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
611 if (isl_stream_eat_if_available(s
, '*') ||
612 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
614 term
= accept_affine_factor(s
,
615 isl_space_copy(space
), v
);
616 term
= isl_pw_aff_scale(term
, tok
->u
.v
);
617 res
= isl_pw_aff_add(res
, term
);
621 res
= add_cst(res
, tok
->u
.v
);
624 } else if (tok
->type
== ISL_TOKEN_NAN
) {
625 res
= isl_pw_aff_add(res
, nan_on_domain(space
));
627 isl_stream_error(s
, tok
, "unexpected isl_token");
628 isl_stream_push_token(s
, tok
);
629 isl_pw_aff_free(res
);
630 isl_space_free(space
);
636 if (tok
&& tok
->type
== '-') {
639 } else if (tok
&& tok
->type
== '+') {
642 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
643 isl_int_is_neg(tok
->u
.v
)) {
644 isl_stream_push_token(s
, tok
);
647 isl_stream_push_token(s
, tok
);
652 isl_space_free(space
);
655 isl_space_free(space
);
657 isl_pw_aff_free(res
);
661 /* Is "type" the type of a comparison operator between lists
662 * of affine expressions?
664 static int is_list_comparator_type(int type
)
667 case ISL_TOKEN_LEX_LT
:
668 case ISL_TOKEN_LEX_GT
:
669 case ISL_TOKEN_LEX_LE
:
670 case ISL_TOKEN_LEX_GE
:
677 static int is_comparator(struct isl_token
*tok
)
681 if (is_list_comparator_type(tok
->type
))
697 static __isl_give isl_map
*read_formula(__isl_keep isl_stream
*s
,
698 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
699 static __isl_give isl_pw_aff
*accept_extended_affine(__isl_keep isl_stream
*s
,
700 __isl_take isl_space
*dim
, struct vars
*v
, int rational
);
702 /* Accept a ternary operator, given the first argument.
704 static __isl_give isl_pw_aff
*accept_ternary(__isl_keep isl_stream
*s
,
705 __isl_take isl_map
*cond
, struct vars
*v
, int rational
)
708 isl_pw_aff
*pwaff1
= NULL
, *pwaff2
= NULL
, *pa_cond
;
713 if (isl_stream_eat(s
, '?'))
716 dim
= isl_space_wrap(isl_map_get_space(cond
));
717 pwaff1
= accept_extended_affine(s
, dim
, v
, rational
);
721 if (isl_stream_eat(s
, ':'))
724 dim
= isl_pw_aff_get_domain_space(pwaff1
);
725 pwaff2
= accept_extended_affine(s
, dim
, v
, rational
);
729 pa_cond
= isl_set_indicator_function(isl_map_wrap(cond
));
730 return isl_pw_aff_cond(pa_cond
, pwaff1
, pwaff2
);
733 isl_pw_aff_free(pwaff1
);
734 isl_pw_aff_free(pwaff2
);
738 /* Set *line and *col to those of the next token, if any.
740 static void set_current_line_col(__isl_keep isl_stream
*s
, int *line
, int *col
)
742 struct isl_token
*tok
;
744 tok
= isl_stream_next_token(s
);
750 isl_stream_push_token(s
, tok
);
753 /* Push a token encapsulating "pa" onto "s", with the given
756 static int push_aff(__isl_keep isl_stream
*s
, int line
, int col
,
757 __isl_take isl_pw_aff
*pa
)
759 struct isl_token
*tok
;
761 tok
= isl_token_new(s
->ctx
, line
, col
, 0);
764 tok
->type
= ISL_TOKEN_AFF
;
766 isl_stream_push_token(s
, tok
);
774 /* Accept an affine expression that may involve ternary operators.
775 * We first read an affine expression.
776 * If it is not followed by a comparison operator, we simply return it.
777 * Otherwise, we assume the affine expression is part of the first
778 * argument of a ternary operator and try to parse that.
780 static __isl_give isl_pw_aff
*accept_extended_affine(__isl_keep isl_stream
*s
,
781 __isl_take isl_space
*dim
, struct vars
*v
, int rational
)
786 struct isl_token
*tok
;
787 int line
= -1, col
= -1;
790 set_current_line_col(s
, &line
, &col
);
792 pwaff
= accept_affine(s
, dim
, v
);
794 pwaff
= isl_pw_aff_set_rational(pwaff
);
798 tok
= isl_stream_next_token(s
);
800 return isl_pw_aff_free(pwaff
);
802 is_comp
= is_comparator(tok
);
803 isl_stream_push_token(s
, tok
);
807 space
= isl_pw_aff_get_domain_space(pwaff
);
808 cond
= isl_map_universe(isl_space_unwrap(space
));
810 if (push_aff(s
, line
, col
, pwaff
) < 0)
811 cond
= isl_map_free(cond
);
815 cond
= read_formula(s
, v
, cond
, rational
);
817 return accept_ternary(s
, cond
, v
, rational
);
820 static __isl_give isl_map
*read_var_def(__isl_keep isl_stream
*s
,
821 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
828 if (type
== isl_dim_param
)
829 pos
= isl_map_dim(map
, isl_dim_param
);
831 pos
= isl_map_dim(map
, isl_dim_in
);
832 if (type
== isl_dim_out
)
833 pos
+= isl_map_dim(map
, isl_dim_out
);
838 def
= accept_extended_affine(s
, isl_space_wrap(isl_map_get_space(map
)),
840 def_map
= isl_map_from_pw_aff(def
);
841 def_map
= isl_map_equate(def_map
, type
, pos
, isl_dim_out
, 0);
842 def_map
= isl_set_unwrap(isl_map_domain(def_map
));
844 map
= isl_map_intersect(map
, def_map
);
849 static __isl_give isl_pw_aff_list
*accept_affine_list(__isl_keep isl_stream
*s
,
850 __isl_take isl_space
*dim
, struct vars
*v
)
853 isl_pw_aff_list
*list
;
854 struct isl_token
*tok
= NULL
;
856 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
857 list
= isl_pw_aff_list_from_pw_aff(pwaff
);
862 tok
= isl_stream_next_token(s
);
864 isl_stream_error(s
, NULL
, "unexpected EOF");
867 if (tok
->type
!= ',') {
868 isl_stream_push_token(s
, tok
);
873 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
874 list
= isl_pw_aff_list_concat(list
,
875 isl_pw_aff_list_from_pw_aff(pwaff
));
884 isl_pw_aff_list_free(list
);
888 static __isl_give isl_map
*read_defined_var_list(__isl_keep isl_stream
*s
,
889 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
891 struct isl_token
*tok
;
893 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
897 if (tok
->type
!= ISL_TOKEN_IDENT
)
900 p
= vars_pos(v
, tok
->u
.s
, -1);
904 isl_stream_error(s
, tok
, "expecting unique identifier");
908 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
911 tok
= isl_stream_next_token(s
);
912 if (tok
&& tok
->type
== '=') {
914 map
= read_var_def(s
, map
, isl_dim_out
, v
, rational
);
915 tok
= isl_stream_next_token(s
);
918 if (!tok
|| tok
->type
!= ',')
924 isl_stream_push_token(s
, tok
);
933 static int next_is_tuple(__isl_keep isl_stream
*s
)
935 struct isl_token
*tok
;
938 tok
= isl_stream_next_token(s
);
941 if (tok
->type
== '[') {
942 isl_stream_push_token(s
, tok
);
945 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
946 isl_stream_push_token(s
, tok
);
950 is_tuple
= isl_stream_next_token_is(s
, '[');
952 isl_stream_push_token(s
, tok
);
957 /* Is "pa" an expression in term of earlier dimensions?
958 * The alternative is that the dimension is defined to be equal to itself,
959 * meaning that it has a universe domain and an expression that depends
960 * on itself. "i" is the position of the expression in a sequence
961 * of "n" expressions. The final dimensions of "pa" correspond to
962 * these "n" expressions.
964 static int pw_aff_is_expr(__isl_keep isl_pw_aff
*pa
, int i
, int n
)
972 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
976 if (isl_int_is_zero(aff
->v
->el
[aff
->v
->size
- n
+ i
]))
981 /* Does the tuple contain any dimensions that are defined
982 * in terms of earlier dimensions?
984 static int tuple_has_expr(__isl_keep isl_multi_pw_aff
*tuple
)
992 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
993 for (i
= 0; i
< n
; ++i
) {
994 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
995 has_expr
= pw_aff_is_expr(pa
, i
, n
);
997 if (has_expr
< 0 || has_expr
)
1004 /* Set the name of dimension "pos" in "space" to "name".
1005 * During printing, we add primes if the same name appears more than once
1006 * to distinguish the occurrences. Here, we remove those primes from "name"
1007 * before setting the name of the dimension.
1009 static __isl_give isl_space
*space_set_dim_name(__isl_take isl_space
*space
,
1010 int pos
, char *name
)
1017 prime
= strchr(name
, '\'');
1020 space
= isl_space_set_dim_name(space
, isl_dim_out
, pos
, name
);
1027 /* Accept a piecewise affine expression.
1029 * At the outer level, the piecewise affine expression may be of the form
1031 * aff1 : condition1; aff2 : conditions2; ...
1037 * each of the affine expressions may in turn include ternary operators.
1039 * There may be parentheses around some subexpression of "aff1"
1040 * around "aff1" itself, around "aff1 : condition1" and/or
1041 * around the entire piecewise affine expression.
1042 * We therefore remove the opening parenthesis (if any) from the stream
1043 * in case the closing parenthesis follows the colon, but if the closing
1044 * parenthesis is the first thing in the stream after the parsed affine
1045 * expression, we push the parsed expression onto the stream and parse
1046 * again in case the parentheses enclose some subexpression of "aff1".
1048 static __isl_give isl_pw_aff
*accept_piecewise_affine(__isl_keep isl_stream
*s
,
1049 __isl_take isl_space
*space
, struct vars
*v
, int rational
)
1052 isl_space
*res_space
;
1054 res_space
= isl_space_from_domain(isl_space_copy(space
));
1055 res_space
= isl_space_add_dims(res_space
, isl_dim_out
, 1);
1056 res
= isl_pw_aff_empty(res_space
);
1060 int line
= -1, col
= -1;
1062 set_current_line_col(s
, &line
, &col
);
1063 seen_paren
= isl_stream_eat_if_available(s
, '(');
1065 pa
= accept_piecewise_affine(s
, isl_space_copy(space
),
1068 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1070 if (seen_paren
&& isl_stream_eat_if_available(s
, ')')) {
1072 if (push_aff(s
, line
, col
, pa
) < 0)
1074 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1077 if (isl_stream_eat_if_available(s
, ':')) {
1078 isl_space
*dom_space
;
1081 dom_space
= isl_pw_aff_get_domain_space(pa
);
1082 dom
= isl_set_universe(dom_space
);
1083 dom
= read_formula(s
, v
, dom
, rational
);
1084 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
1087 res
= isl_pw_aff_union_add(res
, pa
);
1089 if (seen_paren
&& isl_stream_eat(s
, ')'))
1091 } while (isl_stream_eat_if_available(s
, ';'));
1093 isl_space_free(space
);
1097 isl_space_free(space
);
1098 return isl_pw_aff_free(res
);
1101 /* Read an affine expression from "s" for use in read_tuple.
1103 * accept_extended_affine requires a wrapped space as input.
1104 * read_tuple on the other hand expects each isl_pw_aff
1105 * to have an anonymous space. We therefore adjust the space
1106 * of the isl_pw_aff before returning it.
1108 static __isl_give isl_pw_aff
*read_tuple_var_def(__isl_keep isl_stream
*s
,
1109 struct vars
*v
, int rational
)
1114 space
= isl_space_wrap(isl_space_alloc(s
->ctx
, 0, v
->n
, 0));
1116 def
= accept_piecewise_affine(s
, space
, v
, rational
);
1118 space
= isl_space_set_alloc(s
->ctx
, 0, v
->n
);
1119 def
= isl_pw_aff_reset_domain_space(def
, space
);
1124 /* Read a list of tuple elements by calling "read_el" on each of them and
1125 * return a space with the same number of set dimensions derived from
1126 * the parameter space "space" and possibly updated by "read_el".
1127 * The elements in the list are separated by either "," or "][".
1128 * If "comma" is set then only "," is allowed.
1130 static __isl_give isl_space
*read_tuple_list(__isl_keep isl_stream
*s
,
1131 struct vars
*v
, __isl_take isl_space
*space
, int rational
, int comma
,
1132 __isl_give isl_space
*(*read_el
)(__isl_keep isl_stream
*s
,
1133 struct vars
*v
, __isl_take isl_space
*space
, int rational
,
1140 space
= isl_space_set_from_params(space
);
1142 if (isl_stream_next_token_is(s
, ']'))
1146 struct isl_token
*tok
;
1148 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
1150 space
= read_el(s
, v
, space
, rational
, user
);
1154 tok
= isl_stream_next_token(s
);
1155 if (!comma
&& tok
&& tok
->type
== ']' &&
1156 isl_stream_next_token_is(s
, '[')) {
1157 isl_token_free(tok
);
1158 tok
= isl_stream_next_token(s
);
1159 } else if (!tok
|| tok
->type
!= ',') {
1161 isl_stream_push_token(s
, tok
);
1165 isl_token_free(tok
);
1171 /* Read a tuple space from "s" derived from the parameter space "space".
1172 * Call "read_el" on each element in the tuples.
1174 static __isl_give isl_space
*read_tuple_space(__isl_keep isl_stream
*s
,
1175 struct vars
*v
, __isl_take isl_space
*space
, int rational
, int comma
,
1176 __isl_give isl_space
*(*read_el
)(__isl_keep isl_stream
*s
,
1177 struct vars
*v
, __isl_take isl_space
*space
, int rational
,
1181 struct isl_token
*tok
;
1183 isl_space
*res
= NULL
;
1185 tok
= isl_stream_next_token(s
);
1188 if (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
) {
1189 name
= strdup(tok
->u
.s
);
1190 isl_token_free(tok
);
1194 isl_stream_push_token(s
, tok
);
1195 if (isl_stream_eat(s
, '['))
1197 if (next_is_tuple(s
)) {
1199 res
= read_tuple_space(s
, v
, isl_space_copy(space
),
1200 rational
, comma
, read_el
, user
);
1201 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
1203 out
= read_tuple_space(s
, v
, isl_space_copy(space
),
1204 rational
, comma
, read_el
, user
);
1205 res
= isl_space_product(res
, out
);
1207 res
= read_tuple_list(s
, v
, isl_space_copy(space
),
1208 rational
, comma
, read_el
, user
);
1209 if (isl_stream_eat(s
, ']'))
1213 res
= isl_space_set_tuple_name(res
, isl_dim_set
, name
);
1217 isl_space_free(space
);
1221 isl_space_free(res
);
1222 isl_space_free(space
);
1226 /* Construct an isl_pw_aff defined on a space with v->n variables
1227 * that is equal to the last of those variables.
1229 static __isl_give isl_pw_aff
*identity_tuple_el(struct vars
*v
)
1234 space
= isl_space_set_alloc(v
->ctx
, 0, v
->n
);
1235 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1236 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, v
->n
- 1, 1);
1237 return isl_pw_aff_from_aff(aff
);
1240 /* This function is called for each element in a tuple inside read_tuple.
1241 * Add a new variable to "v" and construct a corresponding isl_pw_aff defined
1242 * over a space containing all variables in "v" defined so far.
1243 * The isl_pw_aff expresses the new variable in terms of earlier variables
1244 * if a definition is provided. Otherwise, it is represented as being
1246 * Add the isl_pw_aff to *list.
1247 * If the new variable was named, then adjust "space" accordingly and
1248 * return the updated space.
1250 static __isl_give isl_space
*read_tuple_pw_aff_el(__isl_keep isl_stream
*s
,
1251 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
1253 isl_pw_aff_list
**list
= (isl_pw_aff_list
**) user
;
1255 struct isl_token
*tok
;
1258 tok
= next_token(s
);
1260 isl_stream_error(s
, NULL
, "unexpected EOF");
1261 return isl_space_free(space
);
1264 if (tok
->type
== ISL_TOKEN_IDENT
) {
1266 int p
= vars_pos(v
, tok
->u
.s
, -1);
1272 if (tok
->type
== '*') {
1273 if (vars_add_anon(v
) < 0)
1275 isl_token_free(tok
);
1276 pa
= identity_tuple_el(v
);
1277 } else if (new_name
) {
1278 int pos
= isl_space_dim(space
, isl_dim_out
) - 1;
1279 space
= space_set_dim_name(space
, pos
, v
->v
->name
);
1280 isl_token_free(tok
);
1281 if (isl_stream_eat_if_available(s
, '='))
1282 pa
= read_tuple_var_def(s
, v
, rational
);
1284 pa
= identity_tuple_el(v
);
1286 isl_stream_push_token(s
, tok
);
1288 if (vars_add_anon(v
) < 0)
1290 pa
= read_tuple_var_def(s
, v
, rational
);
1293 *list
= isl_pw_aff_list_add(*list
, pa
);
1295 return isl_space_free(space
);
1299 isl_token_free(tok
);
1300 return isl_space_free(space
);
1303 /* Read a tuple and represent it as an isl_multi_pw_aff.
1304 * The range space of the isl_multi_pw_aff is the space of the tuple.
1305 * The domain space is an anonymous space
1306 * with a dimension for each variable in the set of variables in "v",
1307 * including the variables in the range.
1308 * If a given dimension is not defined in terms of earlier dimensions in
1309 * the input, then the corresponding isl_pw_aff is set equal to one time
1310 * the variable corresponding to the dimension being defined.
1312 * The elements in the tuple are collected in a list by read_tuple_pw_aff_el.
1313 * Each element in this list is defined over a space representing
1314 * the variables defined so far. We need to adjust the earlier
1315 * elements to have as many variables in the domain as the final
1316 * element in the list.
1318 static __isl_give isl_multi_pw_aff
*read_tuple(__isl_keep isl_stream
*s
,
1319 struct vars
*v
, int rational
, int comma
)
1323 isl_pw_aff_list
*list
;
1325 space
= isl_space_params_alloc(v
->ctx
, 0);
1326 list
= isl_pw_aff_list_alloc(s
->ctx
, 0);
1327 space
= read_tuple_space(s
, v
, space
, rational
, comma
,
1328 &read_tuple_pw_aff_el
, &list
);
1329 n
= isl_space_dim(space
, isl_dim_set
);
1330 for (i
= 0; i
+ 1 < n
; ++i
) {
1333 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
1334 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, n
- (i
+ 1));
1335 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
1338 space
= isl_space_from_range(space
);
1339 space
= isl_space_add_dims(space
, isl_dim_in
, v
->n
);
1340 return isl_multi_pw_aff_from_pw_aff_list(space
, list
);
1343 /* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
1344 * We first create the appropriate space in "map" based on the range
1345 * space of this isl_multi_pw_aff. Then, we add equalities based
1346 * on the affine expressions. These live in an anonymous space,
1347 * however, so we first need to reset the space to that of "map".
1349 static __isl_give isl_map
*map_from_tuple(__isl_take isl_multi_pw_aff
*tuple
,
1350 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1355 isl_space
*space
= NULL
;
1359 ctx
= isl_multi_pw_aff_get_ctx(tuple
);
1360 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1361 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
1365 if (type
== isl_dim_param
) {
1366 if (isl_space_has_tuple_name(space
, isl_dim_set
) ||
1367 isl_space_is_wrapping(space
)) {
1368 isl_die(ctx
, isl_error_invalid
,
1369 "parameter tuples cannot be named or nested",
1372 map
= isl_map_add_dims(map
, type
, n
);
1373 for (i
= 0; i
< n
; ++i
) {
1375 if (!isl_space_has_dim_name(space
, isl_dim_set
, i
))
1376 isl_die(ctx
, isl_error_invalid
,
1377 "parameters must be named",
1379 id
= isl_space_get_dim_id(space
, isl_dim_set
, i
);
1380 map
= isl_map_set_dim_id(map
, isl_dim_param
, i
, id
);
1382 } else if (type
== isl_dim_in
) {
1385 set
= isl_set_universe(isl_space_copy(space
));
1387 set
= isl_set_set_rational(set
);
1388 set
= isl_set_intersect_params(set
, isl_map_params(map
));
1389 map
= isl_map_from_domain(set
);
1393 set
= isl_set_universe(isl_space_copy(space
));
1395 set
= isl_set_set_rational(set
);
1396 map
= isl_map_from_domain_and_range(isl_map_domain(map
), set
);
1399 for (i
= 0; i
< n
; ++i
) {
1406 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1407 space
= isl_pw_aff_get_domain_space(pa
);
1408 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1409 aff
= isl_aff_add_coefficient_si(aff
,
1410 isl_dim_in
, v
->n
- n
+ i
, -1);
1411 pa
= isl_pw_aff_add(pa
, isl_pw_aff_from_aff(aff
));
1413 pa
= isl_pw_aff_set_rational(pa
);
1414 set
= isl_pw_aff_zero_set(pa
);
1415 map_i
= isl_map_from_range(set
);
1416 map_i
= isl_map_reset_space(map_i
, isl_map_get_space(map
));
1417 map
= isl_map_intersect(map
, map_i
);
1420 isl_space_free(space
);
1421 isl_multi_pw_aff_free(tuple
);
1424 isl_space_free(space
);
1425 isl_multi_pw_aff_free(tuple
);
1430 /* Read a tuple from "s" and add it to "map".
1431 * The tuple is initially represented as an isl_multi_pw_aff and
1432 * then added to "map".
1434 static __isl_give isl_map
*read_map_tuple(__isl_keep isl_stream
*s
,
1435 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1436 int rational
, int comma
)
1438 isl_multi_pw_aff
*tuple
;
1440 tuple
= read_tuple(s
, v
, rational
, comma
);
1442 return isl_map_free(map
);
1444 return map_from_tuple(tuple
, map
, type
, v
, rational
);
1447 /* Given two equal-length lists of piecewise affine expression with the space
1448 * of "set" as domain, construct a set in the same space that expresses
1449 * that "left" and "right" satisfy the comparison "type".
1451 * A space is constructed of the same dimension as the number of elements
1452 * in the two lists. The comparison is then expressed in a map from
1453 * this space to itself and wrapped into a set. Finally the two lists
1454 * of piecewise affine expressions are plugged into this set.
1456 * Let S be the space of "set" and T the constructed space.
1457 * The lists are first changed into two isl_multi_pw_affs in S -> T and
1458 * then combined into an isl_multi_pw_aff in S -> [T -> T],
1459 * while the comparison is first expressed in T -> T, then [T -> T]
1462 static __isl_give isl_set
*list_cmp(__isl_keep isl_set
*set
, int type
,
1463 __isl_take isl_pw_aff_list
*left
, __isl_take isl_pw_aff_list
*right
)
1467 isl_multi_pw_aff
*mpa1
, *mpa2
;
1469 if (!set
|| !left
|| !right
)
1472 space
= isl_set_get_space(set
);
1473 n
= isl_pw_aff_list_n_pw_aff(left
);
1474 space
= isl_space_from_domain(space
);
1475 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
1476 mpa1
= isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space
), left
);
1477 mpa2
= isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space
), right
);
1478 mpa1
= isl_multi_pw_aff_range_product(mpa1
, mpa2
);
1480 space
= isl_space_range(space
);
1482 case ISL_TOKEN_LEX_LT
:
1483 set
= isl_map_wrap(isl_map_lex_lt(space
));
1485 case ISL_TOKEN_LEX_GT
:
1486 set
= isl_map_wrap(isl_map_lex_gt(space
));
1488 case ISL_TOKEN_LEX_LE
:
1489 set
= isl_map_wrap(isl_map_lex_le(space
));
1491 case ISL_TOKEN_LEX_GE
:
1492 set
= isl_map_wrap(isl_map_lex_ge(space
));
1495 isl_multi_pw_aff_free(mpa1
);
1496 isl_space_free(space
);
1497 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
1498 "unhandled list comparison type", return NULL
);
1500 set
= isl_set_preimage_multi_pw_aff(set
, mpa1
);
1503 isl_pw_aff_list_free(left
);
1504 isl_pw_aff_list_free(right
);
1508 /* Construct constraints of the form
1512 * where a is an element in "left", op is an operator of type "type" and
1513 * b is an element in "right", add the constraints to "set" and return
1515 * "rational" is set if the constraints should be treated as
1516 * a rational constraints.
1518 * If "type" is the type of a comparison operator between lists
1519 * of affine expressions, then a single (compound) constraint
1520 * is constructed by list_cmp instead.
1522 static __isl_give isl_set
*construct_constraints(
1523 __isl_take isl_set
*set
, int type
,
1524 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
,
1529 left
= isl_pw_aff_list_copy(left
);
1530 right
= isl_pw_aff_list_copy(right
);
1532 left
= isl_pw_aff_list_set_rational(left
);
1533 right
= isl_pw_aff_list_set_rational(right
);
1535 if (is_list_comparator_type(type
))
1536 cond
= list_cmp(set
, type
, left
, right
);
1537 else if (type
== ISL_TOKEN_LE
)
1538 cond
= isl_pw_aff_list_le_set(left
, right
);
1539 else if (type
== ISL_TOKEN_GE
)
1540 cond
= isl_pw_aff_list_ge_set(left
, right
);
1541 else if (type
== ISL_TOKEN_LT
)
1542 cond
= isl_pw_aff_list_lt_set(left
, right
);
1543 else if (type
== ISL_TOKEN_GT
)
1544 cond
= isl_pw_aff_list_gt_set(left
, right
);
1545 else if (type
== ISL_TOKEN_NE
)
1546 cond
= isl_pw_aff_list_ne_set(left
, right
);
1548 cond
= isl_pw_aff_list_eq_set(left
, right
);
1550 return isl_set_intersect(set
, cond
);
1553 /* Read a constraint from "s", add it to "map" and return the result.
1554 * "v" contains a description of the identifiers parsed so far.
1555 * "rational" is set if the constraint should be treated as
1556 * a rational constraint.
1557 * The constraint read from "s" may be applied to multiple pairs
1558 * of affine expressions and may be chained.
1559 * In particular, a list of affine expressions is read, followed
1560 * by a comparison operator and another list of affine expressions.
1561 * The comparison operator is then applied to each pair of elements
1562 * in the two lists and the results are added to "map".
1563 * However, if the operator expects two lists of affine expressions,
1564 * then it is applied directly to those lists and the two lists
1565 * are required to have the same length.
1566 * If the next token is another comparison operator, then another
1567 * list of affine expressions is read and the process repeats.
1569 * The processing is performed on a wrapped copy of "map" because
1570 * an affine expression cannot have a binary relation as domain.
1572 static __isl_give isl_map
*add_constraint(__isl_keep isl_stream
*s
,
1573 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1575 struct isl_token
*tok
;
1577 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
1581 set
= isl_map_wrap(map
);
1582 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1585 tok
= isl_stream_next_token(s
);
1586 if (!is_comparator(tok
)) {
1587 isl_stream_error(s
, tok
, "missing operator");
1589 isl_stream_push_token(s
, tok
);
1593 isl_token_free(tok
);
1595 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1598 n1
= isl_pw_aff_list_n_pw_aff(list1
);
1599 n2
= isl_pw_aff_list_n_pw_aff(list2
);
1600 if (is_list_comparator_type(type
) && n1
!= n2
) {
1601 isl_stream_error(s
, NULL
,
1602 "list arguments not of same size");
1606 set
= construct_constraints(set
, type
, list1
, list2
, rational
);
1607 isl_pw_aff_list_free(list1
);
1610 tok
= isl_stream_next_token(s
);
1611 if (!is_comparator(tok
)) {
1613 isl_stream_push_token(s
, tok
);
1617 isl_token_free(tok
);
1619 isl_pw_aff_list_free(list1
);
1621 return isl_set_unwrap(set
);
1623 isl_pw_aff_list_free(list1
);
1624 isl_pw_aff_list_free(list2
);
1629 static __isl_give isl_map
*read_exists(__isl_keep isl_stream
*s
,
1630 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1633 int seen_paren
= isl_stream_eat_if_available(s
, '(');
1635 map
= isl_map_from_domain(isl_map_wrap(map
));
1636 map
= read_defined_var_list(s
, v
, map
, rational
);
1638 if (isl_stream_eat(s
, ':'))
1641 map
= read_formula(s
, v
, map
, rational
);
1642 map
= isl_set_unwrap(isl_map_domain(map
));
1644 vars_drop(v
, v
->n
- n
);
1645 if (seen_paren
&& isl_stream_eat(s
, ')'))
1654 /* Parse an expression between parentheses and push the result
1655 * back on the stream.
1657 * The parsed expression may be either an affine expression
1658 * or a condition. The first type is pushed onto the stream
1659 * as an isl_pw_aff, while the second is pushed as an isl_map.
1661 * If the initial token indicates the start of a condition,
1662 * we parse it as such.
1663 * Otherwise, we first parse an affine expression and push
1664 * that onto the stream. If the affine expression covers the
1665 * entire expression between parentheses, we return.
1666 * Otherwise, we assume that the affine expression is the
1667 * start of a condition and continue parsing.
1669 static int resolve_paren_expr(__isl_keep isl_stream
*s
,
1670 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1672 struct isl_token
*tok
, *tok2
;
1676 tok
= isl_stream_next_token(s
);
1677 if (!tok
|| tok
->type
!= '(')
1680 if (isl_stream_next_token_is(s
, '('))
1681 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1684 if (isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1685 isl_stream_next_token_is(s
, ISL_TOKEN_NOT
) ||
1686 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1687 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
) ||
1688 isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1689 map
= read_formula(s
, v
, map
, rational
);
1690 if (isl_stream_eat(s
, ')'))
1692 tok
->type
= ISL_TOKEN_MAP
;
1694 isl_stream_push_token(s
, tok
);
1698 tok2
= isl_stream_next_token(s
);
1703 isl_stream_push_token(s
, tok2
);
1705 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
1709 tok2
= isl_token_new(s
->ctx
, line
, col
, 0);
1712 tok2
->type
= ISL_TOKEN_AFF
;
1713 tok2
->u
.pwaff
= pwaff
;
1715 if (isl_stream_eat_if_available(s
, ')')) {
1716 isl_stream_push_token(s
, tok2
);
1717 isl_token_free(tok
);
1722 isl_stream_push_token(s
, tok2
);
1724 map
= read_formula(s
, v
, map
, rational
);
1725 if (isl_stream_eat(s
, ')'))
1728 tok
->type
= ISL_TOKEN_MAP
;
1730 isl_stream_push_token(s
, tok
);
1734 isl_pw_aff_free(pwaff
);
1736 isl_token_free(tok
);
1741 static __isl_give isl_map
*read_conjunct(__isl_keep isl_stream
*s
,
1742 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1744 if (isl_stream_next_token_is(s
, '('))
1745 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1748 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1749 struct isl_token
*tok
;
1750 tok
= isl_stream_next_token(s
);
1754 map
= isl_map_copy(tok
->u
.map
);
1755 isl_token_free(tok
);
1759 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
1760 return read_exists(s
, v
, map
, rational
);
1762 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
1765 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
1766 isl_space
*dim
= isl_map_get_space(map
);
1768 return isl_map_empty(dim
);
1771 return add_constraint(s
, v
, map
, rational
);
1777 static __isl_give isl_map
*read_conjuncts(__isl_keep isl_stream
*s
,
1778 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1783 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1784 res
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1786 res
= isl_map_subtract(isl_map_copy(map
), res
);
1788 while (res
&& isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
1791 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1792 res_i
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1794 res
= isl_map_subtract(res
, res_i
);
1796 res
= isl_map_intersect(res
, res_i
);
1803 static struct isl_map
*read_disjuncts(__isl_keep isl_stream
*s
,
1804 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1808 if (isl_stream_next_token_is(s
, '}'))
1811 res
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1812 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
1815 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1816 res
= isl_map_union(res
, res_i
);
1823 /* Read a first order formula from "s", add the corresponding
1824 * constraints to "map" and return the result.
1826 * In particular, read a formula of the form
1834 * where a and b are disjunctions.
1836 * In the first case, map is replaced by
1838 * map \cap { [..] : a }
1840 * In the second case, it is replaced by
1842 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
1844 static __isl_give isl_map
*read_formula(__isl_keep isl_stream
*s
,
1845 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1849 res
= read_disjuncts(s
, v
, isl_map_copy(map
), rational
);
1851 if (isl_stream_eat_if_available(s
, ISL_TOKEN_IMPLIES
)) {
1854 res
= isl_map_subtract(isl_map_copy(map
), res
);
1855 res2
= read_disjuncts(s
, v
, map
, rational
);
1856 res
= isl_map_union(res
, res2
);
1863 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
1865 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
))
1866 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1867 isl_basic_map_dim(bmap
, isl_dim_in
) + pos
;
1868 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
1870 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
))
1871 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) + pos
;
1872 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
1874 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
))
1875 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1876 isl_basic_map_dim(bmap
, isl_dim_in
) +
1877 isl_basic_map_dim(bmap
, isl_dim_out
) + pos
;
1878 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
1880 if (pos
< isl_basic_map_dim(bmap
, isl_dim_param
))
1886 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
1887 __isl_keep isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
1890 struct isl_token
*tok
;
1898 tok
= isl_stream_next_token(s
);
1899 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1900 isl_stream_error(s
, tok
, "expecting coefficient");
1902 isl_stream_push_token(s
, tok
);
1905 if (!tok
->on_new_line
) {
1906 isl_stream_error(s
, tok
, "coefficient should appear on new line");
1907 isl_stream_push_token(s
, tok
);
1911 type
= isl_int_get_si(tok
->u
.v
);
1912 isl_token_free(tok
);
1914 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
1916 k
= isl_basic_map_alloc_equality(bmap
);
1919 k
= isl_basic_map_alloc_inequality(bmap
);
1925 for (j
= 0; j
< 1 + isl_basic_map_total_dim(bmap
); ++j
) {
1927 tok
= isl_stream_next_token(s
);
1928 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1929 isl_stream_error(s
, tok
, "expecting coefficient");
1931 isl_stream_push_token(s
, tok
);
1934 if (tok
->on_new_line
) {
1935 isl_stream_error(s
, tok
,
1936 "coefficient should not appear on new line");
1937 isl_stream_push_token(s
, tok
);
1940 pos
= polylib_pos_to_isl_pos(bmap
, j
);
1941 isl_int_set(c
[pos
], tok
->u
.v
);
1942 isl_token_free(tok
);
1947 isl_basic_map_free(bmap
);
1951 static __isl_give isl_basic_map
*basic_map_read_polylib(
1952 __isl_keep isl_stream
*s
)
1955 struct isl_token
*tok
;
1956 struct isl_token
*tok2
;
1959 unsigned in
= 0, out
, local
= 0;
1960 struct isl_basic_map
*bmap
= NULL
;
1963 tok
= isl_stream_next_token(s
);
1965 isl_stream_error(s
, NULL
, "unexpected EOF");
1968 tok2
= isl_stream_next_token(s
);
1970 isl_token_free(tok
);
1971 isl_stream_error(s
, NULL
, "unexpected EOF");
1974 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1975 isl_stream_push_token(s
, tok2
);
1976 isl_stream_push_token(s
, tok
);
1977 isl_stream_error(s
, NULL
,
1978 "expecting constraint matrix dimensions");
1981 n_row
= isl_int_get_si(tok
->u
.v
);
1982 n_col
= isl_int_get_si(tok2
->u
.v
);
1983 on_new_line
= tok2
->on_new_line
;
1984 isl_token_free(tok2
);
1985 isl_token_free(tok
);
1986 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
1987 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
1988 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
1989 tok
= isl_stream_next_token_on_same_line(s
);
1991 if (tok
->type
!= ISL_TOKEN_VALUE
) {
1992 isl_stream_error(s
, tok
,
1993 "expecting number of output dimensions");
1994 isl_stream_push_token(s
, tok
);
1997 out
= isl_int_get_si(tok
->u
.v
);
1998 isl_token_free(tok
);
2000 tok
= isl_stream_next_token_on_same_line(s
);
2001 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2002 isl_stream_error(s
, tok
,
2003 "expecting number of input dimensions");
2005 isl_stream_push_token(s
, tok
);
2008 in
= isl_int_get_si(tok
->u
.v
);
2009 isl_token_free(tok
);
2011 tok
= isl_stream_next_token_on_same_line(s
);
2012 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2013 isl_stream_error(s
, tok
,
2014 "expecting number of existentials");
2016 isl_stream_push_token(s
, tok
);
2019 local
= isl_int_get_si(tok
->u
.v
);
2020 isl_token_free(tok
);
2022 tok
= isl_stream_next_token_on_same_line(s
);
2023 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2024 isl_stream_error(s
, tok
,
2025 "expecting number of parameters");
2027 isl_stream_push_token(s
, tok
);
2030 nparam
= isl_int_get_si(tok
->u
.v
);
2031 isl_token_free(tok
);
2032 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
2033 isl_stream_error(s
, NULL
,
2034 "dimensions don't match");
2038 out
= n_col
- 2 - nparam
;
2039 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
2043 for (i
= 0; i
< local
; ++i
) {
2044 int k
= isl_basic_map_alloc_div(bmap
);
2047 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
2050 for (i
= 0; i
< n_row
; ++i
)
2051 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
2053 tok
= isl_stream_next_token_on_same_line(s
);
2055 isl_stream_error(s
, tok
, "unexpected extra token on line");
2056 isl_stream_push_token(s
, tok
);
2060 bmap
= isl_basic_map_simplify(bmap
);
2061 bmap
= isl_basic_map_finalize(bmap
);
2064 isl_basic_map_free(bmap
);
2068 static struct isl_map
*map_read_polylib(__isl_keep isl_stream
*s
)
2070 struct isl_token
*tok
;
2071 struct isl_token
*tok2
;
2073 struct isl_map
*map
;
2075 tok
= isl_stream_next_token(s
);
2077 isl_stream_error(s
, NULL
, "unexpected EOF");
2080 tok2
= isl_stream_next_token_on_same_line(s
);
2081 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
2082 isl_stream_push_token(s
, tok2
);
2083 isl_stream_push_token(s
, tok
);
2084 return isl_map_from_basic_map(basic_map_read_polylib(s
));
2087 isl_stream_error(s
, tok2
, "unexpected token");
2088 isl_stream_push_token(s
, tok2
);
2089 isl_stream_push_token(s
, tok
);
2092 n
= isl_int_get_si(tok
->u
.v
);
2093 isl_token_free(tok
);
2095 isl_assert(s
->ctx
, n
>= 1, return NULL
);
2097 map
= isl_map_from_basic_map(basic_map_read_polylib(s
));
2099 for (i
= 1; map
&& i
< n
; ++i
)
2100 map
= isl_map_union(map
,
2101 isl_map_from_basic_map(basic_map_read_polylib(s
)));
2106 static int optional_power(__isl_keep isl_stream
*s
)
2109 struct isl_token
*tok
;
2111 tok
= isl_stream_next_token(s
);
2114 if (tok
->type
!= '^') {
2115 isl_stream_push_token(s
, tok
);
2118 isl_token_free(tok
);
2119 tok
= isl_stream_next_token(s
);
2120 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2121 isl_stream_error(s
, tok
, "expecting exponent");
2123 isl_stream_push_token(s
, tok
);
2126 pow
= isl_int_get_si(tok
->u
.v
);
2127 isl_token_free(tok
);
2131 static __isl_give isl_pw_qpolynomial
*read_term(__isl_keep isl_stream
*s
,
2132 __isl_keep isl_map
*map
, struct vars
*v
);
2134 static __isl_give isl_pw_qpolynomial
*read_factor(__isl_keep isl_stream
*s
,
2135 __isl_keep isl_map
*map
, struct vars
*v
)
2137 isl_pw_qpolynomial
*pwqp
;
2138 struct isl_token
*tok
;
2140 tok
= next_token(s
);
2142 isl_stream_error(s
, NULL
, "unexpected EOF");
2145 if (tok
->type
== '(') {
2148 isl_token_free(tok
);
2149 pwqp
= read_term(s
, map
, v
);
2152 if (isl_stream_eat(s
, ')'))
2154 pow
= optional_power(s
);
2155 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2156 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
2157 struct isl_token
*tok2
;
2158 isl_qpolynomial
*qp
;
2160 tok2
= isl_stream_next_token(s
);
2161 if (tok2
&& tok2
->type
== '/') {
2162 isl_token_free(tok2
);
2163 tok2
= next_token(s
);
2164 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2165 isl_stream_error(s
, tok2
, "expected denominator");
2166 isl_token_free(tok
);
2167 isl_token_free(tok2
);
2170 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
2171 tok
->u
.v
, tok2
->u
.v
);
2172 isl_token_free(tok2
);
2174 isl_stream_push_token(s
, tok2
);
2175 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
2178 isl_token_free(tok
);
2179 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2180 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
2181 isl_qpolynomial
*qp
;
2182 isl_token_free(tok
);
2183 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
2184 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2185 } else if (tok
->type
== ISL_TOKEN_NAN
) {
2186 isl_qpolynomial
*qp
;
2187 isl_token_free(tok
);
2188 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
2189 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2190 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
2192 int pos
= vars_pos(v
, tok
->u
.s
, -1);
2194 isl_qpolynomial
*qp
;
2196 isl_token_free(tok
);
2200 vars_drop(v
, v
->n
- n
);
2201 isl_stream_error(s
, tok
, "unknown identifier");
2202 isl_token_free(tok
);
2205 isl_token_free(tok
);
2206 pow
= optional_power(s
);
2207 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
2208 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2209 } else if (is_start_of_div(tok
)) {
2213 isl_stream_push_token(s
, tok
);
2214 pwaff
= accept_div(s
, isl_map_get_space(map
), v
);
2215 pow
= optional_power(s
);
2216 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
2217 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2218 } else if (tok
->type
== '-') {
2219 isl_token_free(tok
);
2220 pwqp
= read_factor(s
, map
, v
);
2221 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
2223 isl_stream_error(s
, tok
, "unexpected isl_token");
2224 isl_stream_push_token(s
, tok
);
2228 if (isl_stream_eat_if_available(s
, '*') ||
2229 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
2230 isl_pw_qpolynomial
*pwqp2
;
2232 pwqp2
= read_factor(s
, map
, v
);
2233 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
2238 isl_pw_qpolynomial_free(pwqp
);
2242 static __isl_give isl_pw_qpolynomial
*read_term(__isl_keep isl_stream
*s
,
2243 __isl_keep isl_map
*map
, struct vars
*v
)
2245 struct isl_token
*tok
;
2246 isl_pw_qpolynomial
*pwqp
;
2248 pwqp
= read_factor(s
, map
, v
);
2251 tok
= next_token(s
);
2255 if (tok
->type
== '+') {
2256 isl_pw_qpolynomial
*pwqp2
;
2258 isl_token_free(tok
);
2259 pwqp2
= read_factor(s
, map
, v
);
2260 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2261 } else if (tok
->type
== '-') {
2262 isl_pw_qpolynomial
*pwqp2
;
2264 isl_token_free(tok
);
2265 pwqp2
= read_factor(s
, map
, v
);
2266 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
2267 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
2268 isl_int_is_neg(tok
->u
.v
)) {
2269 isl_pw_qpolynomial
*pwqp2
;
2271 isl_stream_push_token(s
, tok
);
2272 pwqp2
= read_factor(s
, map
, v
);
2273 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2275 isl_stream_push_token(s
, tok
);
2283 static __isl_give isl_map
*read_optional_formula(__isl_keep isl_stream
*s
,
2284 __isl_take isl_map
*map
, struct vars
*v
, int rational
)
2286 struct isl_token
*tok
;
2288 tok
= isl_stream_next_token(s
);
2290 isl_stream_error(s
, NULL
, "unexpected EOF");
2293 if (tok
->type
== ':' ||
2294 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
2295 isl_token_free(tok
);
2296 map
= read_formula(s
, v
, map
, rational
);
2298 isl_stream_push_token(s
, tok
);
2306 static struct isl_obj
obj_read_poly(__isl_keep isl_stream
*s
,
2307 __isl_take isl_map
*map
, struct vars
*v
, int n
)
2309 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
2310 isl_pw_qpolynomial
*pwqp
;
2311 struct isl_set
*set
;
2313 pwqp
= read_term(s
, map
, v
);
2314 map
= read_optional_formula(s
, map
, v
, 0);
2315 set
= isl_map_range(map
);
2317 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
2319 vars_drop(v
, v
->n
- n
);
2325 static struct isl_obj
obj_read_poly_or_fold(__isl_keep isl_stream
*s
,
2326 __isl_take isl_set
*set
, struct vars
*v
, int n
)
2328 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
2329 isl_pw_qpolynomial
*pwqp
;
2330 isl_pw_qpolynomial_fold
*pwf
= NULL
;
2332 if (!isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
))
2333 return obj_read_poly(s
, set
, v
, n
);
2335 if (isl_stream_eat(s
, '('))
2338 pwqp
= read_term(s
, set
, v
);
2339 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
, pwqp
);
2341 while (isl_stream_eat_if_available(s
, ',')) {
2342 isl_pw_qpolynomial_fold
*pwf_i
;
2343 pwqp
= read_term(s
, set
, v
);
2344 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
2346 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
2349 if (isl_stream_eat(s
, ')'))
2352 set
= read_optional_formula(s
, set
, v
, 0);
2353 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
2355 vars_drop(v
, v
->n
- n
);
2361 isl_pw_qpolynomial_fold_free(pwf
);
2362 obj
.type
= isl_obj_none
;
2366 static int is_rational(__isl_keep isl_stream
*s
)
2368 struct isl_token
*tok
;
2370 tok
= isl_stream_next_token(s
);
2373 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
2374 isl_token_free(tok
);
2375 isl_stream_eat(s
, ':');
2379 isl_stream_push_token(s
, tok
);
2384 static struct isl_obj
obj_read_body(__isl_keep isl_stream
*s
,
2385 __isl_take isl_map
*map
, struct vars
*v
)
2387 struct isl_token
*tok
;
2388 struct isl_obj obj
= { isl_obj_set
, NULL
};
2392 rational
= is_rational(s
);
2394 map
= isl_map_set_rational(map
);
2396 if (isl_stream_next_token_is(s
, ':')) {
2397 obj
.type
= isl_obj_set
;
2398 obj
.v
= read_optional_formula(s
, map
, v
, rational
);
2402 if (!next_is_tuple(s
))
2403 return obj_read_poly_or_fold(s
, map
, v
, n
);
2405 map
= read_map_tuple(s
, map
, isl_dim_in
, v
, rational
, 0);
2408 tok
= isl_stream_next_token(s
);
2411 if (tok
->type
== ISL_TOKEN_TO
) {
2412 obj
.type
= isl_obj_map
;
2413 isl_token_free(tok
);
2414 if (!next_is_tuple(s
)) {
2415 isl_set
*set
= isl_map_domain(map
);
2416 return obj_read_poly_or_fold(s
, set
, v
, n
);
2418 map
= read_map_tuple(s
, map
, isl_dim_out
, v
, rational
, 0);
2422 map
= isl_map_domain(map
);
2423 isl_stream_push_token(s
, tok
);
2426 map
= read_optional_formula(s
, map
, v
, rational
);
2428 vars_drop(v
, v
->n
- n
);
2434 obj
.type
= isl_obj_none
;
2438 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
2440 if (obj
.type
== isl_obj_map
) {
2441 obj
.v
= isl_union_map_from_map(obj
.v
);
2442 obj
.type
= isl_obj_union_map
;
2443 } else if (obj
.type
== isl_obj_set
) {
2444 obj
.v
= isl_union_set_from_set(obj
.v
);
2445 obj
.type
= isl_obj_union_set
;
2446 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
2447 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
2448 obj
.type
= isl_obj_union_pw_qpolynomial
;
2449 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
2450 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
2451 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
2453 isl_assert(ctx
, 0, goto error
);
2456 obj
.type
->free(obj
.v
);
2457 obj
.type
= isl_obj_none
;
2461 static struct isl_obj
obj_add(__isl_keep isl_stream
*s
,
2462 struct isl_obj obj1
, struct isl_obj obj2
)
2464 if (obj2
.type
== isl_obj_none
|| !obj2
.v
)
2466 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
2467 obj1
= to_union(s
->ctx
, obj1
);
2468 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
2469 obj2
= to_union(s
->ctx
, obj2
);
2470 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
2471 obj1
= to_union(s
->ctx
, obj1
);
2472 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
2473 obj2
= to_union(s
->ctx
, obj2
);
2474 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2475 obj2
.type
== isl_obj_union_pw_qpolynomial
)
2476 obj1
= to_union(s
->ctx
, obj1
);
2477 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
2478 obj2
.type
== isl_obj_pw_qpolynomial
)
2479 obj2
= to_union(s
->ctx
, obj2
);
2480 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2481 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
2482 obj1
= to_union(s
->ctx
, obj1
);
2483 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
2484 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
2485 obj2
= to_union(s
->ctx
, obj2
);
2486 if (obj1
.type
!= obj2
.type
) {
2487 isl_stream_error(s
, NULL
,
2488 "attempt to combine incompatible objects");
2491 if (!obj1
.type
->add
)
2492 isl_die(s
->ctx
, isl_error_internal
,
2493 "combination not supported on object type", goto error
);
2494 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
2495 obj1
= to_union(s
->ctx
, obj1
);
2496 obj2
= to_union(s
->ctx
, obj2
);
2498 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
2499 obj1
= to_union(s
->ctx
, obj1
);
2500 obj2
= to_union(s
->ctx
, obj2
);
2502 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2503 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
2504 obj1
= to_union(s
->ctx
, obj1
);
2505 obj2
= to_union(s
->ctx
, obj2
);
2507 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2508 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
2509 obj1
= to_union(s
->ctx
, obj1
);
2510 obj2
= to_union(s
->ctx
, obj2
);
2512 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
2515 obj1
.type
->free(obj1
.v
);
2516 obj2
.type
->free(obj2
.v
);
2517 obj1
.type
= isl_obj_none
;
2522 /* Are the first two tokens on "s", "domain" (either as a string
2523 * or as an identifier) followed by ":"?
2525 static int next_is_domain_colon(__isl_keep isl_stream
*s
)
2527 struct isl_token
*tok
;
2531 tok
= isl_stream_next_token(s
);
2534 if (tok
->type
!= ISL_TOKEN_IDENT
&& tok
->type
!= ISL_TOKEN_STRING
) {
2535 isl_stream_push_token(s
, tok
);
2539 name
= isl_token_get_str(s
->ctx
, tok
);
2540 res
= !strcmp(name
, "domain") && isl_stream_next_token_is(s
, ':');
2543 isl_stream_push_token(s
, tok
);
2548 /* Do the first tokens on "s" look like a schedule?
2550 * The root of a schedule is always a domain node, so the first thing
2551 * we expect in the stream is a domain key, i.e., "domain" followed
2552 * by ":". If the schedule was printed in YAML flow style, then
2553 * we additionally expect a "{" to open the outer mapping.
2555 static int next_is_schedule(__isl_keep isl_stream
*s
)
2557 struct isl_token
*tok
;
2560 tok
= isl_stream_next_token(s
);
2563 if (tok
->type
!= '{') {
2564 isl_stream_push_token(s
, tok
);
2565 return next_is_domain_colon(s
);
2568 is_schedule
= next_is_domain_colon(s
);
2569 isl_stream_push_token(s
, tok
);
2574 /* Read an isl_schedule from "s" and store it in an isl_obj.
2576 static struct isl_obj
schedule_read(__isl_keep isl_stream
*s
)
2580 obj
.type
= isl_obj_schedule
;
2581 obj
.v
= isl_stream_read_schedule(s
);
2586 /* Read a disjunction of object bodies from "s".
2587 * That is, read the inside of the braces, but not the braces themselves.
2588 * "v" contains a description of the identifiers parsed so far.
2589 * "map" contains information about the parameters.
2591 static struct isl_obj
obj_read_disjuncts(__isl_keep isl_stream
*s
,
2592 struct vars
*v
, __isl_keep isl_map
*map
)
2594 struct isl_obj obj
= { isl_obj_set
, NULL
};
2596 if (isl_stream_next_token_is(s
, '}')) {
2597 obj
.type
= isl_obj_union_set
;
2598 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
2604 o
= obj_read_body(s
, isl_map_copy(map
), v
);
2608 obj
= obj_add(s
, obj
, o
);
2609 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2611 if (!isl_stream_eat_if_available(s
, ';'))
2613 if (isl_stream_next_token_is(s
, '}'))
2620 static struct isl_obj
obj_read(__isl_keep isl_stream
*s
)
2622 isl_map
*map
= NULL
;
2623 struct isl_token
*tok
;
2624 struct vars
*v
= NULL
;
2625 struct isl_obj obj
= { isl_obj_set
, NULL
};
2627 if (next_is_schedule(s
))
2628 return schedule_read(s
);
2630 tok
= next_token(s
);
2632 isl_stream_error(s
, NULL
, "unexpected EOF");
2635 if (tok
->type
== ISL_TOKEN_VALUE
) {
2636 struct isl_token
*tok2
;
2637 struct isl_map
*map
;
2639 tok2
= isl_stream_next_token(s
);
2640 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
2641 isl_int_is_neg(tok2
->u
.v
)) {
2643 isl_stream_push_token(s
, tok2
);
2644 obj
.type
= isl_obj_val
;
2645 obj
.v
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
2646 isl_token_free(tok
);
2649 isl_stream_push_token(s
, tok2
);
2650 isl_stream_push_token(s
, tok
);
2651 map
= map_read_polylib(s
);
2654 if (isl_map_may_be_set(map
))
2655 obj
.v
= isl_map_range(map
);
2657 obj
.type
= isl_obj_map
;
2662 v
= vars_new(s
->ctx
);
2664 isl_stream_push_token(s
, tok
);
2667 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
2668 if (tok
->type
== '[') {
2669 isl_stream_push_token(s
, tok
);
2670 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 0);
2673 tok
= isl_stream_next_token(s
);
2674 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
2675 isl_stream_error(s
, tok
, "expecting '->'");
2677 isl_stream_push_token(s
, tok
);
2680 isl_token_free(tok
);
2681 tok
= isl_stream_next_token(s
);
2683 if (!tok
|| tok
->type
!= '{') {
2684 isl_stream_error(s
, tok
, "expecting '{'");
2686 isl_stream_push_token(s
, tok
);
2689 isl_token_free(tok
);
2691 tok
= isl_stream_next_token(s
);
2694 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
2695 isl_token_free(tok
);
2696 if (isl_stream_eat(s
, '='))
2698 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 1);
2702 isl_stream_push_token(s
, tok
);
2704 obj
= obj_read_disjuncts(s
, v
, map
);
2705 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2708 tok
= isl_stream_next_token(s
);
2709 if (tok
&& tok
->type
== '}') {
2710 isl_token_free(tok
);
2712 isl_stream_error(s
, tok
, "unexpected isl_token");
2714 isl_token_free(tok
);
2724 obj
.type
->free(obj
.v
);
2731 struct isl_obj
isl_stream_read_obj(__isl_keep isl_stream
*s
)
2736 __isl_give isl_map
*isl_stream_read_map(__isl_keep isl_stream
*s
)
2742 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
2743 obj
.type
== isl_obj_set
, goto error
);
2745 if (obj
.type
== isl_obj_set
)
2746 obj
.v
= isl_map_from_range(obj
.v
);
2750 obj
.type
->free(obj
.v
);
2754 __isl_give isl_set
*isl_stream_read_set(__isl_keep isl_stream
*s
)
2760 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
2761 obj
.v
= isl_map_range(obj
.v
);
2762 obj
.type
= isl_obj_set
;
2764 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
2769 obj
.type
->free(obj
.v
);
2773 __isl_give isl_union_map
*isl_stream_read_union_map(__isl_keep isl_stream
*s
)
2778 if (obj
.type
== isl_obj_map
) {
2779 obj
.type
= isl_obj_union_map
;
2780 obj
.v
= isl_union_map_from_map(obj
.v
);
2782 if (obj
.type
== isl_obj_set
) {
2783 obj
.type
= isl_obj_union_set
;
2784 obj
.v
= isl_union_set_from_set(obj
.v
);
2786 if (obj
.v
&& obj
.type
== isl_obj_union_set
&&
2787 isl_union_set_is_empty(obj
.v
))
2788 obj
.type
= isl_obj_union_map
;
2789 if (obj
.v
&& obj
.type
!= isl_obj_union_map
)
2790 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
2794 obj
.type
->free(obj
.v
);
2798 /* Extract an isl_union_set from "obj".
2799 * This only works if the object was detected as either a set
2800 * (in which case it is converted to a union set) or a union set.
2802 static __isl_give isl_union_set
*extract_union_set(isl_ctx
*ctx
,
2805 if (obj
.type
== isl_obj_set
) {
2806 obj
.type
= isl_obj_union_set
;
2807 obj
.v
= isl_union_set_from_set(obj
.v
);
2810 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
2814 obj
.type
->free(obj
.v
);
2818 /* Read an isl_union_set from "s".
2819 * First read a generic object and then try and extract
2820 * an isl_union_set from that.
2822 __isl_give isl_union_set
*isl_stream_read_union_set(__isl_keep isl_stream
*s
)
2827 return extract_union_set(s
->ctx
, obj
);
2830 static __isl_give isl_basic_map
*basic_map_read(__isl_keep isl_stream
*s
)
2833 struct isl_map
*map
;
2834 struct isl_basic_map
*bmap
;
2837 if (obj
.v
&& (obj
.type
!= isl_obj_map
&& obj
.type
!= isl_obj_set
))
2838 isl_die(s
->ctx
, isl_error_invalid
, "not a (basic) set or map",
2845 isl_die(s
->ctx
, isl_error_invalid
,
2846 "set or map description involves "
2847 "more than one disjunct", goto error
);
2850 bmap
= isl_basic_map_empty(isl_map_get_space(map
));
2852 bmap
= isl_basic_map_copy(map
->p
[0]);
2858 obj
.type
->free(obj
.v
);
2862 static __isl_give isl_basic_set
*basic_set_read(__isl_keep isl_stream
*s
)
2864 isl_basic_map
*bmap
;
2865 bmap
= basic_map_read(s
);
2868 if (!isl_basic_map_may_be_set(bmap
))
2869 isl_die(s
->ctx
, isl_error_invalid
,
2870 "input is not a set", goto error
);
2871 return isl_basic_map_range(bmap
);
2873 isl_basic_map_free(bmap
);
2877 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
2880 struct isl_basic_map
*bmap
;
2881 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2884 bmap
= basic_map_read(s
);
2889 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
2892 isl_basic_set
*bset
;
2893 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2896 bset
= basic_set_read(s
);
2901 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
2904 struct isl_basic_map
*bmap
;
2905 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2908 bmap
= basic_map_read(s
);
2913 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
2916 isl_basic_set
*bset
;
2917 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2920 bset
= basic_set_read(s
);
2925 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
2928 struct isl_map
*map
;
2929 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2932 map
= isl_stream_read_map(s
);
2937 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
2940 struct isl_map
*map
;
2941 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2944 map
= isl_stream_read_map(s
);
2949 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
2953 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2956 set
= isl_stream_read_set(s
);
2961 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
2965 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2968 set
= isl_stream_read_set(s
);
2973 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
2976 isl_union_map
*umap
;
2977 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2980 umap
= isl_stream_read_union_map(s
);
2985 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
2988 isl_union_map
*umap
;
2989 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2992 umap
= isl_stream_read_union_map(s
);
2997 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
3000 isl_union_set
*uset
;
3001 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3004 uset
= isl_stream_read_union_set(s
);
3009 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
3012 isl_union_set
*uset
;
3013 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3016 uset
= isl_stream_read_union_set(s
);
3021 static __isl_give isl_vec
*isl_vec_read_polylib(__isl_keep isl_stream
*s
)
3023 struct isl_vec
*vec
= NULL
;
3024 struct isl_token
*tok
;
3028 tok
= isl_stream_next_token(s
);
3029 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
3030 isl_stream_error(s
, tok
, "expecting vector length");
3034 size
= isl_int_get_si(tok
->u
.v
);
3035 isl_token_free(tok
);
3037 vec
= isl_vec_alloc(s
->ctx
, size
);
3039 for (j
= 0; j
< size
; ++j
) {
3040 tok
= isl_stream_next_token(s
);
3041 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
3042 isl_stream_error(s
, tok
, "expecting constant value");
3045 isl_int_set(vec
->el
[j
], tok
->u
.v
);
3046 isl_token_free(tok
);
3051 isl_token_free(tok
);
3056 static __isl_give isl_vec
*vec_read(__isl_keep isl_stream
*s
)
3058 return isl_vec_read_polylib(s
);
3061 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
3064 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3072 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
3073 __isl_keep isl_stream
*s
)
3079 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
3084 obj
.type
->free(obj
.v
);
3088 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
3091 isl_pw_qpolynomial
*pwqp
;
3092 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3095 pwqp
= isl_stream_read_pw_qpolynomial(s
);
3100 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
3103 isl_pw_qpolynomial
*pwqp
;
3104 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3107 pwqp
= isl_stream_read_pw_qpolynomial(s
);
3112 /* Is the next token an identifer not in "v"?
3114 static int next_is_fresh_ident(__isl_keep isl_stream
*s
, struct vars
*v
)
3118 struct isl_token
*tok
;
3120 tok
= isl_stream_next_token(s
);
3123 fresh
= tok
->type
== ISL_TOKEN_IDENT
&& vars_pos(v
, tok
->u
.s
, -1) >= n
;
3124 isl_stream_push_token(s
, tok
);
3126 vars_drop(v
, v
->n
- n
);
3131 /* First read the domain of the affine expression, which may be
3132 * a parameter space or a set.
3133 * The tricky part is that we don't know if the domain is a set or not,
3134 * so when we are trying to read the domain, we may actually be reading
3135 * the affine expression itself (defined on a parameter domains)
3136 * If the tuple we are reading is named, we assume it's the domain.
3137 * Also, if inside the tuple, the first thing we find is a nested tuple
3138 * or a new identifier, we again assume it's the domain.
3139 * Finally, if the tuple is empty, then it must be the domain
3140 * since it does not contain an affine expression.
3141 * Otherwise, we assume we are reading an affine expression.
3143 static __isl_give isl_set
*read_aff_domain(__isl_keep isl_stream
*s
,
3144 __isl_take isl_set
*dom
, struct vars
*v
)
3146 struct isl_token
*tok
, *tok2
;
3149 tok
= isl_stream_next_token(s
);
3150 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
3151 isl_stream_push_token(s
, tok
);
3152 return read_map_tuple(s
, dom
, isl_dim_set
, v
, 0, 0);
3154 if (!tok
|| tok
->type
!= '[') {
3155 isl_stream_error(s
, tok
, "expecting '['");
3158 tok2
= isl_stream_next_token(s
);
3159 is_empty
= tok2
&& tok2
->type
== ']';
3161 isl_stream_push_token(s
, tok2
);
3162 if (is_empty
|| next_is_tuple(s
) || next_is_fresh_ident(s
, v
)) {
3163 isl_stream_push_token(s
, tok
);
3164 dom
= read_map_tuple(s
, dom
, isl_dim_set
, v
, 0, 0);
3166 isl_stream_push_token(s
, tok
);
3171 isl_stream_push_token(s
, tok
);
3176 /* Read an affine expression from "s".
3178 __isl_give isl_aff
*isl_stream_read_aff(__isl_keep isl_stream
*s
)
3183 ma
= isl_stream_read_multi_aff(s
);
3186 if (isl_multi_aff_dim(ma
, isl_dim_out
) != 1)
3187 isl_die(s
->ctx
, isl_error_invalid
,
3188 "expecting single affine expression",
3191 aff
= isl_multi_aff_get_aff(ma
, 0);
3192 isl_multi_aff_free(ma
);
3195 isl_multi_aff_free(ma
);
3199 /* Read a piecewise affine expression from "s" with domain (space) "dom".
3201 static __isl_give isl_pw_aff
*read_pw_aff_with_dom(__isl_keep isl_stream
*s
,
3202 __isl_take isl_set
*dom
, struct vars
*v
)
3204 isl_pw_aff
*pwaff
= NULL
;
3206 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
3209 if (isl_stream_eat(s
, '['))
3212 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
3214 if (isl_stream_eat(s
, ']'))
3217 dom
= read_optional_formula(s
, dom
, v
, 0);
3218 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
3223 isl_pw_aff_free(pwaff
);
3227 __isl_give isl_pw_aff
*isl_stream_read_pw_aff(__isl_keep isl_stream
*s
)
3230 isl_set
*dom
= NULL
;
3232 isl_pw_aff
*pa
= NULL
;
3235 v
= vars_new(s
->ctx
);
3239 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3240 if (next_is_tuple(s
)) {
3241 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3242 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3245 if (isl_stream_eat(s
, '{'))
3249 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3250 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3251 vars_drop(v
, v
->n
- n
);
3253 while (isl_stream_eat_if_available(s
, ';')) {
3257 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3258 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3259 vars_drop(v
, v
->n
- n
);
3261 pa
= isl_pw_aff_union_add(pa
, pa_i
);
3264 if (isl_stream_eat(s
, '}'))
3273 isl_pw_aff_free(pa
);
3277 __isl_give isl_aff
*isl_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
3280 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3283 aff
= isl_stream_read_aff(s
);
3288 __isl_give isl_pw_aff
*isl_pw_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
3291 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3294 pa
= isl_stream_read_pw_aff(s
);
3299 /* Extract an isl_multi_pw_aff with domain space "dom_space"
3300 * from a tuple "tuple" read by read_tuple.
3302 * Note that the function read_tuple accepts tuples where some output or
3303 * set dimensions are defined in terms of other output or set dimensions
3304 * since this function is also used to read maps. As a special case,
3305 * read_tuple also accept dimensions that are defined in terms of themselves
3306 * (i.e., that are not defined).
3307 * These cases are not allowed when extracting an isl_multi_pw_aff so check
3308 * that the definitions of the output/set dimensions do not involve any
3309 * output/set dimensions.
3310 * Finally, drop the output dimensions from the domain of the result
3311 * of read_tuple (which is of the form [input, output] -> [output],
3312 * with anonymous domain) and reset the space.
3314 static __isl_give isl_multi_pw_aff
*extract_mpa_from_tuple(
3315 __isl_take isl_space
*dom_space
, __isl_keep isl_multi_pw_aff
*tuple
)
3319 isl_multi_pw_aff
*mpa
;
3321 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3322 dim
= isl_space_dim(dom_space
, isl_dim_all
);
3323 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3324 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3325 if (!isl_space_is_params(dom_space
))
3326 space
= isl_space_map_from_domain_and_range(
3327 isl_space_copy(dom_space
), space
);
3328 isl_space_free(dom_space
);
3329 mpa
= isl_multi_pw_aff_alloc(space
);
3331 for (i
= 0; i
< n
; ++i
) {
3333 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3335 return isl_multi_pw_aff_free(mpa
);
3336 if (isl_pw_aff_involves_dims(pa
, isl_dim_in
, dim
, i
+ 1)) {
3337 isl_ctx
*ctx
= isl_pw_aff_get_ctx(pa
);
3338 isl_pw_aff_free(pa
);
3339 isl_die(ctx
, isl_error_invalid
,
3340 "not an affine expression",
3341 return isl_multi_pw_aff_free(mpa
));
3343 pa
= isl_pw_aff_drop_dims(pa
, isl_dim_in
, dim
, n
);
3344 space
= isl_multi_pw_aff_get_domain_space(mpa
);
3345 pa
= isl_pw_aff_reset_domain_space(pa
, space
);
3346 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
3352 /* Read a tuple of affine expressions, together with optional constraints
3353 * on the domain from "s". "dom" represents the initial constraints
3356 * The isl_multi_aff may live in either a set or a map space.
3357 * First read the first tuple and check if it is followed by a "->".
3358 * If so, convert the tuple into the domain of the isl_multi_pw_aff and
3359 * read in the next tuple. This tuple (or the first tuple if it was
3360 * not followed by a "->") is then converted into an isl_multi_pw_aff
3361 * through a call to extract_mpa_from_tuple.
3362 * The result is converted to an isl_pw_multi_aff and
3363 * its domain is intersected with the domain.
3365 static __isl_give isl_pw_multi_aff
*read_conditional_multi_aff(
3366 __isl_keep isl_stream
*s
, __isl_take isl_set
*dom
, struct vars
*v
)
3368 isl_multi_pw_aff
*tuple
;
3369 isl_multi_pw_aff
*mpa
;
3370 isl_pw_multi_aff
*pma
;
3373 tuple
= read_tuple(s
, v
, 0, 0);
3376 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3377 isl_map
*map
= map_from_tuple(tuple
, dom
, isl_dim_in
, v
, 0);
3378 dom
= isl_map_domain(map
);
3379 tuple
= read_tuple(s
, v
, 0, 0);
3384 dom
= read_optional_formula(s
, dom
, v
, 0);
3386 vars_drop(v
, v
->n
- n
);
3388 mpa
= extract_mpa_from_tuple(isl_set_get_space(dom
), tuple
);
3389 isl_multi_pw_aff_free(tuple
);
3390 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
3391 pma
= isl_pw_multi_aff_intersect_domain(pma
, dom
);
3399 /* Read an isl_pw_multi_aff from "s".
3401 * In particular, first read the parameters and then read a sequence
3402 * of one or more tuples of affine expressions with optional conditions and
3405 __isl_give isl_pw_multi_aff
*isl_stream_read_pw_multi_aff(
3406 __isl_keep isl_stream
*s
)
3410 isl_pw_multi_aff
*pma
= NULL
;
3412 v
= vars_new(s
->ctx
);
3416 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3417 if (next_is_tuple(s
)) {
3418 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3419 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3422 if (isl_stream_eat(s
, '{'))
3425 pma
= read_conditional_multi_aff(s
, isl_set_copy(dom
), v
);
3427 while (isl_stream_eat_if_available(s
, ';')) {
3428 isl_pw_multi_aff
*pma2
;
3430 pma2
= read_conditional_multi_aff(s
, isl_set_copy(dom
), v
);
3431 pma
= isl_pw_multi_aff_union_add(pma
, pma2
);
3436 if (isl_stream_eat(s
, '}'))
3443 isl_pw_multi_aff_free(pma
);
3449 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_read_from_str(isl_ctx
*ctx
,
3452 isl_pw_multi_aff
*pma
;
3453 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3456 pma
= isl_stream_read_pw_multi_aff(s
);
3461 /* Read an isl_union_pw_multi_aff from "s".
3462 * We currently read a generic object and if it turns out to be a set or
3463 * a map, we convert that to an isl_union_pw_multi_aff.
3464 * It would be more efficient if we were to construct
3465 * the isl_union_pw_multi_aff directly.
3467 __isl_give isl_union_pw_multi_aff
*isl_stream_read_union_pw_multi_aff(
3468 __isl_keep isl_stream
*s
)
3476 if (obj
.type
== isl_obj_map
|| obj
.type
== isl_obj_set
)
3477 obj
= to_union(s
->ctx
, obj
);
3478 if (obj
.type
== isl_obj_union_map
)
3479 return isl_union_pw_multi_aff_from_union_map(obj
.v
);
3480 if (obj
.type
== isl_obj_union_set
)
3481 return isl_union_pw_multi_aff_from_union_set(obj
.v
);
3483 obj
.type
->free(obj
.v
);
3484 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
3488 /* Read an isl_union_pw_multi_aff from "str".
3490 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_read_from_str(
3491 isl_ctx
*ctx
, const char *str
)
3493 isl_union_pw_multi_aff
*upma
;
3494 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3497 upma
= isl_stream_read_union_pw_multi_aff(s
);
3502 /* Assuming "pa" represents a single affine expression defined on a universe
3503 * domain, extract this affine expression.
3505 static __isl_give isl_aff
*aff_from_pw_aff(__isl_take isl_pw_aff
*pa
)
3512 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3513 "expecting single affine expression",
3515 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
3516 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3517 "expecting universe domain",
3520 aff
= isl_aff_copy(pa
->p
[0].aff
);
3521 isl_pw_aff_free(pa
);
3524 isl_pw_aff_free(pa
);
3528 /* This function is called for each element in a tuple inside
3529 * isl_stream_read_multi_val.
3530 * Read an isl_val from "s" and add it to *list.
3532 static __isl_give isl_space
*read_val_el(__isl_keep isl_stream
*s
,
3533 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
3535 isl_val_list
**list
= (isl_val_list
**) user
;
3538 val
= isl_stream_read_val(s
);
3539 *list
= isl_val_list_add(*list
, val
);
3541 return isl_space_free(space
);
3546 /* Read an isl_multi_val from "s".
3548 * We first read a tuple space, collecting the element values in a list.
3549 * Then we create an isl_multi_val from the space and the isl_val_list.
3551 __isl_give isl_multi_val
*isl_stream_read_multi_val(__isl_keep isl_stream
*s
)
3554 isl_set
*dom
= NULL
;
3556 isl_multi_val
*mv
= NULL
;
3559 v
= vars_new(s
->ctx
);
3563 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3564 if (next_is_tuple(s
)) {
3565 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3566 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3569 if (!isl_set_plain_is_universe(dom
))
3570 isl_die(s
->ctx
, isl_error_invalid
,
3571 "expecting universe parameter domain", goto error
);
3572 if (isl_stream_eat(s
, '{'))
3575 space
= isl_set_get_space(dom
);
3577 list
= isl_val_list_alloc(s
->ctx
, 0);
3578 space
= read_tuple_space(s
, v
, space
, 1, 0, &read_val_el
, &list
);
3579 mv
= isl_multi_val_from_val_list(space
, list
);
3581 if (isl_stream_eat(s
, '}'))
3590 isl_multi_val_free(mv
);
3594 /* Read an isl_multi_val from "str".
3596 __isl_give isl_multi_val
*isl_multi_val_read_from_str(isl_ctx
*ctx
,
3600 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3603 mv
= isl_stream_read_multi_val(s
);
3608 /* Read a multi-affine expression from "s".
3609 * If the multi-affine expression has a domain, then the tuple
3610 * representing this domain cannot involve any affine expressions.
3611 * The tuple representing the actual expressions needs to consist
3612 * of only affine expressions. Moreover, these expressions can
3613 * only depend on parameters and input dimensions and not on other
3614 * output dimensions.
3616 __isl_give isl_multi_aff
*isl_stream_read_multi_aff(__isl_keep isl_stream
*s
)
3619 isl_set
*dom
= NULL
;
3620 isl_multi_pw_aff
*tuple
= NULL
;
3622 isl_space
*space
, *dom_space
;
3623 isl_multi_aff
*ma
= NULL
;
3625 v
= vars_new(s
->ctx
);
3629 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3630 if (next_is_tuple(s
)) {
3631 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3632 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3635 if (!isl_set_plain_is_universe(dom
))
3636 isl_die(s
->ctx
, isl_error_invalid
,
3637 "expecting universe parameter domain", goto error
);
3638 if (isl_stream_eat(s
, '{'))
3641 tuple
= read_tuple(s
, v
, 0, 0);
3644 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3649 has_expr
= tuple_has_expr(tuple
);
3653 isl_die(s
->ctx
, isl_error_invalid
,
3654 "expecting universe domain", goto error
);
3655 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3656 set
= isl_set_universe(space
);
3657 dom
= isl_set_intersect_params(set
, dom
);
3658 isl_multi_pw_aff_free(tuple
);
3659 tuple
= read_tuple(s
, v
, 0, 0);
3664 if (isl_stream_eat(s
, '}'))
3667 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3668 dim
= isl_set_dim(dom
, isl_dim_all
);
3669 dom_space
= isl_set_get_space(dom
);
3670 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3671 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3672 if (!isl_space_is_params(dom_space
))
3673 space
= isl_space_map_from_domain_and_range(
3674 isl_space_copy(dom_space
), space
);
3675 isl_space_free(dom_space
);
3676 ma
= isl_multi_aff_alloc(space
);
3678 for (i
= 0; i
< n
; ++i
) {
3681 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3682 aff
= aff_from_pw_aff(pa
);
3685 if (isl_aff_involves_dims(aff
, isl_dim_in
, dim
, i
+ 1)) {
3687 isl_die(s
->ctx
, isl_error_invalid
,
3688 "not an affine expression", goto error
);
3690 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, dim
, n
);
3691 space
= isl_multi_aff_get_domain_space(ma
);
3692 aff
= isl_aff_reset_domain_space(aff
, space
);
3693 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3696 isl_multi_pw_aff_free(tuple
);
3701 isl_multi_pw_aff_free(tuple
);
3704 isl_multi_aff_free(ma
);
3708 __isl_give isl_multi_aff
*isl_multi_aff_read_from_str(isl_ctx
*ctx
,
3711 isl_multi_aff
*maff
;
3712 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3715 maff
= isl_stream_read_multi_aff(s
);
3720 /* Read an isl_multi_pw_aff from "s".
3722 * The input format is similar to that of map, except that any conditions
3723 * on the domains should be specified inside the tuple since each
3724 * piecewise affine expression may have a different domain.
3725 * However, additional, shared conditions can also be specified.
3726 * This is especially useful for setting the explicit domain
3727 * of a zero-dimensional isl_multi_pw_aff.
3729 * Since we do not know in advance if the isl_multi_pw_aff lives
3730 * in a set or a map space, we first read the first tuple and check
3731 * if it is followed by a "->". If so, we convert the tuple into
3732 * the domain of the isl_multi_pw_aff and read in the next tuple.
3733 * This tuple (or the first tuple if it was not followed by a "->")
3734 * is then converted into the isl_multi_pw_aff through a call
3735 * to extract_mpa_from_tuple and the domain of the result
3736 * is intersected with the domain.
3738 __isl_give isl_multi_pw_aff
*isl_stream_read_multi_pw_aff(
3739 __isl_keep isl_stream
*s
)
3742 isl_set
*dom
= NULL
;
3743 isl_multi_pw_aff
*tuple
= NULL
;
3744 isl_multi_pw_aff
*mpa
= NULL
;
3746 v
= vars_new(s
->ctx
);
3750 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3751 if (next_is_tuple(s
)) {
3752 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3753 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3756 if (isl_stream_eat(s
, '{'))
3759 tuple
= read_tuple(s
, v
, 0, 0);
3762 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3763 isl_map
*map
= map_from_tuple(tuple
, dom
, isl_dim_in
, v
, 0);
3764 dom
= isl_map_domain(map
);
3765 tuple
= read_tuple(s
, v
, 0, 0);
3770 if (isl_stream_eat_if_available(s
, ':'))
3771 dom
= read_formula(s
, v
, dom
, 0);
3773 if (isl_stream_eat(s
, '}'))
3776 mpa
= extract_mpa_from_tuple(isl_set_get_space(dom
), tuple
);
3778 isl_multi_pw_aff_free(tuple
);
3780 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
3783 isl_multi_pw_aff_free(tuple
);
3786 isl_multi_pw_aff_free(mpa
);
3790 /* Read an isl_multi_pw_aff from "str".
3792 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_read_from_str(isl_ctx
*ctx
,
3795 isl_multi_pw_aff
*mpa
;
3796 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3799 mpa
= isl_stream_read_multi_pw_aff(s
);
3804 /* Read the body of an isl_union_pw_aff from "s" with parameter domain "dom".
3806 static __isl_give isl_union_pw_aff
*read_union_pw_aff_with_dom(
3807 __isl_keep isl_stream
*s
, __isl_take isl_set
*dom
, struct vars
*v
)
3810 isl_union_pw_aff
*upa
= NULL
;
3815 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3816 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3817 vars_drop(v
, v
->n
- n
);
3819 upa
= isl_union_pw_aff_from_pw_aff(pa
);
3821 while (isl_stream_eat_if_available(s
, ';')) {
3823 isl_union_pw_aff
*upa_i
;
3826 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3827 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3828 vars_drop(v
, v
->n
- n
);
3830 upa_i
= isl_union_pw_aff_from_pw_aff(pa_i
);
3831 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
3838 /* Read an isl_union_pw_aff from "s".
3840 * First check if there are any paramters, then read in the opening brace
3841 * and use read_union_pw_aff_with_dom to read in the body of
3842 * the isl_union_pw_aff. Finally, read the closing brace.
3844 __isl_give isl_union_pw_aff
*isl_stream_read_union_pw_aff(
3845 __isl_keep isl_stream
*s
)
3849 isl_union_pw_aff
*upa
= NULL
;
3851 v
= vars_new(s
->ctx
);
3855 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3856 if (next_is_tuple(s
)) {
3857 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3858 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3861 if (isl_stream_eat(s
, '{'))
3864 upa
= read_union_pw_aff_with_dom(s
, isl_set_copy(dom
), v
);
3866 if (isl_stream_eat(s
, '}'))
3875 isl_union_pw_aff_free(upa
);
3879 /* Read an isl_union_pw_aff from "str".
3881 __isl_give isl_union_pw_aff
*isl_union_pw_aff_read_from_str(isl_ctx
*ctx
,
3884 isl_union_pw_aff
*upa
;
3885 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3888 upa
= isl_stream_read_union_pw_aff(s
);
3893 /* This function is called for each element in a tuple inside
3894 * isl_stream_read_multi_union_pw_aff.
3896 * Read a '{', the union piecewise affine expression body and a '}' and
3897 * add the isl_union_pw_aff to *list.
3899 static __isl_give isl_space
*read_union_pw_aff_el(__isl_keep isl_stream
*s
,
3900 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
3903 isl_union_pw_aff
*upa
;
3904 isl_union_pw_aff_list
**list
= (isl_union_pw_aff_list
**) user
;
3906 dom
= isl_set_universe(isl_space_params(isl_space_copy(space
)));
3907 if (isl_stream_eat(s
, '{'))
3909 upa
= read_union_pw_aff_with_dom(s
, dom
, v
);
3910 *list
= isl_union_pw_aff_list_add(*list
, upa
);
3911 if (isl_stream_eat(s
, '}'))
3912 return isl_space_free(space
);
3914 return isl_space_free(space
);
3918 return isl_space_free(space
);
3921 /* Do the next tokens in "s" correspond to an empty tuple?
3922 * In particular, does the stream start with a '[', followed by a ']',
3923 * not followed by a "->"?
3925 static int next_is_empty_tuple(__isl_keep isl_stream
*s
)
3927 struct isl_token
*tok
, *tok2
, *tok3
;
3928 int is_empty_tuple
= 0;
3930 tok
= isl_stream_next_token(s
);
3933 if (tok
->type
!= '[') {
3934 isl_stream_push_token(s
, tok
);
3938 tok2
= isl_stream_next_token(s
);
3939 if (tok2
&& tok2
->type
== ']') {
3940 tok3
= isl_stream_next_token(s
);
3941 is_empty_tuple
= !tok
|| tok
->type
!= ISL_TOKEN_TO
;
3943 isl_stream_push_token(s
, tok3
);
3946 isl_stream_push_token(s
, tok2
);
3947 isl_stream_push_token(s
, tok
);
3949 return is_empty_tuple
;
3952 /* Do the next tokens in "s" correspond to a tuple of parameters?
3953 * In particular, does the stream start with a '[' that is not
3954 * followed by a '{' or a nested tuple?
3956 static int next_is_param_tuple(__isl_keep isl_stream
*s
)
3958 struct isl_token
*tok
, *tok2
;
3961 tok
= isl_stream_next_token(s
);
3964 if (tok
->type
!= '[' || next_is_tuple(s
)) {
3965 isl_stream_push_token(s
, tok
);
3969 tok2
= isl_stream_next_token(s
);
3970 is_tuple
= tok2
&& tok2
->type
!= '{';
3972 isl_stream_push_token(s
, tok2
);
3973 isl_stream_push_token(s
, tok
);
3978 /* Read the core of a body of an isl_multi_union_pw_aff from "s",
3979 * i.e., everything except the parameter specification and
3980 * without shared domain constraints.
3981 * "v" contains a description of the identifiers parsed so far.
3982 * The parameters, if any, are specified by "space".
3984 * The body is of the form
3986 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
3988 * Read the tuple, collecting the individual isl_union_pw_aff
3989 * elements in a list and construct the result from the tuple space and
3992 static __isl_give isl_multi_union_pw_aff
*read_multi_union_pw_aff_body_core(
3993 __isl_keep isl_stream
*s
, struct vars
*v
, __isl_take isl_space
*space
)
3995 isl_union_pw_aff_list
*list
;
3996 isl_multi_union_pw_aff
*mupa
;
3998 list
= isl_union_pw_aff_list_alloc(s
->ctx
, 0);
3999 space
= read_tuple_space(s
, v
, space
, 1, 0,
4000 &read_union_pw_aff_el
, &list
);
4001 mupa
= isl_multi_union_pw_aff_from_union_pw_aff_list(space
, list
);
4006 /* Read the body of an isl_union_set from "s",
4007 * i.e., everything except the parameter specification.
4008 * "v" contains a description of the identifiers parsed so far.
4009 * The parameters, if any, are specified by "space".
4011 * First read a generic disjunction of object bodies and then try and extract
4012 * an isl_union_set from that.
4014 static __isl_give isl_union_set
*read_union_set_body(__isl_keep isl_stream
*s
,
4015 struct vars
*v
, __isl_take isl_space
*space
)
4017 struct isl_obj obj
= { isl_obj_set
, NULL
};
4020 map
= isl_set_universe(space
);
4021 if (isl_stream_eat(s
, '{') < 0)
4023 obj
= obj_read_disjuncts(s
, v
, map
);
4024 if (isl_stream_eat(s
, '}') < 0)
4028 return extract_union_set(s
->ctx
, obj
);
4030 obj
.type
->free(obj
.v
);
4035 /* Read the body of an isl_multi_union_pw_aff from "s",
4036 * i.e., everything except the parameter specification.
4037 * "v" contains a description of the identifiers parsed so far.
4038 * The parameters, if any, are specified by "space".
4040 * In particular, handle the special case with shared domain constraints.
4041 * These are specified as
4045 * and are especially useful for setting the explicit domain
4046 * of a zero-dimensional isl_multi_union_pw_aff.
4047 * The core isl_multi_union_pw_aff body ([...]) is read by
4048 * read_multi_union_pw_aff_body_core.
4050 static __isl_give isl_multi_union_pw_aff
*read_multi_union_pw_aff_body(
4051 __isl_keep isl_stream
*s
, struct vars
*v
, __isl_take isl_space
*space
)
4053 isl_multi_union_pw_aff
*mupa
;
4055 if (!isl_stream_next_token_is(s
, '('))
4056 return read_multi_union_pw_aff_body_core(s
, v
, space
);
4058 if (isl_stream_eat(s
, '(') < 0)
4060 mupa
= read_multi_union_pw_aff_body_core(s
, v
, isl_space_copy(space
));
4061 if (isl_stream_eat_if_available(s
, ':')) {
4064 dom
= read_union_set_body(s
, v
, space
);
4065 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
4067 isl_space_free(space
);
4069 if (isl_stream_eat(s
, ')') < 0)
4070 return isl_multi_union_pw_aff_free(mupa
);
4074 isl_space_free(space
);
4078 /* Read an isl_multi_union_pw_aff from "s".
4080 * The input has the form
4082 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4086 * [..] -> [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4088 * Additionally, a shared domain may be specified as
4094 * [..] -> ([..] : ...)
4096 * The first case is handled by the caller, the second case
4097 * is handled by read_multi_union_pw_aff_body.
4099 * We first check for the special case of an empty tuple "[]".
4100 * Then we check if there are any parameters.
4101 * Finally, read the tuple and construct the result.
4103 static __isl_give isl_multi_union_pw_aff
*read_multi_union_pw_aff_core(
4104 __isl_keep isl_stream
*s
)
4107 isl_set
*dom
= NULL
;
4109 isl_multi_union_pw_aff
*mupa
= NULL
;
4111 if (next_is_empty_tuple(s
)) {
4112 if (isl_stream_eat(s
, '['))
4114 if (isl_stream_eat(s
, ']'))
4116 space
= isl_space_set_alloc(s
->ctx
, 0, 0);
4117 return isl_multi_union_pw_aff_zero(space
);
4120 v
= vars_new(s
->ctx
);
4124 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
4125 if (next_is_param_tuple(s
)) {
4126 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
4127 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
4130 space
= isl_set_get_space(dom
);
4132 mupa
= read_multi_union_pw_aff_body(s
, v
, space
);
4140 isl_multi_union_pw_aff_free(mupa
);
4144 /* Read an isl_multi_union_pw_aff from "s".
4146 * In particular, handle the special case with shared domain constraints.
4147 * These are specified as
4151 * and are especially useful for setting the explicit domain
4152 * of a zero-dimensional isl_multi_union_pw_aff.
4153 * The core isl_multi_union_pw_aff ([...]) is read by
4154 * read_multi_union_pw_aff_core.
4156 __isl_give isl_multi_union_pw_aff
*isl_stream_read_multi_union_pw_aff(
4157 __isl_keep isl_stream
*s
)
4159 isl_multi_union_pw_aff
*mupa
;
4161 if (!isl_stream_next_token_is(s
, '('))
4162 return read_multi_union_pw_aff_core(s
);
4164 if (isl_stream_eat(s
, '(') < 0)
4166 mupa
= read_multi_union_pw_aff_core(s
);
4167 if (isl_stream_eat_if_available(s
, ':')) {
4170 dom
= isl_stream_read_union_set(s
);
4171 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
4173 if (isl_stream_eat(s
, ')') < 0)
4174 return isl_multi_union_pw_aff_free(mupa
);
4178 /* Read an isl_multi_union_pw_aff from "str".
4180 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_read_from_str(
4181 isl_ctx
*ctx
, const char *str
)
4183 isl_multi_union_pw_aff
*mupa
;
4184 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
4187 mupa
= isl_stream_read_multi_union_pw_aff(s
);
4192 __isl_give isl_union_pw_qpolynomial
*isl_stream_read_union_pw_qpolynomial(
4193 __isl_keep isl_stream
*s
)
4198 if (obj
.type
== isl_obj_pw_qpolynomial
) {
4199 obj
.type
= isl_obj_union_pw_qpolynomial
;
4200 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
4203 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_pw_qpolynomial
,
4208 obj
.type
->free(obj
.v
);
4212 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_read_from_str(
4213 isl_ctx
*ctx
, const char *str
)
4215 isl_union_pw_qpolynomial
*upwqp
;
4216 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
4219 upwqp
= isl_stream_read_union_pw_qpolynomial(s
);