2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012 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_map.h>
26 #include <isl_mat_private.h>
27 #include <isl_aff_private.h>
29 #include <isl_val_private.h>
34 struct variable *next;
43 static struct vars *vars_new(struct isl_ctx *ctx)
46 v = isl_alloc_type(ctx, struct vars);
55 static void variable_free(struct variable *var)
58 struct variable *next = var->next;
65 static void vars_free(struct vars *v)
73 static void vars_drop(struct vars *v, int n)
84 struct variable *next = var->next;
92 static struct variable *variable_new(struct vars *v, const char *name, int len,
96 var = isl_calloc_type(v->ctx, struct variable);
99 var->name = strdup(name);
100 var->name[len] = '\0';
109 static int vars_pos(struct vars *v, const char *s, int len)
116 for (q = v->v; q; q = q->next) {
117 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
124 v->v = variable_new(v, s, len, v->n);
132 static int vars_add_anon(struct vars *v)
134 v->v = variable_new(v, "", 0, v->n);
143 /* Obtain next token, with some preprocessing.
144 * In particular, evaluate expressions of the form x^y,
145 * with x and y values.
147 static struct isl_token *next_token(struct isl_stream *s)
149 struct isl_token *tok, *tok2;
151 tok = isl_stream_next_token(s);
152 if (!tok || tok->type != ISL_TOKEN_VALUE)
154 if (!isl_stream_eat_if_available(s, '^'))
156 tok2 = isl_stream_next_token(s);
157 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
158 isl_stream_error(s, tok2, "expecting constant value");
162 isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
164 isl_token_free(tok2);
168 isl_token_free(tok2);
172 /* Read an isl_val from "s".
174 * The following token sequences are recognized
177 * "-" "infty" -> -infty
182 * where n, d and v are integer constants.
184 __isl_give isl_val *isl_stream_read_val(struct isl_stream *s)
186 struct isl_token *tok = NULL;
187 struct isl_token *tok2 = NULL;
192 isl_stream_error(s, NULL, "unexpected EOF");
195 if (tok->type == ISL_TOKEN_INFTY) {
197 return isl_val_infty(s->ctx);
199 if (tok->type == '-' &&
200 isl_stream_eat_if_available(s, ISL_TOKEN_INFTY)) {
202 return isl_val_neginfty(s->ctx);
204 if (tok->type == ISL_TOKEN_NAN) {
206 return isl_val_nan(s->ctx);
208 if (tok->type != ISL_TOKEN_VALUE) {
209 isl_stream_error(s, tok, "expecting value");
213 if (isl_stream_eat_if_available(s, '/')) {
214 tok2 = next_token(s);
216 isl_stream_error(s, NULL, "unexpected EOF");
219 if (tok2->type != ISL_TOKEN_VALUE) {
220 isl_stream_error(s, tok2, "expecting value");
223 val = isl_val_rat_from_isl_int(s->ctx, tok->u.v, tok2->u.v);
224 val = isl_val_normalize(val);
226 val = isl_val_int_from_isl_int(s->ctx, tok->u.v);
230 isl_token_free(tok2);
234 isl_token_free(tok2);
238 /* Read an isl_val from "str".
240 struct isl_val *isl_val_read_from_str(struct isl_ctx *ctx,
244 struct isl_stream *s = isl_stream_new_str(ctx, str);
247 val = isl_stream_read_val(s);
252 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
254 struct isl_token *tok;
257 if (!tok || tok->type != ISL_TOKEN_VALUE) {
258 isl_stream_error(s, tok, "expecting constant value");
262 isl_int_mul(*f, *f, tok->u.v);
266 if (isl_stream_eat_if_available(s, '*'))
267 return accept_cst_factor(s, f);
275 /* Given an affine expression aff, return an affine expression
276 * for aff % d, with d the next token on the stream, which is
277 * assumed to be a constant.
279 * We introduce an integer division q = [aff/d] and the result
280 * is set to aff - d q.
282 static __isl_give isl_pw_aff *affine_mod(struct isl_stream *s,
283 struct vars *v, __isl_take isl_pw_aff *aff)
285 struct isl_token *tok;
289 if (!tok || tok->type != ISL_TOKEN_VALUE) {
290 isl_stream_error(s, tok, "expecting constant value");
294 q = isl_pw_aff_copy(aff);
295 q = isl_pw_aff_scale_down(q, tok->u.v);
296 q = isl_pw_aff_floor(q);
297 q = isl_pw_aff_scale(q, tok->u.v);
299 aff = isl_pw_aff_sub(aff, q);
304 isl_pw_aff_free(aff);
309 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
310 __isl_take isl_space *dim, struct vars *v);
311 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
312 __isl_take isl_space *dim, struct vars *v);
314 static __isl_give isl_pw_aff *accept_minmax(struct isl_stream *s,
315 __isl_take isl_space *dim, struct vars *v)
317 struct isl_token *tok;
318 isl_pw_aff_list *list = NULL;
321 tok = isl_stream_next_token(s);
324 min = tok->type == ISL_TOKEN_MIN;
327 if (isl_stream_eat(s, '('))
330 list = accept_affine_list(s, isl_space_copy(dim), v);
334 if (isl_stream_eat(s, ')'))
338 return min ? isl_pw_aff_list_min(list) : isl_pw_aff_list_max(list);
341 isl_pw_aff_list_free(list);
345 static __isl_give isl_pw_aff *accept_div(struct isl_stream *s,
346 __isl_take isl_space *dim, struct vars *v)
348 struct isl_token *tok;
351 isl_pw_aff *pwaff = NULL;
353 if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOORD))
355 else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEILD))
358 if (isl_stream_eat(s, '('))
361 if (isl_stream_eat(s, '['))
365 pwaff = accept_affine(s, isl_space_copy(dim), v);
368 if (isl_stream_eat(s, ','))
374 if (tok->type != ISL_TOKEN_VALUE) {
375 isl_stream_error(s, tok, "expected denominator");
376 isl_stream_push_token(s, tok);
379 isl_pw_aff_scale_down(pwaff, tok->u.v);
384 pwaff = isl_pw_aff_ceil(pwaff);
386 pwaff = isl_pw_aff_floor(pwaff);
389 if (isl_stream_eat(s, ')'))
392 if (isl_stream_eat(s, ']'))
400 isl_pw_aff_free(pwaff);
404 static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
405 __isl_take isl_space *dim, struct vars *v)
407 struct isl_token *tok = NULL;
408 isl_pw_aff *res = NULL;
412 isl_stream_error(s, NULL, "unexpected EOF");
416 if (tok->type == ISL_TOKEN_AFF) {
417 res = isl_pw_aff_copy(tok->u.pwaff);
419 } else if (tok->type == ISL_TOKEN_IDENT) {
421 int pos = vars_pos(v, tok->u.s, -1);
427 vars_drop(v, v->n - n);
428 isl_stream_error(s, tok, "unknown identifier");
432 aff = isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim)));
435 isl_int_set_si(aff->v->el[2 + pos], 1);
436 res = isl_pw_aff_from_aff(aff);
438 } else if (tok->type == ISL_TOKEN_VALUE) {
439 if (isl_stream_eat_if_available(s, '*')) {
440 res = accept_affine_factor(s, isl_space_copy(dim), v);
441 res = isl_pw_aff_scale(res, tok->u.v);
445 ls = isl_local_space_from_space(isl_space_copy(dim));
446 aff = isl_aff_zero_on_domain(ls);
447 aff = isl_aff_add_constant(aff, tok->u.v);
448 res = isl_pw_aff_from_aff(aff);
451 } else if (tok->type == '(') {
454 res = accept_affine(s, isl_space_copy(dim), v);
457 if (isl_stream_eat(s, ')'))
459 } else if (tok->type == '[' ||
460 tok->type == ISL_TOKEN_FLOORD ||
461 tok->type == ISL_TOKEN_CEILD) {
462 isl_stream_push_token(s, tok);
464 res = accept_div(s, isl_space_copy(dim), v);
465 } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
466 isl_stream_push_token(s, tok);
468 res = accept_minmax(s, isl_space_copy(dim), v);
470 isl_stream_error(s, tok, "expecting factor");
473 if (isl_stream_eat_if_available(s, '%') ||
474 isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
476 return affine_mod(s, v, res);
478 if (isl_stream_eat_if_available(s, '*')) {
481 isl_int_set_si(f, 1);
482 if (accept_cst_factor(s, &f) < 0) {
486 res = isl_pw_aff_scale(res, f);
489 if (isl_stream_eat_if_available(s, '/')) {
492 isl_int_set_si(f, 1);
493 if (accept_cst_factor(s, &f) < 0) {
497 res = isl_pw_aff_scale_down(res, f);
506 isl_pw_aff_free(res);
511 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
516 space = isl_pw_aff_get_domain_space(pwaff);
517 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
518 aff = isl_aff_add_constant(aff, v);
520 return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
523 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
524 __isl_take isl_space *dim, struct vars *v)
526 struct isl_token *tok = NULL;
531 ls = isl_local_space_from_space(isl_space_copy(dim));
532 res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
539 isl_stream_error(s, NULL, "unexpected EOF");
542 if (tok->type == '-') {
547 if (tok->type == '(' || tok->type == '[' ||
548 tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
549 tok->type == ISL_TOKEN_FLOORD ||
550 tok->type == ISL_TOKEN_CEILD ||
551 tok->type == ISL_TOKEN_IDENT ||
552 tok->type == ISL_TOKEN_AFF) {
554 isl_stream_push_token(s, tok);
556 term = accept_affine_factor(s, isl_space_copy(dim), v);
558 res = isl_pw_aff_sub(res, term);
560 res = isl_pw_aff_add(res, term);
564 } else if (tok->type == ISL_TOKEN_VALUE) {
566 isl_int_neg(tok->u.v, tok->u.v);
567 if (isl_stream_eat_if_available(s, '*') ||
568 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
570 term = accept_affine_factor(s,
571 isl_space_copy(dim), v);
572 term = isl_pw_aff_scale(term, tok->u.v);
573 res = isl_pw_aff_add(res, term);
577 res = add_cst(res, tok->u.v);
581 isl_stream_error(s, tok, "unexpected isl_token");
582 isl_stream_push_token(s, tok);
583 isl_pw_aff_free(res);
590 if (tok && tok->type == '-') {
593 } else if (tok && tok->type == '+') {
596 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
597 isl_int_is_neg(tok->u.v)) {
598 isl_stream_push_token(s, tok);
601 isl_stream_push_token(s, tok);
611 isl_pw_aff_free(res);
615 static int is_comparator(struct isl_token *tok)
633 static struct isl_map *read_disjuncts(struct isl_stream *s,
634 struct vars *v, __isl_take isl_map *map, int rational);
635 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
636 __isl_take isl_space *dim, struct vars *v, int rational);
638 /* Accept a ternary operator, given the first argument.
640 static __isl_give isl_pw_aff *accept_ternary(struct isl_stream *s,
641 __isl_take isl_map *cond, struct vars *v, int rational)
644 isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL, *pa_cond;
649 if (isl_stream_eat(s, '?'))
652 dim = isl_space_wrap(isl_map_get_space(cond));
653 pwaff1 = accept_extended_affine(s, dim, v, rational);
657 if (isl_stream_eat(s, ':'))
660 dim = isl_pw_aff_get_domain_space(pwaff1);
661 pwaff2 = accept_extended_affine(s, dim, v, rational);
665 pa_cond = isl_set_indicator_function(isl_map_wrap(cond));
666 return isl_pw_aff_cond(pa_cond, pwaff1, pwaff2);
669 isl_pw_aff_free(pwaff1);
670 isl_pw_aff_free(pwaff2);
674 /* Accept an affine expression that may involve ternary operators.
675 * We first read an affine expression.
676 * If it is not followed by a comparison operator, we simply return it.
677 * Otherwise, we assume the affine epxression is part of the first
678 * argument of a ternary operator and try to parse that.
680 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
681 __isl_take isl_space *dim, struct vars *v, int rational)
686 struct isl_token *tok;
687 int line = -1, col = -1;
690 tok = isl_stream_next_token(s);
694 isl_stream_push_token(s, tok);
697 pwaff = accept_affine(s, dim, v);
699 pwaff = isl_pw_aff_set_rational(pwaff);
703 tok = isl_stream_next_token(s);
705 return isl_pw_aff_free(pwaff);
707 is_comp = is_comparator(tok);
708 isl_stream_push_token(s, tok);
712 tok = isl_token_new(s->ctx, line, col, 0);
714 return isl_pw_aff_free(pwaff);
715 tok->type = ISL_TOKEN_AFF;
716 tok->u.pwaff = pwaff;
718 space = isl_pw_aff_get_domain_space(pwaff);
719 cond = isl_map_universe(isl_space_unwrap(space));
721 isl_stream_push_token(s, tok);
723 cond = read_disjuncts(s, v, cond, rational);
725 return accept_ternary(s, cond, v, rational);
728 static __isl_give isl_map *read_var_def(struct isl_stream *s,
729 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
736 if (type == isl_dim_param)
737 pos = isl_map_dim(map, isl_dim_param);
739 pos = isl_map_dim(map, isl_dim_in);
740 if (type == isl_dim_out)
741 pos += isl_map_dim(map, isl_dim_out);
746 def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)),
748 def_map = isl_map_from_pw_aff(def);
749 def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
750 def_map = isl_set_unwrap(isl_map_domain(def_map));
752 map = isl_map_intersect(map, def_map);
757 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
758 __isl_take isl_space *dim, struct vars *v)
761 isl_pw_aff_list *list;
762 struct isl_token *tok = NULL;
764 pwaff = accept_affine(s, isl_space_copy(dim), v);
765 list = isl_pw_aff_list_from_pw_aff(pwaff);
770 tok = isl_stream_next_token(s);
772 isl_stream_error(s, NULL, "unexpected EOF");
775 if (tok->type != ',') {
776 isl_stream_push_token(s, tok);
781 pwaff = accept_affine(s, isl_space_copy(dim), v);
782 list = isl_pw_aff_list_concat(list,
783 isl_pw_aff_list_from_pw_aff(pwaff));
792 isl_pw_aff_list_free(list);
796 static __isl_give isl_map *read_defined_var_list(struct isl_stream *s,
797 struct vars *v, __isl_take isl_map *map, int rational)
799 struct isl_token *tok;
801 while ((tok = isl_stream_next_token(s)) != NULL) {
805 if (tok->type != ISL_TOKEN_IDENT)
808 p = vars_pos(v, tok->u.s, -1);
812 isl_stream_error(s, tok, "expecting unique identifier");
816 map = isl_map_add_dims(map, isl_dim_out, 1);
819 tok = isl_stream_next_token(s);
820 if (tok && tok->type == '=') {
822 map = read_var_def(s, map, isl_dim_out, v, rational);
823 tok = isl_stream_next_token(s);
826 if (!tok || tok->type != ',')
832 isl_stream_push_token(s, tok);
841 static int next_is_tuple(struct isl_stream *s)
843 struct isl_token *tok;
846 tok = isl_stream_next_token(s);
849 if (tok->type == '[') {
850 isl_stream_push_token(s, tok);
853 if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
854 isl_stream_push_token(s, tok);
858 is_tuple = isl_stream_next_token_is(s, '[');
860 isl_stream_push_token(s, tok);
865 /* Allocate an initial tuple with zero dimensions and an anonymous,
866 * unstructured space.
867 * A tuple is represented as an isl_multi_pw_aff.
868 * The range space is the space of the tuple.
869 * The domain space is an anonymous space
870 * with a dimension for each variable in the set of variables in "v".
871 * If a given dimension is not defined in terms of earlier dimensions in
872 * the input, then the corresponding isl_pw_aff is set equal to one time
873 * the variable corresponding to the dimension being defined.
875 static __isl_give isl_multi_pw_aff *tuple_alloc(struct vars *v)
877 return isl_multi_pw_aff_alloc(isl_space_alloc(v->ctx, 0, v->n, 0));
880 /* Is "pa" an expression in term of earlier dimensions?
881 * The alternative is that the dimension is defined to be equal to itself,
882 * meaning that it has a universe domain and an expression that depends
883 * on itself. "i" is the position of the expression in a sequence
884 * of "n" expressions. The final dimensions of "pa" correspond to
885 * these "n" expressions.
887 static int pw_aff_is_expr(__isl_keep isl_pw_aff *pa, int i, int n)
895 if (!isl_set_plain_is_universe(pa->p[0].set))
899 if (isl_int_is_zero(aff->v->el[aff->v->size - n + i]))
904 /* Does the tuple contain any dimensions that are defined
905 * in terms of earlier dimensions?
907 static int tuple_has_expr(__isl_keep isl_multi_pw_aff *tuple)
915 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
916 for (i = 0; i < n; ++i) {
917 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
918 has_expr = pw_aff_is_expr(pa, i, n);
920 if (has_expr < 0 || has_expr)
927 /* Add a dimension to the given tuple.
928 * The dimension is initially undefined, so it is encoded
929 * as one times itself.
931 static __isl_give isl_multi_pw_aff *tuple_add_dim(
932 __isl_take isl_multi_pw_aff *tuple, struct vars *v)
938 tuple = isl_multi_pw_aff_add_dims(tuple, isl_dim_in, 1);
939 space = isl_multi_pw_aff_get_domain_space(tuple);
940 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
941 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n, 1);
942 pa = isl_pw_aff_from_aff(aff);
943 tuple = isl_multi_pw_aff_flat_range_product(tuple,
944 isl_multi_pw_aff_from_pw_aff(pa));
949 /* Set the name of dimension "pos" in "tuple" to "name".
950 * During printing, we add primes if the same name appears more than once
951 * to distinguish the occurrences. Here, we remove those primes from "name"
952 * before setting the name of the dimension.
954 static __isl_give isl_multi_pw_aff *tuple_set_dim_name(
955 __isl_take isl_multi_pw_aff *tuple, int pos, char *name)
962 prime = strchr(name, '\'');
965 tuple = isl_multi_pw_aff_set_dim_name(tuple, isl_dim_set, pos, name);
972 /* Read an affine expression from "s" and replace the definition
973 * of dimension "pos" in "tuple" by this expression.
975 * accept_extended_affine requires a wrapped space as input.
976 * The domain space of "tuple", on the other hand is an anonymous space,
977 * so we have to adjust the space of the isl_pw_aff before adding it
980 static __isl_give isl_multi_pw_aff *read_tuple_var_def(struct isl_stream *s,
981 __isl_take isl_multi_pw_aff *tuple, int pos, struct vars *v,
987 space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
988 def = accept_extended_affine(s, space, v, rational);
989 space = isl_space_set_alloc(s->ctx, 0, v->n);
990 def = isl_pw_aff_reset_domain_space(def, space);
991 tuple = isl_multi_pw_aff_set_pw_aff(tuple, pos, def);
996 /* Read a list of variables and/or affine expressions and return the list
997 * as an isl_multi_pw_aff.
998 * The elements in the list are separated by either "," or "][".
999 * If "comma" is set then only "," is allowed.
1001 static __isl_give isl_multi_pw_aff *read_tuple_var_list(struct isl_stream *s,
1002 struct vars *v, int rational, int comma)
1005 struct isl_token *tok;
1006 isl_multi_pw_aff *res;
1008 res = tuple_alloc(v);
1010 if (isl_stream_next_token_is(s, ']'))
1013 while ((tok = next_token(s)) != NULL) {
1016 res = tuple_add_dim(res, v);
1018 if (tok->type == ISL_TOKEN_IDENT) {
1020 int p = vars_pos(v, tok->u.s, -1);
1026 if (tok->type == '*') {
1027 if (vars_add_anon(v) < 0)
1029 isl_token_free(tok);
1030 } else if (new_name) {
1031 res = tuple_set_dim_name(res, i, v->v->name);
1032 isl_token_free(tok);
1033 if (isl_stream_eat_if_available(s, '='))
1034 res = read_tuple_var_def(s, res, i, v,
1037 isl_stream_push_token(s, tok);
1039 if (vars_add_anon(v) < 0)
1041 res = read_tuple_var_def(s, res, i, v, rational);
1044 tok = isl_stream_next_token(s);
1045 if (!comma && tok && tok->type == ']' &&
1046 isl_stream_next_token_is(s, '[')) {
1047 isl_token_free(tok);
1048 tok = isl_stream_next_token(s);
1049 } else if (!tok || tok->type != ',')
1052 isl_token_free(tok);
1056 isl_stream_push_token(s, tok);
1060 isl_token_free(tok);
1061 return isl_multi_pw_aff_free(res);
1064 /* Read a tuple and represent it as an isl_multi_pw_aff. See tuple_alloc.
1066 static __isl_give isl_multi_pw_aff *read_tuple(struct isl_stream *s,
1067 struct vars *v, int rational, int comma)
1069 struct isl_token *tok;
1071 isl_multi_pw_aff *res = NULL;
1073 tok = isl_stream_next_token(s);
1076 if (tok->type == ISL_TOKEN_IDENT || tok->is_keyword) {
1077 name = strdup(tok->u.s);
1078 isl_token_free(tok);
1082 isl_stream_push_token(s, tok);
1083 if (isl_stream_eat(s, '['))
1085 if (next_is_tuple(s)) {
1086 isl_multi_pw_aff *out;
1088 res = read_tuple(s, v, rational, comma);
1089 if (isl_stream_eat(s, ISL_TOKEN_TO))
1091 out = read_tuple(s, v, rational, comma);
1092 n = isl_multi_pw_aff_dim(out, isl_dim_out);
1093 res = isl_multi_pw_aff_add_dims(res, isl_dim_in, n);
1094 res = isl_multi_pw_aff_range_product(res, out);
1096 res = read_tuple_var_list(s, v, rational, comma);
1097 if (isl_stream_eat(s, ']'))
1101 res = isl_multi_pw_aff_set_tuple_name(res, isl_dim_out, name);
1108 return isl_multi_pw_aff_free(res);
1111 /* Read a tuple from "s" and add it to "map".
1112 * The tuple is initially represented as an isl_multi_pw_aff.
1113 * We first create the appropriate space in "map" based on the range
1114 * space of this isl_multi_pw_aff. Then, we add equalities based
1115 * on the affine expressions. These live in an anonymous space,
1116 * however, so we first need to reset the space to that of "map".
1118 static __isl_give isl_map *read_map_tuple(struct isl_stream *s,
1119 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1120 int rational, int comma)
1123 isl_multi_pw_aff *tuple;
1124 isl_space *space = NULL;
1126 tuple = read_tuple(s, v, rational, comma);
1130 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
1131 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
1135 if (type == isl_dim_param) {
1136 if (isl_space_has_tuple_name(space, isl_dim_set) ||
1137 isl_space_is_wrapping(space)) {
1138 isl_die(s->ctx, isl_error_invalid,
1139 "parameter tuples cannot be named or nested",
1142 map = isl_map_add_dims(map, type, n);
1143 for (i = 0; i < n; ++i) {
1145 if (!isl_space_has_dim_name(space, isl_dim_set, i))
1146 isl_die(s->ctx, isl_error_invalid,
1147 "parameters must be named",
1149 id = isl_space_get_dim_id(space, isl_dim_set, i);
1150 map = isl_map_set_dim_id(map, isl_dim_param, i, id);
1152 } else if (type == isl_dim_in) {
1155 set = isl_set_universe(isl_space_copy(space));
1157 set = isl_set_set_rational(set);
1158 set = isl_set_intersect_params(set, isl_map_params(map));
1159 map = isl_map_from_domain(set);
1163 set = isl_set_universe(isl_space_copy(space));
1165 set = isl_set_set_rational(set);
1166 map = isl_map_from_domain_and_range(isl_map_domain(map), set);
1169 for (i = 0; i < n; ++i) {
1176 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1177 space = isl_pw_aff_get_domain_space(pa);
1178 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1179 aff = isl_aff_add_coefficient_si(aff,
1180 isl_dim_in, v->n - n + i, -1);
1181 pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
1183 pa = isl_pw_aff_set_rational(pa);
1184 set = isl_pw_aff_zero_set(pa);
1185 map_i = isl_map_from_range(set);
1186 map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
1187 map = isl_map_intersect(map, map_i);
1190 isl_space_free(space);
1191 isl_multi_pw_aff_free(tuple);
1194 isl_space_free(space);
1195 isl_multi_pw_aff_free(tuple);
1200 static __isl_give isl_set *construct_constraints(
1201 __isl_take isl_set *set, int type,
1202 __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right,
1207 left = isl_pw_aff_list_copy(left);
1208 right = isl_pw_aff_list_copy(right);
1210 left = isl_pw_aff_list_set_rational(left);
1211 right = isl_pw_aff_list_set_rational(right);
1213 if (type == ISL_TOKEN_LE)
1214 cond = isl_pw_aff_list_le_set(left, right);
1215 else if (type == ISL_TOKEN_GE)
1216 cond = isl_pw_aff_list_ge_set(left, right);
1217 else if (type == ISL_TOKEN_LT)
1218 cond = isl_pw_aff_list_lt_set(left, right);
1219 else if (type == ISL_TOKEN_GT)
1220 cond = isl_pw_aff_list_gt_set(left, right);
1221 else if (type == ISL_TOKEN_NE)
1222 cond = isl_pw_aff_list_ne_set(left, right);
1224 cond = isl_pw_aff_list_eq_set(left, right);
1226 return isl_set_intersect(set, cond);
1229 static __isl_give isl_map *add_constraint(struct isl_stream *s,
1230 struct vars *v, __isl_take isl_map *map, int rational)
1232 struct isl_token *tok = NULL;
1233 isl_pw_aff_list *list1 = NULL, *list2 = NULL;
1236 set = isl_map_wrap(map);
1237 list1 = accept_affine_list(s, isl_set_get_space(set), v);
1240 tok = isl_stream_next_token(s);
1241 if (!is_comparator(tok)) {
1242 isl_stream_error(s, tok, "missing operator");
1244 isl_stream_push_token(s, tok);
1249 list2 = accept_affine_list(s, isl_set_get_space(set), v);
1253 set = construct_constraints(set, tok->type, list1, list2,
1255 isl_token_free(tok);
1256 isl_pw_aff_list_free(list1);
1259 tok = isl_stream_next_token(s);
1260 if (!is_comparator(tok)) {
1262 isl_stream_push_token(s, tok);
1266 isl_pw_aff_list_free(list1);
1268 return isl_set_unwrap(set);
1271 isl_token_free(tok);
1272 isl_pw_aff_list_free(list1);
1273 isl_pw_aff_list_free(list2);
1278 static __isl_give isl_map *read_exists(struct isl_stream *s,
1279 struct vars *v, __isl_take isl_map *map, int rational)
1282 int seen_paren = isl_stream_eat_if_available(s, '(');
1284 map = isl_map_from_domain(isl_map_wrap(map));
1285 map = read_defined_var_list(s, v, map, rational);
1287 if (isl_stream_eat(s, ':'))
1290 map = read_disjuncts(s, v, map, rational);
1291 map = isl_set_unwrap(isl_map_domain(map));
1293 vars_drop(v, v->n - n);
1294 if (seen_paren && isl_stream_eat(s, ')'))
1303 /* Parse an expression between parentheses and push the result
1304 * back on the stream.
1306 * The parsed expression may be either an affine expression
1307 * or a condition. The first type is pushed onto the stream
1308 * as an isl_pw_aff, while the second is pushed as an isl_map.
1310 * If the initial token indicates the start of a condition,
1311 * we parse it as such.
1312 * Otherwise, we first parse an affine expression and push
1313 * that onto the stream. If the affine expression covers the
1314 * entire expression between parentheses, we return.
1315 * Otherwise, we assume that the affine expression is the
1316 * start of a condition and continue parsing.
1318 static int resolve_paren_expr(struct isl_stream *s,
1319 struct vars *v, __isl_take isl_map *map, int rational)
1321 struct isl_token *tok, *tok2;
1325 tok = isl_stream_next_token(s);
1326 if (!tok || tok->type != '(')
1329 if (isl_stream_next_token_is(s, '('))
1330 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1333 if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1334 isl_stream_next_token_is(s, ISL_TOKEN_NOT) ||
1335 isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
1336 isl_stream_next_token_is(s, ISL_TOKEN_FALSE) ||
1337 isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1338 map = read_disjuncts(s, v, map, rational);
1339 if (isl_stream_eat(s, ')'))
1341 tok->type = ISL_TOKEN_MAP;
1343 isl_stream_push_token(s, tok);
1347 tok2 = isl_stream_next_token(s);
1352 isl_stream_push_token(s, tok2);
1354 pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
1358 tok2 = isl_token_new(s->ctx, line, col, 0);
1361 tok2->type = ISL_TOKEN_AFF;
1362 tok2->u.pwaff = pwaff;
1364 if (isl_stream_eat_if_available(s, ')')) {
1365 isl_stream_push_token(s, tok2);
1366 isl_token_free(tok);
1371 isl_stream_push_token(s, tok2);
1373 map = read_disjuncts(s, v, map, rational);
1374 if (isl_stream_eat(s, ')'))
1377 tok->type = ISL_TOKEN_MAP;
1379 isl_stream_push_token(s, tok);
1383 isl_pw_aff_free(pwaff);
1385 isl_token_free(tok);
1390 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
1391 struct vars *v, __isl_take isl_map *map, int rational)
1393 if (isl_stream_next_token_is(s, '('))
1394 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1397 if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1398 struct isl_token *tok;
1399 tok = isl_stream_next_token(s);
1403 map = isl_map_copy(tok->u.map);
1404 isl_token_free(tok);
1408 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1409 return read_exists(s, v, map, rational);
1411 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1414 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1415 isl_space *dim = isl_map_get_space(map);
1417 return isl_map_empty(dim);
1420 return add_constraint(s, v, map, rational);
1426 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
1427 struct vars *v, __isl_take isl_map *map, int rational)
1432 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1433 res = read_conjunct(s, v, isl_map_copy(map), rational);
1435 res = isl_map_subtract(isl_map_copy(map), res);
1437 while (res && isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
1440 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1441 res_i = read_conjunct(s, v, isl_map_copy(map), rational);
1443 res = isl_map_subtract(res, res_i);
1445 res = isl_map_intersect(res, res_i);
1452 static struct isl_map *read_disjuncts(struct isl_stream *s,
1453 struct vars *v, __isl_take isl_map *map, int rational)
1457 if (isl_stream_next_token_is(s, '}')) {
1458 isl_space *dim = isl_map_get_space(map);
1460 return isl_map_universe(dim);
1463 res = read_conjuncts(s, v, isl_map_copy(map), rational);
1464 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1467 res_i = read_conjuncts(s, v, isl_map_copy(map), rational);
1468 res = isl_map_union(res, res_i);
1475 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1477 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1478 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1479 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1480 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1482 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1483 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1484 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1486 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1487 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1488 isl_basic_map_dim(bmap, isl_dim_in) +
1489 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1490 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1492 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1498 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1499 struct isl_stream *s, __isl_take isl_basic_map *bmap)
1502 struct isl_token *tok;
1512 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1513 dim = isl_basic_map_dim(bmap, isl_dim_out);
1515 tok = isl_stream_next_token(s);
1516 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1517 isl_stream_error(s, tok, "expecting coefficient");
1519 isl_stream_push_token(s, tok);
1522 if (!tok->on_new_line) {
1523 isl_stream_error(s, tok, "coefficient should appear on new line");
1524 isl_stream_push_token(s, tok);
1528 type = isl_int_get_si(tok->u.v);
1529 isl_token_free(tok);
1531 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1533 k = isl_basic_map_alloc_equality(bmap);
1536 k = isl_basic_map_alloc_inequality(bmap);
1542 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1544 tok = isl_stream_next_token(s);
1545 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1546 isl_stream_error(s, tok, "expecting coefficient");
1548 isl_stream_push_token(s, tok);
1551 if (tok->on_new_line) {
1552 isl_stream_error(s, tok,
1553 "coefficient should not appear on new line");
1554 isl_stream_push_token(s, tok);
1557 pos = polylib_pos_to_isl_pos(bmap, j);
1558 isl_int_set(c[pos], tok->u.v);
1559 isl_token_free(tok);
1564 isl_basic_map_free(bmap);
1568 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s)
1571 struct isl_token *tok;
1572 struct isl_token *tok2;
1575 unsigned in = 0, out, local = 0;
1576 struct isl_basic_map *bmap = NULL;
1579 tok = isl_stream_next_token(s);
1581 isl_stream_error(s, NULL, "unexpected EOF");
1584 tok2 = isl_stream_next_token(s);
1586 isl_token_free(tok);
1587 isl_stream_error(s, NULL, "unexpected EOF");
1590 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1591 isl_stream_push_token(s, tok2);
1592 isl_stream_push_token(s, tok);
1593 isl_stream_error(s, NULL,
1594 "expecting constraint matrix dimensions");
1597 n_row = isl_int_get_si(tok->u.v);
1598 n_col = isl_int_get_si(tok2->u.v);
1599 on_new_line = tok2->on_new_line;
1600 isl_token_free(tok2);
1601 isl_token_free(tok);
1602 isl_assert(s->ctx, !on_new_line, return NULL);
1603 isl_assert(s->ctx, n_row >= 0, return NULL);
1604 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1605 tok = isl_stream_next_token_on_same_line(s);
1607 if (tok->type != ISL_TOKEN_VALUE) {
1608 isl_stream_error(s, tok,
1609 "expecting number of output dimensions");
1610 isl_stream_push_token(s, tok);
1613 out = isl_int_get_si(tok->u.v);
1614 isl_token_free(tok);
1616 tok = isl_stream_next_token_on_same_line(s);
1617 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1618 isl_stream_error(s, tok,
1619 "expecting number of input dimensions");
1621 isl_stream_push_token(s, tok);
1624 in = isl_int_get_si(tok->u.v);
1625 isl_token_free(tok);
1627 tok = isl_stream_next_token_on_same_line(s);
1628 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1629 isl_stream_error(s, tok,
1630 "expecting number of existentials");
1632 isl_stream_push_token(s, tok);
1635 local = isl_int_get_si(tok->u.v);
1636 isl_token_free(tok);
1638 tok = isl_stream_next_token_on_same_line(s);
1639 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1640 isl_stream_error(s, tok,
1641 "expecting number of parameters");
1643 isl_stream_push_token(s, tok);
1646 nparam = isl_int_get_si(tok->u.v);
1647 isl_token_free(tok);
1648 if (n_col != 1 + out + in + local + nparam + 1) {
1649 isl_stream_error(s, NULL,
1650 "dimensions don't match");
1654 out = n_col - 2 - nparam;
1655 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1659 for (i = 0; i < local; ++i) {
1660 int k = isl_basic_map_alloc_div(bmap);
1663 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1666 for (i = 0; i < n_row; ++i)
1667 bmap = basic_map_read_polylib_constraint(s, bmap);
1669 tok = isl_stream_next_token_on_same_line(s);
1671 isl_stream_error(s, tok, "unexpected extra token on line");
1672 isl_stream_push_token(s, tok);
1676 bmap = isl_basic_map_simplify(bmap);
1677 bmap = isl_basic_map_finalize(bmap);
1680 isl_basic_map_free(bmap);
1684 static struct isl_map *map_read_polylib(struct isl_stream *s)
1686 struct isl_token *tok;
1687 struct isl_token *tok2;
1689 struct isl_map *map;
1691 tok = isl_stream_next_token(s);
1693 isl_stream_error(s, NULL, "unexpected EOF");
1696 tok2 = isl_stream_next_token_on_same_line(s);
1697 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1698 isl_stream_push_token(s, tok2);
1699 isl_stream_push_token(s, tok);
1700 return isl_map_from_basic_map(basic_map_read_polylib(s));
1703 isl_stream_error(s, tok2, "unexpected token");
1704 isl_stream_push_token(s, tok2);
1705 isl_stream_push_token(s, tok);
1708 n = isl_int_get_si(tok->u.v);
1709 isl_token_free(tok);
1711 isl_assert(s->ctx, n >= 1, return NULL);
1713 map = isl_map_from_basic_map(basic_map_read_polylib(s));
1715 for (i = 1; map && i < n; ++i)
1716 map = isl_map_union(map,
1717 isl_map_from_basic_map(basic_map_read_polylib(s)));
1722 static int optional_power(struct isl_stream *s)
1725 struct isl_token *tok;
1727 tok = isl_stream_next_token(s);
1730 if (tok->type != '^') {
1731 isl_stream_push_token(s, tok);
1734 isl_token_free(tok);
1735 tok = isl_stream_next_token(s);
1736 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1737 isl_stream_error(s, tok, "expecting exponent");
1739 isl_stream_push_token(s, tok);
1742 pow = isl_int_get_si(tok->u.v);
1743 isl_token_free(tok);
1747 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1748 __isl_keep isl_map *map, struct vars *v);
1750 static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
1751 __isl_keep isl_map *map, struct vars *v)
1753 isl_pw_qpolynomial *pwqp;
1754 struct isl_token *tok;
1756 tok = next_token(s);
1758 isl_stream_error(s, NULL, "unexpected EOF");
1761 if (tok->type == '(') {
1764 isl_token_free(tok);
1765 pwqp = read_term(s, map, v);
1768 if (isl_stream_eat(s, ')'))
1770 pow = optional_power(s);
1771 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1772 } else if (tok->type == ISL_TOKEN_VALUE) {
1773 struct isl_token *tok2;
1774 isl_qpolynomial *qp;
1776 tok2 = isl_stream_next_token(s);
1777 if (tok2 && tok2->type == '/') {
1778 isl_token_free(tok2);
1779 tok2 = next_token(s);
1780 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1781 isl_stream_error(s, tok2, "expected denominator");
1782 isl_token_free(tok);
1783 isl_token_free(tok2);
1786 qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
1787 tok->u.v, tok2->u.v);
1788 isl_token_free(tok2);
1790 isl_stream_push_token(s, tok2);
1791 qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
1794 isl_token_free(tok);
1795 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1796 } else if (tok->type == ISL_TOKEN_INFTY) {
1797 isl_qpolynomial *qp;
1798 isl_token_free(tok);
1799 qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
1800 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1801 } else if (tok->type == ISL_TOKEN_NAN) {
1802 isl_qpolynomial *qp;
1803 isl_token_free(tok);
1804 qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
1805 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1806 } else if (tok->type == ISL_TOKEN_IDENT) {
1808 int pos = vars_pos(v, tok->u.s, -1);
1810 isl_qpolynomial *qp;
1812 isl_token_free(tok);
1816 vars_drop(v, v->n - n);
1817 isl_stream_error(s, tok, "unknown identifier");
1818 isl_token_free(tok);
1821 isl_token_free(tok);
1822 pow = optional_power(s);
1823 qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
1824 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1825 } else if (tok->type == '[') {
1829 isl_stream_push_token(s, tok);
1830 pwaff = accept_div(s, isl_map_get_space(map), v);
1831 pow = optional_power(s);
1832 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
1833 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1834 } else if (tok->type == '-') {
1835 isl_token_free(tok);
1836 pwqp = read_factor(s, map, v);
1837 pwqp = isl_pw_qpolynomial_neg(pwqp);
1839 isl_stream_error(s, tok, "unexpected isl_token");
1840 isl_stream_push_token(s, tok);
1844 if (isl_stream_eat_if_available(s, '*') ||
1845 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1846 isl_pw_qpolynomial *pwqp2;
1848 pwqp2 = read_factor(s, map, v);
1849 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
1854 isl_pw_qpolynomial_free(pwqp);
1858 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1859 __isl_keep isl_map *map, struct vars *v)
1861 struct isl_token *tok;
1862 isl_pw_qpolynomial *pwqp;
1864 pwqp = read_factor(s, map, v);
1867 tok = next_token(s);
1871 if (tok->type == '+') {
1872 isl_pw_qpolynomial *pwqp2;
1874 isl_token_free(tok);
1875 pwqp2 = read_factor(s, map, v);
1876 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1877 } else if (tok->type == '-') {
1878 isl_pw_qpolynomial *pwqp2;
1880 isl_token_free(tok);
1881 pwqp2 = read_factor(s, map, v);
1882 pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
1883 } else if (tok->type == ISL_TOKEN_VALUE &&
1884 isl_int_is_neg(tok->u.v)) {
1885 isl_pw_qpolynomial *pwqp2;
1887 isl_stream_push_token(s, tok);
1888 pwqp2 = read_factor(s, map, v);
1889 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1891 isl_stream_push_token(s, tok);
1899 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1900 __isl_take isl_map *map, struct vars *v, int rational)
1902 struct isl_token *tok;
1904 tok = isl_stream_next_token(s);
1906 isl_stream_error(s, NULL, "unexpected EOF");
1909 if (tok->type == ':' ||
1910 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1911 isl_token_free(tok);
1912 map = read_disjuncts(s, v, map, rational);
1914 isl_stream_push_token(s, tok);
1922 static struct isl_obj obj_read_poly(struct isl_stream *s,
1923 __isl_take isl_map *map, struct vars *v, int n)
1925 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1926 isl_pw_qpolynomial *pwqp;
1927 struct isl_set *set;
1929 pwqp = read_term(s, map, v);
1930 map = read_optional_disjuncts(s, map, v, 0);
1931 set = isl_map_range(map);
1933 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
1935 vars_drop(v, v->n - n);
1941 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1942 __isl_take isl_set *set, struct vars *v, int n)
1944 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1945 isl_pw_qpolynomial *pwqp;
1946 isl_pw_qpolynomial_fold *pwf = NULL;
1948 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1949 return obj_read_poly(s, set, v, n);
1951 if (isl_stream_eat(s, '('))
1954 pwqp = read_term(s, set, v);
1955 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1957 while (isl_stream_eat_if_available(s, ',')) {
1958 isl_pw_qpolynomial_fold *pwf_i;
1959 pwqp = read_term(s, set, v);
1960 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1962 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1965 if (isl_stream_eat(s, ')'))
1968 set = read_optional_disjuncts(s, set, v, 0);
1969 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1971 vars_drop(v, v->n - n);
1977 isl_pw_qpolynomial_fold_free(pwf);
1978 obj.type = isl_obj_none;
1982 static int is_rational(struct isl_stream *s)
1984 struct isl_token *tok;
1986 tok = isl_stream_next_token(s);
1989 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1990 isl_token_free(tok);
1991 isl_stream_eat(s, ':');
1995 isl_stream_push_token(s, tok);
2000 static struct isl_obj obj_read_body(struct isl_stream *s,
2001 __isl_take isl_map *map, struct vars *v)
2003 struct isl_token *tok;
2004 struct isl_obj obj = { isl_obj_set, NULL };
2008 rational = is_rational(s);
2010 map = isl_map_set_rational(map);
2012 if (isl_stream_next_token_is(s, ':')) {
2013 obj.type = isl_obj_set;
2014 obj.v = read_optional_disjuncts(s, map, v, rational);
2018 if (!next_is_tuple(s))
2019 return obj_read_poly_or_fold(s, map, v, n);
2021 map = read_map_tuple(s, map, isl_dim_in, v, rational, 0);
2024 tok = isl_stream_next_token(s);
2027 if (tok->type == ISL_TOKEN_TO) {
2028 obj.type = isl_obj_map;
2029 isl_token_free(tok);
2030 if (!next_is_tuple(s)) {
2031 isl_set *set = isl_map_domain(map);
2032 return obj_read_poly_or_fold(s, set, v, n);
2034 map = read_map_tuple(s, map, isl_dim_out, v, rational, 0);
2038 map = isl_map_domain(map);
2039 isl_stream_push_token(s, tok);
2042 map = read_optional_disjuncts(s, map, v, rational);
2044 vars_drop(v, v->n - n);
2050 obj.type = isl_obj_none;
2054 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
2056 if (obj.type == isl_obj_map) {
2057 obj.v = isl_union_map_from_map(obj.v);
2058 obj.type = isl_obj_union_map;
2059 } else if (obj.type == isl_obj_set) {
2060 obj.v = isl_union_set_from_set(obj.v);
2061 obj.type = isl_obj_union_set;
2062 } else if (obj.type == isl_obj_pw_qpolynomial) {
2063 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
2064 obj.type = isl_obj_union_pw_qpolynomial;
2065 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
2066 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
2067 obj.type = isl_obj_union_pw_qpolynomial_fold;
2069 isl_assert(ctx, 0, goto error);
2072 obj.type->free(obj.v);
2073 obj.type = isl_obj_none;
2077 static struct isl_obj obj_add(struct isl_ctx *ctx,
2078 struct isl_obj obj1, struct isl_obj obj2)
2080 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
2081 obj1 = to_union(ctx, obj1);
2082 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
2083 obj2 = to_union(ctx, obj2);
2084 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
2085 obj1 = to_union(ctx, obj1);
2086 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
2087 obj2 = to_union(ctx, obj2);
2088 if (obj1.type == isl_obj_pw_qpolynomial &&
2089 obj2.type == isl_obj_union_pw_qpolynomial)
2090 obj1 = to_union(ctx, obj1);
2091 if (obj1.type == isl_obj_union_pw_qpolynomial &&
2092 obj2.type == isl_obj_pw_qpolynomial)
2093 obj2 = to_union(ctx, obj2);
2094 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2095 obj2.type == isl_obj_union_pw_qpolynomial_fold)
2096 obj1 = to_union(ctx, obj1);
2097 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
2098 obj2.type == isl_obj_pw_qpolynomial_fold)
2099 obj2 = to_union(ctx, obj2);
2100 isl_assert(ctx, obj1.type == obj2.type, goto error);
2101 if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
2102 obj1 = to_union(ctx, obj1);
2103 obj2 = to_union(ctx, obj2);
2105 if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
2106 obj1 = to_union(ctx, obj1);
2107 obj2 = to_union(ctx, obj2);
2109 if (obj1.type == isl_obj_pw_qpolynomial &&
2110 !isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)) {
2111 obj1 = to_union(ctx, obj1);
2112 obj2 = to_union(ctx, obj2);
2114 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2115 !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
2116 obj1 = to_union(ctx, obj1);
2117 obj2 = to_union(ctx, obj2);
2119 obj1.v = obj1.type->add(obj1.v, obj2.v);
2122 obj1.type->free(obj1.v);
2123 obj2.type->free(obj2.v);
2124 obj1.type = isl_obj_none;
2129 static struct isl_obj obj_read(struct isl_stream *s)
2131 isl_map *map = NULL;
2132 struct isl_token *tok;
2133 struct vars *v = NULL;
2134 struct isl_obj obj = { isl_obj_set, NULL };
2136 tok = next_token(s);
2138 isl_stream_error(s, NULL, "unexpected EOF");
2141 if (tok->type == ISL_TOKEN_VALUE) {
2142 struct isl_token *tok2;
2143 struct isl_map *map;
2145 tok2 = isl_stream_next_token(s);
2146 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
2147 isl_int_is_neg(tok2->u.v)) {
2149 isl_stream_push_token(s, tok2);
2150 obj.type = isl_obj_int;
2151 obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
2152 isl_token_free(tok);
2155 isl_stream_push_token(s, tok2);
2156 isl_stream_push_token(s, tok);
2157 map = map_read_polylib(s);
2160 if (isl_map_may_be_set(map))
2161 obj.v = isl_map_range(map);
2163 obj.type = isl_obj_map;
2168 v = vars_new(s->ctx);
2170 isl_stream_push_token(s, tok);
2173 map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
2174 if (tok->type == '[') {
2175 isl_stream_push_token(s, tok);
2176 map = read_map_tuple(s, map, isl_dim_param, v, 0, 0);
2179 tok = isl_stream_next_token(s);
2180 if (!tok || tok->type != ISL_TOKEN_TO) {
2181 isl_stream_error(s, tok, "expecting '->'");
2183 isl_stream_push_token(s, tok);
2186 isl_token_free(tok);
2187 tok = isl_stream_next_token(s);
2189 if (!tok || tok->type != '{') {
2190 isl_stream_error(s, tok, "expecting '{'");
2192 isl_stream_push_token(s, tok);
2195 isl_token_free(tok);
2197 tok = isl_stream_next_token(s);
2200 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
2201 isl_token_free(tok);
2202 if (isl_stream_eat(s, '='))
2204 map = read_map_tuple(s, map, isl_dim_param, v, 0, 1);
2207 } else if (tok->type == '}') {
2208 obj.type = isl_obj_union_set;
2209 obj.v = isl_union_set_empty(isl_map_get_space(map));
2210 isl_token_free(tok);
2213 isl_stream_push_token(s, tok);
2218 o = obj_read_body(s, isl_map_copy(map), v);
2219 if (o.type == isl_obj_none || !o.v)
2224 obj = obj_add(s->ctx, obj, o);
2225 if (obj.type == isl_obj_none || !obj.v)
2228 tok = isl_stream_next_token(s);
2229 if (!tok || tok->type != ';')
2231 isl_token_free(tok);
2232 if (isl_stream_next_token_is(s, '}')) {
2233 tok = isl_stream_next_token(s);
2238 if (tok && tok->type == '}') {
2239 isl_token_free(tok);
2241 isl_stream_error(s, tok, "unexpected isl_token");
2243 isl_token_free(tok);
2253 obj.type->free(obj.v);
2260 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
2265 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s)
2271 isl_assert(s->ctx, obj.type == isl_obj_map ||
2272 obj.type == isl_obj_set, goto error);
2274 if (obj.type == isl_obj_set)
2275 obj.v = isl_map_from_range(obj.v);
2279 obj.type->free(obj.v);
2283 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s)
2289 if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
2290 obj.v = isl_map_range(obj.v);
2291 obj.type = isl_obj_set;
2293 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2298 obj.type->free(obj.v);
2302 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
2307 if (obj.type == isl_obj_map) {
2308 obj.type = isl_obj_union_map;
2309 obj.v = isl_union_map_from_map(obj.v);
2311 if (obj.type == isl_obj_set) {
2312 obj.type = isl_obj_union_set;
2313 obj.v = isl_union_set_from_set(obj.v);
2315 if (obj.v && obj.type == isl_obj_union_set &&
2316 isl_union_set_is_empty(obj.v))
2317 obj.type = isl_obj_union_map;
2318 if (obj.v && obj.type != isl_obj_union_map)
2319 isl_die(s->ctx, isl_error_invalid, "invalid input", goto error);
2323 obj.type->free(obj.v);
2327 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
2332 if (obj.type == isl_obj_set) {
2333 obj.type = isl_obj_union_set;
2334 obj.v = isl_union_set_from_set(obj.v);
2337 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2341 obj.type->free(obj.v);
2345 static __isl_give isl_basic_map *basic_map_read(struct isl_stream *s)
2348 struct isl_map *map;
2349 struct isl_basic_map *bmap;
2356 isl_assert(map->ctx, map->n <= 1, goto error);
2359 bmap = isl_basic_map_empty_like_map(map);
2361 bmap = isl_basic_map_copy(map->p[0]);
2371 static __isl_give isl_basic_set *basic_set_read(struct isl_stream *s)
2373 isl_basic_map *bmap;
2374 bmap = basic_map_read(s);
2377 if (!isl_basic_map_may_be_set(bmap))
2378 isl_die(s->ctx, isl_error_invalid,
2379 "input is not a set", goto error);
2380 return isl_basic_map_range(bmap);
2382 isl_basic_map_free(bmap);
2386 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2389 struct isl_basic_map *bmap;
2390 struct isl_stream *s = isl_stream_new_file(ctx, input);
2393 bmap = basic_map_read(s);
2398 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2401 isl_basic_set *bset;
2402 struct isl_stream *s = isl_stream_new_file(ctx, input);
2405 bset = basic_set_read(s);
2410 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2413 struct isl_basic_map *bmap;
2414 struct isl_stream *s = isl_stream_new_str(ctx, str);
2417 bmap = basic_map_read(s);
2422 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2425 isl_basic_set *bset;
2426 struct isl_stream *s = isl_stream_new_str(ctx, str);
2429 bset = basic_set_read(s);
2434 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2437 struct isl_map *map;
2438 struct isl_stream *s = isl_stream_new_file(ctx, input);
2441 map = isl_stream_read_map(s);
2446 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2449 struct isl_map *map;
2450 struct isl_stream *s = isl_stream_new_str(ctx, str);
2453 map = isl_stream_read_map(s);
2458 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2462 struct isl_stream *s = isl_stream_new_file(ctx, input);
2465 set = isl_stream_read_set(s);
2470 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2474 struct isl_stream *s = isl_stream_new_str(ctx, str);
2477 set = isl_stream_read_set(s);
2482 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2485 isl_union_map *umap;
2486 struct isl_stream *s = isl_stream_new_file(ctx, input);
2489 umap = isl_stream_read_union_map(s);
2494 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2497 isl_union_map *umap;
2498 struct isl_stream *s = isl_stream_new_str(ctx, str);
2501 umap = isl_stream_read_union_map(s);
2506 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2509 isl_union_set *uset;
2510 struct isl_stream *s = isl_stream_new_file(ctx, input);
2513 uset = isl_stream_read_union_set(s);
2518 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2521 isl_union_set *uset;
2522 struct isl_stream *s = isl_stream_new_str(ctx, str);
2525 uset = isl_stream_read_union_set(s);
2530 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2532 struct isl_vec *vec = NULL;
2533 struct isl_token *tok;
2537 tok = isl_stream_next_token(s);
2538 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2539 isl_stream_error(s, tok, "expecting vector length");
2543 size = isl_int_get_si(tok->u.v);
2544 isl_token_free(tok);
2546 vec = isl_vec_alloc(s->ctx, size);
2548 for (j = 0; j < size; ++j) {
2549 tok = isl_stream_next_token(s);
2550 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2551 isl_stream_error(s, tok, "expecting constant value");
2554 isl_int_set(vec->el[j], tok->u.v);
2555 isl_token_free(tok);
2560 isl_token_free(tok);
2565 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2567 return isl_vec_read_polylib(s);
2570 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2573 struct isl_stream *s = isl_stream_new_file(ctx, input);
2581 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2582 struct isl_stream *s)
2588 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2593 obj.type->free(obj.v);
2597 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2600 isl_pw_qpolynomial *pwqp;
2601 struct isl_stream *s = isl_stream_new_str(ctx, str);
2604 pwqp = isl_stream_read_pw_qpolynomial(s);
2609 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2612 isl_pw_qpolynomial *pwqp;
2613 struct isl_stream *s = isl_stream_new_file(ctx, input);
2616 pwqp = isl_stream_read_pw_qpolynomial(s);
2621 /* Is the next token an identifer not in "v"?
2623 static int next_is_fresh_ident(struct isl_stream *s, struct vars *v)
2627 struct isl_token *tok;
2629 tok = isl_stream_next_token(s);
2632 fresh = tok->type == ISL_TOKEN_IDENT && vars_pos(v, tok->u.s, -1) >= n;
2633 isl_stream_push_token(s, tok);
2635 vars_drop(v, v->n - n);
2640 /* First read the domain of the affine expression, which may be
2641 * a parameter space or a set.
2642 * The tricky part is that we don't know if the domain is a set or not,
2643 * so when we are trying to read the domain, we may actually be reading
2644 * the affine expression itself (defined on a parameter domains)
2645 * If the tuple we are reading is named, we assume it's the domain.
2646 * Also, if inside the tuple, the first thing we find is a nested tuple
2647 * or a new identifier, we again assume it's the domain.
2648 * Otherwise, we assume we are reading an affine expression.
2650 static __isl_give isl_set *read_aff_domain(struct isl_stream *s,
2651 __isl_take isl_set *dom, struct vars *v)
2653 struct isl_token *tok;
2655 tok = isl_stream_next_token(s);
2656 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
2657 isl_stream_push_token(s, tok);
2658 return read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
2660 if (!tok || tok->type != '[') {
2661 isl_stream_error(s, tok, "expecting '['");
2664 if (next_is_tuple(s) || next_is_fresh_ident(s, v)) {
2665 isl_stream_push_token(s, tok);
2666 dom = read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
2668 isl_stream_push_token(s, tok);
2673 isl_stream_push_token(s, tok);
2678 /* Read an affine expression from "s".
2680 __isl_give isl_aff *isl_stream_read_aff(struct isl_stream *s)
2685 ma = isl_stream_read_multi_aff(s);
2688 if (isl_multi_aff_dim(ma, isl_dim_out) != 1)
2689 isl_die(s->ctx, isl_error_invalid,
2690 "expecting single affine expression",
2693 aff = isl_multi_aff_get_aff(ma, 0);
2694 isl_multi_aff_free(ma);
2697 isl_multi_aff_free(ma);
2701 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2703 static __isl_give isl_pw_aff *read_pw_aff_with_dom(struct isl_stream *s,
2704 __isl_take isl_set *dom, struct vars *v)
2706 isl_pw_aff *pwaff = NULL;
2708 if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
2711 if (isl_stream_eat(s, '['))
2714 pwaff = accept_affine(s, isl_set_get_space(dom), v);
2716 if (isl_stream_eat(s, ']'))
2719 dom = read_optional_disjuncts(s, dom, v, 0);
2720 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2725 isl_pw_aff_free(pwaff);
2729 __isl_give isl_pw_aff *isl_stream_read_pw_aff(struct isl_stream *s)
2732 isl_set *dom = NULL;
2734 isl_pw_aff *pa = NULL;
2737 v = vars_new(s->ctx);
2741 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
2742 if (next_is_tuple(s)) {
2743 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
2744 if (isl_stream_eat(s, ISL_TOKEN_TO))
2747 if (isl_stream_eat(s, '{'))
2751 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
2752 pa = read_pw_aff_with_dom(s, aff_dom, v);
2753 vars_drop(v, v->n - n);
2755 while (isl_stream_eat_if_available(s, ';')) {
2759 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
2760 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
2761 vars_drop(v, v->n - n);
2763 pa = isl_pw_aff_union_add(pa, pa_i);
2766 if (isl_stream_eat(s, '}'))
2775 isl_pw_aff_free(pa);
2779 __isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
2782 struct isl_stream *s = isl_stream_new_str(ctx, str);
2785 aff = isl_stream_read_aff(s);
2790 __isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
2793 struct isl_stream *s = isl_stream_new_str(ctx, str);
2796 pa = isl_stream_read_pw_aff(s);
2801 /* Read an isl_pw_multi_aff from "s".
2802 * We currently read a generic object and if it turns out to be a set or
2803 * a map, we convert that to an isl_pw_multi_aff.
2804 * It would be more efficient if we were to construct the isl_pw_multi_aff
2807 __isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(struct isl_stream *s)
2815 if (obj.type == isl_obj_map)
2816 return isl_pw_multi_aff_from_map(obj.v);
2817 if (obj.type == isl_obj_set)
2818 return isl_pw_multi_aff_from_set(obj.v);
2820 obj.type->free(obj.v);
2821 isl_die(s->ctx, isl_error_invalid, "unexpected object type",
2825 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
2828 isl_pw_multi_aff *pma;
2829 struct isl_stream *s = isl_stream_new_str(ctx, str);
2832 pma = isl_stream_read_pw_multi_aff(s);
2837 /* Read an isl_union_pw_multi_aff from "s".
2838 * We currently read a generic object and if it turns out to be a set or
2839 * a map, we convert that to an isl_union_pw_multi_aff.
2840 * It would be more efficient if we were to construct
2841 * the isl_union_pw_multi_aff directly.
2843 __isl_give isl_union_pw_multi_aff *isl_stream_read_union_pw_multi_aff(
2844 struct isl_stream *s)
2852 if (obj.type == isl_obj_map || obj.type == isl_obj_set)
2853 obj = to_union(s->ctx, obj);
2854 if (obj.type == isl_obj_union_map)
2855 return isl_union_pw_multi_aff_from_union_map(obj.v);
2856 if (obj.type == isl_obj_union_set)
2857 return isl_union_pw_multi_aff_from_union_set(obj.v);
2859 obj.type->free(obj.v);
2860 isl_die(s->ctx, isl_error_invalid, "unexpected object type",
2864 /* Read an isl_union_pw_multi_aff from "str".
2866 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_read_from_str(
2867 isl_ctx *ctx, const char *str)
2869 isl_union_pw_multi_aff *upma;
2870 struct isl_stream *s = isl_stream_new_str(ctx, str);
2873 upma = isl_stream_read_union_pw_multi_aff(s);
2878 /* Assuming "pa" represents a single affine expression defined on a universe
2879 * domain, extract this affine expression.
2881 static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
2888 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2889 "expecting single affine expression",
2891 if (!isl_set_plain_is_universe(pa->p[0].set))
2892 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2893 "expecting universe domain",
2896 aff = isl_aff_copy(pa->p[0].aff);
2897 isl_pw_aff_free(pa);
2900 isl_pw_aff_free(pa);
2904 /* Read a multi-affine expression from "s".
2905 * If the multi-affine expression has a domain, then then tuple
2906 * representing this domain cannot involve any affine expressions.
2907 * The tuple representing the actual expressions needs to consist
2908 * of only affine expressions. Moreover, these expressions can
2909 * only depend on parameters and input dimensions and not on other
2910 * output dimensions.
2912 __isl_give isl_multi_aff *isl_stream_read_multi_aff(struct isl_stream *s)
2915 isl_set *dom = NULL;
2916 isl_multi_pw_aff *tuple = NULL;
2918 isl_space *space, *dom_space;
2919 isl_multi_aff *ma = NULL;
2921 v = vars_new(s->ctx);
2925 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
2926 if (next_is_tuple(s)) {
2927 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
2928 if (isl_stream_eat(s, ISL_TOKEN_TO))
2931 if (!isl_set_plain_is_universe(dom))
2932 isl_die(s->ctx, isl_error_invalid,
2933 "expecting universe parameter domain", goto error);
2934 if (isl_stream_eat(s, '{'))
2937 tuple = read_tuple(s, v, 0, 0);
2940 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
2945 has_expr = tuple_has_expr(tuple);
2949 isl_die(s->ctx, isl_error_invalid,
2950 "expecting universe domain", goto error);
2951 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
2952 set = isl_set_universe(space);
2953 dom = isl_set_intersect_params(set, dom);
2954 isl_multi_pw_aff_free(tuple);
2955 tuple = read_tuple(s, v, 0, 0);
2960 if (isl_stream_eat(s, '}'))
2963 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
2964 dim = isl_set_dim(dom, isl_dim_all);
2965 dom_space = isl_set_get_space(dom);
2966 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
2967 space = isl_space_align_params(space, isl_space_copy(dom_space));
2968 if (!isl_space_is_params(dom_space))
2969 space = isl_space_map_from_domain_and_range(
2970 isl_space_copy(dom_space), space);
2971 isl_space_free(dom_space);
2972 ma = isl_multi_aff_alloc(space);
2974 for (i = 0; i < n; ++i) {
2977 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
2978 aff = aff_from_pw_aff(pa);
2981 if (isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)) {
2983 isl_die(s->ctx, isl_error_invalid,
2984 "not an affine expression", goto error);
2986 aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
2987 space = isl_multi_aff_get_domain_space(ma);
2988 aff = isl_aff_reset_domain_space(aff, space);
2989 ma = isl_multi_aff_set_aff(ma, i, aff);
2992 isl_multi_pw_aff_free(tuple);
2997 isl_multi_pw_aff_free(tuple);
3000 isl_multi_aff_free(ma);
3004 __isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
3007 isl_multi_aff *maff;
3008 struct isl_stream *s = isl_stream_new_str(ctx, str);
3011 maff = isl_stream_read_multi_aff(s);
3016 __isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
3017 struct isl_stream *s)
3022 if (obj.type == isl_obj_pw_qpolynomial) {
3023 obj.type = isl_obj_union_pw_qpolynomial;
3024 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
3027 isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
3032 obj.type->free(obj.v);
3036 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
3037 isl_ctx *ctx, const char *str)
3039 isl_union_pw_qpolynomial *upwqp;
3040 struct isl_stream *s = isl_stream_new_str(ctx, str);
3043 upwqp = isl_stream_read_union_pw_qpolynomial(s);