add isl_set_flatten
[platform/upstream/isl.git] / isl_input.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2010      INRIA Saclay
4  *
5  * Use of this software is governed by the GNU LGPLv2.1 license
6  *
7  * Written by Sven Verdoolaege, K.U.Leuven, Departement
8  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
11  */
12
13 #include <ctype.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <strings.h>
17 #include <isl_set.h>
18 #include <isl_seq.h>
19 #include <isl_div.h>
20 #include "isl_stream.h"
21 #include "isl_map_private.h"
22 #include "isl_obj.h"
23 #include "isl_polynomial_private.h"
24 #include <isl_union_map.h>
25 #include <isl_mat_private.h>
26
27 struct variable {
28         char                    *name;
29         int                      pos;
30         isl_vec                 *def;
31         struct variable         *next;
32 };
33
34 struct vars {
35         struct isl_ctx  *ctx;
36         int              n;
37         struct variable *v;
38 };
39
40 static struct vars *vars_new(struct isl_ctx *ctx)
41 {
42         struct vars *v;
43         v = isl_alloc_type(ctx, struct vars);
44         if (!v)
45                 return NULL;
46         v->ctx = ctx;
47         v->n = 0;
48         v->v = NULL;
49         return v;
50 }
51
52 static void variable_free(struct variable *var)
53 {
54         while (var) {
55                 struct variable *next = var->next;
56                 isl_vec_free(var->def);
57                 free(var->name);
58                 free(var);
59                 var = next;
60         }
61 }
62
63 static void vars_free(struct vars *v)
64 {
65         if (!v)
66                 return;
67         variable_free(v->v);
68         free(v);
69 }
70
71 static void vars_drop(struct vars *v, int n)
72 {
73         struct variable *var;
74
75         if (!v || !v->v)
76                 return;
77
78         v->n -= n;
79
80         var = v->v;
81         while (--n >= 0) {
82                 struct variable *next = var->next;
83                 isl_vec_free(var->def);
84                 free(var->name);
85                 free(var);
86                 var = next;
87         }
88         v->v = var;
89 }
90
91 static struct variable *variable_new(struct vars *v, const char *name, int len,
92                                 int pos)
93 {
94         struct variable *var;
95         var = isl_alloc_type(v->ctx, struct variable);
96         if (!var)
97                 goto error;
98         var->name = strdup(name);
99         var->name[len] = '\0';
100         var->pos = pos;
101         var->def = NULL;
102         var->next = v->v;
103         return var;
104 error:
105         variable_free(v->v);
106         return NULL;
107 }
108
109 static int vars_pos(struct vars *v, const char *s, int len)
110 {
111         int pos;
112         struct variable *q;
113
114         if (len == -1)
115                 len = strlen(s);
116         for (q = v->v; q; q = q->next) {
117                 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
118                         break;
119         }
120         if (q)
121                 pos = q->pos;
122         else {
123                 pos = v->n;
124                 v->v = variable_new(v, s, len, v->n);
125                 if (!v->v)
126                         return -1;
127                 v->n++;
128         }
129         return pos;
130 }
131
132 static int vars_add_anon(struct vars *v)
133 {
134         v->v = variable_new(v, "", 0, v->n);
135
136         if (!v->v)
137                 return -1;
138         v->n++;
139
140         return 0;
141 }
142
143 static __isl_give isl_basic_map *set_name(__isl_take isl_basic_map *bmap,
144         enum isl_dim_type type, unsigned pos, char *name)
145 {
146         char *prime;
147
148         if (!bmap)
149                 return NULL;
150         if (!name)
151                 return bmap;
152
153         prime = strchr(name, '\'');
154         if (prime)
155                 *prime = '\0';
156         bmap = isl_basic_map_set_dim_name(bmap, type, pos, name);
157         if (prime)
158                 *prime = '\'';
159
160         return bmap;
161 }
162
163 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
164 {
165         struct isl_token *tok;
166
167         tok = isl_stream_next_token(s);
168         if (!tok || tok->type != ISL_TOKEN_VALUE) {
169                 isl_stream_error(s, tok, "expecting constant value");
170                 goto error;
171         }
172
173         isl_int_mul(*f, *f, tok->u.v);
174
175         isl_token_free(tok);
176
177         if (isl_stream_eat_if_available(s, '*'))
178                 return accept_cst_factor(s, f);
179
180         return 0;
181 error:
182         isl_token_free(tok);
183         return -1;
184 }
185
186 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v);
187 static int read_div_definition(struct isl_stream *s, struct vars *v);
188
189 static __isl_give isl_vec *accept_affine_factor(struct isl_stream *s,
190         struct vars *v)
191 {
192         struct isl_token *tok = NULL;
193         isl_vec *aff = NULL;
194
195         tok = isl_stream_next_token(s);
196         if (!tok) {
197                 isl_stream_error(s, NULL, "unexpected EOF");
198                 goto error;
199         }
200         if (tok->type == ISL_TOKEN_IDENT) {
201                 int n = v->n;
202                 int pos = vars_pos(v, tok->u.s, -1);
203                 if (pos < 0)
204                         goto error;
205                 if (pos >= n) {
206                         isl_stream_error(s, tok, "unknown identifier");
207                         goto error;
208                 }
209
210                 aff = isl_vec_alloc(v->ctx, 1 + v->n);
211                 if (!aff)
212                         goto error;
213                 isl_seq_clr(aff->el, aff->size);
214                 isl_int_set_si(aff->el[1 + pos], 1);
215                 isl_token_free(tok);
216         } else if (tok->type == ISL_TOKEN_VALUE) {
217                 if (isl_stream_eat_if_available(s, '*')) {
218                         aff = accept_affine_factor(s, v);
219                         aff = isl_vec_scale(aff, tok->u.v);
220                 } else {
221                         aff = isl_vec_alloc(v->ctx, 1 + v->n);
222                         if (!aff)
223                                 goto error;
224                         isl_seq_clr(aff->el, aff->size);
225                         isl_int_set(aff->el[0], tok->u.v);
226                 }
227                 isl_token_free(tok);
228         } else if (tok->type == '(') {
229                 isl_token_free(tok);
230                 tok = NULL;
231                 aff = accept_affine(s, v);
232                 if (!aff)
233                         goto error;
234                 if (isl_stream_eat(s, ')'))
235                         goto error;
236         } else if (tok->type == '[') {
237                 if (vars_add_anon(v) < 0)
238                         goto error;
239                 aff = isl_vec_alloc(v->ctx, 1 + v->n);
240                 if (!aff)
241                         goto error;
242                 isl_seq_clr(aff->el, aff->size);
243                 isl_int_set_si(aff->el[1 + v->n - 1], 1);
244                 isl_stream_push_token(s, tok);
245                 tok = NULL;
246                 if (read_div_definition(s, v) < 0)
247                         goto error;
248         } else {
249                 isl_stream_error(s, tok, "expecting factor");
250                 goto error;
251         }
252         if (isl_stream_eat_if_available(s, '*')) {
253                 isl_int f;
254                 isl_int_init(f);
255                 isl_int_set_si(f, 1);
256                 if (accept_cst_factor(s, &f) < 0) {
257                         isl_int_clear(f);
258                         goto error;
259                 }
260                 aff = isl_vec_scale(aff, f);
261                 isl_int_clear(f);
262         }
263
264         return aff;
265 error:
266         isl_token_free(tok);
267         isl_vec_free(aff);
268         return NULL;
269 }
270
271 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
272 {
273         struct isl_token *tok = NULL;
274         struct isl_vec *aff;
275         int sign = 1;
276
277         aff = isl_vec_alloc(v->ctx, 1 + v->n);
278         if (!aff)
279                 return NULL;
280         isl_seq_clr(aff->el, aff->size);
281
282         for (;;) {
283                 tok = isl_stream_next_token(s);
284                 if (!tok) {
285                         isl_stream_error(s, NULL, "unexpected EOF");
286                         goto error;
287                 }
288                 if (tok->type == '-') {
289                         sign = -sign;
290                         isl_token_free(tok);
291                         continue;
292                 }
293                 if (tok->type == '(' || tok->type == '[' ||
294                     tok->type == ISL_TOKEN_IDENT) {
295                         isl_vec *aff2;
296                         isl_stream_push_token(s, tok);
297                         tok = NULL;
298                         aff2 = accept_affine_factor(s, v);
299                         if (sign < 0)
300                                 aff2 = isl_vec_scale(aff2, s->ctx->negone);
301                         aff = isl_vec_zero_extend(aff, 1 + v->n);
302                         aff = isl_vec_add(aff, aff2);
303                         if (!aff)
304                                 goto error;
305                         sign = 1;
306                 } else if (tok->type == ISL_TOKEN_VALUE) {
307                         if (sign < 0)
308                                 isl_int_neg(tok->u.v, tok->u.v);
309                         if (isl_stream_eat_if_available(s, '*') ||
310                             isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
311                                 isl_vec *aff2;
312                                 aff2 = accept_affine_factor(s, v);
313                                 aff2 = isl_vec_scale(aff2, tok->u.v);
314                                 aff = isl_vec_zero_extend(aff, 1 + v->n);
315                                 aff = isl_vec_add(aff, aff2);
316                                 if (!aff)
317                                         goto error;
318                         } else {
319                                 isl_int_add(aff->el[0], aff->el[0], tok->u.v);
320                         }
321                         sign = 1;
322                 } else {
323                         isl_stream_error(s, tok, "unexpected isl_token");
324                         isl_stream_push_token(s, tok);
325                         isl_vec_free(aff);
326                         return NULL;
327                 }
328                 isl_token_free(tok);
329
330                 tok = isl_stream_next_token(s);
331                 if (tok && tok->type == '-') {
332                         sign = -sign;
333                         isl_token_free(tok);
334                 } else if (tok && tok->type == '+') {
335                         /* nothing */
336                         isl_token_free(tok);
337                 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
338                            isl_int_is_neg(tok->u.v)) {
339                         isl_stream_push_token(s, tok);
340                 } else {
341                         if (tok)
342                                 isl_stream_push_token(s, tok);
343                         break;
344                 }
345         }
346
347         return aff;
348 error:
349         isl_token_free(tok);
350         isl_vec_free(aff);
351         return NULL;
352 }
353
354 /* Add any variables in the variable list "v" that are not already in "bmap"
355  * as existentially quantified variables in "bmap".
356  */
357 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
358         struct vars *v)
359 {
360         int i;
361         int extra;
362         struct variable *var;
363
364         extra = v->n - isl_basic_map_total_dim(bmap);
365
366         if (extra == 0)
367                 return bmap;
368
369         bmap = isl_basic_map_extend_dim(bmap, isl_basic_map_get_dim(bmap),
370                                         extra, 0, 2 * extra);
371
372         for (i = 0; i < extra; ++i)
373                 if (isl_basic_map_alloc_div(bmap) < 0)
374                         goto error;
375
376         for (i = 0, var = v->v; i < extra; ++i, var = var->next) {
377                 int k = bmap->n_div - 1 - i;
378
379                 isl_seq_cpy(bmap->div[k], var->def->el, 2 + var->pos);
380                 isl_seq_clr(bmap->div[k] + 2 + var->pos, v->n - var->pos);
381
382                 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
383                         goto error;
384         }
385
386         return bmap;
387 error:
388         isl_basic_map_free(bmap);
389         return NULL;
390 }
391
392 static __isl_give isl_basic_map *read_var_def(struct isl_stream *s,
393         __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
394 {
395         struct isl_vec *vec;
396         int k;
397         int n;
398
399         if (vars_add_anon(v) < 0)
400                 goto error;
401         n = v->n;
402
403         vec = accept_affine(s, v);
404         if (!vec)
405                 goto error;
406
407         bmap = add_divs(bmap, v);
408         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
409         k = isl_basic_map_alloc_equality(bmap);
410         if (k >= 0) {
411                 isl_seq_cpy(bmap->eq[k], vec->el, vec->size);
412                 isl_int_set_si(bmap->eq[k][1 + n - 1], -1);
413         }
414         isl_vec_free(vec);
415         if (k < 0)
416                 goto error;
417
418         vars_drop(v, v->n - n);
419
420         return bmap;
421 error:
422         isl_basic_map_free(bmap);
423         return NULL;
424 }
425
426 static __isl_give isl_basic_map *read_var_list(struct isl_stream *s,
427         __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
428 {
429         int i = 0;
430         struct isl_token *tok;
431
432         while ((tok = isl_stream_next_token(s)) != NULL) {
433                 int new_name = 0;
434
435                 if (tok->type == ISL_TOKEN_IDENT) {
436                         int n = v->n;
437                         int p = vars_pos(v, tok->u.s, -1);
438                         if (p < 0)
439                                 goto error;
440                         new_name = p >= n;
441                 }
442
443                 if (new_name) {
444                         bmap = isl_basic_map_add(bmap, type, 1);
445                         bmap = set_name(bmap, type, i, v->v->name);
446                         isl_token_free(tok);
447                 } else if (tok->type == ISL_TOKEN_IDENT ||
448                            tok->type == ISL_TOKEN_VALUE ||
449                            tok->type == '-' ||
450                            tok->type == '(') {
451                         if (type == isl_dim_param) {
452                                 isl_stream_error(s, tok,
453                                                 "expecting unique identifier");
454                                 goto error;
455                         }
456                         isl_stream_push_token(s, tok);
457                         tok = NULL;
458                         bmap = isl_basic_map_add(bmap, type, 1);
459                         bmap = read_var_def(s, bmap, type, v);
460                 } else
461                         break;
462
463                 tok = isl_stream_next_token(s);
464                 if (!tok || tok->type != ',')
465                         break;
466
467                 isl_token_free(tok);
468                 i++;
469         }
470         if (tok)
471                 isl_stream_push_token(s, tok);
472
473         return bmap;
474 error:
475         isl_token_free(tok);
476         isl_basic_map_free(bmap);
477         return NULL;
478 }
479
480 static __isl_give isl_mat *accept_affine_list(struct isl_stream *s,
481         struct vars *v)
482 {
483         struct isl_vec *vec;
484         struct isl_mat *mat;
485         struct isl_token *tok = NULL;
486
487         vec = accept_affine(s, v);
488         mat = isl_mat_from_row_vec(vec);
489         if (!mat)
490                 return NULL;
491
492         for (;;) {
493                 tok = isl_stream_next_token(s);
494                 if (!tok) {
495                         isl_stream_error(s, NULL, "unexpected EOF");
496                         goto error;
497                 }
498                 if (tok->type != ',') {
499                         isl_stream_push_token(s, tok);
500                         break;
501                 }
502                 isl_token_free(tok);
503
504                 vec = accept_affine(s, v);
505                 mat = isl_mat_add_zero_cols(mat, 1 + v->n - isl_mat_cols(mat));
506                 mat = isl_mat_vec_concat(mat, vec);
507                 if (!mat)
508                         return NULL;
509         }
510
511         return mat;
512 error:
513         isl_mat_free(mat);
514         return NULL;
515 }
516
517 static int read_div_definition(struct isl_stream *s, struct vars *v)
518 {
519         struct isl_token *tok;
520         int seen_paren = 0;
521         struct isl_vec *aff;
522         struct variable *var;
523
524         if (isl_stream_eat(s, '['))
525                 return -1;
526
527         tok = isl_stream_next_token(s);
528         if (!tok)
529                 return -1;
530         if (tok->type == '(') {
531                 seen_paren = 1;
532                 isl_token_free(tok);
533         } else
534                 isl_stream_push_token(s, tok);
535
536         var = v->v;
537
538         aff = accept_affine(s, v);
539         if (!aff)
540                 return -1;
541
542         var->def = isl_vec_alloc(s->ctx, 2 + v->n);
543         if (!var->def) {
544                 isl_vec_free(aff);
545                 return -1;
546         }
547
548         isl_seq_cpy(var->def->el + 1, aff->el, aff->size);
549
550         isl_vec_free(aff);
551
552         if (seen_paren && isl_stream_eat(s, ')'))
553                 return -1;
554         if (isl_stream_eat(s, '/'))
555                 return -1;
556
557         tok = isl_stream_next_token(s);
558         if (!tok)
559                 return -1;
560         if (tok->type != ISL_TOKEN_VALUE) {
561                 isl_stream_error(s, tok, "expected denominator");
562                 isl_stream_push_token(s, tok);
563                 return -1;
564         }
565         isl_int_set(var->def->el[0], tok->u.v);
566         isl_token_free(tok);
567
568         if (isl_stream_eat(s, ']'))
569                 return -1;
570
571         return 0;
572 }
573
574 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
575         struct vars *v, struct isl_basic_map *bmap, int k)
576 {
577         struct variable *var = v->v;
578
579         if (read_div_definition(s, v) < 0)
580                 goto error;
581
582         isl_seq_cpy(bmap->div[k], var->def->el, 2 + v->n);
583
584         if (isl_basic_map_add_div_constraints(bmap, k) < 0)
585                 goto error;
586
587         return bmap;
588 error:
589         isl_basic_map_free(bmap);
590         return NULL;
591 }
592
593 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
594         struct vars *v, struct isl_basic_map *bmap)
595 {
596         struct isl_token *tok;
597
598         while ((tok = isl_stream_next_token(s)) != NULL) {
599                 int k;
600                 int p;
601                 int n = v->n;
602                 unsigned total = isl_basic_map_total_dim(bmap);
603
604                 if (tok->type != ISL_TOKEN_IDENT)
605                         break;
606
607                 p = vars_pos(v, tok->u.s, -1);
608                 if (p < 0)
609                         goto error;
610                 if (p < n) {
611                         isl_stream_error(s, tok, "expecting unique identifier");
612                         goto error;
613                 }
614
615                 bmap = isl_basic_map_cow(bmap);
616                 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
617                                                 1, 0, 2);
618
619                 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
620                         goto error;
621                 isl_seq_clr(bmap->div[k], 1 + 1 + total);
622
623                 isl_token_free(tok);
624                 tok = isl_stream_next_token(s);
625                 if (tok && tok->type == '=') {
626                         isl_token_free(tok);
627                         bmap = add_div_definition(s, v, bmap, k);
628                         tok = isl_stream_next_token(s);
629                 }
630
631                 if (!tok || tok->type != ',')
632                         break;
633
634                 isl_token_free(tok);
635         }
636         if (tok)
637                 isl_stream_push_token(s, tok);
638
639         return bmap;
640 error:
641         isl_token_free(tok);
642         isl_basic_map_free(bmap);
643         return NULL;
644 }
645
646 static int next_is_tuple(struct isl_stream *s)
647 {
648         struct isl_token *tok;
649         int is_tuple;
650
651         tok = isl_stream_next_token(s);
652         if (!tok)
653                 return 0;
654         if (tok->type == '[') {
655                 isl_stream_push_token(s, tok);
656                 return 1;
657         }
658         if (tok->type != ISL_TOKEN_IDENT) {
659                 isl_stream_push_token(s, tok);
660                 return 0;
661         }
662
663         is_tuple = isl_stream_next_token_is(s, '[');
664
665         isl_stream_push_token(s, tok);
666
667         return is_tuple;
668 }
669
670 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
671         __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v);
672
673 static __isl_give isl_basic_map *read_nested_tuple(struct isl_stream *s,
674         __isl_take isl_basic_map *bmap, struct vars *v)
675 {
676         bmap = read_tuple(s, bmap, isl_dim_in, v);
677         if (isl_stream_eat(s, ISL_TOKEN_TO))
678                 goto error;
679         bmap = read_tuple(s, bmap, isl_dim_out, v);
680         bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
681         return bmap;
682 error:
683         isl_basic_map_free(bmap);
684         return NULL;
685 }
686
687 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
688         __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
689 {
690         struct isl_token *tok;
691         char *name = NULL;
692
693         tok = isl_stream_next_token(s);
694         if (tok && tok->type == ISL_TOKEN_IDENT) {
695                 name = strdup(tok->u.s);
696                 if (!name)
697                         goto error;
698                 isl_token_free(tok);
699                 tok = isl_stream_next_token(s);
700         }
701         if (!tok || tok->type != '[') {
702                 isl_stream_error(s, tok, "expecting '['");
703                 goto error;
704         }
705         isl_token_free(tok);
706         if (type != isl_dim_param && next_is_tuple(s)) {
707                 isl_dim *dim = isl_basic_map_get_dim(bmap);
708                 int nparam = isl_dim_size(dim, isl_dim_param);
709                 int n_in = isl_dim_size(dim, isl_dim_in);
710                 isl_basic_map *nested;
711                 if (type == isl_dim_out)
712                         dim = isl_dim_move(dim, isl_dim_param, nparam,
713                                                 isl_dim_in, 0, n_in);
714                 nested = isl_basic_map_alloc_dim(dim, 0, 0, 0);
715                 nested = read_nested_tuple(s, nested, v);
716                 if (type == isl_dim_in) {
717                         nested = isl_basic_map_reverse(nested);
718                         bmap = isl_basic_map_intersect(nested, bmap);
719                 } else {
720                         isl_basic_set *bset;
721                         dim = isl_dim_range(isl_basic_map_get_dim(nested));
722                         dim = isl_dim_drop(dim, isl_dim_param, nparam, n_in);
723                         dim = isl_dim_join(isl_basic_map_get_dim(bmap), dim);
724                         bset = isl_basic_map_domain(bmap);
725                         nested = isl_basic_map_reset_dim(nested, dim);
726                         bmap = isl_basic_map_intersect_domain(nested, bset);
727                 }
728         } else
729                 bmap = read_var_list(s, bmap, type, v);
730         tok = isl_stream_next_token(s);
731         if (!tok || tok->type != ']') {
732                 isl_stream_error(s, tok, "expecting ']'");
733                 goto error;
734         }
735         isl_token_free(tok);
736
737         if (name) {
738                 bmap = isl_basic_map_set_tuple_name(bmap, type, name);
739                 free(name);
740         }
741
742         return bmap;
743 error:
744         if (tok)
745                 isl_token_free(tok);
746         isl_basic_map_free(bmap);
747         return NULL;
748 }
749
750 static struct isl_basic_map *add_constraints(struct isl_stream *s,
751         struct vars *v, struct isl_basic_map *bmap);
752
753 static struct isl_basic_map *add_exists(struct isl_stream *s,
754         struct vars *v, struct isl_basic_map *bmap)
755 {
756         struct isl_token *tok;
757         int n = v->n;
758         int extra;
759         int seen_paren = 0;
760         int i;
761         unsigned total;
762
763         tok = isl_stream_next_token(s);
764         if (!tok)
765                 goto error;
766         if (tok->type == '(') {
767                 seen_paren = 1;
768                 isl_token_free(tok);
769         } else
770                 isl_stream_push_token(s, tok);
771
772         bmap = read_defined_var_list(s, v, bmap);
773         if (!bmap)
774                 goto error;
775
776         if (isl_stream_eat(s, ':'))
777                 goto error;
778         bmap = add_constraints(s, v, bmap);
779         if (seen_paren && isl_stream_eat(s, ')'))
780                 goto error;
781         return bmap;
782 error:
783         isl_basic_map_free(bmap);
784         return NULL;
785 }
786
787 static __isl_give isl_basic_map *construct_constraint(
788         __isl_take isl_basic_map *bmap, enum isl_token_type type,
789         isl_int *left, isl_int *right)
790 {
791         int k;
792         unsigned len;
793         struct isl_ctx *ctx;
794
795         if (!bmap)
796                 return NULL;
797         len = 1 + isl_basic_map_total_dim(bmap);
798         ctx = bmap->ctx;
799
800         k = isl_basic_map_alloc_inequality(bmap);
801         if (k < 0)
802                 goto error;
803         if (type == ISL_TOKEN_LE)
804                 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
805                                                ctx->one, right,
806                                                len);
807         else if (type == ISL_TOKEN_GE)
808                 isl_seq_combine(bmap->ineq[k], ctx->one, left,
809                                                ctx->negone, right,
810                                                len);
811         else if (type == ISL_TOKEN_LT) {
812                 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
813                                                ctx->one, right,
814                                                len);
815                 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
816         } else if (type == ISL_TOKEN_GT) {
817                 isl_seq_combine(bmap->ineq[k], ctx->one, left,
818                                                ctx->negone, right,
819                                                len);
820                 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
821         } else {
822                 isl_seq_combine(bmap->ineq[k], ctx->one, left,
823                                                ctx->negone, right,
824                                                len);
825                 isl_basic_map_inequality_to_equality(bmap, k);
826         }
827
828         return bmap;
829 error:
830         isl_basic_map_free(bmap);
831         return NULL;
832 }
833
834 static int is_comparator(struct isl_token *tok)
835 {
836         if (!tok)
837                 return 0;
838
839         switch (tok->type) {
840         case ISL_TOKEN_LT:
841         case ISL_TOKEN_GT:
842         case ISL_TOKEN_LE:
843         case ISL_TOKEN_GE:
844         case '=':
845                 return 1;
846         default:
847                 return 0;
848         }
849 }
850
851 static struct isl_basic_map *add_constraint(struct isl_stream *s,
852         struct vars *v, struct isl_basic_map *bmap)
853 {
854         int i, j;
855         struct isl_token *tok = NULL;
856         struct isl_mat *aff1 = NULL, *aff2 = NULL;
857
858         tok = isl_stream_next_token(s);
859         if (!tok)
860                 goto error;
861         if (tok->type == ISL_TOKEN_EXISTS) {
862                 isl_token_free(tok);
863                 return add_exists(s, v, bmap);
864         }
865         isl_stream_push_token(s, tok);
866         tok = NULL;
867
868         bmap = isl_basic_map_cow(bmap);
869
870         aff1 = accept_affine_list(s, v);
871         if (!aff1)
872                 goto error;
873         tok = isl_stream_next_token(s);
874         if (!is_comparator(tok)) {
875                 isl_stream_error(s, tok, "missing operator");
876                 if (tok)
877                         isl_stream_push_token(s, tok);
878                 tok = NULL;
879                 goto error;
880         }
881         for (;;) {
882                 aff2 = accept_affine_list(s, v);
883                 if (!aff2)
884                         goto error;
885
886                 aff1 = isl_mat_add_zero_cols(aff1, aff2->n_col - aff1->n_col);
887                 if (!aff1)
888                         goto error;
889                 bmap = add_divs(bmap, v);
890                 bmap = isl_basic_map_extend_constraints(bmap, 0,
891                                                 aff1->n_row * aff2->n_row);
892                 for (i = 0; i < aff1->n_row; ++i)
893                         for (j = 0; j < aff2->n_row; ++j)
894                                 bmap = construct_constraint(bmap, tok->type,
895                                                     aff1->row[i], aff2->row[j]);
896                 isl_token_free(tok);
897                 isl_mat_free(aff1);
898                 aff1 = aff2;
899
900                 tok = isl_stream_next_token(s);
901                 if (!is_comparator(tok)) {
902                         if (tok)
903                                 isl_stream_push_token(s, tok);
904                         break;
905                 }
906         }
907         isl_mat_free(aff1);
908
909         return bmap;
910 error:
911         if (tok)
912                 isl_token_free(tok);
913         isl_mat_free(aff1);
914         isl_mat_free(aff2);
915         isl_basic_map_free(bmap);
916         return NULL;
917 }
918
919 static struct isl_basic_map *add_constraints(struct isl_stream *s,
920         struct vars *v, struct isl_basic_map *bmap)
921 {
922         struct isl_token *tok;
923
924         for (;;) {
925                 bmap = add_constraint(s, v, bmap);
926                 if (!bmap)
927                         return NULL;
928                 tok = isl_stream_next_token(s);
929                 if (!tok) {
930                         isl_stream_error(s, NULL, "unexpected EOF");
931                         goto error;
932                 }
933                 if (tok->type != ISL_TOKEN_AND)
934                         break;
935                 isl_token_free(tok);
936         }
937         isl_stream_push_token(s, tok);
938
939         return bmap;
940 error:
941         if (tok)
942                 isl_token_free(tok);
943         isl_basic_map_free(bmap);
944         return NULL;
945 }
946
947 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
948         struct vars *v, __isl_take isl_dim *dim)
949 {
950         int seen_paren = 0;
951         struct isl_token *tok;
952         struct isl_basic_map *bmap;
953
954         bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
955         if (!bmap)
956                 return NULL;
957
958         tok = isl_stream_next_token(s);
959         if (!tok)
960                 goto error;
961         if (tok->type == '(') {
962                 seen_paren = 1;
963                 isl_token_free(tok);
964         } else
965                 isl_stream_push_token(s, tok);
966
967         bmap = add_constraints(s, v, bmap);
968         bmap = isl_basic_map_simplify(bmap);
969         bmap = isl_basic_map_finalize(bmap);
970
971         if (seen_paren && isl_stream_eat(s, ')'))
972                 goto error;
973
974         return bmap;
975 error:
976         isl_basic_map_free(bmap);
977         return NULL;
978 }
979
980 static struct isl_map *read_disjuncts(struct isl_stream *s,
981         struct vars *v, __isl_take isl_dim *dim)
982 {
983         struct isl_token *tok;
984         struct isl_map *map;
985
986         tok = isl_stream_next_token(s);
987         if (!tok) {
988                 isl_stream_error(s, NULL, "unexpected EOF");
989                 goto error;
990         }
991         if (tok->type == '}') {
992                 isl_stream_push_token(s, tok);
993                 return isl_map_universe(dim);
994         }
995         isl_stream_push_token(s, tok);
996
997         map = isl_map_empty(isl_dim_copy(dim));
998         for (;;) {
999                 struct isl_basic_map *bmap;
1000                 int n = v->n;
1001
1002                 bmap = read_disjunct(s, v, isl_dim_copy(dim));
1003                 map = isl_map_union(map, isl_map_from_basic_map(bmap));
1004
1005                 vars_drop(v, v->n - n);
1006
1007                 tok = isl_stream_next_token(s);
1008                 if (!tok || tok->type != ISL_TOKEN_OR)
1009                         break;
1010                 isl_token_free(tok);
1011         }
1012         if (tok)
1013                 isl_stream_push_token(s, tok);
1014
1015         isl_dim_free(dim);
1016         return map;
1017 error:
1018         isl_dim_free(dim);
1019         return NULL;
1020 }
1021
1022 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1023 {
1024         if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1025                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1026                            isl_basic_map_dim(bmap, isl_dim_in) + pos;
1027         pos -= isl_basic_map_dim(bmap, isl_dim_out);
1028
1029         if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1030                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1031         pos -= isl_basic_map_dim(bmap, isl_dim_in);
1032
1033         if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1034                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1035                            isl_basic_map_dim(bmap, isl_dim_in) +
1036                            isl_basic_map_dim(bmap, isl_dim_out) + pos;
1037         pos -= isl_basic_map_dim(bmap, isl_dim_div);
1038
1039         if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1040                 return 1 + pos;
1041
1042         return 0;
1043 }
1044
1045 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1046         struct isl_stream *s, __isl_take isl_basic_map *bmap)
1047 {
1048         int j;
1049         struct isl_token *tok;
1050         int type;
1051         int k;
1052         isl_int *c;
1053         unsigned nparam;
1054         unsigned dim;
1055
1056         if (!bmap)
1057                 return NULL;
1058
1059         nparam = isl_basic_map_dim(bmap, isl_dim_param);
1060         dim = isl_basic_map_dim(bmap, isl_dim_out);
1061
1062         tok = isl_stream_next_token(s);
1063         if (!tok || tok->type != ISL_TOKEN_VALUE) {
1064                 isl_stream_error(s, tok, "expecting coefficient");
1065                 if (tok)
1066                         isl_stream_push_token(s, tok);
1067                 goto error;
1068         }
1069         if (!tok->on_new_line) {
1070                 isl_stream_error(s, tok, "coefficient should appear on new line");
1071                 isl_stream_push_token(s, tok);
1072                 goto error;
1073         }
1074
1075         type = isl_int_get_si(tok->u.v);
1076         isl_token_free(tok);
1077
1078         isl_assert(s->ctx, type == 0 || type == 1, goto error);
1079         if (type == 0) {
1080                 k = isl_basic_map_alloc_equality(bmap);
1081                 c = bmap->eq[k];
1082         } else {
1083                 k = isl_basic_map_alloc_inequality(bmap);
1084                 c = bmap->ineq[k];
1085         }
1086         if (k < 0)
1087                 goto error;
1088
1089         for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1090                 int pos;
1091                 tok = isl_stream_next_token(s);
1092                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1093                         isl_stream_error(s, tok, "expecting coefficient");
1094                         if (tok)
1095                                 isl_stream_push_token(s, tok);
1096                         goto error;
1097                 }
1098                 if (tok->on_new_line) {
1099                         isl_stream_error(s, tok,
1100                                 "coefficient should not appear on new line");
1101                         isl_stream_push_token(s, tok);
1102                         goto error;
1103                 }
1104                 pos = polylib_pos_to_isl_pos(bmap, j);
1105                 isl_int_set(c[pos], tok->u.v);
1106                 isl_token_free(tok);
1107         }
1108
1109         return bmap;
1110 error:
1111         isl_basic_map_free(bmap);
1112         return NULL;
1113 }
1114
1115 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1116         int nparam)
1117 {
1118         int i;
1119         struct isl_token *tok;
1120         struct isl_token *tok2;
1121         int n_row, n_col;
1122         int on_new_line;
1123         unsigned in = 0, out, local = 0;
1124         struct isl_basic_map *bmap = NULL;
1125
1126         if (nparam < 0)
1127                 nparam = 0;
1128
1129         tok = isl_stream_next_token(s);
1130         if (!tok) {
1131                 isl_stream_error(s, NULL, "unexpected EOF");
1132                 return NULL;
1133         }
1134         tok2 = isl_stream_next_token(s);
1135         if (!tok2) {
1136                 isl_token_free(tok);
1137                 isl_stream_error(s, NULL, "unexpected EOF");
1138                 return NULL;
1139         }
1140         n_row = isl_int_get_si(tok->u.v);
1141         n_col = isl_int_get_si(tok2->u.v);
1142         on_new_line = tok2->on_new_line;
1143         isl_token_free(tok2);
1144         isl_token_free(tok);
1145         isl_assert(s->ctx, !on_new_line, return NULL);
1146         isl_assert(s->ctx, n_row >= 0, return NULL);
1147         isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1148         tok = isl_stream_next_token_on_same_line(s);
1149         if (tok) {
1150                 if (tok->type != ISL_TOKEN_VALUE) {
1151                         isl_stream_error(s, tok,
1152                                     "expecting number of output dimensions");
1153                         isl_stream_push_token(s, tok);
1154                         goto error;
1155                 }
1156                 out = isl_int_get_si(tok->u.v);
1157                 isl_token_free(tok);
1158
1159                 tok = isl_stream_next_token_on_same_line(s);
1160                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1161                         isl_stream_error(s, tok,
1162                                     "expecting number of input dimensions");
1163                         if (tok)
1164                                 isl_stream_push_token(s, tok);
1165                         goto error;
1166                 }
1167                 in = isl_int_get_si(tok->u.v);
1168                 isl_token_free(tok);
1169
1170                 tok = isl_stream_next_token_on_same_line(s);
1171                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1172                         isl_stream_error(s, tok,
1173                                     "expecting number of existentials");
1174                         if (tok)
1175                                 isl_stream_push_token(s, tok);
1176                         goto error;
1177                 }
1178                 local = isl_int_get_si(tok->u.v);
1179                 isl_token_free(tok);
1180
1181                 tok = isl_stream_next_token_on_same_line(s);
1182                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1183                         isl_stream_error(s, tok,
1184                                     "expecting number of parameters");
1185                         if (tok)
1186                                 isl_stream_push_token(s, tok);
1187                         goto error;
1188                 }
1189                 nparam = isl_int_get_si(tok->u.v);
1190                 isl_token_free(tok);
1191                 if (n_col != 1 + out + in + local + nparam + 1) {
1192                         isl_stream_error(s, NULL,
1193                                     "dimensions don't match");
1194                         goto error;
1195                 }
1196         } else
1197                 out = n_col - 2 - nparam;
1198         bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1199         if (!bmap)
1200                 return NULL;
1201
1202         for (i = 0; i < local; ++i) {
1203                 int k = isl_basic_map_alloc_div(bmap);
1204                 if (k < 0)
1205                         goto error;
1206         }
1207
1208         for (i = 0; i < n_row; ++i)
1209                 bmap = basic_map_read_polylib_constraint(s, bmap);
1210
1211         tok = isl_stream_next_token_on_same_line(s);
1212         if (tok) {
1213                 isl_stream_error(s, tok, "unexpected extra token on line");
1214                 isl_stream_push_token(s, tok);
1215                 goto error;
1216         }
1217
1218         bmap = isl_basic_map_simplify(bmap);
1219         bmap = isl_basic_map_finalize(bmap);
1220         return bmap;
1221 error:
1222         isl_basic_map_free(bmap);
1223         return NULL;
1224 }
1225
1226 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1227 {
1228         struct isl_token *tok;
1229         struct isl_token *tok2;
1230         int i, n;
1231         struct isl_map *map;
1232
1233         tok = isl_stream_next_token(s);
1234         if (!tok) {
1235                 isl_stream_error(s, NULL, "unexpected EOF");
1236                 return NULL;
1237         }
1238         tok2 = isl_stream_next_token_on_same_line(s);
1239         if (tok2) {
1240                 isl_stream_push_token(s, tok2);
1241                 isl_stream_push_token(s, tok);
1242                 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1243         }
1244         n = isl_int_get_si(tok->u.v);
1245         isl_token_free(tok);
1246
1247         isl_assert(s->ctx, n >= 1, return NULL);
1248
1249         map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1250
1251         for (i = 1; i < n; ++i)
1252                 map = isl_map_union(map,
1253                         isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1254
1255         return map;
1256 }
1257
1258 static int optional_power(struct isl_stream *s)
1259 {
1260         int pow;
1261         struct isl_token *tok;
1262
1263         tok = isl_stream_next_token(s);
1264         if (!tok)
1265                 return 1;
1266         if (tok->type != '^') {
1267                 isl_stream_push_token(s, tok);
1268                 return 1;
1269         }
1270         isl_token_free(tok);
1271         tok = isl_stream_next_token(s);
1272         if (!tok || tok->type != ISL_TOKEN_VALUE) {
1273                 isl_stream_error(s, tok, "expecting exponent");
1274                 if (tok)
1275                         isl_stream_push_token(s, tok);
1276                 return 1;
1277         }
1278         pow = isl_int_get_si(tok->u.v);
1279         isl_token_free(tok);
1280         return pow;
1281 }
1282
1283 static __isl_give isl_div *read_div(struct isl_stream *s,
1284         __isl_keep isl_basic_map *bmap, struct vars *v)
1285 {
1286         unsigned total = isl_basic_map_total_dim(bmap);
1287         int k;
1288
1289         bmap = isl_basic_map_copy(bmap);
1290         bmap = isl_basic_map_cow(bmap);
1291         bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1292                                         1, 0, 2);
1293
1294         if ((k = isl_basic_map_alloc_div(bmap)) < 0)
1295                 goto error;
1296         isl_seq_clr(bmap->div[k], 1 + 1 + total);
1297
1298         if (vars_add_anon(v) < 0)
1299                 goto error;
1300         bmap = add_div_definition(s, v, bmap, k);
1301         vars_drop(v, 1);
1302
1303         return isl_basic_map_div(bmap, k);
1304 error:
1305         isl_basic_map_free(bmap);
1306         return NULL;
1307 }
1308
1309 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1310         __isl_keep isl_basic_map *bmap, struct vars *v);
1311
1312 static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
1313         __isl_keep isl_basic_map *bmap, struct vars *v)
1314 {
1315         struct isl_qpolynomial *qp;
1316         struct isl_token *tok;
1317
1318         tok = isl_stream_next_token(s);
1319         if (!tok) {
1320                 isl_stream_error(s, NULL, "unexpected EOF");
1321                 return NULL;
1322         }
1323         if (tok->type == '(') {
1324                 isl_token_free(tok);
1325                 qp = read_term(s, bmap, v);
1326                 if (!qp)
1327                         return NULL;
1328                 if (isl_stream_eat(s, ')'))
1329                         goto error;
1330         } else if (tok->type == ISL_TOKEN_VALUE) {
1331                 struct isl_token *tok2;
1332                 tok2 = isl_stream_next_token(s);
1333                 if (tok2 && tok2->type == '/') {
1334                         isl_token_free(tok2);
1335                         tok2 = isl_stream_next_token(s);
1336                         if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1337                                 isl_stream_error(s, tok2, "expected denominator");
1338                                 isl_token_free(tok);
1339                                 isl_token_free(tok2);
1340                                 return NULL;
1341                         }
1342                         qp = isl_qpolynomial_rat_cst(isl_basic_map_get_dim(bmap),
1343                                                     tok->u.v, tok2->u.v);
1344                         isl_token_free(tok2);
1345                 } else {
1346                         isl_stream_push_token(s, tok2);
1347                         qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1348                                                 tok->u.v);
1349                 }
1350                 isl_token_free(tok);
1351         } else if (tok->type == ISL_TOKEN_INFTY) {
1352                 isl_token_free(tok);
1353                 qp = isl_qpolynomial_infty(isl_basic_map_get_dim(bmap));
1354         } else if (tok->type == ISL_TOKEN_NAN) {
1355                 isl_token_free(tok);
1356                 qp = isl_qpolynomial_nan(isl_basic_map_get_dim(bmap));
1357         } else if (tok->type == ISL_TOKEN_IDENT) {
1358                 int n = v->n;
1359                 int pos = vars_pos(v, tok->u.s, -1);
1360                 int pow;
1361                 if (pos < 0) {
1362                         isl_token_free(tok);
1363                         return NULL;
1364                 }
1365                 if (pos >= n) {
1366                         isl_stream_error(s, tok, "unknown identifier");
1367                         isl_token_free(tok);
1368                         return NULL;
1369                 }
1370                 isl_token_free(tok);
1371                 pow = optional_power(s);
1372                 qp = isl_qpolynomial_pow(isl_basic_map_get_dim(bmap), pos, pow);
1373         } else if (tok->type == '[') {
1374                 isl_div *div;
1375                 int pow;
1376
1377                 isl_stream_push_token(s, tok);
1378                 div = read_div(s, bmap, v);
1379                 pow = optional_power(s);
1380                 qp = isl_qpolynomial_div_pow(div, pow);
1381         } else if (tok->type == '-') {
1382                 struct isl_qpolynomial *qp2;
1383
1384                 isl_token_free(tok);
1385                 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1386                                             s->ctx->negone);
1387                 qp2 = read_factor(s, bmap, v);
1388                 qp = isl_qpolynomial_mul(qp, qp2);
1389         } else {
1390                 isl_stream_error(s, tok, "unexpected isl_token");
1391                 isl_stream_push_token(s, tok);
1392                 return NULL;
1393         }
1394
1395         if (isl_stream_eat_if_available(s, '*') ||
1396             isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1397                 struct isl_qpolynomial *qp2;
1398
1399                 qp2 = read_factor(s, bmap, v);
1400                 qp = isl_qpolynomial_mul(qp, qp2);
1401         }
1402
1403         return qp;
1404 error:
1405         isl_qpolynomial_free(qp);
1406         return NULL;
1407 }
1408
1409 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1410         __isl_keep isl_basic_map *bmap, struct vars *v)
1411 {
1412         struct isl_token *tok;
1413         struct isl_qpolynomial *qp;
1414
1415         qp = read_factor(s, bmap, v);
1416
1417         for (;;) {
1418                 tok = isl_stream_next_token(s);
1419                 if (!tok)
1420                         return qp;
1421
1422                 if (tok->type == '+') {
1423                         struct isl_qpolynomial *qp2;
1424
1425                         isl_token_free(tok);
1426                         qp2 = read_factor(s, bmap, v);
1427                         qp = isl_qpolynomial_add(qp, qp2);
1428                 } else if (tok->type == '-') {
1429                         struct isl_qpolynomial *qp2;
1430
1431                         isl_token_free(tok);
1432                         qp2 = read_factor(s, bmap, v);
1433                         qp = isl_qpolynomial_sub(qp, qp2);
1434                 } else if (tok->type == ISL_TOKEN_VALUE &&
1435                             isl_int_is_neg(tok->u.v)) {
1436                         struct isl_qpolynomial *qp2;
1437
1438                         isl_stream_push_token(s, tok);
1439                         qp2 = read_factor(s, bmap, v);
1440                         qp = isl_qpolynomial_add(qp, qp2);
1441                 } else {
1442                         isl_stream_push_token(s, tok);
1443                         break;
1444                 }
1445         }
1446
1447         return qp;
1448 }
1449
1450 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1451         __isl_take isl_basic_map *bmap, struct vars *v)
1452 {
1453         struct isl_token *tok;
1454         struct isl_map *map;
1455
1456         tok = isl_stream_next_token(s);
1457         if (!tok) {
1458                 isl_stream_error(s, NULL, "unexpected EOF");
1459                 goto error;
1460         }
1461         map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
1462         if (tok->type == ':') {
1463                 isl_token_free(tok);
1464                 map = isl_map_intersect(map,
1465                             read_disjuncts(s, v, isl_basic_map_get_dim(bmap)));
1466         } else
1467                 isl_stream_push_token(s, tok);
1468
1469         isl_basic_map_free(bmap);
1470
1471         return map;
1472 error:
1473         isl_basic_map_free(bmap);
1474         return NULL;
1475 }
1476
1477 static struct isl_obj obj_read_poly(struct isl_stream *s,
1478         __isl_take isl_basic_map *bmap, struct vars *v, int n)
1479 {
1480         struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1481         struct isl_pw_qpolynomial *pwqp;
1482         struct isl_qpolynomial *qp;
1483         struct isl_map *map;
1484         struct isl_set *set;
1485
1486         qp = read_term(s, bmap, v);
1487         map = read_optional_disjuncts(s, bmap, v);
1488         set = isl_map_range(map);
1489
1490         pwqp = isl_pw_qpolynomial_alloc(set, qp);
1491
1492         vars_drop(v, v->n - n);
1493
1494         obj.v = pwqp;
1495         return obj;
1496 }
1497
1498 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1499         __isl_take isl_basic_map *bmap, struct vars *v, int n)
1500 {
1501         struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1502         struct isl_obj obj_p;
1503         isl_qpolynomial *qp;
1504         isl_qpolynomial_fold *fold = NULL;
1505         isl_pw_qpolynomial_fold *pwf;
1506         isl_map *map;
1507         isl_set *set;
1508
1509         if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1510                 return obj_read_poly(s, bmap, v, n);
1511
1512         if (isl_stream_eat(s, '('))
1513                 goto error;
1514
1515         qp = read_term(s, bmap, v);
1516         fold = isl_qpolynomial_fold_alloc(isl_fold_max, qp);
1517
1518         while (isl_stream_eat_if_available(s, ',')) {
1519                 isl_qpolynomial_fold *fold_i;
1520                 qp = read_term(s, bmap, v);
1521                 fold_i = isl_qpolynomial_fold_alloc(isl_fold_max, qp);
1522                 fold = isl_qpolynomial_fold_fold(fold, fold_i);
1523         }
1524
1525         if (isl_stream_eat(s, ')'))
1526                 goto error;
1527
1528         map = read_optional_disjuncts(s, bmap, v);
1529         set = isl_map_range(map);
1530         pwf = isl_pw_qpolynomial_fold_alloc(isl_fold_max, set, fold);
1531
1532         vars_drop(v, v->n - n);
1533
1534         obj.v = pwf;
1535         return obj;
1536 error:
1537         isl_basic_map_free(bmap);
1538         isl_qpolynomial_fold_free(fold);
1539         obj.type = isl_obj_none;
1540         return obj;
1541 }
1542
1543 static struct isl_obj obj_read_body(struct isl_stream *s,
1544         __isl_take isl_basic_map *bmap, struct vars *v)
1545 {
1546         struct isl_map *map = NULL;
1547         struct isl_token *tok;
1548         struct isl_obj obj = { isl_obj_set, NULL };
1549         int n = v->n;
1550
1551         if (!next_is_tuple(s))
1552                 return obj_read_poly_or_fold(s, bmap, v, n);
1553
1554         bmap = read_tuple(s, bmap, isl_dim_in, v);
1555         if (!bmap)
1556                 goto error;
1557         tok = isl_stream_next_token(s);
1558         if (tok && tok->type == ISL_TOKEN_TO) {
1559                 obj.type = isl_obj_map;
1560                 isl_token_free(tok);
1561                 if (!next_is_tuple(s)) {
1562                         bmap = isl_basic_map_reverse(bmap);
1563                         return obj_read_poly_or_fold(s, bmap, v, n);
1564                 }
1565                 bmap = read_tuple(s, bmap, isl_dim_out, v);
1566                 if (!bmap)
1567                         goto error;
1568         } else {
1569                 bmap = isl_basic_map_reverse(bmap);
1570                 if (tok)
1571                         isl_stream_push_token(s, tok);
1572         }
1573
1574         map = read_optional_disjuncts(s, bmap, v);
1575
1576         vars_drop(v, v->n - n);
1577
1578         obj.v = map;
1579         return obj;
1580 error:
1581         isl_basic_map_free(bmap);
1582         obj.type = isl_obj_none;
1583         return obj;
1584 }
1585
1586 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1587 {
1588         if (obj.type == isl_obj_map) {
1589                 obj.v = isl_union_map_from_map(obj.v);
1590                 obj.type = isl_obj_union_map;
1591         } else if (obj.type == isl_obj_set) {
1592                 obj.v = isl_union_set_from_set(obj.v);
1593                 obj.type = isl_obj_union_set;
1594         } else if (obj.type == isl_obj_pw_qpolynomial) {
1595                 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1596                 obj.type = isl_obj_union_pw_qpolynomial;
1597         } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1598                 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1599                 obj.type = isl_obj_union_pw_qpolynomial_fold;
1600         } else
1601                 isl_assert(ctx, 0, goto error);
1602         return obj;
1603 error:
1604         obj.type->free(obj.v);
1605         obj.type = isl_obj_none;
1606         return obj;
1607 }
1608
1609 static struct isl_obj obj_add(struct isl_ctx *ctx,
1610         struct isl_obj obj1, struct isl_obj obj2)
1611 {
1612         if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1613                 obj1 = to_union(ctx, obj1);
1614         if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1615                 obj2 = to_union(ctx, obj2);
1616         if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1617                 obj1 = to_union(ctx, obj1);
1618         if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1619                 obj2 = to_union(ctx, obj2);
1620         if (obj1.type == isl_obj_pw_qpolynomial &&
1621             obj2.type == isl_obj_union_pw_qpolynomial)
1622                 obj1 = to_union(ctx, obj1);
1623         if (obj1.type == isl_obj_union_pw_qpolynomial &&
1624             obj2.type == isl_obj_pw_qpolynomial)
1625                 obj2 = to_union(ctx, obj2);
1626         if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1627             obj2.type == isl_obj_union_pw_qpolynomial_fold)
1628                 obj1 = to_union(ctx, obj1);
1629         if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1630             obj2.type == isl_obj_pw_qpolynomial_fold)
1631                 obj2 = to_union(ctx, obj2);
1632         isl_assert(ctx, obj1.type == obj2.type, goto error);
1633         if (obj1.type == isl_obj_map && !isl_map_has_equal_dim(obj1.v, obj2.v)) {
1634                 obj1 = to_union(ctx, obj1);
1635                 obj2 = to_union(ctx, obj2);
1636         }
1637         if (obj1.type == isl_obj_set && !isl_set_has_equal_dim(obj1.v, obj2.v)) {
1638                 obj1 = to_union(ctx, obj1);
1639                 obj2 = to_union(ctx, obj2);
1640         }
1641         if (obj1.type == isl_obj_pw_qpolynomial &&
1642             !isl_pw_qpolynomial_has_equal_dim(obj1.v, obj2.v)) {
1643                 obj1 = to_union(ctx, obj1);
1644                 obj2 = to_union(ctx, obj2);
1645         }
1646         if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1647             !isl_pw_qpolynomial_fold_has_equal_dim(obj1.v, obj2.v)) {
1648                 obj1 = to_union(ctx, obj1);
1649                 obj2 = to_union(ctx, obj2);
1650         }
1651         obj1.v = obj1.type->add(obj1.v, obj2.v);
1652         return obj1;
1653 error:
1654         obj1.type->free(obj1.v);
1655         obj2.type->free(obj2.v);
1656         obj1.type = isl_obj_none;
1657         obj1.v = NULL;
1658         return obj1;
1659 }
1660
1661 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1662 {
1663         isl_basic_map *bmap = NULL;
1664         struct isl_token *tok;
1665         struct vars *v = NULL;
1666         struct isl_obj obj = { isl_obj_set, NULL };
1667
1668         tok = isl_stream_next_token(s);
1669         if (!tok) {
1670                 isl_stream_error(s, NULL, "unexpected EOF");
1671                 goto error;
1672         }
1673         if (tok->type == ISL_TOKEN_VALUE) {
1674                 struct isl_map *map;
1675                 isl_stream_push_token(s, tok);
1676                 map = map_read_polylib(s, nparam);
1677                 if (!map)
1678                         goto error;
1679                 if (isl_map_dim(map, isl_dim_in) > 0)
1680                         obj.type = isl_obj_map;
1681                 obj.v = map;
1682                 return obj;
1683         }
1684         v = vars_new(s->ctx);
1685         if (!v) {
1686                 isl_stream_push_token(s, tok);
1687                 goto error;
1688         }
1689         bmap = isl_basic_map_alloc(s->ctx, 0, 0, 0, 0, 0, 0);
1690         if (tok->type == '[') {
1691                 isl_stream_push_token(s, tok);
1692                 bmap = read_tuple(s, bmap, isl_dim_param, v);
1693                 if (!bmap)
1694                         goto error;
1695                 if (nparam >= 0)
1696                         isl_assert(s->ctx, nparam == v->n, goto error);
1697                 tok = isl_stream_next_token(s);
1698                 if (!tok || tok->type != ISL_TOKEN_TO) {
1699                         isl_stream_error(s, tok, "expecting '->'");
1700                         if (tok)
1701                                 isl_stream_push_token(s, tok);
1702                         goto error;
1703                 }
1704                 isl_token_free(tok);
1705                 tok = isl_stream_next_token(s);
1706         } else if (nparam > 0)
1707                 bmap = isl_basic_map_add(bmap, isl_dim_param, nparam);
1708         if (!tok || tok->type != '{') {
1709                 isl_stream_error(s, tok, "expecting '{'");
1710                 if (tok)
1711                         isl_stream_push_token(s, tok);
1712                 goto error;
1713         }
1714         isl_token_free(tok);
1715
1716         tok = isl_stream_next_token(s);
1717         if (!tok)
1718                 ;
1719         else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1720                 isl_token_free(tok);
1721                 if (isl_stream_eat(s, '='))
1722                         goto error;
1723                 bmap = read_tuple(s, bmap, isl_dim_param, v);
1724                 if (!bmap)
1725                         goto error;
1726                 if (nparam >= 0)
1727                         isl_assert(s->ctx, nparam == v->n, goto error);
1728         } else if (tok->type == '}') {
1729                 obj.type = isl_obj_union_set;
1730                 obj.v = isl_union_set_empty(isl_basic_map_get_dim(bmap));
1731                 isl_token_free(tok);
1732                 goto done;
1733         } else
1734                 isl_stream_push_token(s, tok);
1735
1736         for (;;) {
1737                 struct isl_obj o;
1738                 tok = NULL;
1739                 o = obj_read_body(s, isl_basic_map_copy(bmap), v);
1740                 if (o.type == isl_obj_none || !o.v)
1741                         goto error;
1742                 if (!obj.v)
1743                         obj = o;
1744                 else {
1745                         obj = obj_add(s->ctx, obj, o);
1746                         if (obj.type == isl_obj_none || !obj.v)
1747                                 goto error;
1748                 }
1749                 tok = isl_stream_next_token(s);
1750                 if (!tok || tok->type != ';')
1751                         break;
1752                 isl_token_free(tok);
1753         }
1754
1755         if (tok && tok->type == '}') {
1756                 isl_token_free(tok);
1757         } else {
1758                 isl_stream_error(s, tok, "unexpected isl_token");
1759                 if (tok)
1760                         isl_token_free(tok);
1761                 goto error;
1762         }
1763 done:
1764         vars_free(v);
1765         isl_basic_map_free(bmap);
1766
1767         return obj;
1768 error:
1769         isl_basic_map_free(bmap);
1770         obj.type->free(obj.v);
1771         if (v)
1772                 vars_free(v);
1773         obj.v = NULL;
1774         return obj;
1775 }
1776
1777 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1778 {
1779         return obj_read(s, -1);
1780 }
1781
1782 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1783 {
1784         struct isl_obj obj;
1785         struct isl_map *map;
1786
1787         obj = obj_read(s, nparam);
1788         if (obj.v)
1789                 isl_assert(s->ctx, obj.type == isl_obj_map ||
1790                                    obj.type == isl_obj_set, goto error);
1791
1792         return obj.v;
1793 error:
1794         obj.type->free(obj.v);
1795         return NULL;
1796 }
1797
1798 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
1799 {
1800         struct isl_obj obj;
1801         struct isl_set *set;
1802
1803         obj = obj_read(s, nparam);
1804         if (obj.v)
1805                 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
1806
1807         return obj.v;
1808 error:
1809         obj.type->free(obj.v);
1810         return NULL;
1811 }
1812
1813 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
1814 {
1815         struct isl_obj obj;
1816         struct isl_map *map;
1817         struct isl_basic_map *bmap;
1818
1819         obj = obj_read(s, nparam);
1820         map = obj.v;
1821         if (!map)
1822                 return NULL;
1823
1824         isl_assert(map->ctx, map->n <= 1, goto error);
1825
1826         if (map->n == 0)
1827                 bmap = isl_basic_map_empty_like_map(map);
1828         else
1829                 bmap = isl_basic_map_copy(map->p[0]);
1830
1831         isl_map_free(map);
1832
1833         return bmap;
1834 error:
1835         isl_map_free(map);
1836         return NULL;
1837 }
1838
1839 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
1840                 FILE *input, int nparam)
1841 {
1842         struct isl_basic_map *bmap;
1843         struct isl_stream *s = isl_stream_new_file(ctx, input);
1844         if (!s)
1845                 return NULL;
1846         bmap = basic_map_read(s, nparam);
1847         isl_stream_free(s);
1848         return bmap;
1849 }
1850
1851 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
1852                 FILE *input, int nparam)
1853 {
1854         struct isl_basic_map *bmap;
1855         bmap = isl_basic_map_read_from_file(ctx, input, nparam);
1856         if (!bmap)
1857                 return NULL;
1858         isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1859         return (struct isl_basic_set *)bmap;
1860 error:
1861         isl_basic_map_free(bmap);
1862         return NULL;
1863 }
1864
1865 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
1866                 const char *str, int nparam)
1867 {
1868         struct isl_basic_map *bmap;
1869         struct isl_stream *s = isl_stream_new_str(ctx, str);
1870         if (!s)
1871                 return NULL;
1872         bmap = basic_map_read(s, nparam);
1873         isl_stream_free(s);
1874         return bmap;
1875 }
1876
1877 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
1878                 const char *str, int nparam)
1879 {
1880         struct isl_basic_map *bmap;
1881         bmap = isl_basic_map_read_from_str(ctx, str, nparam);
1882         if (!bmap)
1883                 return NULL;
1884         isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1885         return (struct isl_basic_set *)bmap;
1886 error:
1887         isl_basic_map_free(bmap);
1888         return NULL;
1889 }
1890
1891 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
1892                 FILE *input, int nparam)
1893 {
1894         struct isl_map *map;
1895         struct isl_stream *s = isl_stream_new_file(ctx, input);
1896         if (!s)
1897                 return NULL;
1898         map = isl_stream_read_map(s, nparam);
1899         isl_stream_free(s);
1900         return map;
1901 }
1902
1903 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
1904                 const char *str, int nparam)
1905 {
1906         struct isl_map *map;
1907         struct isl_stream *s = isl_stream_new_str(ctx, str);
1908         if (!s)
1909                 return NULL;
1910         map = isl_stream_read_map(s, nparam);
1911         isl_stream_free(s);
1912         return map;
1913 }
1914
1915 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
1916                 FILE *input, int nparam)
1917 {
1918         struct isl_map *map;
1919         map = isl_map_read_from_file(ctx, input, nparam);
1920         if (!map)
1921                 return NULL;
1922         isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1923         return (struct isl_set *)map;
1924 error:
1925         isl_map_free(map);
1926         return NULL;
1927 }
1928
1929 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
1930                 const char *str, int nparam)
1931 {
1932         struct isl_map *map;
1933         map = isl_map_read_from_str(ctx, str, nparam);
1934         if (!map)
1935                 return NULL;
1936         isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1937         return (struct isl_set *)map;
1938 error:
1939         isl_map_free(map);
1940         return NULL;
1941 }
1942
1943 static char *next_line(FILE *input, char *line, unsigned len)
1944 {
1945         char *p;
1946
1947         do {
1948                 if (!(p = fgets(line, len, input)))
1949                         return NULL;
1950                 while (isspace(*p) && *p != '\n')
1951                         ++p;
1952         } while (*p == '#' || *p == '\n');
1953
1954         return p;
1955 }
1956
1957 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
1958                 FILE *input)
1959 {
1960         struct isl_vec *vec = NULL;
1961         char line[1024];
1962         char val[1024];
1963         char *p;
1964         unsigned size;
1965         int j;
1966         int n;
1967         int offset;
1968
1969         isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
1970         isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
1971
1972         vec = isl_vec_alloc(ctx, size);
1973
1974         p = next_line(input, line, sizeof(line));
1975         isl_assert(ctx, p, goto error);
1976
1977         for (j = 0; j < size; ++j) {
1978                 n = sscanf(p, "%s%n", val, &offset);
1979                 isl_assert(ctx, n != 0, goto error);
1980                 isl_int_read(vec->el[j], val);
1981                 p += offset;
1982         }
1983
1984         return vec;
1985 error:
1986         isl_vec_free(vec);
1987         return NULL;
1988 }
1989
1990 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
1991                 FILE *input, unsigned input_format)
1992 {
1993         if (input_format == ISL_FORMAT_POLYLIB)
1994                 return isl_vec_read_from_file_polylib(ctx, input);
1995         else
1996                 isl_assert(ctx, 0, return NULL);
1997 }
1998
1999 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2000         struct isl_stream *s)
2001 {
2002         struct isl_obj obj;
2003         struct isl_pw_qpolynomial *pwqp;
2004
2005         obj = obj_read(s, -1);
2006         if (obj.v)
2007                 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2008                            goto error);
2009
2010         return obj.v;
2011 error:
2012         obj.type->free(obj.v);
2013         return NULL;
2014 }
2015
2016 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2017                 const char *str)
2018 {
2019         isl_pw_qpolynomial *pwqp;
2020         struct isl_stream *s = isl_stream_new_str(ctx, str);
2021         if (!s)
2022                 return NULL;
2023         pwqp = isl_stream_read_pw_qpolynomial(s);
2024         isl_stream_free(s);
2025         return pwqp;
2026 }