doc: add a note about isl_*_intersect and parametric sets
[platform/upstream/isl.git] / isl_input.c
index ce9b04b..1282224 100644 (file)
@@ -17,8 +17,7 @@
 #include <isl_map_private.h>
 #include <isl/set.h>
 #include <isl/seq.h>
-#include <isl/div.h>
-#include <isl/stream.h>
+#include <isl_stream_private.h>
 #include <isl/obj.h>
 #include "isl_polynomial_private.h"
 #include <isl/union_map.h>
@@ -245,12 +244,12 @@ error:
 }
 
 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
-       __isl_take isl_dim *dim, struct vars *v);
+       __isl_take isl_space *dim, struct vars *v);
 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
-       __isl_take isl_dim *dim, struct vars *v);
+       __isl_take isl_space *dim, struct vars *v);
 
 static __isl_give isl_pw_aff *accept_minmax(struct isl_stream *s,
-       __isl_take isl_dim *dim, struct vars *v)
+       __isl_take isl_space *dim, struct vars *v)
 {
        struct isl_token *tok;
        isl_pw_aff_list *list = NULL;
@@ -265,23 +264,23 @@ static __isl_give isl_pw_aff *accept_minmax(struct isl_stream *s,
        if (isl_stream_eat(s, '('))
                goto error;
 
-       list = accept_affine_list(s, isl_dim_copy(dim), v);
+       list = accept_affine_list(s, isl_space_copy(dim), v);
        if (!list)
                goto error;
 
        if (isl_stream_eat(s, ')'))
                goto error;
 
-       isl_dim_free(dim);
+       isl_space_free(dim);
        return min ? isl_pw_aff_list_min(list) : isl_pw_aff_list_max(list);
 error:
-       isl_dim_free(dim);
+       isl_space_free(dim);
        isl_pw_aff_list_free(list);
        return NULL;
 }
 
 static __isl_give isl_pw_aff *accept_div(struct isl_stream *s,
-       __isl_take isl_dim *dim, struct vars *v)
+       __isl_take isl_space *dim, struct vars *v)
 {
        struct isl_token *tok;
        int seen_paren = 0;
@@ -303,7 +302,7 @@ static __isl_give isl_pw_aff *accept_div(struct isl_stream *s,
                        seen_paren = 1;
        }
 
-       pwaff = accept_affine(s, isl_dim_copy(dim), v);
+       pwaff = accept_affine(s, isl_space_copy(dim), v);
 
        if (f || c) {
                if (isl_stream_eat(s, ','))
@@ -339,16 +338,16 @@ static __isl_give isl_pw_aff *accept_div(struct isl_stream *s,
                        goto error;
        }
 
-       isl_dim_free(dim);
+       isl_space_free(dim);
        return pwaff;
 error:
-       isl_dim_free(dim);
+       isl_space_free(dim);
        isl_pw_aff_free(pwaff);
        return NULL;
 }
 
 static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
-       __isl_take isl_dim *dim, struct vars *v)
+       __isl_take isl_space *dim, struct vars *v)
 {
        struct isl_token *tok = NULL;
        isl_pw_aff *res = NULL;
@@ -358,7 +357,11 @@ static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
                isl_stream_error(s, NULL, "unexpected EOF");
                goto error;
        }
-       if (tok->type == ISL_TOKEN_IDENT) {
+
+       if (tok->type == ISL_TOKEN_AFF) {
+               res = isl_pw_aff_copy(tok->u.pwaff);
+               isl_token_free(tok);
+       } else if (tok->type == ISL_TOKEN_IDENT) {
                int n = v->n;
                int pos = vars_pos(v, tok->u.s, -1);
                isl_aff *aff;
@@ -370,7 +373,7 @@ static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
                        goto error;
                }
 
-               aff = isl_aff_zero(isl_local_space_from_dim(isl_dim_copy(dim)));
+               aff = isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim)));
                if (!aff)
                        goto error;
                isl_int_set_si(aff->v->el[2 + pos], 1);
@@ -378,13 +381,13 @@ static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
                isl_token_free(tok);
        } else if (tok->type == ISL_TOKEN_VALUE) {
                if (isl_stream_eat_if_available(s, '*')) {
-                       res = accept_affine_factor(s, isl_dim_copy(dim), v);
+                       res = accept_affine_factor(s, isl_space_copy(dim), v);
                        res = isl_pw_aff_scale(res, tok->u.v);
                } else {
                        isl_local_space *ls;
                        isl_aff *aff;
-                       ls = isl_local_space_from_dim(isl_dim_copy(dim));
-                       aff = isl_aff_zero(ls);
+                       ls = isl_local_space_from_space(isl_space_copy(dim));
+                       aff = isl_aff_zero_on_domain(ls);
                        aff = isl_aff_add_constant(aff, tok->u.v);
                        res = isl_pw_aff_from_aff(aff);
                }
@@ -392,7 +395,7 @@ static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
        } else if (tok->type == '(') {
                isl_token_free(tok);
                tok = NULL;
-               res = accept_affine(s, isl_dim_copy(dim), v);
+               res = accept_affine(s, isl_space_copy(dim), v);
                if (!res)
                        goto error;
                if (isl_stream_eat(s, ')'))
@@ -402,18 +405,18 @@ static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
                    tok->type == ISL_TOKEN_CEILD) {
                isl_stream_push_token(s, tok);
                tok = NULL;
-               res = accept_div(s, isl_dim_copy(dim), v);
+               res = accept_div(s, isl_space_copy(dim), v);
        } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
                isl_stream_push_token(s, tok);
                tok = NULL;
-               res = accept_minmax(s, isl_dim_copy(dim), v);
+               res = accept_minmax(s, isl_space_copy(dim), v);
        } else {
                isl_stream_error(s, tok, "expecting factor");
                goto error;
        }
        if (isl_stream_eat_if_available(s, '%') ||
            isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
-               isl_dim_free(dim);
+               isl_space_free(dim);
                return affine_mod(s, v, res);
        }
        if (isl_stream_eat_if_available(s, '*')) {
@@ -428,36 +431,38 @@ static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
                isl_int_clear(f);
        }
 
-       isl_dim_free(dim);
+       isl_space_free(dim);
        return res;
 error:
        isl_token_free(tok);
 error2:
        isl_pw_aff_free(res);
-       isl_dim_free(dim);
+       isl_space_free(dim);
        return NULL;
 }
 
 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
 {
        isl_aff *aff;
+       isl_space *space;
 
-       aff = isl_aff_zero(isl_local_space_from_dim(isl_pw_aff_get_dim(pwaff)));
+       space = isl_pw_aff_get_domain_space(pwaff);
+       aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
        aff = isl_aff_add_constant(aff, v);
 
        return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
 }
 
 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
-       __isl_take isl_dim *dim, struct vars *v)
+       __isl_take isl_space *dim, struct vars *v)
 {
        struct isl_token *tok = NULL;
        isl_local_space *ls;
        isl_pw_aff *res;
        int sign = 1;
 
-       ls = isl_local_space_from_dim(isl_dim_copy(dim));
-       res = isl_pw_aff_from_aff(isl_aff_zero(ls));
+       ls = isl_local_space_from_space(isl_space_copy(dim));
+       res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
        if (!res)
                goto error;
 
@@ -476,11 +481,12 @@ static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
                    tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
                    tok->type == ISL_TOKEN_FLOORD ||
                    tok->type == ISL_TOKEN_CEILD ||
-                   tok->type == ISL_TOKEN_IDENT) {
+                   tok->type == ISL_TOKEN_IDENT ||
+                   tok->type == ISL_TOKEN_AFF) {
                        isl_pw_aff *term;
                        isl_stream_push_token(s, tok);
                        tok = NULL;
-                       term = accept_affine_factor(s, isl_dim_copy(dim), v);
+                       term = accept_affine_factor(s, isl_space_copy(dim), v);
                        if (sign < 0)
                                res = isl_pw_aff_sub(res, term);
                        else
@@ -495,7 +501,7 @@ static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
                            isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
                                isl_pw_aff *term;
                                term = accept_affine_factor(s,
-                                                       isl_dim_copy(dim), v);
+                                                       isl_space_copy(dim), v);
                                term = isl_pw_aff_scale(term, tok->u.v);
                                res = isl_pw_aff_add(res, term);
                                if (!res)
@@ -508,7 +514,7 @@ static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
                        isl_stream_error(s, tok, "unexpected isl_token");
                        isl_stream_push_token(s, tok);
                        isl_pw_aff_free(res);
-                       isl_dim_free(dim);
+                       isl_space_free(dim);
                        return NULL;
                }
                isl_token_free(tok);
@@ -530,15 +536,126 @@ static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
                }
        }
 
-       isl_dim_free(dim);
+       isl_space_free(dim);
        return res;
 error:
-       isl_dim_free(dim);
+       isl_space_free(dim);
        isl_token_free(tok);
        isl_pw_aff_free(res);
        return NULL;
 }
 
+static int is_comparator(struct isl_token *tok)
+{
+       if (!tok)
+               return 0;
+
+       switch (tok->type) {
+       case ISL_TOKEN_LT:
+       case ISL_TOKEN_GT:
+       case ISL_TOKEN_LE:
+       case ISL_TOKEN_GE:
+       case ISL_TOKEN_NE:
+       case '=':
+               return 1;
+       default:
+               return 0;
+       }
+}
+
+static struct isl_map *read_disjuncts(struct isl_stream *s,
+       struct vars *v, __isl_take isl_map *map);
+static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
+       __isl_take isl_space *dim, struct vars *v);
+
+/* Accept a ternary operator, given the first argument.
+ */
+static __isl_give isl_pw_aff *accept_ternary(struct isl_stream *s,
+       __isl_take isl_map *cond, struct vars *v)
+{
+       isl_space *dim;
+       isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL, *pa_cond;
+
+       if (!cond)
+               return NULL;
+
+       if (isl_stream_eat(s, '?'))
+               goto error;
+
+       dim = isl_space_wrap(isl_map_get_space(cond));
+       pwaff1 = accept_extended_affine(s, dim, v);
+       if (!pwaff1)
+               goto error;
+
+       if (isl_stream_eat(s, ':'))
+               goto error;
+
+       dim = isl_pw_aff_get_domain_space(pwaff1);
+       pwaff2 = accept_extended_affine(s, dim, v);
+       if (!pwaff1)
+               goto error;
+
+       pa_cond = isl_set_indicator_function(isl_map_wrap(cond));
+       return isl_pw_aff_cond(pa_cond, pwaff1, pwaff2);
+error:
+       isl_map_free(cond);
+       isl_pw_aff_free(pwaff1);
+       isl_pw_aff_free(pwaff2);
+       return NULL;
+}
+
+/* Accept an affine expression that may involve ternary operators.
+ * We first read an affine expression.
+ * If it is not followed by a comparison operator, we simply return it.
+ * Otherwise, we assume the affine epxression is part of the first
+ * argument of a ternary operator and try to parse that.
+ */
+static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
+       __isl_take isl_space *dim, struct vars *v)
+{
+       isl_space *space;
+       isl_map *cond;
+       isl_pw_aff *pwaff;
+       struct isl_token *tok;
+       int line = -1, col = -1;
+       int is_comp;
+
+       tok = isl_stream_next_token(s);
+       if (tok) {
+               line = tok->line;
+               col = tok->col;
+               isl_stream_push_token(s, tok);
+       }
+
+       pwaff = accept_affine(s, dim, v);
+       if (!pwaff)
+               return NULL;
+
+       tok = isl_stream_next_token(s);
+       if (!tok)
+               return isl_pw_aff_free(pwaff);
+
+       is_comp = is_comparator(tok);
+       isl_stream_push_token(s, tok);
+       if (!is_comp)
+               return pwaff;
+
+       tok = isl_token_new(s->ctx, line, col, 0);
+       if (!tok)
+               return isl_pw_aff_free(pwaff);
+       tok->type = ISL_TOKEN_AFF;
+       tok->u.pwaff = pwaff;
+
+       space = isl_pw_aff_get_domain_space(pwaff);
+       cond = isl_map_universe(isl_space_unwrap(space));
+
+       isl_stream_push_token(s, tok);
+
+       cond = read_disjuncts(s, v, cond);
+
+       return accept_ternary(s, cond, v);
+}
+
 static __isl_give isl_map *read_var_def(struct isl_stream *s,
        __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
 {
@@ -546,14 +663,19 @@ static __isl_give isl_map *read_var_def(struct isl_stream *s,
        int pos;
        isl_map *def_map;
 
-       pos = isl_map_dim(map, isl_dim_in);
-       if (type == isl_dim_out)
-               pos += isl_map_dim(map, isl_dim_out);
+       if (type == isl_dim_param)
+               pos = isl_map_dim(map, isl_dim_param);
+       else {
+               pos = isl_map_dim(map, isl_dim_in);
+               if (type == isl_dim_out)
+                       pos += isl_map_dim(map, isl_dim_out);
+               type = isl_dim_in;
+       }
        --pos;
 
-       def = accept_affine(s, isl_dim_wrap(isl_map_get_dim(map)), v);
+       def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
        def_map = isl_map_from_pw_aff(def);
-       def_map = isl_map_equate(def_map, isl_dim_in, pos, isl_dim_out, 0);
+       def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
        def_map = isl_set_unwrap(isl_map_domain(def_map));
 
        map = isl_map_intersect(map, def_map);
@@ -568,7 +690,7 @@ static __isl_give isl_map *read_var_list(struct isl_stream *s,
        struct isl_token *tok;
 
        if (isl_stream_next_token_is(s, ']'))
-               return map;
+               return isl_map_add_dims(map, type, 0);
 
        while ((tok = next_token(s)) != NULL) {
                int new_name = 0;
@@ -585,6 +707,8 @@ static __isl_give isl_map *read_var_list(struct isl_stream *s,
                        map = isl_map_add_dims(map, type, 1);
                        map = set_name(map, type, i, v->v->name);
                        isl_token_free(tok);
+                       if (isl_stream_eat_if_available(s, '='))
+                               map = read_var_def(s, map, type, v);
                } else {
                        if (type == isl_dim_param) {
                                isl_stream_error(s, tok,
@@ -621,13 +745,13 @@ error:
 }
 
 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
-       __isl_take isl_dim *dim, struct vars *v)
+       __isl_take isl_space *dim, struct vars *v)
 {
        isl_pw_aff *pwaff;
        isl_pw_aff_list *list;
        struct isl_token *tok = NULL;
 
-       pwaff = accept_affine(s, isl_dim_copy(dim), v);
+       pwaff = accept_affine(s, isl_space_copy(dim), v);
        list = isl_pw_aff_list_from_pw_aff(pwaff);
        if (!list)
                goto error;
@@ -644,17 +768,17 @@ static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
                }
                isl_token_free(tok);
 
-               pwaff = accept_affine(s, isl_dim_copy(dim), v);
+               pwaff = accept_affine(s, isl_space_copy(dim), v);
                list = isl_pw_aff_list_concat(list,
                                isl_pw_aff_list_from_pw_aff(pwaff));
                if (!list)
                        return NULL;
        }
 
-       isl_dim_free(dim);
+       isl_space_free(dim);
        return list;
 error:
-       isl_dim_free(dim);
+       isl_space_free(dim);
        isl_pw_aff_list_free(list);
        return NULL;
 }
@@ -731,15 +855,14 @@ static int next_is_tuple(struct isl_stream *s)
 static __isl_give isl_map *read_tuple(struct isl_stream *s,
        __isl_take isl_map *map, enum isl_dim_type type, struct vars *v);
 
-static __isl_give isl_map *read_nested_tuple(struct isl_stream *s,
+static __isl_give isl_set *read_nested_tuple(struct isl_stream *s,
        __isl_take isl_map *map, struct vars *v)
 {
        map = read_tuple(s, map, isl_dim_in, v);
        if (isl_stream_eat(s, ISL_TOKEN_TO))
                goto error;
        map = read_tuple(s, map, isl_dim_out, v);
-       map = isl_map_from_range(isl_map_wrap(map));
-       return map;
+       return isl_map_wrap(map);
 error:
        isl_map_free(map);
        return NULL;
@@ -765,25 +888,26 @@ static __isl_give isl_map *read_tuple(struct isl_stream *s,
        }
        isl_token_free(tok);
        if (type != isl_dim_param && next_is_tuple(s)) {
-               isl_dim *dim = isl_map_get_dim(map);
-               int nparam = isl_dim_size(dim, isl_dim_param);
-               int n_in = isl_dim_size(dim, isl_dim_in);
-               isl_map *nested;
-               if (type == isl_dim_out)
-                       dim = isl_dim_move(dim, isl_dim_param, nparam,
+               isl_space *dim = isl_map_get_space(map);
+               int nparam = isl_space_dim(dim, isl_dim_param);
+               int n_in = isl_space_dim(dim, isl_dim_in);
+               isl_set *nested;
+               if (type == isl_dim_out) {
+                       dim = isl_space_move_dims(dim, isl_dim_param, nparam,
                                                isl_dim_in, 0, n_in);
-               nested = isl_map_universe(dim);
-               nested = read_nested_tuple(s, nested, v);
+                       dim = isl_space_params(dim);
+               }
+               nested = read_nested_tuple(s, isl_map_universe(dim), v);
                if (type == isl_dim_in) {
                        nested = isl_map_reverse(nested);
-                       map = isl_map_intersect(nested, map);
+                       map = isl_map_intersect_params(nested, map);
                } else {
                        isl_set *set;
-                       dim = isl_dim_range(isl_map_get_dim(nested));
-                       dim = isl_dim_drop(dim, isl_dim_param, nparam, n_in);
-                       dim = isl_dim_join(isl_map_get_dim(map), dim);
+                       dim = isl_set_get_space(nested);
+                       dim = isl_space_drop_dims(dim, isl_dim_param, nparam, n_in);
+                       dim = isl_space_join(isl_map_get_space(map), dim);
                        set = isl_map_domain(map);
-                       nested = isl_map_reset_dim(nested, dim);
+                       nested = isl_map_reset_space(nested, dim);
                        map = isl_map_intersect_domain(nested, set);
                }
        } else
@@ -809,7 +933,7 @@ error:
 }
 
 static __isl_give isl_set *construct_constraints(
-       __isl_take isl_set *set, enum isl_token_type type,
+       __isl_take isl_set *set, int type,
        __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right)
 {
        isl_set *cond;
@@ -826,6 +950,9 @@ static __isl_give isl_set *construct_constraints(
        else if (type == ISL_TOKEN_GT)
                cond = isl_pw_aff_list_gt_set(isl_pw_aff_list_copy(left),
                                              isl_pw_aff_list_copy(right));
+       else if (type == ISL_TOKEN_NE)
+               cond = isl_pw_aff_list_ne_set(isl_pw_aff_list_copy(left),
+                                             isl_pw_aff_list_copy(right));
        else
                cond = isl_pw_aff_list_eq_set(isl_pw_aff_list_copy(left),
                                              isl_pw_aff_list_copy(right));
@@ -833,23 +960,6 @@ static __isl_give isl_set *construct_constraints(
        return isl_set_intersect(set, cond);
 }
 
-static int is_comparator(struct isl_token *tok)
-{
-       if (!tok)
-               return 0;
-
-       switch (tok->type) {
-       case ISL_TOKEN_LT:
-       case ISL_TOKEN_GT:
-       case ISL_TOKEN_LE:
-       case ISL_TOKEN_GE:
-       case '=':
-               return 1;
-       default:
-               return 0;
-       }
-}
-
 static __isl_give isl_map *add_constraint(struct isl_stream *s,
        struct vars *v, __isl_take isl_map *map)
 {
@@ -858,7 +968,7 @@ static __isl_give isl_map *add_constraint(struct isl_stream *s,
        isl_set *set;
 
        set = isl_map_wrap(map);
-       list1 = accept_affine_list(s, isl_set_get_dim(set), v);
+       list1 = accept_affine_list(s, isl_set_get_space(set), v);
        if (!list1)
                goto error;
        tok = isl_stream_next_token(s);
@@ -870,7 +980,7 @@ static __isl_give isl_map *add_constraint(struct isl_stream *s,
                goto error;
        }
        for (;;) {
-               list2 = accept_affine_list(s, isl_set_get_dim(set), v);
+               list2 = accept_affine_list(s, isl_set_get_space(set), v);
                if (!list2)
                        goto error;
 
@@ -898,9 +1008,6 @@ error:
        return NULL;
 }
 
-static struct isl_map *read_disjuncts(struct isl_stream *s,
-       struct vars *v, __isl_take isl_map *map);
-
 static __isl_give isl_map *read_exists(struct isl_stream *s,
        struct vars *v, __isl_take isl_map *map)
 {
@@ -926,13 +1033,108 @@ error:
        return NULL;
 }
 
-static __isl_give isl_map *read_conjunct(struct isl_stream *s,
+/* Parse an expression between parentheses and push the result
+ * back on the stream.
+ *
+ * The parsed expression may be either an affine expression
+ * or a condition.  The first type is pushed onto the stream
+ * as an isl_pw_aff, while the second is pushed as an isl_map.
+ *
+ * If the initial token indicates the start of a condition,
+ * we parse it as such.
+ * Otherwise, we first parse an affine expression and push
+ * that onto the stream.  If the affine expression covers the
+ * entire expression between parentheses, we return.
+ * Otherwise, we assume that the affine expression is the
+ * start of a condition and continue parsing.
+ */
+static int resolve_paren_expr(struct isl_stream *s,
        struct vars *v, __isl_take isl_map *map)
 {
-       if (isl_stream_eat_if_available(s, '(')) {
+       struct isl_token *tok, *tok2;
+       int line, col;
+       isl_pw_aff *pwaff;
+
+       tok = isl_stream_next_token(s);
+       if (!tok || tok->type != '(')
+               goto error;
+
+       if (isl_stream_next_token_is(s, '('))
+               if (resolve_paren_expr(s, v, isl_map_copy(map)))
+                       goto error;
+
+       if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
+           isl_stream_next_token_is(s, ISL_TOKEN_NOT) ||
+           isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
+           isl_stream_next_token_is(s, ISL_TOKEN_FALSE) ||
+           isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
                map = read_disjuncts(s, v, map);
                if (isl_stream_eat(s, ')'))
                        goto error;
+               tok->type = ISL_TOKEN_MAP;
+               tok->u.map = map;
+               isl_stream_push_token(s, tok);
+               return 0;
+       }
+
+       tok2 = isl_stream_next_token(s);
+       if (!tok2)
+               goto error;
+       line = tok2->line;
+       col = tok2->col;
+       isl_stream_push_token(s, tok2);
+
+       pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
+       if (!pwaff)
+               goto error;
+
+       tok2 = isl_token_new(s->ctx, line, col, 0);
+       if (!tok2)
+               goto error2;
+       tok2->type = ISL_TOKEN_AFF;
+       tok2->u.pwaff = pwaff;
+
+       if (isl_stream_eat_if_available(s, ')')) {
+               isl_stream_push_token(s, tok2);
+               isl_token_free(tok);
+               isl_map_free(map);
+               return 0;
+       }
+
+       isl_stream_push_token(s, tok2);
+
+       map = read_disjuncts(s, v, map);
+       if (isl_stream_eat(s, ')'))
+               goto error;
+
+       tok->type = ISL_TOKEN_MAP;
+       tok->u.map = map;
+       isl_stream_push_token(s, tok);
+
+       return 0;
+error2:
+       isl_pw_aff_free(pwaff);
+error:
+       isl_token_free(tok);
+       isl_map_free(map);
+       return -1;
+}
+
+static __isl_give isl_map *read_conjunct(struct isl_stream *s,
+       struct vars *v, __isl_take isl_map *map)
+{
+       if (isl_stream_next_token_is(s, '('))
+               if (resolve_paren_expr(s, v, isl_map_copy(map)))
+                       goto error;
+
+       if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
+               struct isl_token *tok;
+               tok = isl_stream_next_token(s);
+               if (!tok)
+                       goto error;
+               isl_map_free(map);
+               map = isl_map_copy(tok->u.map);
+               isl_token_free(tok);
                return map;
        }
 
@@ -943,7 +1145,7 @@ static __isl_give isl_map *read_conjunct(struct isl_stream *s,
                return map;
 
        if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
-               isl_dim *dim = isl_map_get_dim(map);
+               isl_space *dim = isl_map_get_space(map);
                isl_map_free(map);
                return isl_map_empty(dim);
        }
@@ -986,7 +1188,7 @@ static struct isl_map *read_disjuncts(struct isl_stream *s,
        isl_map *res;
 
        if (isl_stream_next_token_is(s, '}')) {
-               isl_dim *dim = isl_map_get_dim(map);
+               isl_space *dim = isl_map_get_space(map);
                isl_map_free(map);
                return isl_map_universe(dim);
        }
@@ -1096,8 +1298,7 @@ error:
        return NULL;
 }
 
-static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
-       int nparam)
+static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s)
 {
        int i;
        struct isl_token *tok;
@@ -1106,9 +1307,7 @@ static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
        int on_new_line;
        unsigned in = 0, out, local = 0;
        struct isl_basic_map *bmap = NULL;
-
-       if (nparam < 0)
-               nparam = 0;
+       int nparam = 0;
 
        tok = isl_stream_next_token(s);
        if (!tok) {
@@ -1215,7 +1414,7 @@ error:
        return NULL;
 }
 
-static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
+static struct isl_map *map_read_polylib(struct isl_stream *s)
 {
        struct isl_token *tok;
        struct isl_token *tok2;
@@ -1231,7 +1430,7 @@ static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
        if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
                isl_stream_push_token(s, tok2);
                isl_stream_push_token(s, tok);
-               return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
+               return isl_map_from_basic_map(basic_map_read_polylib(s));
        }
        if (tok2) {
                isl_stream_error(s, tok2, "unexpected token");
@@ -1244,11 +1443,11 @@ static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
 
        isl_assert(s->ctx, n >= 1, return NULL);
 
-       map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
+       map = isl_map_from_basic_map(basic_map_read_polylib(s));
 
        for (i = 1; map && i < n; ++i)
                map = isl_map_union(map,
-                       isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
+                       isl_map_from_basic_map(basic_map_read_polylib(s)));
 
        return map;
 }
@@ -1316,12 +1515,12 @@ static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
                                isl_token_free(tok2);
                                return NULL;
                        }
-                       qp = isl_qpolynomial_rat_cst(isl_map_get_dim(map),
+                       qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
                                                    tok->u.v, tok2->u.v);
                        isl_token_free(tok2);
                } else {
                        isl_stream_push_token(s, tok2);
-                       qp = isl_qpolynomial_cst(isl_map_get_dim(map),
+                       qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
                                                tok->u.v);
                }
                isl_token_free(tok);
@@ -1329,12 +1528,12 @@ static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
        } else if (tok->type == ISL_TOKEN_INFTY) {
                isl_qpolynomial *qp;
                isl_token_free(tok);
-               qp = isl_qpolynomial_infty(isl_map_get_dim(map));
+               qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
                pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
        } else if (tok->type == ISL_TOKEN_NAN) {
                isl_qpolynomial *qp;
                isl_token_free(tok);
-               qp = isl_qpolynomial_nan(isl_map_get_dim(map));
+               qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
                pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
        } else if (tok->type == ISL_TOKEN_IDENT) {
                int n = v->n;
@@ -1353,14 +1552,14 @@ static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
                }
                isl_token_free(tok);
                pow = optional_power(s);
-               qp = isl_qpolynomial_var_pow(isl_map_get_dim(map), pos, pow);
+               qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
                pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
        } else if (tok->type == '[') {
                isl_pw_aff *pwaff;
                int pow;
 
                isl_stream_push_token(s, tok);
-               pwaff = accept_affine(s, isl_map_get_dim(map), v);
+               pwaff = accept_div(s, isl_map_get_space(map), v);
                pow = optional_power(s);
                pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
                pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
@@ -1472,25 +1671,24 @@ static struct isl_obj obj_read_poly(struct isl_stream *s,
 }
 
 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
-       __isl_take isl_map *map, struct vars *v, int n)
+       __isl_take isl_set *set, struct vars *v, int n)
 {
        struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
        isl_pw_qpolynomial *pwqp;
        isl_pw_qpolynomial_fold *pwf = NULL;
-       isl_set *set;
 
        if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
-               return obj_read_poly(s, map, v, n);
+               return obj_read_poly(s, set, v, n);
 
        if (isl_stream_eat(s, '('))
                goto error;
 
-       pwqp = read_term(s, map, v);
+       pwqp = read_term(s, set, v);
        pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
 
        while (isl_stream_eat_if_available(s, ',')) {
                isl_pw_qpolynomial_fold *pwf_i;
-               pwqp = read_term(s, map, v);
+               pwqp = read_term(s, set, v);
                pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
                                                                        pwqp);
                pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
@@ -1499,8 +1697,7 @@ static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
        if (isl_stream_eat(s, ')'))
                goto error;
 
-       map = read_optional_disjuncts(s, map, v);
-       set = isl_map_range(map);
+       set = read_optional_disjuncts(s, set, v);
        pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
 
        vars_drop(v, v->n - n);
@@ -1508,7 +1705,7 @@ static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
        obj.v = pwf;
        return obj;
 error:
-       isl_map_free(map);
+       isl_set_free(set);
        isl_pw_qpolynomial_fold_free(pwf);
        obj.type = isl_obj_none;
        return obj;
@@ -1542,6 +1739,12 @@ static struct isl_obj obj_read_body(struct isl_stream *s,
        if (is_rational(s))
                map = isl_map_set_rational(map);
 
+       if (isl_stream_next_token_is(s, ':')) {
+               obj.type = isl_obj_set;
+               obj.v = read_optional_disjuncts(s, map, v);
+               return obj;
+       }
+
        if (!next_is_tuple(s))
                return obj_read_poly_or_fold(s, map, v, n);
 
@@ -1553,8 +1756,8 @@ static struct isl_obj obj_read_body(struct isl_stream *s,
                obj.type = isl_obj_map;
                isl_token_free(tok);
                if (!next_is_tuple(s)) {
-                       map = isl_map_reverse(map);
-                       return obj_read_poly_or_fold(s, map, v, n);
+                       isl_set *set = isl_map_domain(map);
+                       return obj_read_poly_or_fold(s, set, v, n);
                }
                map = read_tuple(s, map, isl_dim_out, v);
                if (!map)
@@ -1624,21 +1827,21 @@ static struct isl_obj obj_add(struct isl_ctx *ctx,
            obj2.type == isl_obj_pw_qpolynomial_fold)
                obj2 = to_union(ctx, obj2);
        isl_assert(ctx, obj1.type == obj2.type, goto error);
-       if (obj1.type == isl_obj_map && !isl_map_has_equal_dim(obj1.v, obj2.v)) {
+       if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
                obj1 = to_union(ctx, obj1);
                obj2 = to_union(ctx, obj2);
        }
-       if (obj1.type == isl_obj_set && !isl_set_has_equal_dim(obj1.v, obj2.v)) {
+       if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
                obj1 = to_union(ctx, obj1);
                obj2 = to_union(ctx, obj2);
        }
        if (obj1.type == isl_obj_pw_qpolynomial &&
-           !isl_pw_qpolynomial_has_equal_dim(obj1.v, obj2.v)) {
+           !isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)) {
                obj1 = to_union(ctx, obj1);
                obj2 = to_union(ctx, obj2);
        }
        if (obj1.type == isl_obj_pw_qpolynomial_fold &&
-           !isl_pw_qpolynomial_fold_has_equal_dim(obj1.v, obj2.v)) {
+           !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
                obj1 = to_union(ctx, obj1);
                obj2 = to_union(ctx, obj2);
        }
@@ -1652,7 +1855,7 @@ error:
        return obj1;
 }
 
-static struct isl_obj obj_read(struct isl_stream *s, int nparam)
+static struct isl_obj obj_read(struct isl_stream *s)
 {
        isl_map *map = NULL;
        struct isl_token *tok;
@@ -1680,12 +1883,15 @@ static struct isl_obj obj_read(struct isl_stream *s, int nparam)
                }
                isl_stream_push_token(s, tok2);
                isl_stream_push_token(s, tok);
-               map = map_read_polylib(s, nparam);
+               map = map_read_polylib(s);
                if (!map)
                        goto error;
-               if (isl_map_dim(map, isl_dim_in) > 0)
+               if (isl_map_may_be_set(map))
+                       obj.v = isl_map_range(map);
+               else {
                        obj.type = isl_obj_map;
-               obj.v = map;
+                       obj.v = map;
+               }
                return obj;
        }
        v = vars_new(s->ctx);
@@ -1693,14 +1899,12 @@ static struct isl_obj obj_read(struct isl_stream *s, int nparam)
                isl_stream_push_token(s, tok);
                goto error;
        }
-       map = isl_map_universe(isl_dim_alloc(s->ctx, 0, 0, 0));
+       map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
        if (tok->type == '[') {
                isl_stream_push_token(s, tok);
                map = read_tuple(s, map, isl_dim_param, v);
                if (!map)
                        goto error;
-               if (nparam >= 0)
-                       isl_assert(s->ctx, nparam == v->n, goto error);
                tok = isl_stream_next_token(s);
                if (!tok || tok->type != ISL_TOKEN_TO) {
                        isl_stream_error(s, tok, "expecting '->'");
@@ -1710,8 +1914,7 @@ static struct isl_obj obj_read(struct isl_stream *s, int nparam)
                }
                isl_token_free(tok);
                tok = isl_stream_next_token(s);
-       } else if (nparam > 0)
-               map = isl_map_add_dims(map, isl_dim_param, nparam);
+       }
        if (!tok || tok->type != '{') {
                isl_stream_error(s, tok, "expecting '{'");
                if (tok)
@@ -1730,11 +1933,9 @@ static struct isl_obj obj_read(struct isl_stream *s, int nparam)
                map = read_tuple(s, map, isl_dim_param, v);
                if (!map)
                        goto error;
-               if (nparam >= 0)
-                       isl_assert(s->ctx, nparam == v->n, goto error);
        } else if (tok->type == '}') {
                obj.type = isl_obj_union_set;
-               obj.v = isl_union_set_empty(isl_map_get_dim(map));
+               obj.v = isl_union_set_empty(isl_map_get_space(map));
                isl_token_free(tok);
                goto done;
        } else
@@ -1787,14 +1988,14 @@ error:
 
 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
 {
-       return obj_read(s, -1);
+       return obj_read(s);
 }
 
-__isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
+__isl_give isl_map *isl_stream_read_map(struct isl_stream *s)
 {
        struct isl_obj obj;
 
-       obj = obj_read(s, nparam);
+       obj = obj_read(s);
        if (obj.v)
                isl_assert(s->ctx, obj.type == isl_obj_map ||
                                   obj.type == isl_obj_set, goto error);
@@ -1805,13 +2006,18 @@ error:
        return NULL;
 }
 
-__isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
+__isl_give isl_set *isl_stream_read_set(struct isl_stream *s)
 {
        struct isl_obj obj;
 
-       obj = obj_read(s, nparam);
-       if (obj.v)
+       obj = obj_read(s);
+       if (obj.v) {
+               if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
+                       obj.v = isl_map_range(obj.v);
+                       obj.type = isl_obj_set;
+               }
                isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
+       }
 
        return obj.v;
 error:
@@ -1823,7 +2029,7 @@ __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
 {
        struct isl_obj obj;
 
-       obj = obj_read(s, -1);
+       obj = obj_read(s);
        if (obj.type == isl_obj_map) {
                obj.type = isl_obj_union_map;
                obj.v = isl_union_map_from_map(obj.v);
@@ -1846,7 +2052,7 @@ __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
 {
        struct isl_obj obj;
 
-       obj = obj_read(s, -1);
+       obj = obj_read(s);
        if (obj.type == isl_obj_set) {
                obj.type = isl_obj_union_set;
                obj.v = isl_union_set_from_set(obj.v);
@@ -1860,13 +2066,13 @@ error:
        return NULL;
 }
 
-static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
+static __isl_give isl_basic_map *basic_map_read(struct isl_stream *s)
 {
        struct isl_obj obj;
        struct isl_map *map;
        struct isl_basic_map *bmap;
 
-       obj = obj_read(s, nparam);
+       obj = obj_read(s);
        map = obj.v;
        if (!map)
                return NULL;
@@ -1886,108 +2092,115 @@ error:
        return NULL;
 }
 
+static __isl_give isl_basic_set *basic_set_read(struct isl_stream *s)
+{
+       isl_basic_map *bmap;
+       bmap = basic_map_read(s);
+       if (!bmap)
+               return NULL;
+       if (!isl_basic_map_may_be_set(bmap))
+               isl_die(s->ctx, isl_error_invalid,
+                       "input is not a set", goto error);
+       return isl_basic_map_range(bmap);
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
-               FILE *input, int nparam)
+       FILE *input)
 {
        struct isl_basic_map *bmap;
        struct isl_stream *s = isl_stream_new_file(ctx, input);
        if (!s)
                return NULL;
-       bmap = basic_map_read(s, nparam);
+       bmap = basic_map_read(s);
        isl_stream_free(s);
        return bmap;
 }
 
 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
-               FILE *input, int nparam)
+       FILE *input)
 {
-       struct isl_basic_map *bmap;
-       bmap = isl_basic_map_read_from_file(ctx, input, nparam);
-       if (!bmap)
+       isl_basic_set *bset;
+       struct isl_stream *s = isl_stream_new_file(ctx, input);
+       if (!s)
                return NULL;
-       isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
-       return (struct isl_basic_set *)bmap;
-error:
-       isl_basic_map_free(bmap);
-       return NULL;
+       bset = basic_set_read(s);
+       isl_stream_free(s);
+       return bset;
 }
 
 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
-               const char *str, int nparam)
+       const char *str)
 {
        struct isl_basic_map *bmap;
        struct isl_stream *s = isl_stream_new_str(ctx, str);
        if (!s)
                return NULL;
-       bmap = basic_map_read(s, nparam);
+       bmap = basic_map_read(s);
        isl_stream_free(s);
        return bmap;
 }
 
 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
-               const char *str, int nparam)
+       const char *str)
 {
-       struct isl_basic_map *bmap;
-       bmap = isl_basic_map_read_from_str(ctx, str, nparam);
-       if (!bmap)
+       isl_basic_set *bset;
+       struct isl_stream *s = isl_stream_new_str(ctx, str);
+       if (!s)
                return NULL;
-       isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
-       return (struct isl_basic_set *)bmap;
-error:
-       isl_basic_map_free(bmap);
-       return NULL;
+       bset = basic_set_read(s);
+       isl_stream_free(s);
+       return bset;
 }
 
 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
-               FILE *input, int nparam)
+       FILE *input)
 {
        struct isl_map *map;
        struct isl_stream *s = isl_stream_new_file(ctx, input);
        if (!s)
                return NULL;
-       map = isl_stream_read_map(s, nparam);
+       map = isl_stream_read_map(s);
        isl_stream_free(s);
        return map;
 }
 
 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
-               const char *str, int nparam)
+       const char *str)
 {
        struct isl_map *map;
        struct isl_stream *s = isl_stream_new_str(ctx, str);
        if (!s)
                return NULL;
-       map = isl_stream_read_map(s, nparam);
+       map = isl_stream_read_map(s);
        isl_stream_free(s);
        return map;
 }
 
 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
-               FILE *input, int nparam)
+       FILE *input)
 {
-       struct isl_map *map;
-       map = isl_map_read_from_file(ctx, input, nparam);
-       if (!map)
+       isl_set *set;
+       struct isl_stream *s = isl_stream_new_file(ctx, input);
+       if (!s)
                return NULL;
-       isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
-       return (struct isl_set *)map;
-error:
-       isl_map_free(map);
-       return NULL;
+       set = isl_stream_read_set(s);
+       isl_stream_free(s);
+       return set;
 }
 
 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
-               const char *str, int nparam)
+       const char *str)
 {
-       struct isl_map *map;
-       map = isl_map_read_from_str(ctx, str, nparam);
-       if (!map)
+       isl_set *set;
+       struct isl_stream *s = isl_stream_new_str(ctx, str);
+       if (!s)
                return NULL;
-       isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
-       return (struct isl_set *)map;
-error:
-       isl_map_free(map);
-       return NULL;
+       set = isl_stream_read_set(s);
+       isl_stream_free(s);
+       return set;
 }
 
 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
@@ -2094,7 +2307,7 @@ __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
 {
        struct isl_obj obj;
 
-       obj = obj_read(s, -1);
+       obj = obj_read(s);
        if (obj.v)
                isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
                           goto error);
@@ -2128,3 +2341,347 @@ __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
        isl_stream_free(s);
        return pwqp;
 }
+
+/* Read an affine expression from "s" with domain (space) "dom".
+ * We call accept_affine to parse a possibly piecewise affine expression
+ * and then check that the result is a single affine expression on
+ * a universe domain.
+ */
+static __isl_give isl_aff *read_aff_with_dom(struct isl_stream *s,
+       __isl_take isl_set *dom, struct vars *v)
+{
+       isl_aff *aff = NULL;
+       isl_pw_aff *pwaff = NULL;
+
+       if (!isl_set_plain_is_universe(dom))
+               isl_die(s->ctx, isl_error_invalid,
+                       "expecting universe domain", goto error);
+
+       if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
+               goto error;
+
+       if (isl_stream_eat(s, '['))
+               goto error;
+
+       pwaff = accept_affine(s, isl_set_get_space(dom), v);
+
+       if (isl_stream_eat(s, ']'))
+               goto error;
+       if (isl_stream_eat(s, '}'))
+               goto error;
+
+       if (!pwaff)
+               goto error;
+
+       if (pwaff->n != 1)
+               isl_die(s->ctx, isl_error_invalid,
+                       "expecting single affine expression", goto error);
+       if (!isl_set_plain_is_universe(pwaff->p[0].set))
+               isl_die(s->ctx, isl_error_invalid,
+                       "expecting universe domain", goto error);
+
+       aff = isl_aff_copy(pwaff->p[0].aff);
+
+       vars_free(v);
+       isl_pw_aff_free(pwaff);
+       isl_set_free(dom);
+       return aff;
+error:
+       vars_free(v);
+       isl_pw_aff_free(pwaff);
+       isl_set_free(dom);
+       return NULL;
+}
+
+/* Is the next token an identifer not in "v"?
+ */
+static int next_is_fresh_ident(struct isl_stream *s, struct vars *v)
+{
+       int n = v->n;
+       int fresh;
+       struct isl_token *tok;
+
+       tok = isl_stream_next_token(s);
+       if (!tok)
+               return 0;
+       fresh = tok->type == ISL_TOKEN_IDENT && vars_pos(v, tok->u.s, -1) >= n;
+       isl_stream_push_token(s, tok);
+
+       vars_drop(v, v->n - n);
+
+       return fresh;
+}
+
+/* First read the domain of the affine expression, which may be
+ * a parameter space or a set.
+ * The tricky part is that we don't know if the domain is a set or not,
+ * so when we are trying to read the domain, we may actually be reading
+ * the affine expression itself (defined on a parameter domains)
+ * If the tuple we are reading is named, we assume it's the domain.
+ * Also, if inside the tuple, the first thing we find is a nested tuple
+ * or a new identifier, we again assume it's the domain.
+ * Otherwise, we assume we are reading an affine expression.
+ */
+static __isl_give isl_set *read_aff_domain(struct isl_stream *s,
+       __isl_take isl_set *dom, struct vars *v)
+{
+       struct isl_token *tok;
+
+       tok = isl_stream_next_token(s);
+       if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
+               isl_stream_push_token(s, tok);
+               return read_tuple(s, dom, isl_dim_set, v);
+       }
+       if (!tok || tok->type != '[') {
+               isl_stream_error(s, tok, "expecting '['");
+               goto error;
+       }
+       if (next_is_tuple(s) || next_is_fresh_ident(s, v)) {
+               isl_stream_push_token(s, tok);
+               dom = read_tuple(s, dom, isl_dim_set, v);
+       } else
+               isl_stream_push_token(s, tok);
+
+       return dom;
+error:
+       if (tok)
+               isl_stream_push_token(s, tok);
+       vars_free(v);
+       isl_set_free(dom);
+       return NULL;
+}
+
+/* Read an affine expression from "s".
+ * We first read the domain of the affine expression, which may be
+ * a parameter space or a set, and then call read_aff_with_dom.
+ */
+__isl_give isl_aff *isl_stream_read_aff(struct isl_stream *s)
+{
+       struct vars *v;
+       isl_set *dom = NULL;
+
+       v = vars_new(s->ctx);
+       if (!v)
+               return NULL;
+
+       dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
+       if (next_is_tuple(s)) {
+               dom = read_tuple(s, dom, isl_dim_param, v);
+               if (isl_stream_eat(s, ISL_TOKEN_TO))
+                       goto error;
+       }
+       if (isl_stream_eat(s, '{'))
+               goto error;
+
+       dom = read_aff_domain(s, dom, v);
+       return read_aff_with_dom(s, dom, v);
+error:
+       vars_free(v);
+       isl_set_free(dom);
+       return NULL;
+}
+
+/* Read a piecewise affine expression from "s" with domain (space) "dom".
+ */
+static __isl_give isl_pw_aff *read_pw_aff_with_dom(struct isl_stream *s,
+       __isl_take isl_set *dom, struct vars *v)
+{
+       isl_pw_aff *pwaff = NULL;
+
+       if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
+               goto error;
+
+       if (isl_stream_eat(s, '['))
+               goto error;
+
+       pwaff = accept_affine(s, isl_set_get_space(dom), v);
+
+       if (isl_stream_eat(s, ']'))
+               goto error;
+
+       dom = read_optional_disjuncts(s, dom, v);
+       pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
+
+       return pwaff;
+error:
+       isl_set_free(dom);
+       isl_pw_aff_free(pwaff);
+       return NULL;
+}
+
+__isl_give isl_pw_aff *isl_stream_read_pw_aff(struct isl_stream *s)
+{
+       struct vars *v;
+       isl_set *dom = NULL;
+       isl_set *aff_dom;
+       isl_pw_aff *pa = NULL;
+       int n;
+
+       v = vars_new(s->ctx);
+       if (!v)
+               return NULL;
+
+       dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
+       if (next_is_tuple(s)) {
+               dom = read_tuple(s, dom, isl_dim_param, v);
+               if (isl_stream_eat(s, ISL_TOKEN_TO))
+                       goto error;
+       }
+       if (isl_stream_eat(s, '{'))
+               goto error;
+
+       n = v->n;
+       aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
+       pa = read_pw_aff_with_dom(s, aff_dom, v);
+       vars_drop(v, v->n - n);
+
+       while (isl_stream_eat_if_available(s, ';')) {
+               isl_pw_aff *pa_i;
+
+               n = v->n;
+               aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
+               pa_i = read_pw_aff_with_dom(s, aff_dom, v);
+               vars_drop(v, v->n - n);
+
+               pa = isl_pw_aff_union_add(pa, pa_i);
+       }
+
+       if (isl_stream_eat(s, '}'))
+               goto error;
+
+       vars_free(v);
+       isl_set_free(dom);
+       return pa;
+error:
+       vars_free(v);
+       isl_set_free(dom);
+       isl_pw_aff_free(pa);
+       return NULL;
+}
+
+__isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
+{
+       isl_aff *aff;
+       struct isl_stream *s = isl_stream_new_str(ctx, str);
+       if (!s)
+               return NULL;
+       aff = isl_stream_read_aff(s);
+       isl_stream_free(s);
+       return aff;
+}
+
+__isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
+{
+       isl_pw_aff *pa;
+       struct isl_stream *s = isl_stream_new_str(ctx, str);
+       if (!s)
+               return NULL;
+       pa = isl_stream_read_pw_aff(s);
+       isl_stream_free(s);
+       return pa;
+}
+
+/* Read an isl_pw_multi_aff from "s".
+ * We currently read a generic object and if it turns out to be a set or
+ * a map, we convert that to an isl_pw_multi_aff.
+ * It would be more efficient if we were to construct the isl_pw_multi_aff
+ * directly.
+ */
+__isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(struct isl_stream *s)
+{
+       struct isl_obj obj;
+
+       obj = obj_read(s);
+       if (!obj.v)
+               return NULL;
+
+       if (obj.type == isl_obj_map)
+               return isl_pw_multi_aff_from_map(obj.v);
+       if (obj.type == isl_obj_set)
+               return isl_pw_multi_aff_from_set(obj.v);
+
+       obj.type->free(obj.v);
+       isl_die(s->ctx, isl_error_invalid, "unexpected object type",
+               return NULL);
+}
+
+__isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
+       const char *str)
+{
+       isl_pw_multi_aff *pma;
+       struct isl_stream *s = isl_stream_new_str(ctx, str);
+       if (!s)
+               return NULL;
+       pma = isl_stream_read_pw_multi_aff(s);
+       isl_stream_free(s);
+       return pma;
+}
+
+/* Read a multi-affine expression from "s".
+ * We call isl_stream_read_pw_multi_aff to parse a possibly piecewise
+ * multi-affine expression and then check that the result is
+ * a single multi-affine expression on a universe domain.
+ */
+__isl_give isl_multi_aff *isl_stream_read_multi_aff(struct isl_stream *s)
+{
+       isl_pw_multi_aff *pma;
+       isl_multi_aff *maff;
+
+       pma = isl_stream_read_pw_multi_aff(s);
+       if (!pma)
+               return NULL;
+       if (pma->n != 1)
+               isl_die(s->ctx, isl_error_invalid,
+                       "expecting single list of affine expressions",
+                       return isl_pw_multi_aff_free(pma));
+       if (!isl_set_plain_is_universe(pma->p[0].set))
+               isl_die(s->ctx, isl_error_invalid, "expecting universe domain",
+                       return isl_pw_multi_aff_free(pma));
+       maff = isl_multi_aff_copy(pma->p[0].maff);
+       isl_pw_multi_aff_free(pma);
+       return maff;
+}
+
+__isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
+       const char *str)
+{
+       isl_multi_aff *maff;
+       struct isl_stream *s = isl_stream_new_str(ctx, str);
+       if (!s)
+               return NULL;
+       maff = isl_stream_read_multi_aff(s);
+       isl_stream_free(s);
+       return maff;
+}
+
+__isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
+       struct isl_stream *s)
+{
+       struct isl_obj obj;
+
+       obj = obj_read(s);
+       if (obj.type == isl_obj_pw_qpolynomial) {
+               obj.type = isl_obj_union_pw_qpolynomial;
+               obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
+       }
+       if (obj.v)
+               isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
+                          goto error);
+
+       return obj.v;
+error:
+       obj.type->free(obj.v);
+       return NULL;
+}
+
+__isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
+       isl_ctx *ctx, const char *str)
+{
+       isl_union_pw_qpolynomial *upwqp;
+       struct isl_stream *s = isl_stream_new_str(ctx, str);
+       if (!s)
+               return NULL;
+       upwqp = isl_stream_read_union_pw_qpolynomial(s);
+       isl_stream_free(s);
+       return upwqp;
+}