isl_stream_read_map: read_var_def: read var definition in "clean" basic map
[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         isl_dim *dim;
396         isl_basic_map *def = NULL;
397         struct isl_vec *vec;
398         int k;
399         int n;
400
401         if (vars_add_anon(v) < 0)
402                 goto error;
403         n = v->n;
404
405         vec = accept_affine(s, v);
406         if (!vec)
407                 goto error;
408
409         dim = isl_basic_map_get_dim(bmap);
410         def = isl_basic_map_universe(dim);
411         def = add_divs(def, v);
412         def = isl_basic_map_extend_constraints(def, 1, 0);
413         k = isl_basic_map_alloc_equality(def);
414         if (k >= 0) {
415                 isl_seq_cpy(def->eq[k], vec->el, vec->size);
416                 isl_int_set_si(def->eq[k][1 + n - 1], -1);
417         }
418         isl_vec_free(vec);
419         if (k < 0)
420                 goto error;
421
422         vars_drop(v, v->n - n);
423
424         def = isl_basic_map_simplify(def);
425         def = isl_basic_map_finalize(def);
426         bmap = isl_basic_map_intersect(bmap, def);
427         return bmap;
428 error:
429         isl_basic_map_free(bmap);
430         isl_basic_map_free(def);
431         return NULL;
432 }
433
434 static __isl_give isl_basic_map *read_var_list(struct isl_stream *s,
435         __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
436 {
437         int i = 0;
438         struct isl_token *tok;
439
440         while ((tok = isl_stream_next_token(s)) != NULL) {
441                 int new_name = 0;
442
443                 if (tok->type == ISL_TOKEN_IDENT) {
444                         int n = v->n;
445                         int p = vars_pos(v, tok->u.s, -1);
446                         if (p < 0)
447                                 goto error;
448                         new_name = p >= n;
449                 }
450
451                 if (new_name) {
452                         bmap = isl_basic_map_add(bmap, type, 1);
453                         bmap = set_name(bmap, type, i, v->v->name);
454                         isl_token_free(tok);
455                 } else if (tok->type == ISL_TOKEN_IDENT ||
456                            tok->type == ISL_TOKEN_VALUE ||
457                            tok->type == '-' ||
458                            tok->type == '(') {
459                         if (type == isl_dim_param) {
460                                 isl_stream_error(s, tok,
461                                                 "expecting unique identifier");
462                                 goto error;
463                         }
464                         isl_stream_push_token(s, tok);
465                         tok = NULL;
466                         bmap = isl_basic_map_add(bmap, type, 1);
467                         bmap = read_var_def(s, bmap, type, v);
468                 } else
469                         break;
470
471                 tok = isl_stream_next_token(s);
472                 if (!tok || tok->type != ',')
473                         break;
474
475                 isl_token_free(tok);
476                 i++;
477         }
478         if (tok)
479                 isl_stream_push_token(s, tok);
480
481         return bmap;
482 error:
483         isl_token_free(tok);
484         isl_basic_map_free(bmap);
485         return NULL;
486 }
487
488 static __isl_give isl_mat *accept_affine_list(struct isl_stream *s,
489         struct vars *v)
490 {
491         struct isl_vec *vec;
492         struct isl_mat *mat;
493         struct isl_token *tok = NULL;
494
495         vec = accept_affine(s, v);
496         mat = isl_mat_from_row_vec(vec);
497         if (!mat)
498                 return NULL;
499
500         for (;;) {
501                 tok = isl_stream_next_token(s);
502                 if (!tok) {
503                         isl_stream_error(s, NULL, "unexpected EOF");
504                         goto error;
505                 }
506                 if (tok->type != ',') {
507                         isl_stream_push_token(s, tok);
508                         break;
509                 }
510                 isl_token_free(tok);
511
512                 vec = accept_affine(s, v);
513                 mat = isl_mat_add_zero_cols(mat, 1 + v->n - isl_mat_cols(mat));
514                 mat = isl_mat_vec_concat(mat, vec);
515                 if (!mat)
516                         return NULL;
517         }
518
519         return mat;
520 error:
521         isl_mat_free(mat);
522         return NULL;
523 }
524
525 static int read_div_definition(struct isl_stream *s, struct vars *v)
526 {
527         struct isl_token *tok;
528         int seen_paren = 0;
529         struct isl_vec *aff;
530         struct variable *var;
531
532         if (isl_stream_eat(s, '['))
533                 return -1;
534
535         tok = isl_stream_next_token(s);
536         if (!tok)
537                 return -1;
538         if (tok->type == '(') {
539                 seen_paren = 1;
540                 isl_token_free(tok);
541         } else
542                 isl_stream_push_token(s, tok);
543
544         var = v->v;
545
546         aff = accept_affine(s, v);
547         if (!aff)
548                 return -1;
549
550         var->def = isl_vec_alloc(s->ctx, 2 + v->n);
551         if (!var->def) {
552                 isl_vec_free(aff);
553                 return -1;
554         }
555
556         isl_seq_cpy(var->def->el + 1, aff->el, aff->size);
557
558         isl_vec_free(aff);
559
560         if (seen_paren && isl_stream_eat(s, ')'))
561                 return -1;
562         if (isl_stream_eat(s, '/'))
563                 return -1;
564
565         tok = isl_stream_next_token(s);
566         if (!tok)
567                 return -1;
568         if (tok->type != ISL_TOKEN_VALUE) {
569                 isl_stream_error(s, tok, "expected denominator");
570                 isl_stream_push_token(s, tok);
571                 return -1;
572         }
573         isl_int_set(var->def->el[0], tok->u.v);
574         isl_token_free(tok);
575
576         if (isl_stream_eat(s, ']'))
577                 return -1;
578
579         return 0;
580 }
581
582 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
583         struct vars *v, struct isl_basic_map *bmap, int k)
584 {
585         struct variable *var = v->v;
586
587         if (read_div_definition(s, v) < 0)
588                 goto error;
589
590         isl_seq_cpy(bmap->div[k], var->def->el, 2 + v->n);
591
592         if (isl_basic_map_add_div_constraints(bmap, k) < 0)
593                 goto error;
594
595         return bmap;
596 error:
597         isl_basic_map_free(bmap);
598         return NULL;
599 }
600
601 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
602         struct vars *v, struct isl_basic_map *bmap)
603 {
604         struct isl_token *tok;
605
606         while ((tok = isl_stream_next_token(s)) != NULL) {
607                 int k;
608                 int p;
609                 int n = v->n;
610                 unsigned total = isl_basic_map_total_dim(bmap);
611
612                 if (tok->type != ISL_TOKEN_IDENT)
613                         break;
614
615                 p = vars_pos(v, tok->u.s, -1);
616                 if (p < 0)
617                         goto error;
618                 if (p < n) {
619                         isl_stream_error(s, tok, "expecting unique identifier");
620                         goto error;
621                 }
622
623                 bmap = isl_basic_map_cow(bmap);
624                 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
625                                                 1, 0, 2);
626
627                 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
628                         goto error;
629                 isl_seq_clr(bmap->div[k], 1 + 1 + total);
630
631                 isl_token_free(tok);
632                 tok = isl_stream_next_token(s);
633                 if (tok && tok->type == '=') {
634                         isl_token_free(tok);
635                         bmap = add_div_definition(s, v, bmap, k);
636                         tok = isl_stream_next_token(s);
637                 }
638
639                 if (!tok || tok->type != ',')
640                         break;
641
642                 isl_token_free(tok);
643         }
644         if (tok)
645                 isl_stream_push_token(s, tok);
646
647         return bmap;
648 error:
649         isl_token_free(tok);
650         isl_basic_map_free(bmap);
651         return NULL;
652 }
653
654 static int next_is_tuple(struct isl_stream *s)
655 {
656         struct isl_token *tok;
657         int is_tuple;
658
659         tok = isl_stream_next_token(s);
660         if (!tok)
661                 return 0;
662         if (tok->type == '[') {
663                 isl_stream_push_token(s, tok);
664                 return 1;
665         }
666         if (tok->type != ISL_TOKEN_IDENT) {
667                 isl_stream_push_token(s, tok);
668                 return 0;
669         }
670
671         is_tuple = isl_stream_next_token_is(s, '[');
672
673         isl_stream_push_token(s, tok);
674
675         return is_tuple;
676 }
677
678 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
679         __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v);
680
681 static __isl_give isl_basic_map *read_nested_tuple(struct isl_stream *s,
682         __isl_take isl_basic_map *bmap, struct vars *v)
683 {
684         bmap = read_tuple(s, bmap, isl_dim_in, v);
685         if (isl_stream_eat(s, ISL_TOKEN_TO))
686                 goto error;
687         bmap = read_tuple(s, bmap, isl_dim_out, v);
688         bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
689         return bmap;
690 error:
691         isl_basic_map_free(bmap);
692         return NULL;
693 }
694
695 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
696         __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
697 {
698         struct isl_token *tok;
699         char *name = NULL;
700
701         tok = isl_stream_next_token(s);
702         if (tok && tok->type == ISL_TOKEN_IDENT) {
703                 name = strdup(tok->u.s);
704                 if (!name)
705                         goto error;
706                 isl_token_free(tok);
707                 tok = isl_stream_next_token(s);
708         }
709         if (!tok || tok->type != '[') {
710                 isl_stream_error(s, tok, "expecting '['");
711                 goto error;
712         }
713         isl_token_free(tok);
714         if (type != isl_dim_param && next_is_tuple(s)) {
715                 isl_dim *dim = isl_basic_map_get_dim(bmap);
716                 int nparam = isl_dim_size(dim, isl_dim_param);
717                 int n_in = isl_dim_size(dim, isl_dim_in);
718                 isl_basic_map *nested;
719                 if (type == isl_dim_out)
720                         dim = isl_dim_move(dim, isl_dim_param, nparam,
721                                                 isl_dim_in, 0, n_in);
722                 nested = isl_basic_map_alloc_dim(dim, 0, 0, 0);
723                 nested = read_nested_tuple(s, nested, v);
724                 if (type == isl_dim_in) {
725                         nested = isl_basic_map_reverse(nested);
726                         bmap = isl_basic_map_intersect(nested, bmap);
727                 } else {
728                         isl_basic_set *bset;
729                         dim = isl_dim_range(isl_basic_map_get_dim(nested));
730                         dim = isl_dim_drop(dim, isl_dim_param, nparam, n_in);
731                         dim = isl_dim_join(isl_basic_map_get_dim(bmap), dim);
732                         bset = isl_basic_map_domain(bmap);
733                         nested = isl_basic_map_reset_dim(nested, dim);
734                         bmap = isl_basic_map_intersect_domain(nested, bset);
735                 }
736         } else
737                 bmap = read_var_list(s, bmap, type, v);
738         tok = isl_stream_next_token(s);
739         if (!tok || tok->type != ']') {
740                 isl_stream_error(s, tok, "expecting ']'");
741                 goto error;
742         }
743         isl_token_free(tok);
744
745         if (name) {
746                 bmap = isl_basic_map_set_tuple_name(bmap, type, name);
747                 free(name);
748         }
749
750         return bmap;
751 error:
752         if (tok)
753                 isl_token_free(tok);
754         isl_basic_map_free(bmap);
755         return NULL;
756 }
757
758 static struct isl_basic_map *add_constraints(struct isl_stream *s,
759         struct vars *v, struct isl_basic_map *bmap);
760
761 static struct isl_basic_map *add_exists(struct isl_stream *s,
762         struct vars *v, struct isl_basic_map *bmap)
763 {
764         struct isl_token *tok;
765         int n = v->n;
766         int extra;
767         int seen_paren = 0;
768         int i;
769         unsigned total;
770
771         tok = isl_stream_next_token(s);
772         if (!tok)
773                 goto error;
774         if (tok->type == '(') {
775                 seen_paren = 1;
776                 isl_token_free(tok);
777         } else
778                 isl_stream_push_token(s, tok);
779
780         bmap = read_defined_var_list(s, v, bmap);
781         if (!bmap)
782                 goto error;
783
784         if (isl_stream_eat(s, ':'))
785                 goto error;
786         bmap = add_constraints(s, v, bmap);
787         if (seen_paren && isl_stream_eat(s, ')'))
788                 goto error;
789         return bmap;
790 error:
791         isl_basic_map_free(bmap);
792         return NULL;
793 }
794
795 static __isl_give isl_basic_map *construct_constraint(
796         __isl_take isl_basic_map *bmap, enum isl_token_type type,
797         isl_int *left, isl_int *right)
798 {
799         int k;
800         unsigned len;
801         struct isl_ctx *ctx;
802
803         if (!bmap)
804                 return NULL;
805         len = 1 + isl_basic_map_total_dim(bmap);
806         ctx = bmap->ctx;
807
808         k = isl_basic_map_alloc_inequality(bmap);
809         if (k < 0)
810                 goto error;
811         if (type == ISL_TOKEN_LE)
812                 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
813                                                ctx->one, right,
814                                                len);
815         else if (type == ISL_TOKEN_GE)
816                 isl_seq_combine(bmap->ineq[k], ctx->one, left,
817                                                ctx->negone, right,
818                                                len);
819         else if (type == ISL_TOKEN_LT) {
820                 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
821                                                ctx->one, right,
822                                                len);
823                 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
824         } else if (type == ISL_TOKEN_GT) {
825                 isl_seq_combine(bmap->ineq[k], ctx->one, left,
826                                                ctx->negone, right,
827                                                len);
828                 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
829         } else {
830                 isl_seq_combine(bmap->ineq[k], ctx->one, left,
831                                                ctx->negone, right,
832                                                len);
833                 isl_basic_map_inequality_to_equality(bmap, k);
834         }
835
836         return bmap;
837 error:
838         isl_basic_map_free(bmap);
839         return NULL;
840 }
841
842 static int is_comparator(struct isl_token *tok)
843 {
844         if (!tok)
845                 return 0;
846
847         switch (tok->type) {
848         case ISL_TOKEN_LT:
849         case ISL_TOKEN_GT:
850         case ISL_TOKEN_LE:
851         case ISL_TOKEN_GE:
852         case '=':
853                 return 1;
854         default:
855                 return 0;
856         }
857 }
858
859 static struct isl_basic_map *add_constraint(struct isl_stream *s,
860         struct vars *v, struct isl_basic_map *bmap)
861 {
862         int i, j;
863         struct isl_token *tok = NULL;
864         struct isl_mat *aff1 = NULL, *aff2 = NULL;
865
866         tok = isl_stream_next_token(s);
867         if (!tok)
868                 goto error;
869         if (tok->type == ISL_TOKEN_EXISTS) {
870                 isl_token_free(tok);
871                 return add_exists(s, v, bmap);
872         }
873         isl_stream_push_token(s, tok);
874         tok = NULL;
875
876         bmap = isl_basic_map_cow(bmap);
877
878         aff1 = accept_affine_list(s, v);
879         if (!aff1)
880                 goto error;
881         tok = isl_stream_next_token(s);
882         if (!is_comparator(tok)) {
883                 isl_stream_error(s, tok, "missing operator");
884                 if (tok)
885                         isl_stream_push_token(s, tok);
886                 tok = NULL;
887                 goto error;
888         }
889         for (;;) {
890                 aff2 = accept_affine_list(s, v);
891                 if (!aff2)
892                         goto error;
893
894                 aff1 = isl_mat_add_zero_cols(aff1, aff2->n_col - aff1->n_col);
895                 if (!aff1)
896                         goto error;
897                 bmap = add_divs(bmap, v);
898                 bmap = isl_basic_map_extend_constraints(bmap, 0,
899                                                 aff1->n_row * aff2->n_row);
900                 for (i = 0; i < aff1->n_row; ++i)
901                         for (j = 0; j < aff2->n_row; ++j)
902                                 bmap = construct_constraint(bmap, tok->type,
903                                                     aff1->row[i], aff2->row[j]);
904                 isl_token_free(tok);
905                 isl_mat_free(aff1);
906                 aff1 = aff2;
907
908                 tok = isl_stream_next_token(s);
909                 if (!is_comparator(tok)) {
910                         if (tok)
911                                 isl_stream_push_token(s, tok);
912                         break;
913                 }
914         }
915         isl_mat_free(aff1);
916
917         return bmap;
918 error:
919         if (tok)
920                 isl_token_free(tok);
921         isl_mat_free(aff1);
922         isl_mat_free(aff2);
923         isl_basic_map_free(bmap);
924         return NULL;
925 }
926
927 static struct isl_basic_map *add_constraints(struct isl_stream *s,
928         struct vars *v, struct isl_basic_map *bmap)
929 {
930         struct isl_token *tok;
931
932         for (;;) {
933                 bmap = add_constraint(s, v, bmap);
934                 if (!bmap)
935                         return NULL;
936                 tok = isl_stream_next_token(s);
937                 if (!tok) {
938                         isl_stream_error(s, NULL, "unexpected EOF");
939                         goto error;
940                 }
941                 if (tok->type != ISL_TOKEN_AND)
942                         break;
943                 isl_token_free(tok);
944         }
945         isl_stream_push_token(s, tok);
946
947         return bmap;
948 error:
949         if (tok)
950                 isl_token_free(tok);
951         isl_basic_map_free(bmap);
952         return NULL;
953 }
954
955 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
956         struct vars *v, __isl_take isl_dim *dim)
957 {
958         int seen_paren = 0;
959         struct isl_token *tok;
960         struct isl_basic_map *bmap;
961
962         bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
963         if (!bmap)
964                 return NULL;
965
966         tok = isl_stream_next_token(s);
967         if (!tok)
968                 goto error;
969         if (tok->type == '(') {
970                 seen_paren = 1;
971                 isl_token_free(tok);
972         } else
973                 isl_stream_push_token(s, tok);
974
975         bmap = add_constraints(s, v, bmap);
976         bmap = isl_basic_map_simplify(bmap);
977         bmap = isl_basic_map_finalize(bmap);
978
979         if (seen_paren && isl_stream_eat(s, ')'))
980                 goto error;
981
982         return bmap;
983 error:
984         isl_basic_map_free(bmap);
985         return NULL;
986 }
987
988 static struct isl_map *read_disjuncts(struct isl_stream *s,
989         struct vars *v, __isl_take isl_dim *dim)
990 {
991         struct isl_token *tok;
992         struct isl_map *map;
993
994         tok = isl_stream_next_token(s);
995         if (!tok) {
996                 isl_stream_error(s, NULL, "unexpected EOF");
997                 goto error;
998         }
999         if (tok->type == '}') {
1000                 isl_stream_push_token(s, tok);
1001                 return isl_map_universe(dim);
1002         }
1003         isl_stream_push_token(s, tok);
1004
1005         map = isl_map_empty(isl_dim_copy(dim));
1006         for (;;) {
1007                 struct isl_basic_map *bmap;
1008                 int n = v->n;
1009
1010                 bmap = read_disjunct(s, v, isl_dim_copy(dim));
1011                 map = isl_map_union(map, isl_map_from_basic_map(bmap));
1012
1013                 vars_drop(v, v->n - n);
1014
1015                 tok = isl_stream_next_token(s);
1016                 if (!tok || tok->type != ISL_TOKEN_OR)
1017                         break;
1018                 isl_token_free(tok);
1019         }
1020         if (tok)
1021                 isl_stream_push_token(s, tok);
1022
1023         isl_dim_free(dim);
1024         return map;
1025 error:
1026         isl_dim_free(dim);
1027         return NULL;
1028 }
1029
1030 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1031 {
1032         if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1033                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1034                            isl_basic_map_dim(bmap, isl_dim_in) + pos;
1035         pos -= isl_basic_map_dim(bmap, isl_dim_out);
1036
1037         if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1038                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1039         pos -= isl_basic_map_dim(bmap, isl_dim_in);
1040
1041         if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1042                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1043                            isl_basic_map_dim(bmap, isl_dim_in) +
1044                            isl_basic_map_dim(bmap, isl_dim_out) + pos;
1045         pos -= isl_basic_map_dim(bmap, isl_dim_div);
1046
1047         if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1048                 return 1 + pos;
1049
1050         return 0;
1051 }
1052
1053 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1054         struct isl_stream *s, __isl_take isl_basic_map *bmap)
1055 {
1056         int j;
1057         struct isl_token *tok;
1058         int type;
1059         int k;
1060         isl_int *c;
1061         unsigned nparam;
1062         unsigned dim;
1063
1064         if (!bmap)
1065                 return NULL;
1066
1067         nparam = isl_basic_map_dim(bmap, isl_dim_param);
1068         dim = isl_basic_map_dim(bmap, isl_dim_out);
1069
1070         tok = isl_stream_next_token(s);
1071         if (!tok || tok->type != ISL_TOKEN_VALUE) {
1072                 isl_stream_error(s, tok, "expecting coefficient");
1073                 if (tok)
1074                         isl_stream_push_token(s, tok);
1075                 goto error;
1076         }
1077         if (!tok->on_new_line) {
1078                 isl_stream_error(s, tok, "coefficient should appear on new line");
1079                 isl_stream_push_token(s, tok);
1080                 goto error;
1081         }
1082
1083         type = isl_int_get_si(tok->u.v);
1084         isl_token_free(tok);
1085
1086         isl_assert(s->ctx, type == 0 || type == 1, goto error);
1087         if (type == 0) {
1088                 k = isl_basic_map_alloc_equality(bmap);
1089                 c = bmap->eq[k];
1090         } else {
1091                 k = isl_basic_map_alloc_inequality(bmap);
1092                 c = bmap->ineq[k];
1093         }
1094         if (k < 0)
1095                 goto error;
1096
1097         for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1098                 int pos;
1099                 tok = isl_stream_next_token(s);
1100                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1101                         isl_stream_error(s, tok, "expecting coefficient");
1102                         if (tok)
1103                                 isl_stream_push_token(s, tok);
1104                         goto error;
1105                 }
1106                 if (tok->on_new_line) {
1107                         isl_stream_error(s, tok,
1108                                 "coefficient should not appear on new line");
1109                         isl_stream_push_token(s, tok);
1110                         goto error;
1111                 }
1112                 pos = polylib_pos_to_isl_pos(bmap, j);
1113                 isl_int_set(c[pos], tok->u.v);
1114                 isl_token_free(tok);
1115         }
1116
1117         return bmap;
1118 error:
1119         isl_basic_map_free(bmap);
1120         return NULL;
1121 }
1122
1123 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1124         int nparam)
1125 {
1126         int i;
1127         struct isl_token *tok;
1128         struct isl_token *tok2;
1129         int n_row, n_col;
1130         int on_new_line;
1131         unsigned in = 0, out, local = 0;
1132         struct isl_basic_map *bmap = NULL;
1133
1134         if (nparam < 0)
1135                 nparam = 0;
1136
1137         tok = isl_stream_next_token(s);
1138         if (!tok) {
1139                 isl_stream_error(s, NULL, "unexpected EOF");
1140                 return NULL;
1141         }
1142         tok2 = isl_stream_next_token(s);
1143         if (!tok2) {
1144                 isl_token_free(tok);
1145                 isl_stream_error(s, NULL, "unexpected EOF");
1146                 return NULL;
1147         }
1148         n_row = isl_int_get_si(tok->u.v);
1149         n_col = isl_int_get_si(tok2->u.v);
1150         on_new_line = tok2->on_new_line;
1151         isl_token_free(tok2);
1152         isl_token_free(tok);
1153         isl_assert(s->ctx, !on_new_line, return NULL);
1154         isl_assert(s->ctx, n_row >= 0, return NULL);
1155         isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1156         tok = isl_stream_next_token_on_same_line(s);
1157         if (tok) {
1158                 if (tok->type != ISL_TOKEN_VALUE) {
1159                         isl_stream_error(s, tok,
1160                                     "expecting number of output dimensions");
1161                         isl_stream_push_token(s, tok);
1162                         goto error;
1163                 }
1164                 out = isl_int_get_si(tok->u.v);
1165                 isl_token_free(tok);
1166
1167                 tok = isl_stream_next_token_on_same_line(s);
1168                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1169                         isl_stream_error(s, tok,
1170                                     "expecting number of input dimensions");
1171                         if (tok)
1172                                 isl_stream_push_token(s, tok);
1173                         goto error;
1174                 }
1175                 in = isl_int_get_si(tok->u.v);
1176                 isl_token_free(tok);
1177
1178                 tok = isl_stream_next_token_on_same_line(s);
1179                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1180                         isl_stream_error(s, tok,
1181                                     "expecting number of existentials");
1182                         if (tok)
1183                                 isl_stream_push_token(s, tok);
1184                         goto error;
1185                 }
1186                 local = isl_int_get_si(tok->u.v);
1187                 isl_token_free(tok);
1188
1189                 tok = isl_stream_next_token_on_same_line(s);
1190                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1191                         isl_stream_error(s, tok,
1192                                     "expecting number of parameters");
1193                         if (tok)
1194                                 isl_stream_push_token(s, tok);
1195                         goto error;
1196                 }
1197                 nparam = isl_int_get_si(tok->u.v);
1198                 isl_token_free(tok);
1199                 if (n_col != 1 + out + in + local + nparam + 1) {
1200                         isl_stream_error(s, NULL,
1201                                     "dimensions don't match");
1202                         goto error;
1203                 }
1204         } else
1205                 out = n_col - 2 - nparam;
1206         bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1207         if (!bmap)
1208                 return NULL;
1209
1210         for (i = 0; i < local; ++i) {
1211                 int k = isl_basic_map_alloc_div(bmap);
1212                 if (k < 0)
1213                         goto error;
1214                 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1215         }
1216
1217         for (i = 0; i < n_row; ++i)
1218                 bmap = basic_map_read_polylib_constraint(s, bmap);
1219
1220         tok = isl_stream_next_token_on_same_line(s);
1221         if (tok) {
1222                 isl_stream_error(s, tok, "unexpected extra token on line");
1223                 isl_stream_push_token(s, tok);
1224                 goto error;
1225         }
1226
1227         bmap = isl_basic_map_simplify(bmap);
1228         bmap = isl_basic_map_finalize(bmap);
1229         return bmap;
1230 error:
1231         isl_basic_map_free(bmap);
1232         return NULL;
1233 }
1234
1235 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1236 {
1237         struct isl_token *tok;
1238         struct isl_token *tok2;
1239         int i, n;
1240         struct isl_map *map;
1241
1242         tok = isl_stream_next_token(s);
1243         if (!tok) {
1244                 isl_stream_error(s, NULL, "unexpected EOF");
1245                 return NULL;
1246         }
1247         tok2 = isl_stream_next_token_on_same_line(s);
1248         if (tok2) {
1249                 isl_stream_push_token(s, tok2);
1250                 isl_stream_push_token(s, tok);
1251                 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1252         }
1253         n = isl_int_get_si(tok->u.v);
1254         isl_token_free(tok);
1255
1256         isl_assert(s->ctx, n >= 1, return NULL);
1257
1258         map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1259
1260         for (i = 1; i < n; ++i)
1261                 map = isl_map_union(map,
1262                         isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1263
1264         return map;
1265 }
1266
1267 static int optional_power(struct isl_stream *s)
1268 {
1269         int pow;
1270         struct isl_token *tok;
1271
1272         tok = isl_stream_next_token(s);
1273         if (!tok)
1274                 return 1;
1275         if (tok->type != '^') {
1276                 isl_stream_push_token(s, tok);
1277                 return 1;
1278         }
1279         isl_token_free(tok);
1280         tok = isl_stream_next_token(s);
1281         if (!tok || tok->type != ISL_TOKEN_VALUE) {
1282                 isl_stream_error(s, tok, "expecting exponent");
1283                 if (tok)
1284                         isl_stream_push_token(s, tok);
1285                 return 1;
1286         }
1287         pow = isl_int_get_si(tok->u.v);
1288         isl_token_free(tok);
1289         return pow;
1290 }
1291
1292 static __isl_give isl_div *read_div(struct isl_stream *s,
1293         __isl_keep isl_basic_map *bmap, struct vars *v)
1294 {
1295         unsigned total = isl_basic_map_total_dim(bmap);
1296         int k;
1297
1298         bmap = isl_basic_map_copy(bmap);
1299         bmap = isl_basic_map_cow(bmap);
1300         bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1301                                         1, 0, 2);
1302
1303         if ((k = isl_basic_map_alloc_div(bmap)) < 0)
1304                 goto error;
1305         isl_seq_clr(bmap->div[k], 1 + 1 + total);
1306
1307         if (vars_add_anon(v) < 0)
1308                 goto error;
1309         bmap = add_div_definition(s, v, bmap, k);
1310         vars_drop(v, 1);
1311
1312         return isl_basic_map_div(bmap, k);
1313 error:
1314         isl_basic_map_free(bmap);
1315         return NULL;
1316 }
1317
1318 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1319         __isl_keep isl_basic_map *bmap, struct vars *v);
1320
1321 static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
1322         __isl_keep isl_basic_map *bmap, struct vars *v)
1323 {
1324         struct isl_qpolynomial *qp;
1325         struct isl_token *tok;
1326
1327         tok = isl_stream_next_token(s);
1328         if (!tok) {
1329                 isl_stream_error(s, NULL, "unexpected EOF");
1330                 return NULL;
1331         }
1332         if (tok->type == '(') {
1333                 isl_token_free(tok);
1334                 qp = read_term(s, bmap, v);
1335                 if (!qp)
1336                         return NULL;
1337                 if (isl_stream_eat(s, ')'))
1338                         goto error;
1339         } else if (tok->type == ISL_TOKEN_VALUE) {
1340                 struct isl_token *tok2;
1341                 tok2 = isl_stream_next_token(s);
1342                 if (tok2 && tok2->type == '/') {
1343                         isl_token_free(tok2);
1344                         tok2 = isl_stream_next_token(s);
1345                         if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1346                                 isl_stream_error(s, tok2, "expected denominator");
1347                                 isl_token_free(tok);
1348                                 isl_token_free(tok2);
1349                                 return NULL;
1350                         }
1351                         qp = isl_qpolynomial_rat_cst(isl_basic_map_get_dim(bmap),
1352                                                     tok->u.v, tok2->u.v);
1353                         isl_token_free(tok2);
1354                 } else {
1355                         isl_stream_push_token(s, tok2);
1356                         qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1357                                                 tok->u.v);
1358                 }
1359                 isl_token_free(tok);
1360         } else if (tok->type == ISL_TOKEN_INFTY) {
1361                 isl_token_free(tok);
1362                 qp = isl_qpolynomial_infty(isl_basic_map_get_dim(bmap));
1363         } else if (tok->type == ISL_TOKEN_NAN) {
1364                 isl_token_free(tok);
1365                 qp = isl_qpolynomial_nan(isl_basic_map_get_dim(bmap));
1366         } else if (tok->type == ISL_TOKEN_IDENT) {
1367                 int n = v->n;
1368                 int pos = vars_pos(v, tok->u.s, -1);
1369                 int pow;
1370                 if (pos < 0) {
1371                         isl_token_free(tok);
1372                         return NULL;
1373                 }
1374                 if (pos >= n) {
1375                         isl_stream_error(s, tok, "unknown identifier");
1376                         isl_token_free(tok);
1377                         return NULL;
1378                 }
1379                 isl_token_free(tok);
1380                 pow = optional_power(s);
1381                 qp = isl_qpolynomial_pow(isl_basic_map_get_dim(bmap), pos, pow);
1382         } else if (tok->type == '[') {
1383                 isl_div *div;
1384                 int pow;
1385
1386                 isl_stream_push_token(s, tok);
1387                 div = read_div(s, bmap, v);
1388                 pow = optional_power(s);
1389                 qp = isl_qpolynomial_div_pow(div, pow);
1390         } else if (tok->type == '-') {
1391                 struct isl_qpolynomial *qp2;
1392
1393                 isl_token_free(tok);
1394                 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1395                                             s->ctx->negone);
1396                 qp2 = read_factor(s, bmap, v);
1397                 qp = isl_qpolynomial_mul(qp, qp2);
1398         } else {
1399                 isl_stream_error(s, tok, "unexpected isl_token");
1400                 isl_stream_push_token(s, tok);
1401                 return NULL;
1402         }
1403
1404         if (isl_stream_eat_if_available(s, '*') ||
1405             isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1406                 struct isl_qpolynomial *qp2;
1407
1408                 qp2 = read_factor(s, bmap, v);
1409                 qp = isl_qpolynomial_mul(qp, qp2);
1410         }
1411
1412         return qp;
1413 error:
1414         isl_qpolynomial_free(qp);
1415         return NULL;
1416 }
1417
1418 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1419         __isl_keep isl_basic_map *bmap, struct vars *v)
1420 {
1421         struct isl_token *tok;
1422         struct isl_qpolynomial *qp;
1423
1424         qp = read_factor(s, bmap, v);
1425
1426         for (;;) {
1427                 tok = isl_stream_next_token(s);
1428                 if (!tok)
1429                         return qp;
1430
1431                 if (tok->type == '+') {
1432                         struct isl_qpolynomial *qp2;
1433
1434                         isl_token_free(tok);
1435                         qp2 = read_factor(s, bmap, v);
1436                         qp = isl_qpolynomial_add(qp, qp2);
1437                 } else if (tok->type == '-') {
1438                         struct isl_qpolynomial *qp2;
1439
1440                         isl_token_free(tok);
1441                         qp2 = read_factor(s, bmap, v);
1442                         qp = isl_qpolynomial_sub(qp, qp2);
1443                 } else if (tok->type == ISL_TOKEN_VALUE &&
1444                             isl_int_is_neg(tok->u.v)) {
1445                         struct isl_qpolynomial *qp2;
1446
1447                         isl_stream_push_token(s, tok);
1448                         qp2 = read_factor(s, bmap, v);
1449                         qp = isl_qpolynomial_add(qp, qp2);
1450                 } else {
1451                         isl_stream_push_token(s, tok);
1452                         break;
1453                 }
1454         }
1455
1456         return qp;
1457 }
1458
1459 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1460         __isl_take isl_basic_map *bmap, struct vars *v)
1461 {
1462         struct isl_token *tok;
1463         struct isl_map *map;
1464
1465         tok = isl_stream_next_token(s);
1466         if (!tok) {
1467                 isl_stream_error(s, NULL, "unexpected EOF");
1468                 goto error;
1469         }
1470         map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
1471         if (tok->type == ':') {
1472                 isl_token_free(tok);
1473                 map = isl_map_intersect(map,
1474                             read_disjuncts(s, v, isl_basic_map_get_dim(bmap)));
1475         } else
1476                 isl_stream_push_token(s, tok);
1477
1478         isl_basic_map_free(bmap);
1479
1480         return map;
1481 error:
1482         isl_basic_map_free(bmap);
1483         return NULL;
1484 }
1485
1486 static struct isl_obj obj_read_poly(struct isl_stream *s,
1487         __isl_take isl_basic_map *bmap, struct vars *v, int n)
1488 {
1489         struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1490         struct isl_pw_qpolynomial *pwqp;
1491         struct isl_qpolynomial *qp;
1492         struct isl_map *map;
1493         struct isl_set *set;
1494
1495         qp = read_term(s, bmap, v);
1496         map = read_optional_disjuncts(s, bmap, v);
1497         set = isl_map_range(map);
1498
1499         pwqp = isl_pw_qpolynomial_alloc(set, qp);
1500
1501         vars_drop(v, v->n - n);
1502
1503         obj.v = pwqp;
1504         return obj;
1505 }
1506
1507 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1508         __isl_take isl_basic_map *bmap, struct vars *v, int n)
1509 {
1510         struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1511         struct isl_obj obj_p;
1512         isl_qpolynomial *qp;
1513         isl_qpolynomial_fold *fold = NULL;
1514         isl_pw_qpolynomial_fold *pwf;
1515         isl_map *map;
1516         isl_set *set;
1517
1518         if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1519                 return obj_read_poly(s, bmap, v, n);
1520
1521         if (isl_stream_eat(s, '('))
1522                 goto error;
1523
1524         qp = read_term(s, bmap, v);
1525         fold = isl_qpolynomial_fold_alloc(isl_fold_max, qp);
1526
1527         while (isl_stream_eat_if_available(s, ',')) {
1528                 isl_qpolynomial_fold *fold_i;
1529                 qp = read_term(s, bmap, v);
1530                 fold_i = isl_qpolynomial_fold_alloc(isl_fold_max, qp);
1531                 fold = isl_qpolynomial_fold_fold(fold, fold_i);
1532         }
1533
1534         if (isl_stream_eat(s, ')'))
1535                 goto error;
1536
1537         map = read_optional_disjuncts(s, bmap, v);
1538         set = isl_map_range(map);
1539         pwf = isl_pw_qpolynomial_fold_alloc(isl_fold_max, set, fold);
1540
1541         vars_drop(v, v->n - n);
1542
1543         obj.v = pwf;
1544         return obj;
1545 error:
1546         isl_basic_map_free(bmap);
1547         isl_qpolynomial_fold_free(fold);
1548         obj.type = isl_obj_none;
1549         return obj;
1550 }
1551
1552 static struct isl_obj obj_read_body(struct isl_stream *s,
1553         __isl_take isl_basic_map *bmap, struct vars *v)
1554 {
1555         struct isl_map *map = NULL;
1556         struct isl_token *tok;
1557         struct isl_obj obj = { isl_obj_set, NULL };
1558         int n = v->n;
1559
1560         if (!next_is_tuple(s))
1561                 return obj_read_poly_or_fold(s, bmap, v, n);
1562
1563         bmap = read_tuple(s, bmap, isl_dim_in, v);
1564         if (!bmap)
1565                 goto error;
1566         tok = isl_stream_next_token(s);
1567         if (tok && tok->type == ISL_TOKEN_TO) {
1568                 obj.type = isl_obj_map;
1569                 isl_token_free(tok);
1570                 if (!next_is_tuple(s)) {
1571                         bmap = isl_basic_map_reverse(bmap);
1572                         return obj_read_poly_or_fold(s, bmap, v, n);
1573                 }
1574                 bmap = read_tuple(s, bmap, isl_dim_out, v);
1575                 if (!bmap)
1576                         goto error;
1577         } else {
1578                 bmap = isl_basic_map_reverse(bmap);
1579                 if (tok)
1580                         isl_stream_push_token(s, tok);
1581         }
1582
1583         map = read_optional_disjuncts(s, bmap, v);
1584
1585         vars_drop(v, v->n - n);
1586
1587         obj.v = map;
1588         return obj;
1589 error:
1590         isl_basic_map_free(bmap);
1591         obj.type = isl_obj_none;
1592         return obj;
1593 }
1594
1595 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1596 {
1597         if (obj.type == isl_obj_map) {
1598                 obj.v = isl_union_map_from_map(obj.v);
1599                 obj.type = isl_obj_union_map;
1600         } else if (obj.type == isl_obj_set) {
1601                 obj.v = isl_union_set_from_set(obj.v);
1602                 obj.type = isl_obj_union_set;
1603         } else if (obj.type == isl_obj_pw_qpolynomial) {
1604                 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1605                 obj.type = isl_obj_union_pw_qpolynomial;
1606         } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1607                 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1608                 obj.type = isl_obj_union_pw_qpolynomial_fold;
1609         } else
1610                 isl_assert(ctx, 0, goto error);
1611         return obj;
1612 error:
1613         obj.type->free(obj.v);
1614         obj.type = isl_obj_none;
1615         return obj;
1616 }
1617
1618 static struct isl_obj obj_add(struct isl_ctx *ctx,
1619         struct isl_obj obj1, struct isl_obj obj2)
1620 {
1621         if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1622                 obj1 = to_union(ctx, obj1);
1623         if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1624                 obj2 = to_union(ctx, obj2);
1625         if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1626                 obj1 = to_union(ctx, obj1);
1627         if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1628                 obj2 = to_union(ctx, obj2);
1629         if (obj1.type == isl_obj_pw_qpolynomial &&
1630             obj2.type == isl_obj_union_pw_qpolynomial)
1631                 obj1 = to_union(ctx, obj1);
1632         if (obj1.type == isl_obj_union_pw_qpolynomial &&
1633             obj2.type == isl_obj_pw_qpolynomial)
1634                 obj2 = to_union(ctx, obj2);
1635         if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1636             obj2.type == isl_obj_union_pw_qpolynomial_fold)
1637                 obj1 = to_union(ctx, obj1);
1638         if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1639             obj2.type == isl_obj_pw_qpolynomial_fold)
1640                 obj2 = to_union(ctx, obj2);
1641         isl_assert(ctx, obj1.type == obj2.type, goto error);
1642         if (obj1.type == isl_obj_map && !isl_map_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_set && !isl_set_has_equal_dim(obj1.v, obj2.v)) {
1647                 obj1 = to_union(ctx, obj1);
1648                 obj2 = to_union(ctx, obj2);
1649         }
1650         if (obj1.type == isl_obj_pw_qpolynomial &&
1651             !isl_pw_qpolynomial_has_equal_dim(obj1.v, obj2.v)) {
1652                 obj1 = to_union(ctx, obj1);
1653                 obj2 = to_union(ctx, obj2);
1654         }
1655         if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1656             !isl_pw_qpolynomial_fold_has_equal_dim(obj1.v, obj2.v)) {
1657                 obj1 = to_union(ctx, obj1);
1658                 obj2 = to_union(ctx, obj2);
1659         }
1660         obj1.v = obj1.type->add(obj1.v, obj2.v);
1661         return obj1;
1662 error:
1663         obj1.type->free(obj1.v);
1664         obj2.type->free(obj2.v);
1665         obj1.type = isl_obj_none;
1666         obj1.v = NULL;
1667         return obj1;
1668 }
1669
1670 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1671 {
1672         isl_basic_map *bmap = NULL;
1673         struct isl_token *tok;
1674         struct vars *v = NULL;
1675         struct isl_obj obj = { isl_obj_set, NULL };
1676
1677         tok = isl_stream_next_token(s);
1678         if (!tok) {
1679                 isl_stream_error(s, NULL, "unexpected EOF");
1680                 goto error;
1681         }
1682         if (tok->type == ISL_TOKEN_VALUE) {
1683                 struct isl_map *map;
1684                 isl_stream_push_token(s, tok);
1685                 map = map_read_polylib(s, nparam);
1686                 if (!map)
1687                         goto error;
1688                 if (isl_map_dim(map, isl_dim_in) > 0)
1689                         obj.type = isl_obj_map;
1690                 obj.v = map;
1691                 return obj;
1692         }
1693         v = vars_new(s->ctx);
1694         if (!v) {
1695                 isl_stream_push_token(s, tok);
1696                 goto error;
1697         }
1698         bmap = isl_basic_map_alloc(s->ctx, 0, 0, 0, 0, 0, 0);
1699         if (tok->type == '[') {
1700                 isl_stream_push_token(s, tok);
1701                 bmap = read_tuple(s, bmap, isl_dim_param, v);
1702                 if (!bmap)
1703                         goto error;
1704                 if (nparam >= 0)
1705                         isl_assert(s->ctx, nparam == v->n, goto error);
1706                 tok = isl_stream_next_token(s);
1707                 if (!tok || tok->type != ISL_TOKEN_TO) {
1708                         isl_stream_error(s, tok, "expecting '->'");
1709                         if (tok)
1710                                 isl_stream_push_token(s, tok);
1711                         goto error;
1712                 }
1713                 isl_token_free(tok);
1714                 tok = isl_stream_next_token(s);
1715         } else if (nparam > 0)
1716                 bmap = isl_basic_map_add(bmap, isl_dim_param, nparam);
1717         if (!tok || tok->type != '{') {
1718                 isl_stream_error(s, tok, "expecting '{'");
1719                 if (tok)
1720                         isl_stream_push_token(s, tok);
1721                 goto error;
1722         }
1723         isl_token_free(tok);
1724
1725         tok = isl_stream_next_token(s);
1726         if (!tok)
1727                 ;
1728         else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1729                 isl_token_free(tok);
1730                 if (isl_stream_eat(s, '='))
1731                         goto error;
1732                 bmap = read_tuple(s, bmap, isl_dim_param, v);
1733                 if (!bmap)
1734                         goto error;
1735                 if (nparam >= 0)
1736                         isl_assert(s->ctx, nparam == v->n, goto error);
1737         } else if (tok->type == '}') {
1738                 obj.type = isl_obj_union_set;
1739                 obj.v = isl_union_set_empty(isl_basic_map_get_dim(bmap));
1740                 isl_token_free(tok);
1741                 goto done;
1742         } else
1743                 isl_stream_push_token(s, tok);
1744
1745         for (;;) {
1746                 struct isl_obj o;
1747                 tok = NULL;
1748                 o = obj_read_body(s, isl_basic_map_copy(bmap), v);
1749                 if (o.type == isl_obj_none || !o.v)
1750                         goto error;
1751                 if (!obj.v)
1752                         obj = o;
1753                 else {
1754                         obj = obj_add(s->ctx, obj, o);
1755                         if (obj.type == isl_obj_none || !obj.v)
1756                                 goto error;
1757                 }
1758                 tok = isl_stream_next_token(s);
1759                 if (!tok || tok->type != ';')
1760                         break;
1761                 isl_token_free(tok);
1762         }
1763
1764         if (tok && tok->type == '}') {
1765                 isl_token_free(tok);
1766         } else {
1767                 isl_stream_error(s, tok, "unexpected isl_token");
1768                 if (tok)
1769                         isl_token_free(tok);
1770                 goto error;
1771         }
1772 done:
1773         vars_free(v);
1774         isl_basic_map_free(bmap);
1775
1776         return obj;
1777 error:
1778         isl_basic_map_free(bmap);
1779         obj.type->free(obj.v);
1780         if (v)
1781                 vars_free(v);
1782         obj.v = NULL;
1783         return obj;
1784 }
1785
1786 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1787 {
1788         return obj_read(s, -1);
1789 }
1790
1791 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1792 {
1793         struct isl_obj obj;
1794         struct isl_map *map;
1795
1796         obj = obj_read(s, nparam);
1797         if (obj.v)
1798                 isl_assert(s->ctx, obj.type == isl_obj_map ||
1799                                    obj.type == isl_obj_set, goto error);
1800
1801         return obj.v;
1802 error:
1803         obj.type->free(obj.v);
1804         return NULL;
1805 }
1806
1807 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
1808 {
1809         struct isl_obj obj;
1810         struct isl_set *set;
1811
1812         obj = obj_read(s, nparam);
1813         if (obj.v)
1814                 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
1815
1816         return obj.v;
1817 error:
1818         obj.type->free(obj.v);
1819         return NULL;
1820 }
1821
1822 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
1823 {
1824         struct isl_obj obj;
1825         struct isl_map *map;
1826         struct isl_basic_map *bmap;
1827
1828         obj = obj_read(s, nparam);
1829         map = obj.v;
1830         if (!map)
1831                 return NULL;
1832
1833         isl_assert(map->ctx, map->n <= 1, goto error);
1834
1835         if (map->n == 0)
1836                 bmap = isl_basic_map_empty_like_map(map);
1837         else
1838                 bmap = isl_basic_map_copy(map->p[0]);
1839
1840         isl_map_free(map);
1841
1842         return bmap;
1843 error:
1844         isl_map_free(map);
1845         return NULL;
1846 }
1847
1848 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
1849                 FILE *input, int nparam)
1850 {
1851         struct isl_basic_map *bmap;
1852         struct isl_stream *s = isl_stream_new_file(ctx, input);
1853         if (!s)
1854                 return NULL;
1855         bmap = basic_map_read(s, nparam);
1856         isl_stream_free(s);
1857         return bmap;
1858 }
1859
1860 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
1861                 FILE *input, int nparam)
1862 {
1863         struct isl_basic_map *bmap;
1864         bmap = isl_basic_map_read_from_file(ctx, input, nparam);
1865         if (!bmap)
1866                 return NULL;
1867         isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1868         return (struct isl_basic_set *)bmap;
1869 error:
1870         isl_basic_map_free(bmap);
1871         return NULL;
1872 }
1873
1874 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
1875                 const char *str, int nparam)
1876 {
1877         struct isl_basic_map *bmap;
1878         struct isl_stream *s = isl_stream_new_str(ctx, str);
1879         if (!s)
1880                 return NULL;
1881         bmap = basic_map_read(s, nparam);
1882         isl_stream_free(s);
1883         return bmap;
1884 }
1885
1886 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
1887                 const char *str, int nparam)
1888 {
1889         struct isl_basic_map *bmap;
1890         bmap = isl_basic_map_read_from_str(ctx, str, nparam);
1891         if (!bmap)
1892                 return NULL;
1893         isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1894         return (struct isl_basic_set *)bmap;
1895 error:
1896         isl_basic_map_free(bmap);
1897         return NULL;
1898 }
1899
1900 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
1901                 FILE *input, int nparam)
1902 {
1903         struct isl_map *map;
1904         struct isl_stream *s = isl_stream_new_file(ctx, input);
1905         if (!s)
1906                 return NULL;
1907         map = isl_stream_read_map(s, nparam);
1908         isl_stream_free(s);
1909         return map;
1910 }
1911
1912 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
1913                 const char *str, int nparam)
1914 {
1915         struct isl_map *map;
1916         struct isl_stream *s = isl_stream_new_str(ctx, str);
1917         if (!s)
1918                 return NULL;
1919         map = isl_stream_read_map(s, nparam);
1920         isl_stream_free(s);
1921         return map;
1922 }
1923
1924 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
1925                 FILE *input, int nparam)
1926 {
1927         struct isl_map *map;
1928         map = isl_map_read_from_file(ctx, input, nparam);
1929         if (!map)
1930                 return NULL;
1931         isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1932         return (struct isl_set *)map;
1933 error:
1934         isl_map_free(map);
1935         return NULL;
1936 }
1937
1938 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
1939                 const char *str, int nparam)
1940 {
1941         struct isl_map *map;
1942         map = isl_map_read_from_str(ctx, str, nparam);
1943         if (!map)
1944                 return NULL;
1945         isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1946         return (struct isl_set *)map;
1947 error:
1948         isl_map_free(map);
1949         return NULL;
1950 }
1951
1952 static char *next_line(FILE *input, char *line, unsigned len)
1953 {
1954         char *p;
1955
1956         do {
1957                 if (!(p = fgets(line, len, input)))
1958                         return NULL;
1959                 while (isspace(*p) && *p != '\n')
1960                         ++p;
1961         } while (*p == '#' || *p == '\n');
1962
1963         return p;
1964 }
1965
1966 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
1967                 FILE *input)
1968 {
1969         struct isl_vec *vec = NULL;
1970         char line[1024];
1971         char val[1024];
1972         char *p;
1973         unsigned size;
1974         int j;
1975         int n;
1976         int offset;
1977
1978         isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
1979         isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
1980
1981         vec = isl_vec_alloc(ctx, size);
1982
1983         p = next_line(input, line, sizeof(line));
1984         isl_assert(ctx, p, goto error);
1985
1986         for (j = 0; j < size; ++j) {
1987                 n = sscanf(p, "%s%n", val, &offset);
1988                 isl_assert(ctx, n != 0, goto error);
1989                 isl_int_read(vec->el[j], val);
1990                 p += offset;
1991         }
1992
1993         return vec;
1994 error:
1995         isl_vec_free(vec);
1996         return NULL;
1997 }
1998
1999 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
2000                 FILE *input, unsigned input_format)
2001 {
2002         if (input_format == ISL_FORMAT_POLYLIB)
2003                 return isl_vec_read_from_file_polylib(ctx, input);
2004         else
2005                 isl_assert(ctx, 0, return NULL);
2006 }
2007
2008 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2009         struct isl_stream *s)
2010 {
2011         struct isl_obj obj;
2012         struct isl_pw_qpolynomial *pwqp;
2013
2014         obj = obj_read(s, -1);
2015         if (obj.v)
2016                 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2017                            goto error);
2018
2019         return obj.v;
2020 error:
2021         obj.type->free(obj.v);
2022         return NULL;
2023 }
2024
2025 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2026                 const char *str)
2027 {
2028         isl_pw_qpolynomial *pwqp;
2029         struct isl_stream *s = isl_stream_new_str(ctx, str);
2030         if (!s)
2031                 return NULL;
2032         pwqp = isl_stream_read_pw_qpolynomial(s);
2033         isl_stream_free(s);
2034         return pwqp;
2035 }