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_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");
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_dim(isl_dim_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_dim_copy(dim), v);
386 res = isl_pw_aff_scale(res, tok->u.v);
390 ls = isl_local_space_from_dim(isl_dim_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_dim_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_dim_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_dim_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_dim(isl_pw_aff_get_dim(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_dim *dim, struct vars *v)
458 struct isl_token *tok = NULL;
463 ls = isl_local_space_from_dim(isl_dim_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_dim_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_dim_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_dim *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_dim_wrap(isl_map_get_dim(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_dim(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_dim *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_dim_unwrap(isl_pw_aff_get_dim(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_dim_wrap(isl_map_get_dim(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_dim *dim, struct vars *v)
746 isl_pw_aff_list *list;
747 struct isl_token *tok = NULL;
749 pwaff = accept_affine(s, isl_dim_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_dim_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_map *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 map = isl_map_from_range(isl_map_wrap(map));
867 static __isl_give isl_map *read_tuple(struct isl_stream *s,
868 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
870 struct isl_token *tok;
873 tok = isl_stream_next_token(s);
874 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
875 name = strdup(tok->u.s);
879 tok = isl_stream_next_token(s);
881 if (!tok || tok->type != '[') {
882 isl_stream_error(s, tok, "expecting '['");
886 if (type != isl_dim_param && next_is_tuple(s)) {
887 isl_dim *dim = isl_map_get_dim(map);
888 int nparam = isl_dim_size(dim, isl_dim_param);
889 int n_in = isl_dim_size(dim, isl_dim_in);
891 if (type == isl_dim_out)
892 dim = isl_dim_move(dim, isl_dim_param, nparam,
893 isl_dim_in, 0, n_in);
894 nested = isl_map_universe(dim);
895 nested = read_nested_tuple(s, nested, v);
896 if (type == isl_dim_in) {
897 nested = isl_map_reverse(nested);
898 map = isl_map_intersect(nested, map);
901 dim = isl_dim_range(isl_map_get_dim(nested));
902 dim = isl_dim_drop(dim, isl_dim_param, nparam, n_in);
903 dim = isl_dim_join(isl_map_get_dim(map), dim);
904 set = isl_map_domain(map);
905 nested = isl_map_reset_dim(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_dim(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_dim(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_dim_wrap(isl_map_get_dim(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_dim *dim = isl_map_get_dim(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_dim *dim = isl_map_get_dim(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_dim(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_dim(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_dim(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_dim(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_dim(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_dim(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_map *map, 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;
1673 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1674 return obj_read_poly(s, map, v, n);
1676 if (isl_stream_eat(s, '('))
1679 pwqp = read_term(s, map, v);
1680 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1682 while (isl_stream_eat_if_available(s, ',')) {
1683 isl_pw_qpolynomial_fold *pwf_i;
1684 pwqp = read_term(s, map, v);
1685 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1687 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1690 if (isl_stream_eat(s, ')'))
1693 map = read_optional_disjuncts(s, map, v);
1694 set = isl_map_range(map);
1695 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1697 vars_drop(v, v->n - n);
1703 isl_pw_qpolynomial_fold_free(pwf);
1704 obj.type = isl_obj_none;
1708 static int is_rational(struct isl_stream *s)
1710 struct isl_token *tok;
1712 tok = isl_stream_next_token(s);
1715 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1716 isl_token_free(tok);
1717 isl_stream_eat(s, ':');
1721 isl_stream_push_token(s, tok);
1726 static struct isl_obj obj_read_body(struct isl_stream *s,
1727 __isl_take isl_map *map, struct vars *v)
1729 struct isl_token *tok;
1730 struct isl_obj obj = { isl_obj_set, NULL };
1734 map = isl_map_set_rational(map);
1736 if (!next_is_tuple(s))
1737 return obj_read_poly_or_fold(s, map, v, n);
1739 map = read_tuple(s, map, isl_dim_in, v);
1742 tok = isl_stream_next_token(s);
1743 if (tok && tok->type == ISL_TOKEN_TO) {
1744 obj.type = isl_obj_map;
1745 isl_token_free(tok);
1746 if (!next_is_tuple(s)) {
1747 map = isl_map_reverse(map);
1748 return obj_read_poly_or_fold(s, map, v, n);
1750 map = read_tuple(s, map, isl_dim_out, v);
1754 map = isl_map_reverse(map);
1756 isl_stream_push_token(s, tok);
1759 map = read_optional_disjuncts(s, map, v);
1761 vars_drop(v, v->n - n);
1767 obj.type = isl_obj_none;
1771 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1773 if (obj.type == isl_obj_map) {
1774 obj.v = isl_union_map_from_map(obj.v);
1775 obj.type = isl_obj_union_map;
1776 } else if (obj.type == isl_obj_set) {
1777 obj.v = isl_union_set_from_set(obj.v);
1778 obj.type = isl_obj_union_set;
1779 } else if (obj.type == isl_obj_pw_qpolynomial) {
1780 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1781 obj.type = isl_obj_union_pw_qpolynomial;
1782 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1783 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1784 obj.type = isl_obj_union_pw_qpolynomial_fold;
1786 isl_assert(ctx, 0, goto error);
1789 obj.type->free(obj.v);
1790 obj.type = isl_obj_none;
1794 static struct isl_obj obj_add(struct isl_ctx *ctx,
1795 struct isl_obj obj1, struct isl_obj obj2)
1797 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1798 obj1 = to_union(ctx, obj1);
1799 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1800 obj2 = to_union(ctx, obj2);
1801 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1802 obj1 = to_union(ctx, obj1);
1803 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1804 obj2 = to_union(ctx, obj2);
1805 if (obj1.type == isl_obj_pw_qpolynomial &&
1806 obj2.type == isl_obj_union_pw_qpolynomial)
1807 obj1 = to_union(ctx, obj1);
1808 if (obj1.type == isl_obj_union_pw_qpolynomial &&
1809 obj2.type == isl_obj_pw_qpolynomial)
1810 obj2 = to_union(ctx, obj2);
1811 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1812 obj2.type == isl_obj_union_pw_qpolynomial_fold)
1813 obj1 = to_union(ctx, obj1);
1814 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1815 obj2.type == isl_obj_pw_qpolynomial_fold)
1816 obj2 = to_union(ctx, obj2);
1817 isl_assert(ctx, obj1.type == obj2.type, goto error);
1818 if (obj1.type == isl_obj_map && !isl_map_has_equal_dim(obj1.v, obj2.v)) {
1819 obj1 = to_union(ctx, obj1);
1820 obj2 = to_union(ctx, obj2);
1822 if (obj1.type == isl_obj_set && !isl_set_has_equal_dim(obj1.v, obj2.v)) {
1823 obj1 = to_union(ctx, obj1);
1824 obj2 = to_union(ctx, obj2);
1826 if (obj1.type == isl_obj_pw_qpolynomial &&
1827 !isl_pw_qpolynomial_has_equal_dim(obj1.v, obj2.v)) {
1828 obj1 = to_union(ctx, obj1);
1829 obj2 = to_union(ctx, obj2);
1831 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1832 !isl_pw_qpolynomial_fold_has_equal_dim(obj1.v, obj2.v)) {
1833 obj1 = to_union(ctx, obj1);
1834 obj2 = to_union(ctx, obj2);
1836 obj1.v = obj1.type->add(obj1.v, obj2.v);
1839 obj1.type->free(obj1.v);
1840 obj2.type->free(obj2.v);
1841 obj1.type = isl_obj_none;
1846 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1848 isl_map *map = NULL;
1849 struct isl_token *tok;
1850 struct vars *v = NULL;
1851 struct isl_obj obj = { isl_obj_set, NULL };
1853 tok = next_token(s);
1855 isl_stream_error(s, NULL, "unexpected EOF");
1858 if (tok->type == ISL_TOKEN_VALUE) {
1859 struct isl_token *tok2;
1860 struct isl_map *map;
1862 tok2 = isl_stream_next_token(s);
1863 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
1864 isl_int_is_neg(tok2->u.v)) {
1866 isl_stream_push_token(s, tok2);
1867 obj.type = isl_obj_int;
1868 obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
1869 isl_token_free(tok);
1872 isl_stream_push_token(s, tok2);
1873 isl_stream_push_token(s, tok);
1874 map = map_read_polylib(s, nparam);
1877 if (isl_map_dim(map, isl_dim_in) > 0)
1878 obj.type = isl_obj_map;
1882 v = vars_new(s->ctx);
1884 isl_stream_push_token(s, tok);
1887 map = isl_map_universe(isl_dim_alloc(s->ctx, 0, 0, 0));
1888 if (tok->type == '[') {
1889 isl_stream_push_token(s, tok);
1890 map = read_tuple(s, map, isl_dim_param, v);
1894 isl_assert(s->ctx, nparam == v->n, goto error);
1895 tok = isl_stream_next_token(s);
1896 if (!tok || tok->type != ISL_TOKEN_TO) {
1897 isl_stream_error(s, tok, "expecting '->'");
1899 isl_stream_push_token(s, tok);
1902 isl_token_free(tok);
1903 tok = isl_stream_next_token(s);
1904 } else if (nparam > 0)
1905 map = isl_map_add_dims(map, isl_dim_param, nparam);
1906 if (!tok || tok->type != '{') {
1907 isl_stream_error(s, tok, "expecting '{'");
1909 isl_stream_push_token(s, tok);
1912 isl_token_free(tok);
1914 tok = isl_stream_next_token(s);
1917 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1918 isl_token_free(tok);
1919 if (isl_stream_eat(s, '='))
1921 map = read_tuple(s, map, isl_dim_param, v);
1925 isl_assert(s->ctx, nparam == v->n, goto error);
1926 } else if (tok->type == '}') {
1927 obj.type = isl_obj_union_set;
1928 obj.v = isl_union_set_empty(isl_map_get_dim(map));
1929 isl_token_free(tok);
1932 isl_stream_push_token(s, tok);
1937 o = obj_read_body(s, isl_map_copy(map), v);
1938 if (o.type == isl_obj_none || !o.v)
1943 obj = obj_add(s->ctx, obj, o);
1944 if (obj.type == isl_obj_none || !obj.v)
1947 tok = isl_stream_next_token(s);
1948 if (!tok || tok->type != ';')
1950 isl_token_free(tok);
1951 if (isl_stream_next_token_is(s, '}')) {
1952 tok = isl_stream_next_token(s);
1957 if (tok && tok->type == '}') {
1958 isl_token_free(tok);
1960 isl_stream_error(s, tok, "unexpected isl_token");
1962 isl_token_free(tok);
1972 obj.type->free(obj.v);
1979 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1981 return obj_read(s, -1);
1984 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1988 obj = obj_read(s, nparam);
1990 isl_assert(s->ctx, obj.type == isl_obj_map ||
1991 obj.type == isl_obj_set, goto error);
1995 obj.type->free(obj.v);
1999 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
2003 obj = obj_read(s, nparam);
2005 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2009 obj.type->free(obj.v);
2013 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
2017 obj = obj_read(s, -1);
2018 if (obj.type == isl_obj_map) {
2019 obj.type = isl_obj_union_map;
2020 obj.v = isl_union_map_from_map(obj.v);
2022 if (obj.type == isl_obj_set) {
2023 obj.type = isl_obj_union_set;
2024 obj.v = isl_union_set_from_set(obj.v);
2027 isl_assert(s->ctx, obj.type == isl_obj_union_map ||
2028 obj.type == isl_obj_union_set, goto error);
2032 obj.type->free(obj.v);
2036 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
2040 obj = obj_read(s, -1);
2041 if (obj.type == isl_obj_set) {
2042 obj.type = isl_obj_union_set;
2043 obj.v = isl_union_set_from_set(obj.v);
2046 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2050 obj.type->free(obj.v);
2054 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
2057 struct isl_map *map;
2058 struct isl_basic_map *bmap;
2060 obj = obj_read(s, nparam);
2065 isl_assert(map->ctx, map->n <= 1, goto error);
2068 bmap = isl_basic_map_empty_like_map(map);
2070 bmap = isl_basic_map_copy(map->p[0]);
2080 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2081 FILE *input, int nparam)
2083 struct isl_basic_map *bmap;
2084 struct isl_stream *s = isl_stream_new_file(ctx, input);
2087 bmap = basic_map_read(s, nparam);
2092 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2093 FILE *input, int nparam)
2095 struct isl_basic_map *bmap;
2096 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
2099 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
2100 return (struct isl_basic_set *)bmap;
2102 isl_basic_map_free(bmap);
2106 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2107 const char *str, int nparam)
2109 struct isl_basic_map *bmap;
2110 struct isl_stream *s = isl_stream_new_str(ctx, str);
2113 bmap = basic_map_read(s, nparam);
2118 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2119 const char *str, int nparam)
2121 struct isl_basic_map *bmap;
2122 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
2125 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
2126 return (struct isl_basic_set *)bmap;
2128 isl_basic_map_free(bmap);
2132 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2133 FILE *input, int nparam)
2135 struct isl_map *map;
2136 struct isl_stream *s = isl_stream_new_file(ctx, input);
2139 map = isl_stream_read_map(s, nparam);
2144 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2145 const char *str, int nparam)
2147 struct isl_map *map;
2148 struct isl_stream *s = isl_stream_new_str(ctx, str);
2151 map = isl_stream_read_map(s, nparam);
2156 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2157 FILE *input, int nparam)
2159 struct isl_map *map;
2160 map = isl_map_read_from_file(ctx, input, nparam);
2163 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
2164 return (struct isl_set *)map;
2170 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2171 const char *str, int nparam)
2173 struct isl_map *map;
2174 map = isl_map_read_from_str(ctx, str, nparam);
2177 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
2178 return (struct isl_set *)map;
2184 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2187 isl_union_map *umap;
2188 struct isl_stream *s = isl_stream_new_file(ctx, input);
2191 umap = isl_stream_read_union_map(s);
2196 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2199 isl_union_map *umap;
2200 struct isl_stream *s = isl_stream_new_str(ctx, str);
2203 umap = isl_stream_read_union_map(s);
2208 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2211 isl_union_set *uset;
2212 struct isl_stream *s = isl_stream_new_file(ctx, input);
2215 uset = isl_stream_read_union_set(s);
2220 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2223 isl_union_set *uset;
2224 struct isl_stream *s = isl_stream_new_str(ctx, str);
2227 uset = isl_stream_read_union_set(s);
2232 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2234 struct isl_vec *vec = NULL;
2235 struct isl_token *tok;
2239 tok = isl_stream_next_token(s);
2240 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2241 isl_stream_error(s, tok, "expecting vector length");
2245 size = isl_int_get_si(tok->u.v);
2246 isl_token_free(tok);
2248 vec = isl_vec_alloc(s->ctx, size);
2250 for (j = 0; j < size; ++j) {
2251 tok = isl_stream_next_token(s);
2252 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2253 isl_stream_error(s, tok, "expecting constant value");
2256 isl_int_set(vec->el[j], tok->u.v);
2257 isl_token_free(tok);
2262 isl_token_free(tok);
2267 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2269 return isl_vec_read_polylib(s);
2272 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2275 struct isl_stream *s = isl_stream_new_file(ctx, input);
2283 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2284 struct isl_stream *s)
2288 obj = obj_read(s, -1);
2290 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2295 obj.type->free(obj.v);
2299 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2302 isl_pw_qpolynomial *pwqp;
2303 struct isl_stream *s = isl_stream_new_str(ctx, str);
2306 pwqp = isl_stream_read_pw_qpolynomial(s);
2311 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2314 isl_pw_qpolynomial *pwqp;
2315 struct isl_stream *s = isl_stream_new_file(ctx, input);
2318 pwqp = isl_stream_read_pw_qpolynomial(s);