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