2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
21 #include <isl_stream_private.h>
23 #include "isl_polynomial_private.h"
24 #include <isl/union_map.h>
25 #include <isl_mat_private.h>
26 #include <isl_aff_private.h>
32 struct variable *next;
41 static struct vars *vars_new(struct isl_ctx *ctx)
44 v = isl_alloc_type(ctx, struct vars);
53 static void variable_free(struct variable *var)
56 struct variable *next = var->next;
63 static void vars_free(struct vars *v)
71 static void vars_drop(struct vars *v, int n)
82 struct variable *next = var->next;
90 static struct variable *variable_new(struct vars *v, const char *name, int len,
94 var = isl_calloc_type(v->ctx, struct variable);
97 var->name = strdup(name);
98 var->name[len] = '\0';
107 static int vars_pos(struct vars *v, const char *s, int len)
114 for (q = v->v; q; q = q->next) {
115 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
122 v->v = variable_new(v, s, len, v->n);
130 static int vars_add_anon(struct vars *v)
132 v->v = variable_new(v, "", 0, v->n);
141 static __isl_give isl_map *set_name(__isl_take isl_map *map,
142 enum isl_dim_type type, unsigned pos, char *name)
151 prime = strchr(name, '\'');
154 map = isl_map_set_dim_name(map, type, pos, name);
161 /* Obtain next token, with some preprocessing.
162 * In particular, evaluate expressions of the form x^y,
163 * with x and y values.
165 static struct isl_token *next_token(struct isl_stream *s)
167 struct isl_token *tok, *tok2;
169 tok = isl_stream_next_token(s);
170 if (!tok || tok->type != ISL_TOKEN_VALUE)
172 if (!isl_stream_eat_if_available(s, '^'))
174 tok2 = isl_stream_next_token(s);
175 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
176 isl_stream_error(s, tok2, "expecting constant value");
180 isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
182 isl_token_free(tok2);
186 isl_token_free(tok2);
190 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
192 struct isl_token *tok;
195 if (!tok || tok->type != ISL_TOKEN_VALUE) {
196 isl_stream_error(s, tok, "expecting constant value");
200 isl_int_mul(*f, *f, tok->u.v);
204 if (isl_stream_eat_if_available(s, '*'))
205 return accept_cst_factor(s, f);
213 /* Given an affine expression aff, return an affine expression
214 * for aff % d, with d the next token on the stream, which is
215 * assumed to be a constant.
217 * We introduce an integer division q = [aff/d] and the result
218 * is set to aff - d q.
220 static __isl_give isl_pw_aff *affine_mod(struct isl_stream *s,
221 struct vars *v, __isl_take isl_pw_aff *aff)
223 struct isl_token *tok;
227 if (!tok || tok->type != ISL_TOKEN_VALUE) {
228 isl_stream_error(s, tok, "expecting constant value");
232 q = isl_pw_aff_copy(aff);
233 q = isl_pw_aff_scale_down(q, tok->u.v);
234 q = isl_pw_aff_floor(q);
235 q = isl_pw_aff_scale(q, tok->u.v);
237 aff = isl_pw_aff_sub(aff, q);
242 isl_pw_aff_free(aff);
247 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
248 __isl_take isl_space *dim, struct vars *v);
249 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
250 __isl_take isl_space *dim, struct vars *v);
252 static __isl_give isl_pw_aff *accept_minmax(struct isl_stream *s,
253 __isl_take isl_space *dim, struct vars *v)
255 struct isl_token *tok;
256 isl_pw_aff_list *list = NULL;
259 tok = isl_stream_next_token(s);
262 min = tok->type == ISL_TOKEN_MIN;
265 if (isl_stream_eat(s, '('))
268 list = accept_affine_list(s, isl_space_copy(dim), v);
272 if (isl_stream_eat(s, ')'))
276 return min ? isl_pw_aff_list_min(list) : isl_pw_aff_list_max(list);
279 isl_pw_aff_list_free(list);
283 static __isl_give isl_pw_aff *accept_div(struct isl_stream *s,
284 __isl_take isl_space *dim, struct vars *v)
286 struct isl_token *tok;
290 isl_pw_aff *pwaff = NULL;
292 if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOORD))
294 else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEILD))
297 if (isl_stream_eat(s, '('))
300 if (isl_stream_eat(s, '['))
302 if (isl_stream_eat_if_available(s, '('))
306 pwaff = accept_affine(s, isl_space_copy(dim), v);
309 if (isl_stream_eat(s, ','))
312 if (seen_paren && isl_stream_eat(s, ')'))
314 if (isl_stream_eat(s, '/'))
321 if (tok->type != ISL_TOKEN_VALUE) {
322 isl_stream_error(s, tok, "expected denominator");
323 isl_stream_push_token(s, tok);
326 isl_pw_aff_scale_down(pwaff, tok->u.v);
330 pwaff = isl_pw_aff_ceil(pwaff);
332 pwaff = isl_pw_aff_floor(pwaff);
335 if (isl_stream_eat(s, ')'))
338 if (isl_stream_eat(s, ']'))
346 isl_pw_aff_free(pwaff);
350 static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
351 __isl_take isl_space *dim, struct vars *v)
353 struct isl_token *tok = NULL;
354 isl_pw_aff *res = NULL;
358 isl_stream_error(s, NULL, "unexpected EOF");
362 if (tok->type == ISL_TOKEN_AFF) {
363 res = isl_pw_aff_copy(tok->u.pwaff);
365 } else if (tok->type == ISL_TOKEN_IDENT) {
367 int pos = vars_pos(v, tok->u.s, -1);
373 isl_stream_error(s, tok, "unknown identifier");
377 aff = isl_aff_zero(isl_local_space_from_space(isl_space_copy(dim)));
380 isl_int_set_si(aff->v->el[2 + pos], 1);
381 res = isl_pw_aff_from_aff(aff);
383 } else if (tok->type == ISL_TOKEN_VALUE) {
384 if (isl_stream_eat_if_available(s, '*')) {
385 res = accept_affine_factor(s, isl_space_copy(dim), v);
386 res = isl_pw_aff_scale(res, tok->u.v);
390 ls = isl_local_space_from_space(isl_space_copy(dim));
391 aff = isl_aff_zero(ls);
392 aff = isl_aff_add_constant(aff, tok->u.v);
393 res = isl_pw_aff_from_aff(aff);
396 } else if (tok->type == '(') {
399 res = accept_affine(s, isl_space_copy(dim), v);
402 if (isl_stream_eat(s, ')'))
404 } else if (tok->type == '[' ||
405 tok->type == ISL_TOKEN_FLOORD ||
406 tok->type == ISL_TOKEN_CEILD) {
407 isl_stream_push_token(s, tok);
409 res = accept_div(s, isl_space_copy(dim), v);
410 } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
411 isl_stream_push_token(s, tok);
413 res = accept_minmax(s, isl_space_copy(dim), v);
415 isl_stream_error(s, tok, "expecting factor");
418 if (isl_stream_eat_if_available(s, '%') ||
419 isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
421 return affine_mod(s, v, res);
423 if (isl_stream_eat_if_available(s, '*')) {
426 isl_int_set_si(f, 1);
427 if (accept_cst_factor(s, &f) < 0) {
431 res = isl_pw_aff_scale(res, f);
440 isl_pw_aff_free(res);
445 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
449 aff = isl_aff_zero(isl_local_space_from_space(isl_pw_aff_get_space(pwaff)));
450 aff = isl_aff_add_constant(aff, v);
452 return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
455 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
456 __isl_take isl_space *dim, struct vars *v)
458 struct isl_token *tok = NULL;
463 ls = isl_local_space_from_space(isl_space_copy(dim));
464 res = isl_pw_aff_from_aff(isl_aff_zero(ls));
471 isl_stream_error(s, NULL, "unexpected EOF");
474 if (tok->type == '-') {
479 if (tok->type == '(' || tok->type == '[' ||
480 tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
481 tok->type == ISL_TOKEN_FLOORD ||
482 tok->type == ISL_TOKEN_CEILD ||
483 tok->type == ISL_TOKEN_IDENT ||
484 tok->type == ISL_TOKEN_AFF) {
486 isl_stream_push_token(s, tok);
488 term = accept_affine_factor(s, isl_space_copy(dim), v);
490 res = isl_pw_aff_sub(res, term);
492 res = isl_pw_aff_add(res, term);
496 } else if (tok->type == ISL_TOKEN_VALUE) {
498 isl_int_neg(tok->u.v, tok->u.v);
499 if (isl_stream_eat_if_available(s, '*') ||
500 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
502 term = accept_affine_factor(s,
503 isl_space_copy(dim), v);
504 term = isl_pw_aff_scale(term, tok->u.v);
505 res = isl_pw_aff_add(res, term);
509 res = add_cst(res, tok->u.v);
513 isl_stream_error(s, tok, "unexpected isl_token");
514 isl_stream_push_token(s, tok);
515 isl_pw_aff_free(res);
522 if (tok && tok->type == '-') {
525 } else if (tok && tok->type == '+') {
528 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
529 isl_int_is_neg(tok->u.v)) {
530 isl_stream_push_token(s, tok);
533 isl_stream_push_token(s, tok);
543 isl_pw_aff_free(res);
547 static int is_comparator(struct isl_token *tok)
565 static struct isl_map *read_disjuncts(struct isl_stream *s,
566 struct vars *v, __isl_take isl_map *map);
567 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
568 __isl_take isl_space *dim, struct vars *v);
570 /* Accept a ternary operator, given the first argument.
572 static __isl_give isl_pw_aff *accept_ternary(struct isl_stream *s,
573 __isl_take isl_map *cond, struct vars *v)
576 isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL;
581 if (isl_stream_eat(s, '?'))
584 dim = isl_space_wrap(isl_map_get_space(cond));
585 pwaff1 = accept_extended_affine(s, dim, v);
589 if (isl_stream_eat(s, ':'))
592 pwaff2 = accept_extended_affine(s, isl_pw_aff_get_space(pwaff1), v);
596 return isl_pw_aff_cond(isl_map_wrap(cond), pwaff1, pwaff2);
599 isl_pw_aff_free(pwaff1);
600 isl_pw_aff_free(pwaff2);
604 /* Accept an affine expression that may involve ternary operators.
605 * We first read an affine expression.
606 * If it is not followed by a comparison operator, we simply return it.
607 * Otherwise, we assume the affine epxression is part of the first
608 * argument of a ternary operator and try to parse that.
610 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
611 __isl_take isl_space *dim, struct vars *v)
615 struct isl_token *tok;
616 int line = -1, col = -1;
619 tok = isl_stream_next_token(s);
623 isl_stream_push_token(s, tok);
626 pwaff = accept_affine(s, dim, v);
630 tok = isl_stream_next_token(s);
632 return isl_pw_aff_free(pwaff);
634 is_comp = is_comparator(tok);
635 isl_stream_push_token(s, tok);
639 tok = isl_token_new(s->ctx, line, col, 0);
641 return isl_pw_aff_free(pwaff);
642 tok->type = ISL_TOKEN_AFF;
643 tok->u.pwaff = pwaff;
645 cond = isl_map_universe(isl_space_unwrap(isl_pw_aff_get_space(pwaff)));
647 isl_stream_push_token(s, tok);
649 cond = read_disjuncts(s, v, cond);
651 return accept_ternary(s, cond, v);
654 static __isl_give isl_map *read_var_def(struct isl_stream *s,
655 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
661 if (type == isl_dim_param)
662 pos = isl_map_dim(map, isl_dim_param);
664 pos = isl_map_dim(map, isl_dim_in);
665 if (type == isl_dim_out)
666 pos += isl_map_dim(map, isl_dim_out);
671 def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
672 def_map = isl_map_from_pw_aff(def);
673 def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
674 def_map = isl_set_unwrap(isl_map_domain(def_map));
676 map = isl_map_intersect(map, def_map);
681 static __isl_give isl_map *read_var_list(struct isl_stream *s,
682 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
685 struct isl_token *tok;
687 if (isl_stream_next_token_is(s, ']'))
690 while ((tok = next_token(s)) != NULL) {
693 if (tok->type == ISL_TOKEN_IDENT) {
695 int p = vars_pos(v, tok->u.s, -1);
702 map = isl_map_add_dims(map, type, 1);
703 map = set_name(map, type, i, v->v->name);
705 if (isl_stream_eat_if_available(s, '='))
706 map = read_var_def(s, map, type, v);
708 if (type == isl_dim_param) {
709 isl_stream_error(s, tok,
710 "expecting unique identifier");
713 isl_stream_push_token(s, tok);
715 if (vars_add_anon(v) < 0)
717 map = isl_map_add_dims(map, type, 1);
718 map = read_var_def(s, map, type, v);
721 tok = isl_stream_next_token(s);
722 if (tok && tok->type == ']' &&
723 isl_stream_next_token_is(s, '[')) {
725 tok = isl_stream_next_token(s);
726 } else if (!tok || tok->type != ',')
733 isl_stream_push_token(s, tok);
742 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
743 __isl_take isl_space *dim, struct vars *v)
746 isl_pw_aff_list *list;
747 struct isl_token *tok = NULL;
749 pwaff = accept_affine(s, isl_space_copy(dim), v);
750 list = isl_pw_aff_list_from_pw_aff(pwaff);
755 tok = isl_stream_next_token(s);
757 isl_stream_error(s, NULL, "unexpected EOF");
760 if (tok->type != ',') {
761 isl_stream_push_token(s, tok);
766 pwaff = accept_affine(s, isl_space_copy(dim), v);
767 list = isl_pw_aff_list_concat(list,
768 isl_pw_aff_list_from_pw_aff(pwaff));
777 isl_pw_aff_list_free(list);
781 static __isl_give isl_map *read_defined_var_list(struct isl_stream *s,
782 struct vars *v, __isl_take isl_map *map)
784 struct isl_token *tok;
786 while ((tok = isl_stream_next_token(s)) != NULL) {
790 if (tok->type != ISL_TOKEN_IDENT)
793 p = vars_pos(v, tok->u.s, -1);
797 isl_stream_error(s, tok, "expecting unique identifier");
801 map = isl_map_add_dims(map, isl_dim_out, 1);
804 tok = isl_stream_next_token(s);
805 if (tok && tok->type == '=') {
807 map = read_var_def(s, map, isl_dim_out, v);
808 tok = isl_stream_next_token(s);
811 if (!tok || tok->type != ',')
817 isl_stream_push_token(s, tok);
826 static int next_is_tuple(struct isl_stream *s)
828 struct isl_token *tok;
831 tok = isl_stream_next_token(s);
834 if (tok->type == '[') {
835 isl_stream_push_token(s, tok);
838 if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
839 isl_stream_push_token(s, tok);
843 is_tuple = isl_stream_next_token_is(s, '[');
845 isl_stream_push_token(s, tok);
850 static __isl_give isl_map *read_tuple(struct isl_stream *s,
851 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v);
853 static __isl_give isl_set *read_nested_tuple(struct isl_stream *s,
854 __isl_take isl_map *map, struct vars *v)
856 map = read_tuple(s, map, isl_dim_in, v);
857 if (isl_stream_eat(s, ISL_TOKEN_TO))
859 map = read_tuple(s, map, isl_dim_out, v);
860 return isl_map_wrap(map);
866 static __isl_give isl_map *read_tuple(struct isl_stream *s,
867 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
869 struct isl_token *tok;
872 tok = isl_stream_next_token(s);
873 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
874 name = strdup(tok->u.s);
878 tok = isl_stream_next_token(s);
880 if (!tok || tok->type != '[') {
881 isl_stream_error(s, tok, "expecting '['");
885 if (type != isl_dim_param && next_is_tuple(s)) {
886 isl_space *dim = isl_map_get_space(map);
887 int nparam = isl_space_dim(dim, isl_dim_param);
888 int n_in = isl_space_dim(dim, isl_dim_in);
890 if (type == isl_dim_out) {
891 dim = isl_space_move_dims(dim, isl_dim_param, nparam,
892 isl_dim_in, 0, n_in);
893 dim = isl_space_params(dim);
895 nested = read_nested_tuple(s, isl_map_universe(dim), v);
896 if (type == isl_dim_in) {
897 nested = isl_map_reverse(nested);
898 map = isl_map_intersect(nested, map);
901 dim = isl_set_get_space(nested);
902 dim = isl_space_drop_dims(dim, isl_dim_param, nparam, n_in);
903 dim = isl_space_join(isl_map_get_space(map), dim);
904 set = isl_map_domain(map);
905 nested = isl_map_reset_space(nested, dim);
906 map = isl_map_intersect_domain(nested, set);
909 map = read_var_list(s, map, type, v);
910 tok = isl_stream_next_token(s);
911 if (!tok || tok->type != ']') {
912 isl_stream_error(s, tok, "expecting ']'");
918 map = isl_map_set_tuple_name(map, type, name);
930 static __isl_give isl_set *construct_constraints(
931 __isl_take isl_set *set, enum isl_token_type type,
932 __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right)
936 if (type == ISL_TOKEN_LE)
937 cond = isl_pw_aff_list_le_set(isl_pw_aff_list_copy(left),
938 isl_pw_aff_list_copy(right));
939 else if (type == ISL_TOKEN_GE)
940 cond = isl_pw_aff_list_ge_set(isl_pw_aff_list_copy(left),
941 isl_pw_aff_list_copy(right));
942 else if (type == ISL_TOKEN_LT)
943 cond = isl_pw_aff_list_lt_set(isl_pw_aff_list_copy(left),
944 isl_pw_aff_list_copy(right));
945 else if (type == ISL_TOKEN_GT)
946 cond = isl_pw_aff_list_gt_set(isl_pw_aff_list_copy(left),
947 isl_pw_aff_list_copy(right));
948 else if (type == ISL_TOKEN_NE)
949 cond = isl_pw_aff_list_ne_set(isl_pw_aff_list_copy(left),
950 isl_pw_aff_list_copy(right));
952 cond = isl_pw_aff_list_eq_set(isl_pw_aff_list_copy(left),
953 isl_pw_aff_list_copy(right));
955 return isl_set_intersect(set, cond);
958 static __isl_give isl_map *add_constraint(struct isl_stream *s,
959 struct vars *v, __isl_take isl_map *map)
961 struct isl_token *tok = NULL;
962 isl_pw_aff_list *list1 = NULL, *list2 = NULL;
965 set = isl_map_wrap(map);
966 list1 = accept_affine_list(s, isl_set_get_space(set), v);
969 tok = isl_stream_next_token(s);
970 if (!is_comparator(tok)) {
971 isl_stream_error(s, tok, "missing operator");
973 isl_stream_push_token(s, tok);
978 list2 = accept_affine_list(s, isl_set_get_space(set), v);
982 set = construct_constraints(set, tok->type, list1, list2);
984 isl_pw_aff_list_free(list1);
987 tok = isl_stream_next_token(s);
988 if (!is_comparator(tok)) {
990 isl_stream_push_token(s, tok);
994 isl_pw_aff_list_free(list1);
996 return isl_set_unwrap(set);
1000 isl_pw_aff_list_free(list1);
1001 isl_pw_aff_list_free(list2);
1006 static __isl_give isl_map *read_exists(struct isl_stream *s,
1007 struct vars *v, __isl_take isl_map *map)
1010 int seen_paren = isl_stream_eat_if_available(s, '(');
1012 map = isl_map_from_domain(isl_map_wrap(map));
1013 map = read_defined_var_list(s, v, map);
1015 if (isl_stream_eat(s, ':'))
1018 map = read_disjuncts(s, v, map);
1019 map = isl_set_unwrap(isl_map_domain(map));
1021 vars_drop(v, v->n - n);
1022 if (seen_paren && isl_stream_eat(s, ')'))
1031 /* Parse an expression between parentheses and push the result
1032 * back on the stream.
1034 * The parsed expression may be either an affine expression
1035 * or a condition. The first type is pushed onto the stream
1036 * as an isl_pw_aff, while the second is pushed as an isl_map.
1038 * If the initial token indicates the start of a condition,
1039 * we parse it as such.
1040 * Otherwise, we first parse an affine expression and push
1041 * that onto the stream. If the affine expression covers the
1042 * entire expression between parentheses, we return.
1043 * Otherwise, we assume that the affine expression is the
1044 * start of a condition and continue parsing.
1046 static int resolve_paren_expr(struct isl_stream *s,
1047 struct vars *v, __isl_take isl_map *map)
1049 struct isl_token *tok, *tok2;
1053 tok = isl_stream_next_token(s);
1054 if (!tok || tok->type != '(')
1057 if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1058 isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
1059 isl_stream_next_token_is(s, ISL_TOKEN_FALSE)) {
1060 map = read_disjuncts(s, v, map);
1061 if (isl_stream_eat(s, ')'))
1063 tok->type = ISL_TOKEN_MAP;
1065 isl_stream_push_token(s, tok);
1069 tok2 = isl_stream_next_token(s);
1074 isl_stream_push_token(s, tok2);
1076 pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
1080 tok2 = isl_token_new(s->ctx, line, col, 0);
1083 tok2->type = ISL_TOKEN_AFF;
1084 tok2->u.pwaff = pwaff;
1086 if (isl_stream_eat_if_available(s, ')')) {
1087 isl_stream_push_token(s, tok2);
1088 isl_token_free(tok);
1093 isl_stream_push_token(s, tok2);
1095 map = read_disjuncts(s, v, map);
1096 if (isl_stream_eat(s, ')'))
1099 tok->type = ISL_TOKEN_MAP;
1101 isl_stream_push_token(s, tok);
1105 isl_pw_aff_free(pwaff);
1107 isl_token_free(tok);
1112 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
1113 struct vars *v, __isl_take isl_map *map)
1115 if (isl_stream_next_token_is(s, '('))
1116 if (resolve_paren_expr(s, v, isl_map_copy(map)))
1119 if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1120 struct isl_token *tok;
1121 tok = isl_stream_next_token(s);
1125 map = isl_map_copy(tok->u.map);
1126 isl_token_free(tok);
1130 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1131 return read_exists(s, v, map);
1133 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1136 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1137 isl_space *dim = isl_map_get_space(map);
1139 return isl_map_empty(dim);
1142 return add_constraint(s, v, map);
1148 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
1149 struct vars *v, __isl_take isl_map *map)
1154 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1155 res = read_conjunct(s, v, isl_map_copy(map));
1157 res = isl_map_subtract(isl_map_copy(map), res);
1159 while (isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
1162 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1163 res_i = read_conjunct(s, v, isl_map_copy(map));
1165 res = isl_map_subtract(res, res_i);
1167 res = isl_map_intersect(res, res_i);
1174 static struct isl_map *read_disjuncts(struct isl_stream *s,
1175 struct vars *v, __isl_take isl_map *map)
1179 if (isl_stream_next_token_is(s, '}')) {
1180 isl_space *dim = isl_map_get_space(map);
1182 return isl_map_universe(dim);
1185 res = read_conjuncts(s, v, isl_map_copy(map));
1186 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1189 res_i = read_conjuncts(s, v, isl_map_copy(map));
1190 res = isl_map_union(res, res_i);
1197 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1199 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1200 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1201 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1202 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1204 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1205 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1206 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1208 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1209 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1210 isl_basic_map_dim(bmap, isl_dim_in) +
1211 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1212 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1214 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1220 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1221 struct isl_stream *s, __isl_take isl_basic_map *bmap)
1224 struct isl_token *tok;
1234 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1235 dim = isl_basic_map_dim(bmap, isl_dim_out);
1237 tok = isl_stream_next_token(s);
1238 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1239 isl_stream_error(s, tok, "expecting coefficient");
1241 isl_stream_push_token(s, tok);
1244 if (!tok->on_new_line) {
1245 isl_stream_error(s, tok, "coefficient should appear on new line");
1246 isl_stream_push_token(s, tok);
1250 type = isl_int_get_si(tok->u.v);
1251 isl_token_free(tok);
1253 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1255 k = isl_basic_map_alloc_equality(bmap);
1258 k = isl_basic_map_alloc_inequality(bmap);
1264 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1266 tok = isl_stream_next_token(s);
1267 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1268 isl_stream_error(s, tok, "expecting coefficient");
1270 isl_stream_push_token(s, tok);
1273 if (tok->on_new_line) {
1274 isl_stream_error(s, tok,
1275 "coefficient should not appear on new line");
1276 isl_stream_push_token(s, tok);
1279 pos = polylib_pos_to_isl_pos(bmap, j);
1280 isl_int_set(c[pos], tok->u.v);
1281 isl_token_free(tok);
1286 isl_basic_map_free(bmap);
1290 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1294 struct isl_token *tok;
1295 struct isl_token *tok2;
1298 unsigned in = 0, out, local = 0;
1299 struct isl_basic_map *bmap = NULL;
1304 tok = isl_stream_next_token(s);
1306 isl_stream_error(s, NULL, "unexpected EOF");
1309 tok2 = isl_stream_next_token(s);
1311 isl_token_free(tok);
1312 isl_stream_error(s, NULL, "unexpected EOF");
1315 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1316 isl_stream_push_token(s, tok2);
1317 isl_stream_push_token(s, tok);
1318 isl_stream_error(s, NULL,
1319 "expecting constraint matrix dimensions");
1322 n_row = isl_int_get_si(tok->u.v);
1323 n_col = isl_int_get_si(tok2->u.v);
1324 on_new_line = tok2->on_new_line;
1325 isl_token_free(tok2);
1326 isl_token_free(tok);
1327 isl_assert(s->ctx, !on_new_line, return NULL);
1328 isl_assert(s->ctx, n_row >= 0, return NULL);
1329 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1330 tok = isl_stream_next_token_on_same_line(s);
1332 if (tok->type != ISL_TOKEN_VALUE) {
1333 isl_stream_error(s, tok,
1334 "expecting number of output dimensions");
1335 isl_stream_push_token(s, tok);
1338 out = isl_int_get_si(tok->u.v);
1339 isl_token_free(tok);
1341 tok = isl_stream_next_token_on_same_line(s);
1342 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1343 isl_stream_error(s, tok,
1344 "expecting number of input dimensions");
1346 isl_stream_push_token(s, tok);
1349 in = isl_int_get_si(tok->u.v);
1350 isl_token_free(tok);
1352 tok = isl_stream_next_token_on_same_line(s);
1353 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1354 isl_stream_error(s, tok,
1355 "expecting number of existentials");
1357 isl_stream_push_token(s, tok);
1360 local = isl_int_get_si(tok->u.v);
1361 isl_token_free(tok);
1363 tok = isl_stream_next_token_on_same_line(s);
1364 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1365 isl_stream_error(s, tok,
1366 "expecting number of parameters");
1368 isl_stream_push_token(s, tok);
1371 nparam = isl_int_get_si(tok->u.v);
1372 isl_token_free(tok);
1373 if (n_col != 1 + out + in + local + nparam + 1) {
1374 isl_stream_error(s, NULL,
1375 "dimensions don't match");
1379 out = n_col - 2 - nparam;
1380 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1384 for (i = 0; i < local; ++i) {
1385 int k = isl_basic_map_alloc_div(bmap);
1388 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1391 for (i = 0; i < n_row; ++i)
1392 bmap = basic_map_read_polylib_constraint(s, bmap);
1394 tok = isl_stream_next_token_on_same_line(s);
1396 isl_stream_error(s, tok, "unexpected extra token on line");
1397 isl_stream_push_token(s, tok);
1401 bmap = isl_basic_map_simplify(bmap);
1402 bmap = isl_basic_map_finalize(bmap);
1405 isl_basic_map_free(bmap);
1409 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1411 struct isl_token *tok;
1412 struct isl_token *tok2;
1414 struct isl_map *map;
1416 tok = isl_stream_next_token(s);
1418 isl_stream_error(s, NULL, "unexpected EOF");
1421 tok2 = isl_stream_next_token_on_same_line(s);
1422 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1423 isl_stream_push_token(s, tok2);
1424 isl_stream_push_token(s, tok);
1425 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1428 isl_stream_error(s, tok2, "unexpected token");
1429 isl_stream_push_token(s, tok2);
1430 isl_stream_push_token(s, tok);
1433 n = isl_int_get_si(tok->u.v);
1434 isl_token_free(tok);
1436 isl_assert(s->ctx, n >= 1, return NULL);
1438 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1440 for (i = 1; map && i < n; ++i)
1441 map = isl_map_union(map,
1442 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1447 static int optional_power(struct isl_stream *s)
1450 struct isl_token *tok;
1452 tok = isl_stream_next_token(s);
1455 if (tok->type != '^') {
1456 isl_stream_push_token(s, tok);
1459 isl_token_free(tok);
1460 tok = isl_stream_next_token(s);
1461 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1462 isl_stream_error(s, tok, "expecting exponent");
1464 isl_stream_push_token(s, tok);
1467 pow = isl_int_get_si(tok->u.v);
1468 isl_token_free(tok);
1472 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1473 __isl_keep isl_map *map, struct vars *v);
1475 static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
1476 __isl_keep isl_map *map, struct vars *v)
1478 isl_pw_qpolynomial *pwqp;
1479 struct isl_token *tok;
1481 tok = next_token(s);
1483 isl_stream_error(s, NULL, "unexpected EOF");
1486 if (tok->type == '(') {
1489 isl_token_free(tok);
1490 pwqp = read_term(s, map, v);
1493 if (isl_stream_eat(s, ')'))
1495 pow = optional_power(s);
1496 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1497 } else if (tok->type == ISL_TOKEN_VALUE) {
1498 struct isl_token *tok2;
1499 tok2 = isl_stream_next_token(s);
1500 isl_qpolynomial *qp;
1501 if (tok2 && tok2->type == '/') {
1502 isl_token_free(tok2);
1503 tok2 = next_token(s);
1504 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1505 isl_stream_error(s, tok2, "expected denominator");
1506 isl_token_free(tok);
1507 isl_token_free(tok2);
1510 qp = isl_qpolynomial_rat_cst(isl_map_get_space(map),
1511 tok->u.v, tok2->u.v);
1512 isl_token_free(tok2);
1514 isl_stream_push_token(s, tok2);
1515 qp = isl_qpolynomial_cst(isl_map_get_space(map),
1518 isl_token_free(tok);
1519 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1520 } else if (tok->type == ISL_TOKEN_INFTY) {
1521 isl_qpolynomial *qp;
1522 isl_token_free(tok);
1523 qp = isl_qpolynomial_infty(isl_map_get_space(map));
1524 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1525 } else if (tok->type == ISL_TOKEN_NAN) {
1526 isl_qpolynomial *qp;
1527 isl_token_free(tok);
1528 qp = isl_qpolynomial_nan(isl_map_get_space(map));
1529 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1530 } else if (tok->type == ISL_TOKEN_IDENT) {
1532 int pos = vars_pos(v, tok->u.s, -1);
1534 isl_qpolynomial *qp;
1536 isl_token_free(tok);
1540 vars_drop(v, v->n - n);
1541 isl_stream_error(s, tok, "unknown identifier");
1542 isl_token_free(tok);
1545 isl_token_free(tok);
1546 pow = optional_power(s);
1547 qp = isl_qpolynomial_var_pow(isl_map_get_space(map), pos, pow);
1548 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1549 } else if (tok->type == '[') {
1553 isl_stream_push_token(s, tok);
1554 pwaff = accept_affine(s, isl_map_get_space(map), v);
1555 pow = optional_power(s);
1556 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
1557 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1558 } else if (tok->type == '-') {
1559 isl_token_free(tok);
1560 pwqp = read_factor(s, map, v);
1561 pwqp = isl_pw_qpolynomial_neg(pwqp);
1563 isl_stream_error(s, tok, "unexpected isl_token");
1564 isl_stream_push_token(s, tok);
1568 if (isl_stream_eat_if_available(s, '*') ||
1569 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1570 isl_pw_qpolynomial *pwqp2;
1572 pwqp2 = read_factor(s, map, v);
1573 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
1578 isl_pw_qpolynomial_free(pwqp);
1582 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1583 __isl_keep isl_map *map, struct vars *v)
1585 struct isl_token *tok;
1586 isl_pw_qpolynomial *pwqp;
1588 pwqp = read_factor(s, map, v);
1591 tok = next_token(s);
1595 if (tok->type == '+') {
1596 isl_pw_qpolynomial *pwqp2;
1598 isl_token_free(tok);
1599 pwqp2 = read_factor(s, map, v);
1600 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1601 } else if (tok->type == '-') {
1602 isl_pw_qpolynomial *pwqp2;
1604 isl_token_free(tok);
1605 pwqp2 = read_factor(s, map, v);
1606 pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
1607 } else if (tok->type == ISL_TOKEN_VALUE &&
1608 isl_int_is_neg(tok->u.v)) {
1609 isl_pw_qpolynomial *pwqp2;
1611 isl_stream_push_token(s, tok);
1612 pwqp2 = read_factor(s, map, v);
1613 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1615 isl_stream_push_token(s, tok);
1623 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1624 __isl_take isl_map *map, struct vars *v)
1626 struct isl_token *tok;
1628 tok = isl_stream_next_token(s);
1630 isl_stream_error(s, NULL, "unexpected EOF");
1633 if (tok->type == ':' ||
1634 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1635 isl_token_free(tok);
1636 map = read_disjuncts(s, v, map);
1638 isl_stream_push_token(s, tok);
1646 static struct isl_obj obj_read_poly(struct isl_stream *s,
1647 __isl_take isl_map *map, struct vars *v, int n)
1649 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1650 isl_pw_qpolynomial *pwqp;
1651 struct isl_set *set;
1653 pwqp = read_term(s, map, v);
1654 map = read_optional_disjuncts(s, map, v);
1655 set = isl_map_range(map);
1657 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
1659 vars_drop(v, v->n - n);
1665 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1666 __isl_take isl_set *set, struct vars *v, int n)
1668 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1669 isl_pw_qpolynomial *pwqp;
1670 isl_pw_qpolynomial_fold *pwf = NULL;
1672 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1673 return obj_read_poly(s, set, v, n);
1675 if (isl_stream_eat(s, '('))
1678 pwqp = read_term(s, set, v);
1679 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1681 while (isl_stream_eat_if_available(s, ',')) {
1682 isl_pw_qpolynomial_fold *pwf_i;
1683 pwqp = read_term(s, set, v);
1684 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1686 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1689 if (isl_stream_eat(s, ')'))
1692 set = read_optional_disjuncts(s, set, v);
1693 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1695 vars_drop(v, v->n - n);
1701 isl_pw_qpolynomial_fold_free(pwf);
1702 obj.type = isl_obj_none;
1706 static int is_rational(struct isl_stream *s)
1708 struct isl_token *tok;
1710 tok = isl_stream_next_token(s);
1713 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1714 isl_token_free(tok);
1715 isl_stream_eat(s, ':');
1719 isl_stream_push_token(s, tok);
1724 static struct isl_obj obj_read_body(struct isl_stream *s,
1725 __isl_take isl_map *map, struct vars *v)
1727 struct isl_token *tok;
1728 struct isl_obj obj = { isl_obj_set, NULL };
1732 map = isl_map_set_rational(map);
1734 if (isl_stream_next_token_is(s, ':')) {
1735 obj.type = isl_obj_set;
1736 obj.v = read_optional_disjuncts(s, map, v);
1740 if (!next_is_tuple(s))
1741 return obj_read_poly_or_fold(s, map, v, n);
1743 map = read_tuple(s, map, isl_dim_in, v);
1746 tok = isl_stream_next_token(s);
1747 if (tok && tok->type == ISL_TOKEN_TO) {
1748 obj.type = isl_obj_map;
1749 isl_token_free(tok);
1750 if (!next_is_tuple(s)) {
1751 isl_set *set = isl_map_domain(map);
1752 return obj_read_poly_or_fold(s, set, v, n);
1754 map = read_tuple(s, map, isl_dim_out, v);
1758 map = isl_map_reverse(map);
1760 isl_stream_push_token(s, tok);
1763 map = read_optional_disjuncts(s, map, v);
1765 vars_drop(v, v->n - n);
1771 obj.type = isl_obj_none;
1775 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1777 if (obj.type == isl_obj_map) {
1778 obj.v = isl_union_map_from_map(obj.v);
1779 obj.type = isl_obj_union_map;
1780 } else if (obj.type == isl_obj_set) {
1781 obj.v = isl_union_set_from_set(obj.v);
1782 obj.type = isl_obj_union_set;
1783 } else if (obj.type == isl_obj_pw_qpolynomial) {
1784 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1785 obj.type = isl_obj_union_pw_qpolynomial;
1786 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1787 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1788 obj.type = isl_obj_union_pw_qpolynomial_fold;
1790 isl_assert(ctx, 0, goto error);
1793 obj.type->free(obj.v);
1794 obj.type = isl_obj_none;
1798 static struct isl_obj obj_add(struct isl_ctx *ctx,
1799 struct isl_obj obj1, struct isl_obj obj2)
1801 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1802 obj1 = to_union(ctx, obj1);
1803 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1804 obj2 = to_union(ctx, obj2);
1805 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1806 obj1 = to_union(ctx, obj1);
1807 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1808 obj2 = to_union(ctx, obj2);
1809 if (obj1.type == isl_obj_pw_qpolynomial &&
1810 obj2.type == isl_obj_union_pw_qpolynomial)
1811 obj1 = to_union(ctx, obj1);
1812 if (obj1.type == isl_obj_union_pw_qpolynomial &&
1813 obj2.type == isl_obj_pw_qpolynomial)
1814 obj2 = to_union(ctx, obj2);
1815 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1816 obj2.type == isl_obj_union_pw_qpolynomial_fold)
1817 obj1 = to_union(ctx, obj1);
1818 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1819 obj2.type == isl_obj_pw_qpolynomial_fold)
1820 obj2 = to_union(ctx, obj2);
1821 isl_assert(ctx, obj1.type == obj2.type, goto error);
1822 if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
1823 obj1 = to_union(ctx, obj1);
1824 obj2 = to_union(ctx, obj2);
1826 if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
1827 obj1 = to_union(ctx, obj1);
1828 obj2 = to_union(ctx, obj2);
1830 if (obj1.type == isl_obj_pw_qpolynomial &&
1831 !isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)) {
1832 obj1 = to_union(ctx, obj1);
1833 obj2 = to_union(ctx, obj2);
1835 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1836 !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
1837 obj1 = to_union(ctx, obj1);
1838 obj2 = to_union(ctx, obj2);
1840 obj1.v = obj1.type->add(obj1.v, obj2.v);
1843 obj1.type->free(obj1.v);
1844 obj2.type->free(obj2.v);
1845 obj1.type = isl_obj_none;
1850 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1852 isl_map *map = NULL;
1853 struct isl_token *tok;
1854 struct vars *v = NULL;
1855 struct isl_obj obj = { isl_obj_set, NULL };
1857 tok = next_token(s);
1859 isl_stream_error(s, NULL, "unexpected EOF");
1862 if (tok->type == ISL_TOKEN_VALUE) {
1863 struct isl_token *tok2;
1864 struct isl_map *map;
1866 tok2 = isl_stream_next_token(s);
1867 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
1868 isl_int_is_neg(tok2->u.v)) {
1870 isl_stream_push_token(s, tok2);
1871 obj.type = isl_obj_int;
1872 obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
1873 isl_token_free(tok);
1876 isl_stream_push_token(s, tok2);
1877 isl_stream_push_token(s, tok);
1878 map = map_read_polylib(s, nparam);
1881 if (isl_map_may_be_set(map))
1882 obj.v = isl_map_range(map);
1884 obj.type = isl_obj_map;
1889 v = vars_new(s->ctx);
1891 isl_stream_push_token(s, tok);
1894 map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
1895 if (tok->type == '[') {
1896 isl_stream_push_token(s, tok);
1897 map = read_tuple(s, map, isl_dim_param, v);
1901 isl_assert(s->ctx, nparam == v->n, goto error);
1902 tok = isl_stream_next_token(s);
1903 if (!tok || tok->type != ISL_TOKEN_TO) {
1904 isl_stream_error(s, tok, "expecting '->'");
1906 isl_stream_push_token(s, tok);
1909 isl_token_free(tok);
1910 tok = isl_stream_next_token(s);
1911 } else if (nparam > 0)
1912 map = isl_map_add_dims(map, isl_dim_param, nparam);
1913 if (!tok || tok->type != '{') {
1914 isl_stream_error(s, tok, "expecting '{'");
1916 isl_stream_push_token(s, tok);
1919 isl_token_free(tok);
1921 tok = isl_stream_next_token(s);
1924 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1925 isl_token_free(tok);
1926 if (isl_stream_eat(s, '='))
1928 map = read_tuple(s, map, isl_dim_param, v);
1932 isl_assert(s->ctx, nparam == v->n, goto error);
1933 } else if (tok->type == '}') {
1934 obj.type = isl_obj_union_set;
1935 obj.v = isl_union_set_empty(isl_map_get_space(map));
1936 isl_token_free(tok);
1939 isl_stream_push_token(s, tok);
1944 o = obj_read_body(s, isl_map_copy(map), v);
1945 if (o.type == isl_obj_none || !o.v)
1950 obj = obj_add(s->ctx, obj, o);
1951 if (obj.type == isl_obj_none || !obj.v)
1954 tok = isl_stream_next_token(s);
1955 if (!tok || tok->type != ';')
1957 isl_token_free(tok);
1958 if (isl_stream_next_token_is(s, '}')) {
1959 tok = isl_stream_next_token(s);
1964 if (tok && tok->type == '}') {
1965 isl_token_free(tok);
1967 isl_stream_error(s, tok, "unexpected isl_token");
1969 isl_token_free(tok);
1979 obj.type->free(obj.v);
1986 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1988 return obj_read(s, -1);
1991 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1995 obj = obj_read(s, nparam);
1997 isl_assert(s->ctx, obj.type == isl_obj_map ||
1998 obj.type == isl_obj_set, goto error);
2002 obj.type->free(obj.v);
2006 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
2010 obj = obj_read(s, nparam);
2012 if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
2013 obj.v = isl_map_range(obj.v);
2014 obj.type = isl_obj_set;
2016 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2021 obj.type->free(obj.v);
2025 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
2029 obj = obj_read(s, -1);
2030 if (obj.type == isl_obj_map) {
2031 obj.type = isl_obj_union_map;
2032 obj.v = isl_union_map_from_map(obj.v);
2034 if (obj.type == isl_obj_set) {
2035 obj.type = isl_obj_union_set;
2036 obj.v = isl_union_set_from_set(obj.v);
2039 isl_assert(s->ctx, obj.type == isl_obj_union_map ||
2040 obj.type == isl_obj_union_set, goto error);
2044 obj.type->free(obj.v);
2048 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
2052 obj = obj_read(s, -1);
2053 if (obj.type == isl_obj_set) {
2054 obj.type = isl_obj_union_set;
2055 obj.v = isl_union_set_from_set(obj.v);
2058 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2062 obj.type->free(obj.v);
2066 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
2069 struct isl_map *map;
2070 struct isl_basic_map *bmap;
2072 obj = obj_read(s, nparam);
2077 isl_assert(map->ctx, map->n <= 1, goto error);
2080 bmap = isl_basic_map_empty_like_map(map);
2082 bmap = isl_basic_map_copy(map->p[0]);
2092 static __isl_give isl_basic_set *basic_set_read(struct isl_stream *s,
2095 isl_basic_map *bmap;
2096 bmap = basic_map_read(s, nparam);
2099 if (!isl_basic_map_may_be_set(bmap))
2100 isl_die(s->ctx, isl_error_invalid,
2101 "input is not a set", goto error);
2102 return isl_basic_map_range(bmap);
2104 isl_basic_map_free(bmap);
2108 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2109 FILE *input, int nparam)
2111 struct isl_basic_map *bmap;
2112 struct isl_stream *s = isl_stream_new_file(ctx, input);
2115 bmap = basic_map_read(s, nparam);
2120 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2121 FILE *input, int nparam)
2123 isl_basic_set *bset;
2124 struct isl_stream *s = isl_stream_new_file(ctx, input);
2127 bset = basic_set_read(s, nparam);
2132 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2133 const char *str, int nparam)
2135 struct isl_basic_map *bmap;
2136 struct isl_stream *s = isl_stream_new_str(ctx, str);
2139 bmap = basic_map_read(s, nparam);
2144 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2145 const char *str, int nparam)
2147 isl_basic_set *bset;
2148 struct isl_stream *s = isl_stream_new_str(ctx, str);
2151 bset = basic_set_read(s, nparam);
2156 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2157 FILE *input, int nparam)
2159 struct isl_map *map;
2160 struct isl_stream *s = isl_stream_new_file(ctx, input);
2163 map = isl_stream_read_map(s, nparam);
2168 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2169 const char *str, int nparam)
2171 struct isl_map *map;
2172 struct isl_stream *s = isl_stream_new_str(ctx, str);
2175 map = isl_stream_read_map(s, nparam);
2180 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2181 FILE *input, int nparam)
2184 struct isl_stream *s = isl_stream_new_file(ctx, input);
2187 set = isl_stream_read_set(s, nparam);
2192 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2193 const char *str, int nparam)
2196 struct isl_stream *s = isl_stream_new_str(ctx, str);
2199 set = isl_stream_read_set(s, nparam);
2204 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2207 isl_union_map *umap;
2208 struct isl_stream *s = isl_stream_new_file(ctx, input);
2211 umap = isl_stream_read_union_map(s);
2216 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2219 isl_union_map *umap;
2220 struct isl_stream *s = isl_stream_new_str(ctx, str);
2223 umap = isl_stream_read_union_map(s);
2228 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2231 isl_union_set *uset;
2232 struct isl_stream *s = isl_stream_new_file(ctx, input);
2235 uset = isl_stream_read_union_set(s);
2240 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2243 isl_union_set *uset;
2244 struct isl_stream *s = isl_stream_new_str(ctx, str);
2247 uset = isl_stream_read_union_set(s);
2252 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2254 struct isl_vec *vec = NULL;
2255 struct isl_token *tok;
2259 tok = isl_stream_next_token(s);
2260 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2261 isl_stream_error(s, tok, "expecting vector length");
2265 size = isl_int_get_si(tok->u.v);
2266 isl_token_free(tok);
2268 vec = isl_vec_alloc(s->ctx, size);
2270 for (j = 0; j < size; ++j) {
2271 tok = isl_stream_next_token(s);
2272 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2273 isl_stream_error(s, tok, "expecting constant value");
2276 isl_int_set(vec->el[j], tok->u.v);
2277 isl_token_free(tok);
2282 isl_token_free(tok);
2287 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2289 return isl_vec_read_polylib(s);
2292 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2295 struct isl_stream *s = isl_stream_new_file(ctx, input);
2303 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2304 struct isl_stream *s)
2308 obj = obj_read(s, -1);
2310 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2315 obj.type->free(obj.v);
2319 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2322 isl_pw_qpolynomial *pwqp;
2323 struct isl_stream *s = isl_stream_new_str(ctx, str);
2326 pwqp = isl_stream_read_pw_qpolynomial(s);
2331 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2334 isl_pw_qpolynomial *pwqp;
2335 struct isl_stream *s = isl_stream_new_file(ctx, input);
2338 pwqp = isl_stream_read_pw_qpolynomial(s);