add isl_set_complement
[platform/upstream/isl.git] / isl_input.c
index 1fdbc4e..12838bf 100644 (file)
@@ -120,38 +120,29 @@ static int vars_pos(struct vars *v, const char *s, int len)
        return pos;
 }
 
-static struct vars *read_var_list(struct isl_stream *s, struct vars *v)
+static __isl_give isl_basic_map *set_name(__isl_take isl_basic_map *bmap,
+       enum isl_dim_type type, unsigned pos, char *name)
 {
-       struct isl_token *tok;
+       char *prime;
 
-       while ((tok = isl_stream_next_token(s)) != NULL) {
-               int p;
-               int n = v->n;
+       if (!bmap)
+               return NULL;
+       if (!name)
+               return bmap;
 
-               if (tok->type != ISL_TOKEN_IDENT)
-                       break;
+       prime = strchr(name, '\'');
+       if (prime)
+               *prime = '\0';
+       bmap->dim = isl_dim_set_name(bmap->dim, type, pos, name);
+       if (prime)
+               *prime = '\'';
 
-               p = vars_pos(v, tok->u.s, -1);
-               if (p < 0)
-                       goto error;
-               if (p < n) {
-                       isl_stream_error(s, tok, "expecting unique identifier");
-                       goto error;
-               }
-               isl_token_free(tok);
-               tok = isl_stream_next_token(s);
-               if (!tok || tok->type != ',')
-                       break;
-
-               isl_token_free(tok);
-       }
-       if (tok)
-               isl_stream_push_token(s, tok);
+       if (!bmap->dim)
+               goto error;
 
-       return v;
+       return bmap;
 error:
-       isl_token_free(tok);
-       vars_free(v);
+       isl_basic_map_free(bmap);
        return NULL;
 }
 
@@ -179,6 +170,7 @@ static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
                                goto error;
                        if (pos >= n) {
                                isl_stream_error(s, tok, "unknown identifier");
+                               isl_token_free(tok);
                                goto error;
                        }
                        if (sign > 0)
@@ -200,6 +192,7 @@ static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
                                if (pos >= n) {
                                        isl_stream_error(s, tok2,
                                                "unknown identifier");
+                                       isl_token_free(tok);
                                        isl_token_free(tok2);
                                        goto error;
                                }
@@ -228,6 +221,75 @@ error:
        return NULL;
 }
 
+static __isl_give isl_basic_map *read_var_list(struct isl_stream *s,
+       __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
+{
+       int i = 0;
+       struct isl_token *tok;
+
+       while ((tok = isl_stream_next_token(s)) != NULL) {
+               int new_name = 0;
+
+               if (tok->type == ISL_TOKEN_IDENT) {
+                       int n = v->n;
+                       int p = vars_pos(v, tok->u.s, -1);
+                       if (p < 0)
+                               goto error;
+                       new_name = p >= n;
+               }
+
+               if (new_name) {
+                       bmap = isl_basic_map_add(bmap, type, 1);
+                       bmap = set_name(bmap, type, i, v->v->name);
+                       isl_token_free(tok);
+               } else if (tok->type == ISL_TOKEN_IDENT ||
+                          tok->type == ISL_TOKEN_VALUE) {
+                       struct isl_vec *vec;
+                       int k;
+                       if (type == isl_dim_param) {
+                               isl_stream_error(s, tok,
+                                               "expecting unique identifier");
+                               goto error;
+                       }
+                       isl_stream_push_token(s, tok);
+                       tok = NULL;
+                       vec = accept_affine(s, v);
+                       if (!vec)
+                               goto error;
+                       v->v = variable_new(v, "", 0, v->n);
+                       if (!v->v)
+                               goto error;
+                       v->n++;
+                       bmap = isl_basic_map_add(bmap, type, 1);
+                       bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
+                       k = isl_basic_map_alloc_equality(bmap);
+                       if (k >= 0) {
+                               isl_seq_cpy(bmap->eq[k], vec->el, vec->size);
+                               isl_int_set_si(bmap->eq[k][vec->size], -1);
+                       }
+                       isl_vec_free(vec);
+                       if (k < 0)
+                               goto error;
+               } else
+                       break;
+
+               tok = isl_stream_next_token(s);
+               if (!tok || tok->type != ',')
+                       break;
+
+               isl_token_free(tok);
+               i++;
+       }
+       if (tok)
+               isl_stream_push_token(s, tok);
+
+       return bmap;
+error:
+       isl_token_free(tok);
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
 static __isl_give isl_mat *accept_affine_list(struct isl_stream *s,
        struct vars *v)
 {
@@ -372,7 +434,8 @@ error:
        return NULL;
 }
 
-static struct vars *read_tuple(struct isl_stream *s, struct vars *v)
+static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
+       __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
 {
        struct isl_token *tok;
 
@@ -382,7 +445,7 @@ static struct vars *read_tuple(struct isl_stream *s, struct vars *v)
                goto error;
        }
        isl_token_free(tok);
-       v = read_var_list(s, v);
+       bmap = read_var_list(s, bmap, type, v);
        tok = isl_stream_next_token(s);
        if (!tok || tok->type != ']') {
                isl_stream_error(s, tok, "expecting ']'");
@@ -390,11 +453,11 @@ static struct vars *read_tuple(struct isl_stream *s, struct vars *v)
        }
        isl_token_free(tok);
 
-       return v;
+       return bmap;
 error:
        if (tok)
                isl_token_free(tok);
-       vars_free(v);
+       isl_basic_map_free(bmap);
        return NULL;
 }
 
@@ -597,16 +660,34 @@ error:
 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
        struct vars *v, __isl_take isl_dim *dim)
 {
+       int seen_paren = 0;
+       struct isl_token *tok;
        struct isl_basic_map *bmap;
 
        bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
        if (!bmap)
                return NULL;
 
+       tok = isl_stream_next_token(s);
+       if (!tok)
+               goto error;
+       if (tok->type == '(') {
+               seen_paren = 1;
+               isl_token_free(tok);
+       } else
+               isl_stream_push_token(s, tok);
+
        bmap = add_constraints(s, v, bmap);
        bmap = isl_basic_map_simplify(bmap);
        bmap = isl_basic_map_finalize(bmap);
+
+       if (seen_paren && isl_stream_eat(s, ')'))
+               goto error;
+
        return bmap;
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
 }
 
 static struct isl_map *read_disjuncts(struct isl_stream *s,
@@ -840,6 +921,13 @@ static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
        for (i = 0; i < n_row; ++i)
                bmap = basic_map_read_polylib_constraint(s, bmap);
 
+       tok = isl_stream_next_token_on_same_line(s);
+       if (tok) {
+               isl_stream_error(s, tok, "unexpected extra token on line");
+               isl_stream_push_token(s, tok);
+               goto error;
+       }
+
        bmap = isl_basic_map_simplify(bmap);
        bmap = isl_basic_map_finalize(bmap);
        return bmap;
@@ -860,18 +948,12 @@ static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
                isl_stream_error(s, NULL, "unexpected EOF");
                return NULL;
        }
-       tok2 = isl_stream_next_token(s);
-       if (!tok2) {
-               isl_token_free(tok);
-               isl_stream_error(s, NULL, "unexpected EOF");
-               return NULL;
-       }
-       if (!tok2->on_new_line) {
+       tok2 = isl_stream_next_token_on_same_line(s);
+       if (tok2) {
                isl_stream_push_token(s, tok2);
                isl_stream_push_token(s, tok);
                return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
        }
-       isl_stream_push_token(s, tok2);
        n = isl_int_get_si(tok->u.v);
        isl_token_free(tok);
 
@@ -886,41 +968,55 @@ static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
        return map;
 }
 
-static struct isl_dim *set_names(struct isl_dim *dim, struct vars *vars,
-       enum isl_dim_type type, int offset, int n)
+static struct isl_map *map_read_body(struct isl_stream *s,
+       __isl_take isl_basic_map *bmap, struct vars *v)
 {
-       int i;
-       struct variable *v;
-
-       for (i = 0, v = vars->v; i < offset; ++i, v = v->next)
-               ;
-       for (i = n - 1; i >= 0; --i, v = v->next)
-               dim = isl_dim_set_name(dim, type, i, v->name);
-
-       return dim;
-}
+       struct isl_map *map = NULL;
+       struct isl_token *tok;
+       int n = v->n;
 
-static struct isl_dim *dim_from_vars(struct vars *vars,
-       int nparam, int n_in, int n_out)
-{
-       struct isl_dim *dim;
+       bmap = read_tuple(s, bmap, isl_dim_in, v);
+       if (!bmap)
+               goto error;
+       tok = isl_stream_next_token(s);
+       if (tok && tok->type == ISL_TOKEN_TO) {
+               isl_token_free(tok);
+               bmap = read_tuple(s, bmap, isl_dim_out, v);
+               if (!bmap)
+                       goto error;
+       } else {
+               bmap = isl_basic_map_reverse(bmap);
+               if (tok)
+                       isl_stream_push_token(s, tok);
+       }
+       tok = isl_stream_next_token(s);
+       if (!tok) {
+               isl_stream_error(s, NULL, "unexpected EOF");
+               goto error;
+       }
+       map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
+       if (tok->type == ':') {
+               isl_token_free(tok);
+               map = isl_map_intersect(map,
+                           read_disjuncts(s, v, isl_basic_map_get_dim(bmap)));
+       } else
+               isl_stream_push_token(s, tok);
 
-       dim = isl_dim_alloc(vars->ctx, nparam, n_in, n_out);
-       dim = set_names(dim, vars, isl_dim_param, n_out + n_in, nparam);
-       dim = set_names(dim, vars, isl_dim_in, n_out, n_in);
-       dim = set_names(dim, vars, isl_dim_out, 0, n_out);
+       vars_drop(v, v->n - n);
 
-       return dim;
+       isl_basic_map_free(bmap);
+       return map;
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
 }
 
 static struct isl_map *map_read(struct isl_stream *s, int nparam)
 {
-       struct isl_dim *dim = NULL;
+       struct isl_basic_map *bmap = NULL;
        struct isl_map *map = NULL;
        struct isl_token *tok;
        struct vars *v = NULL;
-       int n1;
-       int n2;
 
        tok = isl_stream_next_token(s);
        if (!tok) {
@@ -932,14 +1028,14 @@ static struct isl_map *map_read(struct isl_stream *s, int nparam)
                return map_read_polylib(s, nparam);
        }
        v = vars_new(s->ctx);
+       bmap = isl_basic_map_alloc(s->ctx, 0, 0, 0, 0, 0, 0);
        if (tok->type == '[') {
                isl_stream_push_token(s, tok);
-               v = read_tuple(s, v);
-               if (!v)
-                       return NULL;
+               bmap = read_tuple(s, bmap, isl_dim_param, v);
+               if (!bmap)
+                       goto error;
                if (nparam >= 0)
                        isl_assert(s->ctx, nparam == v->n, goto error);
-               nparam = v->n;
                tok = isl_stream_next_token(s);
                if (!tok || tok->type != ISL_TOKEN_TO) {
                        isl_stream_error(s, tok, "expecting '->'");
@@ -949,9 +1045,8 @@ static struct isl_map *map_read(struct isl_stream *s, int nparam)
                }
                isl_token_free(tok);
                tok = isl_stream_next_token(s);
-       }
-       if (nparam < 0)
-               nparam = 0;
+       } if (nparam > 0)
+               bmap = isl_basic_map_add(bmap, isl_dim_param, nparam);
        if (!tok || tok->type != '{') {
                isl_stream_error(s, tok, "expecting '{'");
                if (tok)
@@ -959,35 +1054,21 @@ static struct isl_map *map_read(struct isl_stream *s, int nparam)
                goto error;
        }
        isl_token_free(tok);
-       v = read_tuple(s, v);
-       if (!v)
-               return NULL;
-       n1 = v->n - nparam;
-       tok = isl_stream_next_token(s);
-       if (tok && tok->type == ISL_TOKEN_TO) {
+
+       for (;;) {
+               isl_map *m = map_read_body(s, isl_basic_map_copy(bmap), v);
+               if (!m)
+                       break;
+               if (map)
+                       map = isl_map_union(map, m);
+               else
+                       map = m;
+               tok = isl_stream_next_token(s);
+               if (!tok || tok->type != ';')
+                       break;
                isl_token_free(tok);
-               v = read_tuple(s, v);
-               if (!v)
-                       return NULL;
-               n2 = v->n - n1 - nparam;
-       } else {
-               if (tok)
-                       isl_stream_push_token(s, tok);
-               n2 = n1;
-               n1 = 0;
-       }
-       dim = dim_from_vars(v, nparam, n1, n2);
-       tok = isl_stream_next_token(s);
-       if (!tok) {
-               isl_stream_error(s, NULL, "unexpected EOF");
-               goto error;
        }
-       if (tok->type == ':') {
-               isl_token_free(tok);
-               map = read_disjuncts(s, v, isl_dim_copy(dim));
-               tok = isl_stream_next_token(s);
-       } else
-               map = isl_map_universe(isl_dim_copy(dim));
+
        if (tok && tok->type == '}') {
                isl_token_free(tok);
        } else {
@@ -997,11 +1078,11 @@ static struct isl_map *map_read(struct isl_stream *s, int nparam)
                goto error;
        }
        vars_free(v);
-       isl_dim_free(dim);
+       isl_basic_map_free(bmap);
 
        return map;
 error:
-       isl_dim_free(dim);
+       isl_basic_map_free(bmap);
        isl_map_free(map);
        if (v)
                vars_free(v);