isl_input.c: obj_read: start off from parameter space
[platform/upstream/isl.git] / isl_input.c
index 175c2ec..c36dfef 100644 (file)
@@ -18,7 +18,7 @@
 #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 +245,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 +265,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 +303,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 +339,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 +358,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 +374,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(isl_local_space_from_space(isl_space_copy(dim)));
                if (!aff)
                        goto error;
                isl_int_set_si(aff->v->el[2 + pos], 1);
@@ -378,12 +382,12 @@ 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));
+                       ls = isl_local_space_from_space(isl_space_copy(dim));
                        aff = isl_aff_zero(ls);
                        aff = isl_aff_add_constant(aff, tok->u.v);
                        res = isl_pw_aff_from_aff(aff);
@@ -392,7 +396,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,17 +406,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_dim_free(dim);
+       if (isl_stream_eat_if_available(s, '%') ||
+           isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
+               isl_space_free(dim);
                return affine_mod(s, v, res);
        }
        if (isl_stream_eat_if_available(s, '*')) {
@@ -427,13 +432,13 @@ 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;
 }
 
@@ -441,21 +446,21 @@ static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
 {
        isl_aff *aff;
 
-       aff = isl_aff_zero(isl_local_space_from_dim(isl_pw_aff_get_dim(pwaff)));
+       aff = isl_aff_zero(isl_local_space_from_space(isl_pw_aff_get_space(pwaff)));
        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));
+       ls = isl_local_space_from_space(isl_space_copy(dim));
        res = isl_pw_aff_from_aff(isl_aff_zero(ls));
        if (!res)
                goto error;
@@ -475,11 +480,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
@@ -494,7 +500,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)
@@ -507,7 +513,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);
@@ -529,15 +535,122 @@ 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;
+
+       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;
+
+       pwaff2 = accept_extended_affine(s, isl_pw_aff_get_space(pwaff1), v);
+       if (!pwaff1)
+               goto error;
+
+       return isl_pw_aff_cond(isl_map_wrap(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_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;
+
+       cond = isl_map_universe(isl_space_unwrap(isl_pw_aff_get_space(pwaff)));
+
+       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)
 {
@@ -545,14 +658,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);
@@ -584,6 +702,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,
@@ -620,13 +740,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;
@@ -643,17 +763,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;
 }
@@ -730,15 +850,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;
@@ -764,25 +883,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);
                } 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
@@ -825,6 +945,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));
@@ -832,23 +955,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)
 {
@@ -857,7 +963,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);
@@ -869,7 +975,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;
 
@@ -897,9 +1003,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)
 {
@@ -925,13 +1028,102 @@ 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, ISL_TOKEN_EXISTS) ||
+           isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
+           isl_stream_next_token_is(s, ISL_TOKEN_FALSE)) {
                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;
        }
 
@@ -942,7 +1134,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);
        }
@@ -985,7 +1177,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);
        }
@@ -1315,12 +1507,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(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(isl_map_get_space(map),
                                                tok->u.v);
                }
                isl_token_free(tok);
@@ -1328,12 +1520,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(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(isl_map_get_space(map));
                pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
        } else if (tok->type == ISL_TOKEN_IDENT) {
                int n = v->n;
@@ -1352,14 +1544,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(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_affine(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);
@@ -1471,25 +1663,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);
@@ -1498,8 +1689,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);
@@ -1507,7 +1697,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;
@@ -1541,6 +1731,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);
 
@@ -1552,8 +1748,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)
@@ -1623,21 +1819,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);
        }
@@ -1692,7 +1888,7 @@ 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);
@@ -1733,7 +1929,7 @@ static struct isl_obj obj_read(struct isl_stream *s, int nparam)
                        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