clean up isl_pw_qpolynomial_neg
[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                            tok->type == '-') {
369                         if (type == isl_dim_param) {
370                                 isl_stream_error(s, tok,
371                                                 "expecting unique identifier");
372                                 goto error;
373                         }
374                         isl_stream_push_token(s, tok);
375                         tok = NULL;
376                         dim = isl_dim_add(dim, type, 1);
377                         *eq = isl_mat_add_zero_cols(*eq, 1);
378                         *eq = read_var_def(s, *eq, type, v);
379                 } else
380                         break;
381
382                 tok = isl_stream_next_token(s);
383                 if (!tok || tok->type != ',')
384                         break;
385
386                 isl_token_free(tok);
387                 i++;
388         }
389         if (tok)
390                 isl_stream_push_token(s, tok);
391
392         return dim;
393 error:
394         isl_token_free(tok);
395         isl_dim_free(dim);
396         return NULL;
397 }
398
399 static __isl_give isl_mat *accept_affine_list(struct isl_stream *s,
400         struct vars *v)
401 {
402         struct isl_vec *vec;
403         struct isl_mat *mat;
404         struct isl_token *tok = NULL;
405
406         vec = accept_affine(s, v);
407         mat = isl_mat_from_row_vec(vec);
408         if (!mat)
409                 return NULL;
410
411         for (;;) {
412                 tok = isl_stream_next_token(s);
413                 if (!tok) {
414                         isl_stream_error(s, NULL, "unexpected EOF");
415                         goto error;
416                 }
417                 if (tok->type != ',') {
418                         isl_stream_push_token(s, tok);
419                         break;
420                 }
421                 isl_token_free(tok);
422
423                 vec = accept_affine(s, v);
424                 mat = isl_mat_vec_concat(mat, vec);
425                 if (!mat)
426                         return NULL;
427         }
428
429         return mat;
430 error:
431         isl_mat_free(mat);
432         return NULL;
433 }
434
435 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
436         struct vars *v, struct isl_basic_map *bmap, int k)
437 {
438         struct isl_token *tok;
439         int seen_paren = 0;
440         struct isl_vec *aff;
441
442         if (isl_stream_eat(s, '['))
443                 goto error;
444
445         tok = isl_stream_next_token(s);
446         if (!tok)
447                 goto error;
448         if (tok->type == '(') {
449                 seen_paren = 1;
450                 isl_token_free(tok);
451         } else
452                 isl_stream_push_token(s, tok);
453
454         aff = accept_affine(s, v);
455         if (!aff)
456                 goto error;
457
458         isl_seq_cpy(bmap->div[k] + 1, aff->el, aff->size);
459
460         isl_vec_free(aff);
461
462         if (seen_paren && isl_stream_eat(s, ')'))
463                 goto error;
464         if (isl_stream_eat(s, '/'))
465                 goto error;
466
467         tok = isl_stream_next_token(s);
468         if (!tok)
469                 goto error;
470         if (tok->type != ISL_TOKEN_VALUE) {
471                 isl_stream_error(s, tok, "expected denominator");
472                 isl_stream_push_token(s, tok);
473                 goto error;
474         }
475         isl_int_set(bmap->div[k][0], tok->u.v);
476         isl_token_free(tok);
477
478         if (isl_stream_eat(s, ']'))
479                 goto error;
480
481         if (isl_basic_map_add_div_constraints(bmap, k) < 0)
482                 goto error;
483
484         return bmap;
485 error:
486         isl_basic_map_free(bmap);
487         return NULL;
488 }
489
490 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
491         struct vars *v, struct isl_basic_map *bmap)
492 {
493         struct isl_token *tok;
494
495         while ((tok = isl_stream_next_token(s)) != NULL) {
496                 int k;
497                 int p;
498                 int n = v->n;
499                 unsigned total = isl_basic_map_total_dim(bmap);
500
501                 if (tok->type != ISL_TOKEN_IDENT)
502                         break;
503
504                 p = vars_pos(v, tok->u.s, -1);
505                 if (p < 0)
506                         goto error;
507                 if (p < n) {
508                         isl_stream_error(s, tok, "expecting unique identifier");
509                         goto error;
510                 }
511
512                 bmap = isl_basic_map_cow(bmap);
513                 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
514                                                 1, 0, 2);
515
516                 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
517                         goto error;
518                 isl_seq_clr(bmap->div[k], 1 + 1 + total);
519
520                 isl_token_free(tok);
521                 tok = isl_stream_next_token(s);
522                 if (tok && tok->type == '=') {
523                         isl_token_free(tok);
524                         bmap = add_div_definition(s, v, bmap, k);
525                         tok = isl_stream_next_token(s);
526                 }
527
528                 if (!tok || tok->type != ',')
529                         break;
530
531                 isl_token_free(tok);
532         }
533         if (tok)
534                 isl_stream_push_token(s, tok);
535
536         return bmap;
537 error:
538         isl_token_free(tok);
539         isl_basic_map_free(bmap);
540         return NULL;
541 }
542
543 static int next_is_tuple(struct isl_stream *s)
544 {
545         struct isl_token *tok;
546         int is_tuple;
547
548         tok = isl_stream_next_token(s);
549         if (!tok)
550                 return 0;
551         if (tok->type == '[') {
552                 isl_stream_push_token(s, tok);
553                 return 1;
554         }
555         if (tok->type != ISL_TOKEN_IDENT) {
556                 isl_stream_push_token(s, tok);
557                 return 0;
558         }
559
560         is_tuple = isl_stream_next_token_is(s, '[');
561
562         isl_stream_push_token(s, tok);
563
564         return is_tuple;
565 }
566
567 static __isl_give isl_dim *read_tuple(struct isl_stream *s,
568         __isl_take isl_dim *dim, enum isl_dim_type type, struct vars *v,
569         __isl_keep isl_mat **eq);
570
571 static __isl_give isl_dim *read_nested_tuple(struct isl_stream *s,
572         __isl_take isl_dim *dim, struct vars *v, __isl_keep isl_mat **eq)
573 {
574         dim = read_tuple(s, dim, isl_dim_in, v, eq);
575         if (isl_stream_eat(s, ISL_TOKEN_TO))
576                 goto error;
577         dim = read_tuple(s, dim, isl_dim_out, v, eq);
578         dim = isl_dim_wrap(dim);
579         return dim;
580 error:
581         isl_dim_free(dim);
582         return NULL;
583 }
584
585 static __isl_give isl_dim *read_tuple(struct isl_stream *s,
586         __isl_take isl_dim *dim, enum isl_dim_type type, struct vars *v,
587         __isl_keep isl_mat **eq)
588 {
589         struct isl_token *tok;
590         char *name = NULL;
591
592         tok = isl_stream_next_token(s);
593         if (tok && tok->type == ISL_TOKEN_IDENT) {
594                 name = strdup(tok->u.s);
595                 if (!name)
596                         goto error;
597                 isl_token_free(tok);
598                 tok = isl_stream_next_token(s);
599         }
600         if (!tok || tok->type != '[') {
601                 isl_stream_error(s, tok, "expecting '['");
602                 goto error;
603         }
604         isl_token_free(tok);
605         if (type != isl_dim_param && next_is_tuple(s)) {
606                 isl_dim *nested = isl_dim_copy(dim);
607                 nested = isl_dim_drop(nested, isl_dim_in, 0,
608                                         isl_dim_size(nested, isl_dim_in));
609                 nested = isl_dim_drop(nested, isl_dim_out, 0,
610                                         isl_dim_size(nested, isl_dim_out));
611                 nested = read_nested_tuple(s, nested, v, eq);
612                 if (type == isl_dim_in) {
613                         isl_dim_free(dim);
614                         dim = isl_dim_reverse(nested);
615                 } else {
616                         dim = isl_dim_join(dim, nested);
617                 }
618         } else
619                 dim = read_var_list(s, dim, type, v, eq);
620         tok = isl_stream_next_token(s);
621         if (!tok || tok->type != ']') {
622                 isl_stream_error(s, tok, "expecting ']'");
623                 goto error;
624         }
625         isl_token_free(tok);
626
627         if (name) {
628                 dim = isl_dim_set_tuple_name(dim, type, name);
629                 free(name);
630         }
631
632         return dim;
633 error:
634         if (tok)
635                 isl_token_free(tok);
636         isl_dim_free(dim);
637         return NULL;
638 }
639
640 static struct isl_basic_map *add_constraints(struct isl_stream *s,
641         struct vars *v, struct isl_basic_map *bmap);
642
643 static struct isl_basic_map *add_exists(struct isl_stream *s,
644         struct vars *v, struct isl_basic_map *bmap)
645 {
646         struct isl_token *tok;
647         int n = v->n;
648         int extra;
649         int seen_paren = 0;
650         int i;
651         unsigned total;
652
653         tok = isl_stream_next_token(s);
654         if (!tok)
655                 goto error;
656         if (tok->type == '(') {
657                 seen_paren = 1;
658                 isl_token_free(tok);
659         } else
660                 isl_stream_push_token(s, tok);
661
662         bmap = read_defined_var_list(s, v, bmap);
663         if (!bmap)
664                 goto error;
665
666         if (isl_stream_eat(s, ':'))
667                 goto error;
668         bmap = add_constraints(s, v, bmap);
669         if (seen_paren && isl_stream_eat(s, ')'))
670                 goto error;
671         return bmap;
672 error:
673         isl_basic_map_free(bmap);
674         return NULL;
675 }
676
677 static __isl_give isl_basic_map *construct_constraint(
678         __isl_take isl_basic_map *bmap, enum isl_token_type type,
679         isl_int *left, isl_int *right)
680 {
681         int k;
682         unsigned len;
683         struct isl_ctx *ctx;
684
685         if (!bmap)
686                 return NULL;
687         len = 1 + isl_basic_map_total_dim(bmap);
688         ctx = bmap->ctx;
689
690         k = isl_basic_map_alloc_inequality(bmap);
691         if (k < 0)
692                 goto error;
693         if (type == ISL_TOKEN_LE)
694                 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
695                                                ctx->one, right,
696                                                len);
697         else if (type == ISL_TOKEN_GE)
698                 isl_seq_combine(bmap->ineq[k], ctx->one, left,
699                                                ctx->negone, right,
700                                                len);
701         else if (type == ISL_TOKEN_LT) {
702                 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
703                                                ctx->one, right,
704                                                len);
705                 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
706         } else if (type == ISL_TOKEN_GT) {
707                 isl_seq_combine(bmap->ineq[k], ctx->one, left,
708                                                ctx->negone, right,
709                                                len);
710                 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
711         } else {
712                 isl_seq_combine(bmap->ineq[k], ctx->one, left,
713                                                ctx->negone, right,
714                                                len);
715                 isl_basic_map_inequality_to_equality(bmap, k);
716         }
717
718         return bmap;
719 error:
720         isl_basic_map_free(bmap);
721         return NULL;
722 }
723
724 static int is_comparator(struct isl_token *tok)
725 {
726         if (!tok)
727                 return 0;
728
729         switch (tok->type) {
730         case ISL_TOKEN_LT:
731         case ISL_TOKEN_GT:
732         case ISL_TOKEN_LE:
733         case ISL_TOKEN_GE:
734         case '=':
735                 return 1;
736         default:
737                 return 0;
738         }
739 }
740
741 static struct isl_basic_map *add_constraint(struct isl_stream *s,
742         struct vars *v, struct isl_basic_map *bmap)
743 {
744         int i, j;
745         unsigned total = isl_basic_map_total_dim(bmap);
746         struct isl_token *tok = NULL;
747         struct isl_mat *aff1 = NULL, *aff2 = NULL;
748
749         tok = isl_stream_next_token(s);
750         if (!tok)
751                 goto error;
752         if (tok->type == ISL_TOKEN_EXISTS) {
753                 isl_token_free(tok);
754                 return add_exists(s, v, bmap);
755         }
756         isl_stream_push_token(s, tok);
757         tok = NULL;
758
759         bmap = isl_basic_map_cow(bmap);
760
761         aff1 = accept_affine_list(s, v);
762         if (!aff1)
763                 goto error;
764         tok = isl_stream_next_token(s);
765         if (!is_comparator(tok)) {
766                 isl_stream_error(s, tok, "missing operator");
767                 if (tok)
768                         isl_stream_push_token(s, tok);
769                 tok = NULL;
770                 goto error;
771         }
772         isl_assert(aff1->ctx, aff1->n_col == 1 + total, goto error);
773         for (;;) {
774                 aff2 = accept_affine_list(s, v);
775                 if (!aff2)
776                         goto error;
777                 isl_assert(aff2->ctx, aff2->n_col == 1 + total, goto error);
778
779                 bmap = isl_basic_map_extend_constraints(bmap, 0,
780                                                 aff1->n_row * aff2->n_row);
781                 for (i = 0; i < aff1->n_row; ++i)
782                         for (j = 0; j < aff2->n_row; ++j)
783                                 bmap = construct_constraint(bmap, tok->type,
784                                                     aff1->row[i], aff2->row[j]);
785                 isl_token_free(tok);
786                 isl_mat_free(aff1);
787                 aff1 = aff2;
788
789                 tok = isl_stream_next_token(s);
790                 if (!is_comparator(tok)) {
791                         if (tok)
792                                 isl_stream_push_token(s, tok);
793                         break;
794                 }
795         }
796         isl_mat_free(aff1);
797
798         return bmap;
799 error:
800         if (tok)
801                 isl_token_free(tok);
802         isl_mat_free(aff1);
803         isl_mat_free(aff2);
804         isl_basic_map_free(bmap);
805         return NULL;
806 }
807
808 static struct isl_basic_map *add_constraints(struct isl_stream *s,
809         struct vars *v, struct isl_basic_map *bmap)
810 {
811         struct isl_token *tok;
812
813         for (;;) {
814                 bmap = add_constraint(s, v, bmap);
815                 if (!bmap)
816                         return NULL;
817                 tok = isl_stream_next_token(s);
818                 if (!tok) {
819                         isl_stream_error(s, NULL, "unexpected EOF");
820                         goto error;
821                 }
822                 if (tok->type != ISL_TOKEN_AND)
823                         break;
824                 isl_token_free(tok);
825         }
826         isl_stream_push_token(s, tok);
827
828         return bmap;
829 error:
830         if (tok)
831                 isl_token_free(tok);
832         isl_basic_map_free(bmap);
833         return NULL;
834 }
835
836 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
837         struct vars *v, __isl_take isl_dim *dim)
838 {
839         int seen_paren = 0;
840         struct isl_token *tok;
841         struct isl_basic_map *bmap;
842
843         bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
844         if (!bmap)
845                 return NULL;
846
847         tok = isl_stream_next_token(s);
848         if (!tok)
849                 goto error;
850         if (tok->type == '(') {
851                 seen_paren = 1;
852                 isl_token_free(tok);
853         } else
854                 isl_stream_push_token(s, tok);
855
856         bmap = add_constraints(s, v, bmap);
857         bmap = isl_basic_map_simplify(bmap);
858         bmap = isl_basic_map_finalize(bmap);
859
860         if (seen_paren && isl_stream_eat(s, ')'))
861                 goto error;
862
863         return bmap;
864 error:
865         isl_basic_map_free(bmap);
866         return NULL;
867 }
868
869 static struct isl_map *read_disjuncts(struct isl_stream *s,
870         struct vars *v, __isl_take isl_dim *dim)
871 {
872         struct isl_token *tok;
873         struct isl_map *map;
874
875         tok = isl_stream_next_token(s);
876         if (!tok) {
877                 isl_stream_error(s, NULL, "unexpected EOF");
878                 goto error;
879         }
880         if (tok->type == '}') {
881                 isl_stream_push_token(s, tok);
882                 return isl_map_universe(dim);
883         }
884         isl_stream_push_token(s, tok);
885
886         map = isl_map_empty(isl_dim_copy(dim));
887         for (;;) {
888                 struct isl_basic_map *bmap;
889                 int n = v->n;
890
891                 bmap = read_disjunct(s, v, isl_dim_copy(dim));
892                 map = isl_map_union(map, isl_map_from_basic_map(bmap));
893
894                 vars_drop(v, v->n - n);
895
896                 tok = isl_stream_next_token(s);
897                 if (!tok || tok->type != ISL_TOKEN_OR)
898                         break;
899                 isl_token_free(tok);
900         }
901         if (tok)
902                 isl_stream_push_token(s, tok);
903
904         isl_dim_free(dim);
905         return map;
906 error:
907         isl_dim_free(dim);
908         return NULL;
909 }
910
911 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
912 {
913         if (pos < isl_basic_map_dim(bmap, isl_dim_out))
914                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
915                            isl_basic_map_dim(bmap, isl_dim_in) + pos;
916         pos -= isl_basic_map_dim(bmap, isl_dim_out);
917
918         if (pos < isl_basic_map_dim(bmap, isl_dim_in))
919                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
920         pos -= isl_basic_map_dim(bmap, isl_dim_in);
921
922         if (pos < isl_basic_map_dim(bmap, isl_dim_div))
923                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
924                            isl_basic_map_dim(bmap, isl_dim_in) +
925                            isl_basic_map_dim(bmap, isl_dim_out) + pos;
926         pos -= isl_basic_map_dim(bmap, isl_dim_div);
927
928         if (pos < isl_basic_map_dim(bmap, isl_dim_param))
929                 return 1 + pos;
930
931         return 0;
932 }
933
934 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
935         struct isl_stream *s, __isl_take isl_basic_map *bmap)
936 {
937         int j;
938         struct isl_token *tok;
939         int type;
940         int k;
941         isl_int *c;
942         unsigned nparam;
943         unsigned dim;
944
945         if (!bmap)
946                 return NULL;
947
948         nparam = isl_basic_map_dim(bmap, isl_dim_param);
949         dim = isl_basic_map_dim(bmap, isl_dim_out);
950
951         tok = isl_stream_next_token(s);
952         if (!tok || tok->type != ISL_TOKEN_VALUE) {
953                 isl_stream_error(s, tok, "expecting coefficient");
954                 if (tok)
955                         isl_stream_push_token(s, tok);
956                 goto error;
957         }
958         if (!tok->on_new_line) {
959                 isl_stream_error(s, tok, "coefficient should appear on new line");
960                 isl_stream_push_token(s, tok);
961                 goto error;
962         }
963
964         type = isl_int_get_si(tok->u.v);
965         isl_token_free(tok);
966
967         isl_assert(s->ctx, type == 0 || type == 1, goto error);
968         if (type == 0) {
969                 k = isl_basic_map_alloc_equality(bmap);
970                 c = bmap->eq[k];
971         } else {
972                 k = isl_basic_map_alloc_inequality(bmap);
973                 c = bmap->ineq[k];
974         }
975         if (k < 0)
976                 goto error;
977
978         for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
979                 int pos;
980                 tok = isl_stream_next_token(s);
981                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
982                         isl_stream_error(s, tok, "expecting coefficient");
983                         if (tok)
984                                 isl_stream_push_token(s, tok);
985                         goto error;
986                 }
987                 if (tok->on_new_line) {
988                         isl_stream_error(s, tok,
989                                 "coefficient should not appear on new line");
990                         isl_stream_push_token(s, tok);
991                         goto error;
992                 }
993                 pos = polylib_pos_to_isl_pos(bmap, j);
994                 isl_int_set(c[pos], tok->u.v);
995                 isl_token_free(tok);
996         }
997
998         return bmap;
999 error:
1000         isl_basic_map_free(bmap);
1001         return NULL;
1002 }
1003
1004 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1005         int nparam)
1006 {
1007         int i;
1008         struct isl_token *tok;
1009         struct isl_token *tok2;
1010         int n_row, n_col;
1011         int on_new_line;
1012         unsigned in = 0, out, local = 0;
1013         struct isl_basic_map *bmap = NULL;
1014
1015         if (nparam < 0)
1016                 nparam = 0;
1017
1018         tok = isl_stream_next_token(s);
1019         if (!tok) {
1020                 isl_stream_error(s, NULL, "unexpected EOF");
1021                 return NULL;
1022         }
1023         tok2 = isl_stream_next_token(s);
1024         if (!tok2) {
1025                 isl_token_free(tok);
1026                 isl_stream_error(s, NULL, "unexpected EOF");
1027                 return NULL;
1028         }
1029         n_row = isl_int_get_si(tok->u.v);
1030         n_col = isl_int_get_si(tok2->u.v);
1031         on_new_line = tok2->on_new_line;
1032         isl_token_free(tok2);
1033         isl_token_free(tok);
1034         isl_assert(s->ctx, !on_new_line, return NULL);
1035         isl_assert(s->ctx, n_row >= 0, return NULL);
1036         isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1037         tok = isl_stream_next_token_on_same_line(s);
1038         if (tok) {
1039                 if (tok->type != ISL_TOKEN_VALUE) {
1040                         isl_stream_error(s, tok,
1041                                     "expecting number of output dimensions");
1042                         isl_stream_push_token(s, tok);
1043                         goto error;
1044                 }
1045                 out = isl_int_get_si(tok->u.v);
1046                 isl_token_free(tok);
1047
1048                 tok = isl_stream_next_token_on_same_line(s);
1049                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1050                         isl_stream_error(s, tok,
1051                                     "expecting number of input dimensions");
1052                         if (tok)
1053                                 isl_stream_push_token(s, tok);
1054                         goto error;
1055                 }
1056                 in = isl_int_get_si(tok->u.v);
1057                 isl_token_free(tok);
1058
1059                 tok = isl_stream_next_token_on_same_line(s);
1060                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1061                         isl_stream_error(s, tok,
1062                                     "expecting number of existentials");
1063                         if (tok)
1064                                 isl_stream_push_token(s, tok);
1065                         goto error;
1066                 }
1067                 local = isl_int_get_si(tok->u.v);
1068                 isl_token_free(tok);
1069
1070                 tok = isl_stream_next_token_on_same_line(s);
1071                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1072                         isl_stream_error(s, tok,
1073                                     "expecting number of parameters");
1074                         if (tok)
1075                                 isl_stream_push_token(s, tok);
1076                         goto error;
1077                 }
1078                 nparam = isl_int_get_si(tok->u.v);
1079                 isl_token_free(tok);
1080                 if (n_col != 1 + out + in + local + nparam + 1) {
1081                         isl_stream_error(s, NULL,
1082                                     "dimensions don't match");
1083                         goto error;
1084                 }
1085         } else
1086                 out = n_col - 2 - nparam;
1087         bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1088         if (!bmap)
1089                 return NULL;
1090
1091         for (i = 0; i < local; ++i) {
1092                 int k = isl_basic_map_alloc_div(bmap);
1093                 if (k < 0)
1094                         goto error;
1095         }
1096
1097         for (i = 0; i < n_row; ++i)
1098                 bmap = basic_map_read_polylib_constraint(s, bmap);
1099
1100         tok = isl_stream_next_token_on_same_line(s);
1101         if (tok) {
1102                 isl_stream_error(s, tok, "unexpected extra token on line");
1103                 isl_stream_push_token(s, tok);
1104                 goto error;
1105         }
1106
1107         bmap = isl_basic_map_simplify(bmap);
1108         bmap = isl_basic_map_finalize(bmap);
1109         return bmap;
1110 error:
1111         isl_basic_map_free(bmap);
1112         return NULL;
1113 }
1114
1115 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1116 {
1117         struct isl_token *tok;
1118         struct isl_token *tok2;
1119         int i, n;
1120         struct isl_map *map;
1121
1122         tok = isl_stream_next_token(s);
1123         if (!tok) {
1124                 isl_stream_error(s, NULL, "unexpected EOF");
1125                 return NULL;
1126         }
1127         tok2 = isl_stream_next_token_on_same_line(s);
1128         if (tok2) {
1129                 isl_stream_push_token(s, tok2);
1130                 isl_stream_push_token(s, tok);
1131                 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1132         }
1133         n = isl_int_get_si(tok->u.v);
1134         isl_token_free(tok);
1135
1136         isl_assert(s->ctx, n >= 1, return NULL);
1137
1138         map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1139
1140         for (i = 1; i < n; ++i)
1141                 map = isl_map_union(map,
1142                         isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1143
1144         return map;
1145 }
1146
1147 static int optional_power(struct isl_stream *s)
1148 {
1149         int pow;
1150         struct isl_token *tok;
1151
1152         tok = isl_stream_next_token(s);
1153         if (!tok)
1154                 return 1;
1155         if (tok->type != '^') {
1156                 isl_stream_push_token(s, tok);
1157                 return 1;
1158         }
1159         isl_token_free(tok);
1160         tok = isl_stream_next_token(s);
1161         if (!tok || tok->type != ISL_TOKEN_VALUE) {
1162                 isl_stream_error(s, tok, "expecting exponent");
1163                 if (tok)
1164                         isl_stream_push_token(s, tok);
1165                 return 1;
1166         }
1167         pow = isl_int_get_si(tok->u.v);
1168         isl_token_free(tok);
1169         return pow;
1170 }
1171
1172 static __isl_give isl_div *read_div(struct isl_stream *s,
1173         __isl_keep isl_basic_map *bmap, struct vars *v)
1174 {
1175         unsigned total = isl_basic_map_total_dim(bmap);
1176         int k;
1177
1178         bmap = isl_basic_map_copy(bmap);
1179         bmap = isl_basic_map_cow(bmap);
1180         bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1181                                         1, 0, 2);
1182
1183         if ((k = isl_basic_map_alloc_div(bmap)) < 0)
1184                 goto error;
1185         isl_seq_clr(bmap->div[k], 1 + 1 + total);
1186         bmap = add_div_definition(s, v, bmap, k);
1187
1188         return isl_basic_map_div(bmap, k);
1189 error:
1190         isl_basic_map_free(bmap);
1191         return NULL;
1192 }
1193
1194 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1195         __isl_keep isl_basic_map *bmap, struct vars *v);
1196
1197 static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
1198         __isl_keep isl_basic_map *bmap, struct vars *v)
1199 {
1200         struct isl_qpolynomial *qp;
1201         struct isl_token *tok;
1202
1203         tok = isl_stream_next_token(s);
1204         if (!tok) {
1205                 isl_stream_error(s, NULL, "unexpected EOF");
1206                 return NULL;
1207         }
1208         if (tok->type == '(') {
1209                 isl_token_free(tok);
1210                 qp = read_term(s, bmap, v);
1211                 if (!qp)
1212                         return NULL;
1213                 if (isl_stream_eat(s, ')'))
1214                         goto error;
1215         } else if (tok->type == ISL_TOKEN_VALUE) {
1216                 struct isl_token *tok2;
1217                 tok2 = isl_stream_next_token(s);
1218                 if (tok2 && tok2->type == '/') {
1219                         isl_token_free(tok2);
1220                         tok2 = isl_stream_next_token(s);
1221                         if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1222                                 isl_stream_error(s, tok2, "expected denominator");
1223                                 isl_token_free(tok);
1224                                 isl_token_free(tok2);
1225                                 return NULL;
1226                         }
1227                         qp = isl_qpolynomial_rat_cst(isl_basic_map_get_dim(bmap),
1228                                                     tok->u.v, tok2->u.v);
1229                         isl_token_free(tok2);
1230                 } else {
1231                         isl_stream_push_token(s, tok2);
1232                         qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1233                                                 tok->u.v);
1234                 }
1235                 isl_token_free(tok);
1236         } else if (tok->type == ISL_TOKEN_INFTY) {
1237                 isl_token_free(tok);
1238                 qp = isl_qpolynomial_infty(isl_basic_map_get_dim(bmap));
1239         } else if (tok->type == ISL_TOKEN_NAN) {
1240                 isl_token_free(tok);
1241                 qp = isl_qpolynomial_nan(isl_basic_map_get_dim(bmap));
1242         } else if (tok->type == ISL_TOKEN_IDENT) {
1243                 int n = v->n;
1244                 int pos = vars_pos(v, tok->u.s, -1);
1245                 int pow;
1246                 if (pos < 0) {
1247                         isl_token_free(tok);
1248                         return NULL;
1249                 }
1250                 if (pos >= n) {
1251                         isl_stream_error(s, tok, "unknown identifier");
1252                         isl_token_free(tok);
1253                         return NULL;
1254                 }
1255                 isl_token_free(tok);
1256                 pow = optional_power(s);
1257                 qp = isl_qpolynomial_pow(isl_basic_map_get_dim(bmap), pos, pow);
1258         } else if (tok->type == '[') {
1259                 isl_div *div;
1260                 int pow;
1261
1262                 isl_stream_push_token(s, tok);
1263                 div = read_div(s, bmap, v);
1264                 pow = optional_power(s);
1265                 qp = isl_qpolynomial_div_pow(div, pow);
1266         } else if (tok->type == '-') {
1267                 struct isl_qpolynomial *qp2;
1268
1269                 isl_token_free(tok);
1270                 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1271                                             s->ctx->negone);
1272                 qp2 = read_factor(s, bmap, v);
1273                 qp = isl_qpolynomial_mul(qp, qp2);
1274         } else {
1275                 isl_stream_error(s, tok, "unexpected isl_token");
1276                 isl_stream_push_token(s, tok);
1277                 return NULL;
1278         }
1279
1280         if (isl_stream_eat_if_available(s, '*') ||
1281             isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1282                 struct isl_qpolynomial *qp2;
1283
1284                 qp2 = read_factor(s, bmap, v);
1285                 qp = isl_qpolynomial_mul(qp, qp2);
1286         }
1287
1288         return qp;
1289 error:
1290         isl_qpolynomial_free(qp);
1291         return NULL;
1292 }
1293
1294 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1295         __isl_keep isl_basic_map *bmap, struct vars *v)
1296 {
1297         struct isl_token *tok;
1298         struct isl_qpolynomial *qp;
1299
1300         qp = read_factor(s, bmap, v);
1301
1302         for (;;) {
1303                 tok = isl_stream_next_token(s);
1304                 if (!tok)
1305                         return qp;
1306
1307                 if (tok->type == '+') {
1308                         struct isl_qpolynomial *qp2;
1309
1310                         isl_token_free(tok);
1311                         qp2 = read_factor(s, bmap, v);
1312                         qp = isl_qpolynomial_add(qp, qp2);
1313                 } else if (tok->type == '-') {
1314                         struct isl_qpolynomial *qp2;
1315
1316                         isl_token_free(tok);
1317                         qp2 = read_factor(s, bmap, v);
1318                         qp = isl_qpolynomial_sub(qp, qp2);
1319                 } else if (tok->type == ISL_TOKEN_VALUE &&
1320                             isl_int_is_neg(tok->u.v)) {
1321                         struct isl_qpolynomial *qp2;
1322
1323                         isl_stream_push_token(s, tok);
1324                         qp2 = read_factor(s, bmap, v);
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(isl_fold_max, 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 }