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.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_dim *dim, struct vars *v);
249 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
250 __isl_take isl_dim *dim, struct vars *v);
252 static __isl_give isl_pw_aff *accept_minmax(struct isl_stream *s,
253 __isl_take isl_dim *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_dim_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_dim *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_dim_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_dim *dim, struct vars *v)
353 struct isl_token *tok = NULL;
354 isl_pw_aff *res = NULL;
358 isl_stream_error(s, NULL, "unexpected EOF");
361 if (tok->type == ISL_TOKEN_IDENT) {
363 int pos = vars_pos(v, tok->u.s, -1);
369 isl_stream_error(s, tok, "unknown identifier");
373 aff = isl_aff_zero(isl_local_space_from_dim(isl_dim_copy(dim)));
376 isl_int_set_si(aff->v->el[2 + pos], 1);
377 res = isl_pw_aff_from_aff(aff);
379 } else if (tok->type == ISL_TOKEN_VALUE) {
380 if (isl_stream_eat_if_available(s, '*')) {
381 res = accept_affine_factor(s, isl_dim_copy(dim), v);
382 res = isl_pw_aff_scale(res, tok->u.v);
386 ls = isl_local_space_from_dim(isl_dim_copy(dim));
387 aff = isl_aff_zero(ls);
388 aff = isl_aff_add_constant(aff, tok->u.v);
389 res = isl_pw_aff_from_aff(aff);
392 } else if (tok->type == '(') {
395 res = accept_affine(s, isl_dim_copy(dim), v);
398 if (isl_stream_eat(s, ')'))
400 } else if (tok->type == '[' ||
401 tok->type == ISL_TOKEN_FLOORD ||
402 tok->type == ISL_TOKEN_CEILD) {
403 isl_stream_push_token(s, tok);
405 res = accept_div(s, isl_dim_copy(dim), v);
406 } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
407 isl_stream_push_token(s, tok);
409 res = accept_minmax(s, isl_dim_copy(dim), v);
411 isl_stream_error(s, tok, "expecting factor");
414 if (isl_stream_eat_if_available(s, '%')) {
416 return affine_mod(s, v, res);
418 if (isl_stream_eat_if_available(s, '*')) {
421 isl_int_set_si(f, 1);
422 if (accept_cst_factor(s, &f) < 0) {
426 res = isl_pw_aff_scale(res, f);
435 isl_pw_aff_free(res);
440 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
444 aff = isl_aff_zero(isl_local_space_from_dim(isl_pw_aff_get_dim(pwaff)));
445 aff = isl_aff_add_constant(aff, v);
447 return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
450 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
451 __isl_take isl_dim *dim, struct vars *v)
453 struct isl_token *tok = NULL;
458 ls = isl_local_space_from_dim(isl_dim_copy(dim));
459 res = isl_pw_aff_from_aff(isl_aff_zero(ls));
466 isl_stream_error(s, NULL, "unexpected EOF");
469 if (tok->type == '-') {
474 if (tok->type == '(' || tok->type == '[' ||
475 tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
476 tok->type == ISL_TOKEN_FLOORD ||
477 tok->type == ISL_TOKEN_CEILD ||
478 tok->type == ISL_TOKEN_IDENT) {
480 isl_stream_push_token(s, tok);
482 term = accept_affine_factor(s, isl_dim_copy(dim), v);
484 res = isl_pw_aff_sub(res, term);
486 res = isl_pw_aff_add(res, term);
490 } else if (tok->type == ISL_TOKEN_VALUE) {
492 isl_int_neg(tok->u.v, tok->u.v);
493 if (isl_stream_eat_if_available(s, '*') ||
494 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
496 term = accept_affine_factor(s,
497 isl_dim_copy(dim), v);
498 term = isl_pw_aff_scale(term, tok->u.v);
499 res = isl_pw_aff_add(res, term);
503 res = add_cst(res, tok->u.v);
507 isl_stream_error(s, tok, "unexpected isl_token");
508 isl_stream_push_token(s, tok);
509 isl_pw_aff_free(res);
516 if (tok && tok->type == '-') {
519 } else if (tok && tok->type == '+') {
522 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
523 isl_int_is_neg(tok->u.v)) {
524 isl_stream_push_token(s, tok);
527 isl_stream_push_token(s, tok);
537 isl_pw_aff_free(res);
541 static __isl_give isl_map *read_var_def(struct isl_stream *s,
542 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
548 pos = isl_map_dim(map, isl_dim_in);
549 if (type == isl_dim_out)
550 pos += isl_map_dim(map, isl_dim_out);
553 def = accept_affine(s, isl_dim_wrap(isl_map_get_dim(map)), v);
554 def_map = isl_map_from_pw_aff(def);
555 def_map = isl_map_equate(def_map, isl_dim_in, pos, isl_dim_out, 0);
556 def_map = isl_set_unwrap(isl_map_domain(def_map));
558 map = isl_map_intersect(map, def_map);
563 static __isl_give isl_map *read_var_list(struct isl_stream *s,
564 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
567 struct isl_token *tok;
569 if (isl_stream_next_token_is(s, ']'))
572 while ((tok = next_token(s)) != NULL) {
575 if (tok->type == ISL_TOKEN_IDENT) {
577 int p = vars_pos(v, tok->u.s, -1);
584 map = isl_map_add_dims(map, type, 1);
585 map = set_name(map, type, i, v->v->name);
588 if (type == isl_dim_param) {
589 isl_stream_error(s, tok,
590 "expecting unique identifier");
593 isl_stream_push_token(s, tok);
595 if (vars_add_anon(v) < 0)
597 map = isl_map_add_dims(map, type, 1);
598 map = read_var_def(s, map, type, v);
601 tok = isl_stream_next_token(s);
602 if (tok && tok->type == ']' &&
603 isl_stream_next_token_is(s, '[')) {
605 tok = isl_stream_next_token(s);
606 } else if (!tok || tok->type != ',')
613 isl_stream_push_token(s, tok);
622 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
623 __isl_take isl_dim *dim, struct vars *v)
626 isl_pw_aff_list *list;
627 struct isl_token *tok = NULL;
629 pwaff = accept_affine(s, isl_dim_copy(dim), v);
630 list = isl_pw_aff_list_from_pw_aff(pwaff);
635 tok = isl_stream_next_token(s);
637 isl_stream_error(s, NULL, "unexpected EOF");
640 if (tok->type != ',') {
641 isl_stream_push_token(s, tok);
646 pwaff = accept_affine(s, isl_dim_copy(dim), v);
647 list = isl_pw_aff_list_concat(list,
648 isl_pw_aff_list_from_pw_aff(pwaff));
657 isl_pw_aff_list_free(list);
661 static __isl_give isl_map *read_defined_var_list(struct isl_stream *s,
662 struct vars *v, __isl_take isl_map *map)
664 struct isl_token *tok;
666 while ((tok = isl_stream_next_token(s)) != NULL) {
670 if (tok->type != ISL_TOKEN_IDENT)
673 p = vars_pos(v, tok->u.s, -1);
677 isl_stream_error(s, tok, "expecting unique identifier");
681 map = isl_map_add_dims(map, isl_dim_out, 1);
684 tok = isl_stream_next_token(s);
685 if (tok && tok->type == '=') {
687 map = read_var_def(s, map, isl_dim_out, v);
688 tok = isl_stream_next_token(s);
691 if (!tok || tok->type != ',')
697 isl_stream_push_token(s, tok);
706 static int next_is_tuple(struct isl_stream *s)
708 struct isl_token *tok;
711 tok = isl_stream_next_token(s);
714 if (tok->type == '[') {
715 isl_stream_push_token(s, tok);
718 if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
719 isl_stream_push_token(s, tok);
723 is_tuple = isl_stream_next_token_is(s, '[');
725 isl_stream_push_token(s, tok);
730 static __isl_give isl_map *read_tuple(struct isl_stream *s,
731 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v);
733 static __isl_give isl_map *read_nested_tuple(struct isl_stream *s,
734 __isl_take isl_map *map, struct vars *v)
736 map = read_tuple(s, map, isl_dim_in, v);
737 if (isl_stream_eat(s, ISL_TOKEN_TO))
739 map = read_tuple(s, map, isl_dim_out, v);
740 map = isl_map_from_range(isl_map_wrap(map));
747 static __isl_give isl_map *read_tuple(struct isl_stream *s,
748 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
750 struct isl_token *tok;
753 tok = isl_stream_next_token(s);
754 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
755 name = strdup(tok->u.s);
759 tok = isl_stream_next_token(s);
761 if (!tok || tok->type != '[') {
762 isl_stream_error(s, tok, "expecting '['");
766 if (type != isl_dim_param && next_is_tuple(s)) {
767 isl_dim *dim = isl_map_get_dim(map);
768 int nparam = isl_dim_size(dim, isl_dim_param);
769 int n_in = isl_dim_size(dim, isl_dim_in);
771 if (type == isl_dim_out)
772 dim = isl_dim_move(dim, isl_dim_param, nparam,
773 isl_dim_in, 0, n_in);
774 nested = isl_map_universe(dim);
775 nested = read_nested_tuple(s, nested, v);
776 if (type == isl_dim_in) {
777 nested = isl_map_reverse(nested);
778 map = isl_map_intersect(nested, map);
781 dim = isl_dim_range(isl_map_get_dim(nested));
782 dim = isl_dim_drop(dim, isl_dim_param, nparam, n_in);
783 dim = isl_dim_join(isl_map_get_dim(map), dim);
784 set = isl_map_domain(map);
785 nested = isl_map_reset_dim(nested, dim);
786 map = isl_map_intersect_domain(nested, set);
789 map = read_var_list(s, map, type, v);
790 tok = isl_stream_next_token(s);
791 if (!tok || tok->type != ']') {
792 isl_stream_error(s, tok, "expecting ']'");
798 map = isl_map_set_tuple_name(map, type, name);
810 static __isl_give isl_set *construct_constraints(
811 __isl_take isl_set *set, enum isl_token_type type,
812 __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right)
816 if (type == ISL_TOKEN_LE)
817 cond = isl_pw_aff_list_le_set(isl_pw_aff_list_copy(left),
818 isl_pw_aff_list_copy(right));
819 else if (type == ISL_TOKEN_GE)
820 cond = isl_pw_aff_list_ge_set(isl_pw_aff_list_copy(left),
821 isl_pw_aff_list_copy(right));
822 else if (type == ISL_TOKEN_LT)
823 cond = isl_pw_aff_list_lt_set(isl_pw_aff_list_copy(left),
824 isl_pw_aff_list_copy(right));
825 else if (type == ISL_TOKEN_GT)
826 cond = isl_pw_aff_list_gt_set(isl_pw_aff_list_copy(left),
827 isl_pw_aff_list_copy(right));
829 cond = isl_pw_aff_list_eq_set(isl_pw_aff_list_copy(left),
830 isl_pw_aff_list_copy(right));
832 return isl_set_intersect(set, cond);
835 static int is_comparator(struct isl_token *tok)
852 static __isl_give isl_map *add_constraint(struct isl_stream *s,
853 struct vars *v, __isl_take isl_map *map)
855 struct isl_token *tok = NULL;
856 isl_pw_aff_list *list1 = NULL, *list2 = NULL;
859 set = isl_map_wrap(map);
860 list1 = accept_affine_list(s, isl_set_get_dim(set), v);
863 tok = isl_stream_next_token(s);
864 if (!is_comparator(tok)) {
865 isl_stream_error(s, tok, "missing operator");
867 isl_stream_push_token(s, tok);
872 list2 = accept_affine_list(s, isl_set_get_dim(set), v);
876 set = construct_constraints(set, tok->type, list1, list2);
878 isl_pw_aff_list_free(list1);
881 tok = isl_stream_next_token(s);
882 if (!is_comparator(tok)) {
884 isl_stream_push_token(s, tok);
888 isl_pw_aff_list_free(list1);
890 return isl_set_unwrap(set);
894 isl_pw_aff_list_free(list1);
895 isl_pw_aff_list_free(list2);
900 static struct isl_map *read_disjuncts(struct isl_stream *s,
901 struct vars *v, __isl_take isl_map *map);
903 static __isl_give isl_map *read_exists(struct isl_stream *s,
904 struct vars *v, __isl_take isl_map *map)
907 int seen_paren = isl_stream_eat_if_available(s, '(');
909 map = isl_map_from_domain(isl_map_wrap(map));
910 map = read_defined_var_list(s, v, map);
912 if (isl_stream_eat(s, ':'))
915 map = read_disjuncts(s, v, map);
916 map = isl_set_unwrap(isl_map_domain(map));
918 vars_drop(v, v->n - n);
919 if (seen_paren && isl_stream_eat(s, ')'))
928 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
929 struct vars *v, __isl_take isl_map *map)
931 if (isl_stream_eat_if_available(s, '(')) {
932 map = read_disjuncts(s, v, map);
933 if (isl_stream_eat(s, ')'))
938 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
939 return read_exists(s, v, map);
941 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
944 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
945 isl_dim *dim = isl_map_get_dim(map);
947 return isl_map_empty(dim);
950 return add_constraint(s, v, map);
956 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
957 struct vars *v, __isl_take isl_map *map)
962 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
963 res = read_conjunct(s, v, isl_map_copy(map));
965 res = isl_map_subtract(isl_map_copy(map), res);
967 while (isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
970 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
971 res_i = read_conjunct(s, v, isl_map_copy(map));
973 res = isl_map_subtract(res, res_i);
975 res = isl_map_intersect(res, res_i);
982 static struct isl_map *read_disjuncts(struct isl_stream *s,
983 struct vars *v, __isl_take isl_map *map)
987 if (isl_stream_next_token_is(s, '}')) {
988 isl_dim *dim = isl_map_get_dim(map);
990 return isl_map_universe(dim);
993 res = read_conjuncts(s, v, isl_map_copy(map));
994 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
997 res_i = read_conjuncts(s, v, isl_map_copy(map));
998 res = isl_map_union(res, res_i);
1005 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1007 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1008 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1009 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1010 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1012 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1013 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1014 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1016 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1017 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1018 isl_basic_map_dim(bmap, isl_dim_in) +
1019 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1020 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1022 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1028 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1029 struct isl_stream *s, __isl_take isl_basic_map *bmap)
1032 struct isl_token *tok;
1042 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1043 dim = isl_basic_map_dim(bmap, isl_dim_out);
1045 tok = isl_stream_next_token(s);
1046 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1047 isl_stream_error(s, tok, "expecting coefficient");
1049 isl_stream_push_token(s, tok);
1052 if (!tok->on_new_line) {
1053 isl_stream_error(s, tok, "coefficient should appear on new line");
1054 isl_stream_push_token(s, tok);
1058 type = isl_int_get_si(tok->u.v);
1059 isl_token_free(tok);
1061 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1063 k = isl_basic_map_alloc_equality(bmap);
1066 k = isl_basic_map_alloc_inequality(bmap);
1072 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1074 tok = isl_stream_next_token(s);
1075 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1076 isl_stream_error(s, tok, "expecting coefficient");
1078 isl_stream_push_token(s, tok);
1081 if (tok->on_new_line) {
1082 isl_stream_error(s, tok,
1083 "coefficient should not appear on new line");
1084 isl_stream_push_token(s, tok);
1087 pos = polylib_pos_to_isl_pos(bmap, j);
1088 isl_int_set(c[pos], tok->u.v);
1089 isl_token_free(tok);
1094 isl_basic_map_free(bmap);
1098 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1102 struct isl_token *tok;
1103 struct isl_token *tok2;
1106 unsigned in = 0, out, local = 0;
1107 struct isl_basic_map *bmap = NULL;
1112 tok = isl_stream_next_token(s);
1114 isl_stream_error(s, NULL, "unexpected EOF");
1117 tok2 = isl_stream_next_token(s);
1119 isl_token_free(tok);
1120 isl_stream_error(s, NULL, "unexpected EOF");
1123 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1124 isl_stream_push_token(s, tok2);
1125 isl_stream_push_token(s, tok);
1126 isl_stream_error(s, NULL,
1127 "expecting constraint matrix dimensions");
1130 n_row = isl_int_get_si(tok->u.v);
1131 n_col = isl_int_get_si(tok2->u.v);
1132 on_new_line = tok2->on_new_line;
1133 isl_token_free(tok2);
1134 isl_token_free(tok);
1135 isl_assert(s->ctx, !on_new_line, return NULL);
1136 isl_assert(s->ctx, n_row >= 0, return NULL);
1137 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1138 tok = isl_stream_next_token_on_same_line(s);
1140 if (tok->type != ISL_TOKEN_VALUE) {
1141 isl_stream_error(s, tok,
1142 "expecting number of output dimensions");
1143 isl_stream_push_token(s, tok);
1146 out = isl_int_get_si(tok->u.v);
1147 isl_token_free(tok);
1149 tok = isl_stream_next_token_on_same_line(s);
1150 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1151 isl_stream_error(s, tok,
1152 "expecting number of input dimensions");
1154 isl_stream_push_token(s, tok);
1157 in = isl_int_get_si(tok->u.v);
1158 isl_token_free(tok);
1160 tok = isl_stream_next_token_on_same_line(s);
1161 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1162 isl_stream_error(s, tok,
1163 "expecting number of existentials");
1165 isl_stream_push_token(s, tok);
1168 local = isl_int_get_si(tok->u.v);
1169 isl_token_free(tok);
1171 tok = isl_stream_next_token_on_same_line(s);
1172 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1173 isl_stream_error(s, tok,
1174 "expecting number of parameters");
1176 isl_stream_push_token(s, tok);
1179 nparam = isl_int_get_si(tok->u.v);
1180 isl_token_free(tok);
1181 if (n_col != 1 + out + in + local + nparam + 1) {
1182 isl_stream_error(s, NULL,
1183 "dimensions don't match");
1187 out = n_col - 2 - nparam;
1188 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1192 for (i = 0; i < local; ++i) {
1193 int k = isl_basic_map_alloc_div(bmap);
1196 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1199 for (i = 0; i < n_row; ++i)
1200 bmap = basic_map_read_polylib_constraint(s, bmap);
1202 tok = isl_stream_next_token_on_same_line(s);
1204 isl_stream_error(s, tok, "unexpected extra token on line");
1205 isl_stream_push_token(s, tok);
1209 bmap = isl_basic_map_simplify(bmap);
1210 bmap = isl_basic_map_finalize(bmap);
1213 isl_basic_map_free(bmap);
1217 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1219 struct isl_token *tok;
1220 struct isl_token *tok2;
1222 struct isl_map *map;
1224 tok = isl_stream_next_token(s);
1226 isl_stream_error(s, NULL, "unexpected EOF");
1229 tok2 = isl_stream_next_token_on_same_line(s);
1230 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1231 isl_stream_push_token(s, tok2);
1232 isl_stream_push_token(s, tok);
1233 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1236 isl_stream_error(s, tok2, "unexpected token");
1237 isl_stream_push_token(s, tok2);
1238 isl_stream_push_token(s, tok);
1241 n = isl_int_get_si(tok->u.v);
1242 isl_token_free(tok);
1244 isl_assert(s->ctx, n >= 1, return NULL);
1246 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1248 for (i = 1; map && i < n; ++i)
1249 map = isl_map_union(map,
1250 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1255 static int optional_power(struct isl_stream *s)
1258 struct isl_token *tok;
1260 tok = isl_stream_next_token(s);
1263 if (tok->type != '^') {
1264 isl_stream_push_token(s, tok);
1267 isl_token_free(tok);
1268 tok = isl_stream_next_token(s);
1269 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1270 isl_stream_error(s, tok, "expecting exponent");
1272 isl_stream_push_token(s, tok);
1275 pow = isl_int_get_si(tok->u.v);
1276 isl_token_free(tok);
1280 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1281 __isl_keep isl_map *map, struct vars *v);
1283 static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
1284 __isl_keep isl_map *map, struct vars *v)
1286 isl_pw_qpolynomial *pwqp;
1287 struct isl_token *tok;
1289 tok = next_token(s);
1291 isl_stream_error(s, NULL, "unexpected EOF");
1294 if (tok->type == '(') {
1297 isl_token_free(tok);
1298 pwqp = read_term(s, map, v);
1301 if (isl_stream_eat(s, ')'))
1303 pow = optional_power(s);
1304 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1305 } else if (tok->type == ISL_TOKEN_VALUE) {
1306 struct isl_token *tok2;
1307 tok2 = isl_stream_next_token(s);
1308 isl_qpolynomial *qp;
1309 if (tok2 && tok2->type == '/') {
1310 isl_token_free(tok2);
1311 tok2 = next_token(s);
1312 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1313 isl_stream_error(s, tok2, "expected denominator");
1314 isl_token_free(tok);
1315 isl_token_free(tok2);
1318 qp = isl_qpolynomial_rat_cst(isl_map_get_dim(map),
1319 tok->u.v, tok2->u.v);
1320 isl_token_free(tok2);
1322 isl_stream_push_token(s, tok2);
1323 qp = isl_qpolynomial_cst(isl_map_get_dim(map),
1326 isl_token_free(tok);
1327 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1328 } else if (tok->type == ISL_TOKEN_INFTY) {
1329 isl_qpolynomial *qp;
1330 isl_token_free(tok);
1331 qp = isl_qpolynomial_infty(isl_map_get_dim(map));
1332 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1333 } else if (tok->type == ISL_TOKEN_NAN) {
1334 isl_qpolynomial *qp;
1335 isl_token_free(tok);
1336 qp = isl_qpolynomial_nan(isl_map_get_dim(map));
1337 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1338 } else if (tok->type == ISL_TOKEN_IDENT) {
1340 int pos = vars_pos(v, tok->u.s, -1);
1342 isl_qpolynomial *qp;
1344 isl_token_free(tok);
1348 vars_drop(v, v->n - n);
1349 isl_stream_error(s, tok, "unknown identifier");
1350 isl_token_free(tok);
1353 isl_token_free(tok);
1354 pow = optional_power(s);
1355 qp = isl_qpolynomial_var_pow(isl_map_get_dim(map), pos, pow);
1356 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1357 } else if (tok->type == '[') {
1361 isl_stream_push_token(s, tok);
1362 pwaff = accept_affine(s, isl_map_get_dim(map), v);
1363 pow = optional_power(s);
1364 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
1365 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1366 } else if (tok->type == '-') {
1367 isl_token_free(tok);
1368 pwqp = read_factor(s, map, v);
1369 pwqp = isl_pw_qpolynomial_neg(pwqp);
1371 isl_stream_error(s, tok, "unexpected isl_token");
1372 isl_stream_push_token(s, tok);
1376 if (isl_stream_eat_if_available(s, '*') ||
1377 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1378 isl_pw_qpolynomial *pwqp2;
1380 pwqp2 = read_factor(s, map, v);
1381 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
1386 isl_pw_qpolynomial_free(pwqp);
1390 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1391 __isl_keep isl_map *map, struct vars *v)
1393 struct isl_token *tok;
1394 isl_pw_qpolynomial *pwqp;
1396 pwqp = read_factor(s, map, v);
1399 tok = next_token(s);
1403 if (tok->type == '+') {
1404 isl_pw_qpolynomial *pwqp2;
1406 isl_token_free(tok);
1407 pwqp2 = read_factor(s, map, v);
1408 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1409 } else if (tok->type == '-') {
1410 isl_pw_qpolynomial *pwqp2;
1412 isl_token_free(tok);
1413 pwqp2 = read_factor(s, map, v);
1414 pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
1415 } else if (tok->type == ISL_TOKEN_VALUE &&
1416 isl_int_is_neg(tok->u.v)) {
1417 isl_pw_qpolynomial *pwqp2;
1419 isl_stream_push_token(s, tok);
1420 pwqp2 = read_factor(s, map, v);
1421 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1423 isl_stream_push_token(s, tok);
1431 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1432 __isl_take isl_map *map, struct vars *v)
1434 struct isl_token *tok;
1436 tok = isl_stream_next_token(s);
1438 isl_stream_error(s, NULL, "unexpected EOF");
1441 if (tok->type == ':' ||
1442 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1443 isl_token_free(tok);
1444 map = read_disjuncts(s, v, map);
1446 isl_stream_push_token(s, tok);
1454 static struct isl_obj obj_read_poly(struct isl_stream *s,
1455 __isl_take isl_map *map, struct vars *v, int n)
1457 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1458 isl_pw_qpolynomial *pwqp;
1459 struct isl_set *set;
1461 pwqp = read_term(s, map, v);
1462 map = read_optional_disjuncts(s, map, v);
1463 set = isl_map_range(map);
1465 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
1467 vars_drop(v, v->n - n);
1473 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1474 __isl_take isl_map *map, struct vars *v, int n)
1476 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1477 isl_pw_qpolynomial *pwqp;
1478 isl_pw_qpolynomial_fold *pwf = NULL;
1481 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1482 return obj_read_poly(s, map, v, n);
1484 if (isl_stream_eat(s, '('))
1487 pwqp = read_term(s, map, v);
1488 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1490 while (isl_stream_eat_if_available(s, ',')) {
1491 isl_pw_qpolynomial_fold *pwf_i;
1492 pwqp = read_term(s, map, v);
1493 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1495 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1498 if (isl_stream_eat(s, ')'))
1501 map = read_optional_disjuncts(s, map, v);
1502 set = isl_map_range(map);
1503 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1505 vars_drop(v, v->n - n);
1511 isl_pw_qpolynomial_fold_free(pwf);
1512 obj.type = isl_obj_none;
1516 static int is_rational(struct isl_stream *s)
1518 struct isl_token *tok;
1520 tok = isl_stream_next_token(s);
1523 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1524 isl_token_free(tok);
1525 isl_stream_eat(s, ':');
1529 isl_stream_push_token(s, tok);
1534 static struct isl_obj obj_read_body(struct isl_stream *s,
1535 __isl_take isl_map *map, struct vars *v)
1537 struct isl_token *tok;
1538 struct isl_obj obj = { isl_obj_set, NULL };
1542 map = isl_map_set_rational(map);
1544 if (!next_is_tuple(s))
1545 return obj_read_poly_or_fold(s, map, v, n);
1547 map = read_tuple(s, map, isl_dim_in, v);
1550 tok = isl_stream_next_token(s);
1551 if (tok && tok->type == ISL_TOKEN_TO) {
1552 obj.type = isl_obj_map;
1553 isl_token_free(tok);
1554 if (!next_is_tuple(s)) {
1555 map = isl_map_reverse(map);
1556 return obj_read_poly_or_fold(s, map, v, n);
1558 map = read_tuple(s, map, isl_dim_out, v);
1562 map = isl_map_reverse(map);
1564 isl_stream_push_token(s, tok);
1567 map = read_optional_disjuncts(s, map, v);
1569 vars_drop(v, v->n - n);
1575 obj.type = isl_obj_none;
1579 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1581 if (obj.type == isl_obj_map) {
1582 obj.v = isl_union_map_from_map(obj.v);
1583 obj.type = isl_obj_union_map;
1584 } else if (obj.type == isl_obj_set) {
1585 obj.v = isl_union_set_from_set(obj.v);
1586 obj.type = isl_obj_union_set;
1587 } else if (obj.type == isl_obj_pw_qpolynomial) {
1588 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1589 obj.type = isl_obj_union_pw_qpolynomial;
1590 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1591 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1592 obj.type = isl_obj_union_pw_qpolynomial_fold;
1594 isl_assert(ctx, 0, goto error);
1597 obj.type->free(obj.v);
1598 obj.type = isl_obj_none;
1602 static struct isl_obj obj_add(struct isl_ctx *ctx,
1603 struct isl_obj obj1, struct isl_obj obj2)
1605 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1606 obj1 = to_union(ctx, obj1);
1607 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1608 obj2 = to_union(ctx, obj2);
1609 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1610 obj1 = to_union(ctx, obj1);
1611 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1612 obj2 = to_union(ctx, obj2);
1613 if (obj1.type == isl_obj_pw_qpolynomial &&
1614 obj2.type == isl_obj_union_pw_qpolynomial)
1615 obj1 = to_union(ctx, obj1);
1616 if (obj1.type == isl_obj_union_pw_qpolynomial &&
1617 obj2.type == isl_obj_pw_qpolynomial)
1618 obj2 = to_union(ctx, obj2);
1619 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1620 obj2.type == isl_obj_union_pw_qpolynomial_fold)
1621 obj1 = to_union(ctx, obj1);
1622 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1623 obj2.type == isl_obj_pw_qpolynomial_fold)
1624 obj2 = to_union(ctx, obj2);
1625 isl_assert(ctx, obj1.type == obj2.type, goto error);
1626 if (obj1.type == isl_obj_map && !isl_map_has_equal_dim(obj1.v, obj2.v)) {
1627 obj1 = to_union(ctx, obj1);
1628 obj2 = to_union(ctx, obj2);
1630 if (obj1.type == isl_obj_set && !isl_set_has_equal_dim(obj1.v, obj2.v)) {
1631 obj1 = to_union(ctx, obj1);
1632 obj2 = to_union(ctx, obj2);
1634 if (obj1.type == isl_obj_pw_qpolynomial &&
1635 !isl_pw_qpolynomial_has_equal_dim(obj1.v, obj2.v)) {
1636 obj1 = to_union(ctx, obj1);
1637 obj2 = to_union(ctx, obj2);
1639 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1640 !isl_pw_qpolynomial_fold_has_equal_dim(obj1.v, obj2.v)) {
1641 obj1 = to_union(ctx, obj1);
1642 obj2 = to_union(ctx, obj2);
1644 obj1.v = obj1.type->add(obj1.v, obj2.v);
1647 obj1.type->free(obj1.v);
1648 obj2.type->free(obj2.v);
1649 obj1.type = isl_obj_none;
1654 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1656 isl_map *map = NULL;
1657 struct isl_token *tok;
1658 struct vars *v = NULL;
1659 struct isl_obj obj = { isl_obj_set, NULL };
1661 tok = next_token(s);
1663 isl_stream_error(s, NULL, "unexpected EOF");
1666 if (tok->type == ISL_TOKEN_VALUE) {
1667 struct isl_token *tok2;
1668 struct isl_map *map;
1670 tok2 = isl_stream_next_token(s);
1671 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
1672 isl_int_is_neg(tok2->u.v)) {
1674 isl_stream_push_token(s, tok2);
1675 obj.type = isl_obj_int;
1676 obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
1677 isl_token_free(tok);
1680 isl_stream_push_token(s, tok2);
1681 isl_stream_push_token(s, tok);
1682 map = map_read_polylib(s, nparam);
1685 if (isl_map_dim(map, isl_dim_in) > 0)
1686 obj.type = isl_obj_map;
1690 v = vars_new(s->ctx);
1692 isl_stream_push_token(s, tok);
1695 map = isl_map_universe(isl_dim_alloc(s->ctx, 0, 0, 0));
1696 if (tok->type == '[') {
1697 isl_stream_push_token(s, tok);
1698 map = read_tuple(s, map, isl_dim_param, v);
1702 isl_assert(s->ctx, nparam == v->n, goto error);
1703 tok = isl_stream_next_token(s);
1704 if (!tok || tok->type != ISL_TOKEN_TO) {
1705 isl_stream_error(s, tok, "expecting '->'");
1707 isl_stream_push_token(s, tok);
1710 isl_token_free(tok);
1711 tok = isl_stream_next_token(s);
1712 } else if (nparam > 0)
1713 map = isl_map_add_dims(map, isl_dim_param, nparam);
1714 if (!tok || tok->type != '{') {
1715 isl_stream_error(s, tok, "expecting '{'");
1717 isl_stream_push_token(s, tok);
1720 isl_token_free(tok);
1722 tok = isl_stream_next_token(s);
1725 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1726 isl_token_free(tok);
1727 if (isl_stream_eat(s, '='))
1729 map = read_tuple(s, map, isl_dim_param, v);
1733 isl_assert(s->ctx, nparam == v->n, goto error);
1734 } else if (tok->type == '}') {
1735 obj.type = isl_obj_union_set;
1736 obj.v = isl_union_set_empty(isl_map_get_dim(map));
1737 isl_token_free(tok);
1740 isl_stream_push_token(s, tok);
1745 o = obj_read_body(s, isl_map_copy(map), v);
1746 if (o.type == isl_obj_none || !o.v)
1751 obj = obj_add(s->ctx, obj, o);
1752 if (obj.type == isl_obj_none || !obj.v)
1755 tok = isl_stream_next_token(s);
1756 if (!tok || tok->type != ';')
1758 isl_token_free(tok);
1759 if (isl_stream_next_token_is(s, '}')) {
1760 tok = isl_stream_next_token(s);
1765 if (tok && tok->type == '}') {
1766 isl_token_free(tok);
1768 isl_stream_error(s, tok, "unexpected isl_token");
1770 isl_token_free(tok);
1780 obj.type->free(obj.v);
1787 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1789 return obj_read(s, -1);
1792 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1796 obj = obj_read(s, nparam);
1798 isl_assert(s->ctx, obj.type == isl_obj_map ||
1799 obj.type == isl_obj_set, goto error);
1803 obj.type->free(obj.v);
1807 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
1811 obj = obj_read(s, nparam);
1813 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
1817 obj.type->free(obj.v);
1821 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
1825 obj = obj_read(s, -1);
1826 if (obj.type == isl_obj_map) {
1827 obj.type = isl_obj_union_map;
1828 obj.v = isl_union_map_from_map(obj.v);
1830 if (obj.type == isl_obj_set) {
1831 obj.type = isl_obj_union_set;
1832 obj.v = isl_union_set_from_set(obj.v);
1835 isl_assert(s->ctx, obj.type == isl_obj_union_map ||
1836 obj.type == isl_obj_union_set, goto error);
1840 obj.type->free(obj.v);
1844 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
1848 obj = obj_read(s, -1);
1849 if (obj.type == isl_obj_set) {
1850 obj.type = isl_obj_union_set;
1851 obj.v = isl_union_set_from_set(obj.v);
1854 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1858 obj.type->free(obj.v);
1862 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
1865 struct isl_map *map;
1866 struct isl_basic_map *bmap;
1868 obj = obj_read(s, nparam);
1873 isl_assert(map->ctx, map->n <= 1, goto error);
1876 bmap = isl_basic_map_empty_like_map(map);
1878 bmap = isl_basic_map_copy(map->p[0]);
1888 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
1889 FILE *input, int nparam)
1891 struct isl_basic_map *bmap;
1892 struct isl_stream *s = isl_stream_new_file(ctx, input);
1895 bmap = basic_map_read(s, nparam);
1900 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
1901 FILE *input, int nparam)
1903 struct isl_basic_map *bmap;
1904 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
1907 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1908 return (struct isl_basic_set *)bmap;
1910 isl_basic_map_free(bmap);
1914 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
1915 const char *str, int nparam)
1917 struct isl_basic_map *bmap;
1918 struct isl_stream *s = isl_stream_new_str(ctx, str);
1921 bmap = basic_map_read(s, nparam);
1926 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
1927 const char *str, int nparam)
1929 struct isl_basic_map *bmap;
1930 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
1933 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1934 return (struct isl_basic_set *)bmap;
1936 isl_basic_map_free(bmap);
1940 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
1941 FILE *input, int nparam)
1943 struct isl_map *map;
1944 struct isl_stream *s = isl_stream_new_file(ctx, input);
1947 map = isl_stream_read_map(s, nparam);
1952 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
1953 const char *str, int nparam)
1955 struct isl_map *map;
1956 struct isl_stream *s = isl_stream_new_str(ctx, str);
1959 map = isl_stream_read_map(s, nparam);
1964 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
1965 FILE *input, int nparam)
1967 struct isl_map *map;
1968 map = isl_map_read_from_file(ctx, input, nparam);
1971 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1972 return (struct isl_set *)map;
1978 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
1979 const char *str, int nparam)
1981 struct isl_map *map;
1982 map = isl_map_read_from_str(ctx, str, nparam);
1985 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1986 return (struct isl_set *)map;
1992 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
1995 isl_union_map *umap;
1996 struct isl_stream *s = isl_stream_new_file(ctx, input);
1999 umap = isl_stream_read_union_map(s);
2004 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2007 isl_union_map *umap;
2008 struct isl_stream *s = isl_stream_new_str(ctx, str);
2011 umap = isl_stream_read_union_map(s);
2016 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2019 isl_union_set *uset;
2020 struct isl_stream *s = isl_stream_new_file(ctx, input);
2023 uset = isl_stream_read_union_set(s);
2028 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2031 isl_union_set *uset;
2032 struct isl_stream *s = isl_stream_new_str(ctx, str);
2035 uset = isl_stream_read_union_set(s);
2040 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2042 struct isl_vec *vec = NULL;
2043 struct isl_token *tok;
2047 tok = isl_stream_next_token(s);
2048 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2049 isl_stream_error(s, tok, "expecting vector length");
2053 size = isl_int_get_si(tok->u.v);
2054 isl_token_free(tok);
2056 vec = isl_vec_alloc(s->ctx, size);
2058 for (j = 0; j < size; ++j) {
2059 tok = isl_stream_next_token(s);
2060 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2061 isl_stream_error(s, tok, "expecting constant value");
2064 isl_int_set(vec->el[j], tok->u.v);
2065 isl_token_free(tok);
2070 isl_token_free(tok);
2075 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2077 return isl_vec_read_polylib(s);
2080 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2083 struct isl_stream *s = isl_stream_new_file(ctx, input);
2091 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2092 struct isl_stream *s)
2096 obj = obj_read(s, -1);
2098 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2103 obj.type->free(obj.v);
2107 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2110 isl_pw_qpolynomial *pwqp;
2111 struct isl_stream *s = isl_stream_new_str(ctx, str);
2114 pwqp = isl_stream_read_pw_qpolynomial(s);
2119 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2122 isl_pw_qpolynomial *pwqp;
2123 struct isl_stream *s = isl_stream_new_file(ctx, input);
2126 pwqp = isl_stream_read_pw_qpolynomial(s);