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