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>
33 struct variable *next;
42 static struct vars *vars_new(struct isl_ctx *ctx)
45 v = isl_alloc_type(ctx, struct vars);
54 static void variable_free(struct variable *var)
57 struct variable *next = var->next;
64 static void vars_free(struct vars *v)
72 static void vars_drop(struct vars *v, int n)
83 struct variable *next = var->next;
91 static struct variable *variable_new(struct vars *v, const char *name, int len,
95 var = isl_calloc_type(v->ctx, struct variable);
98 var->name = strdup(name);
99 var->name[len] = '\0';
108 static int vars_pos(struct vars *v, const char *s, int len)
115 for (q = v->v; q; q = q->next) {
116 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
123 v->v = variable_new(v, s, len, v->n);
131 static int vars_add_anon(struct vars *v)
133 v->v = variable_new(v, "", 0, v->n);
142 /* Obtain next token, with some preprocessing.
143 * In particular, evaluate expressions of the form x^y,
144 * with x and y values.
146 static struct isl_token *next_token(struct isl_stream *s)
148 struct isl_token *tok, *tok2;
150 tok = isl_stream_next_token(s);
151 if (!tok || tok->type != ISL_TOKEN_VALUE)
153 if (!isl_stream_eat_if_available(s, '^'))
155 tok2 = isl_stream_next_token(s);
156 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
157 isl_stream_error(s, tok2, "expecting constant value");
161 isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
163 isl_token_free(tok2);
167 isl_token_free(tok2);
171 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
173 struct isl_token *tok;
176 if (!tok || tok->type != ISL_TOKEN_VALUE) {
177 isl_stream_error(s, tok, "expecting constant value");
181 isl_int_mul(*f, *f, tok->u.v);
185 if (isl_stream_eat_if_available(s, '*'))
186 return accept_cst_factor(s, f);
194 /* Given an affine expression aff, return an affine expression
195 * for aff % d, with d the next token on the stream, which is
196 * assumed to be a constant.
198 * We introduce an integer division q = [aff/d] and the result
199 * is set to aff - d q.
201 static __isl_give isl_pw_aff *affine_mod(struct isl_stream *s,
202 struct vars *v, __isl_take isl_pw_aff *aff)
204 struct isl_token *tok;
208 if (!tok || tok->type != ISL_TOKEN_VALUE) {
209 isl_stream_error(s, tok, "expecting constant value");
213 q = isl_pw_aff_copy(aff);
214 q = isl_pw_aff_scale_down(q, tok->u.v);
215 q = isl_pw_aff_floor(q);
216 q = isl_pw_aff_scale(q, tok->u.v);
218 aff = isl_pw_aff_sub(aff, q);
223 isl_pw_aff_free(aff);
228 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
229 __isl_take isl_space *dim, struct vars *v);
230 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
231 __isl_take isl_space *dim, struct vars *v);
233 static __isl_give isl_pw_aff *accept_minmax(struct isl_stream *s,
234 __isl_take isl_space *dim, struct vars *v)
236 struct isl_token *tok;
237 isl_pw_aff_list *list = NULL;
240 tok = isl_stream_next_token(s);
243 min = tok->type == ISL_TOKEN_MIN;
246 if (isl_stream_eat(s, '('))
249 list = accept_affine_list(s, isl_space_copy(dim), v);
253 if (isl_stream_eat(s, ')'))
257 return min ? isl_pw_aff_list_min(list) : isl_pw_aff_list_max(list);
260 isl_pw_aff_list_free(list);
264 static __isl_give isl_pw_aff *accept_div(struct isl_stream *s,
265 __isl_take isl_space *dim, struct vars *v)
267 struct isl_token *tok;
270 isl_pw_aff *pwaff = NULL;
272 if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOORD))
274 else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEILD))
277 if (isl_stream_eat(s, '('))
280 if (isl_stream_eat(s, '['))
284 pwaff = accept_affine(s, isl_space_copy(dim), v);
287 if (isl_stream_eat(s, ','))
293 if (tok->type != ISL_TOKEN_VALUE) {
294 isl_stream_error(s, tok, "expected denominator");
295 isl_stream_push_token(s, tok);
298 isl_pw_aff_scale_down(pwaff, tok->u.v);
303 pwaff = isl_pw_aff_ceil(pwaff);
305 pwaff = isl_pw_aff_floor(pwaff);
308 if (isl_stream_eat(s, ')'))
311 if (isl_stream_eat(s, ']'))
319 isl_pw_aff_free(pwaff);
323 static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
324 __isl_take isl_space *dim, struct vars *v)
326 struct isl_token *tok = NULL;
327 isl_pw_aff *res = NULL;
331 isl_stream_error(s, NULL, "unexpected EOF");
335 if (tok->type == ISL_TOKEN_AFF) {
336 res = isl_pw_aff_copy(tok->u.pwaff);
338 } else if (tok->type == ISL_TOKEN_IDENT) {
340 int pos = vars_pos(v, tok->u.s, -1);
346 vars_drop(v, v->n - n);
347 isl_stream_error(s, tok, "unknown identifier");
351 aff = isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim)));
354 isl_int_set_si(aff->v->el[2 + pos], 1);
355 res = isl_pw_aff_from_aff(aff);
357 } else if (tok->type == ISL_TOKEN_VALUE) {
358 if (isl_stream_eat_if_available(s, '*')) {
359 res = accept_affine_factor(s, isl_space_copy(dim), v);
360 res = isl_pw_aff_scale(res, tok->u.v);
364 ls = isl_local_space_from_space(isl_space_copy(dim));
365 aff = isl_aff_zero_on_domain(ls);
366 aff = isl_aff_add_constant(aff, tok->u.v);
367 res = isl_pw_aff_from_aff(aff);
370 } else if (tok->type == '(') {
373 res = accept_affine(s, isl_space_copy(dim), v);
376 if (isl_stream_eat(s, ')'))
378 } else if (tok->type == '[' ||
379 tok->type == ISL_TOKEN_FLOORD ||
380 tok->type == ISL_TOKEN_CEILD) {
381 isl_stream_push_token(s, tok);
383 res = accept_div(s, isl_space_copy(dim), v);
384 } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
385 isl_stream_push_token(s, tok);
387 res = accept_minmax(s, isl_space_copy(dim), v);
389 isl_stream_error(s, tok, "expecting factor");
392 if (isl_stream_eat_if_available(s, '%') ||
393 isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
395 return affine_mod(s, v, res);
397 if (isl_stream_eat_if_available(s, '*')) {
400 isl_int_set_si(f, 1);
401 if (accept_cst_factor(s, &f) < 0) {
405 res = isl_pw_aff_scale(res, f);
408 if (isl_stream_eat_if_available(s, '/')) {
411 isl_int_set_si(f, 1);
412 if (accept_cst_factor(s, &f) < 0) {
416 res = isl_pw_aff_scale_down(res, f);
425 isl_pw_aff_free(res);
430 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
435 space = isl_pw_aff_get_domain_space(pwaff);
436 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
437 aff = isl_aff_add_constant(aff, v);
439 return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
442 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
443 __isl_take isl_space *dim, struct vars *v)
445 struct isl_token *tok = NULL;
450 ls = isl_local_space_from_space(isl_space_copy(dim));
451 res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
458 isl_stream_error(s, NULL, "unexpected EOF");
461 if (tok->type == '-') {
466 if (tok->type == '(' || tok->type == '[' ||
467 tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
468 tok->type == ISL_TOKEN_FLOORD ||
469 tok->type == ISL_TOKEN_CEILD ||
470 tok->type == ISL_TOKEN_IDENT ||
471 tok->type == ISL_TOKEN_AFF) {
473 isl_stream_push_token(s, tok);
475 term = accept_affine_factor(s, isl_space_copy(dim), v);
477 res = isl_pw_aff_sub(res, term);
479 res = isl_pw_aff_add(res, term);
483 } else if (tok->type == ISL_TOKEN_VALUE) {
485 isl_int_neg(tok->u.v, tok->u.v);
486 if (isl_stream_eat_if_available(s, '*') ||
487 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
489 term = accept_affine_factor(s,
490 isl_space_copy(dim), v);
491 term = isl_pw_aff_scale(term, tok->u.v);
492 res = isl_pw_aff_add(res, term);
496 res = add_cst(res, tok->u.v);
500 isl_stream_error(s, tok, "unexpected isl_token");
501 isl_stream_push_token(s, tok);
502 isl_pw_aff_free(res);
509 if (tok && tok->type == '-') {
512 } else if (tok && tok->type == '+') {
515 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
516 isl_int_is_neg(tok->u.v)) {
517 isl_stream_push_token(s, tok);
520 isl_stream_push_token(s, tok);
530 isl_pw_aff_free(res);
534 static int is_comparator(struct isl_token *tok)
552 static struct isl_map *read_disjuncts(struct isl_stream *s,
553 struct vars *v, __isl_take isl_map *map, int rational);
554 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
555 __isl_take isl_space *dim, struct vars *v, int rational);
557 /* Accept a ternary operator, given the first argument.
559 static __isl_give isl_pw_aff *accept_ternary(struct isl_stream *s,
560 __isl_take isl_map *cond, struct vars *v, int rational)
563 isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL, *pa_cond;
568 if (isl_stream_eat(s, '?'))
571 dim = isl_space_wrap(isl_map_get_space(cond));
572 pwaff1 = accept_extended_affine(s, dim, v, rational);
576 if (isl_stream_eat(s, ':'))
579 dim = isl_pw_aff_get_domain_space(pwaff1);
580 pwaff2 = accept_extended_affine(s, dim, v, rational);
584 pa_cond = isl_set_indicator_function(isl_map_wrap(cond));
585 return isl_pw_aff_cond(pa_cond, pwaff1, pwaff2);
588 isl_pw_aff_free(pwaff1);
589 isl_pw_aff_free(pwaff2);
593 /* Accept an affine expression that may involve ternary operators.
594 * We first read an affine expression.
595 * If it is not followed by a comparison operator, we simply return it.
596 * Otherwise, we assume the affine epxression is part of the first
597 * argument of a ternary operator and try to parse that.
599 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
600 __isl_take isl_space *dim, struct vars *v, int rational)
605 struct isl_token *tok;
606 int line = -1, col = -1;
609 tok = isl_stream_next_token(s);
613 isl_stream_push_token(s, tok);
616 pwaff = accept_affine(s, dim, v);
618 pwaff = isl_pw_aff_set_rational(pwaff);
622 tok = isl_stream_next_token(s);
624 return isl_pw_aff_free(pwaff);
626 is_comp = is_comparator(tok);
627 isl_stream_push_token(s, tok);
631 tok = isl_token_new(s->ctx, line, col, 0);
633 return isl_pw_aff_free(pwaff);
634 tok->type = ISL_TOKEN_AFF;
635 tok->u.pwaff = pwaff;
637 space = isl_pw_aff_get_domain_space(pwaff);
638 cond = isl_map_universe(isl_space_unwrap(space));
640 isl_stream_push_token(s, tok);
642 cond = read_disjuncts(s, v, cond, rational);
644 return accept_ternary(s, cond, v, rational);
647 static __isl_give isl_map *read_var_def(struct isl_stream *s,
648 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
655 if (type == isl_dim_param)
656 pos = isl_map_dim(map, isl_dim_param);
658 pos = isl_map_dim(map, isl_dim_in);
659 if (type == isl_dim_out)
660 pos += isl_map_dim(map, isl_dim_out);
665 def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)),
667 def_map = isl_map_from_pw_aff(def);
668 def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
669 def_map = isl_set_unwrap(isl_map_domain(def_map));
671 map = isl_map_intersect(map, def_map);
676 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
677 __isl_take isl_space *dim, struct vars *v)
680 isl_pw_aff_list *list;
681 struct isl_token *tok = NULL;
683 pwaff = accept_affine(s, isl_space_copy(dim), v);
684 list = isl_pw_aff_list_from_pw_aff(pwaff);
689 tok = isl_stream_next_token(s);
691 isl_stream_error(s, NULL, "unexpected EOF");
694 if (tok->type != ',') {
695 isl_stream_push_token(s, tok);
700 pwaff = accept_affine(s, isl_space_copy(dim), v);
701 list = isl_pw_aff_list_concat(list,
702 isl_pw_aff_list_from_pw_aff(pwaff));
711 isl_pw_aff_list_free(list);
715 static __isl_give isl_map *read_defined_var_list(struct isl_stream *s,
716 struct vars *v, __isl_take isl_map *map, int rational)
718 struct isl_token *tok;
720 while ((tok = isl_stream_next_token(s)) != NULL) {
724 if (tok->type != ISL_TOKEN_IDENT)
727 p = vars_pos(v, tok->u.s, -1);
731 isl_stream_error(s, tok, "expecting unique identifier");
735 map = isl_map_add_dims(map, isl_dim_out, 1);
738 tok = isl_stream_next_token(s);
739 if (tok && tok->type == '=') {
741 map = read_var_def(s, map, isl_dim_out, v, rational);
742 tok = isl_stream_next_token(s);
745 if (!tok || tok->type != ',')
751 isl_stream_push_token(s, tok);
760 static int next_is_tuple(struct isl_stream *s)
762 struct isl_token *tok;
765 tok = isl_stream_next_token(s);
768 if (tok->type == '[') {
769 isl_stream_push_token(s, tok);
772 if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
773 isl_stream_push_token(s, tok);
777 is_tuple = isl_stream_next_token_is(s, '[');
779 isl_stream_push_token(s, tok);
784 /* Allocate an initial tuple with zero dimensions and an anonymous,
785 * unstructured space.
786 * A tuple is represented as an isl_multi_pw_aff.
787 * The range space is the space of the tuple.
788 * The domain space is an anonymous space
789 * with a dimension for each variable in the set of variables in "v".
790 * If a given dimension is not defined in terms of earlier dimensions in
791 * the input, then the corresponding isl_pw_aff is set equal to one time
792 * the variable corresponding to the dimension being defined.
794 static __isl_give isl_multi_pw_aff *tuple_alloc(struct vars *v)
796 return isl_multi_pw_aff_alloc(isl_space_alloc(v->ctx, 0, v->n, 0));
799 /* Is "pa" an expression in term of earlier dimensions?
800 * The alternative is that the dimension is defined to be equal to itself,
801 * meaning that it has a universe domain and an expression that depends
802 * on itself. "i" is the position of the expression in a sequence
803 * of "n" expressions. The final dimensions of "pa" correspond to
804 * these "n" expressions.
806 static int pw_aff_is_expr(__isl_keep isl_pw_aff *pa, int i, int n)
814 if (!isl_set_plain_is_universe(pa->p[0].set))
818 if (isl_int_is_zero(aff->v->el[aff->v->size - n + i]))
823 /* Does the tuple contain any dimensions that are defined
824 * in terms of earlier dimensions?
826 static int tuple_has_expr(__isl_keep isl_multi_pw_aff *tuple)
834 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
835 for (i = 0; i < n; ++i) {
836 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
837 has_expr = pw_aff_is_expr(pa, i, n);
839 if (has_expr < 0 || has_expr)
846 /* Add a dimension to the given tuple.
847 * The dimension is initially undefined, so it is encoded
848 * as one times itself.
850 static __isl_give isl_multi_pw_aff *tuple_add_dim(
851 __isl_take isl_multi_pw_aff *tuple, struct vars *v)
857 tuple = isl_multi_pw_aff_add_dims(tuple, isl_dim_in, 1);
858 space = isl_multi_pw_aff_get_domain_space(tuple);
859 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
860 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n, 1);
861 pa = isl_pw_aff_from_aff(aff);
862 tuple = isl_multi_pw_aff_flat_range_product(tuple,
863 isl_multi_pw_aff_from_pw_aff(pa));
868 /* Set the name of dimension "pos" in "tuple" to "name".
869 * During printing, we add primes if the same name appears more than once
870 * to distinguish the occurrences. Here, we remove those primes from "name"
871 * before setting the name of the dimension.
873 static __isl_give isl_multi_pw_aff *tuple_set_dim_name(
874 __isl_take isl_multi_pw_aff *tuple, int pos, char *name)
881 prime = strchr(name, '\'');
884 tuple = isl_multi_pw_aff_set_dim_name(tuple, isl_dim_set, pos, name);
891 /* Read an affine expression from "s" and replace the definition
892 * of dimension "pos" in "tuple" by this expression.
894 * accept_extended_affine requires a wrapped space as input.
895 * The domain space of "tuple", on the other hand is an anonymous space,
896 * so we have to adjust the space of the isl_pw_aff before adding it
899 static __isl_give isl_multi_pw_aff *read_tuple_var_def(struct isl_stream *s,
900 __isl_take isl_multi_pw_aff *tuple, int pos, struct vars *v,
906 space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
907 def = accept_extended_affine(s, space, v, rational);
908 space = isl_space_set_alloc(s->ctx, 0, v->n);
909 def = isl_pw_aff_reset_domain_space(def, space);
910 tuple = isl_multi_pw_aff_set_pw_aff(tuple, pos, def);
915 /* Read a list of variables and/or affine expressions and return the list
916 * as an isl_multi_pw_aff.
917 * The elements in the list are separated by either "," or "][".
918 * If "comma" is set then only "," is allowed.
920 static __isl_give isl_multi_pw_aff *read_tuple_var_list(struct isl_stream *s,
921 struct vars *v, int rational, int comma)
924 struct isl_token *tok;
925 isl_multi_pw_aff *res;
927 res = tuple_alloc(v);
929 if (isl_stream_next_token_is(s, ']'))
932 while ((tok = next_token(s)) != NULL) {
935 res = tuple_add_dim(res, v);
937 if (tok->type == ISL_TOKEN_IDENT) {
939 int p = vars_pos(v, tok->u.s, -1);
945 if (tok->type == '*') {
946 if (vars_add_anon(v) < 0)
949 } else if (new_name) {
950 res = tuple_set_dim_name(res, i, v->v->name);
952 if (isl_stream_eat_if_available(s, '='))
953 res = read_tuple_var_def(s, res, i, v,
956 isl_stream_push_token(s, tok);
958 if (vars_add_anon(v) < 0)
960 res = read_tuple_var_def(s, res, i, v, rational);
963 tok = isl_stream_next_token(s);
964 if (!comma && tok && tok->type == ']' &&
965 isl_stream_next_token_is(s, '[')) {
967 tok = isl_stream_next_token(s);
968 } else if (!tok || tok->type != ',')
975 isl_stream_push_token(s, tok);
980 return isl_multi_pw_aff_free(res);
983 /* Read a tuple and represent it as an isl_multi_pw_aff. See tuple_alloc.
985 static __isl_give isl_multi_pw_aff *read_tuple(struct isl_stream *s,
986 struct vars *v, int rational, int comma)
988 struct isl_token *tok;
990 isl_multi_pw_aff *res = NULL;
992 tok = isl_stream_next_token(s);
995 if (tok->type == ISL_TOKEN_IDENT || tok->is_keyword) {
996 name = strdup(tok->u.s);
1001 isl_stream_push_token(s, tok);
1002 if (isl_stream_eat(s, '['))
1004 if (next_is_tuple(s)) {
1005 isl_multi_pw_aff *out;
1007 res = read_tuple(s, v, rational, comma);
1008 if (isl_stream_eat(s, ISL_TOKEN_TO))
1010 out = read_tuple(s, v, rational, comma);
1011 n = isl_multi_pw_aff_dim(out, isl_dim_out);
1012 res = isl_multi_pw_aff_add_dims(res, isl_dim_in, n);
1013 res = isl_multi_pw_aff_range_product(res, out);
1015 res = read_tuple_var_list(s, v, rational, comma);
1016 if (isl_stream_eat(s, ']'))
1020 res = isl_multi_pw_aff_set_tuple_name(res, isl_dim_out, name);
1027 return isl_multi_pw_aff_free(res);
1030 /* Read a tuple from "s" and add it to "map".
1031 * The tuple is initially represented as an isl_multi_pw_aff.
1032 * We first create the appropriate space in "map" based on the range
1033 * space of this isl_multi_pw_aff. Then, we add equalities based
1034 * on the affine expressions. These live in an anonymous space,
1035 * however, so we first need to reset the space to that of "map".
1037 static __isl_give isl_map *read_map_tuple(struct isl_stream *s,
1038 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1039 int rational, int comma)
1042 isl_multi_pw_aff *tuple;
1043 isl_space *space = NULL;
1045 tuple = read_tuple(s, v, rational, comma);
1049 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
1050 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
1054 if (type == isl_dim_param) {
1055 if (isl_space_has_tuple_name(space, isl_dim_set) ||
1056 isl_space_is_wrapping(space)) {
1057 isl_die(s->ctx, isl_error_invalid,
1058 "parameter tuples cannot be named or nested",
1061 map = isl_map_add_dims(map, type, n);
1062 for (i = 0; i < n; ++i) {
1064 if (!isl_space_has_dim_name(space, isl_dim_set, i))
1065 isl_die(s->ctx, isl_error_invalid,
1066 "parameters must be named",
1068 id = isl_space_get_dim_id(space, isl_dim_set, i);
1069 map = isl_map_set_dim_id(map, isl_dim_param, i, id);
1071 } else if (type == isl_dim_in) {
1074 set = isl_set_universe(isl_space_copy(space));
1076 set = isl_set_set_rational(set);
1077 set = isl_set_intersect_params(set, isl_map_params(map));
1078 map = isl_map_from_domain(set);
1082 set = isl_set_universe(isl_space_copy(space));
1084 set = isl_set_set_rational(set);
1085 map = isl_map_from_domain_and_range(isl_map_domain(map), set);
1088 for (i = 0; i < n; ++i) {
1095 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1096 space = isl_pw_aff_get_domain_space(pa);
1097 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1098 aff = isl_aff_add_coefficient_si(aff,
1099 isl_dim_in, v->n - n + i, -1);
1100 pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
1102 pa = isl_pw_aff_set_rational(pa);
1103 set = isl_pw_aff_zero_set(pa);
1104 map_i = isl_map_from_range(set);
1105 map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
1106 map = isl_map_intersect(map, map_i);
1109 isl_space_free(space);
1110 isl_multi_pw_aff_free(tuple);
1113 isl_space_free(space);
1114 isl_multi_pw_aff_free(tuple);
1119 static __isl_give isl_set *construct_constraints(
1120 __isl_take isl_set *set, int type,
1121 __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right,
1126 left = isl_pw_aff_list_copy(left);
1127 right = isl_pw_aff_list_copy(right);
1129 left = isl_pw_aff_list_set_rational(left);
1130 right = isl_pw_aff_list_set_rational(right);
1132 if (type == ISL_TOKEN_LE)
1133 cond = isl_pw_aff_list_le_set(left, right);
1134 else if (type == ISL_TOKEN_GE)
1135 cond = isl_pw_aff_list_ge_set(left, right);
1136 else if (type == ISL_TOKEN_LT)
1137 cond = isl_pw_aff_list_lt_set(left, right);
1138 else if (type == ISL_TOKEN_GT)
1139 cond = isl_pw_aff_list_gt_set(left, right);
1140 else if (type == ISL_TOKEN_NE)
1141 cond = isl_pw_aff_list_ne_set(left, right);
1143 cond = isl_pw_aff_list_eq_set(left, right);
1145 return isl_set_intersect(set, cond);
1148 static __isl_give isl_map *add_constraint(struct isl_stream *s,
1149 struct vars *v, __isl_take isl_map *map, int rational)
1151 struct isl_token *tok = NULL;
1152 isl_pw_aff_list *list1 = NULL, *list2 = NULL;
1155 set = isl_map_wrap(map);
1156 list1 = accept_affine_list(s, isl_set_get_space(set), v);
1159 tok = isl_stream_next_token(s);
1160 if (!is_comparator(tok)) {
1161 isl_stream_error(s, tok, "missing operator");
1163 isl_stream_push_token(s, tok);
1168 list2 = accept_affine_list(s, isl_set_get_space(set), v);
1172 set = construct_constraints(set, tok->type, list1, list2,
1174 isl_token_free(tok);
1175 isl_pw_aff_list_free(list1);
1178 tok = isl_stream_next_token(s);
1179 if (!is_comparator(tok)) {
1181 isl_stream_push_token(s, tok);
1185 isl_pw_aff_list_free(list1);
1187 return isl_set_unwrap(set);
1190 isl_token_free(tok);
1191 isl_pw_aff_list_free(list1);
1192 isl_pw_aff_list_free(list2);
1197 static __isl_give isl_map *read_exists(struct isl_stream *s,
1198 struct vars *v, __isl_take isl_map *map, int rational)
1201 int seen_paren = isl_stream_eat_if_available(s, '(');
1203 map = isl_map_from_domain(isl_map_wrap(map));
1204 map = read_defined_var_list(s, v, map, rational);
1206 if (isl_stream_eat(s, ':'))
1209 map = read_disjuncts(s, v, map, rational);
1210 map = isl_set_unwrap(isl_map_domain(map));
1212 vars_drop(v, v->n - n);
1213 if (seen_paren && isl_stream_eat(s, ')'))
1222 /* Parse an expression between parentheses and push the result
1223 * back on the stream.
1225 * The parsed expression may be either an affine expression
1226 * or a condition. The first type is pushed onto the stream
1227 * as an isl_pw_aff, while the second is pushed as an isl_map.
1229 * If the initial token indicates the start of a condition,
1230 * we parse it as such.
1231 * Otherwise, we first parse an affine expression and push
1232 * that onto the stream. If the affine expression covers the
1233 * entire expression between parentheses, we return.
1234 * Otherwise, we assume that the affine expression is the
1235 * start of a condition and continue parsing.
1237 static int resolve_paren_expr(struct isl_stream *s,
1238 struct vars *v, __isl_take isl_map *map, int rational)
1240 struct isl_token *tok, *tok2;
1244 tok = isl_stream_next_token(s);
1245 if (!tok || tok->type != '(')
1248 if (isl_stream_next_token_is(s, '('))
1249 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1252 if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1253 isl_stream_next_token_is(s, ISL_TOKEN_NOT) ||
1254 isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
1255 isl_stream_next_token_is(s, ISL_TOKEN_FALSE) ||
1256 isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1257 map = read_disjuncts(s, v, map, rational);
1258 if (isl_stream_eat(s, ')'))
1260 tok->type = ISL_TOKEN_MAP;
1262 isl_stream_push_token(s, tok);
1266 tok2 = isl_stream_next_token(s);
1271 isl_stream_push_token(s, tok2);
1273 pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
1277 tok2 = isl_token_new(s->ctx, line, col, 0);
1280 tok2->type = ISL_TOKEN_AFF;
1281 tok2->u.pwaff = pwaff;
1283 if (isl_stream_eat_if_available(s, ')')) {
1284 isl_stream_push_token(s, tok2);
1285 isl_token_free(tok);
1290 isl_stream_push_token(s, tok2);
1292 map = read_disjuncts(s, v, map, rational);
1293 if (isl_stream_eat(s, ')'))
1296 tok->type = ISL_TOKEN_MAP;
1298 isl_stream_push_token(s, tok);
1302 isl_pw_aff_free(pwaff);
1304 isl_token_free(tok);
1309 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
1310 struct vars *v, __isl_take isl_map *map, int rational)
1312 if (isl_stream_next_token_is(s, '('))
1313 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1316 if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1317 struct isl_token *tok;
1318 tok = isl_stream_next_token(s);
1322 map = isl_map_copy(tok->u.map);
1323 isl_token_free(tok);
1327 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1328 return read_exists(s, v, map, rational);
1330 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1333 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1334 isl_space *dim = isl_map_get_space(map);
1336 return isl_map_empty(dim);
1339 return add_constraint(s, v, map, rational);
1345 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
1346 struct vars *v, __isl_take isl_map *map, int rational)
1351 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1352 res = read_conjunct(s, v, isl_map_copy(map), rational);
1354 res = isl_map_subtract(isl_map_copy(map), res);
1356 while (res && isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
1359 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1360 res_i = read_conjunct(s, v, isl_map_copy(map), rational);
1362 res = isl_map_subtract(res, res_i);
1364 res = isl_map_intersect(res, res_i);
1371 static struct isl_map *read_disjuncts(struct isl_stream *s,
1372 struct vars *v, __isl_take isl_map *map, int rational)
1376 if (isl_stream_next_token_is(s, '}')) {
1377 isl_space *dim = isl_map_get_space(map);
1379 return isl_map_universe(dim);
1382 res = read_conjuncts(s, v, isl_map_copy(map), rational);
1383 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1386 res_i = read_conjuncts(s, v, isl_map_copy(map), rational);
1387 res = isl_map_union(res, res_i);
1394 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1396 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1397 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1398 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1399 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1401 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1402 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1403 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1405 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1406 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1407 isl_basic_map_dim(bmap, isl_dim_in) +
1408 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1409 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1411 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1417 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1418 struct isl_stream *s, __isl_take isl_basic_map *bmap)
1421 struct isl_token *tok;
1431 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1432 dim = isl_basic_map_dim(bmap, isl_dim_out);
1434 tok = isl_stream_next_token(s);
1435 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1436 isl_stream_error(s, tok, "expecting coefficient");
1438 isl_stream_push_token(s, tok);
1441 if (!tok->on_new_line) {
1442 isl_stream_error(s, tok, "coefficient should appear on new line");
1443 isl_stream_push_token(s, tok);
1447 type = isl_int_get_si(tok->u.v);
1448 isl_token_free(tok);
1450 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1452 k = isl_basic_map_alloc_equality(bmap);
1455 k = isl_basic_map_alloc_inequality(bmap);
1461 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1463 tok = isl_stream_next_token(s);
1464 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1465 isl_stream_error(s, tok, "expecting coefficient");
1467 isl_stream_push_token(s, tok);
1470 if (tok->on_new_line) {
1471 isl_stream_error(s, tok,
1472 "coefficient should not appear on new line");
1473 isl_stream_push_token(s, tok);
1476 pos = polylib_pos_to_isl_pos(bmap, j);
1477 isl_int_set(c[pos], tok->u.v);
1478 isl_token_free(tok);
1483 isl_basic_map_free(bmap);
1487 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s)
1490 struct isl_token *tok;
1491 struct isl_token *tok2;
1494 unsigned in = 0, out, local = 0;
1495 struct isl_basic_map *bmap = NULL;
1498 tok = isl_stream_next_token(s);
1500 isl_stream_error(s, NULL, "unexpected EOF");
1503 tok2 = isl_stream_next_token(s);
1505 isl_token_free(tok);
1506 isl_stream_error(s, NULL, "unexpected EOF");
1509 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1510 isl_stream_push_token(s, tok2);
1511 isl_stream_push_token(s, tok);
1512 isl_stream_error(s, NULL,
1513 "expecting constraint matrix dimensions");
1516 n_row = isl_int_get_si(tok->u.v);
1517 n_col = isl_int_get_si(tok2->u.v);
1518 on_new_line = tok2->on_new_line;
1519 isl_token_free(tok2);
1520 isl_token_free(tok);
1521 isl_assert(s->ctx, !on_new_line, return NULL);
1522 isl_assert(s->ctx, n_row >= 0, return NULL);
1523 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1524 tok = isl_stream_next_token_on_same_line(s);
1526 if (tok->type != ISL_TOKEN_VALUE) {
1527 isl_stream_error(s, tok,
1528 "expecting number of output dimensions");
1529 isl_stream_push_token(s, tok);
1532 out = isl_int_get_si(tok->u.v);
1533 isl_token_free(tok);
1535 tok = isl_stream_next_token_on_same_line(s);
1536 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1537 isl_stream_error(s, tok,
1538 "expecting number of input dimensions");
1540 isl_stream_push_token(s, tok);
1543 in = isl_int_get_si(tok->u.v);
1544 isl_token_free(tok);
1546 tok = isl_stream_next_token_on_same_line(s);
1547 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1548 isl_stream_error(s, tok,
1549 "expecting number of existentials");
1551 isl_stream_push_token(s, tok);
1554 local = isl_int_get_si(tok->u.v);
1555 isl_token_free(tok);
1557 tok = isl_stream_next_token_on_same_line(s);
1558 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1559 isl_stream_error(s, tok,
1560 "expecting number of parameters");
1562 isl_stream_push_token(s, tok);
1565 nparam = isl_int_get_si(tok->u.v);
1566 isl_token_free(tok);
1567 if (n_col != 1 + out + in + local + nparam + 1) {
1568 isl_stream_error(s, NULL,
1569 "dimensions don't match");
1573 out = n_col - 2 - nparam;
1574 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1578 for (i = 0; i < local; ++i) {
1579 int k = isl_basic_map_alloc_div(bmap);
1582 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1585 for (i = 0; i < n_row; ++i)
1586 bmap = basic_map_read_polylib_constraint(s, bmap);
1588 tok = isl_stream_next_token_on_same_line(s);
1590 isl_stream_error(s, tok, "unexpected extra token on line");
1591 isl_stream_push_token(s, tok);
1595 bmap = isl_basic_map_simplify(bmap);
1596 bmap = isl_basic_map_finalize(bmap);
1599 isl_basic_map_free(bmap);
1603 static struct isl_map *map_read_polylib(struct isl_stream *s)
1605 struct isl_token *tok;
1606 struct isl_token *tok2;
1608 struct isl_map *map;
1610 tok = isl_stream_next_token(s);
1612 isl_stream_error(s, NULL, "unexpected EOF");
1615 tok2 = isl_stream_next_token_on_same_line(s);
1616 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1617 isl_stream_push_token(s, tok2);
1618 isl_stream_push_token(s, tok);
1619 return isl_map_from_basic_map(basic_map_read_polylib(s));
1622 isl_stream_error(s, tok2, "unexpected token");
1623 isl_stream_push_token(s, tok2);
1624 isl_stream_push_token(s, tok);
1627 n = isl_int_get_si(tok->u.v);
1628 isl_token_free(tok);
1630 isl_assert(s->ctx, n >= 1, return NULL);
1632 map = isl_map_from_basic_map(basic_map_read_polylib(s));
1634 for (i = 1; map && i < n; ++i)
1635 map = isl_map_union(map,
1636 isl_map_from_basic_map(basic_map_read_polylib(s)));
1641 static int optional_power(struct isl_stream *s)
1644 struct isl_token *tok;
1646 tok = isl_stream_next_token(s);
1649 if (tok->type != '^') {
1650 isl_stream_push_token(s, tok);
1653 isl_token_free(tok);
1654 tok = isl_stream_next_token(s);
1655 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1656 isl_stream_error(s, tok, "expecting exponent");
1658 isl_stream_push_token(s, tok);
1661 pow = isl_int_get_si(tok->u.v);
1662 isl_token_free(tok);
1666 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1667 __isl_keep isl_map *map, struct vars *v);
1669 static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
1670 __isl_keep isl_map *map, struct vars *v)
1672 isl_pw_qpolynomial *pwqp;
1673 struct isl_token *tok;
1675 tok = next_token(s);
1677 isl_stream_error(s, NULL, "unexpected EOF");
1680 if (tok->type == '(') {
1683 isl_token_free(tok);
1684 pwqp = read_term(s, map, v);
1687 if (isl_stream_eat(s, ')'))
1689 pow = optional_power(s);
1690 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1691 } else if (tok->type == ISL_TOKEN_VALUE) {
1692 struct isl_token *tok2;
1693 tok2 = isl_stream_next_token(s);
1694 isl_qpolynomial *qp;
1695 if (tok2 && tok2->type == '/') {
1696 isl_token_free(tok2);
1697 tok2 = next_token(s);
1698 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1699 isl_stream_error(s, tok2, "expected denominator");
1700 isl_token_free(tok);
1701 isl_token_free(tok2);
1704 qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
1705 tok->u.v, tok2->u.v);
1706 isl_token_free(tok2);
1708 isl_stream_push_token(s, tok2);
1709 qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
1712 isl_token_free(tok);
1713 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1714 } else if (tok->type == ISL_TOKEN_INFTY) {
1715 isl_qpolynomial *qp;
1716 isl_token_free(tok);
1717 qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
1718 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1719 } else if (tok->type == ISL_TOKEN_NAN) {
1720 isl_qpolynomial *qp;
1721 isl_token_free(tok);
1722 qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
1723 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1724 } else if (tok->type == ISL_TOKEN_IDENT) {
1726 int pos = vars_pos(v, tok->u.s, -1);
1728 isl_qpolynomial *qp;
1730 isl_token_free(tok);
1734 vars_drop(v, v->n - n);
1735 isl_stream_error(s, tok, "unknown identifier");
1736 isl_token_free(tok);
1739 isl_token_free(tok);
1740 pow = optional_power(s);
1741 qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
1742 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1743 } else if (tok->type == '[') {
1747 isl_stream_push_token(s, tok);
1748 pwaff = accept_div(s, isl_map_get_space(map), v);
1749 pow = optional_power(s);
1750 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
1751 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1752 } else if (tok->type == '-') {
1753 isl_token_free(tok);
1754 pwqp = read_factor(s, map, v);
1755 pwqp = isl_pw_qpolynomial_neg(pwqp);
1757 isl_stream_error(s, tok, "unexpected isl_token");
1758 isl_stream_push_token(s, tok);
1762 if (isl_stream_eat_if_available(s, '*') ||
1763 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1764 isl_pw_qpolynomial *pwqp2;
1766 pwqp2 = read_factor(s, map, v);
1767 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
1772 isl_pw_qpolynomial_free(pwqp);
1776 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1777 __isl_keep isl_map *map, struct vars *v)
1779 struct isl_token *tok;
1780 isl_pw_qpolynomial *pwqp;
1782 pwqp = read_factor(s, map, v);
1785 tok = next_token(s);
1789 if (tok->type == '+') {
1790 isl_pw_qpolynomial *pwqp2;
1792 isl_token_free(tok);
1793 pwqp2 = read_factor(s, map, v);
1794 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1795 } else if (tok->type == '-') {
1796 isl_pw_qpolynomial *pwqp2;
1798 isl_token_free(tok);
1799 pwqp2 = read_factor(s, map, v);
1800 pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
1801 } else if (tok->type == ISL_TOKEN_VALUE &&
1802 isl_int_is_neg(tok->u.v)) {
1803 isl_pw_qpolynomial *pwqp2;
1805 isl_stream_push_token(s, tok);
1806 pwqp2 = read_factor(s, map, v);
1807 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1809 isl_stream_push_token(s, tok);
1817 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1818 __isl_take isl_map *map, struct vars *v, int rational)
1820 struct isl_token *tok;
1822 tok = isl_stream_next_token(s);
1824 isl_stream_error(s, NULL, "unexpected EOF");
1827 if (tok->type == ':' ||
1828 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1829 isl_token_free(tok);
1830 map = read_disjuncts(s, v, map, rational);
1832 isl_stream_push_token(s, tok);
1840 static struct isl_obj obj_read_poly(struct isl_stream *s,
1841 __isl_take isl_map *map, struct vars *v, int n)
1843 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1844 isl_pw_qpolynomial *pwqp;
1845 struct isl_set *set;
1847 pwqp = read_term(s, map, v);
1848 map = read_optional_disjuncts(s, map, v, 0);
1849 set = isl_map_range(map);
1851 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
1853 vars_drop(v, v->n - n);
1859 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1860 __isl_take isl_set *set, struct vars *v, int n)
1862 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1863 isl_pw_qpolynomial *pwqp;
1864 isl_pw_qpolynomial_fold *pwf = NULL;
1866 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1867 return obj_read_poly(s, set, v, n);
1869 if (isl_stream_eat(s, '('))
1872 pwqp = read_term(s, set, v);
1873 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1875 while (isl_stream_eat_if_available(s, ',')) {
1876 isl_pw_qpolynomial_fold *pwf_i;
1877 pwqp = read_term(s, set, v);
1878 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1880 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1883 if (isl_stream_eat(s, ')'))
1886 set = read_optional_disjuncts(s, set, v, 0);
1887 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1889 vars_drop(v, v->n - n);
1895 isl_pw_qpolynomial_fold_free(pwf);
1896 obj.type = isl_obj_none;
1900 static int is_rational(struct isl_stream *s)
1902 struct isl_token *tok;
1904 tok = isl_stream_next_token(s);
1907 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1908 isl_token_free(tok);
1909 isl_stream_eat(s, ':');
1913 isl_stream_push_token(s, tok);
1918 static struct isl_obj obj_read_body(struct isl_stream *s,
1919 __isl_take isl_map *map, struct vars *v)
1921 struct isl_token *tok;
1922 struct isl_obj obj = { isl_obj_set, NULL };
1926 rational = is_rational(s);
1928 map = isl_map_set_rational(map);
1930 if (isl_stream_next_token_is(s, ':')) {
1931 obj.type = isl_obj_set;
1932 obj.v = read_optional_disjuncts(s, map, v, rational);
1936 if (!next_is_tuple(s))
1937 return obj_read_poly_or_fold(s, map, v, n);
1939 map = read_map_tuple(s, map, isl_dim_in, v, rational, 0);
1942 tok = isl_stream_next_token(s);
1945 if (tok->type == ISL_TOKEN_TO) {
1946 obj.type = isl_obj_map;
1947 isl_token_free(tok);
1948 if (!next_is_tuple(s)) {
1949 isl_set *set = isl_map_domain(map);
1950 return obj_read_poly_or_fold(s, set, v, n);
1952 map = read_map_tuple(s, map, isl_dim_out, v, rational, 0);
1956 map = isl_map_domain(map);
1957 isl_stream_push_token(s, tok);
1960 map = read_optional_disjuncts(s, map, v, rational);
1962 vars_drop(v, v->n - n);
1968 obj.type = isl_obj_none;
1972 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1974 if (obj.type == isl_obj_map) {
1975 obj.v = isl_union_map_from_map(obj.v);
1976 obj.type = isl_obj_union_map;
1977 } else if (obj.type == isl_obj_set) {
1978 obj.v = isl_union_set_from_set(obj.v);
1979 obj.type = isl_obj_union_set;
1980 } else if (obj.type == isl_obj_pw_qpolynomial) {
1981 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1982 obj.type = isl_obj_union_pw_qpolynomial;
1983 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1984 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1985 obj.type = isl_obj_union_pw_qpolynomial_fold;
1987 isl_assert(ctx, 0, goto error);
1990 obj.type->free(obj.v);
1991 obj.type = isl_obj_none;
1995 static struct isl_obj obj_add(struct isl_ctx *ctx,
1996 struct isl_obj obj1, struct isl_obj obj2)
1998 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1999 obj1 = to_union(ctx, obj1);
2000 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
2001 obj2 = to_union(ctx, obj2);
2002 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
2003 obj1 = to_union(ctx, obj1);
2004 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
2005 obj2 = to_union(ctx, obj2);
2006 if (obj1.type == isl_obj_pw_qpolynomial &&
2007 obj2.type == isl_obj_union_pw_qpolynomial)
2008 obj1 = to_union(ctx, obj1);
2009 if (obj1.type == isl_obj_union_pw_qpolynomial &&
2010 obj2.type == isl_obj_pw_qpolynomial)
2011 obj2 = to_union(ctx, obj2);
2012 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2013 obj2.type == isl_obj_union_pw_qpolynomial_fold)
2014 obj1 = to_union(ctx, obj1);
2015 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
2016 obj2.type == isl_obj_pw_qpolynomial_fold)
2017 obj2 = to_union(ctx, obj2);
2018 isl_assert(ctx, obj1.type == obj2.type, goto error);
2019 if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
2020 obj1 = to_union(ctx, obj1);
2021 obj2 = to_union(ctx, obj2);
2023 if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
2024 obj1 = to_union(ctx, obj1);
2025 obj2 = to_union(ctx, obj2);
2027 if (obj1.type == isl_obj_pw_qpolynomial &&
2028 !isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)) {
2029 obj1 = to_union(ctx, obj1);
2030 obj2 = to_union(ctx, obj2);
2032 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2033 !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
2034 obj1 = to_union(ctx, obj1);
2035 obj2 = to_union(ctx, obj2);
2037 obj1.v = obj1.type->add(obj1.v, obj2.v);
2040 obj1.type->free(obj1.v);
2041 obj2.type->free(obj2.v);
2042 obj1.type = isl_obj_none;
2047 static struct isl_obj obj_read(struct isl_stream *s)
2049 isl_map *map = NULL;
2050 struct isl_token *tok;
2051 struct vars *v = NULL;
2052 struct isl_obj obj = { isl_obj_set, NULL };
2054 tok = next_token(s);
2056 isl_stream_error(s, NULL, "unexpected EOF");
2059 if (tok->type == ISL_TOKEN_VALUE) {
2060 struct isl_token *tok2;
2061 struct isl_map *map;
2063 tok2 = isl_stream_next_token(s);
2064 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
2065 isl_int_is_neg(tok2->u.v)) {
2067 isl_stream_push_token(s, tok2);
2068 obj.type = isl_obj_int;
2069 obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
2070 isl_token_free(tok);
2073 isl_stream_push_token(s, tok2);
2074 isl_stream_push_token(s, tok);
2075 map = map_read_polylib(s);
2078 if (isl_map_may_be_set(map))
2079 obj.v = isl_map_range(map);
2081 obj.type = isl_obj_map;
2086 v = vars_new(s->ctx);
2088 isl_stream_push_token(s, tok);
2091 map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
2092 if (tok->type == '[') {
2093 isl_stream_push_token(s, tok);
2094 map = read_map_tuple(s, map, isl_dim_param, v, 0, 0);
2097 tok = isl_stream_next_token(s);
2098 if (!tok || tok->type != ISL_TOKEN_TO) {
2099 isl_stream_error(s, tok, "expecting '->'");
2101 isl_stream_push_token(s, tok);
2104 isl_token_free(tok);
2105 tok = isl_stream_next_token(s);
2107 if (!tok || tok->type != '{') {
2108 isl_stream_error(s, tok, "expecting '{'");
2110 isl_stream_push_token(s, tok);
2113 isl_token_free(tok);
2115 tok = isl_stream_next_token(s);
2118 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
2119 isl_token_free(tok);
2120 if (isl_stream_eat(s, '='))
2122 map = read_map_tuple(s, map, isl_dim_param, v, 0, 1);
2125 } else if (tok->type == '}') {
2126 obj.type = isl_obj_union_set;
2127 obj.v = isl_union_set_empty(isl_map_get_space(map));
2128 isl_token_free(tok);
2131 isl_stream_push_token(s, tok);
2136 o = obj_read_body(s, isl_map_copy(map), v);
2137 if (o.type == isl_obj_none || !o.v)
2142 obj = obj_add(s->ctx, obj, o);
2143 if (obj.type == isl_obj_none || !obj.v)
2146 tok = isl_stream_next_token(s);
2147 if (!tok || tok->type != ';')
2149 isl_token_free(tok);
2150 if (isl_stream_next_token_is(s, '}')) {
2151 tok = isl_stream_next_token(s);
2156 if (tok && tok->type == '}') {
2157 isl_token_free(tok);
2159 isl_stream_error(s, tok, "unexpected isl_token");
2161 isl_token_free(tok);
2171 obj.type->free(obj.v);
2178 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
2183 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s)
2189 isl_assert(s->ctx, obj.type == isl_obj_map ||
2190 obj.type == isl_obj_set, goto error);
2192 if (obj.type == isl_obj_set)
2193 obj.v = isl_map_from_range(obj.v);
2197 obj.type->free(obj.v);
2201 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s)
2207 if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
2208 obj.v = isl_map_range(obj.v);
2209 obj.type = isl_obj_set;
2211 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2216 obj.type->free(obj.v);
2220 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
2225 if (obj.type == isl_obj_map) {
2226 obj.type = isl_obj_union_map;
2227 obj.v = isl_union_map_from_map(obj.v);
2229 if (obj.type == isl_obj_set) {
2230 obj.type = isl_obj_union_set;
2231 obj.v = isl_union_set_from_set(obj.v);
2233 if (obj.v && obj.type == isl_obj_union_set &&
2234 isl_union_set_is_empty(obj.v))
2235 obj.type = isl_obj_union_map;
2236 if (obj.v && obj.type != isl_obj_union_map)
2237 isl_die(s->ctx, isl_error_invalid, "invalid input", goto error);
2241 obj.type->free(obj.v);
2245 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
2250 if (obj.type == isl_obj_set) {
2251 obj.type = isl_obj_union_set;
2252 obj.v = isl_union_set_from_set(obj.v);
2255 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2259 obj.type->free(obj.v);
2263 static __isl_give isl_basic_map *basic_map_read(struct isl_stream *s)
2266 struct isl_map *map;
2267 struct isl_basic_map *bmap;
2274 isl_assert(map->ctx, map->n <= 1, goto error);
2277 bmap = isl_basic_map_empty_like_map(map);
2279 bmap = isl_basic_map_copy(map->p[0]);
2289 static __isl_give isl_basic_set *basic_set_read(struct isl_stream *s)
2291 isl_basic_map *bmap;
2292 bmap = basic_map_read(s);
2295 if (!isl_basic_map_may_be_set(bmap))
2296 isl_die(s->ctx, isl_error_invalid,
2297 "input is not a set", goto error);
2298 return isl_basic_map_range(bmap);
2300 isl_basic_map_free(bmap);
2304 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2307 struct isl_basic_map *bmap;
2308 struct isl_stream *s = isl_stream_new_file(ctx, input);
2311 bmap = basic_map_read(s);
2316 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2319 isl_basic_set *bset;
2320 struct isl_stream *s = isl_stream_new_file(ctx, input);
2323 bset = basic_set_read(s);
2328 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2331 struct isl_basic_map *bmap;
2332 struct isl_stream *s = isl_stream_new_str(ctx, str);
2335 bmap = basic_map_read(s);
2340 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2343 isl_basic_set *bset;
2344 struct isl_stream *s = isl_stream_new_str(ctx, str);
2347 bset = basic_set_read(s);
2352 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2355 struct isl_map *map;
2356 struct isl_stream *s = isl_stream_new_file(ctx, input);
2359 map = isl_stream_read_map(s);
2364 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2367 struct isl_map *map;
2368 struct isl_stream *s = isl_stream_new_str(ctx, str);
2371 map = isl_stream_read_map(s);
2376 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2380 struct isl_stream *s = isl_stream_new_file(ctx, input);
2383 set = isl_stream_read_set(s);
2388 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2392 struct isl_stream *s = isl_stream_new_str(ctx, str);
2395 set = isl_stream_read_set(s);
2400 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2403 isl_union_map *umap;
2404 struct isl_stream *s = isl_stream_new_file(ctx, input);
2407 umap = isl_stream_read_union_map(s);
2412 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2415 isl_union_map *umap;
2416 struct isl_stream *s = isl_stream_new_str(ctx, str);
2419 umap = isl_stream_read_union_map(s);
2424 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2427 isl_union_set *uset;
2428 struct isl_stream *s = isl_stream_new_file(ctx, input);
2431 uset = isl_stream_read_union_set(s);
2436 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2439 isl_union_set *uset;
2440 struct isl_stream *s = isl_stream_new_str(ctx, str);
2443 uset = isl_stream_read_union_set(s);
2448 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2450 struct isl_vec *vec = NULL;
2451 struct isl_token *tok;
2455 tok = isl_stream_next_token(s);
2456 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2457 isl_stream_error(s, tok, "expecting vector length");
2461 size = isl_int_get_si(tok->u.v);
2462 isl_token_free(tok);
2464 vec = isl_vec_alloc(s->ctx, size);
2466 for (j = 0; j < size; ++j) {
2467 tok = isl_stream_next_token(s);
2468 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2469 isl_stream_error(s, tok, "expecting constant value");
2472 isl_int_set(vec->el[j], tok->u.v);
2473 isl_token_free(tok);
2478 isl_token_free(tok);
2483 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2485 return isl_vec_read_polylib(s);
2488 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2491 struct isl_stream *s = isl_stream_new_file(ctx, input);
2499 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2500 struct isl_stream *s)
2506 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2511 obj.type->free(obj.v);
2515 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2518 isl_pw_qpolynomial *pwqp;
2519 struct isl_stream *s = isl_stream_new_str(ctx, str);
2522 pwqp = isl_stream_read_pw_qpolynomial(s);
2527 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2530 isl_pw_qpolynomial *pwqp;
2531 struct isl_stream *s = isl_stream_new_file(ctx, input);
2534 pwqp = isl_stream_read_pw_qpolynomial(s);
2539 /* Is the next token an identifer not in "v"?
2541 static int next_is_fresh_ident(struct isl_stream *s, struct vars *v)
2545 struct isl_token *tok;
2547 tok = isl_stream_next_token(s);
2550 fresh = tok->type == ISL_TOKEN_IDENT && vars_pos(v, tok->u.s, -1) >= n;
2551 isl_stream_push_token(s, tok);
2553 vars_drop(v, v->n - n);
2558 /* First read the domain of the affine expression, which may be
2559 * a parameter space or a set.
2560 * The tricky part is that we don't know if the domain is a set or not,
2561 * so when we are trying to read the domain, we may actually be reading
2562 * the affine expression itself (defined on a parameter domains)
2563 * If the tuple we are reading is named, we assume it's the domain.
2564 * Also, if inside the tuple, the first thing we find is a nested tuple
2565 * or a new identifier, we again assume it's the domain.
2566 * Otherwise, we assume we are reading an affine expression.
2568 static __isl_give isl_set *read_aff_domain(struct isl_stream *s,
2569 __isl_take isl_set *dom, struct vars *v)
2571 struct isl_token *tok;
2573 tok = isl_stream_next_token(s);
2574 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
2575 isl_stream_push_token(s, tok);
2576 return read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
2578 if (!tok || tok->type != '[') {
2579 isl_stream_error(s, tok, "expecting '['");
2582 if (next_is_tuple(s) || next_is_fresh_ident(s, v)) {
2583 isl_stream_push_token(s, tok);
2584 dom = read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
2586 isl_stream_push_token(s, tok);
2591 isl_stream_push_token(s, tok);
2596 /* Read an affine expression from "s".
2598 __isl_give isl_aff *isl_stream_read_aff(struct isl_stream *s)
2603 ma = isl_stream_read_multi_aff(s);
2606 if (isl_multi_aff_dim(ma, isl_dim_out) != 1)
2607 isl_die(s->ctx, isl_error_invalid,
2608 "expecting single affine expression",
2611 aff = isl_multi_aff_get_aff(ma, 0);
2612 isl_multi_aff_free(ma);
2615 isl_multi_aff_free(ma);
2619 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2621 static __isl_give isl_pw_aff *read_pw_aff_with_dom(struct isl_stream *s,
2622 __isl_take isl_set *dom, struct vars *v)
2624 isl_pw_aff *pwaff = NULL;
2626 if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
2629 if (isl_stream_eat(s, '['))
2632 pwaff = accept_affine(s, isl_set_get_space(dom), v);
2634 if (isl_stream_eat(s, ']'))
2637 dom = read_optional_disjuncts(s, dom, v, 0);
2638 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2643 isl_pw_aff_free(pwaff);
2647 __isl_give isl_pw_aff *isl_stream_read_pw_aff(struct isl_stream *s)
2650 isl_set *dom = NULL;
2652 isl_pw_aff *pa = NULL;
2655 v = vars_new(s->ctx);
2659 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
2660 if (next_is_tuple(s)) {
2661 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
2662 if (isl_stream_eat(s, ISL_TOKEN_TO))
2665 if (isl_stream_eat(s, '{'))
2669 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
2670 pa = read_pw_aff_with_dom(s, aff_dom, v);
2671 vars_drop(v, v->n - n);
2673 while (isl_stream_eat_if_available(s, ';')) {
2677 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
2678 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
2679 vars_drop(v, v->n - n);
2681 pa = isl_pw_aff_union_add(pa, pa_i);
2684 if (isl_stream_eat(s, '}'))
2693 isl_pw_aff_free(pa);
2697 __isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
2700 struct isl_stream *s = isl_stream_new_str(ctx, str);
2703 aff = isl_stream_read_aff(s);
2708 __isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
2711 struct isl_stream *s = isl_stream_new_str(ctx, str);
2714 pa = isl_stream_read_pw_aff(s);
2719 /* Read an isl_pw_multi_aff from "s".
2720 * We currently read a generic object and if it turns out to be a set or
2721 * a map, we convert that to an isl_pw_multi_aff.
2722 * It would be more efficient if we were to construct the isl_pw_multi_aff
2725 __isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(struct isl_stream *s)
2733 if (obj.type == isl_obj_map)
2734 return isl_pw_multi_aff_from_map(obj.v);
2735 if (obj.type == isl_obj_set)
2736 return isl_pw_multi_aff_from_set(obj.v);
2738 obj.type->free(obj.v);
2739 isl_die(s->ctx, isl_error_invalid, "unexpected object type",
2743 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
2746 isl_pw_multi_aff *pma;
2747 struct isl_stream *s = isl_stream_new_str(ctx, str);
2750 pma = isl_stream_read_pw_multi_aff(s);
2755 /* Read an isl_union_pw_multi_aff from "s".
2756 * We currently read a generic object and if it turns out to be a set or
2757 * a map, we convert that to an isl_union_pw_multi_aff.
2758 * It would be more efficient if we were to construct
2759 * the isl_union_pw_multi_aff directly.
2761 __isl_give isl_union_pw_multi_aff *isl_stream_read_union_pw_multi_aff(
2762 struct isl_stream *s)
2770 if (obj.type == isl_obj_map || obj.type == isl_obj_set)
2771 obj = to_union(s->ctx, obj);
2772 if (obj.type == isl_obj_union_map)
2773 return isl_union_pw_multi_aff_from_union_map(obj.v);
2774 if (obj.type == isl_obj_union_set)
2775 return isl_union_pw_multi_aff_from_union_set(obj.v);
2777 obj.type->free(obj.v);
2778 isl_die(s->ctx, isl_error_invalid, "unexpected object type",
2782 /* Read an isl_union_pw_multi_aff from "str".
2784 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_read_from_str(
2785 isl_ctx *ctx, const char *str)
2787 isl_union_pw_multi_aff *upma;
2788 struct isl_stream *s = isl_stream_new_str(ctx, str);
2791 upma = isl_stream_read_union_pw_multi_aff(s);
2796 /* Assuming "pa" represents a single affine expression defined on a universe
2797 * domain, extract this affine expression.
2799 static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
2806 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2807 "expecting single affine expression",
2809 if (!isl_set_plain_is_universe(pa->p[0].set))
2810 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2811 "expecting universe domain",
2814 aff = isl_aff_copy(pa->p[0].aff);
2815 isl_pw_aff_free(pa);
2818 isl_pw_aff_free(pa);
2822 /* Read a multi-affine expression from "s".
2823 * If the multi-affine expression has a domain, then then tuple
2824 * representing this domain cannot involve any affine expressions.
2825 * The tuple representing the actual expressions needs to consist
2826 * of only affine expressions. Moreover, these expressions can
2827 * only depend on parameters and input dimensions and not on other
2828 * output dimensions.
2830 __isl_give isl_multi_aff *isl_stream_read_multi_aff(struct isl_stream *s)
2833 isl_set *dom = NULL;
2834 isl_multi_pw_aff *tuple = NULL;
2836 isl_space *space, *dom_space;
2837 isl_multi_aff *ma = NULL;
2839 v = vars_new(s->ctx);
2843 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
2844 if (next_is_tuple(s)) {
2845 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
2846 if (isl_stream_eat(s, ISL_TOKEN_TO))
2849 if (!isl_set_plain_is_universe(dom))
2850 isl_die(s->ctx, isl_error_invalid,
2851 "expecting universe parameter domain", goto error);
2852 if (isl_stream_eat(s, '{'))
2855 tuple = read_tuple(s, v, 0, 0);
2858 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
2863 has_expr = tuple_has_expr(tuple);
2867 isl_die(s->ctx, isl_error_invalid,
2868 "expecting universe domain", goto error);
2869 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
2870 set = isl_set_universe(space);
2871 dom = isl_set_intersect_params(set, dom);
2872 isl_multi_pw_aff_free(tuple);
2873 tuple = read_tuple(s, v, 0, 0);
2878 if (isl_stream_eat(s, '}'))
2881 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
2882 dim = isl_set_dim(dom, isl_dim_all);
2883 dom_space = isl_set_get_space(dom);
2884 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
2885 space = isl_space_align_params(space, isl_space_copy(dom_space));
2886 if (!isl_space_is_params(dom_space))
2887 space = isl_space_map_from_domain_and_range(
2888 isl_space_copy(dom_space), space);
2889 isl_space_free(dom_space);
2890 ma = isl_multi_aff_alloc(space);
2892 for (i = 0; i < n; ++i) {
2895 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
2896 aff = aff_from_pw_aff(pa);
2899 if (isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)) {
2901 isl_die(s->ctx, isl_error_invalid,
2902 "not an affine expression", goto error);
2904 aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
2905 space = isl_multi_aff_get_domain_space(ma);
2906 aff = isl_aff_reset_domain_space(aff, space);
2907 ma = isl_multi_aff_set_aff(ma, i, aff);
2910 isl_multi_pw_aff_free(tuple);
2915 isl_multi_pw_aff_free(tuple);
2918 isl_multi_aff_free(ma);
2922 __isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
2925 isl_multi_aff *maff;
2926 struct isl_stream *s = isl_stream_new_str(ctx, str);
2929 maff = isl_stream_read_multi_aff(s);
2934 __isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
2935 struct isl_stream *s)
2940 if (obj.type == isl_obj_pw_qpolynomial) {
2941 obj.type = isl_obj_union_pw_qpolynomial;
2942 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
2945 isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
2950 obj.type->free(obj.v);
2954 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
2955 isl_ctx *ctx, const char *str)
2957 isl_union_pw_qpolynomial *upwqp;
2958 struct isl_stream *s = isl_stream_new_str(ctx, str);
2961 upwqp = isl_stream_read_union_pw_qpolynomial(s);