add isl_union_pw_multi_aff
[platform/upstream/isl.git] / isl_input.c
index e14a1b0..495366f 100644 (file)
@@ -17,7 +17,6 @@
 #include <isl_map_private.h>
 #include <isl/set.h>
 #include <isl/seq.h>
-#include <isl/div.h>
 #include <isl_stream_private.h>
 #include <isl/obj.h>
 #include "isl_polynomial_private.h"
@@ -374,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_space(isl_space_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);
@@ -388,7 +387,7 @@ static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
                        isl_local_space *ls;
                        isl_aff *aff;
                        ls = isl_local_space_from_space(isl_space_copy(dim));
-                       aff = isl_aff_zero(ls);
+                       aff = isl_aff_zero_on_domain(ls);
                        aff = isl_aff_add_constant(aff, tok->u.v);
                        res = isl_pw_aff_from_aff(aff);
                }
@@ -445,8 +444,10 @@ error2:
 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_space(isl_pw_aff_get_space(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));
@@ -461,7 +462,7 @@ static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
        int sign = 1;
 
        ls = isl_local_space_from_space(isl_space_copy(dim));
-       res = isl_pw_aff_from_aff(isl_aff_zero(ls));
+       res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
        if (!res)
                goto error;
 
@@ -573,7 +574,7 @@ 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;
+       isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL, *pa_cond;
 
        if (!cond)
                return NULL;
@@ -589,11 +590,13 @@ static __isl_give isl_pw_aff *accept_ternary(struct isl_stream *s,
        if (isl_stream_eat(s, ':'))
                goto error;
 
-       pwaff2 = accept_extended_affine(s, isl_pw_aff_get_space(pwaff1), v);
+       dim = isl_pw_aff_get_domain_space(pwaff1);
+       pwaff2 = accept_extended_affine(s, dim, v);
        if (!pwaff1)
                goto error;
 
-       return isl_pw_aff_cond(isl_map_wrap(cond), pwaff1, pwaff2);
+       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);
@@ -610,6 +613,7 @@ error:
 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;
@@ -642,7 +646,8 @@ static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
        tok->type = ISL_TOKEN_AFF;
        tok->u.pwaff = pwaff;
 
-       cond = isl_map_universe(isl_space_unwrap(isl_pw_aff_get_space(pwaff)));
+       space = isl_pw_aff_get_domain_space(pwaff);
+       cond = isl_map_universe(isl_space_unwrap(space));
 
        isl_stream_push_token(s, tok);
 
@@ -685,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;
@@ -850,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;
@@ -887,18 +891,19 @@ static __isl_give isl_map *read_tuple(struct isl_stream *s,
                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_map *nested;
-               if (type == isl_dim_out)
+               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_space_range(isl_map_get_space(nested));
+                       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);
@@ -1054,9 +1059,15 @@ static int resolve_paren_expr(struct isl_stream *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_FALSE) ||
+           isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
                map = read_disjuncts(s, v, map);
                if (isl_stream_eat(s, ')'))
                        goto error;
@@ -1287,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;
@@ -1297,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) {
@@ -1406,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;
@@ -1422,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");
@@ -1435,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;
 }
@@ -1507,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_space(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_space(map),
+                       qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
                                                tok->u.v);
                }
                isl_token_free(tok);
@@ -1520,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_space(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_space(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;
@@ -1544,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_space(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_space(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);
@@ -1847,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;
@@ -1875,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);
@@ -1888,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_space_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 '->'");
@@ -1905,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)
@@ -1925,8 +1933,6 @@ 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_space(map));
@@ -1982,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);
@@ -2000,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:
@@ -2018,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);
@@ -2041,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);
@@ -2055,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;
@@ -2081,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,
@@ -2289,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);
@@ -2323,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;
+}