ce9b04be63c14da3c4b80e866c6e7f1ab3dc3951
[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
830                 cond = isl_pw_aff_list_eq_set(isl_pw_aff_list_copy(left),
831                                               isl_pw_aff_list_copy(right));
832
833         return isl_set_intersect(set, cond);
834 }
835
836 static int is_comparator(struct isl_token *tok)
837 {
838         if (!tok)
839                 return 0;
840
841         switch (tok->type) {
842         case ISL_TOKEN_LT:
843         case ISL_TOKEN_GT:
844         case ISL_TOKEN_LE:
845         case ISL_TOKEN_GE:
846         case '=':
847                 return 1;
848         default:
849                 return 0;
850         }
851 }
852
853 static __isl_give isl_map *add_constraint(struct isl_stream *s,
854         struct vars *v, __isl_take isl_map *map)
855 {
856         struct isl_token *tok = NULL;
857         isl_pw_aff_list *list1 = NULL, *list2 = NULL;
858         isl_set *set;
859
860         set = isl_map_wrap(map);
861         list1 = accept_affine_list(s, isl_set_get_dim(set), v);
862         if (!list1)
863                 goto error;
864         tok = isl_stream_next_token(s);
865         if (!is_comparator(tok)) {
866                 isl_stream_error(s, tok, "missing operator");
867                 if (tok)
868                         isl_stream_push_token(s, tok);
869                 tok = NULL;
870                 goto error;
871         }
872         for (;;) {
873                 list2 = accept_affine_list(s, isl_set_get_dim(set), v);
874                 if (!list2)
875                         goto error;
876
877                 set = construct_constraints(set, tok->type, list1, list2);
878                 isl_token_free(tok);
879                 isl_pw_aff_list_free(list1);
880                 list1 = list2;
881
882                 tok = isl_stream_next_token(s);
883                 if (!is_comparator(tok)) {
884                         if (tok)
885                                 isl_stream_push_token(s, tok);
886                         break;
887                 }
888         }
889         isl_pw_aff_list_free(list1);
890
891         return isl_set_unwrap(set);
892 error:
893         if (tok)
894                 isl_token_free(tok);
895         isl_pw_aff_list_free(list1);
896         isl_pw_aff_list_free(list2);
897         isl_set_free(set);
898         return NULL;
899 }
900
901 static struct isl_map *read_disjuncts(struct isl_stream *s,
902         struct vars *v, __isl_take isl_map *map);
903
904 static __isl_give isl_map *read_exists(struct isl_stream *s,
905         struct vars *v, __isl_take isl_map *map)
906 {
907         int n = v->n;
908         int seen_paren = isl_stream_eat_if_available(s, '(');
909
910         map = isl_map_from_domain(isl_map_wrap(map));
911         map = read_defined_var_list(s, v, map);
912
913         if (isl_stream_eat(s, ':'))
914                 goto error;
915
916         map = read_disjuncts(s, v, map);
917         map = isl_set_unwrap(isl_map_domain(map));
918
919         vars_drop(v, v->n - n);
920         if (seen_paren && isl_stream_eat(s, ')'))
921                 goto error;
922
923         return map;
924 error:
925         isl_map_free(map);
926         return NULL;
927 }
928
929 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
930         struct vars *v, __isl_take isl_map *map)
931 {
932         if (isl_stream_eat_if_available(s, '(')) {
933                 map = read_disjuncts(s, v, map);
934                 if (isl_stream_eat(s, ')'))
935                         goto error;
936                 return map;
937         }
938
939         if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
940                 return read_exists(s, v, map);
941
942         if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
943                 return map;
944
945         if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
946                 isl_dim *dim = isl_map_get_dim(map);
947                 isl_map_free(map);
948                 return isl_map_empty(dim);
949         }
950                 
951         return add_constraint(s, v, map);
952 error:
953         isl_map_free(map);
954         return NULL;
955 }
956
957 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
958         struct vars *v, __isl_take isl_map *map)
959 {
960         isl_map *res;
961         int negate;
962
963         negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
964         res = read_conjunct(s, v, isl_map_copy(map));
965         if (negate)
966                 res = isl_map_subtract(isl_map_copy(map), res);
967
968         while (isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
969                 isl_map *res_i;
970
971                 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
972                 res_i = read_conjunct(s, v, isl_map_copy(map));
973                 if (negate)
974                         res = isl_map_subtract(res, res_i);
975                 else
976                         res = isl_map_intersect(res, res_i);
977         }
978
979         isl_map_free(map);
980         return res;
981 }
982
983 static struct isl_map *read_disjuncts(struct isl_stream *s,
984         struct vars *v, __isl_take isl_map *map)
985 {
986         isl_map *res;
987
988         if (isl_stream_next_token_is(s, '}')) {
989                 isl_dim *dim = isl_map_get_dim(map);
990                 isl_map_free(map);
991                 return isl_map_universe(dim);
992         }
993
994         res = read_conjuncts(s, v, isl_map_copy(map));
995         while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
996                 isl_map *res_i;
997
998                 res_i = read_conjuncts(s, v, isl_map_copy(map));
999                 res = isl_map_union(res, res_i);
1000         }
1001
1002         isl_map_free(map);
1003         return res;
1004 }
1005
1006 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1007 {
1008         if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1009                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1010                            isl_basic_map_dim(bmap, isl_dim_in) + pos;
1011         pos -= isl_basic_map_dim(bmap, isl_dim_out);
1012
1013         if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1014                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1015         pos -= isl_basic_map_dim(bmap, isl_dim_in);
1016
1017         if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1018                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1019                            isl_basic_map_dim(bmap, isl_dim_in) +
1020                            isl_basic_map_dim(bmap, isl_dim_out) + pos;
1021         pos -= isl_basic_map_dim(bmap, isl_dim_div);
1022
1023         if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1024                 return 1 + pos;
1025
1026         return 0;
1027 }
1028
1029 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1030         struct isl_stream *s, __isl_take isl_basic_map *bmap)
1031 {
1032         int j;
1033         struct isl_token *tok;
1034         int type;
1035         int k;
1036         isl_int *c;
1037         unsigned nparam;
1038         unsigned dim;
1039
1040         if (!bmap)
1041                 return NULL;
1042
1043         nparam = isl_basic_map_dim(bmap, isl_dim_param);
1044         dim = isl_basic_map_dim(bmap, isl_dim_out);
1045
1046         tok = isl_stream_next_token(s);
1047         if (!tok || tok->type != ISL_TOKEN_VALUE) {
1048                 isl_stream_error(s, tok, "expecting coefficient");
1049                 if (tok)
1050                         isl_stream_push_token(s, tok);
1051                 goto error;
1052         }
1053         if (!tok->on_new_line) {
1054                 isl_stream_error(s, tok, "coefficient should appear on new line");
1055                 isl_stream_push_token(s, tok);
1056                 goto error;
1057         }
1058
1059         type = isl_int_get_si(tok->u.v);
1060         isl_token_free(tok);
1061
1062         isl_assert(s->ctx, type == 0 || type == 1, goto error);
1063         if (type == 0) {
1064                 k = isl_basic_map_alloc_equality(bmap);
1065                 c = bmap->eq[k];
1066         } else {
1067                 k = isl_basic_map_alloc_inequality(bmap);
1068                 c = bmap->ineq[k];
1069         }
1070         if (k < 0)
1071                 goto error;
1072
1073         for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1074                 int pos;
1075                 tok = isl_stream_next_token(s);
1076                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1077                         isl_stream_error(s, tok, "expecting coefficient");
1078                         if (tok)
1079                                 isl_stream_push_token(s, tok);
1080                         goto error;
1081                 }
1082                 if (tok->on_new_line) {
1083                         isl_stream_error(s, tok,
1084                                 "coefficient should not appear on new line");
1085                         isl_stream_push_token(s, tok);
1086                         goto error;
1087                 }
1088                 pos = polylib_pos_to_isl_pos(bmap, j);
1089                 isl_int_set(c[pos], tok->u.v);
1090                 isl_token_free(tok);
1091         }
1092
1093         return bmap;
1094 error:
1095         isl_basic_map_free(bmap);
1096         return NULL;
1097 }
1098
1099 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1100         int nparam)
1101 {
1102         int i;
1103         struct isl_token *tok;
1104         struct isl_token *tok2;
1105         int n_row, n_col;
1106         int on_new_line;
1107         unsigned in = 0, out, local = 0;
1108         struct isl_basic_map *bmap = NULL;
1109
1110         if (nparam < 0)
1111                 nparam = 0;
1112
1113         tok = isl_stream_next_token(s);
1114         if (!tok) {
1115                 isl_stream_error(s, NULL, "unexpected EOF");
1116                 return NULL;
1117         }
1118         tok2 = isl_stream_next_token(s);
1119         if (!tok2) {
1120                 isl_token_free(tok);
1121                 isl_stream_error(s, NULL, "unexpected EOF");
1122                 return NULL;
1123         }
1124         if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1125                 isl_stream_push_token(s, tok2);
1126                 isl_stream_push_token(s, tok);
1127                 isl_stream_error(s, NULL,
1128                                  "expecting constraint matrix dimensions");
1129                 return NULL;
1130         }
1131         n_row = isl_int_get_si(tok->u.v);
1132         n_col = isl_int_get_si(tok2->u.v);
1133         on_new_line = tok2->on_new_line;
1134         isl_token_free(tok2);
1135         isl_token_free(tok);
1136         isl_assert(s->ctx, !on_new_line, return NULL);
1137         isl_assert(s->ctx, n_row >= 0, return NULL);
1138         isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1139         tok = isl_stream_next_token_on_same_line(s);
1140         if (tok) {
1141                 if (tok->type != ISL_TOKEN_VALUE) {
1142                         isl_stream_error(s, tok,
1143                                     "expecting number of output dimensions");
1144                         isl_stream_push_token(s, tok);
1145                         goto error;
1146                 }
1147                 out = isl_int_get_si(tok->u.v);
1148                 isl_token_free(tok);
1149
1150                 tok = isl_stream_next_token_on_same_line(s);
1151                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1152                         isl_stream_error(s, tok,
1153                                     "expecting number of input dimensions");
1154                         if (tok)
1155                                 isl_stream_push_token(s, tok);
1156                         goto error;
1157                 }
1158                 in = isl_int_get_si(tok->u.v);
1159                 isl_token_free(tok);
1160
1161                 tok = isl_stream_next_token_on_same_line(s);
1162                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1163                         isl_stream_error(s, tok,
1164                                     "expecting number of existentials");
1165                         if (tok)
1166                                 isl_stream_push_token(s, tok);
1167                         goto error;
1168                 }
1169                 local = isl_int_get_si(tok->u.v);
1170                 isl_token_free(tok);
1171
1172                 tok = isl_stream_next_token_on_same_line(s);
1173                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1174                         isl_stream_error(s, tok,
1175                                     "expecting number of parameters");
1176                         if (tok)
1177                                 isl_stream_push_token(s, tok);
1178                         goto error;
1179                 }
1180                 nparam = isl_int_get_si(tok->u.v);
1181                 isl_token_free(tok);
1182                 if (n_col != 1 + out + in + local + nparam + 1) {
1183                         isl_stream_error(s, NULL,
1184                                     "dimensions don't match");
1185                         goto error;
1186                 }
1187         } else
1188                 out = n_col - 2 - nparam;
1189         bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1190         if (!bmap)
1191                 return NULL;
1192
1193         for (i = 0; i < local; ++i) {
1194                 int k = isl_basic_map_alloc_div(bmap);
1195                 if (k < 0)
1196                         goto error;
1197                 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1198         }
1199
1200         for (i = 0; i < n_row; ++i)
1201                 bmap = basic_map_read_polylib_constraint(s, bmap);
1202
1203         tok = isl_stream_next_token_on_same_line(s);
1204         if (tok) {
1205                 isl_stream_error(s, tok, "unexpected extra token on line");
1206                 isl_stream_push_token(s, tok);
1207                 goto error;
1208         }
1209
1210         bmap = isl_basic_map_simplify(bmap);
1211         bmap = isl_basic_map_finalize(bmap);
1212         return bmap;
1213 error:
1214         isl_basic_map_free(bmap);
1215         return NULL;
1216 }
1217
1218 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1219 {
1220         struct isl_token *tok;
1221         struct isl_token *tok2;
1222         int i, n;
1223         struct isl_map *map;
1224
1225         tok = isl_stream_next_token(s);
1226         if (!tok) {
1227                 isl_stream_error(s, NULL, "unexpected EOF");
1228                 return NULL;
1229         }
1230         tok2 = isl_stream_next_token_on_same_line(s);
1231         if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1232                 isl_stream_push_token(s, tok2);
1233                 isl_stream_push_token(s, tok);
1234                 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1235         }
1236         if (tok2) {
1237                 isl_stream_error(s, tok2, "unexpected token");
1238                 isl_stream_push_token(s, tok2);
1239                 isl_stream_push_token(s, tok);
1240                 return NULL;
1241         }
1242         n = isl_int_get_si(tok->u.v);
1243         isl_token_free(tok);
1244
1245         isl_assert(s->ctx, n >= 1, return NULL);
1246
1247         map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1248
1249         for (i = 1; map && i < n; ++i)
1250                 map = isl_map_union(map,
1251                         isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1252
1253         return map;
1254 }
1255
1256 static int optional_power(struct isl_stream *s)
1257 {
1258         int pow;
1259         struct isl_token *tok;
1260
1261         tok = isl_stream_next_token(s);
1262         if (!tok)
1263                 return 1;
1264         if (tok->type != '^') {
1265                 isl_stream_push_token(s, tok);
1266                 return 1;
1267         }
1268         isl_token_free(tok);
1269         tok = isl_stream_next_token(s);
1270         if (!tok || tok->type != ISL_TOKEN_VALUE) {
1271                 isl_stream_error(s, tok, "expecting exponent");
1272                 if (tok)
1273                         isl_stream_push_token(s, tok);
1274                 return 1;
1275         }
1276         pow = isl_int_get_si(tok->u.v);
1277         isl_token_free(tok);
1278         return pow;
1279 }
1280
1281 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1282         __isl_keep isl_map *map, struct vars *v);
1283
1284 static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
1285         __isl_keep isl_map *map, struct vars *v)
1286 {
1287         isl_pw_qpolynomial *pwqp;
1288         struct isl_token *tok;
1289
1290         tok = next_token(s);
1291         if (!tok) {
1292                 isl_stream_error(s, NULL, "unexpected EOF");
1293                 return NULL;
1294         }
1295         if (tok->type == '(') {
1296                 int pow;
1297
1298                 isl_token_free(tok);
1299                 pwqp = read_term(s, map, v);
1300                 if (!pwqp)
1301                         return NULL;
1302                 if (isl_stream_eat(s, ')'))
1303                         goto error;
1304                 pow = optional_power(s);
1305                 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1306         } else if (tok->type == ISL_TOKEN_VALUE) {
1307                 struct isl_token *tok2;
1308                 tok2 = isl_stream_next_token(s);
1309                 isl_qpolynomial *qp;
1310                 if (tok2 && tok2->type == '/') {
1311                         isl_token_free(tok2);
1312                         tok2 = next_token(s);
1313                         if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1314                                 isl_stream_error(s, tok2, "expected denominator");
1315                                 isl_token_free(tok);
1316                                 isl_token_free(tok2);
1317                                 return NULL;
1318                         }
1319                         qp = isl_qpolynomial_rat_cst(isl_map_get_dim(map),
1320                                                     tok->u.v, tok2->u.v);
1321                         isl_token_free(tok2);
1322                 } else {
1323                         isl_stream_push_token(s, tok2);
1324                         qp = isl_qpolynomial_cst(isl_map_get_dim(map),
1325                                                 tok->u.v);
1326                 }
1327                 isl_token_free(tok);
1328                 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1329         } else if (tok->type == ISL_TOKEN_INFTY) {
1330                 isl_qpolynomial *qp;
1331                 isl_token_free(tok);
1332                 qp = isl_qpolynomial_infty(isl_map_get_dim(map));
1333                 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1334         } else if (tok->type == ISL_TOKEN_NAN) {
1335                 isl_qpolynomial *qp;
1336                 isl_token_free(tok);
1337                 qp = isl_qpolynomial_nan(isl_map_get_dim(map));
1338                 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1339         } else if (tok->type == ISL_TOKEN_IDENT) {
1340                 int n = v->n;
1341                 int pos = vars_pos(v, tok->u.s, -1);
1342                 int pow;
1343                 isl_qpolynomial *qp;
1344                 if (pos < 0) {
1345                         isl_token_free(tok);
1346                         return NULL;
1347                 }
1348                 if (pos >= n) {
1349                         vars_drop(v, v->n - n);
1350                         isl_stream_error(s, tok, "unknown identifier");
1351                         isl_token_free(tok);
1352                         return NULL;
1353                 }
1354                 isl_token_free(tok);
1355                 pow = optional_power(s);
1356                 qp = isl_qpolynomial_var_pow(isl_map_get_dim(map), pos, pow);
1357                 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1358         } else if (tok->type == '[') {
1359                 isl_pw_aff *pwaff;
1360                 int pow;
1361
1362                 isl_stream_push_token(s, tok);
1363                 pwaff = accept_affine(s, isl_map_get_dim(map), v);
1364                 pow = optional_power(s);
1365                 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
1366                 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1367         } else if (tok->type == '-') {
1368                 isl_token_free(tok);
1369                 pwqp = read_factor(s, map, v);
1370                 pwqp = isl_pw_qpolynomial_neg(pwqp);
1371         } else {
1372                 isl_stream_error(s, tok, "unexpected isl_token");
1373                 isl_stream_push_token(s, tok);
1374                 return NULL;
1375         }
1376
1377         if (isl_stream_eat_if_available(s, '*') ||
1378             isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1379                 isl_pw_qpolynomial *pwqp2;
1380
1381                 pwqp2 = read_factor(s, map, v);
1382                 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
1383         }
1384
1385         return pwqp;
1386 error:
1387         isl_pw_qpolynomial_free(pwqp);
1388         return NULL;
1389 }
1390
1391 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1392         __isl_keep isl_map *map, struct vars *v)
1393 {
1394         struct isl_token *tok;
1395         isl_pw_qpolynomial *pwqp;
1396
1397         pwqp = read_factor(s, map, v);
1398
1399         for (;;) {
1400                 tok = next_token(s);
1401                 if (!tok)
1402                         return pwqp;
1403
1404                 if (tok->type == '+') {
1405                         isl_pw_qpolynomial *pwqp2;
1406
1407                         isl_token_free(tok);
1408                         pwqp2 = read_factor(s, map, v);
1409                         pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1410                 } else if (tok->type == '-') {
1411                         isl_pw_qpolynomial *pwqp2;
1412
1413                         isl_token_free(tok);
1414                         pwqp2 = read_factor(s, map, v);
1415                         pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
1416                 } else if (tok->type == ISL_TOKEN_VALUE &&
1417                             isl_int_is_neg(tok->u.v)) {
1418                         isl_pw_qpolynomial *pwqp2;
1419
1420                         isl_stream_push_token(s, tok);
1421                         pwqp2 = read_factor(s, map, v);
1422                         pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1423                 } else {
1424                         isl_stream_push_token(s, tok);
1425                         break;
1426                 }
1427         }
1428
1429         return pwqp;
1430 }
1431
1432 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1433         __isl_take isl_map *map, struct vars *v)
1434 {
1435         struct isl_token *tok;
1436
1437         tok = isl_stream_next_token(s);
1438         if (!tok) {
1439                 isl_stream_error(s, NULL, "unexpected EOF");
1440                 goto error;
1441         }
1442         if (tok->type == ':' ||
1443             (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1444                 isl_token_free(tok);
1445                 map = read_disjuncts(s, v, map);
1446         } else
1447                 isl_stream_push_token(s, tok);
1448
1449         return map;
1450 error:
1451         isl_map_free(map);
1452         return NULL;
1453 }
1454
1455 static struct isl_obj obj_read_poly(struct isl_stream *s,
1456         __isl_take isl_map *map, struct vars *v, int n)
1457 {
1458         struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1459         isl_pw_qpolynomial *pwqp;
1460         struct isl_set *set;
1461
1462         pwqp = read_term(s, map, v);
1463         map = read_optional_disjuncts(s, map, v);
1464         set = isl_map_range(map);
1465
1466         pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
1467
1468         vars_drop(v, v->n - n);
1469
1470         obj.v = pwqp;
1471         return obj;
1472 }
1473
1474 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1475         __isl_take isl_map *map, struct vars *v, int n)
1476 {
1477         struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1478         isl_pw_qpolynomial *pwqp;
1479         isl_pw_qpolynomial_fold *pwf = NULL;
1480         isl_set *set;
1481
1482         if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1483                 return obj_read_poly(s, map, v, n);
1484
1485         if (isl_stream_eat(s, '('))
1486                 goto error;
1487
1488         pwqp = read_term(s, map, v);
1489         pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1490
1491         while (isl_stream_eat_if_available(s, ',')) {
1492                 isl_pw_qpolynomial_fold *pwf_i;
1493                 pwqp = read_term(s, map, v);
1494                 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1495                                                                         pwqp);
1496                 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1497         }
1498
1499         if (isl_stream_eat(s, ')'))
1500                 goto error;
1501
1502         map = read_optional_disjuncts(s, map, v);
1503         set = isl_map_range(map);
1504         pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1505
1506         vars_drop(v, v->n - n);
1507
1508         obj.v = pwf;
1509         return obj;
1510 error:
1511         isl_map_free(map);
1512         isl_pw_qpolynomial_fold_free(pwf);
1513         obj.type = isl_obj_none;
1514         return obj;
1515 }
1516
1517 static int is_rational(struct isl_stream *s)
1518 {
1519         struct isl_token *tok;
1520
1521         tok = isl_stream_next_token(s);
1522         if (!tok)
1523                 return 0;
1524         if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1525                 isl_token_free(tok);
1526                 isl_stream_eat(s, ':');
1527                 return 1;
1528         }
1529
1530         isl_stream_push_token(s, tok);
1531
1532         return 0;
1533 }
1534
1535 static struct isl_obj obj_read_body(struct isl_stream *s,
1536         __isl_take isl_map *map, struct vars *v)
1537 {
1538         struct isl_token *tok;
1539         struct isl_obj obj = { isl_obj_set, NULL };
1540         int n = v->n;
1541
1542         if (is_rational(s))
1543                 map = isl_map_set_rational(map);
1544
1545         if (!next_is_tuple(s))
1546                 return obj_read_poly_or_fold(s, map, v, n);
1547
1548         map = read_tuple(s, map, isl_dim_in, v);
1549         if (!map)
1550                 goto error;
1551         tok = isl_stream_next_token(s);
1552         if (tok && tok->type == ISL_TOKEN_TO) {
1553                 obj.type = isl_obj_map;
1554                 isl_token_free(tok);
1555                 if (!next_is_tuple(s)) {
1556                         map = isl_map_reverse(map);
1557                         return obj_read_poly_or_fold(s, map, v, n);
1558                 }
1559                 map = read_tuple(s, map, isl_dim_out, v);
1560                 if (!map)
1561                         goto error;
1562         } else {
1563                 map = isl_map_reverse(map);
1564                 if (tok)
1565                         isl_stream_push_token(s, tok);
1566         }
1567
1568         map = read_optional_disjuncts(s, map, v);
1569
1570         vars_drop(v, v->n - n);
1571
1572         obj.v = map;
1573         return obj;
1574 error:
1575         isl_map_free(map);
1576         obj.type = isl_obj_none;
1577         return obj;
1578 }
1579
1580 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1581 {
1582         if (obj.type == isl_obj_map) {
1583                 obj.v = isl_union_map_from_map(obj.v);
1584                 obj.type = isl_obj_union_map;
1585         } else if (obj.type == isl_obj_set) {
1586                 obj.v = isl_union_set_from_set(obj.v);
1587                 obj.type = isl_obj_union_set;
1588         } else if (obj.type == isl_obj_pw_qpolynomial) {
1589                 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1590                 obj.type = isl_obj_union_pw_qpolynomial;
1591         } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1592                 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1593                 obj.type = isl_obj_union_pw_qpolynomial_fold;
1594         } else
1595                 isl_assert(ctx, 0, goto error);
1596         return obj;
1597 error:
1598         obj.type->free(obj.v);
1599         obj.type = isl_obj_none;
1600         return obj;
1601 }
1602
1603 static struct isl_obj obj_add(struct isl_ctx *ctx,
1604         struct isl_obj obj1, struct isl_obj obj2)
1605 {
1606         if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1607                 obj1 = to_union(ctx, obj1);
1608         if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1609                 obj2 = to_union(ctx, obj2);
1610         if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1611                 obj1 = to_union(ctx, obj1);
1612         if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1613                 obj2 = to_union(ctx, obj2);
1614         if (obj1.type == isl_obj_pw_qpolynomial &&
1615             obj2.type == isl_obj_union_pw_qpolynomial)
1616                 obj1 = to_union(ctx, obj1);
1617         if (obj1.type == isl_obj_union_pw_qpolynomial &&
1618             obj2.type == isl_obj_pw_qpolynomial)
1619                 obj2 = to_union(ctx, obj2);
1620         if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1621             obj2.type == isl_obj_union_pw_qpolynomial_fold)
1622                 obj1 = to_union(ctx, obj1);
1623         if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1624             obj2.type == isl_obj_pw_qpolynomial_fold)
1625                 obj2 = to_union(ctx, obj2);
1626         isl_assert(ctx, obj1.type == obj2.type, goto error);
1627         if (obj1.type == isl_obj_map && !isl_map_has_equal_dim(obj1.v, obj2.v)) {
1628                 obj1 = to_union(ctx, obj1);
1629                 obj2 = to_union(ctx, obj2);
1630         }
1631         if (obj1.type == isl_obj_set && !isl_set_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_pw_qpolynomial &&
1636             !isl_pw_qpolynomial_has_equal_dim(obj1.v, obj2.v)) {
1637                 obj1 = to_union(ctx, obj1);
1638                 obj2 = to_union(ctx, obj2);
1639         }
1640         if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1641             !isl_pw_qpolynomial_fold_has_equal_dim(obj1.v, obj2.v)) {
1642                 obj1 = to_union(ctx, obj1);
1643                 obj2 = to_union(ctx, obj2);
1644         }
1645         obj1.v = obj1.type->add(obj1.v, obj2.v);
1646         return obj1;
1647 error:
1648         obj1.type->free(obj1.v);
1649         obj2.type->free(obj2.v);
1650         obj1.type = isl_obj_none;
1651         obj1.v = NULL;
1652         return obj1;
1653 }
1654
1655 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1656 {
1657         isl_map *map = NULL;
1658         struct isl_token *tok;
1659         struct vars *v = NULL;
1660         struct isl_obj obj = { isl_obj_set, NULL };
1661
1662         tok = next_token(s);
1663         if (!tok) {
1664                 isl_stream_error(s, NULL, "unexpected EOF");
1665                 goto error;
1666         }
1667         if (tok->type == ISL_TOKEN_VALUE) {
1668                 struct isl_token *tok2;
1669                 struct isl_map *map;
1670
1671                 tok2 = isl_stream_next_token(s);
1672                 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
1673                     isl_int_is_neg(tok2->u.v)) {
1674                         if (tok2)
1675                                 isl_stream_push_token(s, tok2);
1676                         obj.type = isl_obj_int;
1677                         obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
1678                         isl_token_free(tok);
1679                         return obj;
1680                 }
1681                 isl_stream_push_token(s, tok2);
1682                 isl_stream_push_token(s, tok);
1683                 map = map_read_polylib(s, nparam);
1684                 if (!map)
1685                         goto error;
1686                 if (isl_map_dim(map, isl_dim_in) > 0)
1687                         obj.type = isl_obj_map;
1688                 obj.v = map;
1689                 return obj;
1690         }
1691         v = vars_new(s->ctx);
1692         if (!v) {
1693                 isl_stream_push_token(s, tok);
1694                 goto error;
1695         }
1696         map = isl_map_universe(isl_dim_alloc(s->ctx, 0, 0, 0));
1697         if (tok->type == '[') {
1698                 isl_stream_push_token(s, tok);
1699                 map = read_tuple(s, map, isl_dim_param, v);
1700                 if (!map)
1701                         goto error;
1702                 if (nparam >= 0)
1703                         isl_assert(s->ctx, nparam == v->n, goto error);
1704                 tok = isl_stream_next_token(s);
1705                 if (!tok || tok->type != ISL_TOKEN_TO) {
1706                         isl_stream_error(s, tok, "expecting '->'");
1707                         if (tok)
1708                                 isl_stream_push_token(s, tok);
1709                         goto error;
1710                 }
1711                 isl_token_free(tok);
1712                 tok = isl_stream_next_token(s);
1713         } else if (nparam > 0)
1714                 map = isl_map_add_dims(map, isl_dim_param, nparam);
1715         if (!tok || tok->type != '{') {
1716                 isl_stream_error(s, tok, "expecting '{'");
1717                 if (tok)
1718                         isl_stream_push_token(s, tok);
1719                 goto error;
1720         }
1721         isl_token_free(tok);
1722
1723         tok = isl_stream_next_token(s);
1724         if (!tok)
1725                 ;
1726         else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1727                 isl_token_free(tok);
1728                 if (isl_stream_eat(s, '='))
1729                         goto error;
1730                 map = read_tuple(s, map, isl_dim_param, v);
1731                 if (!map)
1732                         goto error;
1733                 if (nparam >= 0)
1734                         isl_assert(s->ctx, nparam == v->n, goto error);
1735         } else if (tok->type == '}') {
1736                 obj.type = isl_obj_union_set;
1737                 obj.v = isl_union_set_empty(isl_map_get_dim(map));
1738                 isl_token_free(tok);
1739                 goto done;
1740         } else
1741                 isl_stream_push_token(s, tok);
1742
1743         for (;;) {
1744                 struct isl_obj o;
1745                 tok = NULL;
1746                 o = obj_read_body(s, isl_map_copy(map), v);
1747                 if (o.type == isl_obj_none || !o.v)
1748                         goto error;
1749                 if (!obj.v)
1750                         obj = o;
1751                 else {
1752                         obj = obj_add(s->ctx, obj, o);
1753                         if (obj.type == isl_obj_none || !obj.v)
1754                                 goto error;
1755                 }
1756                 tok = isl_stream_next_token(s);
1757                 if (!tok || tok->type != ';')
1758                         break;
1759                 isl_token_free(tok);
1760                 if (isl_stream_next_token_is(s, '}')) {
1761                         tok = isl_stream_next_token(s);
1762                         break;
1763                 }
1764         }
1765
1766         if (tok && tok->type == '}') {
1767                 isl_token_free(tok);
1768         } else {
1769                 isl_stream_error(s, tok, "unexpected isl_token");
1770                 if (tok)
1771                         isl_token_free(tok);
1772                 goto error;
1773         }
1774 done:
1775         vars_free(v);
1776         isl_map_free(map);
1777
1778         return obj;
1779 error:
1780         isl_map_free(map);
1781         obj.type->free(obj.v);
1782         if (v)
1783                 vars_free(v);
1784         obj.v = NULL;
1785         return obj;
1786 }
1787
1788 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1789 {
1790         return obj_read(s, -1);
1791 }
1792
1793 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1794 {
1795         struct isl_obj obj;
1796
1797         obj = obj_read(s, nparam);
1798         if (obj.v)
1799                 isl_assert(s->ctx, obj.type == isl_obj_map ||
1800                                    obj.type == isl_obj_set, goto error);
1801
1802         return obj.v;
1803 error:
1804         obj.type->free(obj.v);
1805         return NULL;
1806 }
1807
1808 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
1809 {
1810         struct isl_obj obj;
1811
1812         obj = obj_read(s, nparam);
1813         if (obj.v)
1814                 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
1815
1816         return obj.v;
1817 error:
1818         obj.type->free(obj.v);
1819         return NULL;
1820 }
1821
1822 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
1823 {
1824         struct isl_obj obj;
1825
1826         obj = obj_read(s, -1);
1827         if (obj.type == isl_obj_map) {
1828                 obj.type = isl_obj_union_map;
1829                 obj.v = isl_union_map_from_map(obj.v);
1830         }
1831         if (obj.type == isl_obj_set) {
1832                 obj.type = isl_obj_union_set;
1833                 obj.v = isl_union_set_from_set(obj.v);
1834         }
1835         if (obj.v)
1836                 isl_assert(s->ctx, obj.type == isl_obj_union_map ||
1837                                    obj.type == isl_obj_union_set, goto error);
1838
1839         return obj.v;
1840 error:
1841         obj.type->free(obj.v);
1842         return NULL;
1843 }
1844
1845 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
1846 {
1847         struct isl_obj obj;
1848
1849         obj = obj_read(s, -1);
1850         if (obj.type == isl_obj_set) {
1851                 obj.type = isl_obj_union_set;
1852                 obj.v = isl_union_set_from_set(obj.v);
1853         }
1854         if (obj.v)
1855                 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1856
1857         return obj.v;
1858 error:
1859         obj.type->free(obj.v);
1860         return NULL;
1861 }
1862
1863 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
1864 {
1865         struct isl_obj obj;
1866         struct isl_map *map;
1867         struct isl_basic_map *bmap;
1868
1869         obj = obj_read(s, nparam);
1870         map = obj.v;
1871         if (!map)
1872                 return NULL;
1873
1874         isl_assert(map->ctx, map->n <= 1, goto error);
1875
1876         if (map->n == 0)
1877                 bmap = isl_basic_map_empty_like_map(map);
1878         else
1879                 bmap = isl_basic_map_copy(map->p[0]);
1880
1881         isl_map_free(map);
1882
1883         return bmap;
1884 error:
1885         isl_map_free(map);
1886         return NULL;
1887 }
1888
1889 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
1890                 FILE *input, int nparam)
1891 {
1892         struct isl_basic_map *bmap;
1893         struct isl_stream *s = isl_stream_new_file(ctx, input);
1894         if (!s)
1895                 return NULL;
1896         bmap = basic_map_read(s, nparam);
1897         isl_stream_free(s);
1898         return bmap;
1899 }
1900
1901 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
1902                 FILE *input, int nparam)
1903 {
1904         struct isl_basic_map *bmap;
1905         bmap = isl_basic_map_read_from_file(ctx, input, nparam);
1906         if (!bmap)
1907                 return NULL;
1908         isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1909         return (struct isl_basic_set *)bmap;
1910 error:
1911         isl_basic_map_free(bmap);
1912         return NULL;
1913 }
1914
1915 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
1916                 const char *str, int nparam)
1917 {
1918         struct isl_basic_map *bmap;
1919         struct isl_stream *s = isl_stream_new_str(ctx, str);
1920         if (!s)
1921                 return NULL;
1922         bmap = basic_map_read(s, nparam);
1923         isl_stream_free(s);
1924         return bmap;
1925 }
1926
1927 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
1928                 const char *str, int nparam)
1929 {
1930         struct isl_basic_map *bmap;
1931         bmap = isl_basic_map_read_from_str(ctx, str, nparam);
1932         if (!bmap)
1933                 return NULL;
1934         isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1935         return (struct isl_basic_set *)bmap;
1936 error:
1937         isl_basic_map_free(bmap);
1938         return NULL;
1939 }
1940
1941 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
1942                 FILE *input, int nparam)
1943 {
1944         struct isl_map *map;
1945         struct isl_stream *s = isl_stream_new_file(ctx, input);
1946         if (!s)
1947                 return NULL;
1948         map = isl_stream_read_map(s, nparam);
1949         isl_stream_free(s);
1950         return map;
1951 }
1952
1953 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
1954                 const char *str, int nparam)
1955 {
1956         struct isl_map *map;
1957         struct isl_stream *s = isl_stream_new_str(ctx, str);
1958         if (!s)
1959                 return NULL;
1960         map = isl_stream_read_map(s, nparam);
1961         isl_stream_free(s);
1962         return map;
1963 }
1964
1965 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
1966                 FILE *input, int nparam)
1967 {
1968         struct isl_map *map;
1969         map = isl_map_read_from_file(ctx, input, nparam);
1970         if (!map)
1971                 return NULL;
1972         isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1973         return (struct isl_set *)map;
1974 error:
1975         isl_map_free(map);
1976         return NULL;
1977 }
1978
1979 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
1980                 const char *str, int nparam)
1981 {
1982         struct isl_map *map;
1983         map = isl_map_read_from_str(ctx, str, nparam);
1984         if (!map)
1985                 return NULL;
1986         isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1987         return (struct isl_set *)map;
1988 error:
1989         isl_map_free(map);
1990         return NULL;
1991 }
1992
1993 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
1994         FILE *input)
1995 {
1996         isl_union_map *umap;
1997         struct isl_stream *s = isl_stream_new_file(ctx, input);
1998         if (!s)
1999                 return NULL;
2000         umap = isl_stream_read_union_map(s);
2001         isl_stream_free(s);
2002         return umap;
2003 }
2004
2005 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2006                 const char *str)
2007 {
2008         isl_union_map *umap;
2009         struct isl_stream *s = isl_stream_new_str(ctx, str);
2010         if (!s)
2011                 return NULL;
2012         umap = isl_stream_read_union_map(s);
2013         isl_stream_free(s);
2014         return umap;
2015 }
2016
2017 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2018         FILE *input)
2019 {
2020         isl_union_set *uset;
2021         struct isl_stream *s = isl_stream_new_file(ctx, input);
2022         if (!s)
2023                 return NULL;
2024         uset = isl_stream_read_union_set(s);
2025         isl_stream_free(s);
2026         return uset;
2027 }
2028
2029 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2030                 const char *str)
2031 {
2032         isl_union_set *uset;
2033         struct isl_stream *s = isl_stream_new_str(ctx, str);
2034         if (!s)
2035                 return NULL;
2036         uset = isl_stream_read_union_set(s);
2037         isl_stream_free(s);
2038         return uset;
2039 }
2040
2041 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2042 {
2043         struct isl_vec *vec = NULL;
2044         struct isl_token *tok;
2045         unsigned size;
2046         int j;
2047
2048         tok = isl_stream_next_token(s);
2049         if (!tok || tok->type != ISL_TOKEN_VALUE) {
2050                 isl_stream_error(s, tok, "expecting vector length");
2051                 goto error;
2052         }
2053
2054         size = isl_int_get_si(tok->u.v);
2055         isl_token_free(tok);
2056
2057         vec = isl_vec_alloc(s->ctx, size);
2058
2059         for (j = 0; j < size; ++j) {
2060                 tok = isl_stream_next_token(s);
2061                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2062                         isl_stream_error(s, tok, "expecting constant value");
2063                         goto error;
2064                 }
2065                 isl_int_set(vec->el[j], tok->u.v);
2066                 isl_token_free(tok);
2067         }
2068
2069         return vec;
2070 error:
2071         isl_token_free(tok);
2072         isl_vec_free(vec);
2073         return NULL;
2074 }
2075
2076 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2077 {
2078         return isl_vec_read_polylib(s);
2079 }
2080
2081 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2082 {
2083         isl_vec *v;
2084         struct isl_stream *s = isl_stream_new_file(ctx, input);
2085         if (!s)
2086                 return NULL;
2087         v = vec_read(s);
2088         isl_stream_free(s);
2089         return v;
2090 }
2091
2092 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2093         struct isl_stream *s)
2094 {
2095         struct isl_obj obj;
2096
2097         obj = obj_read(s, -1);
2098         if (obj.v)
2099                 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2100                            goto error);
2101
2102         return obj.v;
2103 error:
2104         obj.type->free(obj.v);
2105         return NULL;
2106 }
2107
2108 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2109                 const char *str)
2110 {
2111         isl_pw_qpolynomial *pwqp;
2112         struct isl_stream *s = isl_stream_new_str(ctx, str);
2113         if (!s)
2114                 return NULL;
2115         pwqp = isl_stream_read_pw_qpolynomial(s);
2116         isl_stream_free(s);
2117         return pwqp;
2118 }
2119
2120 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2121                 FILE *input)
2122 {
2123         isl_pw_qpolynomial *pwqp;
2124         struct isl_stream *s = isl_stream_new_file(ctx, input);
2125         if (!s)
2126                 return NULL;
2127         pwqp = isl_stream_read_pw_qpolynomial(s);
2128         isl_stream_free(s);
2129         return pwqp;
2130 }